]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
Transform scenery coordinates earlier in pipeline when scenery is being
[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];
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
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     /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
67     glColor3fv(color);
68
69     first = 1;
70     ncount = 1;
71     vncount = 1;
72
73     while ( fgets(line, 250, f) != NULL ) {
74         if ( line[0] == '#' ) {
75             /* comment -- ignore */
76         } else if ( strncmp(line, "v ", 2) == 0 ) {
77             /* node (vertex) */
78             if ( ncount < MAXNODES ) {
79                 /* printf("vertex = %s", line); */
80                 sscanf(line, "v %f %f %f\n", 
81                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
82                 if ( ncount == 1 ) {
83                     /* first node becomes the reference point */
84                     ref.x = nodes[ncount][0];
85                     ref.y = nodes[ncount][1];
86                     ref.z = nodes[ncount][2];
87                     scenery.center = ref;
88                 }
89                 ncount++;
90             } else {
91                 printf("Read too many nodes ... dying :-(\n");
92                 exit(-1);
93             }
94         } else if ( strncmp(line, "vn ", 3) == 0 ) {
95             /* vertex normal */
96             if ( vncount < MAXNODES ) {
97                 /* printf("vertex normal = %s", line); */
98                 sscanf(line, "vn %f %f %f\n", 
99                        &normals[vncount][0], &normals[vncount][1], 
100                        &normals[vncount][2]);
101                 vncount++;
102             } else {
103                 printf("Read too many vertex normals ... dying :-(\n");
104                 exit(-1);
105             }
106         } else if ( line[0] == 't' ) {
107             /* start a new triangle strip */
108
109             n1 = n2 = n3 = n4 = 0;
110
111             if ( !first ) {
112                 /* close out the previous structure and start the next */
113                 glEnd();
114             } else {
115                 first = 0;
116             }
117
118             glBegin(GL_TRIANGLE_STRIP);
119
120             printf("new tri strip = %s", line);
121             sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
122
123             printf("(t) = ");
124
125             /* try to get the proper rotation by calculating an
126              * approximate normal and seeing if it is close to the
127              * precalculated normal */
128             v1[0] = nodes[n2][0] - nodes[n1][0];
129             v1[1] = nodes[n2][1] - nodes[n1][1];
130             v1[2] = nodes[n2][2] - nodes[n1][2];
131             v2[0] = nodes[n3][0] - nodes[n1][0];
132             v2[1] = nodes[n3][1] - nodes[n1][1];
133             v2[2] = nodes[n3][2] - nodes[n1][2];
134             MAT3cross_product(approx_normal, v1, v2);
135             MAT3_NORMALIZE_VEC(approx_normal,temp);
136             printf("Approx normal = %.2f %.2f %.2f\n", approx_normal[0], 
137                    approx_normal[1], approx_normal[2]);
138             dot_prod = MAT3_DOT_PRODUCT(normals[n1], approx_normal);
139             printf("Dot product = %.4f\n", dot_prod);
140             /* angle = acos(dot_prod); */
141             /* printf("Normal ANGLE = %.3f rads.\n", angle); */
142             
143             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
144             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
145                        nodes[n1][2] - ref.z);
146
147             if ( dot_prod > 0 ) {
148                 glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
149                 glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
150                            nodes[n2][2] - ref.z);
151
152                 glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
153                 glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
154                            nodes[n3][2] - ref.z);
155             } else {
156                 printf("Reversing\n");
157                 glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
158                 glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
159                            nodes[n3][2] - ref.z);
160
161                 glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
162                 glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
163                            nodes[n2][2] - ref.z);
164             }
165
166             if ( n4 > 0 ) {
167                 glNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);
168                 glVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y, 
169                            nodes[n4][2] - ref.z);
170             }
171         } else if ( line[0] == 'f' ) {
172             /* unoptimized face */
173
174             if ( !first ) {
175                 /* close out the previous structure and start the next */
176                 glEnd();
177             } else {
178                 first = 0;
179             }
180
181             glBegin(GL_TRIANGLES);
182
183             printf("new triangle = %s", line);
184             sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
185
186             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
187             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
188                        nodes[n1][2] - ref.z);
189
190             glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
191             glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
192                        nodes[n2][2] - ref.z);
193
194             glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
195             glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
196                        nodes[n3][2] - ref.z);
197         } else if ( line[0] == 'q' ) {
198             /* continue a triangle strip */
199             n1 = n2 = 0;
200
201             printf("continued tri strip = %s ", line);
202             sscanf(line, "q %d %d\n", &n1, &n2);
203             printf("read %d %d\n", n1, n2);
204
205             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
206             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
207                        nodes[n1][2] - ref.z);
208
209             if ( n2 > 0 ) {
210                 printf(" (cont)\n");
211                 glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
212                 glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
213                            nodes[n2][2] - ref.z);
214             }
215         } else {
216             printf("Unknown line in %s = %s\n", path, line);
217         }
218     }
219
220     glEnd();
221     glEndList();
222
223     fclose(f);
224
225     return(area);
226 }
227
228
229 /* $Log$
230 /* Revision 1.4  1997/11/14 00:26:49  curt
231 /* Transform scenery coordinates earlier in pipeline when scenery is being
232 /* created, not when it is being loaded.  Precalculate normals for each node
233 /* as average of the normals of each containing polygon so Garoude shading is
234 /* now supportable.
235 /*
236  * Revision 1.3  1997/10/31 04:49:12  curt
237  * Tweaking vertex orders.
238  *
239  * Revision 1.2  1997/10/30 12:38:45  curt
240  * Working on new scenery subsystem.
241  *
242  * Revision 1.1  1997/10/28 21:14:54  curt
243  * Initial revision.
244  *
245  */