]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.c
Commented out a couple of debugging messages.
[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, "gbs %.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, n1, n2, n3;
193     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin, dist, radius;
194
195     fprintf(out, "\n");
196     for ( i = 1; i < fcount; i++ ) {
197         n1 = faces[i][0];
198         n2 = faces[i][1];
199         n3 = faces[i][2];
200
201         /* calc center of face */
202         xmin = xmax = nodes[n1][0];
203         ymin = ymax = nodes[n1][1];
204         zmin = zmax = nodes[n1][2];
205
206         if ( nodes[n2][0] < xmin ) { xmin = nodes[n2][0]; }
207         if ( nodes[n2][0] > xmax ) { xmax = nodes[n2][0]; }
208         if ( nodes[n2][1] < ymin ) { ymin = nodes[n2][1]; }
209         if ( nodes[n2][1] > ymax ) { ymax = nodes[n2][1]; }
210         if ( nodes[n2][2] < zmin ) { zmin = nodes[n2][2]; }
211         if ( nodes[n2][2] > zmax ) { zmax = nodes[n2][2]; }
212
213         if ( nodes[n3][0] < xmin ) { xmin = nodes[n3][0]; }
214         if ( nodes[n3][0] > xmax ) { xmax = nodes[n3][0]; }
215         if ( nodes[n3][1] < ymin ) { ymin = nodes[n3][1]; }
216         if ( nodes[n3][1] > ymax ) { ymax = nodes[n3][1]; }
217         if ( nodes[n3][2] < zmin ) { zmin = nodes[n3][2]; }
218         if ( nodes[n3][2] > zmax ) { zmax = nodes[n3][2]; }
219
220         x = (xmin + xmax) / 2.0;
221         y = (ymin + ymax) / 2.0;
222         z = (zmin + zmax) / 2.0;
223
224         /* calc bounding radius */
225         radius = calc_dist(nodes[n1][0] - x, nodes[n1][1] - y, 
226                            nodes[n1][2] - z);
227
228         dist = calc_dist(nodes[n2][0] - x, nodes[n2][1] - y, nodes[n2][2] - z);
229         if ( dist > radius ) { radius = dist; }
230
231         dist = calc_dist(nodes[n3][0] - x, nodes[n3][1] - y, nodes[n3][2] - z);
232         if ( dist > radius ) { radius = dist; }
233         
234         /* output data */
235         fprintf(out, "bs %.2f %.2f %.2f %.2f\n", x, y, z, radius);
236         fprintf(out, "f %d %d %d\n", n1, n2, n3);
237     }
238 }
239
240
241 /* dump list */
242 void dump_list(int *list, int list_ptr) {
243     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin, dist, radius;
244     int i, j, len, n;
245
246     if ( list_ptr < 3 ) {
247         printf("List is empty ... skipping\n");
248         return;
249     }
250
251     printf("Dumping list, size = %d\n", list_ptr);
252
253     i = 0;
254     while ( i < list_ptr ) { 
255         /* do next strip */
256
257         if ( (i % 2) == 0 ) {
258             fprintf(out, "\nusemtl desert1\n");
259         } else {
260             fprintf(out, "\nusemtl desert2\n");
261         }
262
263         /* find length of next tri strip */
264         len = 0;
265         /* scan rest of strip (until -1) */
266         while ( ((i+len) < list_ptr) && (list[i+len] != -1) ) { 
267             // printf("len = %d item = %d\n", len, list[i+len] );
268             len++;
269         }
270         // printf("strip length = %d\n", len);
271
272         /* calc center of face */
273         n = list[i];
274         xmin = xmax = nodes[n][0];
275         ymin = ymax = nodes[n][1];
276         zmin = zmax = nodes[n][2];
277         // printf("%.2f %.2f %.2f\n", nodes[n][0], nodes[n][1], nodes[n][2]);
278
279         for ( j = i + 1; j < i + len; j++ ) {
280             // printf("j = %d\n", j);
281             n = list[j];
282             if ( nodes[n][0] < xmin ) { xmin = nodes[n][0]; }
283             if ( nodes[n][0] > xmax ) { xmax = nodes[n][0]; }
284             if ( nodes[n][1] < ymin ) { ymin = nodes[n][1]; }
285             if ( nodes[n][1] > ymax ) { ymax = nodes[n][1]; }
286             if ( nodes[n][2] < zmin ) { zmin = nodes[n][2]; }
287             if ( nodes[n][2] > zmax ) { zmax = nodes[n][2]; }
288             // printf("%.2f %.2f %.2f\n", nodes[n][0], nodes[n][1], nodes[n][2]);
289         }           
290         x = (xmin + xmax) / 2.0;
291         y = (ymin + ymax) / 2.0;
292         z = (zmin + zmax) / 2.0;
293         // printf("center = %.2f %.2f %.2f\n", x, y, z);
294
295         /* calc bounding radius */
296         n = list[i];
297         radius = calc_dist(nodes[n][0] - x, nodes[n][1] - y, nodes[n][2] - z);
298
299         for ( j = i + 1; j < i + len; j++ ) {
300             n = list[j];
301             dist = calc_dist(nodes[n][0] - x, nodes[n][1] - y, 
302                              nodes[n][2] - z);
303             if ( dist > radius ) { radius = dist; }
304         }
305         // printf("radius = %.2f\n", radius);
306
307         /* dump bounding sphere and header */
308         fprintf(out, "bs %.2f %.2f %.2f %.2f\n", x, y, z, radius);
309         fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
310         /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
311         i += 3;
312
313         /* dump rest of strip (until -1) */
314         while ( (i < list_ptr) && (list[i] != -1) ) { 
315             fprintf(out, "q %d", list[i]);
316             i++;
317             if ( (i < list_ptr) && (list[i] != -1) ) { 
318                 fprintf(out, " %d", list[i]);
319                 i++;
320             }
321             fprintf(out, "\n");
322         }
323
324         i++;
325     }
326 }
327
328
329 /* Check the direction the current triangle faces, compared to it's
330  * pregenerated normal.  Returns the dot product between the target
331  * normal and actual normal.  If the dot product is close to 1.0, they
332  * nearly match.  If the are close to -1.0, the are nearly
333  * opposite. */
334 double check_cur_face(int n1, int n2, int n3) {
335     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
336
337     /* check for the proper rotation by calculating an approximate
338      * normal and seeing if it is close to the precalculated normal */
339     v1[0] = nodes[n2][0] - nodes[n1][0];
340     v1[1] = nodes[n2][1] - nodes[n1][1];
341     v1[2] = nodes[n2][2] - nodes[n1][2];
342     v2[0] = nodes[n3][0] - nodes[n1][0];
343     v2[1] = nodes[n3][1] - nodes[n1][1];
344     v2[2] = nodes[n3][2] - nodes[n1][2];
345
346     MAT3cross_product(approx_normal, v1, v2);
347     MAT3_NORMALIZE_VEC(approx_normal,temp);
348     dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
349
350     /* not first triangle */
351     /* if ( ((dot_prod < -0.5) && !is_backwards) ||
352          ((dot_prod >  0.5) && is_backwards) ) {
353         printf("    Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
354                approx_normal[1], approx_normal[2]);
355         printf("    Dot product = %.4f\n", dot_prod);
356     } */
357     /* angle = acos(dot_prod); */
358     /* printf("Normal ANGLE = %.3f rads.\n", angle); */
359
360     return(dot_prod);
361 }
362
363
364 /* Load a .obj file */
365 void obj_fix(char *infile, char *outfile) {
366     char line[256];
367     double dot_prod;
368     int first, n1, n2, n3, n4;
369     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
370     int is_ccw;
371
372     if ( (in = fopen(infile, "r")) == NULL ) {
373         printf("Cannot open file: %s\n", infile);
374         exit(-1);
375     }
376
377     if ( (out = fopen(outfile, "w")) == NULL ) {
378         printf("Cannot open file: %s\n", outfile);
379         exit(-1);
380     }
381
382     list_init(&ccw_list_ptr);
383     list_init(&cw_list_ptr);
384
385     /* I start counting at one because that is how the triangle
386        program refers to nodes and normals */
387     first = 1;
388     ncount = 1;
389     vncount = 1;
390     fcount = 1;
391
392     printf("Reading file:  %s\n", infile);
393
394     while ( fgets(line, 250, in) != NULL ) {
395         if ( line[0] == '#' ) {
396             /* pass along the comments verbatim */
397             fprintf(out, "%s", line);
398         } else if ( strlen(line) <= 1 ) {
399             /* don't pass along empty lines */
400             // fprintf(out, "%s", line);
401         } else if ( strncmp(line, "v ", 2) == 0 ) {
402             /* save vertex to memory and output to file */
403             if ( ncount < MAXNODES ) {
404                 /* printf("vertex = %s", line); */
405                 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
406                 nodes[ncount][0] = x;
407                 nodes[ncount][1] = y;
408                 nodes[ncount][2] = z;
409
410                 /* first time through set min's and max'es */
411                 if ( ncount == 1 ) {
412                     xmin = x;
413                     xmax = x;
414                     ymin = y;
415                     ymax = y;
416                     zmin = z;
417                     zmax = z;
418                 }
419     
420                 /* keep track of min/max vertex values */
421                 if ( x < xmin ) xmin = x;
422                 if ( x > xmax ) xmax = x;
423                 if ( y < ymin ) ymin = y;
424                 if ( y > ymax ) ymax = y;
425                 if ( z < zmin ) zmin = z;
426                 if ( z > zmax ) zmax = z;               
427
428                 // fprintf(out, "v %.2f %.2f %.2f\n", 
429                 //       nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
430                 ncount++;
431             } else {
432                 printf("Read too many nodes ... dying :-(\n");
433                 exit(-1);
434             }
435         } else if ( strncmp(line, "vn ", 3) == 0 ) {
436             /* save vertex normals to memory and output to file */
437             if ( vncount < MAXNODES ) {
438                 /* printf("vertex normal = %s", line); */
439                 sscanf(line, "vn %lf %lf %lf\n", 
440                        &normals[vncount][0], &normals[vncount][1], 
441                        &normals[vncount][2]);
442                 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
443                 //      normals[vncount][1], normals[vncount][2]);
444                 vncount++;
445             } else {
446                 printf("Read too many vertex normals ... dying :-(\n");
447                 exit(-1);
448             }
449         } else if ( line[0] == 't' ) {
450             /* starting a new triangle strip */
451
452             printf("Starting a new triangle strip\n");
453
454             n1 = n2 = n3 = n4 = 0;
455
456             printf("new tri strip = %s", line);
457             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
458
459             /* special cases to handle bugs in our beloved tri striper */
460             if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
461                 n2 = 3;
462             }
463             if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
464                 n3 = 4;
465             }
466
467             dot_prod = check_cur_face(n1, n2, n3);
468             if ( dot_prod < 0.0 ) {
469                 /* this stripe is backwards (CW) */
470                 is_ccw = 0;
471                 printf(" -> Starting a backwards stripe\n");
472             } else {
473                 /* this stripe is normal (CCW) */
474                 is_ccw = 1;
475             }
476
477             if ( is_ccw ) {
478                 if ( ccw_list_ptr ) {
479                     list_add(ccw_list, &ccw_list_ptr, -1);
480                 }
481
482                 list_add(ccw_list, &ccw_list_ptr, n1);
483                 list_add(ccw_list, &ccw_list_ptr, n2);
484                 list_add(ccw_list, &ccw_list_ptr, n3);
485             } else {
486                 if ( cw_list_ptr ) {
487                     list_add(cw_list, &cw_list_ptr, -1);
488                 }
489
490                 list_add(cw_list, &cw_list_ptr, n1);
491                 list_add(cw_list, &cw_list_ptr, n2);
492                 list_add(cw_list, &cw_list_ptr, n3);
493             }
494
495             if ( n4 > 0 ) {
496                 if ( is_ccw ) {
497                     list_add(ccw_list, &ccw_list_ptr, n4);
498                 } else {
499                     list_add(cw_list, &cw_list_ptr, n4);
500                 }
501             }
502         } else if ( line[0] == 'f' ) {
503             if ( fcount < MAXNODES ) {
504                 /* pass along the unoptimized faces verbatim */
505                 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
506                 faces[fcount][0] = n1;
507                 faces[fcount][1] = n2;
508                 faces[fcount][2] = n3;
509
510                 fcount++;
511             } else {
512                 printf("Read too many unoptimized faces ... dying :-(\n");
513                 exit(-1);
514             }
515  
516             // fprintf(out, "%s", line);
517         } else if ( line[0] == 'q' ) {
518             /* continue a triangle strip */
519             n1 = n2 = 0;
520
521             /* printf("continued tri strip = %s ", line); */
522             sscanf(line, "q %d %d\n", &n1, &n2);
523
524             if ( is_ccw ) {
525                 list_add(ccw_list, &ccw_list_ptr, n1);
526             } else {
527                 list_add(cw_list, &cw_list_ptr, n1);
528             }
529
530             if ( n2 > 0 ) {
531                 if ( is_ccw ) {
532                     list_add(ccw_list, &ccw_list_ptr, n2);
533                 } else {
534                     list_add(cw_list, &cw_list_ptr, n2);
535                 }
536             }
537         } else {
538             printf("Unknown line in %s = %s\n", infile, line);
539         }
540     }
541
542     /* reference point is the "center" */
543     refx = (xmin + xmax) / 2.0;
544     refy = (ymin + ymax) / 2.0;
545     refz = (zmin + zmax) / 2.0;
546
547     /* convert the cw_list to ccw add append to ccw_list */
548     fix_cw_list(cw_list, cw_list_ptr);
549
550     dump_global_bounds();
551     dump_nodes();
552     dump_normals();
553     if ( fcount > 1 ) {
554         dump_faces();
555     }
556
557     dump_list(ccw_list, ccw_list_ptr);
558
559     fclose(in);
560     fclose(out);
561 }
562
563
564 /* $Log$
565 /* Revision 1.16  1998/05/27 02:27:22  curt
566 /* Commented out a couple of debugging messages.
567 /*
568  * Revision 1.15  1998/05/24 02:47:47  curt
569  * For each strip, specify a default material property and calculate a center
570  * and bounding sphere.
571  *
572  * Revision 1.14  1998/05/23 15:19:49  curt
573  * Output more digits after the decimal place.
574  *
575  * Revision 1.13  1998/05/20 20:55:19  curt
576  * Fixed arbitrary polygon winding problem here so all tristrips are passed
577  * to runtime simulator with a consistant counter clockwise winding.
578  *
579  * Revision 1.12  1998/05/16 13:11:26  curt
580  * Fixed an off by one error in node, normal, and face counters.
581  *
582  * Revision 1.11  1998/04/27 15:59:24  curt
583  * Fixed an off by one error.
584  *
585  * Revision 1.10  1998/04/27 03:33:11  curt
586  * Code now calculates a center reference points and outputs everything
587  * relative to that.  This is useful in the rendering engine to keep everything
588  * close to (0, 0, 0) where we can avoid many GLfloat precision problems.
589  *
590  * Revision 1.9  1998/04/18 04:01:03  curt
591  * Now use libMath rather than having local copies of math routines.
592  *
593  * Revision 1.8  1998/04/08 23:19:37  curt
594  * Adopted Gnu automake/autoconf system.
595  *
596  * Revision 1.7  1998/03/19 02:51:41  curt
597  * Added special case handling to compensate for bugs in our beloved tri striper
598  *
599  * Revision 1.6  1998/03/03 15:36:12  curt
600  * Tweaks for compiling with g++
601  *
602  * Revision 1.5  1998/03/03 03:37:03  curt
603  * Cumulative tweaks.
604  *
605  * Revision 1.4  1998/01/31 00:41:25  curt
606  * Made a few changes converting floats to doubles.
607  *
608  * Revision 1.3  1998/01/19 19:51:07  curt
609  * A couple final pre-release tweaks.
610  *
611  * Revision 1.2  1998/01/09 23:03:12  curt
612  * Restructured to split 1deg x 1deg dem's into 64 subsections.
613  *
614  * Revision 1.1  1997/12/08 19:28:54  curt
615  * Initial revision.
616  *
617  */