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>
37 #include <osg/Geometry>
40 #include <osg/MatrixTransform>
41 #include <osg/StateSet>
43 #include <simgear/bucket/newbucket.hxx>
44 #include <simgear/io/sg_binobj.hxx>
45 #include <simgear/math/sg_geodesy.hxx>
46 #include <simgear/math/sg_types.hxx>
47 #include <simgear/misc/texcoord.hxx>
48 #include <simgear/scene/material/mat.hxx>
49 #include <simgear/scene/material/matlib.hxx>
50 #include <simgear/scene/util/SGUpdateVisitor.hxx>
51 #include <simgear/scene/tgdb/leaf.hxx>
52 #include <simgear/scene/tgdb/pt_lights.hxx>
53 #include <simgear/scene/tgdb/userdata.hxx>
66 // Generate an ocean tile
67 bool SGGenTile( const string& path, SGBucket b,
68 Point3D *center, double *bounding_radius,
69 SGMaterialLib *matlib, osg::Group* group )
71 osg::StateSet *state = 0;
73 double tex_width = 1000.0;
76 // find Ocean material in the properties list
77 SGMaterial *mat = matlib->find( "Ocean" );
79 // set the texture width and height values for this
81 tex_width = mat->get_xsize();
82 // tex_height = newmat->get_ysize();
85 state = mat->get_state();
87 SG_LOG( SG_TERRAIN, SG_ALERT,
88 "Ack! unknown usemtl name = " << "Ocean"
92 // Calculate center point
93 double clon = b.get_center_lon();
94 double clat = b.get_center_lat();
95 double height = b.get_height();
96 double width = b.get_width();
98 *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
99 clat*SGD_DEGREES_TO_RADIANS,
101 // cout << "center = " << center << endl;;
103 // Caculate corner vertices
105 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
106 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
107 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
108 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
112 for ( i = 0; i < 4; ++i ) {
113 rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
114 geod[i].y() * SGD_DEGREES_TO_RADIANS,
118 Point3D cart[4], rel[4];
119 for ( i = 0; i < 4; ++i ) {
120 cart[i] = sgGeodToCart(rad[i]);
121 rel[i] = cart[i] - *center;
122 // cout << "corner " << i << " = " << cart[i] << endl;
125 // Calculate bounding radius
126 *bounding_radius = center->distance3D( cart[0] );
127 // cout << "bounding radius = " << t->bounding_radius << endl;
131 for ( i = 0; i < 4; ++i ) {
132 double length = cart[i].distance3D( Point3D(0.0) );
133 normals[i] = cart[i] / length;
134 // cout << "normal = " << normals[i] << endl;
137 // Calculate texture coordinates
138 point_list geod_nodes;
140 geod_nodes.reserve(4);
143 rectangle.reserve(4);
144 for ( i = 0; i < 4; ++i ) {
145 geod_nodes.push_back( geod[i] );
146 rectangle.push_back( i );
148 point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle,
149 1000.0 / tex_width );
151 // Allocate ssg structure
152 osg::Vec3Array *vl = new osg::Vec3Array;
153 osg::Vec3Array *nl = new osg::Vec3Array;
154 osg::Vec2Array *tl = new osg::Vec2Array;
156 for ( i = 0; i < 4; ++i ) {
157 vl->push_back(osg::Vec3(rel[i].x(), rel[i].y(), rel[i].z()));
158 nl->push_back(osg::Vec3(normals[i].x(), normals[i].y(), normals[i].z()));
159 tl->push_back(osg::Vec2(texs[i].x(), texs[i].y()));
162 osg::Vec4Array* cl = new osg::Vec4Array;
163 cl->push_back(osg::Vec4(1, 1, 1, 1));
165 osg::Geometry* geometry = new osg::Geometry;
166 geometry->setVertexArray(vl);
167 geometry->setNormalArray(nl);
168 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
169 geometry->setColorArray(cl);
170 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
171 geometry->setTexCoordArray(0, tl);
172 geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_FAN, 0, vl->size()));
173 osg::Geode* geode = new osg::Geode;
174 geode->setName(path);
175 geode->addDrawable(geometry);
176 geode->setStateSet(state);
178 group->addChild(geode);
185 * SSG callback for an in-range leaf of randomly-placed objects.
187 * This pretraversal callback is attached to a branch that is
188 * traversed only when a leaf is in range. If the leaf is not
189 * currently prepared to be populated with randomly-placed objects,
190 * this callback will prepare it (actual population is handled by
191 * the tri_in_range_callback for individual triangles).
193 * @param entity The entity to which the callback is attached (not used).
194 * @param mask The entity's traversal mask (not used).
195 * @return Always 1, to allow traversal and culling to continue.
198 // leaf_in_range_callback (ssgEntity * entity, int mask)
200 // SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
202 // if (!data->is_filled_in) {
203 // // Iterate through all the triangles
204 // // and populate them.
205 // int num_tris = data->leaf->getNumTriangles();
206 // for ( int i = 0; i < num_tris; ++i ) {
207 // data->setup_triangle(i);
209 // data->is_filled_in = true;
216 * SSG callback for an out-of-range leaf of randomly-placed objects.
218 * This pretraversal callback is attached to a branch that is
219 * traversed only when a leaf is out of range. If the leaf is
220 * currently prepared to be populated with randomly-placed objects (or
221 * is actually populated), the objects will be removed.
223 * @param entity The entity to which the callback is attached (not used).
224 * @param mask The entity's traversal mask (not used).
225 * @return Always 0, to prevent any further traversal or culling.
228 // leaf_out_of_range_callback (ssgEntity * entity, int mask)
230 // SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
231 // if (data->is_filled_in) {
232 // data->branch->removeAllKids();
233 // data->is_filled_in = false;
240 * Randomly place objects on a surface.
242 * The leaf node provides the geometry of the surface, while the
243 * material provides the objects and placement density. Latitude
244 * and longitude are required so that the objects can be rotated
245 * to the world-up vector. This function does not actually add
246 * any objects; instead, it attaches an ssgRangeSelector to the
247 * branch with callbacks to generate the objects when needed.
249 * @param leaf The surface where the objects should be placed.
250 * @param branch The branch that will hold the randomly-placed objects.
251 * @param center The center of the leaf in FlightGear coordinates.
252 * @param material_name The name of the surface's material.
255 gen_random_surface_objects (osg::Node *leaf,
262 // If the surface has no triangles, return
264 int num_tris = leaf->getNumTriangles();
268 // If the material has no randomly-placed
269 // objects, return now.
270 if (mat->get_object_group_count() < 1)
273 // Calculate the geodetic centre of
274 // the tile, for aligning automatic
276 double xyz[3], lon_rad, lat_rad, alt_m;
277 xyz[0] = center->x(); xyz[1] = center->y(); xyz[2] = center->z();
278 sgCartToGeod(xyz, &lat_rad, &lon_rad, &alt_m);
281 // max random object range: 20000m
282 osg::LOD * lod = new osg::LOD;
283 branch->addChild(lod);
285 // Create the in-range and out-of-range
287 osg::Group * in_range = new osg::Group;
288 // osg::Group * out_of_range = new osg::Group;
289 lod->addChild(in_range, 0, 20000 /*OSGFIXME hardcoded visibility ???*/);
290 // lod->addChild(out_of_range, 20000, 1e30);
292 SGLeafUserData * data = new SGLeafUserData;
293 data->is_filled_in = false;
296 data->branch = in_range;
297 data->sin_lat = sin(lat_rad);
298 data->cos_lat = cos(lat_rad);
299 data->sin_lon = sin(lon_rad);
300 data->cos_lon = cos(lon_rad);
302 in_range->setUserData(data);
303 // OSGFIXME: implement random objects to be loaded when in sight
304 // in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
306 // OSGFIXME: implement deletion of tiles that are no longer used
307 // out_of_range->setUserData(data);
308 // out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
309 // leaf_out_of_range_callback);
311 // ->addChild(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
316 // Ok, somehow polygon offset for lights ...
317 // Could never make the polygon offset for our lights get right.
318 // So, why not in this way ...
319 class SGLightOffsetTransform : public osg::Transform {
321 #define SCALE_FACTOR 0.94
322 virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,
323 osg::NodeVisitor* nv) const
325 if (nv && nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
326 double scaleFactor = SCALE_FACTOR;
327 osg::Vec3 center = nv->getEyePoint();
328 osg::Matrix transform;
329 transform(0,0) = scaleFactor;
330 transform(1,1) = scaleFactor;
331 transform(2,2) = scaleFactor;
332 transform(3,0) = center[0]*(1 - scaleFactor);
333 transform(3,1) = center[1]*(1 - scaleFactor);
334 transform(3,2) = center[2]*(1 - scaleFactor);
335 matrix.preMult(transform);
339 virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,
340 osg::NodeVisitor* nv) const
342 if (nv && nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR) {
343 double scaleFactor = 1/SCALE_FACTOR;
344 osg::Vec3 center = nv->getEyePoint();
345 osg::Matrix transform;
346 transform(0,0) = scaleFactor;
347 transform(1,1) = scaleFactor;
348 transform(2,2) = scaleFactor;
349 transform(3,0) = center[0]*(1 - scaleFactor);
350 transform(3,1) = center[1]*(1 - scaleFactor);
351 transform(3,2) = center[2]*(1 - scaleFactor);
352 matrix.preMult(transform);
361 ////////////////////////////////////////////////////////////////////////
363 ////////////////////////////////////////////////////////////////////////
365 // Load an Binary obj file
366 bool SGBinObjLoad( const string& path, const bool is_base,
368 double *bounding_radius,
369 SGMaterialLib *matlib,
370 bool use_random_objects,
371 osg::Group *geometry,
372 osg::Group *_vasi_lights,
373 osg::Group *_rwy_lights,
374 osg::Group *_taxi_lights,
375 osg::Vec3Array *ground_lights )
379 if ( ! obj.read_bin( path ) ) {
383 // Ok, somehow polygon offset for lights ...
384 // Could never make the polygon offset for our lights get right.
385 // So, why not in this way ...
386 SGLightOffsetTransform* vasi_lights = 0;
388 vasi_lights = new SGLightOffsetTransform;
389 _vasi_lights->addChild(vasi_lights);
391 SGLightOffsetTransform *rwy_lights = 0;
393 rwy_lights = new SGLightOffsetTransform;
394 _rwy_lights->addChild(rwy_lights);
396 SGLightOffsetTransform *taxi_lights = 0;
398 taxi_lights = new SGLightOffsetTransform;
399 _taxi_lights->addChild(taxi_lights);
402 osg::Group *local_terrain = new osg::Group;
403 local_terrain->setName( "LocalTerrain" );
404 geometry->addChild( local_terrain );
406 geometry->setName(path);
408 // reference point (center offset/bounding sphere)
409 *center = obj.get_gbs_center();
410 *bounding_radius = obj.get_gbs_radius();
412 point_list const& nodes = obj.get_wgs84_nodes();
413 // point_list const& colors = obj.get_colors();
414 point_list const& normals = obj.get_normals();
415 point_list const& texcoords = obj.get_texcoords();
420 group_list::size_type i;
423 string_list const& pt_materials = obj.get_pt_materials();
424 group_list const& pts_v = obj.get_pts_v();
425 group_list const& pts_n = obj.get_pts_n();
426 for ( i = 0; i < pts_v.size(); ++i ) {
427 // cout << "pts_v.size() = " << pts_v.size() << endl;
428 if ( pt_materials[i].substr(0, 3) == "RWY" ) {
429 // airport environment lighting
430 SGVec3d up(center->x(), center->y(), center->z());
431 // returns a transform -> lod -> leaf structure
432 osg::Node *branch = SGMakeDirectionalLights( nodes, normals,
435 pt_materials[i], up );
436 if ( pt_materials[i] == "RWY_VASI_LIGHTS" ) {
437 vasi_lights->addChild( branch );
438 } else if ( pt_materials[i] == "RWY_BLUE_TAXIWAY_LIGHTS"
439 || pt_materials[i] == "RWY_GREEN_TAXIWAY_LIGHTS" )
441 taxi_lights->addChild( branch );
443 rwy_lights->addChild( branch );
447 material = pt_materials[i];
449 osg::Node *leaf = SGMakeLeaf( path, GL_POINTS, matlib, material,
450 nodes, normals, texcoords,
451 pts_v[i], pts_n[i], tex_index,
452 false, ground_lights );
453 local_terrain->addChild( leaf );
457 // Put all randomly-placed objects under a separate branch
458 // (actually an ssgRangeSelector) named "random-models".
459 osg::Group * random_object_branch = 0;
460 if (use_random_objects) {
461 osg::LOD* object_lod = new osg::LOD;
462 object_lod->setName("random-models");
463 geometry->addChild(object_lod);
464 random_object_branch = new osg::Group;
465 // Maximum 20km range for random objects
466 object_lod->addChild(random_object_branch, 0, 20000);
469 typedef map<string,list<Leaf> > LeafMap;
472 leaf.type = GL_TRIANGLES;
473 string_list const& tri_materials = obj.get_tri_materials();
474 group_list const& tris_v = obj.get_tris_v();
475 group_list const& tris_n = obj.get_tris_n();
476 group_list const& tris_tc = obj.get_tris_tc();
477 for ( i = 0; i < tris_v.size(); i++ ) {
479 leafMap[ tri_materials[i] ].push_back( leaf );
481 leaf.type = GL_TRIANGLE_STRIP;
482 string_list const& strip_materials = obj.get_strip_materials();
483 group_list const& strips_v = obj.get_strips_v();
484 group_list const& strips_n = obj.get_strips_n();
485 group_list const& strips_tc = obj.get_strips_tc();
486 for ( i = 0; i < strips_v.size(); i++ ) {
488 leafMap[ strip_materials[i] ].push_back( leaf );
490 leaf.type = GL_TRIANGLE_FAN;
491 string_list const& fan_materials = obj.get_fan_materials();
492 group_list const& fans_v = obj.get_fans_v();
493 group_list const& fans_n = obj.get_fans_n();
494 group_list const& fans_tc = obj.get_fans_tc();
495 for ( i = 0; i < fans_v.size(); i++ ) {
497 leafMap[ fan_materials[i] ].push_back( leaf );
500 LeafMap::iterator lmi = leafMap.begin();
501 while ( lmi != leafMap.end() ) {
502 list<Leaf> &leaf_list = lmi->second;
503 list<Leaf>::iterator li = leaf_list.begin();
504 while ( li != leaf_list.end() ) {
506 int ind = leaf.index;
507 if ( leaf.type == GL_TRIANGLES ) {
508 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLES, matlib,
510 nodes, normals, texcoords,
511 tris_v[ind], tris_n[ind], tris_tc[ind],
512 is_base, ground_lights );
513 if ( use_random_objects ) {
514 SGMaterial *mat = matlib->find( tri_materials[ind] );
516 SG_LOG( SG_INPUT, SG_ALERT,
517 "Unknown material for random surface objects = "
518 << tri_materials[ind] );
520 gen_random_surface_objects( leaf, random_object_branch,
524 local_terrain->addChild( leaf );
525 } else if ( leaf.type == GL_TRIANGLE_STRIP ) {
526 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLE_STRIP,
527 matlib, strip_materials[ind],
528 nodes, normals, texcoords,
529 strips_v[ind], strips_n[ind], strips_tc[ind],
530 is_base, ground_lights );
531 if ( use_random_objects ) {
532 SGMaterial *mat = matlib->find( strip_materials[ind] );
534 SG_LOG( SG_INPUT, SG_ALERT,
535 "Unknown material for random surface objects = "
536 << strip_materials[ind] );
538 gen_random_surface_objects( leaf, random_object_branch,
542 local_terrain->addChild( leaf );
544 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLE_FAN,
545 matlib, fan_materials[ind],
546 nodes, normals, texcoords,
547 fans_v[ind], fans_n[ind], fans_tc[ind],
548 is_base, ground_lights );
549 if ( use_random_objects ) {
550 SGMaterial *mat = matlib->find( fan_materials[ind] );
552 SG_LOG( SG_INPUT, SG_ALERT,
553 "Unknown material for random surface objects = "
554 << fan_materials[ind] );
556 gen_random_surface_objects( leaf, random_object_branch,
560 local_terrain->addChild( leaf );