]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.c
55201f0fd8e85ee8b181c981d7abbe049797e746
[flightgear.git] / FixObj / obj.c
1 /**************************************************************************
2  * obj.c -- routines to handle WaveFront .obj format files.
3  *
4  * Written by Curtis Olson, started October 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "obj.h"
31
32 #include "../../Src/Math/mat3.h"
33
34
35 /* what do ya' know, here's some global variables */
36 float nodes[MAXNODES][3];
37 float normals[MAXNODES][3];
38
39 int ccw_list[MAXNODES];
40 int ccw_list_ptr;
41
42 int cw_list[MAXNODES];
43 int cw_list_ptr;
44
45 FILE *in, *out;
46
47
48 /* some simple list routines */
49
50 /* reset the list */
51 void list_init(int *list_ptr) {
52     *list_ptr = 0;
53 }
54
55
56 /* add to list */
57 void list_add(int *list, int *list_ptr, int node) {
58     if ( *list_ptr >= MAXNODES ) {
59         printf("ERROR: list overflow in list_add()\n");
60         exit(-1);
61     }
62
63     list[*list_ptr] = node;
64     *list_ptr += 1;
65
66     printf("list pointer = %d  adding %d\n", *list_ptr, node); 
67 }
68
69
70 /* dump list */
71 void dump_list(int *list, int list_ptr) {
72     int i;
73
74     if ( list_ptr < 3 ) {
75         printf("List is empty ... skipping\n");
76         return;
77     }
78
79     printf("Dumping list, size = %d\n", list_ptr);
80
81     i = 0;
82     while ( i < list_ptr ) { 
83         /* do next strip */
84
85         /* dump header */
86         fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
87         printf("t %d %d %d\n", list[i], list[i+1], list[i+2]);
88         i += 3;
89
90         /* dump rest of strip (until -1) */
91         while ( (i < list_ptr) && (list[i] != -1) ) { 
92             fprintf(out, "q %d", list[i]);
93             i++;
94             if ( (i < list_ptr) && (list[i] != -1) ) { 
95                 fprintf(out, " %d", list[i]);
96                 i++;
97             }
98             fprintf(out, "\n");
99         }
100
101         i++;
102     }
103 }
104
105
106 /* Check the direction the current triangle faces, compared to it's
107  * pregenerated normal.  Returns the dot product between the target
108  * normal and actual normal.  If the dot product is close to 1.0, they
109  * nearly match.  If the are close to -1.0, the are nearly
110  * opposite. */
111 double check_cur_face(int n1, int n2, int n3) {
112     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
113
114     /* check for the proper rotation by calculating an approximate
115      * normal and seeing if it is close to the precalculated normal */
116     v1[0] = nodes[n2][0] - nodes[n1][0];
117     v1[1] = nodes[n2][1] - nodes[n1][1];
118     v1[2] = nodes[n2][2] - nodes[n1][2];
119     v2[0] = nodes[n3][0] - nodes[n1][0];
120     v2[1] = nodes[n3][1] - nodes[n1][1];
121     v2[2] = nodes[n3][2] - nodes[n1][2];
122
123     MAT3cross_product(approx_normal, v1, v2);
124     MAT3_NORMALIZE_VEC(approx_normal,temp);
125     dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
126
127     /* not first triangle */
128     /* if ( ((dot_prod < -0.5) && !is_backwards) ||
129          ((dot_prod >  0.5) && is_backwards) ) {
130         printf("    Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
131                approx_normal[1], approx_normal[2]);
132         printf("    Dot product = %.4f\n", dot_prod);
133     } */
134     /* angle = acos(dot_prod); */
135     /* printf("Normal ANGLE = %.3f rads.\n", angle); */
136
137     return(dot_prod);
138 }
139
140
141 /* Load a .obj file */
142 void obj_fix(char *basename) {
143     char line[256];
144     char inpath[256], outpath[256];
145     double dot_prod;
146     int first, ncount, vncount, n1, n2, n3, n4;
147     int is_ccw;
148
149     strcpy(inpath, basename);
150     strcat(inpath, ".obj");
151
152     strcpy(outpath, basename);
153     strcat(outpath, ".1.obj");
154
155     if ( (in = fopen(inpath, "r")) == NULL ) {
156         printf("Cannot open file: %s\n", inpath);
157         exit(-1);
158     }
159
160     if ( (out = fopen(outpath, "w")) == NULL ) {
161         printf("Cannot open file: %s\n", outpath);
162         exit(-1);
163     }
164
165     list_init(&ccw_list_ptr);
166     list_init(&cw_list_ptr);
167
168     first = 1;
169     ncount = 1;
170     vncount = 1;
171
172     printf("Reading file:  %s\n", inpath);
173
174     while ( fgets(line, 250, in) != NULL ) {
175         if ( line[0] == '#' ) {
176             /* pass along the comments verbatim */
177             fprintf(out, "%s", line);
178         } else if ( strlen(line) <= 1 ) {
179             /* pass along empty lines */
180             fprintf(out, "%s", line);
181         } else if ( strncmp(line, "v ", 2) == 0 ) {
182             /* save vertex to memory and output to file */
183             if ( ncount < MAXNODES ) {
184                 /* printf("vertex = %s", line); */
185                 sscanf(line, "v %f %f %f\n", 
186                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
187                 fprintf(out, "v %.2f %.2f %.2f\n", 
188                        nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
189                 ncount++;
190             } else {
191                 printf("Read too many nodes ... dying :-(\n");
192                 exit(-1);
193             }
194         } else if ( strncmp(line, "vn ", 3) == 0 ) {
195             /* save vertex normals to memory and output to file */
196             if ( vncount < MAXNODES ) {
197                 /* printf("vertex normal = %s", line); */
198                 sscanf(line, "vn %f %f %f\n", 
199                        &normals[vncount][0], &normals[vncount][1], 
200                        &normals[vncount][2]);
201                 fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
202                         normals[vncount][1], normals[vncount][2]);
203                 vncount++;
204             } else {
205                 printf("Read too many vertex normals ... dying :-(\n");
206                 exit(-1);
207             }
208         } else if ( line[0] == 't' ) {
209             /* starting a new triangle strip */
210
211             printf("Starting a new triangle strip\n");
212
213             n1 = n2 = n3 = n4 = 0;
214
215             printf("new tri strip = %s", line);
216             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
217
218             dot_prod = check_cur_face(n1, n2, n3);
219             if ( dot_prod < -0.5 ) {
220                 /* this stripe is backwards (CW) */
221                 is_ccw = 0;
222                 printf(" -> Starting a backwards stripe\n");
223             } else {
224                 /* this stripe is normal (CCW) */
225                 is_ccw = 1;
226             }
227
228             if ( is_ccw ) {
229                 if ( ccw_list_ptr ) {
230                     list_add(ccw_list, &ccw_list_ptr, -1);
231                 }
232
233                 list_add(ccw_list, &ccw_list_ptr, n1);
234                 list_add(ccw_list, &ccw_list_ptr, n2);
235                 list_add(ccw_list, &ccw_list_ptr, n3);
236             } else {
237                 if ( cw_list_ptr ) {
238                     list_add(cw_list, &cw_list_ptr, -1);
239                 }
240
241                 list_add(cw_list, &cw_list_ptr, n1);
242                 list_add(cw_list, &cw_list_ptr, n2);
243                 list_add(cw_list, &cw_list_ptr, n3);
244             }
245
246             if ( n4 > 0 ) {
247                 if ( is_ccw ) {
248                     list_add(ccw_list, &ccw_list_ptr, n4);
249                 } else {
250                     list_add(cw_list, &cw_list_ptr, n4);
251                 }
252             }
253         } else if ( line[0] == 'f' ) {
254             /* pass along the unoptimized faces verbatim */
255             fprintf(out, "%s", line);
256         } else if ( line[0] == 'q' ) {
257             /* continue a triangle strip */
258             n1 = n2 = 0;
259
260             /* printf("continued tri strip = %s ", line); */
261             sscanf(line, "q %d %d\n", &n1, &n2);
262
263             if ( is_ccw ) {
264                 list_add(ccw_list, &ccw_list_ptr, n1);
265             } else {
266                 list_add(cw_list, &cw_list_ptr, n1);
267             }
268
269             if ( n2 > 0 ) {
270                 if ( is_ccw ) {
271                     list_add(ccw_list, &ccw_list_ptr, n2);
272                 } else {
273                     list_add(cw_list, &cw_list_ptr, n2);
274                 }
275             }
276         } else {
277             printf("Unknown line in %s = %s\n", inpath, line);
278         }
279     }
280
281     fprintf(out, "winding ccw\n");
282     dump_list(ccw_list, ccw_list_ptr);
283
284     fprintf(out, "winding cw\n");
285     dump_list(cw_list, cw_list_ptr);
286
287     fclose(in);
288     fclose(out);
289 }
290
291
292 /* $Log$
293 /* Revision 1.1  1997/12/08 19:28:54  curt
294 /* Initial revision.
295 /*
296  */