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 "../../Src/Math/mat3.h"
35 /* what do ya' know, here's some global variables */
36 float nodes[MAXNODES][3];
37 float normals[MAXNODES][3];
39 int ccw_list[MAXNODES];
42 int cw_list[MAXNODES];
48 /* some simple list routines */
51 void list_init(int *list_ptr) {
57 void list_add(int *list, int *list_ptr, int node) {
58 if ( *list_ptr >= MAXNODES ) {
59 printf("ERROR: list overflow in list_add()\n");
63 list[*list_ptr] = node;
66 /* printf("list pointer = %d adding %d\n", *list_ptr, node); */
71 void dump_list(int *list, int list_ptr) {
75 printf("List is empty ... skipping\n");
79 printf("Dumping list, size = %d\n", list_ptr);
82 while ( i < list_ptr ) {
86 fprintf(out, "t %d %d %d\n", list[i], list[i+1], list[i+2]);
87 /* printf("t %d %d %d\n", list[i], list[i+1], list[i+2]); */
90 /* dump rest of strip (until -1) */
91 while ( (i < list_ptr) && (list[i] != -1) ) {
92 fprintf(out, "q %d", list[i]);
94 if ( (i < list_ptr) && (list[i] != -1) ) {
95 fprintf(out, " %d", list[i]);
106 /* Check the direction the current triangle faces, compared to it's
107 * pregenerated normal. Returns the dot product between the target
108 * normal and actual normal. If the dot product is close to 1.0, they
109 * nearly match. If the are close to -1.0, the are nearly
111 double check_cur_face(int n1, int n2, int n3) {
112 double v1[3], v2[3], approx_normal[3], dot_prod, temp;
114 /* check for the proper rotation by calculating an approximate
115 * normal and seeing if it is close to the precalculated normal */
116 v1[0] = nodes[n2][0] - nodes[n1][0];
117 v1[1] = nodes[n2][1] - nodes[n1][1];
118 v1[2] = nodes[n2][2] - nodes[n1][2];
119 v2[0] = nodes[n3][0] - nodes[n1][0];
120 v2[1] = nodes[n3][1] - nodes[n1][1];
121 v2[2] = nodes[n3][2] - nodes[n1][2];
123 MAT3cross_product(approx_normal, v1, v2);
124 MAT3_NORMALIZE_VEC(approx_normal,temp);
125 dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
127 /* not first triangle */
128 /* if ( ((dot_prod < -0.5) && !is_backwards) ||
129 ((dot_prod > 0.5) && is_backwards) ) {
130 printf(" Approx normal = %.2f %.2f %.2f\n", approx_normal[0],
131 approx_normal[1], approx_normal[2]);
132 printf(" Dot product = %.4f\n", dot_prod);
134 /* angle = acos(dot_prod); */
135 /* printf("Normal ANGLE = %.3f rads.\n", angle); */
141 /* Load a .obj file */
142 void obj_fix(char *infile, char *outfile) {
145 int first, ncount, vncount, n1, n2, n3, n4;
148 if ( (in = fopen(infile, "r")) == NULL ) {
149 printf("Cannot open file: %s\n", infile);
153 if ( (out = fopen(outfile, "w")) == NULL ) {
154 printf("Cannot open file: %s\n", outfile);
158 list_init(&ccw_list_ptr);
159 list_init(&cw_list_ptr);
165 printf("Reading file: %s\n", infile);
167 while ( fgets(line, 250, in) != NULL ) {
168 if ( line[0] == '#' ) {
169 /* pass along the comments verbatim */
170 fprintf(out, "%s", line);
171 } else if ( strlen(line) <= 1 ) {
172 /* pass along empty lines */
173 fprintf(out, "%s", line);
174 } else if ( strncmp(line, "v ", 2) == 0 ) {
175 /* save vertex to memory and output to file */
176 if ( ncount < MAXNODES ) {
177 /* printf("vertex = %s", line); */
178 sscanf(line, "v %f %f %f\n",
179 &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
180 fprintf(out, "v %.2f %.2f %.2f\n",
181 nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
184 printf("Read too many nodes ... dying :-(\n");
187 } else if ( strncmp(line, "vn ", 3) == 0 ) {
188 /* save vertex normals to memory and output to file */
189 if ( vncount < MAXNODES ) {
190 /* printf("vertex normal = %s", line); */
191 sscanf(line, "vn %f %f %f\n",
192 &normals[vncount][0], &normals[vncount][1],
193 &normals[vncount][2]);
194 fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
195 normals[vncount][1], normals[vncount][2]);
198 printf("Read too many vertex normals ... dying :-(\n");
201 } else if ( line[0] == 't' ) {
202 /* starting a new triangle strip */
204 printf("Starting a new triangle strip\n");
206 n1 = n2 = n3 = n4 = 0;
208 printf("new tri strip = %s", line);
209 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
211 dot_prod = check_cur_face(n1, n2, n3);
212 if ( dot_prod < -0.5 ) {
213 /* this stripe is backwards (CW) */
215 printf(" -> Starting a backwards stripe\n");
217 /* this stripe is normal (CCW) */
222 if ( ccw_list_ptr ) {
223 list_add(ccw_list, &ccw_list_ptr, -1);
226 list_add(ccw_list, &ccw_list_ptr, n1);
227 list_add(ccw_list, &ccw_list_ptr, n2);
228 list_add(ccw_list, &ccw_list_ptr, n3);
231 list_add(cw_list, &cw_list_ptr, -1);
234 list_add(cw_list, &cw_list_ptr, n1);
235 list_add(cw_list, &cw_list_ptr, n2);
236 list_add(cw_list, &cw_list_ptr, n3);
241 list_add(ccw_list, &ccw_list_ptr, n4);
243 list_add(cw_list, &cw_list_ptr, n4);
246 } else if ( line[0] == 'f' ) {
247 /* pass along the unoptimized faces verbatim */
248 fprintf(out, "%s", line);
249 } else if ( line[0] == 'q' ) {
250 /* continue a triangle strip */
253 /* printf("continued tri strip = %s ", line); */
254 sscanf(line, "q %d %d\n", &n1, &n2);
257 list_add(ccw_list, &ccw_list_ptr, n1);
259 list_add(cw_list, &cw_list_ptr, n1);
264 list_add(ccw_list, &ccw_list_ptr, n2);
266 list_add(cw_list, &cw_list_ptr, n2);
270 printf("Unknown line in %s = %s\n", infile, line);
274 fprintf(out, "winding ccw\n");
275 dump_list(ccw_list, ccw_list_ptr);
277 fprintf(out, "winding cw\n");
278 dump_list(cw_list, cw_list_ptr);
286 /* Revision 1.2 1998/01/09 23:03:12 curt
287 /* Restructured to split 1deg x 1deg dem's into 64 subsections.
289 * Revision 1.1 1997/12/08 19:28:54 curt