3 * obj.c -- routines to handle WaveFront .obj format files.
5 * Written by Curtis Olson, started October 1997.
7 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
24 * (Log is kept at end of this file)
25 **************************************************************************/
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>
48 #define MAXNODES 100000
50 static double nodes[MAXNODES][3];
51 static double normals[MAXNODES][3];
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])
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];
63 MAT3cross_product(normal, v1, v2);
64 MAT3_NORMALIZE_VEC(normal,temp);
66 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " Normal = %.2f %.2f %.2f\n",
67 normal[0], normal[1], normal[2]);*/
71 #define FG_TEX_CONSTANT 128.0
73 float calc_lon(double x, double y, double z) {
75 tmp = (RAD_TO_DEG*atan2(y, x)) * FG_TEX_CONSTANT;
77 // printf("lon = %.2f\n", (float)tmp);
82 float calc_lat(double x, double y, double z) {
85 tmp = (90.0 - RAD_TO_DEG*atan2( sqrt(x*x + y*y), z )) * FG_TEX_CONSTANT;
87 // printf("lat = %.2f\n", (float)tmp);
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 };
100 int first, ncount, vncount, n1, n2, n3, n4;
101 static int use_per_vertex_norms = 1;
103 int last1, last2, odd;
105 if ( (f = fopen(path, "r")) == NULL ) {
106 fgPrintf(FG_TERRAIN, FG_ALERT, "Cannot open file: %s\n", path);
110 tile = xglGenLists(1);
111 xglNewList(tile, GL_COMPILE);
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);
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 ) {
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;
142 /* first time through set min's and max'es */
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;
162 fgPrintf( FG_TERRAIN, FG_EXIT,
163 "Read too many nodes ... dying :-(\n");
165 } else if ( strncmp(line, "vn ", 3) == 0 ) {
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]);
174 fgPrintf( FG_TERRAIN, FG_EXIT,
175 "Read too many vertex normals ... dying :-(\n");
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);
181 /* can't call xglFrontFace() between xglBegin() & xglEnd() */
185 if ( strcmp(winding_str, "cw") == 0 ) {
186 xglFrontFace( GL_CW );
189 glFrontFace ( GL_CCW );
192 } else if ( line[0] == 't' ) {
193 /* start a new triangle strip */
195 n1 = n2 = n3 = n4 = 0;
198 /* close out the previous structure and start the next */
204 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " new tri strip = %s",
206 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
208 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = "); */
210 xglBegin(GL_TRIANGLE_STRIP);
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]);
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]);
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]);
237 calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
239 calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
241 MAT3_SCALE_VEC(normal, approx_normal, scale);
242 xglNormal3dv(normal);
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]);
257 if ( use_per_vertex_norms ) {
258 MAT3_SCALE_VEC(normal, normals[n4], scale);
260 calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
261 MAT3_SCALE_VEC(normal, approx_normal, scale);
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]);
271 } else if ( line[0] == 'f' ) {
272 /* unoptimized face */
275 /* close out the previous structure and start the next */
281 xglBegin(GL_TRIANGLES);
283 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
284 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
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]);
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]);
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 */
301 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ",
303 sscanf(line, "q %d %d\n", &n1, &n2);
304 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2); */
306 if ( use_per_vertex_norms ) {
307 MAT3_SCALE_VEC(normal, normals[n1], scale);
308 xglNormal3dv(normal);
311 calc_normal(nodes[last1], nodes[last2], nodes[n1],
314 calc_normal(nodes[last2], nodes[last1], nodes[n1],
317 MAT3_SCALE_VEC(normal, approx_normal, scale);
318 xglNormal3dv(normal);
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]);
329 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n"); */
331 if ( use_per_vertex_norms ) {
332 MAT3_SCALE_VEC(normal, normals[n2], scale);
333 xglNormal3dv(normal);
336 calc_normal(nodes[last1], nodes[last2], nodes[n2],
339 calc_normal(nodes[last2], nodes[last1], nodes[n2],
342 MAT3_SCALE_VEC(normal, approx_normal, scale);
343 xglNormal3dv(normal);
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]);
354 fgPrintf( FG_TERRAIN, FG_WARN, "Unknown line in %s = %s\n",
361 /* Draw normal vectors (for visually verifying normals)*/
364 xglColor3f(0.0, 0.0, 0.0);
365 for ( i = 0; i < ncount; i++ ) {
366 xglVertex3d(nodes[i][0],
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]);
376 // xglDisable(GL_TEXTURE_GEN_S);
377 // xglDisable(GL_TEXTURE_GEN_T);
379 xglFrontFace ( GL_CCW );
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;
395 /* Revision 1.26 1998/04/03 22:11:36 curt
396 /* Converting to Gnu autoconf system.
398 * Revision 1.25 1998/03/14 00:30:50 curt
399 * Beginning initial terrain texturing experiments.
401 * Revision 1.24 1998/02/09 21:30:18 curt
402 * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
404 * Revision 1.23 1998/02/09 15:07:52 curt
407 * Revision 1.22 1998/02/01 03:39:54 curt
410 * Revision 1.21 1998/01/31 00:43:25 curt
411 * Added MetroWorks patches from Carmen Volpe.
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.
416 * Revision 1.19 1998/01/27 03:26:42 curt
417 * Playing with new fgPrintf command.
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.
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.
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.
431 * Revision 1.15 1997/12/30 20:47:51 curt
432 * Integrated new event manager with subsystem initializations.
434 * Revision 1.14 1997/12/30 01:38:46 curt
435 * Switched back to per vertex normals and smooth shading for terrain.
437 * Revision 1.13 1997/12/18 23:32:36 curt
438 * First stab at sky dome actually starting to look reasonable. :-)
440 * Revision 1.12 1997/12/17 23:13:47 curt
441 * Began working on rendering the sky.
443 * Revision 1.11 1997/12/15 23:55:01 curt
444 * Add xgl wrappers for debugging.
445 * Generate terrain normals on the fly.
447 * Revision 1.10 1997/12/12 21:41:28 curt
448 * More light/material property tweaking ... still a ways off.
450 * Revision 1.9 1997/12/12 19:52:57 curt
451 * Working on lightling and material properties.
453 * Revision 1.8 1997/12/10 01:19:51 curt
454 * Tweaks for verion 0.15 release.
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).
461 * Revision 1.6 1997/11/25 19:25:35 curt
462 * Changes to integrate Durk's moon/sun code updates + clean up.
464 * Revision 1.5 1997/11/15 18:16:39 curt
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
473 * Revision 1.3 1997/10/31 04:49:12 curt
474 * Tweaking vertex orders.
476 * Revision 1.2 1997/10/30 12:38:45 curt
477 * Working on new scenery subsystem.
479 * Revision 1.1 1997/10/28 21:14:54 curt