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 *basename) {
144 char inpath[256], outpath[256];
146 int first, ncount, vncount, n1, n2, n3, n4;
149 strcpy(inpath, basename);
150 strcat(inpath, ".obj");
152 strcpy(outpath, basename);
153 strcat(outpath, ".1.obj");
155 if ( (in = fopen(inpath, "r")) == NULL ) {
156 printf("Cannot open file: %s\n", inpath);
160 if ( (out = fopen(outpath, "w")) == NULL ) {
161 printf("Cannot open file: %s\n", outpath);
165 list_init(&ccw_list_ptr);
166 list_init(&cw_list_ptr);
172 printf("Reading file: %s\n", inpath);
174 while ( fgets(line, 250, in) != NULL ) {
175 if ( line[0] == '#' ) {
176 /* pass along the comments verbatim */
177 fprintf(out, "%s", line);
178 } else if ( strlen(line) <= 1 ) {
179 /* pass along empty lines */
180 fprintf(out, "%s", line);
181 } else if ( strncmp(line, "v ", 2) == 0 ) {
182 /* save vertex to memory and output to file */
183 if ( ncount < MAXNODES ) {
184 /* printf("vertex = %s", line); */
185 sscanf(line, "v %f %f %f\n",
186 &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
187 fprintf(out, "v %.2f %.2f %.2f\n",
188 nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
191 printf("Read too many nodes ... dying :-(\n");
194 } else if ( strncmp(line, "vn ", 3) == 0 ) {
195 /* save vertex normals to memory and output to file */
196 if ( vncount < MAXNODES ) {
197 /* printf("vertex normal = %s", line); */
198 sscanf(line, "vn %f %f %f\n",
199 &normals[vncount][0], &normals[vncount][1],
200 &normals[vncount][2]);
201 fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
202 normals[vncount][1], normals[vncount][2]);
205 printf("Read too many vertex normals ... dying :-(\n");
208 } else if ( line[0] == 't' ) {
209 /* starting a new triangle strip */
211 printf("Starting a new triangle strip\n");
213 n1 = n2 = n3 = n4 = 0;
215 printf("new tri strip = %s", line);
216 sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
218 dot_prod = check_cur_face(n1, n2, n3);
219 if ( dot_prod < -0.5 ) {
220 /* this stripe is backwards (CW) */
222 printf(" -> Starting a backwards stripe\n");
224 /* this stripe is normal (CCW) */
229 if ( ccw_list_ptr ) {
230 list_add(ccw_list, &ccw_list_ptr, -1);
233 list_add(ccw_list, &ccw_list_ptr, n1);
234 list_add(ccw_list, &ccw_list_ptr, n2);
235 list_add(ccw_list, &ccw_list_ptr, n3);
238 list_add(cw_list, &cw_list_ptr, -1);
241 list_add(cw_list, &cw_list_ptr, n1);
242 list_add(cw_list, &cw_list_ptr, n2);
243 list_add(cw_list, &cw_list_ptr, n3);
248 list_add(ccw_list, &ccw_list_ptr, n4);
250 list_add(cw_list, &cw_list_ptr, n4);
253 } else if ( line[0] == 'f' ) {
254 /* pass along the unoptimized faces verbatim */
255 fprintf(out, "%s", line);
256 } else if ( line[0] == 'q' ) {
257 /* continue a triangle strip */
260 /* printf("continued tri strip = %s ", line); */
261 sscanf(line, "q %d %d\n", &n1, &n2);
264 list_add(ccw_list, &ccw_list_ptr, n1);
266 list_add(cw_list, &cw_list_ptr, n1);
271 list_add(ccw_list, &ccw_list_ptr, n2);
273 list_add(cw_list, &cw_list_ptr, n2);
277 printf("Unknown line in %s = %s\n", inpath, line);
281 fprintf(out, "winding ccw\n");
282 dump_list(ccw_list, ccw_list_ptr);
284 fprintf(out, "winding cw\n");
285 dump_list(cw_list, cw_list_ptr);
293 /* Revision 1.1 1997/12/08 19:28:54 curt