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 <Include/compiler.h>
44 #include <vector> // STL
45 #include <ctype.h> // isdigit()
47 #include <Debug/logstream.hxx>
48 #include <Misc/fgstream.hxx>
49 #include <Include/fg_constants.h>
50 #include <Main/options.hxx>
51 #include <Math/mat3.h>
52 #include <Math/fg_geodesy.hxx>
53 #include <Math/fg_random.h>
54 #include <Math/point3d.hxx>
55 #include <Math/polar3d.hxx>
56 #include <Misc/stopwatch.hxx>
57 #include <Scenery/tileentry.hxx>
59 #include "materialmgr.hxx"
66 typedef vector < int > int_list;
67 typedef int_list::iterator int_list_iterator;
68 typedef int_list::const_iterator int_point_list_iterator;
71 static double normals[FG_MAX_NODES][3];
72 static double tex_coords[FG_MAX_NODES*3][3];
75 // given three points defining a triangle, calculate the normal
76 static void calc_normal(Point3D p1, Point3D p2,
77 Point3D p3, double normal[3])
82 v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
83 v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
85 MAT3cross_product(normal, v1, v2);
86 MAT3_NORMALIZE_VEC(normal,temp);
88 // fgPrintf( FG_TERRAIN, FG_DEBUG, " Normal = %.2f %.2f %.2f\n",
89 // normal[0], normal[1], normal[2]);
93 #define FG_TEX_CONSTANT 69.0
96 // Calculate texture coordinates for a given point.
97 static Point3D calc_tex_coords(const Point3D& node, const Point3D& ref) {
100 // double tmplon, tmplat;
102 // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
103 // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
105 cp = Point3D( node[0] + ref.x(),
109 pp = fgCartToPolar3d(cp);
111 // tmplon = pp.lon() * RAD_TO_DEG;
112 // tmplat = pp.lat() * RAD_TO_DEG;
113 // cout << tmplon << " " << tmplat << endl;
115 pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
116 pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
118 if ( pp.x() < 0.0 ) {
119 pp.setx( pp.x() + 11.0 );
122 if ( pp.y() < 0.0 ) {
123 pp.sety( pp.y() + 11.0 );
126 // cout << pp << endl;
132 // Generate a generic ocean tile on the fly
133 ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
136 fragment.tile_ptr = t;
138 ssgSimpleState *state = NULL;
140 ssgBranch *tile = new ssgBranch () ;
141 tile -> setName ( (char *)path.c_str() ) ;
143 // find Ocean material in the properties list
144 if ( ! material_mgr.find( "Ocean", fragment.material_ptr )) {
145 FG_LOG( FG_TERRAIN, FG_ALERT,
146 "Ack! unknown usemtl name = " << "Ocean"
150 // set the texture width and height values for this
152 FGMaterial m = fragment.material_ptr->get_m();
153 // double tex_width = m.get_xsize();
154 // double tex_height = m.get_ysize();
157 state = fragment.material_ptr->get_state();
159 // Calculate center point
160 FGBucket b = t->tile_bucket;
161 double clon = b.get_center_lon();
162 double clat = b.get_center_lat();
163 double height = b.get_height();
164 double width = b.get_width();
166 Point3D center = fgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
168 fragment.center = center;
169 // cout << "center = " << center << endl;;
171 // Caculate corner vertices
173 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
174 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
175 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
176 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
179 for ( int i = 0; i < 4; ++i ) {
180 rad[i] = Point3D( geod[i].x() * DEG_TO_RAD, geod[i].y() * DEG_TO_RAD,
184 Point3D cart[4], rel[4];
186 for ( int i = 0; i < 4; ++i ) {
187 cart[i] = fgGeodToCart(rad[i]);
188 rel[i] = cart[i] - center;
189 t->nodes.push_back( rel[i] );
190 // cout << "corner " << i << " = " << cart[i] << endl;
195 // Calculate bounding radius
196 t->bounding_radius = center.distance3D( cart[0] );
197 fragment.bounding_radius = t->bounding_radius;
198 // cout << "bounding radius = " << t->bounding_radius << endl;
202 for ( int i = 0; i < 4; ++i ) {
203 normals[i] = cart[i];
204 double length = normals[i].distance3D( Point3D(0.0) );
205 normals[i] /= length;
206 // cout << "normal = " << normals[i] << endl;
209 // Calculate texture coordinates
211 for ( int i = 0; i < 4; ++i ) {
212 texs[i] = calc_tex_coords( rel[i], center );
213 // cout << "texture coordinate = " << texs[i] << endl;
216 // Build flight gear structure
217 fragment.add_face(0, 1, 2);
218 fragment.add_face(0, 2, 3);
219 t->fragment_list.push_back(fragment);
221 // Build ssg structure
222 t->vtlist = new sgVec3 [ 4 ];
223 t->vnlist = new sgVec3 [ 4 ];
224 t->tclist = new sgVec2 [ 4 ];
226 for ( int i = 0; i < 4; ++i ) {
227 sgSetVec3( t->vtlist[i],
228 rel[i].x(), rel[i].y(), rel[i].z() );
229 sgSetVec3( t->vnlist[i],
230 normals[i].x(), normals[i].y(), normals[i].z() );
231 sgSetVec2( t->tclist[i], texs[i].x(), texs[i].y() );
234 unsigned short *vindex = new unsigned short [ 4 ];
235 unsigned short *tindex = new unsigned short [ 4 ];
236 for ( int i = 0; i < 4; ++i ) {
242 new ssgVTable ( GL_TRIANGLE_FAN,
243 4, vindex, t->vtlist,
244 4, vindex, t->vnlist,
245 4, tindex, t->tclist,
247 leaf->setState( state );
249 tile->addKid( leaf );
255 // Load a .obj file and build the fragment list
256 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
259 double approx_normal[3] /*, normal[3], scale = 0.0 */;
260 // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
261 // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
262 // GLint display_list = 0;
264 bool in_fragment = false, in_faces = false;
265 int vncount, vtcount;
266 int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
268 int last1 = 0, last2 = 0, odd = 0;
272 double tex_width = 1000.0, tex_height = 1000.0;
273 bool shared_done = false;
274 int_list fan_vertices;
275 int_list fan_tex_coords;
277 ssgSimpleState *state = NULL;
279 ssgBranch *tile = new ssgBranch () ;
280 tile -> setName ( (char *)path.c_str() ) ;
282 // Attempt to open "path.gz" or "path"
283 fg_gzifstream in( path );
284 if ( ! in.is_open() ) {
285 FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
286 FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
288 return fgGenTile( path, t );
291 shading = current_options.get_shading();
297 t->bounding_radius = 0.0;
303 // ignore initial comments and blank lines. (priming the pump)
304 // in >> skipcomment;
311 while ( in.get(c) && c != '\0' ) {
314 while ( ! in.eof() ) {
323 if ( in.get( c ) && c == '#' ) {
324 // process a comment line
326 // getline( in, line );
327 // cout << "comment = " << line << endl;
331 if ( token == "gbs" ) {
332 // reference point (center offset)
333 in >> t->center >> t->bounding_radius;
335 // cout << "center = " << center
336 // << " radius = " << t->bounding_radius << endl;
337 } else if ( token == "bs" ) {
338 // reference point (center offset)
339 in >> fragment.center;
340 in >> fragment.bounding_radius;
342 // cout << "center = " << fragment.center
343 // << " radius = " << fragment.bounding_radius << endl;
344 } else if ( token == "usemtl" ) {
345 // material property specification
347 // if first usemtl with shared_done = false, then set
348 // shared_done true and build the ssg shared lists
349 if ( ! shared_done ) {
351 if ( (int)nodes.size() != vncount ) {
352 FG_LOG( FG_TERRAIN, FG_ALERT,
353 "Tile has mismatched nodes and normals: "
359 t->vtlist = new sgVec3 [ nodes.size() ];
360 t->vnlist = new sgVec3 [ vncount ];
361 t->tclist = new sgVec2 [ vtcount ];
363 for ( i = 0; i < (int)nodes.size(); ++i ) {
364 sgSetVec3( t->vtlist[i],
365 nodes[i][0], nodes[i][1], nodes[i][2] );
367 for ( i = 0; i < vncount; ++i ) {
368 sgSetVec3( t->vnlist[i],
373 for ( i = 0; i < vtcount; ++i ) {
374 sgSetVec2( t->tclist[i],
375 tex_coords[i][0], tex_coords[i][1] );
379 // series of individual triangles
384 // this also signals the start of a new fragment
386 // close out the previous structure and start the next
388 // printf("xglEnd(); xglEndList();\n");
391 // fragment.display_list = display_list;
393 // push this fragment onto the tile's object list
394 t->fragment_list.push_back(fragment);
399 // printf("start of fragment (usemtl)\n");
401 // display_list = xglGenLists(1);
402 // xglNewList(display_list, GL_COMPILE);
403 // printf("xglGenLists(); xglNewList();\n");
406 // reset the existing face list
407 // printf("cleaning a fragment with %d faces\n",
408 // fragment.faces.size());
411 // scan the material line
414 fragment.tile_ptr = t;
416 // find this material in the properties list
417 if ( ! material_mgr.find( material, fragment.material_ptr )) {
418 FG_LOG( FG_TERRAIN, FG_ALERT,
419 "Ack! unknown usemtl name = " << material
423 // set the texture width and height values for this
425 FGMaterial m = fragment.material_ptr->get_m();
426 tex_width = m.get_xsize();
427 tex_height = m.get_ysize();
428 state = fragment.material_ptr->get_state();
429 // cout << "(w) = " << tex_width << " (h) = "
430 // << tex_width << endl;
432 // initialize the fragment transformation matrix
434 for ( i = 0; i < 16; i++ ) {
435 fragment.matrix[i] = 0.0;
437 fragment.matrix[0] = fragment.matrix[5] =
438 fragment.matrix[10] = fragment.matrix[15] = 1.0;
441 // unknown comment, just gobble the input untill the
451 // cout << "token = " << token << endl;
453 if ( token == "vn" ) {
455 if ( vncount < FG_MAX_NODES ) {
456 in >> normals[vncount][0]
457 >> normals[vncount][1]
458 >> normals[vncount][2];
461 FG_LOG( FG_TERRAIN, FG_ALERT,
462 "Read too many vertex normals in " << path
463 << " ... dying :-(" );
466 } else if ( token == "vt" ) {
467 // vertex texture coordinate
468 if ( vtcount < FG_MAX_NODES*3 ) {
469 in >> tex_coords[vtcount][0]
470 >> tex_coords[vtcount][1];
473 FG_LOG( FG_TERRAIN, FG_ALERT,
474 "Read too many vertex texture coords in " << path
479 } else if ( token == "v" ) {
481 if ( t->ncount < FG_MAX_NODES ) {
482 /* in >> nodes[t->ncount][0]
483 >> nodes[t->ncount][1]
484 >> nodes[t->ncount][2]; */
486 nodes.push_back(node);
489 FG_LOG( FG_TERRAIN, FG_ALERT,
490 "Read too many nodes in " << path
491 << " ... dying :-(");
494 } else if ( token == "t" ) {
495 // start a new triangle strip
497 n1 = n2 = n3 = n4 = 0;
499 // fgPrintf( FG_TERRAIN, FG_DEBUG,
500 // " new tri strip = %s", line);
501 in >> n1 >> n2 >> n3;
502 fragment.add_face(n1, n2, n3);
504 // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
506 // xglBegin(GL_TRIANGLE_STRIP);
507 // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
513 // Shading model is "GL_SMOOTH" so use precalculated
514 // (averaged) normals
515 // MAT3_SCALE_VEC(normal, normals[n1], scale);
516 // xglNormal3dv(normal);
517 pp = calc_tex_coords(nodes[n1], center);
518 // xglTexCoord2f(pp.lon(), pp.lat());
519 // xglVertex3dv(nodes[n1].get_n());
521 // MAT3_SCALE_VEC(normal, normals[n2], scale);
522 // xglNormal3dv(normal);
523 pp = calc_tex_coords(nodes[n2], center);
524 // xglTexCoord2f(pp.lon(), pp.lat());
525 // xglVertex3dv(nodes[n2].get_n());
527 // MAT3_SCALE_VEC(normal, normals[n3], scale);
528 // xglNormal3dv(normal);
529 pp = calc_tex_coords(nodes[n3], center);
530 // xglTexCoord2f(pp.lon(), pp.lat());
531 // xglVertex3dv(nodes[n3].get_n());
533 // Shading model is "GL_FLAT" so calculate per face
534 // normals on the fly.
536 calc_normal(nodes[n1], nodes[n2],
537 nodes[n3], approx_normal);
539 calc_normal(nodes[n2], nodes[n1],
540 nodes[n3], approx_normal);
542 // MAT3_SCALE_VEC(normal, approx_normal, scale);
543 // xglNormal3dv(normal);
545 pp = calc_tex_coords(nodes[n1], center);
546 // xglTexCoord2f(pp.lon(), pp.lat());
547 // xglVertex3dv(nodes[n1].get_n());
549 pp = calc_tex_coords(nodes[n2], center);
550 // xglTexCoord2f(pp.lon(), pp.lat());
551 // xglVertex3dv(nodes[n2].get_n());
553 pp = calc_tex_coords(nodes[n3], center);
554 // xglTexCoord2f(pp.lon(), pp.lat());
555 // xglVertex3dv(nodes[n3].get_n());
557 // printf("some normals, texcoords, and vertices\n");
563 // There can be three or four values
565 while ( in.get(c) ) {
567 break; // only the one
577 fragment.add_face(n3, n2, n4);
580 // Shading model is "GL_SMOOTH"
581 // MAT3_SCALE_VEC(normal, normals[n4], scale);
583 // Shading model is "GL_FLAT"
584 calc_normal(nodes[n3], nodes[n2], nodes[n4],
586 // MAT3_SCALE_VEC(normal, approx_normal, scale);
588 // xglNormal3dv(normal);
589 pp = calc_tex_coords(nodes[n4], center);
590 // xglTexCoord2f(pp.lon(), pp.lat());
591 // xglVertex3dv(nodes[n4].get_n());
596 // printf("a normal, texcoord, and vertex (4th)\n");
598 } else if ( token == "tf" ) {
600 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new fan");
602 fan_vertices.clear();
603 fan_tex_coords.clear();
605 // xglBegin(GL_TRIANGLE_FAN);
608 fan_vertices.push_back( n1 );
609 // xglNormal3dv(normals[n1]);
610 if ( in.get( c ) && c == '/' ) {
612 fan_tex_coords.push_back( tex );
613 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
614 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
617 pp = calc_tex_coords(nodes[n1], center);
619 // xglTexCoord2f(pp.x(), pp.y());
620 // xglVertex3dv(nodes[n1].get_n());
623 fan_vertices.push_back( n2 );
624 // xglNormal3dv(normals[n2]);
625 if ( in.get( c ) && c == '/' ) {
627 fan_tex_coords.push_back( tex );
628 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
629 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
632 pp = calc_tex_coords(nodes[n2], center);
634 // xglTexCoord2f(pp.x(), pp.y());
635 // xglVertex3dv(nodes[n2].get_n());
637 // read all subsequent numbers until next thing isn't a number
648 if ( ! isdigit(c) || in.eof() ) {
653 fan_vertices.push_back( n3 );
654 // cout << " triangle = "
655 // << n1 << "," << n2 << "," << n3
657 // xglNormal3dv(normals[n3]);
658 if ( in.get( c ) && c == '/' ) {
660 fan_tex_coords.push_back( tex );
661 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
662 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
665 pp = calc_tex_coords(nodes[n3], center);
667 // xglTexCoord2f(pp.x(), pp.y());
668 // xglVertex3dv(nodes[n3].get_n());
670 fragment.add_face(n1, n2, n3);
676 // build the ssg entity
677 unsigned short *vindex =
678 new unsigned short [ fan_vertices.size() ];
679 unsigned short *tindex =
680 new unsigned short [ fan_tex_coords.size() ];
681 for ( i = 0; i < (int)fan_vertices.size(); ++i ) {
682 vindex[i] = fan_vertices[i];
684 for ( i = 0; i < (int)fan_tex_coords.size(); ++i ) {
685 tindex[i] = fan_tex_coords[i];
688 new ssgVTable ( GL_TRIANGLE_FAN,
689 fan_vertices.size(), vindex, t->vtlist,
690 fan_vertices.size(), vindex, t->vnlist,
691 fan_tex_coords.size(), tindex, t->tclist,
693 leaf->setState( state );
695 tile->addKid( leaf );
697 } else if ( token == "f" ) {
701 // xglBegin(GL_TRIANGLES);
702 // printf("xglBegin(triangles)\n");
706 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
707 in >> n1 >> n2 >> n3;
708 fragment.add_face(n1, n2, n3);
710 // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
711 // xglNormal3dv(normals[n1]);
712 pp = calc_tex_coords(nodes[n1], center);
713 // xglTexCoord2f(pp.lon(), pp.lat());
714 // xglVertex3dv(nodes[n1].get_n());
716 // xglNormal3dv(normals[n2]);
717 pp = calc_tex_coords(nodes[n2], center);
718 // xglTexCoord2f(pp.lon(), pp.lat());
719 // xglVertex3dv(nodes[n2].get_n());
721 // xglNormal3dv(normals[n3]);
722 pp = calc_tex_coords(nodes[n3], center);
723 // xglTexCoord2f(pp.lon(), pp.lat());
724 // xglVertex3dv(nodes[n3].get_n());
725 // printf("some normals, texcoords, and vertices (tris)\n");
726 } else if ( token == "q" ) {
727 // continue a triangle strip
730 // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ",
734 // There can be one or two values
736 while ( in.get(c) ) {
738 break; // only the one
747 // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
750 fragment.add_face(last1, last2, n1);
752 fragment.add_face(last2, last1, n1);
756 // Shading model is "GL_SMOOTH"
757 // MAT3_SCALE_VEC(normal, normals[n1], scale);
759 // Shading model is "GL_FLAT"
761 calc_normal(nodes[last1], nodes[last2],
762 nodes[n1], approx_normal);
764 calc_normal(nodes[last2], nodes[last1],
765 nodes[n1], approx_normal);
767 // MAT3_SCALE_VEC(normal, approx_normal, scale);
769 // xglNormal3dv(normal);
771 pp = calc_tex_coords(nodes[n1], center);
772 // xglTexCoord2f(pp.lon(), pp.lat());
773 // xglVertex3dv(nodes[n1].get_n());
774 // printf("a normal, texcoord, and vertex (4th)\n");
781 // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
784 fragment.add_face(last1, last2, n2);
786 fragment.add_face(last2, last1, n2);
790 // Shading model is "GL_SMOOTH"
791 // MAT3_SCALE_VEC(normal, normals[n2], scale);
793 // Shading model is "GL_FLAT"
795 calc_normal(nodes[last1], nodes[last2],
796 nodes[n2], approx_normal);
798 calc_normal(nodes[last2], nodes[last1],
799 nodes[n2], approx_normal);
801 // MAT3_SCALE_VEC(normal, approx_normal, scale);
803 // xglNormal3dv(normal);
805 pp = calc_tex_coords(nodes[n2], center);
806 // xglTexCoord2f(pp.lon(), pp.lat());
807 // xglVertex3dv(nodes[n2].get_n());
808 // printf("a normal, texcoord, and vertex (4th)\n");
815 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in "
816 << path << " = " << token );
819 // eat white space before start of while loop so if we are
820 // done with useful input it is noticed before hand.
830 // close out the previous structure and start the next
833 // printf("xglEnd(); xglEndList();\n");
836 // fragment.display_list = display_list;
838 // push this fragment onto the tile's object list
839 t->fragment_list.push_back(fragment);
843 // Draw normal vectors (for visually verifying normals)
845 xglColor3f(0.0, 0.0, 0.0);
846 for ( i = 0; i < t->ncount; i++ ) {
847 xglVertex3d(nodes[i][0],
850 xglVertex3d(nodes[i][0] + 500*normals[i][0],
851 nodes[i][1] + 500*normals[i][1],
852 nodes[i][2] + 500*normals[i][2]);
860 FG_LOG( FG_TERRAIN, FG_INFO,
861 "Loaded " << path << " in "
862 << stopwatch.elapsedSeconds() << " seconds" );