]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/obj.cxx
363c8910a8eb792920a518222ebcec4944e700e1
[simgear.git] / simgear / scene / tgdb / obj.cxx
1 // obj.cxx -- routines to handle loading scenery and building the plib
2 //            scene graph.
3 //
4 // Written by Curtis Olson, started October 1997.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <simgear_config.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #include <list>
32
33 #include STL_STRING
34
35 #include <osg/StateSet>
36 #include <osg/Geode>
37 #include <osg/Geometry>
38 #include <osg/Group>
39 #include <osg/LOD>
40
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>
51
52 #include "obj.hxx"
53
54 SG_USING_STD(string);
55 SG_USING_STD(list);
56
57 struct Leaf {
58     GLenum type;
59     int index;
60 };
61
62
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 )
67 {
68     osg::StateSet *state = 0;
69
70     double tex_width = 1000.0;
71     // double tex_height;
72
73     // find Ocean material in the properties list
74     SGMaterial *mat = matlib->find( "Ocean" );
75     if ( mat != NULL ) {
76         // set the texture width and height values for this
77         // material
78         tex_width = mat->get_xsize();
79         // tex_height = newmat->get_ysize();
80         
81         // set ssgState
82         state = mat->get_state();
83     } else {
84         SG_LOG( SG_TERRAIN, SG_ALERT, 
85                 "Ack! unknown usemtl name = " << "Ocean" 
86                 << " in " << path );
87     }
88
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();
94
95     *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
96                                     clat*SGD_DEGREES_TO_RADIANS,
97                                     0.0) );
98     // cout << "center = " << center << endl;;
99     
100     // Caculate corner vertices
101     Point3D geod[4];
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 );
106
107     Point3D rad[4];
108     int i;
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,
112                           geod[i].z() );
113     }
114
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;
120     }
121
122     // Calculate bounding radius
123     *bounding_radius = center->distance3D( cart[0] );
124     // cout << "bounding radius = " << t->bounding_radius << endl;
125
126     // Calculate normals
127     Point3D normals[4];
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;
132     }
133
134     // Calculate texture coordinates
135     point_list geod_nodes;
136     geod_nodes.clear();
137     geod_nodes.reserve(4);
138     int_list rectangle;
139     rectangle.clear();
140     rectangle.reserve(4);
141     for ( i = 0; i < 4; ++i ) {
142         geod_nodes.push_back( geod[i] );
143         rectangle.push_back( i );
144     }
145     point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle, 
146                                        1000.0 / tex_width );
147
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;
152
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()));
157     }
158     
159     osg::Vec4Array* cl = new osg::Vec4Array;
160     cl->push_back(osg::Vec4(1, 1, 1, 1));
161
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);
174
175     group->addChild(geode);
176
177     return true;
178 }
179
180
181 /**
182  * SSG callback for an in-range leaf of randomly-placed objects.
183  *
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).
189  *
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.
193  */
194 // static int
195 // leaf_in_range_callback (ssgEntity * entity, int mask)
196 // {
197 //   SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
198
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);
205 //     }
206 //     data->is_filled_in = true;
207 //   }
208 //   return 1;
209 // }
210
211
212 /**
213  * SSG callback for an out-of-range leaf of randomly-placed objects.
214  *
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.
219  *
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.
223  */
224 // static int
225 // leaf_out_of_range_callback (ssgEntity * entity, int mask)
226 // {
227 //   SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
228 //   if (data->is_filled_in) {
229 //     data->branch->removeAllKids();
230 //     data->is_filled_in = false;
231 //   }
232 //   return 0;
233 // }
234
235
236 /**
237  * Randomly place objects on a surface.
238  *
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.
245  *
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.
250  */
251 static void
252 gen_random_surface_objects (osg::Node *leaf,
253                             osg::Group *branch,
254                             Point3D *center,
255                             SGMaterial *mat )
256 {
257   // OSGFIXME
258 #if 0
259                                 // If the surface has no triangles, return
260                                 // now.
261     int num_tris = leaf->getNumTriangles();
262     if (num_tris < 1)
263         return;
264
265                                 // If the material has no randomly-placed
266                                 // objects, return now.
267     if (mat->get_object_group_count() < 1)
268         return;
269
270                                 // Calculate the geodetic centre of
271                                 // the tile, for aligning automatic
272                                 // objects.
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);
276
277                                 // LOD for the leaf
278                                 // max random object range: 20000m
279     osg::LOD * lod = new osg::LOD;
280     branch->addChild(lod);
281
282                                 // Create the in-range and out-of-range
283                                 // branches.
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);
288
289     SGLeafUserData * data = new SGLeafUserData;
290     data->is_filled_in = false;
291     data->leaf = leaf;
292     data->mat = mat;
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);
298
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);
302
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);
307 //     out_of_range
308 //       ->addChild(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
309 #endif
310 }
311
312
313 \f
314 ////////////////////////////////////////////////////////////////////////
315 // Scenery loaders.
316 ////////////////////////////////////////////////////////////////////////
317
318 // Load an Binary obj file
319 bool SGBinObjLoad( const string& path, const bool is_base,
320                    Point3D *center,
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 )
329 {
330     SGBinObject obj;
331
332     if ( ! obj.read_bin( path ) ) {
333         return false;
334     }
335
336     osg::Group *local_terrain = new osg::Group;
337     local_terrain->setName( "LocalTerrain" );
338     geometry->addChild( local_terrain );
339
340     geometry->setName(path);
341
342     // reference point (center offset/bounding sphere)
343     *center = obj.get_gbs_center();
344     *bounding_radius = obj.get_gbs_radius();
345
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();
350
351     string material;
352     int_list tex_index;
353
354     group_list::size_type i;
355
356     // generate points
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,
367                                                          pts_v[i], pts_n[i],
368                                                          matlib,
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" )
374             {
375                 taxi_lights->addChild( branch );
376             } else {
377                 rwy_lights->addChild( branch );
378             }
379         } else {
380             // other geometry
381             material = pt_materials[i];
382             tex_index.clear();
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 );
388         }
389     }
390
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);
401     }
402
403     typedef map<string,list<Leaf> > LeafMap;
404     LeafMap leafMap;
405     Leaf leaf;
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++ ) {
412         leaf.index = i;
413         leafMap[ tri_materials[i] ].push_back( leaf );
414     }
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++ ) {
421         leaf.index = i;
422         leafMap[ strip_materials[i] ].push_back( leaf );
423     }
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++ ) {
430         leaf.index = i;
431         leafMap[ fan_materials[i] ].push_back( leaf );
432     }
433
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() ) {
439             Leaf &leaf = *li;
440             int ind = leaf.index;
441             if ( leaf.type == GL_TRIANGLES ) {
442                 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLES, matlib,
443                                             tri_materials[ind],
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] );
449                     if ( mat == NULL ) {
450                         SG_LOG( SG_INPUT, SG_ALERT,
451                                 "Unknown material for random surface objects = "
452                                 << tri_materials[ind] );
453                     } else {
454                         gen_random_surface_objects( leaf, random_object_branch,
455                                                     center, mat );
456                     }
457                 }
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] );
467                     if ( mat == NULL ) {
468                         SG_LOG( SG_INPUT, SG_ALERT,
469                                 "Unknown material for random surface objects = "
470                                 << strip_materials[ind] );
471                     } else {
472                         gen_random_surface_objects( leaf, random_object_branch,
473                                                     center, mat );
474                     }
475                 }
476                 local_terrain->addChild( leaf );
477             } else {
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] );
485                     if ( mat == NULL ) {
486                         SG_LOG( SG_INPUT, SG_ALERT,
487                                 "Unknown material for random surface objects = "
488                                 << fan_materials[ind] );
489                     } else {
490                         gen_random_surface_objects( leaf, random_object_branch,
491                                                     center, mat );
492                     }
493                 }
494                 local_terrain->addChild( leaf );
495             }
496             ++li;
497         }
498         ++lmi;
499     }
500
501     return true;
502 }