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 <simgear/bucket/newbucket.hxx>
36 #include <simgear/io/sg_binobj.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38 #include <simgear/math/sg_types.hxx>
39 #include <simgear/misc/texcoord.hxx>
40 #include <simgear/scene/material/mat.hxx>
41 #include <simgear/scene/material/matlib.hxx>
42 #include <simgear/scene/tgdb/leaf.hxx>
43 #include <simgear/scene/tgdb/pt_lights.hxx>
44 #include <simgear/scene/tgdb/userdata.hxx>
57 // Generate an ocean tile
58 bool sgGenTile( const string& path, SGBucket b,
59 Point3D *center, double *bounding_radius,
60 SGMaterialLib *matlib, ssgBranch* geometry )
62 ssgSimpleState *state = NULL;
64 geometry->setName( (char *)path.c_str() );
66 double tex_width = 1000.0;
69 // find Ocean material in the properties list
70 SGMaterial *mat = matlib->find( "Ocean" );
72 // set the texture width and height values for this
74 tex_width = mat->get_xsize();
75 // tex_height = newmat->get_ysize();
78 state = mat->get_state();
80 SG_LOG( SG_TERRAIN, SG_ALERT,
81 "Ack! unknown usemtl name = " << "Ocean"
85 // Calculate center point
86 double clon = b.get_center_lon();
87 double clat = b.get_center_lat();
88 double height = b.get_height();
89 double width = b.get_width();
91 *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
92 clat*SGD_DEGREES_TO_RADIANS,
94 // cout << "center = " << center << endl;;
96 // Caculate corner vertices
98 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
99 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
100 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
101 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
105 for ( i = 0; i < 4; ++i ) {
106 rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
107 geod[i].y() * SGD_DEGREES_TO_RADIANS,
111 Point3D cart[4], rel[4];
112 for ( i = 0; i < 4; ++i ) {
113 cart[i] = sgGeodToCart(rad[i]);
114 rel[i] = cart[i] - *center;
115 // cout << "corner " << i << " = " << cart[i] << endl;
118 // Calculate bounding radius
119 *bounding_radius = center->distance3D( cart[0] );
120 // cout << "bounding radius = " << t->bounding_radius << endl;
124 for ( i = 0; i < 4; ++i ) {
125 double length = cart[i].distance3D( Point3D(0.0) );
126 normals[i] = cart[i] / length;
127 // cout << "normal = " << normals[i] << endl;
130 // Calculate texture coordinates
131 point_list geod_nodes;
133 geod_nodes.reserve(4);
136 rectangle.reserve(4);
137 for ( i = 0; i < 4; ++i ) {
138 geod_nodes.push_back( geod[i] );
139 rectangle.push_back( i );
141 point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle,
142 1000.0 / tex_width );
144 // Allocate ssg structure
145 ssgVertexArray *vl = new ssgVertexArray( 4 );
146 ssgNormalArray *nl = new ssgNormalArray( 4 );
147 ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
148 ssgColourArray *cl = new ssgColourArray( 1 );
151 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
154 // sgVec3 *vtlist = new sgVec3 [ 4 ];
155 // t->vec3_ptrs.push_back( vtlist );
156 // sgVec3 *vnlist = new sgVec3 [ 4 ];
157 // t->vec3_ptrs.push_back( vnlist );
158 // sgVec2 *tclist = new sgVec2 [ 4 ];
159 // t->vec2_ptrs.push_back( tclist );
163 for ( i = 0; i < 4; ++i ) {
165 rel[i].x(), rel[i].y(), rel[i].z() );
169 normals[i].x(), normals[i].y(), normals[i].z() );
172 sgSetVec2( tmp2, texs[i].x(), texs[i].y());
177 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
179 leaf->setUserData( new SGMaterialUserData(mat) );
180 leaf->setState( state );
181 geometry->addKid( leaf );
188 * SSG callback for an in-range leaf of randomly-placed objects.
190 * This pretraversal callback is attached to a branch that is
191 * traversed only when a leaf is in range. If the leaf is not
192 * currently prepared to be populated with randomly-placed objects,
193 * this callback will prepare it (actual population is handled by
194 * the tri_in_range_callback for individual triangles).
196 * @param entity The entity to which the callback is attached (not used).
197 * @param mask The entity's traversal mask (not used).
198 * @return Always 1, to allow traversal and culling to continue.
201 leaf_in_range_callback (ssgEntity * entity, int mask)
203 SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
205 if (!data->is_filled_in) {
206 // Iterate through all the triangles
207 // and populate them.
208 int num_tris = data->leaf->getNumTriangles();
209 for ( int i = 0; i < num_tris; ++i ) {
210 data->setup_triangle(i);
212 data->is_filled_in = true;
219 * SSG callback for an out-of-range leaf of randomly-placed objects.
221 * This pretraversal callback is attached to a branch that is
222 * traversed only when a leaf is out of range. If the leaf is
223 * currently prepared to be populated with randomly-placed objects (or
224 * is actually populated), the objects will be removed.
226 * @param entity The entity to which the callback is attached (not used).
227 * @param mask The entity's traversal mask (not used).
228 * @return Always 0, to prevent any further traversal or culling.
231 leaf_out_of_range_callback (ssgEntity * entity, int mask)
233 SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
234 if (data->is_filled_in) {
235 data->branch->removeAllKids();
236 data->is_filled_in = false;
243 * Randomly place objects on a surface.
245 * The leaf node provides the geometry of the surface, while the
246 * material provides the objects and placement density. Latitude
247 * and longitude are required so that the objects can be rotated
248 * to the world-up vector. This function does not actually add
249 * any objects; instead, it attaches an ssgRangeSelector to the
250 * branch with callbacks to generate the objects when needed.
252 * @param leaf The surface where the objects should be placed.
253 * @param branch The branch that will hold the randomly-placed objects.
254 * @param center The center of the leaf in FlightGear coordinates.
255 * @param material_name The name of the surface's material.
258 gen_random_surface_objects (ssgLeaf *leaf,
263 // If the surface has no triangles, return
265 int num_tris = leaf->getNumTriangles();
269 // If the material has no randomly-placed
270 // objects, return now.
271 if (mat->get_object_group_count() < 1)
274 // Calculate the geodetic centre of
275 // the tile, for aligning automatic
277 double xyz[3], lon_rad, lat_rad, alt_m;
278 xyz[0] = center->x(); xyz[1] = center->y(); xyz[2] = center->z();
279 sgCartToGeod(xyz, &lat_rad, &lon_rad, &alt_m);
282 // max random object range: 20000m
283 float ranges[] = { 0, 20000, 1000000 };
284 ssgRangeSelector * lod = new ssgRangeSelector;
285 lod->setRanges(ranges, 3);
288 // Create the in-range and out-of-range
290 ssgBranch * in_range = new ssgBranch;
291 ssgBranch * out_of_range = new ssgBranch;
292 lod->addKid(in_range);
293 lod->addKid(out_of_range);
295 SGLeafUserData * data = new SGLeafUserData;
296 data->is_filled_in = false;
299 data->branch = in_range;
300 data->sin_lat = sin(lat_rad);
301 data->cos_lat = cos(lat_rad);
302 data->sin_lon = sin(lon_rad);
303 data->cos_lon = cos(lon_rad);
305 in_range->setUserData(data);
306 in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
307 out_of_range->setUserData(data);
308 out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
309 leaf_out_of_range_callback);
311 ->addKid(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
316 ////////////////////////////////////////////////////////////////////////
318 ////////////////////////////////////////////////////////////////////////
320 // Load an Binary obj file
321 bool sgBinObjLoad( const string& path, const bool is_base,
323 double *bounding_radius,
324 SGMaterialLib *matlib,
325 bool use_random_objects,
327 ssgBranch *vasi_lights,
328 ssgBranch *rwy_lights,
329 ssgBranch *taxi_lights,
330 ssgVertexArray *ground_lights )
334 if ( ! obj.read_bin( path ) ) {
338 ssgBranch *local_terrain = new ssgBranch;
339 local_terrain->setName( "LocalTerrain" );
340 geometry->addKid( local_terrain );
342 geometry->setName( (char *)path.c_str() );
344 // reference point (center offset/bounding sphere)
345 *center = obj.get_gbs_center();
346 *bounding_radius = obj.get_gbs_radius();
348 point_list const& nodes = obj.get_wgs84_nodes();
349 // point_list const& colors = obj.get_colors();
350 point_list const& normals = obj.get_normals();
351 point_list const& texcoords = obj.get_texcoords();
356 group_list::size_type i;
359 string_list const& pt_materials = obj.get_pt_materials();
360 group_list const& pts_v = obj.get_pts_v();
361 group_list const& pts_n = obj.get_pts_n();
362 for ( i = 0; i < pts_v.size(); ++i ) {
363 // cout << "pts_v.size() = " << pts_v.size() << endl;
364 if ( pt_materials[i].substr(0, 3) == "RWY" ) {
365 // airport environment lighting
367 sgdSetVec3( up, center->x(), center->y(), center->z() );
368 // returns a transform -> lod -> leaf structure
369 ssgBranch *branch = sgMakeDirectionalLights( nodes, normals,
372 pt_materials[i], up );
373 if ( pt_materials[i] == "RWY_VASI_LIGHTS" ) {
374 vasi_lights->addKid( branch );
375 } else if ( pt_materials[i] == "RWY_BLUE_TAXIWAY_LIGHTS"
376 || pt_materials[i] == "RWY_GREEN_TAXIWAY_LIGHTS" )
378 taxi_lights->addKid( branch );
380 rwy_lights->addKid( branch );
384 material = pt_materials[i];
386 ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
387 nodes, normals, texcoords,
388 pts_v[i], pts_n[i], tex_index,
389 false, ground_lights );
390 local_terrain->addKid( leaf );
394 // Put all randomly-placed objects under a separate branch
395 // (actually an ssgRangeSelector) named "random-models".
396 ssgBranch * random_object_branch = 0;
397 if (use_random_objects) {
398 float ranges[] = { 0, 20000 }; // Maximum 20km range for random objects
399 ssgRangeSelector * object_lod = new ssgRangeSelector;
400 object_lod->setRanges(ranges, 2);
401 object_lod->setName("random-models");
402 geometry->addKid(object_lod);
403 random_object_branch = new ssgBranch;
404 object_lod->addKid(random_object_branch);
407 typedef map<string,list<Leaf> > LeafMap;
410 leaf.type = GL_TRIANGLES;
411 string_list const& tri_materials = obj.get_tri_materials();
412 group_list const& tris_v = obj.get_tris_v();
413 group_list const& tris_n = obj.get_tris_n();
414 group_list const& tris_tc = obj.get_tris_tc();
415 for ( i = 0; i < tris_v.size(); i++ ) {
417 leafMap[ tri_materials[i] ].push_back( leaf );
419 leaf.type = GL_TRIANGLE_STRIP;
420 string_list const& strip_materials = obj.get_strip_materials();
421 group_list const& strips_v = obj.get_strips_v();
422 group_list const& strips_n = obj.get_strips_n();
423 group_list const& strips_tc = obj.get_strips_tc();
424 for ( i = 0; i < strips_v.size(); i++ ) {
426 leafMap[ strip_materials[i] ].push_back( leaf );
428 leaf.type = GL_TRIANGLE_FAN;
429 string_list const& fan_materials = obj.get_fan_materials();
430 group_list const& fans_v = obj.get_fans_v();
431 group_list const& fans_n = obj.get_fans_n();
432 group_list const& fans_tc = obj.get_fans_tc();
433 for ( i = 0; i < fans_v.size(); i++ ) {
435 leafMap[ fan_materials[i] ].push_back( leaf );
438 LeafMap::iterator lmi = leafMap.begin();
439 while ( lmi != leafMap.end() ) {
440 list<Leaf> &leaf_list = lmi->second;
441 list<Leaf>::iterator li = leaf_list.begin();
442 while ( li != leaf_list.end() ) {
444 int ind = leaf.index;
445 if ( leaf.type == GL_TRIANGLES ) {
446 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
448 nodes, normals, texcoords,
449 tris_v[ind], tris_n[ind], tris_tc[ind],
450 is_base, ground_lights );
451 if ( use_random_objects ) {
452 SGMaterial *mat = matlib->find( tri_materials[ind] );
454 SG_LOG( SG_INPUT, SG_ALERT,
455 "Unknown material for random surface objects = "
456 << tri_materials[ind] );
458 gen_random_surface_objects( leaf, random_object_branch,
462 local_terrain->addKid( leaf );
463 } else if ( leaf.type == GL_TRIANGLE_STRIP ) {
464 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
465 matlib, strip_materials[ind],
466 nodes, normals, texcoords,
467 strips_v[ind], strips_n[ind], strips_tc[ind],
468 is_base, ground_lights );
469 if ( use_random_objects ) {
470 SGMaterial *mat = matlib->find( strip_materials[ind] );
472 SG_LOG( SG_INPUT, SG_ALERT,
473 "Unknown material for random surface objects = "
474 << strip_materials[ind] );
476 gen_random_surface_objects( leaf, random_object_branch,
480 local_terrain->addKid( leaf );
482 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
483 matlib, fan_materials[ind],
484 nodes, normals, texcoords,
485 fans_v[ind], fans_n[ind], fans_tc[ind],
486 is_base, ground_lights );
487 if ( use_random_objects ) {
488 SGMaterial *mat = matlib->find( fan_materials[ind] );
490 SG_LOG( SG_INPUT, SG_ALERT,
491 "Unknown material for random surface objects = "
492 << fan_materials[ind] );
494 gen_random_surface_objects( leaf, random_object_branch,
498 local_terrain->addKid( leaf );