1 // obj.cxx -- routines to handle loading scenery and building the plib
4 // Written by Curtis Olson, started October 1997.
6 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
35 #include <osg/StateSet>
37 #include <osg/Geometry>
41 #include <simgear/bucket/newbucket.hxx>
42 #include <simgear/io/sg_binobj.hxx>
43 #include <simgear/math/sg_geodesy.hxx>
44 #include <simgear/math/sg_types.hxx>
45 #include <simgear/misc/texcoord.hxx>
46 #include <simgear/scene/material/mat.hxx>
47 #include <simgear/scene/material/matlib.hxx>
48 #include <simgear/scene/tgdb/leaf.hxx>
49 #include <simgear/scene/tgdb/pt_lights.hxx>
50 #include <simgear/scene/tgdb/userdata.hxx>
63 // Generate an ocean tile
64 bool SGGenTile( const string& path, SGBucket b,
65 Point3D *center, double *bounding_radius,
66 SGMaterialLib *matlib, osg::Group* group )
68 osg::StateSet *state = 0;
70 double tex_width = 1000.0;
73 // find Ocean material in the properties list
74 SGMaterial *mat = matlib->find( "Ocean" );
76 // set the texture width and height values for this
78 tex_width = mat->get_xsize();
79 // tex_height = newmat->get_ysize();
82 state = mat->get_state();
84 SG_LOG( SG_TERRAIN, SG_ALERT,
85 "Ack! unknown usemtl name = " << "Ocean"
89 // Calculate center point
90 double clon = b.get_center_lon();
91 double clat = b.get_center_lat();
92 double height = b.get_height();
93 double width = b.get_width();
95 *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
96 clat*SGD_DEGREES_TO_RADIANS,
98 // cout << "center = " << center << endl;;
100 // Caculate corner vertices
102 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
103 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
104 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
105 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
109 for ( i = 0; i < 4; ++i ) {
110 rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
111 geod[i].y() * SGD_DEGREES_TO_RADIANS,
115 Point3D cart[4], rel[4];
116 for ( i = 0; i < 4; ++i ) {
117 cart[i] = sgGeodToCart(rad[i]);
118 rel[i] = cart[i] - *center;
119 // cout << "corner " << i << " = " << cart[i] << endl;
122 // Calculate bounding radius
123 *bounding_radius = center->distance3D( cart[0] );
124 // cout << "bounding radius = " << t->bounding_radius << endl;
128 for ( i = 0; i < 4; ++i ) {
129 double length = cart[i].distance3D( Point3D(0.0) );
130 normals[i] = cart[i] / length;
131 // cout << "normal = " << normals[i] << endl;
134 // Calculate texture coordinates
135 point_list geod_nodes;
137 geod_nodes.reserve(4);
140 rectangle.reserve(4);
141 for ( i = 0; i < 4; ++i ) {
142 geod_nodes.push_back( geod[i] );
143 rectangle.push_back( i );
145 point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle,
146 1000.0 / tex_width );
148 // Allocate ssg structure
149 osg::Vec3Array *vl = new osg::Vec3Array;
150 osg::Vec3Array *nl = new osg::Vec3Array;
151 osg::Vec2Array *tl = new osg::Vec2Array;
153 for ( i = 0; i < 4; ++i ) {
154 vl->push_back(osg::Vec3(rel[i].x(), rel[i].y(), rel[i].z()));
155 nl->push_back(osg::Vec3(normals[i].x(), normals[i].y(), normals[i].z()));
156 tl->push_back(osg::Vec2(texs[i].x(), texs[i].y()));
159 osg::Vec4Array* cl = new osg::Vec4Array;
160 cl->push_back(osg::Vec4(1, 1, 1, 1));
162 osg::Geometry* geometry = new osg::Geometry;
163 geometry->setVertexArray(vl);
164 geometry->setNormalArray(nl);
165 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
166 geometry->setColorArray(cl);
167 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
168 geometry->setTexCoordArray(0, tl);
169 geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_FAN, 0, vl->size()));
170 osg::Geode* geode = new osg::Geode;
171 geode->setName(path);
172 geode->addDrawable(geometry);
173 geode->setStateSet(state);
175 group->addChild(geode);
182 * SSG callback for an in-range leaf of randomly-placed objects.
184 * This pretraversal callback is attached to a branch that is
185 * traversed only when a leaf is in range. If the leaf is not
186 * currently prepared to be populated with randomly-placed objects,
187 * this callback will prepare it (actual population is handled by
188 * the tri_in_range_callback for individual triangles).
190 * @param entity The entity to which the callback is attached (not used).
191 * @param mask The entity's traversal mask (not used).
192 * @return Always 1, to allow traversal and culling to continue.
195 // leaf_in_range_callback (ssgEntity * entity, int mask)
197 // SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
199 // if (!data->is_filled_in) {
200 // // Iterate through all the triangles
201 // // and populate them.
202 // int num_tris = data->leaf->getNumTriangles();
203 // for ( int i = 0; i < num_tris; ++i ) {
204 // data->setup_triangle(i);
206 // data->is_filled_in = true;
213 * SSG callback for an out-of-range leaf of randomly-placed objects.
215 * This pretraversal callback is attached to a branch that is
216 * traversed only when a leaf is out of range. If the leaf is
217 * currently prepared to be populated with randomly-placed objects (or
218 * is actually populated), the objects will be removed.
220 * @param entity The entity to which the callback is attached (not used).
221 * @param mask The entity's traversal mask (not used).
222 * @return Always 0, to prevent any further traversal or culling.
225 // leaf_out_of_range_callback (ssgEntity * entity, int mask)
227 // SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
228 // if (data->is_filled_in) {
229 // data->branch->removeAllKids();
230 // data->is_filled_in = false;
237 * Randomly place objects on a surface.
239 * The leaf node provides the geometry of the surface, while the
240 * material provides the objects and placement density. Latitude
241 * and longitude are required so that the objects can be rotated
242 * to the world-up vector. This function does not actually add
243 * any objects; instead, it attaches an ssgRangeSelector to the
244 * branch with callbacks to generate the objects when needed.
246 * @param leaf The surface where the objects should be placed.
247 * @param branch The branch that will hold the randomly-placed objects.
248 * @param center The center of the leaf in FlightGear coordinates.
249 * @param material_name The name of the surface's material.
252 gen_random_surface_objects (osg::Node *leaf,
259 // If the surface has no triangles, return
261 int num_tris = leaf->getNumTriangles();
265 // If the material has no randomly-placed
266 // objects, return now.
267 if (mat->get_object_group_count() < 1)
270 // Calculate the geodetic centre of
271 // the tile, for aligning automatic
273 double xyz[3], lon_rad, lat_rad, alt_m;
274 xyz[0] = center->x(); xyz[1] = center->y(); xyz[2] = center->z();
275 sgCartToGeod(xyz, &lat_rad, &lon_rad, &alt_m);
278 // max random object range: 20000m
279 osg::LOD * lod = new osg::LOD;
280 branch->addChild(lod);
282 // Create the in-range and out-of-range
284 osg::Group * in_range = new osg::Group;
285 // osg::Group * out_of_range = new osg::Group;
286 lod->addChild(in_range, 0, 20000 /*OSGFIXME hardcoded visibility ???*/);
287 // lod->addChild(out_of_range, 20000, 1e30);
289 SGLeafUserData * data = new SGLeafUserData;
290 data->is_filled_in = false;
293 data->branch = in_range;
294 data->sin_lat = sin(lat_rad);
295 data->cos_lat = cos(lat_rad);
296 data->sin_lon = sin(lon_rad);
297 data->cos_lon = cos(lon_rad);
299 in_range->setUserData(data);
300 // OSGFIXME: implement random objects to be loaded when in sight
301 // in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
303 // OSGFIXME: implement deletion of tiles that are no longer used
304 // out_of_range->setUserData(data);
305 // out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
306 // leaf_out_of_range_callback);
308 // ->addChild(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
314 ////////////////////////////////////////////////////////////////////////
316 ////////////////////////////////////////////////////////////////////////
318 // Load an Binary obj file
319 bool SGBinObjLoad( const string& path, const bool is_base,
321 double *bounding_radius,
322 SGMaterialLib *matlib,
323 bool use_random_objects,
324 osg::Group *geometry,
325 osg::Group *vasi_lights,
326 osg::Group *rwy_lights,
327 osg::Group *taxi_lights,
328 osg::Vec3Array *ground_lights )
332 if ( ! obj.read_bin( path ) ) {
336 osg::Group *local_terrain = new osg::Group;
337 local_terrain->setName( "LocalTerrain" );
338 geometry->addChild( local_terrain );
340 geometry->setName(path);
342 // reference point (center offset/bounding sphere)
343 *center = obj.get_gbs_center();
344 *bounding_radius = obj.get_gbs_radius();
346 point_list const& nodes = obj.get_wgs84_nodes();
347 // point_list const& colors = obj.get_colors();
348 point_list const& normals = obj.get_normals();
349 point_list const& texcoords = obj.get_texcoords();
354 group_list::size_type i;
357 string_list const& pt_materials = obj.get_pt_materials();
358 group_list const& pts_v = obj.get_pts_v();
359 group_list const& pts_n = obj.get_pts_n();
360 for ( i = 0; i < pts_v.size(); ++i ) {
361 // cout << "pts_v.size() = " << pts_v.size() << endl;
362 if ( pt_materials[i].substr(0, 3) == "RWY" ) {
363 // airport environment lighting
364 SGVec3d up(center->x(), center->y(), center->z());
365 // returns a transform -> lod -> leaf structure
366 osg::Node *branch = SGMakeDirectionalLights( nodes, normals,
369 pt_materials[i], up );
370 if ( pt_materials[i] == "RWY_VASI_LIGHTS" ) {
371 vasi_lights->addChild( branch );
372 } else if ( pt_materials[i] == "RWY_BLUE_TAXIWAY_LIGHTS"
373 || pt_materials[i] == "RWY_GREEN_TAXIWAY_LIGHTS" )
375 taxi_lights->addChild( branch );
377 rwy_lights->addChild( branch );
381 material = pt_materials[i];
383 osg::Node *leaf = SGMakeLeaf( path, GL_POINTS, matlib, material,
384 nodes, normals, texcoords,
385 pts_v[i], pts_n[i], tex_index,
386 false, ground_lights );
387 local_terrain->addChild( leaf );
391 // Put all randomly-placed objects under a separate branch
392 // (actually an ssgRangeSelector) named "random-models".
393 osg::Group * random_object_branch = 0;
394 if (use_random_objects) {
395 osg::LOD* object_lod = new osg::LOD;
396 object_lod->setName("random-models");
397 geometry->addChild(object_lod);
398 random_object_branch = new osg::Group;
399 // Maximum 20km range for random objects
400 object_lod->addChild(random_object_branch, 0, 20000);
403 typedef map<string,list<Leaf> > LeafMap;
406 leaf.type = GL_TRIANGLES;
407 string_list const& tri_materials = obj.get_tri_materials();
408 group_list const& tris_v = obj.get_tris_v();
409 group_list const& tris_n = obj.get_tris_n();
410 group_list const& tris_tc = obj.get_tris_tc();
411 for ( i = 0; i < tris_v.size(); i++ ) {
413 leafMap[ tri_materials[i] ].push_back( leaf );
415 leaf.type = GL_TRIANGLE_STRIP;
416 string_list const& strip_materials = obj.get_strip_materials();
417 group_list const& strips_v = obj.get_strips_v();
418 group_list const& strips_n = obj.get_strips_n();
419 group_list const& strips_tc = obj.get_strips_tc();
420 for ( i = 0; i < strips_v.size(); i++ ) {
422 leafMap[ strip_materials[i] ].push_back( leaf );
424 leaf.type = GL_TRIANGLE_FAN;
425 string_list const& fan_materials = obj.get_fan_materials();
426 group_list const& fans_v = obj.get_fans_v();
427 group_list const& fans_n = obj.get_fans_n();
428 group_list const& fans_tc = obj.get_fans_tc();
429 for ( i = 0; i < fans_v.size(); i++ ) {
431 leafMap[ fan_materials[i] ].push_back( leaf );
434 LeafMap::iterator lmi = leafMap.begin();
435 while ( lmi != leafMap.end() ) {
436 list<Leaf> &leaf_list = lmi->second;
437 list<Leaf>::iterator li = leaf_list.begin();
438 while ( li != leaf_list.end() ) {
440 int ind = leaf.index;
441 if ( leaf.type == GL_TRIANGLES ) {
442 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLES, matlib,
444 nodes, normals, texcoords,
445 tris_v[ind], tris_n[ind], tris_tc[ind],
446 is_base, ground_lights );
447 if ( use_random_objects ) {
448 SGMaterial *mat = matlib->find( tri_materials[ind] );
450 SG_LOG( SG_INPUT, SG_ALERT,
451 "Unknown material for random surface objects = "
452 << tri_materials[ind] );
454 gen_random_surface_objects( leaf, random_object_branch,
458 local_terrain->addChild( leaf );
459 } else if ( leaf.type == GL_TRIANGLE_STRIP ) {
460 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLE_STRIP,
461 matlib, strip_materials[ind],
462 nodes, normals, texcoords,
463 strips_v[ind], strips_n[ind], strips_tc[ind],
464 is_base, ground_lights );
465 if ( use_random_objects ) {
466 SGMaterial *mat = matlib->find( strip_materials[ind] );
468 SG_LOG( SG_INPUT, SG_ALERT,
469 "Unknown material for random surface objects = "
470 << strip_materials[ind] );
472 gen_random_surface_objects( leaf, random_object_branch,
476 local_terrain->addChild( leaf );
478 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLE_FAN,
479 matlib, fan_materials[ind],
480 nodes, normals, texcoords,
481 fans_v[ind], fans_n[ind], fans_tc[ind],
482 is_base, ground_lights );
483 if ( use_random_objects ) {
484 SGMaterial *mat = matlib->find( fan_materials[ind] );
486 SG_LOG( SG_INPUT, SG_ALERT,
487 "Unknown material for random surface objects = "
488 << fan_materials[ind] );
490 gen_random_surface_objects( leaf, random_object_branch,
494 local_terrain->addChild( leaf );