]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.c
Output more digits after the decimal place.
[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 /* fix the cw list and append to ccw_list */
75 void fix_cw_list(int *list, int list_ptr) {
76     int i, j, len;
77
78     if ( list_ptr < 3 ) {
79         printf("List is empty ... skipping\n");
80         return;
81     }
82
83     printf("Fixing cw list, size = %d\n", list_ptr);
84
85     i = 0;
86     while ( i < list_ptr ) { 
87         /* do next strip */
88
89         /* find length */
90         len = 0;
91         /* scan rest of strip (until -1) */
92         while ( ((i+len) < list_ptr) && (list[i+len] != -1) ) { 
93             // printf("len = %d item = %d\n", len, list[i+len] );
94             len++;
95         }
96         // printf("          Final length = %d\n", len);
97
98         if ( (len % 2) != 0 ) {
99             /* if length is odd, just reverse order of nodes to reverse
100                winding */
101             if ( ccw_list_ptr ) {
102                 list_add(ccw_list, &ccw_list_ptr, -1);
103             }
104             for ( j = i + len - 1; j >= i; j-- ) {
105                 // printf(" odd -> item = %d\n", list[j] );
106                 list_add(ccw_list, &ccw_list_ptr, list[j]);
107             }
108         } else {
109             /* if length is even, reverse order of (n-1) nodes to
110                reverse winding, and create an orphan triangle for the
111                last "nth" node */
112             if ( ccw_list_ptr ) {
113                 list_add(ccw_list, &ccw_list_ptr, -1);
114             }
115             for ( j = i + len - 2; j >= i; j-- ) {
116                 // printf(" even -> item = %d\n", list[j] );
117                 list_add(ccw_list, &ccw_list_ptr, list[j]);
118             }
119
120             // printf(" even bonus -> item = %d\n", list[i + len - 1] );
121             // printf(" even bonus -> item = %d\n", list[i + len - 2] );
122             // printf(" even bonus -> item = %d\n", list[i + len - 3] );
123             list_add(ccw_list, &ccw_list_ptr, -1);
124             list_add(ccw_list, &ccw_list_ptr, list[i + len - 3]);
125             list_add(ccw_list, &ccw_list_ptr, list[i + len - 2]);
126             list_add(ccw_list, &ccw_list_ptr, list[i + len - 1]);
127         }
128
129         i += len + 1;
130     }
131 }
132
133
134 // Calculate distance between (0,0,0) and the specified point
135 static double calc_dist(double x, double y, double z) {
136     return ( sqrt(x*x + y*y + z*z) );
137 }
138
139
140 void dump_global_bounds( void ) {
141     double dist, radius;
142     int i;
143
144     radius = 0.0;
145
146     fprintf(out, "\n");
147
148     for ( i = 1; i < ncount; i++ ) {
149
150         dist = calc_dist(nodes[i][0] - refx, nodes[i][1] - refy, 
151                          nodes[i][2] - refz);
152         // printf("node = %.2f %.2f %.2f dist = %.2f\n", 
153         //        nodes[i][0], nodes[i][1], nodes[i][2],
154         //        dist);
155
156         if ( dist > radius ) {
157             radius = dist;
158         }
159
160     }
161
162     fprintf(out, "gb %.5f %.5f %.5f %.2f\n", refx, refy, refz, radius);
163 }
164
165
166 /* dump nodes */
167 void dump_nodes( void ) {
168     int i;
169
170     fprintf(out, "\n");
171     for ( i = 1; i < ncount; i++ ) {
172         fprintf(out, "v %.5f %.5f %.5f\n",
173                 nodes[i][0] - refx, nodes[i][1] - refy, nodes[i][2] - refz);
174     }
175 }
176
177
178 /* dump normals */
179 void dump_normals( void ) {
180     int i;
181
182     fprintf(out, "\n");
183     for ( i = 1; i < vncount; i++ ) {
184         fprintf(out, "vn %.5f %.5f %.5f\n", 
185                 normals[i][0], normals[i][1], normals[i][2]);
186     }
187 }
188
189
190 /* dump faces */
191 void dump_faces( void ) {
192     int i;
193
194     fprintf(out, "\n");
195     for ( i = 1; i < fcount; i++ ) {
196         fprintf(out, "f %d %d %d\n", 
197                 faces[i][0], faces[i][1], faces[i][2]);
198     }
199 }
200
201
202 /* dump list */
203 void dump_list(int *list, int list_ptr) {
204     int i;
205
206     if ( list_ptr < 3 ) {
207         printf("List is empty ... skipping\n");
208         return;
209     }
210
211     printf("Dumping list, size = %d\n", list_ptr);
212
213     i = 0;
214     while ( i < list_ptr ) { 
215         /* do next strip */
216
217         if ( (i % 2) == 0 ) {
218             fprintf(out, "\nusemtl desert1\n");
219         } else {
220             fprintf(out, "\nusemtl desert2\n");
221         }
222
223         /* dump header */
224         fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
225         /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
226         i += 3;
227
228         /* dump rest of strip (until -1) */
229         while ( (i < list_ptr) && (list[i] != -1) ) { 
230             fprintf(out, "q %d", list[i]);
231             i++;
232             if ( (i < list_ptr) && (list[i] != -1) ) { 
233                 fprintf(out, " %d", list[i]);
234                 i++;
235             }
236             fprintf(out, "\n");
237         }
238
239         i++;
240     }
241 }
242
243
244 /* Check the direction the current triangle faces, compared to it's
245  * pregenerated normal.  Returns the dot product between the target
246  * normal and actual normal.  If the dot product is close to 1.0, they
247  * nearly match.  If the are close to -1.0, the are nearly
248  * opposite. */
249 double check_cur_face(int n1, int n2, int n3) {
250     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
251
252     /* check for the proper rotation by calculating an approximate
253      * normal and seeing if it is close to the precalculated normal */
254     v1[0] = nodes[n2][0] - nodes[n1][0];
255     v1[1] = nodes[n2][1] - nodes[n1][1];
256     v1[2] = nodes[n2][2] - nodes[n1][2];
257     v2[0] = nodes[n3][0] - nodes[n1][0];
258     v2[1] = nodes[n3][1] - nodes[n1][1];
259     v2[2] = nodes[n3][2] - nodes[n1][2];
260
261     MAT3cross_product(approx_normal, v1, v2);
262     MAT3_NORMALIZE_VEC(approx_normal,temp);
263     dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
264
265     /* not first triangle */
266     /* if ( ((dot_prod < -0.5) && !is_backwards) ||
267          ((dot_prod >  0.5) && is_backwards) ) {
268         printf("    Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
269                approx_normal[1], approx_normal[2]);
270         printf("    Dot product = %.4f\n", dot_prod);
271     } */
272     /* angle = acos(dot_prod); */
273     /* printf("Normal ANGLE = %.3f rads.\n", angle); */
274
275     return(dot_prod);
276 }
277
278
279 /* Load a .obj file */
280 void obj_fix(char *infile, char *outfile) {
281     char line[256];
282     double dot_prod;
283     int first, n1, n2, n3, n4;
284     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
285     int is_ccw;
286
287     if ( (in = fopen(infile, "r")) == NULL ) {
288         printf("Cannot open file: %s\n", infile);
289         exit(-1);
290     }
291
292     if ( (out = fopen(outfile, "w")) == NULL ) {
293         printf("Cannot open file: %s\n", outfile);
294         exit(-1);
295     }
296
297     list_init(&ccw_list_ptr);
298     list_init(&cw_list_ptr);
299
300     /* I start counting at one because that is how the triangle
301        program refers to nodes and normals */
302     first = 1;
303     ncount = 1;
304     vncount = 1;
305     fcount = 1;
306
307     printf("Reading file:  %s\n", infile);
308
309     while ( fgets(line, 250, in) != NULL ) {
310         if ( line[0] == '#' ) {
311             /* pass along the comments verbatim */
312             fprintf(out, "%s", line);
313         } else if ( strlen(line) <= 1 ) {
314             /* don't pass along empty lines */
315             // fprintf(out, "%s", line);
316         } else if ( strncmp(line, "v ", 2) == 0 ) {
317             /* save vertex to memory and output to file */
318             if ( ncount < MAXNODES ) {
319                 /* printf("vertex = %s", line); */
320                 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
321                 nodes[ncount][0] = x;
322                 nodes[ncount][1] = y;
323                 nodes[ncount][2] = z;
324
325                 /* first time through set min's and max'es */
326                 if ( ncount == 1 ) {
327                     xmin = x;
328                     xmax = x;
329                     ymin = y;
330                     ymax = y;
331                     zmin = z;
332                     zmax = z;
333                 }
334     
335                 /* keep track of min/max vertex values */
336                 if ( x < xmin ) xmin = x;
337                 if ( x > xmax ) xmax = x;
338                 if ( y < ymin ) ymin = y;
339                 if ( y > ymax ) ymax = y;
340                 if ( z < zmin ) zmin = z;
341                 if ( z > zmax ) zmax = z;               
342
343                 // fprintf(out, "v %.2f %.2f %.2f\n", 
344                 //       nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
345                 ncount++;
346             } else {
347                 printf("Read too many nodes ... dying :-(\n");
348                 exit(-1);
349             }
350         } else if ( strncmp(line, "vn ", 3) == 0 ) {
351             /* save vertex normals to memory and output to file */
352             if ( vncount < MAXNODES ) {
353                 /* printf("vertex normal = %s", line); */
354                 sscanf(line, "vn %lf %lf %lf\n", 
355                        &normals[vncount][0], &normals[vncount][1], 
356                        &normals[vncount][2]);
357                 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
358                 //      normals[vncount][1], normals[vncount][2]);
359                 vncount++;
360             } else {
361                 printf("Read too many vertex normals ... dying :-(\n");
362                 exit(-1);
363             }
364         } else if ( line[0] == 't' ) {
365             /* starting a new triangle strip */
366
367             printf("Starting a new triangle strip\n");
368
369             n1 = n2 = n3 = n4 = 0;
370
371             printf("new tri strip = %s", line);
372             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
373
374             /* special cases to handle bugs in our beloved tri striper */
375             if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
376                 n2 = 3;
377             }
378             if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
379                 n3 = 4;
380             }
381
382             dot_prod = check_cur_face(n1, n2, n3);
383             if ( dot_prod < 0.0 ) {
384                 /* this stripe is backwards (CW) */
385                 is_ccw = 0;
386                 printf(" -> Starting a backwards stripe\n");
387             } else {
388                 /* this stripe is normal (CCW) */
389                 is_ccw = 1;
390             }
391
392             if ( is_ccw ) {
393                 if ( ccw_list_ptr ) {
394                     list_add(ccw_list, &ccw_list_ptr, -1);
395                 }
396
397                 list_add(ccw_list, &ccw_list_ptr, n1);
398                 list_add(ccw_list, &ccw_list_ptr, n2);
399                 list_add(ccw_list, &ccw_list_ptr, n3);
400             } else {
401                 if ( cw_list_ptr ) {
402                     list_add(cw_list, &cw_list_ptr, -1);
403                 }
404
405                 list_add(cw_list, &cw_list_ptr, n1);
406                 list_add(cw_list, &cw_list_ptr, n2);
407                 list_add(cw_list, &cw_list_ptr, n3);
408             }
409
410             if ( n4 > 0 ) {
411                 if ( is_ccw ) {
412                     list_add(ccw_list, &ccw_list_ptr, n4);
413                 } else {
414                     list_add(cw_list, &cw_list_ptr, n4);
415                 }
416             }
417         } else if ( line[0] == 'f' ) {
418             if ( fcount < MAXNODES ) {
419                 /* pass along the unoptimized faces verbatim */
420                 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
421                 faces[fcount][0] = n1;
422                 faces[fcount][1] = n2;
423                 faces[fcount][2] = n3;
424
425                 fcount++;
426             } else {
427                 printf("Read too many unoptimized faces ... dying :-(\n");
428                 exit(-1);
429             }
430  
431             // fprintf(out, "%s", line);
432         } else if ( line[0] == 'q' ) {
433             /* continue a triangle strip */
434             n1 = n2 = 0;
435
436             /* printf("continued tri strip = %s ", line); */
437             sscanf(line, "q %d %d\n", &n1, &n2);
438
439             if ( is_ccw ) {
440                 list_add(ccw_list, &ccw_list_ptr, n1);
441             } else {
442                 list_add(cw_list, &cw_list_ptr, n1);
443             }
444
445             if ( n2 > 0 ) {
446                 if ( is_ccw ) {
447                     list_add(ccw_list, &ccw_list_ptr, n2);
448                 } else {
449                     list_add(cw_list, &cw_list_ptr, n2);
450                 }
451             }
452         } else {
453             printf("Unknown line in %s = %s\n", infile, line);
454         }
455     }
456
457     /* reference point is the "center" */
458     refx = (xmin + xmax) / 2.0;
459     refy = (ymin + ymax) / 2.0;
460     refz = (zmin + zmax) / 2.0;
461
462     /* convert the cw_list to ccw add append to ccw_list */
463     fix_cw_list(cw_list, cw_list_ptr);
464
465     dump_global_bounds();
466     dump_nodes();
467     dump_normals();
468     if ( fcount > 1 ) {
469         dump_faces();
470     }
471
472     dump_list(ccw_list, ccw_list_ptr);
473
474     fclose(in);
475     fclose(out);
476 }
477
478
479 /* $Log$
480 /* Revision 1.14  1998/05/23 15:19:49  curt
481 /* Output more digits after the decimal place.
482 /*
483  * Revision 1.13  1998/05/20 20:55:19  curt
484  * Fixed arbitrary polygon winding problem here so all tristrips are passed
485  * to runtime simulator with a consistant counter clockwise winding.
486  *
487  * Revision 1.12  1998/05/16 13:11:26  curt
488  * Fixed an off by one error in node, normal, and face counters.
489  *
490  * Revision 1.11  1998/04/27 15:59:24  curt
491  * Fixed an off by one error.
492  *
493  * Revision 1.10  1998/04/27 03:33:11  curt
494  * Code now calculates a center reference points and outputs everything
495  * relative to that.  This is useful in the rendering engine to keep everything
496  * close to (0, 0, 0) where we can avoid many GLfloat precision problems.
497  *
498  * Revision 1.9  1998/04/18 04:01:03  curt
499  * Now use libMath rather than having local copies of math routines.
500  *
501  * Revision 1.8  1998/04/08 23:19:37  curt
502  * Adopted Gnu automake/autoconf system.
503  *
504  * Revision 1.7  1998/03/19 02:51:41  curt
505  * Added special case handling to compensate for bugs in our beloved tri striper
506  *
507  * Revision 1.6  1998/03/03 15:36:12  curt
508  * Tweaks for compiling with g++
509  *
510  * Revision 1.5  1998/03/03 03:37:03  curt
511  * Cumulative tweaks.
512  *
513  * Revision 1.4  1998/01/31 00:41:25  curt
514  * Made a few changes converting floats to doubles.
515  *
516  * Revision 1.3  1998/01/19 19:51:07  curt
517  * A couple final pre-release tweaks.
518  *
519  * Revision 1.2  1998/01/09 23:03:12  curt
520  * Restructured to split 1deg x 1deg dem's into 64 subsections.
521  *
522  * Revision 1.1  1997/12/08 19:28:54  curt
523  * Initial revision.
524  *
525  */