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