]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.c
Fixed an off by one error in node, normal, and face counters.
[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 <Math/mat3.h>
33
34
35 /* what do ya' know, here's some global variables */
36 static double nodes[MAXNODES][3];
37 static double normals[MAXNODES][3];
38 static int faces[MAXNODES][3];
39 int ncount, vncount, fcount;
40
41 static int ccw_list[MAXNODES];
42 int ccw_list_ptr;
43
44 static int cw_list[MAXNODES];
45 int cw_list_ptr;
46
47 FILE *in, *out;
48
49 double refx, refy, refz;
50
51
52 /* some simple list routines */
53
54 /* reset the list */
55 void list_init(int *list_ptr) {
56     *list_ptr = 0;
57 }
58
59
60 /* add to list */
61 void list_add(int *list, int *list_ptr, int node) {
62     if ( *list_ptr >= MAXNODES ) {
63         printf("ERROR: list overflow in list_add()\n");
64         exit(-1);
65     }
66
67     list[*list_ptr] = node;
68     *list_ptr += 1;
69
70     /* printf("list pointer = %d  adding %d\n", *list_ptr, node); */
71 }
72
73
74 /* dump nodes */
75 void dump_nodes( void ) {
76     int i;
77
78     fprintf(out, "\n");
79     for ( i = 1; i <= ncount; i++ ) {
80         fprintf(out, "v %.4lf %.4lf %.4lf\n",
81                 nodes[i][0] - refx, nodes[i][1] - refy, nodes[i][2] - refz);
82     }
83 }
84
85
86 /* dump normals */
87 void dump_normals( void ) {
88     int i;
89
90     fprintf(out, "\n");
91     for ( i = 1; i <= vncount; i++ ) {
92         fprintf(out, "vn %.4lf %.4lf %.4lf\n", 
93                 normals[i][0], normals[i][1], normals[i][2]);
94     }
95 }
96
97
98 /* dump faces */
99 void dump_faces( void ) {
100     int i;
101
102     fprintf(out, "\n");
103     for ( i = 1; i <= fcount; i++ ) {
104         fprintf(out, "f %d %d %d\n", 
105                 faces[i][0], faces[i][1], faces[i][2]);
106     }
107 }
108
109
110 /* dump list */
111 void dump_list(int *list, int list_ptr) {
112     int i;
113
114     if ( list_ptr < 3 ) {
115         printf("List is empty ... skipping\n");
116         return;
117     }
118
119     printf("Dumping list, size = %d\n", list_ptr);
120
121     i = 0;
122     while ( i < list_ptr ) { 
123         /* do next strip */
124
125         /* dump header */
126         fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
127         /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
128         i += 3;
129
130         /* dump rest of strip (until -1) */
131         while ( (i < list_ptr) && (list[i] != -1) ) { 
132             fprintf(out, "q %d", list[i]);
133             i++;
134             if ( (i < list_ptr) && (list[i] != -1) ) { 
135                 fprintf(out, " %d", list[i]);
136                 i++;
137             }
138             fprintf(out, "\n");
139         }
140
141         i++;
142     }
143 }
144
145
146 /* Check the direction the current triangle faces, compared to it's
147  * pregenerated normal.  Returns the dot product between the target
148  * normal and actual normal.  If the dot product is close to 1.0, they
149  * nearly match.  If the are close to -1.0, the are nearly
150  * opposite. */
151 double check_cur_face(int n1, int n2, int n3) {
152     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
153
154     /* check for the proper rotation by calculating an approximate
155      * normal and seeing if it is close to the precalculated normal */
156     v1[0] = nodes[n2][0] - nodes[n1][0];
157     v1[1] = nodes[n2][1] - nodes[n1][1];
158     v1[2] = nodes[n2][2] - nodes[n1][2];
159     v2[0] = nodes[n3][0] - nodes[n1][0];
160     v2[1] = nodes[n3][1] - nodes[n1][1];
161     v2[2] = nodes[n3][2] - nodes[n1][2];
162
163     MAT3cross_product(approx_normal, v1, v2);
164     MAT3_NORMALIZE_VEC(approx_normal,temp);
165     dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
166
167     /* not first triangle */
168     /* if ( ((dot_prod < -0.5) && !is_backwards) ||
169          ((dot_prod >  0.5) && is_backwards) ) {
170         printf("    Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
171                approx_normal[1], approx_normal[2]);
172         printf("    Dot product = %.4f\n", dot_prod);
173     } */
174     /* angle = acos(dot_prod); */
175     /* printf("Normal ANGLE = %.3f rads.\n", angle); */
176
177     return(dot_prod);
178 }
179
180
181 /* Load a .obj file */
182 void obj_fix(char *infile, char *outfile) {
183     char line[256];
184     double dot_prod;
185     int first, n1, n2, n3, n4;
186     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
187     int is_ccw;
188
189     if ( (in = fopen(infile, "r")) == NULL ) {
190         printf("Cannot open file: %s\n", infile);
191         exit(-1);
192     }
193
194     if ( (out = fopen(outfile, "w")) == NULL ) {
195         printf("Cannot open file: %s\n", outfile);
196         exit(-1);
197     }
198
199     list_init(&ccw_list_ptr);
200     list_init(&cw_list_ptr);
201
202     /* I start counting at one because that is how the triangle
203        program refers to nodes and normals */
204     first = 1;
205     ncount = 0;
206     vncount = 0;
207     fcount = 0;
208
209     printf("Reading file:  %s\n", infile);
210
211     while ( fgets(line, 250, in) != NULL ) {
212         if ( line[0] == '#' ) {
213             /* pass along the comments verbatim */
214             fprintf(out, "%s", line);
215         } else if ( strlen(line) <= 1 ) {
216             /* don't pass along empty lines */
217             // fprintf(out, "%s", line);
218         } else if ( strncmp(line, "v ", 2) == 0 ) {
219             /* save vertex to memory and output to file */
220             if ( ncount < MAXNODES ) {
221                 /* printf("vertex = %s", line); */
222                 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
223                 nodes[ncount][0] = x;
224                 nodes[ncount][1] = y;
225                 nodes[ncount][2] = z;
226
227                 /* first time through set min's and max'es */
228                 if ( ncount == 1 ) {
229                     xmin = x;
230                     xmax = x;
231                     ymin = y;
232                     ymax = y;
233                     zmin = z;
234                     zmax = z;
235                 }
236     
237                 /* keep track of min/max vertex values */
238                 if ( x < xmin ) xmin = x;
239                 if ( x > xmax ) xmax = x;
240                 if ( y < ymin ) ymin = y;
241                 if ( y > ymax ) ymax = y;
242                 if ( z < zmin ) zmin = z;
243                 if ( z > zmax ) zmax = z;               
244
245                 // fprintf(out, "v %.2f %.2f %.2f\n", 
246                 //       nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
247                 ncount++;
248             } else {
249                 printf("Read too many nodes ... dying :-(\n");
250                 exit(-1);
251             }
252         } else if ( strncmp(line, "vn ", 3) == 0 ) {
253             /* save vertex normals to memory and output to file */
254             if ( vncount < MAXNODES ) {
255                 /* printf("vertex normal = %s", line); */
256                 sscanf(line, "vn %lf %lf %lf\n", 
257                        &normals[vncount][0], &normals[vncount][1], 
258                        &normals[vncount][2]);
259                 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
260                 //      normals[vncount][1], normals[vncount][2]);
261                 vncount++;
262             } else {
263                 printf("Read too many vertex normals ... dying :-(\n");
264                 exit(-1);
265             }
266         } else if ( line[0] == 't' ) {
267             /* starting a new triangle strip */
268
269             printf("Starting a new triangle strip\n");
270
271             n1 = n2 = n3 = n4 = 0;
272
273             printf("new tri strip = %s", line);
274             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
275
276             /* special cases to handle bugs in our beloved tri striper */
277             if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
278                 n2 = 3;
279             }
280             if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
281                 n3 = 4;
282             }
283
284             dot_prod = check_cur_face(n1, n2, n3);
285             if ( dot_prod < 0.0 ) {
286                 /* this stripe is backwards (CW) */
287                 is_ccw = 0;
288                 printf(" -> Starting a backwards stripe\n");
289             } else {
290                 /* this stripe is normal (CCW) */
291                 is_ccw = 1;
292             }
293
294             if ( is_ccw ) {
295                 if ( ccw_list_ptr ) {
296                     list_add(ccw_list, &ccw_list_ptr, -1);
297                 }
298
299                 list_add(ccw_list, &ccw_list_ptr, n1);
300                 list_add(ccw_list, &ccw_list_ptr, n2);
301                 list_add(ccw_list, &ccw_list_ptr, n3);
302             } else {
303                 if ( cw_list_ptr ) {
304                     list_add(cw_list, &cw_list_ptr, -1);
305                 }
306
307                 list_add(cw_list, &cw_list_ptr, n1);
308                 list_add(cw_list, &cw_list_ptr, n2);
309                 list_add(cw_list, &cw_list_ptr, n3);
310             }
311
312             if ( n4 > 0 ) {
313                 if ( is_ccw ) {
314                     list_add(ccw_list, &ccw_list_ptr, n4);
315                 } else {
316                     list_add(cw_list, &cw_list_ptr, n4);
317                 }
318             }
319         } else if ( line[0] == 'f' ) {
320             if ( fcount < MAXNODES ) {
321                 /* pass along the unoptimized faces verbatim */
322                 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
323                 faces[fcount][0] = n1;
324                 faces[fcount][1] = n2;
325                 faces[fcount][2] = n3;
326
327                 fcount++;
328             } else {
329                 printf("Read too many unoptimized faces ... dying :-(\n");
330                 exit(-1);
331             }
332  
333             // fprintf(out, "%s", line);
334         } else if ( line[0] == 'q' ) {
335             /* continue a triangle strip */
336             n1 = n2 = 0;
337
338             /* printf("continued tri strip = %s ", line); */
339             sscanf(line, "q %d %d\n", &n1, &n2);
340
341             if ( is_ccw ) {
342                 list_add(ccw_list, &ccw_list_ptr, n1);
343             } else {
344                 list_add(cw_list, &cw_list_ptr, n1);
345             }
346
347             if ( n2 > 0 ) {
348                 if ( is_ccw ) {
349                     list_add(ccw_list, &ccw_list_ptr, n2);
350                 } else {
351                     list_add(cw_list, &cw_list_ptr, n2);
352                 }
353             }
354         } else {
355             printf("Unknown line in %s = %s\n", infile, line);
356         }
357     }
358
359     /* reference point is the "center" */
360     refx = (xmin + xmax) / 2.0;
361     refy = (ymin + ymax) / 2.0;
362     refz = (zmin + zmax) / 2.0;
363
364     fprintf(out, "\n");
365     fprintf(out, "ref %.2f %.2f %.2f\n", refx, refy, refz);     
366
367     dump_nodes();
368     dump_normals();
369     dump_faces();
370
371     fprintf(out, "winding ccw\n");
372     dump_list(ccw_list, ccw_list_ptr);
373
374     fprintf(out, "winding cw\n");
375     dump_list(cw_list, cw_list_ptr);
376
377     fclose(in);
378     fclose(out);
379 }
380
381
382 /* $Log$
383 /* Revision 1.12  1998/05/16 13:11:26  curt
384 /* Fixed an off by one error in node, normal, and face counters.
385 /*
386  * Revision 1.11  1998/04/27 15:59:24  curt
387  * Fixed an off by one error.
388  *
389  * Revision 1.10  1998/04/27 03:33:11  curt
390  * Code now calculates a center reference points and outputs everything
391  * relative to that.  This is useful in the rendering engine to keep everything
392  * close to (0, 0, 0) where we can avoid many GLfloat precision problems.
393  *
394  * Revision 1.9  1998/04/18 04:01:03  curt
395  * Now use libMath rather than having local copies of math routines.
396  *
397  * Revision 1.8  1998/04/08 23:19:37  curt
398  * Adopted Gnu automake/autoconf system.
399  *
400  * Revision 1.7  1998/03/19 02:51:41  curt
401  * Added special case handling to compensate for bugs in our beloved tri striper
402  *
403  * Revision 1.6  1998/03/03 15:36:12  curt
404  * Tweaks for compiling with g++
405  *
406  * Revision 1.5  1998/03/03 03:37:03  curt
407  * Cumulative tweaks.
408  *
409  * Revision 1.4  1998/01/31 00:41:25  curt
410  * Made a few changes converting floats to doubles.
411  *
412  * Revision 1.3  1998/01/19 19:51:07  curt
413  * A couple final pre-release tweaks.
414  *
415  * Revision 1.2  1998/01/09 23:03:12  curt
416  * Restructured to split 1deg x 1deg dem's into 64 subsections.
417  *
418  * Revision 1.1  1997/12/08 19:28:54  curt
419  * Initial revision.
420  *
421  */