]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
11b12155ee887733e9d839c2626f741441cced31
[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
35 #include "obj.h"
36 #include "scenery.h"
37
38 #include "../Math/mat3.h"
39
40
41
42 #define MAXNODES 100000
43
44 float nodes[MAXNODES][3];
45 float normals[MAXNODES][3];
46
47
48 /* Load a .obj file and generate the GL call list */
49 GLint fgObjLoad(char *path) {
50     char line[256], winding[256];
51     double v1[3], v2[3], approx_normal[3], dot_prod, temp;
52     struct fgCartesianPoint ref;
53     GLint area;
54     FILE *f;
55     int first, ncount, vncount, n1, n2, n3, n4;
56     int i;
57
58     if ( (f = fopen(path, "r")) == NULL ) {
59         printf("Cannot open file: %s\n", path);
60         exit(-1);
61     }
62
63     area = glGenLists(1);
64     glNewList(area, GL_COMPILE);
65
66     first = 1;
67     ncount = 1;
68     vncount = 1;
69
70     while ( fgets(line, 250, f) != NULL ) {
71         if ( line[0] == '#' ) {
72             /* comment -- ignore */
73         } else if ( strncmp(line, "v ", 2) == 0 ) {
74             /* node (vertex) */
75             if ( ncount < MAXNODES ) {
76                 /* printf("vertex = %s", line); */
77                 sscanf(line, "v %f %f %f\n", 
78                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
79                 if ( ncount == 1 ) {
80                     /* first node becomes the reference point */
81                     ref.x = nodes[ncount][0];
82                     ref.y = nodes[ncount][1];
83                     ref.z = nodes[ncount][2];
84                     scenery.center = ref;
85                 }
86                 ncount++;
87             } else {
88                 printf("Read too many nodes ... dying :-(\n");
89                 exit(-1);
90             }
91         } else if ( strncmp(line, "vn ", 3) == 0 ) {
92             /* vertex normal */
93             if ( vncount < MAXNODES ) {
94                 /* printf("vertex normal = %s", line); */
95                 sscanf(line, "vn %f %f %f\n", 
96                        &normals[vncount][0], &normals[vncount][1], 
97                        &normals[vncount][2]);
98                 vncount++;
99             } else {
100                 printf("Read too many vertex normals ... dying :-(\n");
101                 exit(-1);
102             }
103         } else if ( strncmp(line, "winding ", 8) == 0 ) {
104             sscanf(line+8, "%s", winding);
105             printf("WINDING = %s\n", winding);
106
107             /* can't call glFrontFace() between glBegin() & glEnd() */
108             glEnd();
109             first = 1;
110
111             if ( strcmp(winding, "cw") == 0 ) {
112                 glFrontFace( GL_CW );
113             } else {
114                 glFrontFace ( GL_CCW );
115             }
116         } else if ( line[0] == 't' ) {
117             /* start a new triangle strip */
118
119             n1 = n2 = n3 = n4 = 0;
120
121             if ( !first ) {
122                 /* close out the previous structure and start the next */
123                 glEnd();
124             } else {
125                 first = 0;
126             }
127
128             /* printf("new tri strip = %s", line); */
129             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
130
131             /* printf("(t) = "); */
132
133             /* try to get the proper rotation by calculating an
134              * approximate normal and seeing if it is close to the
135              * precalculated normal */
136             /*v1[0] = nodes[n2][0] - nodes[n1][0];
137             v1[1] = nodes[n2][1] - nodes[n1][1];
138             v1[2] = nodes[n2][2] - nodes[n1][2];
139             v2[0] = nodes[n3][0] - nodes[n1][0];
140             v2[1] = nodes[n3][1] - nodes[n1][1];
141             v2[2] = nodes[n3][2] - nodes[n1][2];
142             MAT3cross_product(approx_normal, v1, v2);
143             MAT3_NORMALIZE_VEC(approx_normal,temp);
144             printf("Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
145                    approx_normal[1], approx_normal[2]);
146             dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
147             printf("Dot product = %.4f\n", dot_prod); */
148             /* angle = acos(dot_prod); */
149             /* printf("Normal ANGLE = %.3f rads.\n", angle); */
150
151             /* if ( dot_prod < -0.5 ) {
152                 glFrontFace( GL_CW );
153             } else {
154                 glFrontFace( GL_CCW );
155             } */
156
157             glBegin(GL_TRIANGLE_STRIP);
158
159             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
160             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
161                        nodes[n1][2] - ref.z);
162
163             glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
164             glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
165                        nodes[n2][2] - ref.z);
166
167             glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
168             glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
169                        nodes[n3][2] - ref.z);
170
171             if ( n4 > 0 ) {
172                 glNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);
173                 glVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y, 
174                            nodes[n4][2] - ref.z);
175             }
176         } else if ( line[0] == 'f' ) {
177             /* unoptimized face */
178
179             if ( !first ) {
180                 /* close out the previous structure and start the next */
181                 glEnd();
182             } else {
183                 first = 0;
184             }
185
186             glBegin(GL_TRIANGLES);
187
188             /* printf("new triangle = %s", line);*/
189             sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
190
191             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
192             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
193                        nodes[n1][2] - ref.z);
194
195             glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
196             glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
197                        nodes[n2][2] - ref.z);
198
199             glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
200             glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
201                        nodes[n3][2] - ref.z);
202         } else if ( line[0] == 'q' ) {
203             /* continue a triangle strip */
204             n1 = n2 = 0;
205
206             /* printf("continued tri strip = %s ", line); */
207             sscanf(line, "q %d %d\n", &n1, &n2);
208             /* printf("read %d %d\n", n1, n2); */
209
210             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
211             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
212                        nodes[n1][2] - ref.z);
213
214             if ( n2 > 0 ) {
215                 /* printf(" (cont)\n"); */
216                 glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
217                 glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
218                            nodes[n2][2] - ref.z);
219             }
220         } else {
221             printf("Unknown line in %s = %s\n", path, line);
222         }
223     }
224
225     glEnd();
226
227     /* Draw normal vectors (for visually verifying normals)*/
228     /*
229     glBegin(GL_LINES);
230     glColor3f(0.0, 0.0, 0.0);
231     for ( i = 0; i < ncount; i++ ) {
232         glVertex3d(nodes[i][0] - ref.x,
233                    nodes[i][1] - ref.y,
234                    nodes[i][2] - ref.z);
235         glVertex3d(nodes[i][0] - ref.x + 500*normals[i][0],
236                    nodes[i][1] - ref.y + 500*normals[i][1],
237                    nodes[i][2] - ref.z + 500*normals[i][2]);
238     } 
239     glEnd();
240     */
241
242     glEndList();
243
244     fclose(f);
245
246     return(area);
247 }
248
249
250 /* $Log$
251 /* Revision 1.10  1997/12/12 21:41:28  curt
252 /* More light/material property tweaking ... still a ways off.
253 /*
254  * Revision 1.9  1997/12/12 19:52:57  curt
255  * Working on lightling and material properties.
256  *
257  * Revision 1.8  1997/12/10 01:19:51  curt
258  * Tweaks for verion 0.15 release.
259  *
260  * Revision 1.7  1997/12/08 22:51:17  curt
261  * Enhanced to handle ccw and cw tri-stripe winding.  This is a temporary
262  * admission of defeat.  I will eventually go back and get all the stripes
263  * wound the same way (ccw).
264  *
265  * Revision 1.6  1997/11/25 19:25:35  curt
266  * Changes to integrate Durk's moon/sun code updates + clean up.
267  *
268  * Revision 1.5  1997/11/15 18:16:39  curt
269  * minor tweaks.
270  *
271  * Revision 1.4  1997/11/14 00:26:49  curt
272  * Transform scenery coordinates earlier in pipeline when scenery is being
273  * created, not when it is being loaded.  Precalculate normals for each node
274  * as average of the normals of each containing polygon so Garoude shading is
275  * now supportable.
276  *
277  * Revision 1.3  1997/10/31 04:49:12  curt
278  * Tweaking vertex orders.
279  *
280  * Revision 1.2  1997/10/30 12:38:45  curt
281  * Working on new scenery subsystem.
282  *
283  * Revision 1.1  1997/10/28 21:14:54  curt
284  * Initial revision.
285  *
286  */