+// Generate a generic ocean tile on the fly
+ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
+ fgFRAGMENT fragment;
+ fragment.init();
+ fragment.tile_ptr = t;
+
+ ssgSimpleState *state = NULL;
+
+ ssgBranch *tile = new ssgBranch () ;
+ tile -> setName ( (char *)path.c_str() ) ;
+
+ // find Ocean material in the properties list
+ if ( ! material_mgr.find( "Ocean", fragment.material_ptr )) {
+ FG_LOG( FG_TERRAIN, FG_ALERT,
+ "Ack! unknown usemtl name = " << "Ocean"
+ << " in " << path );
+ }
+
+ // set the texture width and height values for this
+ // material
+ FGMaterial m = fragment.material_ptr->get_m();
+ double tex_width = m.get_xsize();
+ double tex_height = m.get_ysize();
+
+ // set ssgState
+ state = fragment.material_ptr->get_state();
+
+ // Calculate center point
+ FGBucket b = t->tile_bucket;
+ double clon = b.get_center_lon();
+ double clat = b.get_center_lat();
+ double height = b.get_height();
+ double width = b.get_width();
+
+ Point3D center = fgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
+ t->center = center;
+ fragment.center = center;
+ // cout << "center = " << center << endl;;
+
+ // Caculate corner vertices
+ Point3D geod[4];
+ geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
+ geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
+ geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
+ geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
+
+ Point3D rad[4];
+ int i;
+ for ( i = 0; i < 4; ++i ) {
+ rad[i] = Point3D( geod[i].x() * DEG_TO_RAD, geod[i].y() * DEG_TO_RAD,
+ geod[i].z() );
+ }
+
+ Point3D cart[4], rel[4];
+ t->nodes.clear();
+ for ( i = 0; i < 4; ++i ) {
+ cart[i] = fgGeodToCart(rad[i]);
+ rel[i] = cart[i] - center;
+ t->nodes.push_back( rel[i] );
+ // cout << "corner " << i << " = " << cart[i] << endl;
+ }
+
+ t->ncount = 4;
+
+ // Calculate bounding radius
+ t->bounding_radius = center.distance3D( cart[0] );
+ fragment.bounding_radius = t->bounding_radius;
+ // cout << "bounding radius = " << t->bounding_radius << endl;
+
+ // Calculate normals
+ Point3D normals[4];
+ for ( i = 0; i < 4; ++i ) {
+ normals[i] = cart[i];
+ double length = normals[i].distance3D( Point3D(0.0) );
+ normals[i] /= length;
+ // cout << "normal = " << normals[i] << endl;
+ }
+
+ // Calculate texture coordinates
+ point_list geod_nodes;
+ geod_nodes.clear();
+ for ( i = 0; i < 4; ++i ) {
+ geod_nodes.push_back( geod[i] );
+ }
+ int_list rectangle;
+ rectangle.clear();
+ for ( i = 0; i < 4; ++i ) {
+ rectangle.push_back( i );
+ }
+ point_list texs = calc_tex_coords( b, geod_nodes, rectangle,
+ 1000.0 / tex_width );
+
+ // Build flight gear structure
+ fragment.add_face(0, 1, 2);
+ fragment.add_face(0, 2, 3);
+ t->fragment_list.push_back(fragment);
+
+ // Allocate ssg structure
+ sgVec3 *vtlist = new sgVec3 [ 4 ];
+ t->vec3_ptrs.push_back( vtlist );
+ sgVec3 *vnlist = new sgVec3 [ 4 ];
+ t->vec3_ptrs.push_back( vnlist );
+ sgVec2 *tclist = new sgVec2 [ 4 ];
+ t->vec2_ptrs.push_back( tclist );
+
+ for ( i = 0; i < 4; ++i ) {
+ sgSetVec3( vtlist[i],
+ rel[i].x(), rel[i].y(), rel[i].z() );
+ sgSetVec3( vnlist[i],
+ normals[i].x(), normals[i].y(), normals[i].z() );
+ sgSetVec2( tclist[i], texs[i].x(), texs[i].y() );
+ }
+
+ unsigned short *vindex = new unsigned short [ 4 ];
+ t->index_ptrs.push_back( vindex );
+ unsigned short *tindex = new unsigned short [ 4 ];
+ t->index_ptrs.push_back( tindex );
+ for ( i = 0; i < 4; ++i ) {
+ vindex[i] = i;
+ tindex[i] = i;
+ }
+
+ ssgLeaf *leaf =
+ new ssgVTable ( GL_TRIANGLE_FAN,
+ 4, vindex, vtlist,
+ 4, vindex, vnlist,
+ 4, tindex, tclist,
+ 0, NULL, NULL ) ;
+ leaf->setState( state );
+
+ tile->addKid( leaf );
+ // if ( current_options.get_clouds() ) {
+ // fgGenCloudTile(path, t, tile);
+ // }
+
+ return tile;
+}
+
+
+// Load a .obj file and build the fragment list
+ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {