]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.c
Code now calculates a center reference points and outputs everything
[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 = 0; 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 = 0; 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 = 0; 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     first = 1;
203     ncount = 0;
204     vncount = 0;
205     fcount = 0;
206
207     printf("Reading file:  %s\n", infile);
208
209     while ( fgets(line, 250, in) != NULL ) {
210         if ( line[0] == '#' ) {
211             /* pass along the comments verbatim */
212             fprintf(out, "%s", line);
213         } else if ( strlen(line) <= 1 ) {
214             /* don't pass along empty lines */
215             // fprintf(out, "%s", line);
216         } else if ( strncmp(line, "v ", 2) == 0 ) {
217             /* save vertex to memory and output to file */
218             if ( ncount < MAXNODES ) {
219                 /* printf("vertex = %s", line); */
220                 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
221                 nodes[ncount][0] = x;
222                 nodes[ncount][1] = y;
223                 nodes[ncount][2] = z;
224
225                 /* first time through set min's and max'es */
226                 if ( ncount == 1 ) {
227                     xmin = x;
228                     xmax = x;
229                     ymin = y;
230                     ymax = y;
231                     zmin = z;
232                     zmax = z;
233                 }
234     
235                 /* keep track of min/max vertex values */
236                 if ( x < xmin ) xmin = x;
237                 if ( x > xmax ) xmax = x;
238                 if ( y < ymin ) ymin = y;
239                 if ( y > ymax ) ymax = y;
240                 if ( z < zmin ) zmin = z;
241                 if ( z > zmax ) zmax = z;               
242
243                 // fprintf(out, "v %.2f %.2f %.2f\n", 
244                 //       nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
245                 ncount++;
246             } else {
247                 printf("Read too many nodes ... dying :-(\n");
248                 exit(-1);
249             }
250         } else if ( strncmp(line, "vn ", 3) == 0 ) {
251             /* save vertex normals to memory and output to file */
252             if ( vncount < MAXNODES ) {
253                 /* printf("vertex normal = %s", line); */
254                 sscanf(line, "vn %lf %lf %lf\n", 
255                        &normals[vncount][0], &normals[vncount][1], 
256                        &normals[vncount][2]);
257                 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
258                 //      normals[vncount][1], normals[vncount][2]);
259                 vncount++;
260             } else {
261                 printf("Read too many vertex normals ... dying :-(\n");
262                 exit(-1);
263             }
264         } else if ( line[0] == 't' ) {
265             /* starting a new triangle strip */
266
267             printf("Starting a new triangle strip\n");
268
269             n1 = n2 = n3 = n4 = 0;
270
271             printf("new tri strip = %s", line);
272             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
273
274             /* special cases to handle bugs in our beloved tri striper */
275             if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
276                 n2 = 3;
277             }
278             if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
279                 n3 = 4;
280             }
281
282             dot_prod = check_cur_face(n1, n2, n3);
283             if ( dot_prod < 0.0 ) {
284                 /* this stripe is backwards (CW) */
285                 is_ccw = 0;
286                 printf(" -> Starting a backwards stripe\n");
287             } else {
288                 /* this stripe is normal (CCW) */
289                 is_ccw = 1;
290             }
291
292             if ( is_ccw ) {
293                 if ( ccw_list_ptr ) {
294                     list_add(ccw_list, &ccw_list_ptr, -1);
295                 }
296
297                 list_add(ccw_list, &ccw_list_ptr, n1);
298                 list_add(ccw_list, &ccw_list_ptr, n2);
299                 list_add(ccw_list, &ccw_list_ptr, n3);
300             } else {
301                 if ( cw_list_ptr ) {
302                     list_add(cw_list, &cw_list_ptr, -1);
303                 }
304
305                 list_add(cw_list, &cw_list_ptr, n1);
306                 list_add(cw_list, &cw_list_ptr, n2);
307                 list_add(cw_list, &cw_list_ptr, n3);
308             }
309
310             if ( n4 > 0 ) {
311                 if ( is_ccw ) {
312                     list_add(ccw_list, &ccw_list_ptr, n4);
313                 } else {
314                     list_add(cw_list, &cw_list_ptr, n4);
315                 }
316             }
317         } else if ( line[0] == 'f' ) {
318             if ( fcount < MAXNODES ) {
319                 /* pass along the unoptimized faces verbatim */
320                 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
321                 faces[fcount][0] = n1;
322                 faces[fcount][1] = n2;
323                 faces[fcount][2] = n3;
324
325                 fcount++;
326             } else {
327                 printf("Read too many unoptimized faces ... dying :-(\n");
328                 exit(-1);
329             }
330  
331             // fprintf(out, "%s", line);
332         } else if ( line[0] == 'q' ) {
333             /* continue a triangle strip */
334             n1 = n2 = 0;
335
336             /* printf("continued tri strip = %s ", line); */
337             sscanf(line, "q %d %d\n", &n1, &n2);
338
339             if ( is_ccw ) {
340                 list_add(ccw_list, &ccw_list_ptr, n1);
341             } else {
342                 list_add(cw_list, &cw_list_ptr, n1);
343             }
344
345             if ( n2 > 0 ) {
346                 if ( is_ccw ) {
347                     list_add(ccw_list, &ccw_list_ptr, n2);
348                 } else {
349                     list_add(cw_list, &cw_list_ptr, n2);
350                 }
351             }
352         } else {
353             printf("Unknown line in %s = %s\n", infile, line);
354         }
355     }
356
357     /* reference point is the "center" */
358     refx = (xmin + xmax) / 2.0;
359     refy = (ymin + ymax) / 2.0;
360     refz = (zmin + zmax) / 2.0;
361
362     fprintf(out, "\n");
363     fprintf(out, "ref %.2f %.2f %.2f\n", refx, refy, refz);     
364
365     dump_nodes();
366     dump_normals();
367     dump_faces();
368
369     fprintf(out, "winding ccw\n");
370     dump_list(ccw_list, ccw_list_ptr);
371
372     fprintf(out, "winding cw\n");
373     dump_list(cw_list, cw_list_ptr);
374
375     fclose(in);
376     fclose(out);
377 }
378
379
380 /* $Log$
381 /* Revision 1.10  1998/04/27 03:33:11  curt
382 /* Code now calculates a center reference points and outputs everything
383 /* relative to that.  This is useful in the rendering engine to keep everything
384 /* close to (0, 0, 0) where we can avoid many GLfloat precision problems.
385 /*
386  * Revision 1.9  1998/04/18 04:01:03  curt
387  * Now use libMath rather than having local copies of math routines.
388  *
389  * Revision 1.8  1998/04/08 23:19:37  curt
390  * Adopted Gnu automake/autoconf system.
391  *
392  * Revision 1.7  1998/03/19 02:51:41  curt
393  * Added special case handling to compensate for bugs in our beloved tri striper
394  *
395  * Revision 1.6  1998/03/03 15:36:12  curt
396  * Tweaks for compiling with g++
397  *
398  * Revision 1.5  1998/03/03 03:37:03  curt
399  * Cumulative tweaks.
400  *
401  * Revision 1.4  1998/01/31 00:41:25  curt
402  * Made a few changes converting floats to doubles.
403  *
404  * Revision 1.3  1998/01/19 19:51:07  curt
405  * A couple final pre-release tweaks.
406  *
407  * Revision 1.2  1998/01/09 23:03:12  curt
408  * Restructured to split 1deg x 1deg dem's into 64 subsections.
409  *
410  * Revision 1.1  1997/12/08 19:28:54  curt
411  * Initial revision.
412  *
413  */