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 - curt@infoplane.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
33 #include <simgear/bucket/newbucket.hxx>
34 #include <simgear/io/sg_binobj.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/math/sg_types.hxx>
37 #include <simgear/misc/texcoord.hxx>
38 #include <simgear/scene/material/mat.hxx>
39 #include <simgear/scene/material/matlib.hxx>
40 #include <simgear/scene/tgdb/leaf.hxx>
41 #include <simgear/scene/tgdb/pt_lights.hxx>
42 #include <simgear/scene/tgdb/userdata.hxx>
49 // Generate an ocean tile
50 bool sgGenTile( const string& path, SGBucket b,
51 Point3D *center, double *bounding_radius,
52 SGMaterialLib *matlib, ssgBranch* geometry )
54 ssgSimpleState *state = NULL;
56 geometry->setName( (char *)path.c_str() );
58 double tex_width = 1000.0;
61 // find Ocean material in the properties list
62 SGMaterial *mat = matlib->find( "Ocean" );
64 // set the texture width and height values for this
66 tex_width = mat->get_xsize();
67 // tex_height = newmat->get_ysize();
70 state = mat->get_state();
72 SG_LOG( SG_TERRAIN, SG_ALERT,
73 "Ack! unknown usemtl name = " << "Ocean"
77 // Calculate center point
78 double clon = b.get_center_lon();
79 double clat = b.get_center_lat();
80 double height = b.get_height();
81 double width = b.get_width();
83 *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
84 clat*SGD_DEGREES_TO_RADIANS,
86 // cout << "center = " << center << endl;;
88 // Caculate corner vertices
90 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
91 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
92 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
93 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
97 for ( i = 0; i < 4; ++i ) {
98 rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
99 geod[i].y() * SGD_DEGREES_TO_RADIANS,
103 Point3D cart[4], rel[4];
104 for ( i = 0; i < 4; ++i ) {
105 cart[i] = sgGeodToCart(rad[i]);
106 rel[i] = cart[i] - *center;
107 // cout << "corner " << i << " = " << cart[i] << endl;
110 // Calculate bounding radius
111 *bounding_radius = center->distance3D( cart[0] );
112 // cout << "bounding radius = " << t->bounding_radius << endl;
116 for ( i = 0; i < 4; ++i ) {
117 double length = cart[i].distance3D( Point3D(0.0) );
118 normals[i] = cart[i] / length;
119 // cout << "normal = " << normals[i] << endl;
122 // Calculate texture coordinates
123 point_list geod_nodes;
125 geod_nodes.reserve(4);
128 rectangle.reserve(4);
129 for ( i = 0; i < 4; ++i ) {
130 geod_nodes.push_back( geod[i] );
131 rectangle.push_back( i );
133 point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle,
134 1000.0 / tex_width );
136 // Allocate ssg structure
137 ssgVertexArray *vl = new ssgVertexArray( 4 );
138 ssgNormalArray *nl = new ssgNormalArray( 4 );
139 ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
140 ssgColourArray *cl = new ssgColourArray( 1 );
143 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
146 // sgVec3 *vtlist = new sgVec3 [ 4 ];
147 // t->vec3_ptrs.push_back( vtlist );
148 // sgVec3 *vnlist = new sgVec3 [ 4 ];
149 // t->vec3_ptrs.push_back( vnlist );
150 // sgVec2 *tclist = new sgVec2 [ 4 ];
151 // t->vec2_ptrs.push_back( tclist );
155 for ( i = 0; i < 4; ++i ) {
157 rel[i].x(), rel[i].y(), rel[i].z() );
161 normals[i].x(), normals[i].y(), normals[i].z() );
164 sgSetVec2( tmp2, texs[i].x(), texs[i].y());
169 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
171 leaf->setState( state );
173 geometry->addKid( leaf );
180 * SSG callback for an in-range leaf of randomly-placed objects.
182 * This pretraversal callback is attached to a branch that is
183 * traversed only when a leaf is in range. If the leaf is not
184 * currently prepared to be populated with randomly-placed objects,
185 * this callback will prepare it (actual population is handled by
186 * the tri_in_range_callback for individual triangles).
188 * @param entity The entity to which the callback is attached (not used).
189 * @param mask The entity's traversal mask (not used).
190 * @return Always 1, to allow traversal and culling to continue.
193 leaf_in_range_callback (ssgEntity * entity, int mask)
195 SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
197 if (!data->is_filled_in) {
198 // Iterate through all the triangles
199 // and populate them.
200 int num_tris = data->leaf->getNumTriangles();
201 for ( int i = 0; i < num_tris; ++i ) {
202 data->setup_triangle(i);
204 data->is_filled_in = true;
211 * SSG callback for an out-of-range leaf of randomly-placed objects.
213 * This pretraversal callback is attached to a branch that is
214 * traversed only when a leaf is out of range. If the leaf is
215 * currently prepared to be populated with randomly-placed objects (or
216 * is actually populated), the objects will be removed.
218 * @param entity The entity to which the callback is attached (not used).
219 * @param mask The entity's traversal mask (not used).
220 * @return Always 0, to prevent any further traversal or culling.
223 leaf_out_of_range_callback (ssgEntity * entity, int mask)
225 SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
226 if (data->is_filled_in) {
227 data->branch->removeAllKids();
228 data->is_filled_in = false;
235 * Randomly place objects on a surface.
237 * The leaf node provides the geometry of the surface, while the
238 * material provides the objects and placement density. Latitude
239 * and longitude are required so that the objects can be rotated
240 * to the world-up vector. This function does not actually add
241 * any objects; instead, it attaches an ssgRangeSelector to the
242 * branch with callbacks to generate the objects when needed.
244 * @param leaf The surface where the objects should be placed.
245 * @param branch The branch that will hold the randomly-placed objects.
246 * @param center The center of the leaf in FlightGear coordinates.
247 * @param material_name The name of the surface's material.
250 gen_random_surface_objects (ssgLeaf *leaf,
255 // If the surface has no triangles, return
257 int num_tris = leaf->getNumTriangles();
261 // If the material has no randomly-placed
262 // objects, return now.
263 if (mat->get_object_group_count() < 1)
266 // Calculate the geodetic centre of
267 // the tile, for aligning automatic
269 double xyz[3], lon_rad, lat_rad, alt_m;
270 xyz[0] = center->x(); xyz[1] = center->y(); xyz[2] = center->z();
271 sgCartToGeod(xyz, &lat_rad, &lon_rad, &alt_m);
274 // max random object range: 20000m
275 float ranges[] = { 0, 20000, 1000000 };
276 ssgRangeSelector * lod = new ssgRangeSelector;
277 lod->setRanges(ranges, 3);
280 // Create the in-range and out-of-range
282 ssgBranch * in_range = new ssgBranch;
283 ssgBranch * out_of_range = new ssgBranch;
284 lod->addKid(in_range);
285 lod->addKid(out_of_range);
287 SGLeafUserData * data = new SGLeafUserData;
288 data->is_filled_in = false;
291 data->branch = in_range;
292 data->sin_lat = sin(lat_rad);
293 data->cos_lat = cos(lat_rad);
294 data->sin_lon = sin(lon_rad);
295 data->cos_lon = cos(lon_rad);
297 in_range->setUserData(data);
298 in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
299 out_of_range->setUserData(data);
300 out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
301 leaf_out_of_range_callback);
303 ->addKid(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
308 ////////////////////////////////////////////////////////////////////////
310 ////////////////////////////////////////////////////////////////////////
312 // Load an Binary obj file
313 bool sgBinObjLoad( const string& path, const bool is_base,
315 double *bounding_radius,
316 SGMaterialLib *matlib,
317 bool use_random_objects,
319 ssgBranch *vasi_lights,
320 ssgBranch *rwy_lights,
321 ssgBranch *taxi_lights,
322 ssgVertexArray *ground_lights )
326 if ( ! obj.read_bin( path ) ) {
330 geometry->setName( (char *)path.c_str() );
332 // reference point (center offset/bounding sphere)
333 *center = obj.get_gbs_center();
334 *bounding_radius = obj.get_gbs_radius();
336 point_list const& nodes = obj.get_wgs84_nodes();
337 // point_list const& colors = obj.get_colors();
338 point_list const& normals = obj.get_normals();
339 point_list const& texcoords = obj.get_texcoords();
344 group_list::size_type i;
347 string_list const& pt_materials = obj.get_pt_materials();
348 group_list const& pts_v = obj.get_pts_v();
349 group_list const& pts_n = obj.get_pts_n();
350 for ( i = 0; i < pts_v.size(); ++i ) {
351 // cout << "pts_v.size() = " << pts_v.size() << endl;
352 if ( pt_materials[i].substr(0, 3) == "RWY" ) {
353 // airport environment lighting
355 sgdSetVec3( up, center->x(), center->y(), center->z() );
356 // returns a transform -> lod -> leaf structure
357 ssgBranch *branch = sgMakeDirectionalLights( nodes, normals,
360 pt_materials[i], up );
361 if ( pt_materials[i] == "RWY_VASI_LIGHTS" ) {
362 vasi_lights->addKid( branch );
363 } else if ( pt_materials[i] == "RWY_BLUE_TAXIWAY_LIGHTS"
364 || pt_materials[i] == "RWY_GREEN_TAXIWAY_LIGHTS" )
366 taxi_lights->addKid( branch );
368 rwy_lights->addKid( branch );
372 material = pt_materials[i];
374 ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
375 nodes, normals, texcoords,
376 pts_v[i], pts_n[i], tex_index,
377 false, ground_lights );
378 geometry->addKid( leaf );
382 // Put all randomly-placed objects under a separate branch
383 // (actually an ssgRangeSelector) named "random-models".
384 ssgBranch * random_object_branch = 0;
385 if (use_random_objects) {
386 float ranges[] = { 0, 20000 }; // Maximum 20km range for random objects
387 ssgRangeSelector * object_lod = new ssgRangeSelector;
388 object_lod->setRanges(ranges, 2);
389 object_lod->setName("random-models");
390 geometry->addKid(object_lod);
391 random_object_branch = new ssgBranch;
392 object_lod->addKid(random_object_branch);
395 // generate triangles
396 string_list const& tri_materials = obj.get_tri_materials();
397 group_list const& tris_v = obj.get_tris_v();
398 group_list const& tris_n = obj.get_tris_n();
399 group_list const& tris_tc = obj.get_tris_tc();
400 for ( i = 0; i < tris_v.size(); ++i ) {
401 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
403 nodes, normals, texcoords,
404 tris_v[i], tris_n[i], tris_tc[i],
405 is_base, ground_lights );
407 if ( use_random_objects ) {
408 SGMaterial *mat = matlib->find( tri_materials[i] );
410 SG_LOG( SG_INPUT, SG_ALERT,
411 "Unknown material for random surface objects = "
412 << tri_materials[i] );
414 gen_random_surface_objects( leaf, random_object_branch,
418 geometry->addKid( leaf );
422 string_list const& strip_materials = obj.get_strip_materials();
423 group_list const& strips_v = obj.get_strips_v();
424 group_list const& strips_n = obj.get_strips_n();
425 group_list const& strips_tc = obj.get_strips_tc();
426 for ( i = 0; i < strips_v.size(); ++i ) {
427 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
428 matlib, strip_materials[i],
429 nodes, normals, texcoords,
430 strips_v[i], strips_n[i], strips_tc[i],
431 is_base, ground_lights );
433 if ( use_random_objects ) {
434 SGMaterial *mat = matlib->find( strip_materials[i] );
436 SG_LOG( SG_INPUT, SG_ALERT,
437 "Unknown material for random surface objects = "
438 << strip_materials[i] );
440 gen_random_surface_objects( leaf, random_object_branch,
444 geometry->addKid( leaf );
448 string_list const& fan_materials = obj.get_fan_materials();
449 group_list const& fans_v = obj.get_fans_v();
450 group_list const& fans_n = obj.get_fans_n();
451 group_list const& fans_tc = obj.get_fans_tc();
452 for ( i = 0; i < fans_v.size(); ++i ) {
453 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
454 matlib, fan_materials[i],
455 nodes, normals, texcoords,
456 fans_v[i], fans_n[i], fans_tc[i],
457 is_base, ground_lights );
458 if ( use_random_objects ) {
459 SGMaterial *mat = matlib->find( fan_materials[i] );
461 SG_LOG( SG_INPUT, SG_ALERT,
462 "Unknown material for random surface objects = "
463 << fan_materials[i] );
465 gen_random_surface_objects( leaf, random_object_branch,
470 geometry->addKid( leaf );