]> git.mxchange.org Git - flightgear.git/blob - Scenery/obj.c
Initial revision.
[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 <GL/glut.h>
33
34 #include "obj.h"
35
36 #include "../constants.h"
37 #include "../types.h"
38 #include "../Math/fg_geodesy.h"
39 #include "../Math/polar.h"
40
41
42 #define MAXNODES 100000
43
44 float nodes[MAXNODES][3];
45
46
47 /* convert a geodetic point lon(arcsec), lat(arcsec), elev(meter) to
48  * a cartesian point */
49 struct fgCartesianPoint geod_to_cart(float geod[3]) {
50     struct fgCartesianPoint p;
51     double gc_lon, gc_lat, sl_radius;
52
53     printf("A geodetic point is (%.2f, %.2f, %.2f)\n", 
54            geod[0], geod[1], geod[2]);
55
56     gc_lon = geod[0]*ARCSEC_TO_RAD;
57     fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
58
59     printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon, 
60            gc_lat, sl_radius+geod[2]);
61
62     p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
63     
64     printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z);
65
66     return(p);
67 }
68
69
70 /* Load a .obj file and generate the GL call list */
71 GLint fgObjLoad(char *path) {
72     char line[256];
73     static GLfloat color[4] = { 0.5, 0.5, 0.25, 1.0 };
74     struct fgCartesianPoint p1, p2, p3;
75     GLint area;
76     FILE *f;
77     int first, ncount, n1, n2, n3;
78
79     if ( (f = fopen(path, "r")) == NULL ) {
80         printf("Cannot open file: %s\n", path);
81         exit(-1);
82     }
83
84     area = glGenLists(1);
85     glNewList(area, GL_COMPILE);
86
87     /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
88     glColor3fv(color);
89
90     glBegin(GL_TRIANGLE_STRIP);
91
92     first = 1;
93     ncount = 1;
94
95     while ( fgets(line, 250, f) != NULL ) {
96         if ( line[0] == '#' ) {
97             /* comment -- ignore */
98         } else if ( line[0] == 'v' ) {
99             /* node (vertex) */
100             if ( ncount < MAXNODES ) {
101                 printf("vertex = %s", line);
102                 sscanf(line, "v %f %f %f\n", 
103                        &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
104                 ncount++;
105             } else {
106                 printf("Read too many nodes ... dying :-(\n");
107                 exit(-1);
108             }
109         } else if ( line[0] == 't' ) {
110             /* start a new triangle strip */
111
112             if ( !first ) {
113                 /* close out the previous structure and start the next */
114                 first = 0;
115                 glEnd();
116                 glBegin(GL_TRIANGLE_STRIP);
117             }
118
119             printf("new tri strip = %s", line);
120             sscanf(line, "t %d %d %d\n", &n1, &n2, &n3);
121
122             p1 = geod_to_cart(nodes[n1]);
123             p2 = geod_to_cart(nodes[n2]);
124             p3 = geod_to_cart(nodes[n3]);
125         } else if ( line[0] == 'q' ) {
126             /* continue a triangle strip */
127             n1 = n2 = 0;
128
129             printf("continued tri strip = %s", line);
130             sscanf(line, "q %d %d\n", &n1, &n2);
131             printf("read %d %d\n", n1, n2);
132         } else {
133             printf("Unknown line in %s = %s\n", path, line);
134         }
135     }
136
137     glEnd();
138     glEndList();
139
140     fclose(f);
141
142     return(area);
143 }
144
145
146 /* $Log$
147 /* Revision 1.1  1997/10/28 21:14:54  curt
148 /* Initial revision.
149 /*
150  */