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.
25 // #ifdef HAVE_CONFIG_H
26 // # include <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>
43 #include "userdata.hxx"
50 // Generate an ocean tile
51 bool fgGenTile( const string& path, SGBucket b,
52 Point3D *center, double *bounding_radius,
53 SGMaterialLib *matlib, ssgBranch* geometry )
55 ssgSimpleState *state = NULL;
57 geometry->setName( (char *)path.c_str() );
59 double tex_width = 1000.0;
62 // find Ocean material in the properties list
63 SGMaterial *mat = matlib->find( "Ocean" );
65 // set the texture width and height values for this
67 tex_width = mat->get_xsize();
68 // tex_height = newmat->get_ysize();
71 state = mat->get_state();
73 SG_LOG( SG_TERRAIN, SG_ALERT,
74 "Ack! unknown usemtl name = " << "Ocean"
78 // Calculate center point
79 double clon = b.get_center_lon();
80 double clat = b.get_center_lat();
81 double height = b.get_height();
82 double width = b.get_width();
84 *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
85 clat*SGD_DEGREES_TO_RADIANS,
87 // cout << "center = " << center << endl;;
89 // Caculate corner vertices
91 geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
92 geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
93 geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
94 geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
98 for ( i = 0; i < 4; ++i ) {
99 rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
100 geod[i].y() * SGD_DEGREES_TO_RADIANS,
104 Point3D cart[4], rel[4];
105 for ( i = 0; i < 4; ++i ) {
106 cart[i] = sgGeodToCart(rad[i]);
107 rel[i] = cart[i] - *center;
108 // cout << "corner " << i << " = " << cart[i] << endl;
111 // Calculate bounding radius
112 *bounding_radius = center->distance3D( cart[0] );
113 // cout << "bounding radius = " << t->bounding_radius << endl;
117 for ( i = 0; i < 4; ++i ) {
118 double length = cart[i].distance3D( Point3D(0.0) );
119 normals[i] = cart[i] / length;
120 // cout << "normal = " << normals[i] << endl;
123 // Calculate texture coordinates
124 point_list geod_nodes;
126 geod_nodes.reserve(4);
129 rectangle.reserve(4);
130 for ( i = 0; i < 4; ++i ) {
131 geod_nodes.push_back( geod[i] );
132 rectangle.push_back( i );
134 point_list texs = calc_tex_coords( b, geod_nodes, rectangle,
135 1000.0 / tex_width );
137 // Allocate ssg structure
138 ssgVertexArray *vl = new ssgVertexArray( 4 );
139 ssgNormalArray *nl = new ssgNormalArray( 4 );
140 ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
141 ssgColourArray *cl = new ssgColourArray( 1 );
144 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
147 // sgVec3 *vtlist = new sgVec3 [ 4 ];
148 // t->vec3_ptrs.push_back( vtlist );
149 // sgVec3 *vnlist = new sgVec3 [ 4 ];
150 // t->vec3_ptrs.push_back( vnlist );
151 // sgVec2 *tclist = new sgVec2 [ 4 ];
152 // t->vec2_ptrs.push_back( tclist );
156 for ( i = 0; i < 4; ++i ) {
158 rel[i].x(), rel[i].y(), rel[i].z() );
162 normals[i].x(), normals[i].y(), normals[i].z() );
165 sgSetVec2( tmp2, texs[i].x(), texs[i].y());
170 new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
172 leaf->setState( state );
174 geometry->addKid( leaf );
181 * SSG callback for an in-range leaf of randomly-placed objects.
183 * This pretraversal callback is attached to a branch that is
184 * traversed only when a leaf is in range. If the leaf is not
185 * currently prepared to be populated with randomly-placed objects,
186 * this callback will prepare it (actual population is handled by
187 * the tri_in_range_callback for individual triangles).
189 * @param entity The entity to which the callback is attached (not used).
190 * @param mask The entity's traversal mask (not used).
191 * @return Always 1, to allow traversal and culling to continue.
194 leaf_in_range_callback (ssgEntity * entity, int mask)
196 LeafUserData * data = (LeafUserData *)entity->getUserData();
198 if (!data->is_filled_in) {
199 // Iterate through all the triangles
200 // and populate them.
201 int num_tris = data->leaf->getNumTriangles();
202 for ( int i = 0; i < num_tris; ++i ) {
203 data->setup_triangle(i);
205 data->is_filled_in = true;
212 * SSG callback for an out-of-range leaf of randomly-placed objects.
214 * This pretraversal callback is attached to a branch that is
215 * traversed only when a leaf is out of range. If the leaf is
216 * currently prepared to be populated with randomly-placed objects (or
217 * is actually populated), the objects will be removed.
219 * @param entity The entity to which the callback is attached (not used).
220 * @param mask The entity's traversal mask (not used).
221 * @return Always 0, to prevent any further traversal or culling.
224 leaf_out_of_range_callback (ssgEntity * entity, int mask)
226 LeafUserData * data = (LeafUserData *)entity->getUserData();
227 if (data->is_filled_in) {
228 data->branch->removeAllKids();
229 data->is_filled_in = false;
236 * Randomly place objects on a surface.
238 * The leaf node provides the geometry of the surface, while the
239 * material provides the objects and placement density. Latitude
240 * and longitude are required so that the objects can be rotated
241 * to the world-up vector. This function does not actually add
242 * any objects; instead, it attaches an ssgRangeSelector to the
243 * branch with callbacks to generate the objects when needed.
245 * @param leaf The surface where the objects should be placed.
246 * @param branch The branch that will hold the randomly-placed objects.
247 * @param center The center of the leaf in FlightGear coordinates.
248 * @param material_name The name of the surface's material.
251 gen_random_surface_objects (ssgLeaf *leaf,
256 // If the surface has no triangles, return
258 int num_tris = leaf->getNumTriangles();
262 // If the material has no randomly-placed
263 // objects, return now.
264 if (mat->get_object_group_count() < 1)
267 // Calculate the geodetic centre of
268 // the tile, for aligning automatic
270 double lon_deg, lat_rad, lat_deg, alt_m, sl_radius_m;
271 Point3D geoc = sgCartToPolar3d(*center);
272 lon_deg = geoc.lon() * SGD_RADIANS_TO_DEGREES;
273 sgGeocToGeod(geoc.lat(), geoc.radius(),
274 &lat_rad, &alt_m, &sl_radius_m);
275 lat_deg = lat_rad * SGD_RADIANS_TO_DEGREES;
278 // max random object range: 20000m
279 float ranges[] = { 0, 20000, 1000000 };
280 ssgRangeSelector * lod = new ssgRangeSelector;
281 lod->setRanges(ranges, 3);
284 // Create the in-range and out-of-range
286 ssgBranch * in_range = new ssgBranch;
287 ssgBranch * out_of_range = new ssgBranch;
288 lod->addKid(in_range);
289 lod->addKid(out_of_range);
291 LeafUserData * data = new LeafUserData;
292 data->is_filled_in = false;
295 data->branch = in_range;
296 data->sin_lat = sin(lat_deg * SGD_DEGREES_TO_RADIANS);
297 data->cos_lat = cos(lat_deg * SGD_DEGREES_TO_RADIANS);
298 data->sin_lon = sin(lon_deg * SGD_DEGREES_TO_RADIANS);
299 data->cos_lon = cos(lon_deg * SGD_DEGREES_TO_RADIANS);
301 in_range->setUserData(data);
302 in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
303 out_of_range->setUserData(data);
304 out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
305 leaf_out_of_range_callback);
307 ->addKid(new DummyBSphereEntity(leaf->getBSphere()->getRadius()));
312 ////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////
316 // Load an Binary obj file
317 bool fgBinObjLoad( const string& path, const bool is_base,
319 double *bounding_radius,
320 SGMaterialLib *matlib,
321 bool use_random_objects,
323 ssgBranch* rwy_lights,
324 ssgBranch* taxi_lights,
325 ssgVertexArray *ground_lights )
329 if ( ! obj.read_bin( path ) ) {
333 geometry->setName( (char *)path.c_str() );
335 // reference point (center offset/bounding sphere)
336 *center = obj.get_gbs_center();
337 *bounding_radius = obj.get_gbs_radius();
339 point_list const& nodes = obj.get_wgs84_nodes();
340 // point_list const& colors = obj.get_colors();
341 point_list const& normals = obj.get_normals();
342 point_list const& texcoords = obj.get_texcoords();
347 group_list::size_type i;
350 string_list const& pt_materials = obj.get_pt_materials();
351 group_list const& pts_v = obj.get_pts_v();
352 group_list const& pts_n = obj.get_pts_n();
353 for ( i = 0; i < pts_v.size(); ++i ) {
354 // cout << "pts_v.size() = " << pts_v.size() << endl;
355 if ( pt_materials[i].substr(0, 3) == "RWY" ) {
357 sgSetVec3( up, center->x(), center->y(), center->z() );
358 // returns a transform -> lod -> leaf structure
359 ssgBranch *branch = sgMakeDirectionalLights( nodes, normals,
362 pt_materials[i], up );
363 if ( pt_materials[i].substr(0, 16) == "RWY_BLUE_TAXIWAY" ) {
364 taxi_lights->addKid( branch );
366 rwy_lights->addKid( branch );
369 material = pt_materials[i];
371 ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
372 nodes, normals, texcoords,
373 pts_v[i], pts_n[i], tex_index,
374 false, ground_lights );
375 geometry->addKid( leaf );
379 // Put all randomly-placed objects under a separate branch
380 // (actually an ssgRangeSelector) named "random-models".
381 ssgBranch * random_object_branch = 0;
382 if (use_random_objects) {
383 float ranges[] = { 0, 20000 }; // Maximum 20km range for random objects
384 ssgRangeSelector * object_lod = new ssgRangeSelector;
385 object_lod->setRanges(ranges, 2);
386 object_lod->setName("random-models");
387 geometry->addKid(object_lod);
388 random_object_branch = new ssgBranch;
389 object_lod->addKid(random_object_branch);
392 // generate triangles
393 string_list const& tri_materials = obj.get_tri_materials();
394 group_list const& tris_v = obj.get_tris_v();
395 group_list const& tris_n = obj.get_tris_n();
396 group_list const& tris_tc = obj.get_tris_tc();
397 for ( i = 0; i < tris_v.size(); ++i ) {
398 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
400 nodes, normals, texcoords,
401 tris_v[i], tris_n[i], tris_tc[i],
402 is_base, ground_lights );
404 if ( use_random_objects ) {
405 SGMaterial *mat = matlib->find( tri_materials[i] );
407 SG_LOG( SG_INPUT, SG_ALERT,
408 "Unknown material for random surface objects = "
409 << tri_materials[i] );
411 gen_random_surface_objects( leaf, random_object_branch,
414 geometry->addKid( leaf );
418 string_list const& strip_materials = obj.get_strip_materials();
419 group_list const& strips_v = obj.get_strips_v();
420 group_list const& strips_n = obj.get_strips_n();
421 group_list const& strips_tc = obj.get_strips_tc();
422 for ( i = 0; i < strips_v.size(); ++i ) {
423 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
424 matlib, strip_materials[i],
425 nodes, normals, texcoords,
426 strips_v[i], strips_n[i], strips_tc[i],
427 is_base, ground_lights );
429 if ( use_random_objects ) {
430 SGMaterial *mat = matlib->find( strip_materials[i] );
432 SG_LOG( SG_INPUT, SG_ALERT,
433 "Unknown material for random surface objects = "
434 << strip_materials[i] );
436 gen_random_surface_objects( leaf, random_object_branch,
439 geometry->addKid( leaf );
443 string_list const& fan_materials = obj.get_fan_materials();
444 group_list const& fans_v = obj.get_fans_v();
445 group_list const& fans_n = obj.get_fans_n();
446 group_list const& fans_tc = obj.get_fans_tc();
447 for ( i = 0; i < fans_v.size(); ++i ) {
448 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
449 matlib, fan_materials[i],
450 nodes, normals, texcoords,
451 fans_v[i], fans_n[i], fans_tc[i],
452 is_base, ground_lights );
453 if ( use_random_objects ) {
454 SGMaterial *mat = matlib->find( fan_materials[i] );
456 SG_LOG( SG_INPUT, SG_ALERT,
457 "Unknown material for random surface objects = "
458 << fan_materials[i] );
460 gen_random_surface_objects( leaf, random_object_branch,
463 geometry->addKid( leaf );