]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
7257ffaebd5965b4f0e2f4ca6665ec51f29d4cba
[flightgear.git] / Scenery / obj.c
1 /**************************************************************************
2  * obj.c -- routines to handle WaveFront .obj format files.
3  *
4  * Written by Curtis Olson, started October 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef WIN32
28 #  include <windows.h>
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <GL/glut.h>
34 #include "../XGL/xgl.h"
35
36 #include "obj.h"
37 #include "scenery.h"
38
39 #include "../Math/mat3.h"
40
41
42
43 #define MAXNODES 100000
44
45 float nodes[MAXNODES][3];
46 float normals[MAXNODES][3];
47
48
49 /* given three points defining a triangle, calculate the normal */
50 void calc_normal(float p1[3], float p2[3], float p3[3], double normal[3])
51 {
52     double v1[3], v2[3];
53     float temp;
54
55     v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
56     v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
57
58     MAT3cross_product(normal, v1, v2);
59     MAT3_NORMALIZE_VEC(normal,temp);
60
61     /* printf("  Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);*/
62 }
63
64
65 /* Load a .obj file and generate the GL call list */
66 GLint fgObjLoad(char *path) {
67     char line[256], winding_str[256];
68     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
69     struct fgCartesianPoint ref;
70     GLint area;
71     FILE *f;
72     int first, ncount, vncount, n1, n2, n3, n4;
73     static int use_vertex_norms = 1;
74     int i, winding;
75     int last1, last2, odd;
76
77     if ( (f = fopen(path, "r")) == NULL ) {
78         printf("Cannot open file: %s\n", path);
79         exit(-1);
80     }
81
82     area = xglGenLists(1);
83     xglNewList(area, GL_COMPILE);
84
85     first = 1;
86     ncount = 1;
87     vncount = 1;
88
89     while ( fgets(line, 250, f) != NULL ) {
90         if ( line[0] == '#' ) {
91             /* comment -- ignore */
92         } else if ( strncmp(line, "v ", 2) == 0 ) {
93             /* node (vertex) */
94             if ( ncount < MAXNODES ) {
95                 /* printf("vertex = %s", line); */
96                 sscanf(line, "v %f %f %f\n", 
97                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
98                 if ( ncount == 1 ) {
99                     /* first node becomes the reference point */
100                     ref.x = nodes[ncount][0];
101                     ref.y = nodes[ncount][1];
102                     ref.z = nodes[ncount][2];
103                     scenery.center = ref;
104                 }
105                 ncount++;
106             } else {
107                 printf("Read too many nodes ... dying :-(\n");
108                 exit(-1);
109             }
110         } else if ( strncmp(line, "vn ", 3) == 0 ) {
111             /* vertex normal */
112             if ( vncount < MAXNODES ) {
113                 /* printf("vertex normal = %s", line); */
114                 sscanf(line, "vn %f %f %f\n", 
115                        &normals[vncount][0], &normals[vncount][1], 
116                        &normals[vncount][2]);
117                 vncount++;
118             } else {
119                 printf("Read too many vertex normals ... dying :-(\n");
120                 exit(-1);
121             }
122         } else if ( strncmp(line, "winding ", 8) == 0 ) {
123             sscanf(line+8, "%s", winding_str);
124             printf("WINDING = %s\n", winding_str);
125
126             /* can't call xglFrontFace() between xglBegin() & xglEnd() */
127             xglEnd();
128             first = 1;
129
130             if ( strcmp(winding_str, "cw") == 0 ) {
131                 xglFrontFace( GL_CW );
132                 winding = 0;
133             } else {
134                 xglFrontFace ( GL_CCW );
135                 winding = 1;
136             }
137         } else if ( line[0] == 't' ) {
138             /* start a new triangle strip */
139
140             n1 = n2 = n3 = n4 = 0;
141
142             if ( !first ) {
143                 /* close out the previous structure and start the next */
144                 xglEnd();
145             } else {
146                 first = 0;
147             }
148
149             printf("new tri strip = %s", line);
150             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
151
152             /* printf("(t) = "); */
153
154             xglBegin(GL_TRIANGLE_STRIP);
155
156             if ( winding ) {
157                 odd = 1; 
158             } else {
159                 odd = 0;
160             }
161
162             if ( use_vertex_norms ) {
163                 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
164                 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
165                             nodes[n1][2] - ref.z);
166
167                 xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
168                 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
169                             nodes[n2][2] - ref.z);
170
171                 xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
172                 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
173                             nodes[n3][2] - ref.z);
174             } else {
175                 if ( odd ) {
176                     calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
177                 } else {
178                     calc_normal(nodes[n2], nodes[n1], nodes[n3], approx_normal);
179                 }
180                 xglNormal3dv(approx_normal);
181
182                 xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
183                             nodes[n1][2] - ref.z);
184                 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
185                             nodes[n2][2] - ref.z);
186                 xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
187                             nodes[n3][2] - ref.z);
188             }
189
190             odd = 1 - odd;
191             last1 = n2;
192             last2 = n3;
193
194             if ( n4 > 0 ) {
195                 if ( use_vertex_norms ) {
196                     xglNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);
197                 } else {
198                     calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
199                     xglNormal3dv(approx_normal);
200                 }
201                 xglVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y, 
202                             nodes[n4][2] - ref.z);
203
204                 odd = 1 - odd;
205                 last1 = n3;
206                 last2 = n4;
207             }
208         } else if ( line[0] == 'f' ) {
209             /* unoptimized face */
210
211             if ( !first ) {
212                 /* close out the previous structure and start the next */
213                 xglEnd();
214             } else {
215                 first = 0;
216             }
217
218             xglBegin(GL_TRIANGLES);
219
220             /* printf("new triangle = %s", line);*/
221             sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
222
223             xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
224             xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
225                         nodes[n1][2] - ref.z);
226
227             xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
228             xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
229                         nodes[n2][2] - ref.z);
230
231             xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
232             xglVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
233                         nodes[n3][2] - ref.z);
234         } else if ( line[0] == 'q' ) {
235             /* continue a triangle strip */
236             n1 = n2 = 0;
237
238             /* printf("continued tri strip = %s ", line); */
239             sscanf(line, "q %d %d\n", &n1, &n2);
240             /* printf("read %d %d\n", n1, n2); */
241
242             if ( use_vertex_norms ) {
243                 xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
244             } else {
245                 if ( odd ) {
246                     calc_normal(nodes[last1], nodes[last2], nodes[n1], 
247                                 approx_normal);
248                 } else {
249                     calc_normal(nodes[last2], nodes[last1], nodes[n1], 
250                                 approx_normal);
251                 }
252                 xglNormal3dv(approx_normal);
253             }
254
255             xglVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
256                         nodes[n1][2] - ref.z);
257             
258             odd = 1 - odd;
259             last1 = last2;
260             last2 = n1;
261
262             if ( n2 > 0 ) {
263                 /* printf(" (cont)\n"); */
264
265                 if ( use_vertex_norms ) {
266                     xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
267                 } else {
268                     if ( odd ) {
269                         calc_normal(nodes[last1], nodes[last2], nodes[n2], 
270                                     approx_normal);
271                     } else {
272                         calc_normal(nodes[last2], nodes[last1], nodes[n2], 
273                                     approx_normal);
274                     }
275                     xglNormal3dv(approx_normal);
276                 }
277
278                 xglVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
279                             nodes[n2][2] - ref.z);
280
281                 odd = 1 -odd;
282                 last1 = last2;
283                 last2 = n2;
284             }
285         } else {
286             printf("Unknown line in %s = %s\n", path, line);
287         }
288     }
289
290     xglEnd();
291
292     /* Draw normal vectors (for visually verifying normals)*/
293     /*
294     xglBegin(GL_LINES);
295     xglColor3f(0.0, 0.0, 0.0);
296     for ( i = 0; i < ncount; i++ ) {
297         xglVertex3d(nodes[i][0] - ref.x,
298                     nodes[i][1] - ref.y,
299                     nodes[i][2] - ref.z);
300         xglVertex3d(nodes[i][0] - ref.x + 500*normals[i][0],
301                     nodes[i][1] - ref.y + 500*normals[i][1],
302                     nodes[i][2] - ref.z + 500*normals[i][2]);
303     } 
304     xglEnd();
305     */
306
307     xglFrontFace ( GL_CCW );
308
309     xglEndList();
310
311     fclose(f);
312
313     return(area);
314 }
315
316
317 /* $Log$
318 /* Revision 1.14  1997/12/30 01:38:46  curt
319 /* Switched back to per vertex normals and smooth shading for terrain.
320 /*
321  * Revision 1.13  1997/12/18 23:32:36  curt
322  * First stab at sky dome actually starting to look reasonable. :-)
323  *
324  * Revision 1.12  1997/12/17 23:13:47  curt
325  * Began working on rendering the sky.
326  *
327  * Revision 1.11  1997/12/15 23:55:01  curt
328  * Add xgl wrappers for debugging.
329  * Generate terrain normals on the fly.
330  *
331  * Revision 1.10  1997/12/12 21:41:28  curt
332  * More light/material property tweaking ... still a ways off.
333  *
334  * Revision 1.9  1997/12/12 19:52:57  curt
335  * Working on lightling and material properties.
336  *
337  * Revision 1.8  1997/12/10 01:19:51  curt
338  * Tweaks for verion 0.15 release.
339  *
340  * Revision 1.7  1997/12/08 22:51:17  curt
341  * Enhanced to handle ccw and cw tri-stripe winding.  This is a temporary
342  * admission of defeat.  I will eventually go back and get all the stripes
343  * wound the same way (ccw).
344  *
345  * Revision 1.6  1997/11/25 19:25:35  curt
346  * Changes to integrate Durk's moon/sun code updates + clean up.
347  *
348  * Revision 1.5  1997/11/15 18:16:39  curt
349  * minor tweaks.
350  *
351  * Revision 1.4  1997/11/14 00:26:49  curt
352  * Transform scenery coordinates earlier in pipeline when scenery is being
353  * created, not when it is being loaded.  Precalculate normals for each node
354  * as average of the normals of each containing polygon so Garoude shading is
355  * now supportable.
356  *
357  * Revision 1.3  1997/10/31 04:49:12  curt
358  * Tweaking vertex orders.
359  *
360  * Revision 1.2  1997/10/30 12:38:45  curt
361  * Working on new scenery subsystem.
362  *
363  * Revision 1.1  1997/10/28 21:14:54  curt
364  * Initial revision.
365  *
366  */