]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
Merged in make system changes from Bob Kuehne <rpk@sgi.com>
[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 <Scenery/obj.h>
37 #include <Scenery/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, struct fgCartesianPoint *ref) {
67     char line[256], winding_str[256];
68     double v1[3], v2[3], approx_normal[3], normal[3], dot_prod, scale, temp;
69     GLint area;
70     FILE *f;
71     int first, ncount, vncount, n1, n2, n3, n4;
72     static int use_vertex_norms = 1;
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         return(0);
80     }
81
82     area = xglGenLists(1);
83     xglNewList(area, GL_COMPILE);
84
85     first = 1;
86     ncount = 1;
87     vncount = 1;
88
89     while ( fgets(line, 250, f) != NULL ) {
90         if ( line[0] == '#' ) {
91             /* comment -- ignore */
92         } else if ( line[0] == '\n' ) {
93             /* empty line -- ignore */
94         } else if ( strncmp(line, "v ", 2) == 0 ) {
95             /* node (vertex) */
96             if ( ncount < MAXNODES ) {
97                 /* printf("vertex = %s", line); */
98                 sscanf(line, "v %f %f %f\n", 
99                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
100                 if ( ncount == 1 ) {
101                     /* first node becomes the reference point */
102                     ref->x = nodes[ncount][0];
103                     ref->y = nodes[ncount][1];
104                     ref->z = nodes[ncount][2];
105                     /* scenery.center = ref; */
106                 }
107                 ncount++;
108             } else {
109                 printf("Read too many nodes ... dying :-(\n");
110                 exit(-1);
111             }
112         } else if ( strncmp(line, "vn ", 3) == 0 ) {
113             /* vertex normal */
114             if ( vncount < MAXNODES ) {
115                 /* printf("vertex normal = %s", line); */
116                 sscanf(line, "vn %f %f %f\n", 
117                        &normals[vncount][0], &normals[vncount][1], 
118                        &normals[vncount][2]);
119                 vncount++;
120             } else {
121                 printf("Read too many vertex normals ... dying :-(\n");
122                 exit(-1);
123             }
124         } else if ( strncmp(line, "winding ", 8) == 0 ) {
125             sscanf(line+8, "%s", winding_str);
126             printf("    WINDING = %s\n", winding_str);
127
128             /* can't call xglFrontFace() between xglBegin() & xglEnd() */
129             xglEnd();
130             first = 1;
131
132             if ( strcmp(winding_str, "cw") == 0 ) {
133                 xglFrontFace( GL_CW );
134                 winding = 0;
135             } else {
136                 glFrontFace ( GL_CCW );
137                 winding = 1;
138             }
139         } else if ( line[0] == 't' ) {
140             /* start a new triangle strip */
141
142             n1 = n2 = n3 = n4 = 0;
143
144             if ( !first ) {
145                 /* close out the previous structure and start the next */
146                 xglEnd();
147             } else {
148                 first = 0;
149             }
150
151             /* printf("    new tri strip = %s", line); */
152             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
153
154             /* printf("(t) = "); */
155
156             xglBegin(GL_TRIANGLE_STRIP);
157
158             if ( winding ) {
159                 odd = 1; 
160                 scale = 1.0;
161             } else {
162                 odd = 0;
163                 scale = 1.0;
164             }
165
166             if ( use_vertex_norms ) {
167                 MAT3_SCALE_VEC(normal, normals[n1], scale);
168                 xglNormal3dv(normal);
169                 xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
170                             nodes[n1][2] - ref->z);
171
172                 MAT3_SCALE_VEC(normal, normals[n2], scale);
173                 xglNormal3dv(normal);
174                 xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
175                             nodes[n2][2] - ref->z);
176
177                 MAT3_SCALE_VEC(normal, normals[n3], scale);
178                 xglNormal3dv(normal);
179                 xglVertex3d(nodes[n3][0] - ref->x, nodes[n3][1] - ref->y, 
180                             nodes[n3][2] - ref->z);
181             } else {
182                 if ( odd ) {
183                     calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
184                 } else {
185                     calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
186                 }
187                 MAT3_SCALE_VEC(normal, approx_normal, scale);
188                 xglNormal3dv(normal);
189
190                 xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
191                             nodes[n1][2] - ref->z);
192                 xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
193                             nodes[n2][2] - ref->z);
194                 xglVertex3d(nodes[n3][0] - ref->x, nodes[n3][1] - ref->y, 
195                             nodes[n3][2] - ref->z);
196             }
197
198             odd = 1 - odd;
199             last1 = n2;
200             last2 = n3;
201
202             if ( n4 > 0 ) {
203                 if ( use_vertex_norms ) {
204                     MAT3_SCALE_VEC(normal, normals[n4], scale);
205                 } else {
206                     calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
207                     MAT3_SCALE_VEC(normal, approx_normal, scale);
208                 }
209                 xglNormal3dv(normal);
210                 xglVertex3d(nodes[n4][0] - ref->x, nodes[n4][1] - ref->y, 
211                             nodes[n4][2] - ref->z);
212
213                 odd = 1 - odd;
214                 last1 = n3;
215                 last2 = n4;
216             }
217         } else if ( line[0] == 'f' ) {
218             /* unoptimized face */
219
220             if ( !first ) {
221                 /* close out the previous structure and start the next */
222                 xglEnd();
223             } else {
224                 first = 0;
225             }
226
227             xglBegin(GL_TRIANGLES);
228
229             /* printf("new triangle = %s", line);*/
230             sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
231
232             xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
233             xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
234                         nodes[n1][2] - ref->z);
235
236             xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
237             xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
238                         nodes[n2][2] - ref->z);
239
240             xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
241             xglVertex3d(nodes[n3][0] - ref->x, nodes[n3][1] - ref->y, 
242                         nodes[n3][2] - ref->z);
243         } else if ( line[0] == 'q' ) {
244             /* continue a triangle strip */
245             n1 = n2 = 0;
246
247             /* printf("continued tri strip = %s ", line); */
248             sscanf(line, "q %d %d\n", &n1, &n2);
249             /* printf("read %d %d\n", n1, n2); */
250
251             if ( use_vertex_norms ) {
252                 MAT3_SCALE_VEC(normal, normals[n1], scale);
253                 xglNormal3dv(normal);
254             } else {
255                 if ( odd ) {
256                     calc_normal(nodes[last1], nodes[last2], nodes[n1], 
257                                 approx_normal);
258                 } else {
259                     calc_normal(nodes[last2], nodes[last1], nodes[n1], 
260                                 approx_normal);
261                 }
262                 MAT3_SCALE_VEC(normal, approx_normal, scale);
263                 xglNormal3dv(normal);
264             }
265
266             xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
267                         nodes[n1][2] - ref->z);
268             
269             odd = 1 - odd;
270             last1 = last2;
271             last2 = n1;
272
273             if ( n2 > 0 ) {
274                 /* printf(" (cont)\n"); */
275
276                 if ( use_vertex_norms ) {
277                     MAT3_SCALE_VEC(normal, normals[n2], scale);
278                     xglNormal3dv(normal);
279                 } else {
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                     MAT3_SCALE_VEC(normal, approx_normal, scale);
288                     xglNormal3dv(normal);
289                 }
290
291                 xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
292                             nodes[n2][2] - ref->z);
293
294                 odd = 1 -odd;
295                 last1 = last2;
296                 last2 = n2;
297             }
298         } else {
299             printf("Unknown line in %s = %s\n", path, line);
300         }
301     }
302
303     xglEnd();
304
305     /* Draw normal vectors (for visually verifying normals)*/
306     /*
307     xglBegin(GL_LINES);
308     xglColor3f(0.0, 0.0, 0.0);
309     for ( i = 0; i < ncount; i++ ) {
310         xglVertex3d(nodes[i][0] - ref->x,
311                     nodes[i][1] - ref->y,
312                     nodes[i][2] - ref->z);
313         xglVertex3d(nodes[i][0] - ref->x + 500*normals[i][0],
314                     nodes[i][1] - ref->y + 500*normals[i][1],
315                     nodes[i][2] - ref->z + 500*normals[i][2]);
316     } 
317     xglEnd();
318     */
319
320     xglFrontFace ( GL_CCW );
321
322     xglEndList();
323
324     fclose(f);
325
326     return(area);
327 }
328
329
330 /* $Log$
331 /* Revision 1.18  1998/01/19 19:27:16  curt
332 /* Merged in make system changes from Bob Kuehne <rpk@sgi.com>
333 /* This should simplify things tremendously.
334 /*
335  * Revision 1.17  1998/01/13 00:23:10  curt
336  * Initial changes to support loading and management of scenery tiles.  Note,
337  * there's still a fair amount of work left to be done.
338  *
339  * Revision 1.16  1997/12/30 23:09:40  curt
340  * Worked on winding problem without luck, so back to calling glFrontFace()
341  * 3 times for each scenery area.
342  *
343  * Revision 1.15  1997/12/30 20:47:51  curt
344  * Integrated new event manager with subsystem initializations.
345  *
346  * Revision 1.14  1997/12/30 01:38:46  curt
347  * Switched back to per vertex normals and smooth shading for terrain.
348  *
349  * Revision 1.13  1997/12/18 23:32:36  curt
350  * First stab at sky dome actually starting to look reasonable. :-)
351  *
352  * Revision 1.12  1997/12/17 23:13:47  curt
353  * Began working on rendering the sky.
354  *
355  * Revision 1.11  1997/12/15 23:55:01  curt
356  * Add xgl wrappers for debugging.
357  * Generate terrain normals on the fly.
358  *
359  * Revision 1.10  1997/12/12 21:41:28  curt
360  * More light/material property tweaking ... still a ways off.
361  *
362  * Revision 1.9  1997/12/12 19:52:57  curt
363  * Working on lightling and material properties.
364  *
365  * Revision 1.8  1997/12/10 01:19:51  curt
366  * Tweaks for verion 0.15 release.
367  *
368  * Revision 1.7  1997/12/08 22:51:17  curt
369  * Enhanced to handle ccw and cw tri-stripe winding.  This is a temporary
370  * admission of defeat.  I will eventually go back and get all the stripes
371  * wound the same way (ccw).
372  *
373  * Revision 1.6  1997/11/25 19:25:35  curt
374  * Changes to integrate Durk's moon/sun code updates + clean up.
375  *
376  * Revision 1.5  1997/11/15 18:16:39  curt
377  * minor tweaks.
378  *
379  * Revision 1.4  1997/11/14 00:26:49  curt
380  * Transform scenery coordinates earlier in pipeline when scenery is being
381  * created, not when it is being loaded.  Precalculate normals for each node
382  * as average of the normals of each containing polygon so Garoude shading is
383  * now supportable.
384  *
385  * Revision 1.3  1997/10/31 04:49:12  curt
386  * Tweaking vertex orders.
387  *
388  * Revision 1.2  1997/10/30 12:38:45  curt
389  * Working on new scenery subsystem.
390  *
391  * Revision 1.1  1997/10/28 21:14:54  curt
392  * Initial revision.
393  *
394  */