1 /**************************************************************************
2 * obj.c -- routines to handle WaveFront .obj format files.
4 * Written by Curtis Olson, started October 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
36 #include "../constants.h"
38 #include "../Math/fg_geodesy.h"
39 #include "../Math/polar.h"
42 #define MAXNODES 100000
44 float nodes[MAXNODES][3];
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;
53 printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
54 geod[0], geod[1], geod[2]);
56 gc_lon = geod[0]*ARCSEC_TO_RAD;
57 fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
59 printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
60 gc_lat, sl_radius+geod[2]);
62 p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
64 printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z);
70 /* Load a .obj file and generate the GL call list */
71 GLint fgObjLoad(char *path) {
73 static GLfloat color[4] = { 0.5, 0.5, 0.25, 1.0 };
74 struct fgCartesianPoint p1, p2, p3;
77 int first, ncount, n1, n2, n3;
79 if ( (f = fopen(path, "r")) == NULL ) {
80 printf("Cannot open file: %s\n", path);
85 glNewList(area, GL_COMPILE);
87 /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
90 glBegin(GL_TRIANGLE_STRIP);
95 while ( fgets(line, 250, f) != NULL ) {
96 if ( line[0] == '#' ) {
97 /* comment -- ignore */
98 } else if ( line[0] == 'v' ) {
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]);
106 printf("Read too many nodes ... dying :-(\n");
109 } else if ( line[0] == 't' ) {
110 /* start a new triangle strip */
113 /* close out the previous structure and start the next */
116 glBegin(GL_TRIANGLE_STRIP);
119 printf("new tri strip = %s", line);
120 sscanf(line, "t %d %d %d\n", &n1, &n2, &n3);
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 */
129 printf("continued tri strip = %s", line);
130 sscanf(line, "q %d %d\n", &n1, &n2);
131 printf("read %d %d\n", n1, n2);
133 printf("Unknown line in %s = %s\n", path, line);
147 /* Revision 1.1 1997/10/28 21:14:54 curt