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