]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.cxx
25690d5b4b80d55b7c3a52ee848ac3e9414a80eb
[flightgear.git] / FixObj / obj.cxx
1 // obj.cxx -- routines to handle WaveFront .obj format files.
2 //
3 // Written by Curtis Olson, started October 1997.
4 //
5 // Copyright (C) 1997 - 1998  Curtis L. Olson - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "obj.hxx"
29
30 #include <Math/mat3.h>
31
32
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
100             // reverse 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 opposite.
333 double check_cur_face(int n1, int n2, int n3) {
334     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
335
336     // check for the proper rotation by calculating an approximate
337     // normal and seeing if it is close to the precalculated normal
338     v1[0] = nodes[n2][0] - nodes[n1][0];
339     v1[1] = nodes[n2][1] - nodes[n1][1];
340     v1[2] = nodes[n2][2] - nodes[n1][2];
341     v2[0] = nodes[n3][0] - nodes[n1][0];
342     v2[1] = nodes[n3][1] - nodes[n1][1];
343     v2[2] = nodes[n3][2] - nodes[n1][2];
344
345     MAT3cross_product(approx_normal, v1, v2);
346     MAT3_NORMALIZE_VEC(approx_normal,temp);
347     dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
348
349     // not first triangle
350     // if ( ((dot_prod < -0.5) && !is_backwards) ||
351     //     ((dot_prod >  0.5) && is_backwards) ) {
352     //     printf("    Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
353     //            approx_normal[1], approx_normal[2]);
354     //     printf("    Dot product = %.4f\n", dot_prod);
355     // }
356     // angle = acos(dot_prod);
357     // printf("Normal ANGLE = %.3f rads.\n", angle);
358
359     return(dot_prod);
360 }
361
362
363 // Load a .obj file
364 void obj_fix(char *infile, char *outfile) {
365     char line[256];
366     double dot_prod;
367     int first, n1, n2, n3, n4;
368     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
369     int is_ccw;
370
371     if ( (in = fopen(infile, "r")) == NULL ) {
372         printf("Cannot open file: %s\n", infile);
373         exit(-1);
374     }
375
376     if ( (out = fopen(outfile, "w")) == NULL ) {
377         printf("Cannot open file: %s\n", outfile);
378         exit(-1);
379     }
380
381     list_init(&ccw_list_ptr);
382     list_init(&cw_list_ptr);
383
384     // I start counting at one because that is how the triangle
385     // program refers to nodes and normals
386     first = 1;
387     ncount = 1;
388     vncount = 1;
389     fcount = 1;
390
391     printf("Reading file:  %s\n", infile);
392
393     while ( fgets(line, 250, in) != NULL ) {
394         if ( line[0] == '#' ) {
395             // pass along the comments verbatim
396             fprintf(out, "%s", line);
397         } else if ( strlen(line) <= 1 ) {
398             // don't pass along empty lines
399             // fprintf(out, "%s", line);
400         } else if ( strncmp(line, "v ", 2) == 0 ) {
401             // save vertex to memory and output to file
402             if ( ncount < MAXNODES ) {
403                 // printf("vertex = %s", line);
404                 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
405                 nodes[ncount][0] = x;
406                 nodes[ncount][1] = y;
407                 nodes[ncount][2] = z;
408
409                 // first time through set min's and max'es
410                 if ( ncount == 1 ) {
411                     xmin = x;
412                     xmax = x;
413                     ymin = y;
414                     ymax = y;
415                     zmin = z;
416                     zmax = z;
417                 }
418     
419                 // keep track of min/max vertex values
420                 if ( x < xmin ) xmin = x;
421                 if ( x > xmax ) xmax = x;
422                 if ( y < ymin ) ymin = y;
423                 if ( y > ymax ) ymax = y;
424                 if ( z < zmin ) zmin = z;
425                 if ( z > zmax ) zmax = z;               
426
427                 // fprintf(out, "v %.2f %.2f %.2f\n", 
428                 //       nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
429                 ncount++;
430             } else {
431                 printf("Read too many nodes ... dying :-(\n");
432                 exit(-1);
433             }
434         } else if ( strncmp(line, "vn ", 3) == 0 ) {
435             // save vertex normals to memory and output to file
436             if ( vncount < MAXNODES ) {
437                 // printf("vertex normal = %s", line);
438                 sscanf(line, "vn %lf %lf %lf\n", 
439                        &normals[vncount][0], &normals[vncount][1], 
440                        &normals[vncount][2]);
441                 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
442                 //      normals[vncount][1], normals[vncount][2]);
443                 vncount++;
444             } else {
445                 printf("Read too many vertex normals ... dying :-(\n");
446                 exit(-1);
447             }
448         } else if ( line[0] == 't' ) {
449             // starting a new triangle strip
450
451             printf("Starting a new triangle strip\n");
452
453             n1 = n2 = n3 = n4 = 0;
454
455             printf("new tri strip = %s", line);
456             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
457
458             // special cases to handle bugs in our beloved tri striper
459             if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
460                 n2 = 3;
461             }
462             if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
463                 n3 = 4;
464             }
465
466             dot_prod = check_cur_face(n1, n2, n3);
467             if ( dot_prod < 0.0 ) {
468                 // this stripe is backwards (CW)
469                 is_ccw = 0;
470                 printf(" -> Starting a backwards stripe\n");
471             } else {
472                 // this stripe is normal (CCW)
473                 is_ccw = 1;
474             }
475
476             if ( is_ccw ) {
477                 if ( ccw_list_ptr ) {
478                     list_add(ccw_list, &ccw_list_ptr, -1);
479                 }
480
481                 list_add(ccw_list, &ccw_list_ptr, n1);
482                 list_add(ccw_list, &ccw_list_ptr, n2);
483                 list_add(ccw_list, &ccw_list_ptr, n3);
484             } else {
485                 if ( cw_list_ptr ) {
486                     list_add(cw_list, &cw_list_ptr, -1);
487                 }
488
489                 list_add(cw_list, &cw_list_ptr, n1);
490                 list_add(cw_list, &cw_list_ptr, n2);
491                 list_add(cw_list, &cw_list_ptr, n3);
492             }
493
494             if ( n4 > 0 ) {
495                 if ( is_ccw ) {
496                     list_add(ccw_list, &ccw_list_ptr, n4);
497                 } else {
498                     list_add(cw_list, &cw_list_ptr, n4);
499                 }
500             }
501         } else if ( line[0] == 'f' ) {
502             if ( fcount < MAXNODES ) {
503                 // pass along the unoptimized faces verbatim
504                 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
505                 faces[fcount][0] = n1;
506                 faces[fcount][1] = n2;
507                 faces[fcount][2] = n3;
508
509                 fcount++;
510             } else {
511                 printf("Read too many unoptimized faces ... dying :-(\n");
512                 exit(-1);
513             }
514  
515             // fprintf(out, "%s", line);
516         } else if ( line[0] == 'q' ) {
517             // continue a triangle strip
518             n1 = n2 = 0;
519
520             // printf("continued tri strip = %s ", line);
521             sscanf(line, "q %d %d\n", &n1, &n2);
522
523             if ( is_ccw ) {
524                 list_add(ccw_list, &ccw_list_ptr, n1);
525             } else {
526                 list_add(cw_list, &cw_list_ptr, n1);
527             }
528
529             if ( n2 > 0 ) {
530                 if ( is_ccw ) {
531                     list_add(ccw_list, &ccw_list_ptr, n2);
532                 } else {
533                     list_add(cw_list, &cw_list_ptr, n2);
534                 }
535             }
536         } else {
537             printf("Unknown line in %s = %s\n", infile, line);
538         }
539     }
540
541     // reference point is the "center"
542     refx = (xmin + xmax) / 2.0;
543     refy = (ymin + ymax) / 2.0;
544     refz = (zmin + zmax) / 2.0;
545
546     // convert the cw_list to ccw add append to ccw_list
547     fix_cw_list(cw_list, cw_list_ptr);
548
549     dump_global_bounds();
550     dump_nodes();
551     dump_normals();
552     if ( fcount > 1 ) {
553         dump_faces();
554     }
555
556     dump_list(ccw_list, ccw_list_ptr);
557
558     fclose(in);
559     fclose(out);
560 }
561
562
563 // $Log$
564 // Revision 1.1  1998/06/08 17:11:46  curt
565 // Renamed *.[ch] to *.[ch]xx
566 //
567 // Revision 1.16  1998/05/27 02:27:22  curt
568 // Commented out a couple of debugging messages.
569 //
570 // Revision 1.15  1998/05/24 02:47:47  curt
571 // For each strip, specify a default material property and calculate a center
572 // and bounding sphere.
573 //
574 // Revision 1.14  1998/05/23 15:19:49  curt
575 // Output more digits after the decimal place.
576 //
577 // Revision 1.13  1998/05/20 20:55:19  curt
578 // Fixed arbitrary polygon winding problem here so all tristrips are passed
579 // to runtime simulator with a consistant counter clockwise winding.
580 //
581 // Revision 1.12  1998/05/16 13:11:26  curt
582 // Fixed an off by one error in node, normal, and face counters.
583 //
584 // Revision 1.11  1998/04/27 15:59:24  curt
585 // Fixed an off by one error.
586 //
587 // Revision 1.10  1998/04/27 03:33:11  curt
588 // Code now calculates a center reference points and outputs everything
589 // relative to that.  This is useful in the rendering engine to keep everything
590 // close to (0, 0, 0) where we can avoid many GLfloat precision problems.
591 //
592 // Revision 1.9  1998/04/18 04:01:03  curt
593 // Now use libMath rather than having local copies of math routines.
594 //
595 // Revision 1.8  1998/04/08 23:19:37  curt
596 // Adopted Gnu automake/autoconf system.
597 //
598 // Revision 1.7  1998/03/19 02:51:41  curt
599 // Added special case handling to compensate for bugs in our beloved tri striper
600 //
601 // Revision 1.6  1998/03/03 15:36:12  curt
602 // Tweaks for compiling with g++
603 //
604 // Revision 1.5  1998/03/03 03:37:03  curt
605 // Cumulative tweaks.
606 //
607 // Revision 1.4  1998/01/31 00:41:25  curt
608 // Made a few changes converting floats to doubles.
609 //
610 // Revision 1.3  1998/01/19 19:51:07  curt
611 // A couple final pre-release tweaks.
612 //
613 // Revision 1.2  1998/01/09 23:03:12  curt
614 // Restructured to split 1deg x 1deg dem's into 64 subsections.
615 //
616 // Revision 1.1  1997/12/08 19:28:54  curt
617 // Initial revision.
618 //
619