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 **************************************************************************/
32 #include <Math/mat3.h>
35 /* what do ya' know, here's some global variables */
36 static double nodes[MAXNODES][3];
37 static double normals[MAXNODES][3];
38 static int faces[MAXNODES][3];
39 int ncount, vncount, fcount;
41 static int ccw_list[MAXNODES];
44 static int cw_list[MAXNODES];
49 double refx, refy, refz;
52 /* some simple list routines */
55 void list_init(int *list_ptr) {
61 void list_add(int *list, int *list_ptr, int node) {
62 if ( *list_ptr >= MAXNODES ) {
63 printf("ERROR: list overflow in list_add()\n");
67 list[*list_ptr] = node;
70 /* printf("list pointer = %d adding %d\n", *list_ptr, node); */
74 /* fix the cw list and append to ccw_list */
75 void fix_cw_list(int *list, int list_ptr) {
79 printf("List is empty ... skipping\n");
83 printf("Fixing cw list, size = %d\n", list_ptr);
86 while ( i < list_ptr ) {
91 /* scan rest of strip (until -1) */
92 while ( ((i+len) < list_ptr) && (list[i+len] != -1) ) {
93 // printf("len = %d item = %d\n", len, list[i+len] );
96 // printf(" Final length = %d\n", len);
98 if ( (len % 2) != 0 ) {
99 /* if length is odd, just reverse order of nodes to reverse
101 if ( ccw_list_ptr ) {
102 list_add(ccw_list, &ccw_list_ptr, -1);
104 for ( j = i + len - 1; j >= i; j-- ) {
105 // printf(" odd -> item = %d\n", list[j] );
106 list_add(ccw_list, &ccw_list_ptr, list[j]);
109 /* if length is even, reverse order of (n-1) nodes to
110 reverse winding, and create an orphan triangle for the
112 if ( ccw_list_ptr ) {
113 list_add(ccw_list, &ccw_list_ptr, -1);
115 for ( j = i + len - 2; j >= i; j-- ) {
116 // printf(" even -> item = %d\n", list[j] );
117 list_add(ccw_list, &ccw_list_ptr, list[j]);
120 // printf(" even bonus -> item = %d\n", list[i + len - 1] );
121 // printf(" even bonus -> item = %d\n", list[i + len - 2] );
122 // printf(" even bonus -> item = %d\n", list[i + len - 3] );
123 list_add(ccw_list, &ccw_list_ptr, -1);
124 list_add(ccw_list, &ccw_list_ptr, list[i + len - 3]);
125 list_add(ccw_list, &ccw_list_ptr, list[i + len - 2]);
126 list_add(ccw_list, &ccw_list_ptr, list[i + len - 1]);
134 // Calculate distance between (0,0,0) and the specified point
135 static double calc_dist(double x, double y, double z) {
136 return ( sqrt(x*x + y*y + z*z) );
140 void dump_global_bounds( void ) {
148 for ( i = 1; i < ncount; i++ ) {
150 dist = calc_dist(nodes[i][0] - refx, nodes[i][1] - refy,
152 // printf("node = %.2f %.2f %.2f dist = %.2f\n",
153 // nodes[i][0], nodes[i][1], nodes[i][2],
156 if ( dist > radius ) {
162 fprintf(out, "gb %.5f %.5f %.5f %.2f\n", refx, refy, refz, radius);
167 void dump_nodes( void ) {
171 for ( i = 1; i < ncount; i++ ) {
172 fprintf(out, "v %.5f %.5f %.5f\n",
173 nodes[i][0] - refx, nodes[i][1] - refy, nodes[i][2] - refz);
179 void dump_normals( void ) {
183 for ( i = 1; i < vncount; i++ ) {
184 fprintf(out, "vn %.5f %.5f %.5f\n",
185 normals[i][0], normals[i][1], normals[i][2]);
191 void dump_faces( void ) {
195 for ( i = 1; i < fcount; i++ ) {
196 fprintf(out, "f %d %d %d\n",
197 faces[i][0], faces[i][1], faces[i][2]);
203 void dump_list(int *list, int list_ptr) {
206 if ( list_ptr < 3 ) {
207 printf("List is empty ... skipping\n");
211 printf("Dumping list, size = %d\n", list_ptr);
214 while ( i < list_ptr ) {
217 if ( (i % 2) == 0 ) {
218 fprintf(out, "\nusemtl desert1\n");
220 fprintf(out, "\nusemtl desert2\n");
224 fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
225 /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
228 /* dump rest of strip (until -1) */
229 while ( (i < list_ptr) && (list[i] != -1) ) {
230 fprintf(out, "q %d", list[i]);
232 if ( (i < list_ptr) && (list[i] != -1) ) {
233 fprintf(out, " %d", list[i]);
244 /* Check the direction the current triangle faces, compared to it's
245 * pregenerated normal. Returns the dot product between the target
246 * normal and actual normal. If the dot product is close to 1.0, they
247 * nearly match. If the are close to -1.0, the are nearly
249 double check_cur_face(int n1, int n2, int n3) {
250 double v1[3], v2[3], approx_normal[3], dot_prod, temp;
252 /* check for the proper rotation by calculating an approximate
253 * normal and seeing if it is close to the precalculated normal */
254 v1[0] = nodes[n2][0] - nodes[n1][0];
255 v1[1] = nodes[n2][1] - nodes[n1][1];
256 v1[2] = nodes[n2][2] - nodes[n1][2];
257 v2[0] = nodes[n3][0] - nodes[n1][0];
258 v2[1] = nodes[n3][1] - nodes[n1][1];
259 v2[2] = nodes[n3][2] - nodes[n1][2];
261 MAT3cross_product(approx_normal, v1, v2);
262 MAT3_NORMALIZE_VEC(approx_normal,temp);
263 dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
265 /* not first triangle */
266 /* if ( ((dot_prod < -0.5) && !is_backwards) ||
267 ((dot_prod > 0.5) && is_backwards) ) {
268 printf(" Approx normal = %.2f %.2f %.2f\n", approx_normal[0],
269 approx_normal[1], approx_normal[2]);
270 printf(" Dot product = %.4f\n", dot_prod);
272 /* angle = acos(dot_prod); */
273 /* printf("Normal ANGLE = %.3f rads.\n", angle); */
279 /* Load a .obj file */
280 void obj_fix(char *infile, char *outfile) {
283 int first, n1, n2, n3, n4;
284 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
287 if ( (in = fopen(infile, "r")) == NULL ) {
288 printf("Cannot open file: %s\n", infile);
292 if ( (out = fopen(outfile, "w")) == NULL ) {
293 printf("Cannot open file: %s\n", outfile);
297 list_init(&ccw_list_ptr);
298 list_init(&cw_list_ptr);
300 /* I start counting at one because that is how the triangle
301 program refers to nodes and normals */
307 printf("Reading file: %s\n", infile);
309 while ( fgets(line, 250, in) != NULL ) {
310 if ( line[0] == '#' ) {
311 /* pass along the comments verbatim */
312 fprintf(out, "%s", line);
313 } else if ( strlen(line) <= 1 ) {
314 /* don't pass along empty lines */
315 // fprintf(out, "%s", line);
316 } else if ( strncmp(line, "v ", 2) == 0 ) {
317 /* save vertex to memory and output to file */
318 if ( ncount < MAXNODES ) {
319 /* printf("vertex = %s", line); */
320 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
321 nodes[ncount][0] = x;
322 nodes[ncount][1] = y;
323 nodes[ncount][2] = z;
325 /* first time through set min's and max'es */
335 /* keep track of min/max vertex values */
336 if ( x < xmin ) xmin = x;
337 if ( x > xmax ) xmax = x;
338 if ( y < ymin ) ymin = y;
339 if ( y > ymax ) ymax = y;
340 if ( z < zmin ) zmin = z;
341 if ( z > zmax ) zmax = z;
343 // fprintf(out, "v %.2f %.2f %.2f\n",
344 // nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
347 printf("Read too many nodes ... dying :-(\n");
350 } else if ( strncmp(line, "vn ", 3) == 0 ) {
351 /* save vertex normals to memory and output to file */
352 if ( vncount < MAXNODES ) {
353 /* printf("vertex normal = %s", line); */
354 sscanf(line, "vn %lf %lf %lf\n",
355 &normals[vncount][0], &normals[vncount][1],
356 &normals[vncount][2]);
357 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
358 // normals[vncount][1], normals[vncount][2]);
361 printf("Read too many vertex normals ... dying :-(\n");
364 } else if ( line[0] == 't' ) {
365 /* starting a new triangle strip */
367 printf("Starting a new triangle strip\n");
369 n1 = n2 = n3 = n4 = 0;
371 printf("new tri strip = %s", line);
372 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
374 /* special cases to handle bugs in our beloved tri striper */
375 if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
378 if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
382 dot_prod = check_cur_face(n1, n2, n3);
383 if ( dot_prod < 0.0 ) {
384 /* this stripe is backwards (CW) */
386 printf(" -> Starting a backwards stripe\n");
388 /* this stripe is normal (CCW) */
393 if ( ccw_list_ptr ) {
394 list_add(ccw_list, &ccw_list_ptr, -1);
397 list_add(ccw_list, &ccw_list_ptr, n1);
398 list_add(ccw_list, &ccw_list_ptr, n2);
399 list_add(ccw_list, &ccw_list_ptr, n3);
402 list_add(cw_list, &cw_list_ptr, -1);
405 list_add(cw_list, &cw_list_ptr, n1);
406 list_add(cw_list, &cw_list_ptr, n2);
407 list_add(cw_list, &cw_list_ptr, n3);
412 list_add(ccw_list, &ccw_list_ptr, n4);
414 list_add(cw_list, &cw_list_ptr, n4);
417 } else if ( line[0] == 'f' ) {
418 if ( fcount < MAXNODES ) {
419 /* pass along the unoptimized faces verbatim */
420 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
421 faces[fcount][0] = n1;
422 faces[fcount][1] = n2;
423 faces[fcount][2] = n3;
427 printf("Read too many unoptimized faces ... dying :-(\n");
431 // fprintf(out, "%s", line);
432 } else if ( line[0] == 'q' ) {
433 /* continue a triangle strip */
436 /* printf("continued tri strip = %s ", line); */
437 sscanf(line, "q %d %d\n", &n1, &n2);
440 list_add(ccw_list, &ccw_list_ptr, n1);
442 list_add(cw_list, &cw_list_ptr, n1);
447 list_add(ccw_list, &ccw_list_ptr, n2);
449 list_add(cw_list, &cw_list_ptr, n2);
453 printf("Unknown line in %s = %s\n", infile, line);
457 /* reference point is the "center" */
458 refx = (xmin + xmax) / 2.0;
459 refy = (ymin + ymax) / 2.0;
460 refz = (zmin + zmax) / 2.0;
462 /* convert the cw_list to ccw add append to ccw_list */
463 fix_cw_list(cw_list, cw_list_ptr);
465 dump_global_bounds();
472 dump_list(ccw_list, ccw_list_ptr);
480 /* Revision 1.14 1998/05/23 15:19:49 curt
481 /* Output more digits after the decimal place.
483 * Revision 1.13 1998/05/20 20:55:19 curt
484 * Fixed arbitrary polygon winding problem here so all tristrips are passed
485 * to runtime simulator with a consistant counter clockwise winding.
487 * Revision 1.12 1998/05/16 13:11:26 curt
488 * Fixed an off by one error in node, normal, and face counters.
490 * Revision 1.11 1998/04/27 15:59:24 curt
491 * Fixed an off by one error.
493 * Revision 1.10 1998/04/27 03:33:11 curt
494 * Code now calculates a center reference points and outputs everything
495 * relative to that. This is useful in the rendering engine to keep everything
496 * close to (0, 0, 0) where we can avoid many GLfloat precision problems.
498 * Revision 1.9 1998/04/18 04:01:03 curt
499 * Now use libMath rather than having local copies of math routines.
501 * Revision 1.8 1998/04/08 23:19:37 curt
502 * Adopted Gnu automake/autoconf system.
504 * Revision 1.7 1998/03/19 02:51:41 curt
505 * Added special case handling to compensate for bugs in our beloved tri striper
507 * Revision 1.6 1998/03/03 15:36:12 curt
508 * Tweaks for compiling with g++
510 * Revision 1.5 1998/03/03 03:37:03 curt
513 * Revision 1.4 1998/01/31 00:41:25 curt
514 * Made a few changes converting floats to doubles.
516 * Revision 1.3 1998/01/19 19:51:07 curt
517 * A couple final pre-release tweaks.
519 * Revision 1.2 1998/01/09 23:03:12 curt
520 * Restructured to split 1deg x 1deg dem's into 64 subsections.
522 * Revision 1.1 1997/12/08 19:28:54 curt