1 // obj.cxx -- routines to handle "sorta" WaveFront .obj format files.
3 // Written by Curtis Olson, started October 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #ifdef FG_MATH_EXCEPTION_CLASH
35 // #if defined ( __sun__ )
36 // extern "C" void *memmove(void *, const void *, size_t);
37 // extern "C" void *memset(void *, int, size_t);
40 #include <simgear/compiler.h>
44 #include <vector> // STL
45 #include <ctype.h> // isdigit()
47 #include <simgear/constants.h>
48 #include <simgear/debug/logstream.hxx>
49 #include <simgear/math/fg_geodesy.hxx>
50 #include <simgear/math/fg_random.h>
51 #include <simgear/math/point3d.hxx>
52 #include <simgear/math/polar3d.hxx>
53 #include <simgear/misc/fgstream.hxx>
54 #include <simgear/misc/stopwatch.hxx>
55 #include <simgear/misc/texcoord.hxx>
57 #include <Main/options.hxx>
58 #include <Scenery/tileentry.hxx>
67 typedef vector < int > int_list;
68 typedef int_list::iterator int_list_iterator;
69 typedef int_list::const_iterator int_point_list_iterator;
72 static double normals[FG_MAX_NODES][3];
73 static double tex_coords[FG_MAX_NODES*3][3];
77 // given three points defining a triangle, calculate the normal
78 static void calc_normal(Point3D p1, Point3D p2,
79 Point3D p3, sgVec3 normal)
83 v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
84 v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
86 sgVectorProductVec3( normal, v1, v2 );
87 sgNormalizeVec3( normal );
89 // fgPrintf( FG_TERRAIN, FG_DEBUG, " Normal = %.2f %.2f %.2f\n",
90 // normal[0], normal[1], normal[2]);
95 #define FG_TEX_CONSTANT 69.0
97 // Calculate texture coordinates for a given point.
98 static Point3D local_calc_tex_coords(const Point3D& node, const Point3D& ref) {
101 // double tmplon, tmplat;
103 // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
104 // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
106 cp = Point3D( node[0] + ref.x(),
110 pp = fgCartToPolar3d(cp);
112 // tmplon = pp.lon() * RAD_TO_DEG;
113 // tmplat = pp.lat() * RAD_TO_DEG;
114 // cout << tmplon << " " << tmplat << endl;
116 pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
117 pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
119 if ( pp.x() < 0.0 ) {
120 pp.setx( pp.x() + 11.0 );
123 if ( pp.y() < 0.0 ) {
124 pp.sety( pp.y() + 11.0 );
127 // cout << pp << endl;
133 // Generate a generic ocean tile on the fly
134 ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
137 ssgSimpleState *state = NULL;
139 ssgBranch *tile = new ssgBranch () ;
140 tile -> setName ( (char *)path.c_str() ) ;
142 double tex_width = 1000.0;
143 // double tex_height;
145 // find Ocean material in the properties list
146 newmat = material_lib.find( "Ocean" );
147 if ( newmat != NULL ) {
148 // set the texture width and height values for this
150 tex_width = newmat->get_xsize();
151 // tex_height = newmat->get_ysize();
154 state = newmat->get_state();
156 FG_LOG( FG_TERRAIN, FG_ALERT,
157 "Ack! unknown usemtl name = " << "Ocean"
161 // Calculate center point
162 FGBucket b = t->tile_bucket;
163 double clon = b.get_center_lon();
164 double clat = b.get_center_lat();
165 double height = b.get_height();
166 double width = b.get_width();
168 Point3D center = fgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
170 // cout << "center = " << center << endl;;
172 // Caculate corner vertices
174 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
175 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
176 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
177 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
181 for ( i = 0; i < 4; ++i ) {
182 rad[i] = Point3D( geod[i].x() * DEG_TO_RAD, geod[i].y() * DEG_TO_RAD,
186 Point3D cart[4], rel[4];
188 for ( i = 0; i < 4; ++i ) {
189 cart[i] = fgGeodToCart(rad[i]);
190 rel[i] = cart[i] - center;
191 t->nodes.push_back( rel[i] );
192 // cout << "corner " << i << " = " << cart[i] << endl;
197 // Calculate bounding radius
198 t->bounding_radius = center.distance3D( cart[0] );
199 // cout << "bounding radius = " << t->bounding_radius << endl;
203 for ( i = 0; i < 4; ++i ) {
204 normals[i] = cart[i];
205 double length = normals[i].distance3D( Point3D(0.0) );
206 normals[i] /= length;
207 // cout << "normal = " << normals[i] << endl;
210 // Calculate texture coordinates
211 point_list geod_nodes;
213 for ( i = 0; i < 4; ++i ) {
214 geod_nodes.push_back( geod[i] );
218 for ( i = 0; i < 4; ++i ) {
219 rectangle.push_back( i );
221 point_list texs = calc_tex_coords( b, geod_nodes, rectangle,
222 1000.0 / tex_width );
224 // Allocate ssg structure
225 ssgVertexArray *vl = new ssgVertexArray( 4 );
226 ssgNormalArray *nl = new ssgNormalArray( 4 );
227 ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
228 ssgColourArray *cl = new ssgColourArray( 1 );
231 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
234 // sgVec3 *vtlist = new sgVec3 [ 4 ];
235 // t->vec3_ptrs.push_back( vtlist );
236 // sgVec3 *vnlist = new sgVec3 [ 4 ];
237 // t->vec3_ptrs.push_back( vnlist );
238 // sgVec2 *tclist = new sgVec2 [ 4 ];
239 // t->vec2_ptrs.push_back( tclist );
243 for ( i = 0; i < 4; ++i ) {
245 rel[i].x(), rel[i].y(), rel[i].z() );
249 normals[i].x(), normals[i].y(), normals[i].z() );
252 sgSetVec2( tmp2, texs[i].x(), texs[i].y());
257 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
259 leaf->setState( state );
261 tile->addKid( leaf );
262 // if ( current_options.get_clouds() ) {
263 // fgGenCloudTile(path, t, tile);
271 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t, const bool is_base) {
274 // sgVec3 approx_normal;
275 // double normal[3], scale = 0.0;
276 // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
277 // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
278 // GLint display_list = 0;
280 bool in_faces = false;
281 int vncount, vtcount;
282 int n1 = 0, n2 = 0, n3 = 0;
284 // int last1 = 0, last2 = 0;
289 double scenery_version = 0.0;
290 double tex_width = 1000.0, tex_height = 1000.0;
291 bool shared_done = false;
292 int_list fan_vertices;
293 int_list fan_tex_coords;
295 ssgSimpleState *state = NULL;
296 sgVec3 *vtlist, *vnlist;
299 ssgBranch *tile = new ssgBranch () ;
301 tile -> setName ( (char *)path.c_str() ) ;
303 // Attempt to open "path.gz" or "path"
304 fg_gzifstream in( path );
305 if ( ! in.is_open() ) {
306 FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
307 FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
309 return fgGenTile( path, t );
312 shading = current_options.get_shading();
320 t->bounding_radius = 0.0;
327 // ignore initial comments and blank lines. (priming the pump)
328 // in >> skipcomment;
335 while ( in.get(c) && c != '\0' ) {
338 while ( ! in.eof() ) {
347 if ( in.get( c ) && c == '#' ) {
348 // process a comment line
350 // getline( in, line );
351 // cout << "comment = " << line << endl;
355 if ( token == "Version" ) {
356 // read scenery versions number
357 in >> scenery_version;
358 // cout << "scenery_version = " << scenery_version << endl;
359 } else if ( token == "gbs" ) {
360 // reference point (center offset)
362 in >> t->center >> t->bounding_radius;
366 in >> junk1 >> junk2;
369 // cout << "center = " << center
370 // << " radius = " << t->bounding_radius << endl;
371 } else if ( token == "bs" ) {
372 // reference point (center offset)
376 in >> junk1 >> junk2;
377 } else if ( token == "usemtl" ) {
378 // material property specification
380 // if first usemtl with shared_done = false, then set
381 // shared_done true and build the ssg shared lists
382 if ( ! shared_done ) {
384 if ( (int)nodes.size() != vncount ) {
385 FG_LOG( FG_TERRAIN, FG_ALERT,
386 "Tile has mismatched nodes and normals: "
392 vtlist = new sgVec3 [ nodes.size() ];
393 t->vec3_ptrs.push_back( vtlist );
394 vnlist = new sgVec3 [ vncount ];
395 t->vec3_ptrs.push_back( vnlist );
396 tclist = new sgVec2 [ vtcount ];
397 t->vec2_ptrs.push_back( tclist );
399 for ( i = 0; i < (int)nodes.size(); ++i ) {
400 sgSetVec3( vtlist[i],
401 nodes[i][0], nodes[i][1], nodes[i][2] );
403 for ( i = 0; i < vncount; ++i ) {
404 sgSetVec3( vnlist[i],
409 for ( i = 0; i < vtcount; ++i ) {
410 sgSetVec2( tclist[i],
416 // display_list = xglGenLists(1);
417 // xglNewList(display_list, GL_COMPILE);
418 // printf("xglGenLists(); xglNewList();\n");
421 // scan the material line
425 // find this material in the properties list
427 newmat = material_lib.find( material );
428 if ( newmat == NULL ) {
429 // see if this is an on the fly texture
431 int pos = file.rfind( "/" );
432 file = file.substr( 0, pos );
433 cout << "current file = " << file << endl;
436 cout << "current file = " << file << endl;
437 if ( ! material_lib.add_item( file ) ) {
438 FG_LOG( FG_TERRAIN, FG_ALERT,
439 "Ack! unknown usemtl name = " << material
442 // locate our newly created material
443 newmat = material_lib.find( material );
444 if ( newmat == NULL ) {
445 FG_LOG( FG_TERRAIN, FG_ALERT,
446 "Ack! bad on the fly materia create = "
447 << material << " in " << path );
452 if ( newmat != NULL ) {
453 // set the texture width and height values for this
455 tex_width = newmat->get_xsize();
456 tex_height = newmat->get_ysize();
457 state = newmat->get_state();
458 // cout << "(w) = " << tex_width << " (h) = "
459 // << tex_width << endl;
462 // unknown comment, just gobble the input untill the
472 // cout << "token = " << token << endl;
474 if ( token == "vn" ) {
476 if ( vncount < FG_MAX_NODES ) {
477 in >> normals[vncount][0]
478 >> normals[vncount][1]
479 >> normals[vncount][2];
482 FG_LOG( FG_TERRAIN, FG_ALERT,
483 "Read too many vertex normals in " << path
484 << " ... dying :-(" );
487 } else if ( token == "vt" ) {
488 // vertex texture coordinate
489 if ( vtcount < FG_MAX_NODES*3 ) {
490 in >> tex_coords[vtcount][0]
491 >> tex_coords[vtcount][1];
494 FG_LOG( FG_TERRAIN, FG_ALERT,
495 "Read too many vertex texture coords in " << path
500 } else if ( token == "v" ) {
502 if ( t->ncount < FG_MAX_NODES ) {
503 /* in >> nodes[t->ncount][0]
504 >> nodes[t->ncount][1]
505 >> nodes[t->ncount][2]; */
507 nodes.push_back(node);
512 FG_LOG( FG_TERRAIN, FG_ALERT,
513 "Read too many nodes in " << path
514 << " ... dying :-(");
517 } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
518 // triangle fan, strip, or individual face
519 // FG_LOG( FG_TERRAIN, FG_INFO, "new fan or strip");
521 fan_vertices.clear();
522 fan_tex_coords.clear();
525 // xglBegin(GL_TRIANGLE_FAN);
528 fan_vertices.push_back( n1 );
529 // xglNormal3dv(normals[n1]);
530 if ( in.get( c ) && c == '/' ) {
532 fan_tex_coords.push_back( tex );
533 if ( scenery_version >= 0.4 ) {
534 if ( tex_width > 0 ) {
535 tclist[tex][0] *= (1000.0 / tex_width);
537 if ( tex_height > 0 ) {
538 tclist[tex][1] *= (1000.0 / tex_height);
541 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
542 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
545 pp = local_calc_tex_coords(nodes[n1], center);
547 // xglTexCoord2f(pp.x(), pp.y());
548 // xglVertex3dv(nodes[n1].get_n());
551 fan_vertices.push_back( n2 );
552 // xglNormal3dv(normals[n2]);
553 if ( in.get( c ) && c == '/' ) {
555 fan_tex_coords.push_back( tex );
556 if ( scenery_version >= 0.4 ) {
557 if ( tex_width > 0 ) {
558 tclist[tex][0] *= (1000.0 / tex_width);
560 if ( tex_height > 0 ) {
561 tclist[tex][1] *= (1000.0 / tex_height);
564 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
565 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
568 pp = local_calc_tex_coords(nodes[n2], center);
570 // xglTexCoord2f(pp.x(), pp.y());
571 // xglVertex3dv(nodes[n2].get_n());
573 // read all subsequent numbers until next thing isn't a number
584 if ( ! isdigit(c) || in.eof() ) {
589 fan_vertices.push_back( n3 );
590 // cout << " triangle = "
591 // << n1 << "," << n2 << "," << n3
593 // xglNormal3dv(normals[n3]);
594 if ( in.get( c ) && c == '/' ) {
596 fan_tex_coords.push_back( tex );
597 if ( scenery_version >= 0.4 ) {
598 if ( tex_width > 0 ) {
599 tclist[tex][0] *= (1000.0 / tex_width);
601 if ( tex_height > 0 ) {
602 tclist[tex][1] *= (1000.0 / tex_height);
605 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
606 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
609 pp = local_calc_tex_coords(nodes[n3], center);
611 // xglTexCoord2f(pp.x(), pp.y());
612 // xglVertex3dv(nodes[n3].get_n());
614 if ( (token == "tf") || (token == "f") ) {
627 // build the ssg entity
628 int size = (int)fan_vertices.size();
629 ssgVertexArray *vl = new ssgVertexArray( size );
630 ssgNormalArray *nl = new ssgNormalArray( size );
631 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
632 ssgColourArray *cl = new ssgColourArray( 1 );
635 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
640 for ( i = 0; i < size; ++i ) {
641 sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
644 sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
647 sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
651 ssgLeaf *leaf = NULL;
652 if ( token == "tf" ) {
655 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
656 } else if ( token == "ts" ) {
659 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
660 } else if ( token == "f" ) {
663 new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
665 // leaf->makeDList();
666 leaf->setState( state );
668 tile->addKid( leaf );
670 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in "
671 << path << " = " << token );
674 // eat white space before start of while loop so if we are
675 // done with useful input it is noticed before hand.
689 FG_LOG( FG_TERRAIN, FG_DEBUG,
690 "Loaded " << path << " in "
691 << stopwatch.elapsedSeconds() << " seconds" );
693 // Generate a cloud layer above the tiles
694 // if ( current_options.get_clouds() ) {
695 // fgGenCloudTile(path, t, tile);