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); */
75 void dump_nodes( void ) {
79 for ( i = 1; i <= ncount; i++ ) {
80 fprintf(out, "v %.4lf %.4lf %.4lf\n",
81 nodes[i][0] - refx, nodes[i][1] - refy, nodes[i][2] - refz);
87 void dump_normals( void ) {
91 for ( i = 1; i <= vncount; i++ ) {
92 fprintf(out, "vn %.4lf %.4lf %.4lf\n",
93 normals[i][0], normals[i][1], normals[i][2]);
99 void dump_faces( void ) {
103 for ( i = 1; i <= fcount; i++ ) {
104 fprintf(out, "f %d %d %d\n",
105 faces[i][0], faces[i][1], faces[i][2]);
111 void dump_list(int *list, int list_ptr) {
114 if ( list_ptr < 3 ) {
115 printf("List is empty ... skipping\n");
119 printf("Dumping list, size = %d\n", list_ptr);
122 while ( i < list_ptr ) {
126 fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
127 /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
130 /* dump rest of strip (until -1) */
131 while ( (i < list_ptr) && (list[i] != -1) ) {
132 fprintf(out, "q %d", list[i]);
134 if ( (i < list_ptr) && (list[i] != -1) ) {
135 fprintf(out, " %d", list[i]);
146 /* Check the direction the current triangle faces, compared to it's
147 * pregenerated normal. Returns the dot product between the target
148 * normal and actual normal. If the dot product is close to 1.0, they
149 * nearly match. If the are close to -1.0, the are nearly
151 double check_cur_face(int n1, int n2, int n3) {
152 double v1[3], v2[3], approx_normal[3], dot_prod, temp;
154 /* check for the proper rotation by calculating an approximate
155 * normal and seeing if it is close to the 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];
163 MAT3cross_product(approx_normal, v1, v2);
164 MAT3_NORMALIZE_VEC(approx_normal,temp);
165 dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
167 /* not first triangle */
168 /* if ( ((dot_prod < -0.5) && !is_backwards) ||
169 ((dot_prod > 0.5) && is_backwards) ) {
170 printf(" Approx normal = %.2f %.2f %.2f\n", approx_normal[0],
171 approx_normal[1], approx_normal[2]);
172 printf(" Dot product = %.4f\n", dot_prod);
174 /* angle = acos(dot_prod); */
175 /* printf("Normal ANGLE = %.3f rads.\n", angle); */
181 /* Load a .obj file */
182 void obj_fix(char *infile, char *outfile) {
185 int first, n1, n2, n3, n4;
186 double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
189 if ( (in = fopen(infile, "r")) == NULL ) {
190 printf("Cannot open file: %s\n", infile);
194 if ( (out = fopen(outfile, "w")) == NULL ) {
195 printf("Cannot open file: %s\n", outfile);
199 list_init(&ccw_list_ptr);
200 list_init(&cw_list_ptr);
202 /* I start counting at one because that is how the triangle
203 program refers to nodes and normals */
209 printf("Reading file: %s\n", infile);
211 while ( fgets(line, 250, in) != NULL ) {
212 if ( line[0] == '#' ) {
213 /* pass along the comments verbatim */
214 fprintf(out, "%s", line);
215 } else if ( strlen(line) <= 1 ) {
216 /* don't pass along empty lines */
217 // fprintf(out, "%s", line);
218 } else if ( strncmp(line, "v ", 2) == 0 ) {
219 /* save vertex to memory and output to file */
220 if ( ncount < MAXNODES ) {
221 /* printf("vertex = %s", line); */
222 sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
223 nodes[ncount][0] = x;
224 nodes[ncount][1] = y;
225 nodes[ncount][2] = z;
227 /* first time through set min's and max'es */
237 /* keep track of min/max vertex values */
238 if ( x < xmin ) xmin = x;
239 if ( x > xmax ) xmax = x;
240 if ( y < ymin ) ymin = y;
241 if ( y > ymax ) ymax = y;
242 if ( z < zmin ) zmin = z;
243 if ( z > zmax ) zmax = z;
245 // fprintf(out, "v %.2f %.2f %.2f\n",
246 // nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
249 printf("Read too many nodes ... dying :-(\n");
252 } else if ( strncmp(line, "vn ", 3) == 0 ) {
253 /* save vertex normals to memory and output to file */
254 if ( vncount < MAXNODES ) {
255 /* printf("vertex normal = %s", line); */
256 sscanf(line, "vn %lf %lf %lf\n",
257 &normals[vncount][0], &normals[vncount][1],
258 &normals[vncount][2]);
259 // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
260 // normals[vncount][1], normals[vncount][2]);
263 printf("Read too many vertex normals ... dying :-(\n");
266 } else if ( line[0] == 't' ) {
267 /* starting a new triangle strip */
269 printf("Starting a new triangle strip\n");
271 n1 = n2 = n3 = n4 = 0;
273 printf("new tri strip = %s", line);
274 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
276 /* special cases to handle bugs in our beloved tri striper */
277 if ( (n1 == 4) && (n2 == 2) && (n3 == 2) && (n4 == 1) ) {
280 if ( (n1 == 3) && (n2 == 1) && (n3 == 1) && (n4 == 0) ) {
284 dot_prod = check_cur_face(n1, n2, n3);
285 if ( dot_prod < 0.0 ) {
286 /* this stripe is backwards (CW) */
288 printf(" -> Starting a backwards stripe\n");
290 /* this stripe is normal (CCW) */
295 if ( ccw_list_ptr ) {
296 list_add(ccw_list, &ccw_list_ptr, -1);
299 list_add(ccw_list, &ccw_list_ptr, n1);
300 list_add(ccw_list, &ccw_list_ptr, n2);
301 list_add(ccw_list, &ccw_list_ptr, n3);
304 list_add(cw_list, &cw_list_ptr, -1);
307 list_add(cw_list, &cw_list_ptr, n1);
308 list_add(cw_list, &cw_list_ptr, n2);
309 list_add(cw_list, &cw_list_ptr, n3);
314 list_add(ccw_list, &ccw_list_ptr, n4);
316 list_add(cw_list, &cw_list_ptr, n4);
319 } else if ( line[0] == 'f' ) {
320 if ( fcount < MAXNODES ) {
321 /* pass along the unoptimized faces verbatim */
322 sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
323 faces[fcount][0] = n1;
324 faces[fcount][1] = n2;
325 faces[fcount][2] = n3;
329 printf("Read too many unoptimized faces ... dying :-(\n");
333 // fprintf(out, "%s", line);
334 } else if ( line[0] == 'q' ) {
335 /* continue a triangle strip */
338 /* printf("continued tri strip = %s ", line); */
339 sscanf(line, "q %d %d\n", &n1, &n2);
342 list_add(ccw_list, &ccw_list_ptr, n1);
344 list_add(cw_list, &cw_list_ptr, n1);
349 list_add(ccw_list, &ccw_list_ptr, n2);
351 list_add(cw_list, &cw_list_ptr, n2);
355 printf("Unknown line in %s = %s\n", infile, line);
359 /* reference point is the "center" */
360 refx = (xmin + xmax) / 2.0;
361 refy = (ymin + ymax) / 2.0;
362 refz = (zmin + zmax) / 2.0;
365 fprintf(out, "ref %.2f %.2f %.2f\n", refx, refy, refz);
371 fprintf(out, "winding ccw\n");
372 dump_list(ccw_list, ccw_list_ptr);
374 fprintf(out, "winding cw\n");
375 dump_list(cw_list, cw_list_ptr);
383 /* Revision 1.12 1998/05/16 13:11:26 curt
384 /* Fixed an off by one error in node, normal, and face counters.
386 * Revision 1.11 1998/04/27 15:59:24 curt
387 * Fixed an off by one error.
389 * Revision 1.10 1998/04/27 03:33:11 curt
390 * Code now calculates a center reference points and outputs everything
391 * relative to that. This is useful in the rendering engine to keep everything
392 * close to (0, 0, 0) where we can avoid many GLfloat precision problems.
394 * Revision 1.9 1998/04/18 04:01:03 curt
395 * Now use libMath rather than having local copies of math routines.
397 * Revision 1.8 1998/04/08 23:19:37 curt
398 * Adopted Gnu automake/autoconf system.
400 * Revision 1.7 1998/03/19 02:51:41 curt
401 * Added special case handling to compensate for bugs in our beloved tri striper
403 * Revision 1.6 1998/03/03 15:36:12 curt
404 * Tweaks for compiling with g++
406 * Revision 1.5 1998/03/03 03:37:03 curt
409 * Revision 1.4 1998/01/31 00:41:25 curt
410 * Made a few changes converting floats to doubles.
412 * Revision 1.3 1998/01/19 19:51:07 curt
413 * A couple final pre-release tweaks.
415 * Revision 1.2 1998/01/09 23:03:12 curt
416 * Restructured to split 1deg x 1deg dem's into 64 subsections.
418 * Revision 1.1 1997/12/08 19:28:54 curt