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