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