]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
Changes to integrate Durk's moon/sun code updates + clean up.
[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             glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
148             glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
149                        nodes[n2][2] - ref.z);
150
151             glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
152             glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
153                        nodes[n3][2] - ref.z);
154
155             if ( n4 > 0 ) {
156                 glNormal3d(normals[n4][0], normals[n4][1], normals[n4][2]);
157                 glVertex3d(nodes[n4][0] - ref.x, nodes[n4][1] - ref.y, 
158                            nodes[n4][2] - ref.z);
159             }
160         } else if ( line[0] == 'f' ) {
161             /* unoptimized face */
162
163             if ( !first ) {
164                 /* close out the previous structure and start the next */
165                 glEnd();
166             } else {
167                 first = 0;
168             }
169
170             glBegin(GL_TRIANGLES);
171
172             /* printf("new triangle = %s", line);*/
173             sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
174
175             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
176             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
177                        nodes[n1][2] - ref.z);
178
179             glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
180             glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
181                        nodes[n2][2] - ref.z);
182
183             glNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
184             glVertex3d(nodes[n3][0] - ref.x, nodes[n3][1] - ref.y, 
185                        nodes[n3][2] - ref.z);
186         } else if ( line[0] == 'q' ) {
187             /* continue a triangle strip */
188             n1 = n2 = 0;
189
190             /* printf("continued tri strip = %s ", line); */
191             sscanf(line, "q %d %d\n", &n1, &n2);
192             /* printf("read %d %d\n", n1, n2); */
193
194             glNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
195             glVertex3d(nodes[n1][0] - ref.x, nodes[n1][1] - ref.y, 
196                        nodes[n1][2] - ref.z);
197
198             if ( n2 > 0 ) {
199                 /* printf(" (cont)\n"); */
200                 glNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
201                 glVertex3d(nodes[n2][0] - ref.x, nodes[n2][1] - ref.y, 
202                            nodes[n2][2] - ref.z);
203             }
204         } else {
205             printf("Unknown line in %s = %s\n", path, line);
206         }
207     }
208
209     glEnd();
210     glEndList();
211
212     fclose(f);
213
214     return(area);
215 }
216
217
218 /* $Log$
219 /* Revision 1.6  1997/11/25 19:25:35  curt
220 /* Changes to integrate Durk's moon/sun code updates + clean up.
221 /*
222  * Revision 1.5  1997/11/15 18:16:39  curt
223  * minor tweaks.
224  *
225  * Revision 1.4  1997/11/14 00:26:49  curt
226  * Transform scenery coordinates earlier in pipeline when scenery is being
227  * created, not when it is being loaded.  Precalculate normals for each node
228  * as average of the normals of each containing polygon so Garoude shading is
229  * now supportable.
230  *
231  * Revision 1.3  1997/10/31 04:49:12  curt
232  * Tweaking vertex orders.
233  *
234  * Revision 1.2  1997/10/30 12:38:45  curt
235  * Working on new scenery subsystem.
236  *
237  * Revision 1.1  1997/10/28 21:14:54  curt
238  * Initial revision.
239  *
240  */