]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
Add xgl wrappers for debugging.
[flightgear.git] / Scenery / 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 #ifdef WIN32
28 #  include <windows.h>
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <GL/glut.h>
34 #include "../XGL/xgl.h"
35
36 #include "obj.h"
37 #include "scenery.h"
38
39 #include "../Math/mat3.h"
40
41
42
43 #define MAXNODES 100000
44
45 float nodes[MAXNODES][3];
46 float normals[MAXNODES][3];
47
48
49 /* given three points defining a triangle, calculate the normal */
50 void calc_normal(float p1[3], float p2[3], float p3[3], double normal[3])
51 {
52     double v1[3], v2[3];
53     float temp;
54
55     v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
56     v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
57
58     MAT3cross_product(normal, v1, v2);
59     MAT3_NORMALIZE_VEC(normal,temp);
60
61     printf("  Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
62 }
63
64
65 /* Load a .obj file and generate the GL call list */
66 GLint fgObjLoad(char *path) {
67     char line[256], winding_str[256];
68     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
69     struct fgCartesianPoint ref;
70     GLint area;
71     FILE *f;
72     int first, ncount, vncount, n1, n2, n3, n4;
73     int i, winding;
74     int last1, last2, odd;
75
76     if ( (f = fopen(path, "r")) == NULL ) {
77         printf("Cannot open file: %s\n", path);
78         exit(-1);
79     }
80
81     area = xglGenLists(1);
82     xglNewList(area, GL_COMPILE);
83
84     first = 1;
85     ncount = 1;
86     vncount = 1;
87
88     while ( fgets(line, 250, f) != NULL ) {
89         if ( line[0] == '#' ) {
90             /* comment -- ignore */
91         } else if ( strncmp(line, "v ", 2) == 0 ) {
92             /* node (vertex) */
93             if ( ncount < MAXNODES ) {
94                 /* printf("vertex = %s", line); */
95                 sscanf(line, "v %f %f %f\n", 
96                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
97                 if ( ncount == 1 ) {
98                     /* first node becomes the reference point */
99                     ref.x = nodes[ncount][0];
100                     ref.y = nodes[ncount][1];
101                     ref.z = nodes[ncount][2];
102                     scenery.center = ref;
103                 }
104                 ncount++;
105             } else {
106                 printf("Read too many nodes ... dying :-(\n");
107                 exit(-1);
108             }
109         } else if ( strncmp(line, "vn ", 3) == 0 ) {
110             /* vertex normal */
111             if ( vncount < MAXNODES ) {
112                 /* printf("vertex normal = %s", line); */
113                 sscanf(line, "vn %f %f %f\n", 
114                        &normals[vncount][0], &normals[vncount][1], 
115                        &normals[vncount][2]);
116                 vncount++;
117             } else {
118                 printf("Read too many vertex normals ... dying :-(\n");
119                 exit(-1);
120             }
121         } else if ( strncmp(line, "winding ", 8) == 0 ) {
122             sscanf(line+8, "%s", winding_str);
123             printf("WINDING = %s\n", winding_str);
124
125             /* can't call xglFrontFace() between xglBegin() & xglEnd() */
126             xglEnd();
127             first = 1;
128
129             if ( strcmp(winding_str, "cw") == 0 ) {
130                 xglFrontFace( GL_CW );
131                 winding = 0;
132             } else {
133                 xglFrontFace ( GL_CCW );
134                 winding = 1;
135             }
136         } else if ( line[0] == 't' ) {
137             /* start a new triangle strip */
138
139             n1 = n2 = n3 = n4 = 0;
140
141             if ( !first ) {
142                 /* close out the previous structure and start the next */
143                 xglEnd();
144             } else {
145                 first = 0;
146             }
147
148             printf("new tri strip = %s", line);
149             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
150
151             /* printf("(t) = "); */
152
153             /* try to get the proper rotation by calculating an
154              * approximate normal and seeing if it is close to the
155              * 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             MAT3cross_product(approx_normal, v1, v2);
163             MAT3_NORMALIZE_VEC(approx_normal,temp);
164             printf("Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
165                    approx_normal[1], approx_normal[2]);
166             dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
167             printf("Dot product = %.4f\n", dot_prod); */
168             /* angle = acos(dot_prod); */
169             /* printf("Normal ANGLE = %.3f rads.\n", angle); */
170
171             /* if ( dot_prod < -0.5 ) {
172                 xglFrontFace( GL_CW );
173             } else {
174                 xglFrontFace( GL_CCW );
175             } */
176
177             xglBegin(GL_TRIANGLE_STRIP);
178
179             if ( winding ) {
180                 odd = 1; 
181             } else {
182                 odd = 0;
183             }
184
185             if ( odd ) {
186                 calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
187             } else {
188                 calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
189             }
190             xglNormal3dv(approx_normal);
191
192             /* xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]); */
193             xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
194                         nodes[n1][2] - ref.z);
195
196             /* xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]); */
197             xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
198                         nodes[n2][2] - ref.z);
199
200             /* xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]); */
201             xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
202                         nodes[n3][2] - ref.z);
203
204             odd = 1 - odd;
205             last1 = n2;
206             last2 = n3;
207
208             if ( n4 > 0 ) {
209                 if ( odd ) {
210                     calc_normal(nodes[last1], nodes[last2], nodes[n4], 
211                                 approx_normal);
212                 } else {
213                     calc_normal(nodes[last2], nodes[last1], nodes[n4], 
214                                 approx_normal);
215                 }
216                 calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
217                 xglNormal3dv(approx_normal);
218
219                 /*xglNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);*/
220                 xglVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y, 
221                             nodes[n4][2] - ref.z);
222
223                 odd = 1 - odd;
224                 last1 = n3;
225                 last2 = n4;
226             }
227         } else if ( line[0] == 'f' ) {
228             /* unoptimized face */
229
230             if ( !first ) {
231                 /* close out the previous structure and start the next */
232                 xglEnd();
233             } else {
234                 first = 0;
235             }
236
237             xglBegin(GL_TRIANGLES);
238
239             /* printf("new triangle = %s", line);*/
240             sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
241
242             xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
243             xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
244                         nodes[n1][2] - ref.z);
245
246             xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
247             xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
248                         nodes[n2][2] - ref.z);
249
250             xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
251             xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
252                         nodes[n3][2] - ref.z);
253         } else if ( line[0] == 'q' ) {
254             /* continue a triangle strip */
255             n1 = n2 = 0;
256
257             /* printf("continued tri strip = %s ", line); */
258             sscanf(line, "q %d %d\n", &n1, &n2);
259             /* printf("read %d %d\n", n1, n2); */
260
261             if ( odd ) {
262                 calc_normal(nodes[last1], nodes[last2], nodes[n1], 
263                             approx_normal);
264             } else {
265                 calc_normal(nodes[last2], nodes[last1], nodes[n1], 
266                             approx_normal);
267             }
268             xglNormal3dv(approx_normal);
269
270             /* xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]); */
271             xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
272                         nodes[n1][2] - ref.z);
273             
274             odd = 1 - odd;
275             last1 = last2;
276             last2 = n1;
277
278             if ( n2 > 0 ) {
279                 /* printf(" (cont)\n"); */
280                 if ( odd ) {
281                     calc_normal(nodes[last1], nodes[last2], nodes[n2], 
282                                 approx_normal);
283                 } else {
284                     calc_normal(nodes[last2], nodes[last1], nodes[n2], 
285                                 approx_normal);
286                 }
287                 xglNormal3dv(approx_normal);
288
289                 /*xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);*/
290                 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
291                             nodes[n2][2] - ref.z);
292
293                 odd = 1 -odd;
294                 last1 = last2;
295                 last2 = n2;
296             }
297         } else {
298             printf("Unknown line in %s = %s\n", path, line);
299         }
300     }
301
302     xglEnd();
303
304     /* Draw normal vectors (for visually verifying normals)*/
305     /*
306     xglBegin(GL_LINES);
307     xglColor3f(0.0, 0.0, 0.0);
308     for ( i = 0; i < ncount; i++ ) {
309         xglVertex3d(nodes[i][0] - ref.x,
310                     nodes[i][1] - ref.y,
311                     nodes[i][2] - ref.z);
312         xglVertex3d(nodes[i][0] - ref.x + 500*normals[i][0],
313                     nodes[i][1] - ref.y + 500*normals[i][1],
314                     nodes[i][2] - ref.z + 500*normals[i][2]);
315     } 
316     xglEnd();
317     */
318
319     xglEndList();
320
321     fclose(f);
322
323     return(area);
324 }
325
326
327 /* $Log$
328 /* Revision 1.11  1997/12/15 23:55:01  curt
329 /* Add xgl wrappers for debugging.
330 /* Generate terrain normals on the fly.
331 /*
332  * Revision 1.10  1997/12/12 21:41:28  curt
333  * More light/material property tweaking ... still a ways off.
334  *
335  * Revision 1.9  1997/12/12 19:52:57  curt
336  * Working on lightling and material properties.
337  *
338  * Revision 1.8  1997/12/10 01:19:51  curt
339  * Tweaks for verion 0.15 release.
340  *
341  * Revision 1.7  1997/12/08 22:51:17  curt
342  * Enhanced to handle ccw and cw tri-stripe winding.  This is a temporary
343  * admission of defeat.  I will eventually go back and get all the stripes
344  * wound the same way (ccw).
345  *
346  * Revision 1.6  1997/11/25 19:25:35  curt
347  * Changes to integrate Durk's moon/sun code updates + clean up.
348  *
349  * Revision 1.5  1997/11/15 18:16:39  curt
350  * minor tweaks.
351  *
352  * Revision 1.4  1997/11/14 00:26:49  curt
353  * Transform scenery coordinates earlier in pipeline when scenery is being
354  * created, not when it is being loaded.  Precalculate normals for each node
355  * as average of the normals of each containing polygon so Garoude shading is
356  * now supportable.
357  *
358  * Revision 1.3  1997/10/31 04:49:12  curt
359  * Tweaking vertex orders.
360  *
361  * Revision 1.2  1997/10/30 12:38:45  curt
362  * Working on new scenery subsystem.
363  *
364  * Revision 1.1  1997/10/28 21:14:54  curt
365  * Initial revision.
366  *
367  */