1 // obj.cxx -- routines to handle WaveFront .obj format files.
3 // Written by Curtis Olson, started October 1997.
5 // Copyright (C) 1997 - 1998 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
30 #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
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 opposite.
333 double check_cur_face(int n1, int n2, int n3) {
334 double v1[3], v2[3], approx_normal[3], dot_prod, temp;
336 // check for the proper rotation by calculating an approximate
337 // normal and seeing if it is close to the precalculated normal
338 v1[0] = nodes[n2][0] - nodes[n1][0];
339 v1[1] = nodes[n2][1] - nodes[n1][1];
340 v1[2] = nodes[n2][2] - nodes[n1][2];
341 v2[0] = nodes[n3][0] - nodes[n1][0];
342 v2[1] = nodes[n3][1] - nodes[n1][1];
343 v2[2] = nodes[n3][2] - nodes[n1][2];
345 MAT3cross_product(approx_normal, v1, v2);
346 MAT3_NORMALIZE_VEC(approx_normal,temp);
347 dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
349 // not first triangle
350 // if ( ((dot_prod < -0.5) && !is_backwards) ||
351 // ((dot_prod > 0.5) && is_backwards) ) {
352 // printf(" Approx normal = %.2f %.2f %.2f\n", approx_normal[0],
353 // approx_normal[1], approx_normal[2]);
354 // printf(" Dot product = %.4f\n", dot_prod);
356 // angle = acos(dot_prod);
357 // printf("Normal ANGLE = %.3f rads.\n", angle);
364 void obj_fix(char *infile, char *outfile) {
367 int first, n1, n2, n3, n4;
368 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
371 if ( (in = fopen(infile, "r")) == NULL ) {
372 printf("Cannot open file: %s\n", infile);
376 if ( (out = fopen(outfile, "w")) == NULL ) {
377 printf("Cannot open file: %s\n", outfile);
381 list_init(&ccw_list_ptr);
382 list_init(&cw_list_ptr);
384 // I start counting at one because that is how the triangle
385 // program refers to nodes and normals
391 printf("Reading file: %s\n", infile);
393 while ( fgets(line, 250, in) != NULL ) {
394 if ( line[0] == '#' ) {
395 // pass along the comments verbatim
396 fprintf(out, "%s", line);
397 } else if ( strlen(line) <= 1 ) {
398 // don't pass along empty lines
399 // fprintf(out, "%s", line);
400 } else if ( strncmp(line, "v ", 2) == 0 ) {
401 // save vertex to memory and output to file
402 if ( ncount < MAXNODES ) {
403 // printf("vertex = %s", line);
404 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
405 nodes[ncount][0] = x;
406 nodes[ncount][1] = y;
407 nodes[ncount][2] = z;
409 // first time through set min's and max'es
419 // keep track of min/max vertex values
420 if ( x < xmin ) xmin = x;
421 if ( x > xmax ) xmax = x;
422 if ( y < ymin ) ymin = y;
423 if ( y > ymax ) ymax = y;
424 if ( z < zmin ) zmin = z;
425 if ( z > zmax ) zmax = z;
427 // fprintf(out, "v %.2f %.2f %.2f\n",
428 // nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
431 printf("Read too many nodes ... dying :-(\n");
434 } else if ( strncmp(line, "vn ", 3) == 0 ) {
435 // save vertex normals to memory and output to file
436 if ( vncount < MAXNODES ) {
437 // printf("vertex normal = %s", line);
438 sscanf(line, "vn %lf %lf %lf\n",
439 &normals[vncount][0], &normals[vncount][1],
440 &normals[vncount][2]);
441 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
442 // normals[vncount][1], normals[vncount][2]);
445 printf("Read too many vertex normals ... dying :-(\n");
448 } else if ( line[0] == 't' ) {
449 // starting a new triangle strip
451 printf("Starting a new triangle strip\n");
453 n1 = n2 = n3 = n4 = 0;
455 printf("new tri strip = %s", line);
456 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
458 // special cases to handle bugs in our beloved tri striper
459 if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
462 if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
466 dot_prod = check_cur_face(n1, n2, n3);
467 if ( dot_prod < 0.0 ) {
468 // this stripe is backwards (CW)
470 printf(" -> Starting a backwards stripe\n");
472 // this stripe is normal (CCW)
477 if ( ccw_list_ptr ) {
478 list_add(ccw_list, &ccw_list_ptr, -1);
481 list_add(ccw_list, &ccw_list_ptr, n1);
482 list_add(ccw_list, &ccw_list_ptr, n2);
483 list_add(ccw_list, &ccw_list_ptr, n3);
486 list_add(cw_list, &cw_list_ptr, -1);
489 list_add(cw_list, &cw_list_ptr, n1);
490 list_add(cw_list, &cw_list_ptr, n2);
491 list_add(cw_list, &cw_list_ptr, n3);
496 list_add(ccw_list, &ccw_list_ptr, n4);
498 list_add(cw_list, &cw_list_ptr, n4);
501 } else if ( line[0] == 'f' ) {
502 if ( fcount < MAXNODES ) {
503 // pass along the unoptimized faces verbatim
504 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
505 faces[fcount][0] = n1;
506 faces[fcount][1] = n2;
507 faces[fcount][2] = n3;
511 printf("Read too many unoptimized faces ... dying :-(\n");
515 // fprintf(out, "%s", line);
516 } else if ( line[0] == 'q' ) {
517 // continue a triangle strip
520 // printf("continued tri strip = %s ", line);
521 sscanf(line, "q %d %d\n", &n1, &n2);
524 list_add(ccw_list, &ccw_list_ptr, n1);
526 list_add(cw_list, &cw_list_ptr, n1);
531 list_add(ccw_list, &ccw_list_ptr, n2);
533 list_add(cw_list, &cw_list_ptr, n2);
537 printf("Unknown line in %s = %s\n", infile, line);
541 // reference point is the "center"
542 refx = (xmin + xmax) / 2.0;
543 refy = (ymin + ymax) / 2.0;
544 refz = (zmin + zmax) / 2.0;
546 // convert the cw_list to ccw add append to ccw_list
547 fix_cw_list(cw_list, cw_list_ptr);
549 dump_global_bounds();
556 dump_list(ccw_list, ccw_list_ptr);
564 // Revision 1.1 1998/06/08 17:11:46 curt
565 // Renamed *.[ch] to *.[ch]xx
567 // Revision 1.16 1998/05/27 02:27:22 curt
568 // Commented out a couple of debugging messages.
570 // Revision 1.15 1998/05/24 02:47:47 curt
571 // For each strip, specify a default material property and calculate a center
572 // and bounding sphere.
574 // Revision 1.14 1998/05/23 15:19:49 curt
575 // Output more digits after the decimal place.
577 // Revision 1.13 1998/05/20 20:55:19 curt
578 // Fixed arbitrary polygon winding problem here so all tristrips are passed
579 // to runtime simulator with a consistant counter clockwise winding.
581 // Revision 1.12 1998/05/16 13:11:26 curt
582 // Fixed an off by one error in node, normal, and face counters.
584 // Revision 1.11 1998/04/27 15:59:24 curt
585 // Fixed an off by one error.
587 // Revision 1.10 1998/04/27 03:33:11 curt
588 // Code now calculates a center reference points and outputs everything
589 // relative to that. This is useful in the rendering engine to keep everything
590 // close to (0, 0, 0) where we can avoid many GLfloat precision problems.
592 // Revision 1.9 1998/04/18 04:01:03 curt
593 // Now use libMath rather than having local copies of math routines.
595 // Revision 1.8 1998/04/08 23:19:37 curt
596 // Adopted Gnu automake/autoconf system.
598 // Revision 1.7 1998/03/19 02:51:41 curt
599 // Added special case handling to compensate for bugs in our beloved tri striper
601 // Revision 1.6 1998/03/03 15:36:12 curt
602 // Tweaks for compiling with g++
604 // Revision 1.5 1998/03/03 03:37:03 curt
605 // Cumulative tweaks.
607 // Revision 1.4 1998/01/31 00:41:25 curt
608 // Made a few changes converting floats to doubles.
610 // Revision 1.3 1998/01/19 19:51:07 curt
611 // A couple final pre-release tweaks.
613 // Revision 1.2 1998/01/09 23:03:12 curt
614 // Restructured to split 1deg x 1deg dem's into 64 subsections.
616 // Revision 1.1 1997/12/08 19:28:54 curt