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;
74 int last1, last2, odd;
76 if ( (f = fopen(path, "r")) == NULL ) {
77 printf("Cannot open file: %s\n", path);
81 area = xglGenLists(1);
82 xglNewList(area, GL_COMPILE);
88 while ( fgets(line, 250, f) != NULL ) {
89 if ( line[0] == '#' ) {
90 /* comment -- ignore */
91 } else if ( strncmp(line, "v ", 2) == 0 ) {
93 if ( ncount < MAXNODES ) {
94 /* printf("vertex = %s", line); */
95 sscanf(line, "v %f %f %f\n",
96 &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
98 /* first node becomes the reference point */
99 ref.x = nodes[ncount][0];
100 ref.y = nodes[ncount][1];
101 ref.z = nodes[ncount][2];
102 scenery.center = ref;
106 printf("Read too many nodes ... dying :-(\n");
109 } else if ( strncmp(line, "vn ", 3) == 0 ) {
111 if ( vncount < MAXNODES ) {
112 /* printf("vertex normal = %s", line); */
113 sscanf(line, "vn %f %f %f\n",
114 &normals[vncount][0], &normals[vncount][1],
115 &normals[vncount][2]);
118 printf("Read too many vertex normals ... dying :-(\n");
121 } else if ( strncmp(line, "winding ", 8) == 0 ) {
122 sscanf(line+8, "%s", winding_str);
123 printf("WINDING = %s\n", winding_str);
125 /* can't call xglFrontFace() between xglBegin() & xglEnd() */
129 if ( strcmp(winding_str, "cw") == 0 ) {
130 xglFrontFace( GL_CW );
133 xglFrontFace ( GL_CCW );
136 } else if ( line[0] == 't' ) {
137 /* start a new triangle strip */
139 n1 = n2 = n3 = n4 = 0;
142 /* close out the previous structure and start the next */
148 printf("new tri strip = %s", line);
149 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
151 /* printf("(t) = "); */
153 /* try to get the proper rotation by calculating an
154 * approximate normal and seeing if it is close to the
155 * precalculated normal */
156 /* v1[0] = nodes[n2][0] - nodes[n1][0];
157 v1[1] = nodes[n2][1] - nodes[n1][1];
158 v1[2] = nodes[n2][2] - nodes[n1][2];
159 v2[0] = nodes[n3][0] - nodes[n1][0];
160 v2[1] = nodes[n3][1] - nodes[n1][1];
161 v2[2] = nodes[n3][2] - nodes[n1][2];
162 MAT3cross_product(approx_normal, v1, v2);
163 MAT3_NORMALIZE_VEC(approx_normal,temp);
164 printf("Approx normal = %.2f %.2f %.2f\n", approx_normal[0],
165 approx_normal[1], approx_normal[2]);
166 dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
167 printf("Dot product = %.4f\n", dot_prod); */
168 /* angle = acos(dot_prod); */
169 /* printf("Normal ANGLE = %.3f rads.\n", angle); */
171 /* if ( dot_prod < -0.5 ) {
172 xglFrontFace( GL_CW );
174 xglFrontFace( GL_CCW );
177 xglBegin(GL_TRIANGLE_STRIP);
186 calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
188 calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
190 xglNormal3dv(approx_normal);
192 /* xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]); */
193 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
194 nodes[n1][2] - ref.z);
196 /* xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]); */
197 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
198 nodes[n2][2] - ref.z);
200 /* xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]); */
201 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y,
202 nodes[n3][2] - ref.z);
210 calc_normal(nodes[last1], nodes[last2], nodes[n4],
213 calc_normal(nodes[last2], nodes[last1], nodes[n4],
216 calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
217 xglNormal3dv(approx_normal);
219 /*xglNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);*/
220 xglVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y,
221 nodes[n4][2] - ref.z);
227 } else if ( line[0] == 'f' ) {
228 /* unoptimized face */
231 /* close out the previous structure and start the next */
237 xglBegin(GL_TRIANGLES);
239 /* printf("new triangle = %s", line);*/
240 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
242 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
243 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
244 nodes[n1][2] - ref.z);
246 xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
247 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
248 nodes[n2][2] - ref.z);
250 xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
251 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y,
252 nodes[n3][2] - ref.z);
253 } else if ( line[0] == 'q' ) {
254 /* continue a triangle strip */
257 /* printf("continued tri strip = %s ", line); */
258 sscanf(line, "q %d %d\n", &n1, &n2);
259 /* printf("read %d %d\n", n1, n2); */
262 calc_normal(nodes[last1], nodes[last2], nodes[n1],
265 calc_normal(nodes[last2], nodes[last1], nodes[n1],
268 xglNormal3dv(approx_normal);
270 /* xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]); */
271 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y,
272 nodes[n1][2] - ref.z);
279 /* printf(" (cont)\n"); */
281 calc_normal(nodes[last1], nodes[last2], nodes[n2],
284 calc_normal(nodes[last2], nodes[last1], nodes[n2],
287 xglNormal3dv(approx_normal);
289 /*xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);*/
290 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y,
291 nodes[n2][2] - ref.z);
298 printf("Unknown line in %s = %s\n", path, line);
304 /* Draw normal vectors (for visually verifying normals)*/
307 xglColor3f(0.0, 0.0, 0.0);
308 for ( i = 0; i < ncount; i++ ) {
309 xglVertex3d(nodes[i][0] - ref.x,
311 nodes[i][2] - ref.z);
312 xglVertex3d(nodes[i][0] - ref.x + 500*normals[i][0],
313 nodes[i][1] - ref.y + 500*normals[i][1],
314 nodes[i][2] - ref.z + 500*normals[i][2]);
319 xglFrontFace ( GL_CCW );
330 /* Revision 1.13 1997/12/18 23:32:36 curt
331 /* First stab at sky dome actually starting to look reasonable. :-)
333 * Revision 1.12 1997/12/17 23:13:47 curt
334 * Began working on rendering the sky.
336 * Revision 1.11 1997/12/15 23:55:01 curt
337 * Add xgl wrappers for debugging.
338 * Generate terrain normals on the fly.
340 * Revision 1.10 1997/12/12 21:41:28 curt
341 * More light/material property tweaking ... still a ways off.
343 * Revision 1.9 1997/12/12 19:52:57 curt
344 * Working on lightling and material properties.
346 * Revision 1.8 1997/12/10 01:19:51 curt
347 * Tweaks for verion 0.15 release.
349 * Revision 1.7 1997/12/08 22:51:17 curt
350 * Enhanced to handle ccw and cw tri-stripe winding. This is a temporary
351 * admission of defeat. I will eventually go back and get all the stripes
352 * wound the same way (ccw).
354 * Revision 1.6 1997/11/25 19:25:35 curt
355 * Changes to integrate Durk's moon/sun code updates + clean up.
357 * Revision 1.5 1997/11/15 18:16:39 curt
360 * Revision 1.4 1997/11/14 00:26:49 curt
361 * Transform scenery coordinates earlier in pipeline when scenery is being
362 * created, not when it is being loaded. Precalculate normals for each node
363 * as average of the normals of each containing polygon so Garoude shading is
366 * Revision 1.3 1997/10/31 04:49:12 curt
367 * Tweaking vertex orders.
369 * Revision 1.2 1997/10/30 12:38:45 curt
370 * Working on new scenery subsystem.
372 * Revision 1.1 1997/10/28 21:14:54 curt