1 /**************************************************************************
2 * obj.c -- routines to handle WaveFront .obj format files.
4 * Written by Curtis Olson, started October 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
36 #include <Main/fg_debug.h>
37 #include <Math/mat3.h>
38 #include <Scenery/obj.h>
39 #include <Scenery/scenery.h>
43 #define MAXNODES 100000
45 static double nodes[MAXNODES][3];
46 static double normals[MAXNODES][3];
49 /* given three points defining a triangle, calculate the normal */
50 void calc_normal(double p1[3], double p2[3], double p3[3], double normal[3])
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];
58 MAT3cross_product(normal, v1, v2);
59 MAT3_NORMALIZE_VEC(normal,temp);
61 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " Normal = %.2f %.2f %.2f\n",
62 normal[0], normal[1], normal[2]);*/
66 /* Load a .obj file and generate the GL call list */
67 GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
68 char line[256], winding_str[256];
69 double approx_normal[3], normal[3], scale;
70 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
73 int first, ncount, vncount, n1, n2, n3, n4;
74 static int use_per_vertex_norms = 1;
76 int last1, last2, odd;
78 if ( (f = fopen(path, "r")) == NULL ) {
79 fgPrintf(FG_TERRAIN, FG_ALERT, "Cannot open file: %s\n", path);
83 tile = xglGenLists(1);
84 xglNewList(tile, GL_COMPILE);
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 ) {
97 if ( ncount < MAXNODES ) {
98 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "vertex = %s", line); */
99 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
100 nodes[ncount][0] = x;
101 nodes[ncount][1] = y;
102 nodes[ncount][2] = z;
104 /* first time through set min's and max'es */
114 /* keep track of min/max vertex values */
115 if ( x < xmin ) xmin = x;
116 if ( x > xmax ) xmax = x;
117 if ( y < ymin ) ymin = y;
118 if ( y > ymax ) ymax = y;
119 if ( z < zmin ) zmin = z;
120 if ( z > zmax ) zmax = z;
124 fgPrintf( FG_TERRAIN, FG_EXIT,
125 "Read too many nodes ... dying :-(\n");
127 } else if ( strncmp(line, "vn ", 3) == 0 ) {
129 if ( vncount < MAXNODES ) {
130 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "vertex normal = %s", line); */
131 sscanf(line, "vn %lf %lf %lf\n",
132 &normals[vncount][0], &normals[vncount][1],
133 &normals[vncount][2]);
136 fgPrintf( FG_TERRAIN, FG_EXIT,
137 "Read too many vertex normals ... dying :-(\n");
139 } else if ( strncmp(line, "winding ", 8) == 0 ) {
140 sscanf(line+8, "%s", winding_str);
141 fgPrintf( FG_TERRAIN, FG_DEBUG, " WINDING = %s\n", winding_str);
143 /* can't call xglFrontFace() between xglBegin() & xglEnd() */
147 if ( strcmp(winding_str, "cw") == 0 ) {
148 xglFrontFace( GL_CW );
151 glFrontFace ( GL_CCW );
154 } else if ( line[0] == 't' ) {
155 /* start a new triangle strip */
157 n1 = n2 = n3 = n4 = 0;
160 /* close out the previous structure and start the next */
166 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " new tri strip = %s",
168 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
170 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = "); */
172 xglBegin(GL_TRIANGLE_STRIP);
182 if ( use_per_vertex_norms ) {
183 MAT3_SCALE_VEC(normal, normals[n1], scale);
184 xglNormal3dv(normal);
185 xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
187 MAT3_SCALE_VEC(normal, normals[n2], scale);
188 xglNormal3dv(normal);
189 xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
191 MAT3_SCALE_VEC(normal, normals[n3], scale);
192 xglNormal3dv(normal);
193 xglVertex3d(nodes[n3][0], nodes[n3][1], nodes[n3][2]);
196 calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
198 calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
200 MAT3_SCALE_VEC(normal, approx_normal, scale);
201 xglNormal3dv(normal);
203 xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
204 xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
205 xglVertex3d(nodes[n3][0], nodes[n3][1], nodes[n3][2]);
213 if ( use_per_vertex_norms ) {
214 MAT3_SCALE_VEC(normal, normals[n4], scale);
216 calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
217 MAT3_SCALE_VEC(normal, approx_normal, scale);
219 xglNormal3dv(normal);
220 xglVertex3d(nodes[n4][0], nodes[n4][1], nodes[n4][2]);
226 } else if ( line[0] == 'f' ) {
227 /* unoptimized face */
230 /* close out the previous structure and start the next */
236 xglBegin(GL_TRIANGLES);
238 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
239 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
241 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
242 xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
244 xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
245 xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
247 xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
248 xglVertex3d(nodes[n3][0], nodes[n3][1], nodes[n3][2]);
249 } else if ( line[0] == 'q' ) {
250 /* continue a triangle strip */
253 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ",
255 sscanf(line, "q %d %d\n", &n1, &n2);
256 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2); */
258 if ( use_per_vertex_norms ) {
259 MAT3_SCALE_VEC(normal, normals[n1], scale);
260 xglNormal3dv(normal);
263 calc_normal(nodes[last1], nodes[last2], nodes[n1],
266 calc_normal(nodes[last2], nodes[last1], nodes[n1],
269 MAT3_SCALE_VEC(normal, approx_normal, scale);
270 xglNormal3dv(normal);
273 xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
280 /* fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n"); */
282 if ( use_per_vertex_norms ) {
283 MAT3_SCALE_VEC(normal, normals[n2], scale);
284 xglNormal3dv(normal);
287 calc_normal(nodes[last1], nodes[last2], nodes[n2],
290 calc_normal(nodes[last2], nodes[last1], nodes[n2],
293 MAT3_SCALE_VEC(normal, approx_normal, scale);
294 xglNormal3dv(normal);
297 xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
304 fgPrintf( FG_TERRAIN, FG_WARN, "Unknown line in %s = %s\n",
311 /* Draw normal vectors (for visually verifying normals)*/
314 xglColor3f(0.0, 0.0, 0.0);
315 for ( i = 0; i < ncount; i++ ) {
316 xglVertex3d(nodes[i][0],
319 xglVertex3d(nodes[i][0] + 500*normals[i][0],
320 nodes[i][1] + 500*normals[i][1],
321 nodes[i][2] + 500*normals[i][2]);
326 xglFrontFace ( GL_CCW );
332 /* reference point is the "center" */
333 ref->x = (xmin + xmax) / 2.0;
334 ref->y = (ymin + ymax) / 2.0;
335 ref->z = (zmin + zmax) / 2.0;
342 /* Revision 1.24 1998/02/09 21:30:18 curt
343 /* Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
345 * Revision 1.23 1998/02/09 15:07:52 curt
348 * Revision 1.22 1998/02/01 03:39:54 curt
351 * Revision 1.21 1998/01/31 00:43:25 curt
352 * Added MetroWorks patches from Carmen Volpe.
354 * Revision 1.20 1998/01/29 00:51:39 curt
355 * First pass at tile cache, dynamic tile loading and tile unloading now works.
357 * Revision 1.19 1998/01/27 03:26:42 curt
358 * Playing with new fgPrintf command.
360 * Revision 1.18 1998/01/19 19:27:16 curt
361 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
362 * This should simplify things tremendously.
364 * Revision 1.17 1998/01/13 00:23:10 curt
365 * Initial changes to support loading and management of scenery tiles. Note,
366 * there's still a fair amount of work left to be done.
368 * Revision 1.16 1997/12/30 23:09:40 curt
369 * Worked on winding problem without luck, so back to calling glFrontFace()
370 * 3 times for each scenery area.
372 * Revision 1.15 1997/12/30 20:47:51 curt
373 * Integrated new event manager with subsystem initializations.
375 * Revision 1.14 1997/12/30 01:38:46 curt
376 * Switched back to per vertex normals and smooth shading for terrain.
378 * Revision 1.13 1997/12/18 23:32:36 curt
379 * First stab at sky dome actually starting to look reasonable. :-)
381 * Revision 1.12 1997/12/17 23:13:47 curt
382 * Began working on rendering the sky.
384 * Revision 1.11 1997/12/15 23:55:01 curt
385 * Add xgl wrappers for debugging.
386 * Generate terrain normals on the fly.
388 * Revision 1.10 1997/12/12 21:41:28 curt
389 * More light/material property tweaking ... still a ways off.
391 * Revision 1.9 1997/12/12 19:52:57 curt
392 * Working on lightling and material properties.
394 * Revision 1.8 1997/12/10 01:19:51 curt
395 * Tweaks for verion 0.15 release.
397 * Revision 1.7 1997/12/08 22:51:17 curt
398 * Enhanced to handle ccw and cw tri-stripe winding. This is a temporary
399 * admission of defeat. I will eventually go back and get all the stripes
400 * wound the same way (ccw).
402 * Revision 1.6 1997/11/25 19:25:35 curt
403 * Changes to integrate Durk's moon/sun code updates + clean up.
405 * Revision 1.5 1997/11/15 18:16:39 curt
408 * Revision 1.4 1997/11/14 00:26:49 curt
409 * Transform scenery coordinates earlier in pipeline when scenery is being
410 * created, not when it is being loaded. Precalculate normals for each node
411 * as average of the normals of each containing polygon so Garoude shading is
414 * Revision 1.3 1997/10/31 04:49:12 curt
415 * Tweaking vertex orders.
417 * Revision 1.2 1997/10/30 12:38:45 curt
418 * Working on new scenery subsystem.
420 * Revision 1.1 1997/10/28 21:14:54 curt