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