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 **************************************************************************/
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>
46 #define MAXNODES 100000
48 static double nodes[MAXNODES][3];
49 static double normals[MAXNODES][3];
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])
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];
61 MAT3cross_product(normal, v1, v2);
62 MAT3_NORMALIZE_VEC(normal,temp);
64 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " Normal = %.2f %.2f %.2f\n",
65 normal[0], normal[1], normal[2]);*/
69 #define FG_TEX_CONSTANT 64.0
71 float calc_lon(double x, double y, double z) {
73 tmp = (RAD_TO_DEG*atan2(y, x)) * FG_TEX_CONSTANT;
75 // printf("lon = %.2f\n", (float)tmp);
80 float calc_lat(double x, double y, double z) {
83 tmp = (90.0 - RAD_TO_DEG*atan2( sqrt(x*x + y*y), z )) * FG_TEX_CONSTANT;
85 // printf("lat = %.2f\n", (float)tmp);
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 };
98 int first, ncount, vncount, n1, n2, n3, n4;
99 static int use_per_vertex_norms = 1;
101 int last1, last2, odd;
103 if ( (f = fopen(path, "r")) == NULL ) {
104 fgPrintf(FG_TERRAIN, FG_ALERT, "Cannot open file: %s\n", path);
108 tile = xglGenLists(1);
109 xglNewList(tile, GL_COMPILE);
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);
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 ) {
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;
140 /* first time through set min's and max'es */
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;
160 fgPrintf( FG_TERRAIN, FG_EXIT,
161 "Read too many nodes ... dying :-(\n");
163 } else if ( strncmp(line, "vn ", 3) == 0 ) {
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]);
172 fgPrintf( FG_TERRAIN, FG_EXIT,
173 "Read too many vertex normals ... dying :-(\n");
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);
179 /* can't call xglFrontFace() between xglBegin() & xglEnd() */
183 if ( strcmp(winding_str, "cw") == 0 ) {
184 xglFrontFace( GL_CW );
187 glFrontFace ( GL_CCW );
190 } else if ( line[0] == 't' ) {
191 /* start a new triangle strip */
193 n1 = n2 = n3 = n4 = 0;
196 /* close out the previous structure and start the next */
202 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " new tri strip = %s",
204 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
206 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = "); */
208 xglBegin(GL_TRIANGLE_STRIP);
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]);
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]);
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]);
235 calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
237 calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
239 MAT3_SCALE_VEC(normal, approx_normal, scale);
240 xglNormal3dv(normal);
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]);
255 if ( use_per_vertex_norms ) {
256 MAT3_SCALE_VEC(normal, normals[n4], scale);
258 calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
259 MAT3_SCALE_VEC(normal, approx_normal, scale);
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]);
269 } else if ( line[0] == 'f' ) {
270 /* unoptimized face */
273 /* close out the previous structure and start the next */
279 xglBegin(GL_TRIANGLES);
281 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
282 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
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]);
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]);
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 */
299 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ",
301 sscanf(line, "q %d %d\n", &n1, &n2);
302 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2); */
304 if ( use_per_vertex_norms ) {
305 MAT3_SCALE_VEC(normal, normals[n1], scale);
306 xglNormal3dv(normal);
309 calc_normal(nodes[last1], nodes[last2], nodes[n1],
312 calc_normal(nodes[last2], nodes[last1], nodes[n1],
315 MAT3_SCALE_VEC(normal, approx_normal, scale);
316 xglNormal3dv(normal);
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]);
327 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n"); */
329 if ( use_per_vertex_norms ) {
330 MAT3_SCALE_VEC(normal, normals[n2], scale);
331 xglNormal3dv(normal);
334 calc_normal(nodes[last1], nodes[last2], nodes[n2],
337 calc_normal(nodes[last2], nodes[last1], nodes[n2],
340 MAT3_SCALE_VEC(normal, approx_normal, scale);
341 xglNormal3dv(normal);
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]);
352 fgPrintf( FG_TERRAIN, FG_WARN, "Unknown line in %s = %s\n",
359 /* Draw normal vectors (for visually verifying normals)*/
362 xglColor3f(0.0, 0.0, 0.0);
363 for ( i = 0; i < ncount; i++ ) {
364 xglVertex3d(nodes[i][0],
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]);
374 // xglDisable(GL_TEXTURE_GEN_S);
375 // xglDisable(GL_TEXTURE_GEN_T);
377 xglFrontFace ( GL_CCW );
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;
393 /* Revision 1.25 1998/03/14 00:30:50 curt
394 /* Beginning initial terrain texturing experiments.
396 * Revision 1.24 1998/02/09 21:30:18 curt
397 * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
399 * Revision 1.23 1998/02/09 15:07:52 curt
402 * Revision 1.22 1998/02/01 03:39:54 curt
405 * Revision 1.21 1998/01/31 00:43:25 curt
406 * Added MetroWorks patches from Carmen Volpe.
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.
411 * Revision 1.19 1998/01/27 03:26:42 curt
412 * Playing with new fgPrintf command.
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.
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.
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.
426 * Revision 1.15 1997/12/30 20:47:51 curt
427 * Integrated new event manager with subsystem initializations.
429 * Revision 1.14 1997/12/30 01:38:46 curt
430 * Switched back to per vertex normals and smooth shading for terrain.
432 * Revision 1.13 1997/12/18 23:32:36 curt
433 * First stab at sky dome actually starting to look reasonable. :-)
435 * Revision 1.12 1997/12/17 23:13:47 curt
436 * Began working on rendering the sky.
438 * Revision 1.11 1997/12/15 23:55:01 curt
439 * Add xgl wrappers for debugging.
440 * Generate terrain normals on the fly.
442 * Revision 1.10 1997/12/12 21:41:28 curt
443 * More light/material property tweaking ... still a ways off.
445 * Revision 1.9 1997/12/12 19:52:57 curt
446 * Working on lightling and material properties.
448 * Revision 1.8 1997/12/10 01:19:51 curt
449 * Tweaks for verion 0.15 release.
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).
456 * Revision 1.6 1997/11/25 19:25:35 curt
457 * Changes to integrate Durk's moon/sun code updates + clean up.
459 * Revision 1.5 1997/11/15 18:16:39 curt
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
468 * Revision 1.3 1997/10/31 04:49:12 curt
469 * Tweaking vertex orders.
471 * Revision 1.2 1997/10/30 12:38:45 curt
472 * Working on new scenery subsystem.
474 * Revision 1.1 1997/10/28 21:14:54 curt