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 **************************************************************************/
34 #include "../XGL/xgl.h"
39 #include "../Math/mat3.h"
43 #define MAXNODES 100000
45 float nodes[MAXNODES][3];
46 float normals[MAXNODES][3];
49 /* given three points defining a triangle, calculate the normal */
50 void calc_normal(float p1[3], float p2[3], float 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 /* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);*/
65 /* Load a .obj file and generate the GL call list */
66 GLint fgObjLoad(char *path) {
67 char line[256], winding_str[256];
68 double v1[3], v2[3], approx_normal[3], dot_prod, temp;
69 struct fgCartesianPoint ref;
72 int first, ncount, vncount, n1, n2, n3, n4;
73 static int use_vertex_norms = 1;
75 int last1, last2, odd;
77 if ( (f = fopen(path, "r")) == NULL ) {
78 printf("Cannot open file: %s\n", path);
82 area = xglGenLists(1);
83 xglNewList(area, GL_COMPILE);
89 while ( fgets(line, 250, f) != NULL ) {
90 if ( line[0] == '#' ) {
91 /* comment -- ignore */
92 } else if ( line[0] == '\n' ) {
93 /* empty line -- ignore */
94 } else if ( strncmp(line, "v ", 2) == 0 ) {
96 if ( ncount < MAXNODES ) {
97 /* printf("vertex = %s", line); */
98 sscanf(line, "v %f %f %f\n",
99 &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
101 /* first node becomes the reference point */
102 ref.x = nodes[ncount][0];
103 ref.y = nodes[ncount][1];
104 ref.z = nodes[ncount][2];
105 scenery.center = ref;
109 printf("Read too many nodes ... dying :-(\n");
112 } else if ( strncmp(line, "vn ", 3) == 0 ) {
114 if ( vncount < MAXNODES ) {
115 /* printf("vertex normal = %s", line); */
116 sscanf(line, "vn %f %f %f\n",
117 &normals[vncount][0], &normals[vncount][1],
118 &normals[vncount][2]);
121 printf("Read too many vertex normals ... dying :-(\n");
124 } else if ( strncmp(line, "winding ", 8) == 0 ) {
125 sscanf(line+8, "%s", winding_str);
126 printf(" WINDING = %s\n", winding_str);
128 /* can't call xglFrontFace() between xglBegin() & xglEnd() */
132 if ( strcmp(winding_str, "cw") == 0 ) {
133 xglFrontFace( GL_CW );
136 xglFrontFace ( GL_CCW );
139 } else if ( line[0] == 't' ) {
140 /* start a new triangle strip */
142 n1 = n2 = n3 = n4 = 0;
145 /* close out the previous structure and start the next */
151 /* printf(" new tri strip = %s", line); */
152 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
154 /* printf("(t) = "); */
156 xglBegin(GL_TRIANGLE_STRIP);
164 if ( use_vertex_norms ) {
165 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
166 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
167 nodes[n1][2] - ref.z);
169 xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
170 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
171 nodes[n2][2] - ref.z);
173 xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
174 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y,
175 nodes[n3][2] - ref.z);
178 calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
180 calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
182 xglNormal3dv(approx_normal);
184 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
185 nodes[n1][2] - ref.z);
186 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
187 nodes[n2][2] - ref.z);
188 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y,
189 nodes[n3][2] - ref.z);
197 if ( use_vertex_norms ) {
198 xglNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);
200 calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
201 xglNormal3dv(approx_normal);
203 xglVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y,
204 nodes[n4][2] - ref.z);
210 } else if ( line[0] == 'f' ) {
211 /* unoptimized face */
214 /* close out the previous structure and start the next */
220 xglBegin(GL_TRIANGLES);
222 /* printf("new triangle = %s", line);*/
223 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
225 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
226 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
227 nodes[n1][2] - ref.z);
229 xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
230 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
231 nodes[n2][2] - ref.z);
233 xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
234 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y,
235 nodes[n3][2] - ref.z);
236 } else if ( line[0] == 'q' ) {
237 /* continue a triangle strip */
240 /* printf("continued tri strip = %s ", line); */
241 sscanf(line, "q %d %d\n", &n1, &n2);
242 /* printf("read %d %d\n", n1, n2); */
244 if ( use_vertex_norms ) {
245 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
248 calc_normal(nodes[last1], nodes[last2], nodes[n1],
251 calc_normal(nodes[last2], nodes[last1], nodes[n1],
254 xglNormal3dv(approx_normal);
257 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
258 nodes[n1][2] - ref.z);
265 /* printf(" (cont)\n"); */
267 if ( use_vertex_norms ) {
268 xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
271 calc_normal(nodes[last1], nodes[last2], nodes[n2],
274 calc_normal(nodes[last2], nodes[last1], nodes[n2],
277 xglNormal3dv(approx_normal);
280 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
281 nodes[n2][2] - ref.z);
288 printf("Unknown line in %s = %s\n", path, line);
294 /* Draw normal vectors (for visually verifying normals)*/
297 xglColor3f(0.0, 0.0, 0.0);
298 for ( i = 0; i < ncount; i++ ) {
299 xglVertex3d(nodes[i][0] - ref.x,
301 nodes[i][2] - ref.z);
302 xglVertex3d(nodes[i][0] - ref.x + 500*normals[i][0],
303 nodes[i][1] - ref.y + 500*normals[i][1],
304 nodes[i][2] - ref.z + 500*normals[i][2]);
309 xglFrontFace ( GL_CCW );
320 /* Revision 1.15 1997/12/30 20:47:51 curt
321 /* Integrated new event manager with subsystem initializations.
323 * Revision 1.14 1997/12/30 01:38:46 curt
324 * Switched back to per vertex normals and smooth shading for terrain.
326 * Revision 1.13 1997/12/18 23:32:36 curt
327 * First stab at sky dome actually starting to look reasonable. :-)
329 * Revision 1.12 1997/12/17 23:13:47 curt
330 * Began working on rendering the sky.
332 * Revision 1.11 1997/12/15 23:55:01 curt
333 * Add xgl wrappers for debugging.
334 * Generate terrain normals on the fly.
336 * Revision 1.10 1997/12/12 21:41:28 curt
337 * More light/material property tweaking ... still a ways off.
339 * Revision 1.9 1997/12/12 19:52:57 curt
340 * Working on lightling and material properties.
342 * Revision 1.8 1997/12/10 01:19:51 curt
343 * Tweaks for verion 0.15 release.
345 * Revision 1.7 1997/12/08 22:51:17 curt
346 * Enhanced to handle ccw and cw tri-stripe winding. This is a temporary
347 * admission of defeat. I will eventually go back and get all the stripes
348 * wound the same way (ccw).
350 * Revision 1.6 1997/11/25 19:25:35 curt
351 * Changes to integrate Durk's moon/sun code updates + clean up.
353 * Revision 1.5 1997/11/15 18:16:39 curt
356 * Revision 1.4 1997/11/14 00:26:49 curt
357 * Transform scenery coordinates earlier in pipeline when scenery is being
358 * created, not when it is being loaded. Precalculate normals for each node
359 * as average of the normals of each containing polygon so Garoude shading is
362 * Revision 1.3 1997/10/31 04:49:12 curt
363 * Tweaking vertex orders.
365 * Revision 1.2 1997/10/30 12:38:45 curt
366 * Working on new scenery subsystem.
368 * Revision 1.1 1997/10/28 21:14:54 curt