]> git.mxchange.org Git - flightgear.git/blob - FixObj/obj.c
fde62bbbc0deaca62764cb94d7298b1ded5fd864
[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 "../../Src/Math/mat3.h"
33
34
35 /* what do ya' know, here's some global variables */
36 double nodes[MAXNODES][3];
37 double normals[MAXNODES][3];
38
39 int ccw_list[MAXNODES];
40 int ccw_list_ptr;
41
42 int cw_list[MAXNODES];
43 int cw_list_ptr;
44
45 FILE *in, *out;
46
47
48 /* some simple list routines */
49
50 /* reset the list */
51 void list_init(int *list_ptr) {
52     *list_ptr = 0;
53 }
54
55
56 /* add to list */
57 void list_add(int *list, int *list_ptr, int node) {
58     if ( *list_ptr >= MAXNODES ) {
59         printf("ERROR: list overflow in list_add()\n");
60         exit(-1);
61     }
62
63     list[*list_ptr] = node;
64     *list_ptr += 1;
65
66     /* printf("list pointer = %d  adding %d\n", *list_ptr, node); */
67 }
68
69
70 /* dump list */
71 void dump_list(int *list, int list_ptr) {
72     int i;
73
74     if ( list_ptr < 3 ) {
75         printf("List is empty ... skipping\n");
76         return;
77     }
78
79     printf("Dumping list, size = %d\n", list_ptr);
80
81     i = 0;
82     while ( i < list_ptr ) { 
83         /* do next strip */
84
85         /* dump header */
86         fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
87         /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
88         i += 3;
89
90         /* dump rest of strip (until -1) */
91         while ( (i < list_ptr) && (list[i] != -1) ) { 
92             fprintf(out, "q %d", list[i]);
93             i++;
94             if ( (i < list_ptr) && (list[i] != -1) ) { 
95                 fprintf(out, " %d", list[i]);
96                 i++;
97             }
98             fprintf(out, "\n");
99         }
100
101         i++;
102     }
103 }
104
105
106 /* Check the direction the current triangle faces, compared to it's
107  * pregenerated normal.  Returns the dot product between the target
108  * normal and actual normal.  If the dot product is close to 1.0, they
109  * nearly match.  If the are close to -1.0, the are nearly
110  * opposite. */
111 double check_cur_face(int n1, int n2, int n3) {
112     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
113
114     /* check for the proper rotation by calculating an approximate
115      * normal and seeing if it is close to the precalculated normal */
116     v1[0] = nodes[n2][0] - nodes[n1][0];
117     v1[1] = nodes[n2][1] - nodes[n1][1];
118     v1[2] = nodes[n2][2] - nodes[n1][2];
119     v2[0] = nodes[n3][0] - nodes[n1][0];
120     v2[1] = nodes[n3][1] - nodes[n1][1];
121     v2[2] = nodes[n3][2] - nodes[n1][2];
122
123     MAT3cross_product(approx_normal, v1, v2);
124     MAT3_NORMALIZE_VEC(approx_normal,temp);
125     dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
126
127     /* not first triangle */
128     /* if ( ((dot_prod < -0.5) && !is_backwards) ||
129          ((dot_prod >  0.5) && is_backwards) ) {
130         printf("    Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
131                approx_normal[1], approx_normal[2]);
132         printf("    Dot product = %.4f\n", dot_prod);
133     } */
134     /* angle = acos(dot_prod); */
135     /* printf("Normal ANGLE = %.3f rads.\n", angle); */
136
137     return(dot_prod);
138 }
139
140
141 /* Load a .obj file */
142 void obj_fix(char *infile, char *outfile) {
143     char line[256];
144     double dot_prod;
145     int first, ncount, vncount, n1, n2, n3, n4;
146     int is_ccw;
147
148     if ( (in = fopen(infile, "r")) == NULL ) {
149         printf("Cannot open file: %s\n", infile);
150         exit(-1);
151     }
152
153     if ( (out = fopen(outfile, "w")) == NULL ) {
154         printf("Cannot open file: %s\n", outfile);
155         exit(-1);
156     }
157
158     list_init(&ccw_list_ptr);
159     list_init(&cw_list_ptr);
160
161     first = 1;
162     ncount = 1;
163     vncount = 1;
164
165     printf("Reading file:  %s\n", infile);
166
167     while ( fgets(line, 250, in) != NULL ) {
168         if ( line[0] == '#' ) {
169             /* pass along the comments verbatim */
170             fprintf(out, "%s", line);
171         } else if ( strlen(line) <= 1 ) {
172             /* pass along empty lines */
173             fprintf(out, "%s", line);
174         } else if ( strncmp(line, "v ", 2) == 0 ) {
175             /* save vertex to memory and output to file */
176             if ( ncount < MAXNODES ) {
177                 /* printf("vertex = %s", line); */
178                 sscanf(line, "v %lf %lf %lf\n", 
179                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
180                 fprintf(out, "v %.2f %.2f %.2f\n", 
181                        nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
182                 ncount++;
183             } else {
184                 printf("Read too many nodes ... dying :-(\n");
185                 exit(-1);
186             }
187         } else if ( strncmp(line, "vn ", 3) == 0 ) {
188             /* save vertex normals to memory and output to file */
189             if ( vncount < MAXNODES ) {
190                 /* printf("vertex normal = %s", line); */
191                 sscanf(line, "vn %lf %lf %lf\n", 
192                        &normals[vncount][0], &normals[vncount][1], 
193                        &normals[vncount][2]);
194                 fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], 
195                         normals[vncount][1], normals[vncount][2]);
196                 vncount++;
197             } else {
198                 printf("Read too many vertex normals ... dying :-(\n");
199                 exit(-1);
200             }
201         } else if ( line[0] == 't' ) {
202             /* starting a new triangle strip */
203
204             printf("Starting a new triangle strip\n");
205
206             n1 = n2 = n3 = n4 = 0;
207
208             printf("new tri strip = %s", line);
209             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
210
211             /* special case to handle a bug in our beloved tri striper */
212             if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
213                 n2 = 3;
214             }
215
216             dot_prod = check_cur_face(n1, n2, n3);
217             if ( dot_prod < -0.5 ) {
218                 /* this stripe is backwards (CW) */
219                 is_ccw = 0;
220                 printf(" -> Starting a backwards stripe\n");
221             } else {
222                 /* this stripe is normal (CCW) */
223                 is_ccw = 1;
224             }
225
226             if ( is_ccw ) {
227                 if ( ccw_list_ptr ) {
228                     list_add(ccw_list, &ccw_list_ptr, -1);
229                 }
230
231                 list_add(ccw_list, &ccw_list_ptr, n1);
232                 list_add(ccw_list, &ccw_list_ptr, n2);
233                 list_add(ccw_list, &ccw_list_ptr, n3);
234             } else {
235                 if ( cw_list_ptr ) {
236                     list_add(cw_list, &cw_list_ptr, -1);
237                 }
238
239                 list_add(cw_list, &cw_list_ptr, n1);
240                 list_add(cw_list, &cw_list_ptr, n2);
241                 list_add(cw_list, &cw_list_ptr, n3);
242             }
243
244             if ( n4 > 0 ) {
245                 if ( is_ccw ) {
246                     list_add(ccw_list, &ccw_list_ptr, n4);
247                 } else {
248                     list_add(cw_list, &cw_list_ptr, n4);
249                 }
250             }
251         } else if ( line[0] == 'f' ) {
252             /* pass along the unoptimized faces verbatim */
253             fprintf(out, "%s", line);
254         } else if ( line[0] == 'q' ) {
255             /* continue a triangle strip */
256             n1 = n2 = 0;
257
258             /* printf("continued tri strip = %s ", line); */
259             sscanf(line, "q %d %d\n", &n1, &n2);
260
261             if ( is_ccw ) {
262                 list_add(ccw_list, &ccw_list_ptr, n1);
263             } else {
264                 list_add(cw_list, &cw_list_ptr, n1);
265             }
266
267             if ( n2 > 0 ) {
268                 if ( is_ccw ) {
269                     list_add(ccw_list, &ccw_list_ptr, n2);
270                 } else {
271                     list_add(cw_list, &cw_list_ptr, n2);
272                 }
273             }
274         } else {
275             printf("Unknown line in %s = %s\n", infile, line);
276         }
277     }
278
279     fprintf(out, "winding ccw\n");
280     dump_list(ccw_list, ccw_list_ptr);
281
282     fprintf(out, "winding cw\n");
283     dump_list(cw_list, cw_list_ptr);
284
285     fclose(in);
286     fclose(out);
287 }
288
289
290 /* $Log$
291 /* Revision 1.4  1998/01/31 00:41:25  curt
292 /* Made a few changes converting floats to doubles.
293 /*
294  * Revision 1.3  1998/01/19 19:51:07  curt
295  * A couple final pre-release tweaks.
296  *
297  * Revision 1.2  1998/01/09 23:03:12  curt
298  * Restructured to split 1deg x 1deg dem's into 64 subsections.
299  *
300  * Revision 1.1  1997/12/08 19:28:54  curt
301  * Initial revision.
302  *
303  */