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 SG_MATH_EXCEPTION_CLASH
35 #include <simgear/compiler.h>
36 #include <simgear/io/sg_binobj.hxx>
40 #include <vector> // STL
41 #include <ctype.h> // isdigit()
43 #include <simgear/constants.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/math/point3d.hxx>
46 #include <simgear/math/polar3d.hxx>
47 #include <simgear/math/sg_geodesy.hxx>
48 #include <simgear/math/sg_random.h>
49 #include <simgear/misc/sgstream.hxx>
50 #include <simgear/misc/stopwatch.hxx>
51 #include <simgear/misc/texcoord.hxx>
53 #include <Main/globals.hxx>
54 #include <Main/fg_props.hxx>
55 #include <Time/light.hxx>
56 #include <Scenery/tileentry.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 runway_lights_predraw (ssgEntity * e)
77 // Turn on lights only at night
78 float sun_angle = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
79 return int(sun_angle > 90.0);
83 #define FG_TEX_CONSTANT 69.0
85 // Calculate texture coordinates for a given point.
86 static Point3D local_calc_tex_coords(const Point3D& node, const Point3D& ref) {
89 // double tmplon, tmplat;
91 // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
92 // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
94 cp = Point3D( node[0] + ref.x(),
98 pp = sgCartToPolar3d(cp);
100 // tmplon = pp.lon() * SGD_RADIANS_TO_DEGREES;
101 // tmplat = pp.lat() * SGD_RADIANS_TO_DEGREES;
102 // cout << tmplon << " " << tmplat << endl;
104 pp.setx( fmod(SGD_RADIANS_TO_DEGREES * FG_TEX_CONSTANT * pp.x(), 11.0) );
105 pp.sety( fmod(SGD_RADIANS_TO_DEGREES * FG_TEX_CONSTANT * pp.y(), 11.0) );
107 if ( pp.x() < 0.0 ) {
108 pp.setx( pp.x() + 11.0 );
111 if ( pp.y() < 0.0 ) {
112 pp.sety( pp.y() + 11.0 );
115 // cout << pp << endl;
121 // Generate an ocean tile
122 bool fgGenTile( const string& path, SGBucket b,
124 double *bounding_radius,
125 ssgBranch* geometry )
129 ssgSimpleState *state = NULL;
131 geometry -> setName ( (char *)path.c_str() ) ;
133 double tex_width = 1000.0;
134 // double tex_height;
136 // find Ocean material in the properties list
137 newmat = material_lib.find( "Ocean" );
138 if ( newmat != NULL ) {
139 // set the texture width and height values for this
141 tex_width = newmat->get_xsize();
142 // tex_height = newmat->get_ysize();
145 state = newmat->get_state();
147 SG_LOG( SG_TERRAIN, SG_ALERT,
148 "Ack! unknown usemtl name = " << "Ocean"
152 // Calculate center point
153 double clon = b.get_center_lon();
154 double clat = b.get_center_lat();
155 double height = b.get_height();
156 double width = b.get_width();
158 *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
159 clat*SGD_DEGREES_TO_RADIANS,
161 // cout << "center = " << center << endl;;
163 // Caculate corner vertices
165 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
166 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
167 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
168 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
172 for ( i = 0; i < 4; ++i ) {
173 rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
174 geod[i].y() * SGD_DEGREES_TO_RADIANS,
178 Point3D cart[4], rel[4];
179 for ( i = 0; i < 4; ++i ) {
180 cart[i] = sgGeodToCart(rad[i]);
181 rel[i] = cart[i] - *center;
182 // cout << "corner " << i << " = " << cart[i] << endl;
185 // Calculate bounding radius
186 *bounding_radius = center->distance3D( cart[0] );
187 // cout << "bounding radius = " << t->bounding_radius << endl;
191 for ( i = 0; i < 4; ++i ) {
192 double length = cart[i].distance3D( Point3D(0.0) );
193 normals[i] = cart[i] / length;
194 // cout << "normal = " << normals[i] << endl;
197 // Calculate texture coordinates
198 point_list geod_nodes;
202 for ( i = 0; i < 4; ++i ) {
203 geod_nodes.push_back( geod[i] );
204 rectangle.push_back( i );
206 point_list texs = calc_tex_coords( b, geod_nodes, rectangle,
207 1000.0 / tex_width );
209 // Allocate ssg structure
210 ssgVertexArray *vl = new ssgVertexArray( 4 );
211 ssgNormalArray *nl = new ssgNormalArray( 4 );
212 ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
213 ssgColourArray *cl = new ssgColourArray( 1 );
216 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
219 // sgVec3 *vtlist = new sgVec3 [ 4 ];
220 // t->vec3_ptrs.push_back( vtlist );
221 // sgVec3 *vnlist = new sgVec3 [ 4 ];
222 // t->vec3_ptrs.push_back( vnlist );
223 // sgVec2 *tclist = new sgVec2 [ 4 ];
224 // t->vec2_ptrs.push_back( tclist );
228 for ( i = 0; i < 4; ++i ) {
230 rel[i].x(), rel[i].y(), rel[i].z() );
234 normals[i].x(), normals[i].y(), normals[i].z() );
237 sgSetVec2( tmp2, texs[i].x(), texs[i].y());
242 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
244 leaf->setState( state );
246 geometry->addKid( leaf );
252 static void random_pt_inside_tri( float *res,
253 float *n1, float *n2, float *n3 )
257 double a = sg_random();
258 double b = sg_random();
263 double c = 1 - a - b;
265 sgScaleVec3( p1, n1, a );
266 sgScaleVec3( p2, n2, b );
267 sgScaleVec3( p3, n3, c );
269 sgAddVec3( res, p1, p2 );
270 sgAddVec3( res, p3 );
274 static void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights,
276 int num = leaf->getNumTriangles();
278 short int n1, n2, n3;
282 // generate a repeatable random seed
283 p1 = leaf->getVertex( 0 );
284 unsigned int seed = (unsigned int)p1[0];
287 for ( int i = 0; i < num; ++i ) {
288 leaf->getTriangle( i, &n1, &n2, &n3 );
289 p1 = leaf->getVertex(n1);
290 p2 = leaf->getVertex(n2);
291 p3 = leaf->getVertex(n3);
292 double area = sgTriArea( p1, p2, p3 );
293 double num = area / factor;
295 // generate a light point for each unit of area
296 while ( num > 1.0 ) {
297 random_pt_inside_tri( result, p1, p2, p3 );
298 lights->add( result );
301 // for partial units of area, use a zombie door method to
302 // create the proper random chance of a light being created
305 if ( sg_random() <= num ) {
306 // a zombie made it through our door
307 random_pt_inside_tri( result, p1, p2, p3 );
308 lights->add( result );
317 gen_random_surface_objects (ssgLeaf *leaf,
321 const string &material_name)
323 FGNewMat * mat = material_lib.find(material_name);
325 SG_LOG(SG_INPUT, SG_ALERT, "Unknown material " << material_name);
329 int num = leaf->getNumTriangles();
330 float hdg_deg = 0.0; // do something here later
333 // The object will be aligned for the north pole. This code
334 // calculates a matrix to rotate it to for the surface of the
335 // earth in the current location.
336 sgVec3 obj_right, obj_up;
337 sgSetVec3(obj_right, 0.0, 1.0, 0.0); // Y axis
338 sgSetVec3(obj_up, 0.0, 0.0, 1.0); // Z axis
339 sgMat4 ROT_lon, ROT_lat, ROT_hdg;
340 sgMakeRotMat4(ROT_lon, lon_deg, obj_up);
341 sgMakeRotMat4(ROT_lat, 90 - lat_deg, obj_right);
342 sgMakeRotMat4(ROT_hdg, hdg_deg, obj_up);
344 sgCopyMat4(ROT, ROT_hdg);
345 sgPostMultMat4(ROT, ROT_lat);
346 sgPostMultMat4(ROT, ROT_lon);
349 short int n1, n2, n3;
353 // generate a repeatable random seed
354 p1 = leaf->getVertex( 0 );
355 unsigned int seed = (unsigned int)p1[0];
358 int num_objects = mat->get_object_count();
359 for ( int i = 0; i < num; ++i ) {
360 leaf->getTriangle( i, &n1, &n2, &n3 );
361 p1 = leaf->getVertex(n1);
362 p2 = leaf->getVertex(n2);
363 p3 = leaf->getVertex(n3);
364 double area = sgTriArea( p1, p2, p3 );
365 // Set up a single center point for LOD
368 (p1[0] + p2[0] + p3[0]) / 3.0,
369 (p1[1] + p2[1] + p3[1]) / 3.0,
370 (p1[2] + p2[2] + p3[2]) / 3.0);
371 ssgTransform * location = new ssgTransform;
373 sgMakeTransMat4(TRANS, center);
374 location->setTransform(TRANS);
376 for (int j = 0; j < num_objects; j++) {
377 double num = area / mat->get_object_coverage(j);
378 float ranges[] = {0, mat->get_object_group_lod(j)};
379 ssgRangeSelector * lod = new ssgRangeSelector;
380 lod->setRanges(ranges, 2);
381 lod->addKid(location);
384 // place an object each unit of area
385 while ( num > 1.0 ) {
386 random_pt_inside_tri( result, p1, p2, p3 );
387 sgSubVec3(result, center);
389 sgMakeTransMat4(OBJ_pos, result);
390 sgCopyMat4(OBJ, ROT);
391 sgPostMultMat4(OBJ, OBJ_pos);
392 ssgTransform * pos = new ssgTransform;
393 pos->setTransform(OBJ);
394 pos->addKid(mat->get_object(j));
395 location->addKid(pos);
398 // for partial units of area, use a zombie door method to
399 // create the proper random chance of an object being created
402 if ( sg_random() <= num ) {
403 // a zombie made it through our door
404 random_pt_inside_tri( result, p1, p2, p3 );
405 sgSubVec3(result, center);
407 sgMakeTransMat4(OBJ_pos, result);
408 sgCopyMat4(OBJ, ROT);
409 sgPostMultMat4(OBJ, OBJ_pos);
410 ssgTransform * pos = new ssgTransform;
411 pos->setTransform(OBJ);
412 pos->addKid(mat->get_object(j));
413 location->addKid(pos);
422 // Load an Ascii obj file
423 ssgBranch *fgAsciiObjLoad( const string& path, FGTileEntry *t,
424 ssgVertexArray *lights, const bool is_base)
426 FGNewMat *newmat = NULL;
430 // sgVec3 approx_normal;
431 // double normal[3], scale = 0.0;
432 // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
433 // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
434 // GLint display_list = 0;
436 bool in_faces = false;
437 int vncount, vtcount;
438 int n1 = 0, n2 = 0, n3 = 0;
440 // int last1 = 0, last2 = 0;
445 double scenery_version = 0.0;
446 double tex_width = 1000.0, tex_height = 1000.0;
447 bool shared_done = false;
448 int_list fan_vertices;
449 int_list fan_tex_coords;
451 ssgSimpleState *state = NULL;
452 sgVec3 *vtlist, *vnlist;
455 ssgBranch *tile = new ssgBranch () ;
457 tile -> setName ( (char *)path.c_str() ) ;
459 // Attempt to open "path.gz" or "path"
460 sg_gzifstream in( path );
461 if ( ! in.is_open() ) {
462 SG_LOG( SG_TERRAIN, SG_DEBUG, "Cannot open file: " << path );
463 SG_LOG( SG_TERRAIN, SG_DEBUG, "default to ocean tile: " << path );
470 shading = fgGetBool("/sim/rendering/shading");
478 t->bounding_radius = 0.0;
482 // StopWatch stopwatch;
483 // stopwatch.start();
485 // ignore initial comments and blank lines. (priming the pump)
486 // in >> skipcomment;
493 while ( in.get(c) && c != '\0' ) {
496 while ( ! in.eof() ) {
501 if ( in.get( c ) && c == '#' ) {
502 // process a comment line
504 // getline( in, line );
505 // cout << "comment = " << line << endl;
509 if ( token == "Version" ) {
510 // read scenery versions number
511 in >> scenery_version;
512 // cout << "scenery_version = " << scenery_version << endl;
513 if ( scenery_version > 0.4 ) {
514 SG_LOG( SG_TERRAIN, SG_ALERT,
515 "\nYou are attempting to load a tile format that\n"
516 << "is newer than this version of flightgear can\n"
517 << "handle. You should upgrade your copy of\n"
518 << "FlightGear to the newest version. For\n"
519 << "details, please see:\n"
520 << "\n http://www.flightgear.org\n" );
523 } else if ( token == "gbs" ) {
524 // reference point (center offset)
526 in >> t->center >> t->bounding_radius;
530 in >> junk1 >> junk2;
533 // cout << "center = " << center
534 // << " radius = " << t->bounding_radius << endl;
535 } else if ( token == "bs" ) {
536 // reference point (center offset)
540 in >> junk1 >> junk2;
541 } else if ( token == "usemtl" ) {
542 // material property specification
544 // if first usemtl with shared_done = false, then set
545 // shared_done true and build the ssg shared lists
546 if ( ! shared_done ) {
548 if ( (int)nodes.size() != vncount ) {
549 SG_LOG( SG_TERRAIN, SG_ALERT,
550 "Tile has mismatched nodes = " << nodes.size()
551 << " and normals = " << vncount << " : "
557 vtlist = new sgVec3 [ nodes.size() ];
558 t->vec3_ptrs.push_back( vtlist );
559 vnlist = new sgVec3 [ vncount ];
560 t->vec3_ptrs.push_back( vnlist );
561 tclist = new sgVec2 [ vtcount ];
562 t->vec2_ptrs.push_back( tclist );
564 for ( i = 0; i < (int)nodes.size(); ++i ) {
565 sgSetVec3( vtlist[i],
566 nodes[i][0], nodes[i][1], nodes[i][2] );
568 for ( i = 0; i < vncount; ++i ) {
569 sgSetVec3( vnlist[i],
574 for ( i = 0; i < vtcount; ++i ) {
575 sgSetVec2( tclist[i],
581 // display_list = xglGenLists(1);
582 // xglNewList(display_list, GL_COMPILE);
583 // printf("xglGenLists(); xglNewList();\n");
586 // scan the material line
589 // find this material in the properties list
591 newmat = material_lib.find( material );
592 if ( newmat == NULL ) {
593 // see if this is an on the fly texture
595 int pos = file.rfind( "/" );
596 file = file.substr( 0, pos );
597 // cout << "current file = " << file << endl;
600 // cout << "current file = " << file << endl;
601 if ( ! material_lib.add_item( file ) ) {
602 SG_LOG( SG_TERRAIN, SG_ALERT,
603 "Ack! unknown usemtl name = " << material
606 // locate our newly created material
607 newmat = material_lib.find( material );
608 if ( newmat == NULL ) {
609 SG_LOG( SG_TERRAIN, SG_ALERT,
610 "Ack! bad on the fly materia create = "
611 << material << " in " << path );
616 if ( newmat != NULL ) {
617 // set the texture width and height values for this
619 tex_width = newmat->get_xsize();
620 tex_height = newmat->get_ysize();
621 state = newmat->get_state();
622 coverage = newmat->get_light_coverage();
623 // cout << "(w) = " << tex_width << " (h) = "
624 // << tex_width << endl;
629 // unknown comment, just gobble the input until the
639 // cout << "token = " << token << endl;
641 if ( token == "vn" ) {
643 if ( vncount < FG_MAX_NODES ) {
644 in >> normals[vncount][0]
645 >> normals[vncount][1]
646 >> normals[vncount][2];
649 SG_LOG( SG_TERRAIN, SG_ALERT,
650 "Read too many vertex normals in " << path
651 << " ... dying :-(" );
654 } else if ( token == "vt" ) {
655 // vertex texture coordinate
656 if ( vtcount < FG_MAX_NODES*3 ) {
657 in >> tex_coords[vtcount][0]
658 >> tex_coords[vtcount][1];
661 SG_LOG( SG_TERRAIN, SG_ALERT,
662 "Read too many vertex texture coords in " << path
667 } else if ( token == "v" ) {
669 if ( t->ncount < FG_MAX_NODES ) {
670 /* in >> nodes[t->ncount][0]
671 >> nodes[t->ncount][1]
672 >> nodes[t->ncount][2]; */
674 nodes.push_back(node);
679 SG_LOG( SG_TERRAIN, SG_ALERT,
680 "Read too many nodes in " << path
681 << " ... dying :-(");
684 } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
685 // triangle fan, strip, or individual face
686 // SG_LOG( SG_TERRAIN, SG_INFO, "new fan or strip");
688 fan_vertices.clear();
689 fan_tex_coords.clear();
692 // xglBegin(GL_TRIANGLE_FAN);
695 fan_vertices.push_back( n1 );
696 // xglNormal3dv(normals[n1]);
697 if ( in.get( c ) && c == '/' ) {
699 fan_tex_coords.push_back( tex );
700 if ( scenery_version >= 0.4 ) {
701 if ( tex_width > 0 ) {
702 tclist[tex][0] *= (1000.0 / tex_width);
704 if ( tex_height > 0 ) {
705 tclist[tex][1] *= (1000.0 / tex_height);
708 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
709 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
712 pp = local_calc_tex_coords(nodes[n1], center);
714 // xglTexCoord2f(pp.x(), pp.y());
715 // xglVertex3dv(nodes[n1].get_n());
718 fan_vertices.push_back( n2 );
719 // xglNormal3dv(normals[n2]);
720 if ( in.get( c ) && c == '/' ) {
722 fan_tex_coords.push_back( tex );
723 if ( scenery_version >= 0.4 ) {
724 if ( tex_width > 0 ) {
725 tclist[tex][0] *= (1000.0 / tex_width);
727 if ( tex_height > 0 ) {
728 tclist[tex][1] *= (1000.0 / tex_height);
731 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
732 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
735 pp = local_calc_tex_coords(nodes[n2], center);
737 // xglTexCoord2f(pp.x(), pp.y());
738 // xglVertex3dv(nodes[n2].get_n());
740 // read all subsequent numbers until next thing isn't a number
747 if ( ! isdigit(c) || in.eof() ) {
752 fan_vertices.push_back( n3 );
753 // cout << " triangle = "
754 // << n1 << "," << n2 << "," << n3
756 // xglNormal3dv(normals[n3]);
757 if ( in.get( c ) && c == '/' ) {
759 fan_tex_coords.push_back( tex );
760 if ( scenery_version >= 0.4 ) {
761 if ( tex_width > 0 ) {
762 tclist[tex][0] *= (1000.0 / tex_width);
764 if ( tex_height > 0 ) {
765 tclist[tex][1] *= (1000.0 / tex_height);
768 pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
769 pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
772 pp = local_calc_tex_coords(nodes[n3], center);
774 // xglTexCoord2f(pp.x(), pp.y());
775 // xglVertex3dv(nodes[n3].get_n());
777 if ( (token == "tf") || (token == "f") ) {
790 // build the ssg entity
791 int size = (int)fan_vertices.size();
792 ssgVertexArray *vl = new ssgVertexArray( size );
793 ssgNormalArray *nl = new ssgNormalArray( size );
794 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
795 ssgColourArray *cl = new ssgColourArray( 1 );
798 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
803 for ( i = 0; i < size; ++i ) {
804 sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
807 sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
810 sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
814 ssgLeaf *leaf = NULL;
815 if ( token == "tf" ) {
818 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
819 } else if ( token == "ts" ) {
822 new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
823 } else if ( token == "f" ) {
826 new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
828 // leaf->makeDList();
829 leaf->setState( state );
831 tile->addKid( leaf );
834 if ( coverage > 0.0 ) {
835 if ( coverage < 10000.0 ) {
836 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
837 << coverage << ", pushing up to 10000");
840 gen_random_surface_points(leaf, lights, coverage);
844 SG_LOG( SG_TERRAIN, SG_WARN, "Unknown token in "
845 << path << " = " << token );
848 // eat white space before start of while loop so if we are
849 // done with useful input it is noticed before hand.
859 // SG_LOG( SG_TERRAIN, SG_DEBUG,
860 // "Loaded " << path << " in "
861 // << stopwatch.elapsedSeconds() << " seconds" );
867 ssgLeaf *gen_leaf( const string& path,
868 const GLenum ty, const string& material,
869 const point_list& nodes, const point_list& normals,
870 const point_list& texcoords,
871 const int_list node_index,
872 const int_list normal_index,
873 const int_list& tex_index,
874 const bool calc_lights, ssgVertexArray *lights )
876 double tex_width = 1000.0, tex_height = 1000.0;
877 ssgSimpleState *state = NULL;
880 FGNewMat *newmat = material_lib.find( material );
881 if ( newmat == NULL ) {
882 // see if this is an on the fly texture
884 int pos = file.rfind( "/" );
885 file = file.substr( 0, pos );
886 // cout << "current file = " << file << endl;
889 // cout << "current file = " << file << endl;
890 if ( ! material_lib.add_item( file ) ) {
891 SG_LOG( SG_TERRAIN, SG_ALERT,
892 "Ack! unknown usemtl name = " << material
895 // locate our newly created material
896 newmat = material_lib.find( material );
897 if ( newmat == NULL ) {
898 SG_LOG( SG_TERRAIN, SG_ALERT,
899 "Ack! bad on the fly material create = "
900 << material << " in " << path );
905 if ( newmat != NULL ) {
906 // set the texture width and height values for this
908 tex_width = newmat->get_xsize();
909 tex_height = newmat->get_ysize();
910 state = newmat->get_state();
911 coverage = newmat->get_light_coverage();
912 // cout << "(w) = " << tex_width << " (h) = "
913 // << tex_width << endl;
924 int size = node_index.size();
926 SG_LOG( SG_TERRAIN, SG_ALERT, "Woh! node list size < 1" );
929 ssgVertexArray *vl = new ssgVertexArray( size );
931 for ( i = 0; i < size; ++i ) {
932 node = nodes[ node_index[i] ];
933 sgSetVec3( tmp3, node[0], node[1], node[2] );
939 ssgNormalArray *nl = new ssgNormalArray( size );
940 if ( normal_index.size() ) {
941 // object file specifies normal indices (i.e. normal indices
943 for ( i = 0; i < size; ++i ) {
944 normal = normals[ normal_index[i] ];
945 sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
949 // use implied normal indices. normal index = vertex index.
950 for ( i = 0; i < size; ++i ) {
951 normal = normals[ node_index[i] ];
952 sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
958 ssgColourArray *cl = new ssgColourArray( 1 );
959 sgSetVec4( tmp4, 1.0, 1.0, 1.0, 1.0 );
962 // texture coordinates
963 size = tex_index.size();
965 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
967 texcoord = texcoords[ tex_index[0] ];
968 sgSetVec2( tmp2, texcoord[0], texcoord[1] );
969 sgSetVec2( tmp2, texcoord[0], texcoord[1] );
970 if ( tex_width > 0 ) {
971 tmp2[0] *= (1000.0 / tex_width);
973 if ( tex_height > 0 ) {
974 tmp2[1] *= (1000.0 / tex_height);
977 } else if ( size > 1 ) {
978 for ( i = 0; i < size; ++i ) {
979 texcoord = texcoords[ tex_index[i] ];
980 sgSetVec2( tmp2, texcoord[0], texcoord[1] );
981 if ( tex_width > 0 ) {
982 tmp2[0] *= (1000.0 / tex_width);
984 if ( tex_height > 0 ) {
985 tmp2[1] *= (1000.0 / tex_height);
991 ssgLeaf *leaf = new ssgVtxTable ( ty, vl, nl, tl, cl );
993 // lookup the state record
995 leaf->setState( state );
998 if ( coverage > 0.0 ) {
999 if ( coverage < 10000.0 ) {
1000 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
1001 << coverage << ", pushing up to 10000");
1004 gen_random_surface_points(leaf, lights, coverage);
1012 // Load an Binary obj file
1013 bool fgBinObjLoad( const string& path, const bool is_base,
1015 double *bounding_radius,
1016 ssgBranch* geometry,
1017 ssgBranch* rwy_lights,
1018 ssgVertexArray *ground_lights )
1021 bool use_dynamic_objects =
1022 fgGetBool("/sim/rendering/dynamic-objects", false);
1024 if ( ! obj.read_bin( path ) ) {
1028 geometry->setName( (char *)path.c_str() );
1030 double geod_lon = 0.0, geod_lat = 0.0, geod_alt = 0.0,
1031 geod_sl_radius = 0.0;
1033 // reference point (center offset/bounding sphere)
1034 *center = obj.get_gbs_center();
1035 *bounding_radius = obj.get_gbs_radius();
1037 // Calculate the geodetic centre of
1038 // the tile, for aligning automatic
1040 Point3D geoc = sgCartToPolar3d(*center);
1041 geod_lon = geoc.lon();
1042 sgGeocToGeod(geoc.lat(), geoc.radius(),
1043 &geod_lat, &geod_alt, &geod_sl_radius);
1044 geod_lon *= SGD_RADIANS_TO_DEGREES;
1045 geod_lat *= SGD_RADIANS_TO_DEGREES;
1048 point_list nodes = obj.get_wgs84_nodes();
1049 point_list colors = obj.get_colors();
1050 point_list normals = obj.get_normals();
1051 point_list texcoords = obj.get_texcoords();
1053 string material, tmp_mat;
1054 int_list vertex_index;
1055 int_list normal_index;
1059 bool is_lighting = false;
1062 string_list pt_materials = obj.get_pt_materials();
1063 group_list pts_v = obj.get_pts_v();
1064 group_list pts_n = obj.get_pts_n();
1065 for ( i = 0; i < (int)pts_v.size(); ++i ) {
1066 // cout << "pts_v.size() = " << pts_v.size() << endl;
1067 tmp_mat = pt_materials[i];
1068 if ( tmp_mat.substr(0, 3) == "RWY" ) {
1069 material = "LIGHTS";
1074 vertex_index = pts_v[i];
1075 normal_index = pts_n[i];
1077 ssgLeaf *leaf = gen_leaf( path, GL_POINTS, material,
1078 nodes, normals, texcoords,
1079 vertex_index, normal_index, tex_index,
1080 false, ground_lights );
1082 if ( is_lighting ) {
1083 float ranges[] = { 0, 12000 };
1084 leaf->setCallback(SSG_CALLBACK_PREDRAW, runway_lights_predraw);
1085 ssgRangeSelector * lod = new ssgRangeSelector;
1086 lod->setRanges(ranges, 2);
1088 rwy_lights->addKid(lod);
1090 geometry->addKid( leaf );
1094 // generate triangles
1095 string_list tri_materials = obj.get_tri_materials();
1096 group_list tris_v = obj.get_tris_v();
1097 group_list tris_n = obj.get_tris_n();
1098 group_list tris_tc = obj.get_tris_tc();
1099 for ( i = 0; i < (int)tris_v.size(); ++i ) {
1100 material = tri_materials[i];
1101 vertex_index = tris_v[i];
1102 normal_index = tris_n[i];
1103 tex_index = tris_tc[i];
1104 ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLES, material,
1105 nodes, normals, texcoords,
1106 vertex_index, normal_index, tex_index,
1107 is_base, ground_lights );
1109 if (use_dynamic_objects)
1110 gen_random_surface_objects(leaf, geometry, geod_lon, geod_lat,
1112 geometry->addKid( leaf );
1116 string_list strip_materials = obj.get_strip_materials();
1117 group_list strips_v = obj.get_strips_v();
1118 group_list strips_n = obj.get_strips_n();
1119 group_list strips_tc = obj.get_strips_tc();
1120 for ( i = 0; i < (int)strips_v.size(); ++i ) {
1121 material = strip_materials[i];
1122 vertex_index = strips_v[i];
1123 normal_index = strips_n[i];
1124 tex_index = strips_tc[i];
1125 ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, material,
1126 nodes, normals, texcoords,
1127 vertex_index, normal_index, tex_index,
1128 is_base, ground_lights );
1130 if (use_dynamic_objects)
1131 gen_random_surface_objects(leaf, geometry, geod_lon, geod_lat,
1133 geometry->addKid( leaf );
1137 string_list fan_materials = obj.get_fan_materials();
1138 group_list fans_v = obj.get_fans_v();
1139 group_list fans_n = obj.get_fans_n();
1140 group_list fans_tc = obj.get_fans_tc();
1141 for ( i = 0; i < (int)fans_v.size(); ++i ) {
1142 material = fan_materials[i];
1143 vertex_index = fans_v[i];
1144 normal_index = fans_n[i];
1145 tex_index = fans_tc[i];
1146 ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_FAN, material,
1147 nodes, normals, texcoords,
1148 vertex_index, normal_index, tex_index,
1149 is_base, ground_lights );
1150 if (use_dynamic_objects)
1151 gen_random_surface_objects(leaf, geometry, geod_lon, geod_lat,
1153 geometry->addKid( leaf );