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