+ssgLeaf *gen_leaf( const string& path,
+ const GLenum ty, const string& material,
+ const point_list& nodes, const point_list& normals,
+ const point_list& texcoords,
+ const int_list node_index,
+ const int_list& tex_index,
+ const bool calc_lights, ssgVertexArray *lights )
+{
+ double tex_width = 1000.0, tex_height = 1000.0;
+ ssgSimpleState *state = NULL;
+ float coverage = -1;
+
+ FGNewMat *newmat = material_lib.find( material );
+ if ( newmat == NULL ) {
+ // see if this is an on the fly texture
+ string file = path;
+ int pos = file.rfind( "/" );
+ file = file.substr( 0, pos );
+ cout << "current file = " << file << endl;
+ file += "/";
+ file += material;
+ cout << "current file = " << file << endl;
+ if ( ! material_lib.add_item( file ) ) {
+ SG_LOG( SG_TERRAIN, SG_ALERT,
+ "Ack! unknown usemtl name = " << material
+ << " in " << path );
+ } else {
+ // locate our newly created material
+ newmat = material_lib.find( material );
+ if ( newmat == NULL ) {
+ SG_LOG( SG_TERRAIN, SG_ALERT,
+ "Ack! bad on the fly material create = "
+ << material << " in " << path );
+ }
+ }
+ }
+
+ if ( newmat != NULL ) {
+ // set the texture width and height values for this
+ // material
+ tex_width = newmat->get_xsize();
+ tex_height = newmat->get_ysize();
+ state = newmat->get_state();
+ coverage = newmat->get_light_coverage();
+ // cout << "(w) = " << tex_width << " (h) = "
+ // << tex_width << endl;
+ } else {
+ coverage = -1;
+ }
+
+ // cout << "before list allocs" << endl;
+
+ int size = node_index.size();
+
+ if ( size < 1 ) {
+ SG_LOG( SG_TERRAIN, SG_ALERT, "Woh! list size < 1" );
+ exit(-1);
+ }
+
+ // cout << "before vl, size = " << size << endl;
+ ssgVertexArray *vl = new ssgVertexArray( size );
+ // cout << "before nl" << endl;
+ ssgNormalArray *nl = new ssgNormalArray( size );
+ // cout << "before tl" << endl;
+ ssgTexCoordArray *tl = new ssgTexCoordArray( size );
+ // cout << "before cl" << endl;
+ ssgColourArray *cl = new ssgColourArray( 1 );
+
+ sgVec4 color;
+ sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
+ cl->add( color );
+
+ sgVec2 tmp2;
+ sgVec3 tmp3;
+ int i;
+ for ( i = 0; i < size; ++i ) {
+ Point3D node = nodes[ node_index[i] ];
+ sgSetVec3( tmp3, node[0], node[1], node[2] );
+ vl -> add( tmp3 );
+
+ Point3D normal = normals[ node_index[i] ];
+ sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
+ nl -> add( tmp3 );
+
+ Point3D texcoord = texcoords[ tex_index[i] ];
+ sgSetVec2( tmp2, texcoord[0], texcoord[1] );
+ if ( tex_width > 0 ) {
+ tmp2[0] *= (1000.0 / tex_width);
+ }
+ if ( tex_height > 0 ) {
+ tmp2[1] *= (1000.0 / tex_height);
+ }
+ tl -> add( tmp2 );
+ }
+
+ // cout << "before leaf create" << endl;
+ ssgLeaf *leaf = new ssgVtxTable ( ty, vl, nl, tl, cl );
+ // cout << "after leaf create" << endl;
+
+ // lookup the state record
+ // cout << "looking up material = " << endl;
+ // cout << material << endl;
+ // cout << "'" << endl;
+
+ leaf->setState( state );
+
+ if ( calc_lights ) {
+ if ( coverage > 0.0 ) {
+ if ( coverage < 10000.0 ) {
+ SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
+ << coverage << ", pushing up to 10000");
+ coverage = 10000;
+ }
+ gen_random_surface_points(leaf, lights, coverage);
+ }
+ }
+
+ return leaf;
+}
+
+
+// Load an Binary obj file
+ssgBranch *fgBinObjLoad( const string& path, FGTileEntry *t,
+ ssgVertexArray *lights, const bool is_base)
+{
+ int i;
+
+ SGBinObject obj;
+ bool result = obj.read_bin( path );
+
+ if ( !result ) {
+ return NULL;
+ }
+
+ // cout << "fans size = " << obj.get_fans_v().size()
+ // << " fan_mats size = " << obj.get_fan_materials().size() << endl;
+
+ ssgBranch *object = new ssgBranch();
+ object->setName( (char *)path.c_str() );
+
+ if ( is_base && t != NULL ) {
+ // reference point (center offset/bounding sphere)
+ t->center = obj.get_gbs_center();
+ t->bounding_radius = obj.get_gbs_radius();
+ }
+
+ point_list nodes = obj.get_wgs84_nodes();
+ point_list normals = obj.get_normals();
+ point_list texcoords = obj.get_texcoords();
+
+ string material;
+ int_list vertex_index;
+ int_list tex_index;
+
+ // generate triangles
+ string_list tri_materials = obj.get_tri_materials();
+ group_list tris_v = obj.get_tris_v();
+ group_list tris_tc = obj.get_tris_tc();
+ for ( i = 0; i < (int)tris_v.size(); ++i ) {
+ material = tri_materials[i];
+ vertex_index = tris_v[i];
+ tex_index = tris_tc[i];
+ ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLES, material,
+ nodes, normals, texcoords,
+ vertex_index, tex_index,
+ is_base, lights );
+
+ object->addKid( leaf );
+ }
+
+ // generate strips
+ string_list strip_materials = obj.get_strip_materials();
+ group_list strips_v = obj.get_strips_v();
+ group_list strips_tc = obj.get_strips_tc();
+ for ( i = 0; i < (int)strips_v.size(); ++i ) {
+ material = strip_materials[i];
+ vertex_index = strips_v[i];
+ tex_index = strips_tc[i];
+ ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, material,
+ nodes, normals, texcoords,
+ vertex_index, tex_index,
+ is_base, lights );
+
+ object->addKid( leaf );
+ }
+
+ // generate fans
+ string_list fan_materials = obj.get_fan_materials();
+ group_list fans_v = obj.get_fans_v();
+ group_list fans_tc = obj.get_fans_tc();
+ for ( i = 0; i < (int)fans_v.size(); ++i ) {
+ material = fan_materials[i];
+ vertex_index = fans_v[i];
+ tex_index = fans_tc[i];
+ ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_FAN, material,
+ nodes, normals, texcoords,
+ vertex_index, tex_index,
+ is_base, lights );
+
+ object->addKid( leaf );
+ }
+
+ return object;
+}