]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/obj.cxx
Modified Files:
[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/Depth>
36 #include <osg/Geode>
37 #include <osg/Geometry>
38 #include <osg/Group>
39 #include <osg/LOD>
40 #include <osg/MatrixTransform>
41 #include <osg/StateSet>
42
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>
54
55 #include "obj.hxx"
56
57 SG_USING_STD(string);
58 SG_USING_STD(list);
59
60 struct Leaf {
61     GLenum type;
62     int index;
63 };
64
65
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 )
70 {
71     osg::StateSet *state = 0;
72
73     double tex_width = 1000.0;
74     // double tex_height;
75
76     // find Ocean material in the properties list
77     SGMaterial *mat = matlib->find( "Ocean" );
78     if ( mat != NULL ) {
79         // set the texture width and height values for this
80         // material
81         tex_width = mat->get_xsize();
82         // tex_height = newmat->get_ysize();
83         
84         // set ssgState
85         state = mat->get_state();
86     } else {
87         SG_LOG( SG_TERRAIN, SG_ALERT, 
88                 "Ack! unknown usemtl name = " << "Ocean" 
89                 << " in " << path );
90     }
91
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();
97
98     *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
99                                     clat*SGD_DEGREES_TO_RADIANS,
100                                     0.0) );
101     // cout << "center = " << center << endl;;
102     
103     // Caculate corner vertices
104     Point3D geod[4];
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 );
109
110     Point3D rad[4];
111     int i;
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,
115                           geod[i].z() );
116     }
117
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;
123     }
124
125     // Calculate bounding radius
126     *bounding_radius = center->distance3D( cart[0] );
127     // cout << "bounding radius = " << t->bounding_radius << endl;
128
129     // Calculate normals
130     Point3D normals[4];
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;
135     }
136
137     // Calculate texture coordinates
138     point_list geod_nodes;
139     geod_nodes.clear();
140     geod_nodes.reserve(4);
141     int_list rectangle;
142     rectangle.clear();
143     rectangle.reserve(4);
144     for ( i = 0; i < 4; ++i ) {
145         geod_nodes.push_back( geod[i] );
146         rectangle.push_back( i );
147     }
148     point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle, 
149                                        1000.0 / tex_width );
150
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;
155
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()));
160     }
161     
162     osg::Vec4Array* cl = new osg::Vec4Array;
163     cl->push_back(osg::Vec4(1, 1, 1, 1));
164
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);
177
178     group->addChild(geode);
179
180     return true;
181 }
182
183
184 /**
185  * SSG callback for an in-range leaf of randomly-placed objects.
186  *
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).
192  *
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.
196  */
197 // static int
198 // leaf_in_range_callback (ssgEntity * entity, int mask)
199 // {
200 //   SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
201
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);
208 //     }
209 //     data->is_filled_in = true;
210 //   }
211 //   return 1;
212 // }
213
214
215 /**
216  * SSG callback for an out-of-range leaf of randomly-placed objects.
217  *
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.
222  *
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.
226  */
227 // static int
228 // leaf_out_of_range_callback (ssgEntity * entity, int mask)
229 // {
230 //   SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
231 //   if (data->is_filled_in) {
232 //     data->branch->removeAllKids();
233 //     data->is_filled_in = false;
234 //   }
235 //   return 0;
236 // }
237
238
239 /**
240  * Randomly place objects on a surface.
241  *
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.
248  *
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.
253  */
254 static void
255 gen_random_surface_objects (osg::Node *leaf,
256                             osg::Group *branch,
257                             Point3D *center,
258                             SGMaterial *mat )
259 {
260   // OSGFIXME
261 #if 0
262                                 // If the surface has no triangles, return
263                                 // now.
264     int num_tris = leaf->getNumTriangles();
265     if (num_tris < 1)
266         return;
267
268                                 // If the material has no randomly-placed
269                                 // objects, return now.
270     if (mat->get_object_group_count() < 1)
271         return;
272
273                                 // Calculate the geodetic centre of
274                                 // the tile, for aligning automatic
275                                 // objects.
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);
279
280                                 // LOD for the leaf
281                                 // max random object range: 20000m
282     osg::LOD * lod = new osg::LOD;
283     branch->addChild(lod);
284
285                                 // Create the in-range and out-of-range
286                                 // branches.
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);
291
292     SGLeafUserData * data = new SGLeafUserData;
293     data->is_filled_in = false;
294     data->leaf = leaf;
295     data->mat = mat;
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);
301
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);
305
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);
310 //     out_of_range
311 //       ->addChild(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
312 #endif
313 }
314
315
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 {
320 public:
321 #define SCALE_FACTOR 0.94
322   virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,
323                                          osg::NodeVisitor* nv) const 
324   {
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);
336     }
337     return true;
338   }
339   virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,
340                                          osg::NodeVisitor* nv) const
341   {
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);
353     }
354     return true;
355   }
356 #undef SCALE_FACTOR
357 };
358
359
360 \f
361 ////////////////////////////////////////////////////////////////////////
362 // Scenery loaders.
363 ////////////////////////////////////////////////////////////////////////
364
365 // Load an Binary obj file
366 bool SGBinObjLoad( const string& path, const bool is_base,
367                    Point3D *center,
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 )
376 {
377     SGBinObject obj;
378
379     if ( ! obj.read_bin( path ) ) {
380         return false;
381     }
382
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;
387     if (_vasi_lights) {
388       vasi_lights = new SGLightOffsetTransform;
389       _vasi_lights->addChild(vasi_lights);
390     }
391     SGLightOffsetTransform *rwy_lights = 0;
392     if (_rwy_lights) {
393       rwy_lights = new SGLightOffsetTransform;
394       _rwy_lights->addChild(rwy_lights);
395     }
396     SGLightOffsetTransform *taxi_lights = 0;
397     if (_taxi_lights) {
398       taxi_lights = new SGLightOffsetTransform;
399       _taxi_lights->addChild(taxi_lights);
400     }
401
402     osg::Group *local_terrain = new osg::Group;
403     local_terrain->setName( "LocalTerrain" );
404     geometry->addChild( local_terrain );
405
406     geometry->setName(path);
407
408     // reference point (center offset/bounding sphere)
409     *center = obj.get_gbs_center();
410     *bounding_radius = obj.get_gbs_radius();
411
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();
416
417     string material;
418     int_list tex_index;
419
420     group_list::size_type i;
421
422     // generate points
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,
433                                                          pts_v[i], pts_n[i],
434                                                          matlib,
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" )
440             {
441                 taxi_lights->addChild( branch );
442             } else {
443                 rwy_lights->addChild( branch );
444             }
445         } else {
446             // other geometry
447             material = pt_materials[i];
448             tex_index.clear();
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 );
454         }
455     }
456
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);
467     }
468
469     typedef map<string,list<Leaf> > LeafMap;
470     LeafMap leafMap;
471     Leaf leaf;
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++ ) {
478         leaf.index = i;
479         leafMap[ tri_materials[i] ].push_back( leaf );
480     }
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++ ) {
487         leaf.index = i;
488         leafMap[ strip_materials[i] ].push_back( leaf );
489     }
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++ ) {
496         leaf.index = i;
497         leafMap[ fan_materials[i] ].push_back( leaf );
498     }
499
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() ) {
505             Leaf &leaf = *li;
506             int ind = leaf.index;
507             if ( leaf.type == GL_TRIANGLES ) {
508                 osg::Node *leaf = SGMakeLeaf( path, GL_TRIANGLES, matlib,
509                                             tri_materials[ind],
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] );
515                     if ( mat == NULL ) {
516                         SG_LOG( SG_INPUT, SG_ALERT,
517                                 "Unknown material for random surface objects = "
518                                 << tri_materials[ind] );
519                     } else {
520                         gen_random_surface_objects( leaf, random_object_branch,
521                                                     center, mat );
522                     }
523                 }
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] );
533                     if ( mat == NULL ) {
534                         SG_LOG( SG_INPUT, SG_ALERT,
535                                 "Unknown material for random surface objects = "
536                                 << strip_materials[ind] );
537                     } else {
538                         gen_random_surface_objects( leaf, random_object_branch,
539                                                     center, mat );
540                     }
541                 }
542                 local_terrain->addChild( leaf );
543             } else {
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] );
551                     if ( mat == NULL ) {
552                         SG_LOG( SG_INPUT, SG_ALERT,
553                                 "Unknown material for random surface objects = "
554                                 << fan_materials[ind] );
555                     } else {
556                         gen_random_surface_objects( leaf, random_object_branch,
557                                                     center, mat );
558                     }
559                 }
560                 local_terrain->addChild( leaf );
561             }
562             ++li;
563         }
564         ++lmi;
565     }
566
567     return true;
568 }