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, "gbs %.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 ) {
193 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin, dist, radius;
196 for ( i = 1; i < fcount; i++ ) {
201 /* calc center of face */
202 xmin = xmax = nodes[n1][0];
203 ymin = ymax = nodes[n1][1];
204 zmin = zmax = nodes[n1][2];
206 if ( nodes[n2][0] < xmin ) { xmin = nodes[n2][0]; }
207 if ( nodes[n2][0] > xmax ) { xmax = nodes[n2][0]; }
208 if ( nodes[n2][1] < ymin ) { ymin = nodes[n2][1]; }
209 if ( nodes[n2][1] > ymax ) { ymax = nodes[n2][1]; }
210 if ( nodes[n2][2] < zmin ) { zmin = nodes[n2][2]; }
211 if ( nodes[n2][2] > zmax ) { zmax = nodes[n2][2]; }
213 if ( nodes[n3][0] < xmin ) { xmin = nodes[n3][0]; }
214 if ( nodes[n3][0] > xmax ) { xmax = nodes[n3][0]; }
215 if ( nodes[n3][1] < ymin ) { ymin = nodes[n3][1]; }
216 if ( nodes[n3][1] > ymax ) { ymax = nodes[n3][1]; }
217 if ( nodes[n3][2] < zmin ) { zmin = nodes[n3][2]; }
218 if ( nodes[n3][2] > zmax ) { zmax = nodes[n3][2]; }
220 x = (xmin + xmax) / 2.0;
221 y = (ymin + ymax) / 2.0;
222 z = (zmin + zmax) / 2.0;
224 /* calc bounding radius */
225 radius = calc_dist(nodes[n1][0] - x, nodes[n1][1] - y,
228 dist = calc_dist(nodes[n2][0] - x, nodes[n2][1] - y, nodes[n2][2] - z);
229 if ( dist > radius ) { radius = dist; }
231 dist = calc_dist(nodes[n3][0] - x, nodes[n3][1] - y, nodes[n3][2] - z);
232 if ( dist > radius ) { radius = dist; }
235 fprintf(out, "bs %.2f %.2f %.2f %.2f\n", x, y, z, radius);
236 fprintf(out, "f %d %d %d\n", n1, n2, n3);
242 void dump_list(int *list, int list_ptr) {
243 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin, dist, radius;
246 if ( list_ptr < 3 ) {
247 printf("List is empty ... skipping\n");
251 printf("Dumping list, size = %d\n", list_ptr);
254 while ( i < list_ptr ) {
257 if ( (i % 2) == 0 ) {
258 fprintf(out, "\nusemtl desert1\n");
260 fprintf(out, "\nusemtl desert2\n");
263 /* find length of next tri strip */
265 /* scan rest of strip (until -1) */
266 while ( ((i+len) < list_ptr) && (list[i+len] != -1) ) {
267 // printf("len = %d item = %d\n", len, list[i+len] );
270 // printf("strip length = %d\n", len);
272 /* calc center of face */
274 xmin = xmax = nodes[n][0];
275 ymin = ymax = nodes[n][1];
276 zmin = zmax = nodes[n][2];
277 printf("%.2f %.2f %.2f\n", nodes[n][0], nodes[n][1], nodes[n][2]);
279 for ( j = i + 1; j < i + len; j++ ) {
280 // printf("j = %d\n", j);
282 if ( nodes[n][0] < xmin ) { xmin = nodes[n][0]; }
283 if ( nodes[n][0] > xmax ) { xmax = nodes[n][0]; }
284 if ( nodes[n][1] < ymin ) { ymin = nodes[n][1]; }
285 if ( nodes[n][1] > ymax ) { ymax = nodes[n][1]; }
286 if ( nodes[n][2] < zmin ) { zmin = nodes[n][2]; }
287 if ( nodes[n][2] > zmax ) { zmax = nodes[n][2]; }
288 printf("%.2f %.2f %.2f\n", nodes[n][0], nodes[n][1], nodes[n][2]);
290 x = (xmin + xmax) / 2.0;
291 y = (ymin + ymax) / 2.0;
292 z = (zmin + zmax) / 2.0;
293 printf("center = %.2f %.2f %.2f\n", x, y, z);
295 /* calc bounding radius */
297 radius = calc_dist(nodes[n][0] - x, nodes[n][1] - y, nodes[n][2] - z);
299 for ( j = i + 1; j < i + len; j++ ) {
301 dist = calc_dist(nodes[n][0] - x, nodes[n][1] - y,
303 if ( dist > radius ) { radius = dist; }
305 printf("radius = %.2f\n", radius);
307 /* dump bounding sphere and header */
308 fprintf(out, "bs %.2f %.2f %.2f %.2f\n", x, y, z, radius);
309 fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
310 /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
313 /* dump rest of strip (until -1) */
314 while ( (i < list_ptr) && (list[i] != -1) ) {
315 fprintf(out, "q %d", list[i]);
317 if ( (i < list_ptr) && (list[i] != -1) ) {
318 fprintf(out, " %d", list[i]);
329 /* Check the direction the current triangle faces, compared to it's
330 * pregenerated normal. Returns the dot product between the target
331 * normal and actual normal. If the dot product is close to 1.0, they
332 * nearly match. If the are close to -1.0, the are nearly
334 double check_cur_face(int n1, int n2, int n3) {
335 double v1[3], v2[3], approx_normal[3], dot_prod, temp;
337 /* check for the proper rotation by calculating an approximate
338 * normal and seeing if it is close to the precalculated normal */
339 v1[0] = nodes[n2][0] - nodes[n1][0];
340 v1[1] = nodes[n2][1] - nodes[n1][1];
341 v1[2] = nodes[n2][2] - nodes[n1][2];
342 v2[0] = nodes[n3][0] - nodes[n1][0];
343 v2[1] = nodes[n3][1] - nodes[n1][1];
344 v2[2] = nodes[n3][2] - nodes[n1][2];
346 MAT3cross_product(approx_normal, v1, v2);
347 MAT3_NORMALIZE_VEC(approx_normal,temp);
348 dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
350 /* not first triangle */
351 /* if ( ((dot_prod < -0.5) && !is_backwards) ||
352 ((dot_prod > 0.5) && is_backwards) ) {
353 printf(" Approx normal = %.2f %.2f %.2f\n", approx_normal[0],
354 approx_normal[1], approx_normal[2]);
355 printf(" Dot product = %.4f\n", dot_prod);
357 /* angle = acos(dot_prod); */
358 /* printf("Normal ANGLE = %.3f rads.\n", angle); */
364 /* Load a .obj file */
365 void obj_fix(char *infile, char *outfile) {
368 int first, n1, n2, n3, n4;
369 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
372 if ( (in = fopen(infile, "r")) == NULL ) {
373 printf("Cannot open file: %s\n", infile);
377 if ( (out = fopen(outfile, "w")) == NULL ) {
378 printf("Cannot open file: %s\n", outfile);
382 list_init(&ccw_list_ptr);
383 list_init(&cw_list_ptr);
385 /* I start counting at one because that is how the triangle
386 program refers to nodes and normals */
392 printf("Reading file: %s\n", infile);
394 while ( fgets(line, 250, in) != NULL ) {
395 if ( line[0] == '#' ) {
396 /* pass along the comments verbatim */
397 fprintf(out, "%s", line);
398 } else if ( strlen(line) <= 1 ) {
399 /* don't pass along empty lines */
400 // fprintf(out, "%s", line);
401 } else if ( strncmp(line, "v ", 2) == 0 ) {
402 /* save vertex to memory and output to file */
403 if ( ncount < MAXNODES ) {
404 /* printf("vertex = %s", line); */
405 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
406 nodes[ncount][0] = x;
407 nodes[ncount][1] = y;
408 nodes[ncount][2] = z;
410 /* first time through set min's and max'es */
420 /* keep track of min/max vertex values */
421 if ( x < xmin ) xmin = x;
422 if ( x > xmax ) xmax = x;
423 if ( y < ymin ) ymin = y;
424 if ( y > ymax ) ymax = y;
425 if ( z < zmin ) zmin = z;
426 if ( z > zmax ) zmax = z;
428 // fprintf(out, "v %.2f %.2f %.2f\n",
429 // nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
432 printf("Read too many nodes ... dying :-(\n");
435 } else if ( strncmp(line, "vn ", 3) == 0 ) {
436 /* save vertex normals to memory and output to file */
437 if ( vncount < MAXNODES ) {
438 /* printf("vertex normal = %s", line); */
439 sscanf(line, "vn %lf %lf %lf\n",
440 &normals[vncount][0], &normals[vncount][1],
441 &normals[vncount][2]);
442 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
443 // normals[vncount][1], normals[vncount][2]);
446 printf("Read too many vertex normals ... dying :-(\n");
449 } else if ( line[0] == 't' ) {
450 /* starting a new triangle strip */
452 printf("Starting a new triangle strip\n");
454 n1 = n2 = n3 = n4 = 0;
456 printf("new tri strip = %s", line);
457 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
459 /* special cases to handle bugs in our beloved tri striper */
460 if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
463 if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
467 dot_prod = check_cur_face(n1, n2, n3);
468 if ( dot_prod < 0.0 ) {
469 /* this stripe is backwards (CW) */
471 printf(" -> Starting a backwards stripe\n");
473 /* this stripe is normal (CCW) */
478 if ( ccw_list_ptr ) {
479 list_add(ccw_list, &ccw_list_ptr, -1);
482 list_add(ccw_list, &ccw_list_ptr, n1);
483 list_add(ccw_list, &ccw_list_ptr, n2);
484 list_add(ccw_list, &ccw_list_ptr, n3);
487 list_add(cw_list, &cw_list_ptr, -1);
490 list_add(cw_list, &cw_list_ptr, n1);
491 list_add(cw_list, &cw_list_ptr, n2);
492 list_add(cw_list, &cw_list_ptr, n3);
497 list_add(ccw_list, &ccw_list_ptr, n4);
499 list_add(cw_list, &cw_list_ptr, n4);
502 } else if ( line[0] == 'f' ) {
503 if ( fcount < MAXNODES ) {
504 /* pass along the unoptimized faces verbatim */
505 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
506 faces[fcount][0] = n1;
507 faces[fcount][1] = n2;
508 faces[fcount][2] = n3;
512 printf("Read too many unoptimized faces ... dying :-(\n");
516 // fprintf(out, "%s", line);
517 } else if ( line[0] == 'q' ) {
518 /* continue a triangle strip */
521 /* printf("continued tri strip = %s ", line); */
522 sscanf(line, "q %d %d\n", &n1, &n2);
525 list_add(ccw_list, &ccw_list_ptr, n1);
527 list_add(cw_list, &cw_list_ptr, n1);
532 list_add(ccw_list, &ccw_list_ptr, n2);
534 list_add(cw_list, &cw_list_ptr, n2);
538 printf("Unknown line in %s = %s\n", infile, line);
542 /* reference point is the "center" */
543 refx = (xmin + xmax) / 2.0;
544 refy = (ymin + ymax) / 2.0;
545 refz = (zmin + zmax) / 2.0;
547 /* convert the cw_list to ccw add append to ccw_list */
548 fix_cw_list(cw_list, cw_list_ptr);
550 dump_global_bounds();
557 dump_list(ccw_list, ccw_list_ptr);
565 /* Revision 1.15 1998/05/24 02:47:47 curt
566 /* For each strip, specify a default material property and calculate a center
567 /* and bounding sphere.
569 * Revision 1.14 1998/05/23 15:19:49 curt
570 * Output more digits after the decimal place.
572 * Revision 1.13 1998/05/20 20:55:19 curt
573 * Fixed arbitrary polygon winding problem here so all tristrips are passed
574 * to runtime simulator with a consistant counter clockwise winding.
576 * Revision 1.12 1998/05/16 13:11:26 curt
577 * Fixed an off by one error in node, normal, and face counters.
579 * Revision 1.11 1998/04/27 15:59:24 curt
580 * Fixed an off by one error.
582 * Revision 1.10 1998/04/27 03:33:11 curt
583 * Code now calculates a center reference points and outputs everything
584 * relative to that. This is useful in the rendering engine to keep everything
585 * close to (0, 0, 0) where we can avoid many GLfloat precision problems.
587 * Revision 1.9 1998/04/18 04:01:03 curt
588 * Now use libMath rather than having local copies of math routines.
590 * Revision 1.8 1998/04/08 23:19:37 curt
591 * Adopted Gnu automake/autoconf system.
593 * Revision 1.7 1998/03/19 02:51:41 curt
594 * Added special case handling to compensate for bugs in our beloved tri striper
596 * Revision 1.6 1998/03/03 15:36:12 curt
597 * Tweaks for compiling with g++
599 * Revision 1.5 1998/03/03 03:37:03 curt
602 * Revision 1.4 1998/01/31 00:41:25 curt
603 * Made a few changes converting floats to doubles.
605 * Revision 1.3 1998/01/19 19:51:07 curt
606 * A couple final pre-release tweaks.
608 * Revision 1.2 1998/01/09 23:03:12 curt
609 * Restructured to split 1deg x 1deg dem's into 64 subsections.
611 * Revision 1.1 1997/12/08 19:28:54 curt