]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/obj.cxx
Check for the plib version when using display lists, just to be sure.
[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  - curt@infoplane.com
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., 675 Mass Ave, Cambridge, MA 02139, 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 <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>
45
46 #include "obj.hxx"
47
48 SG_USING_STD(string);
49 SG_USING_STD(list);
50
51 struct Leaf {
52     GLenum type;
53     int index;
54 };
55
56
57 // Generate an ocean tile
58 bool sgGenTile( const string& path, SGBucket b,
59                 Point3D *center, double *bounding_radius,
60                 SGMaterialLib *matlib, ssgBranch* geometry )
61 {
62     ssgSimpleState *state = NULL;
63
64     geometry->setName( (char *)path.c_str() );
65
66     double tex_width = 1000.0;
67     // double tex_height;
68
69     // find Ocean material in the properties list
70     SGMaterial *mat = matlib->find( "Ocean" );
71     if ( mat != NULL ) {
72         // set the texture width and height values for this
73         // material
74         tex_width = mat->get_xsize();
75         // tex_height = newmat->get_ysize();
76         
77         // set ssgState
78         state = mat->get_state();
79     } else {
80         SG_LOG( SG_TERRAIN, SG_ALERT, 
81                 "Ack! unknown usemtl name = " << "Ocean" 
82                 << " in " << path );
83     }
84
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();
90
91     *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
92                                     clat*SGD_DEGREES_TO_RADIANS,
93                                     0.0) );
94     // cout << "center = " << center << endl;;
95     
96     // Caculate corner vertices
97     Point3D geod[4];
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 );
102
103     Point3D rad[4];
104     int i;
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,
108                           geod[i].z() );
109     }
110
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;
116     }
117
118     // Calculate bounding radius
119     *bounding_radius = center->distance3D( cart[0] );
120     // cout << "bounding radius = " << t->bounding_radius << endl;
121
122     // Calculate normals
123     Point3D normals[4];
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;
128     }
129
130     // Calculate texture coordinates
131     point_list geod_nodes;
132     geod_nodes.clear();
133     geod_nodes.reserve(4);
134     int_list rectangle;
135     rectangle.clear();
136     rectangle.reserve(4);
137     for ( i = 0; i < 4; ++i ) {
138         geod_nodes.push_back( geod[i] );
139         rectangle.push_back( i );
140     }
141     point_list texs = sgCalcTexCoords( b, geod_nodes, rectangle, 
142                                        1000.0 / tex_width );
143
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 );
149
150     sgVec4 color;
151     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
152     cl->add( color );
153
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 );
160
161     sgVec2 tmp2;
162     sgVec3 tmp3;
163     for ( i = 0; i < 4; ++i ) {
164         sgSetVec3( tmp3, 
165                    rel[i].x(), rel[i].y(), rel[i].z() );
166         vl->add( tmp3 );
167
168         sgSetVec3( tmp3, 
169                    normals[i].x(), normals[i].y(), normals[i].z() );
170         nl->add( tmp3 );
171
172         sgSetVec2( tmp2, texs[i].x(), texs[i].y());
173         tl->add( tmp2 );
174     }
175     
176     ssgLeaf *leaf = 
177         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
178
179     leaf->setState( state );
180 #if PLIB_VERSION > 183
181     leaf->makeDList();
182 #endif
183
184     geometry->addKid( leaf );
185
186     return true;
187 }
188
189
190 /**
191  * SSG callback for an in-range leaf of randomly-placed objects.
192  *
193  * This pretraversal callback is attached to a branch that is
194  * traversed only when a leaf is in range.  If the leaf is not
195  * currently prepared to be populated with randomly-placed objects,
196  * this callback will prepare it (actual population is handled by
197  * the tri_in_range_callback for individual triangles).
198  *
199  * @param entity The entity to which the callback is attached (not used).
200  * @param mask The entity's traversal mask (not used).
201  * @return Always 1, to allow traversal and culling to continue.
202  */
203 static int
204 leaf_in_range_callback (ssgEntity * entity, int mask)
205 {
206   SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
207
208   if (!data->is_filled_in) {
209                                 // Iterate through all the triangles
210                                 // and populate them.
211     int num_tris = data->leaf->getNumTriangles();
212     for ( int i = 0; i < num_tris; ++i ) {
213             data->setup_triangle(i);
214     }
215     data->is_filled_in = true;
216   }
217   return 1;
218 }
219
220
221 /**
222  * SSG callback for an out-of-range leaf of randomly-placed objects.
223  *
224  * This pretraversal callback is attached to a branch that is
225  * traversed only when a leaf is out of range.  If the leaf is
226  * currently prepared to be populated with randomly-placed objects (or
227  * is actually populated), the objects will be removed.
228  *
229  * @param entity The entity to which the callback is attached (not used).
230  * @param mask The entity's traversal mask (not used).
231  * @return Always 0, to prevent any further traversal or culling.
232  */
233 static int
234 leaf_out_of_range_callback (ssgEntity * entity, int mask)
235 {
236   SGLeafUserData * data = (SGLeafUserData *)entity->getUserData();
237   if (data->is_filled_in) {
238     data->branch->removeAllKids();
239     data->is_filled_in = false;
240   }
241   return 0;
242 }
243
244
245 /**
246  * Randomly place objects on a surface.
247  *
248  * The leaf node provides the geometry of the surface, while the
249  * material provides the objects and placement density.  Latitude
250  * and longitude are required so that the objects can be rotated
251  * to the world-up vector.  This function does not actually add
252  * any objects; instead, it attaches an ssgRangeSelector to the
253  * branch with callbacks to generate the objects when needed.
254  *
255  * @param leaf The surface where the objects should be placed.
256  * @param branch The branch that will hold the randomly-placed objects.
257  * @param center The center of the leaf in FlightGear coordinates.
258  * @param material_name The name of the surface's material.
259  */
260 static void
261 gen_random_surface_objects (ssgLeaf *leaf,
262                             ssgBranch *branch,
263                             Point3D *center,
264                             SGMaterial *mat )
265 {
266                                 // If the surface has no triangles, return
267                                 // now.
268     int num_tris = leaf->getNumTriangles();
269     if (num_tris < 1)
270         return;
271
272                                 // If the material has no randomly-placed
273                                 // objects, return now.
274     if (mat->get_object_group_count() < 1)
275         return;
276
277                                 // Calculate the geodetic centre of
278                                 // the tile, for aligning automatic
279                                 // objects.
280     double xyz[3], lon_rad, lat_rad, alt_m;
281     xyz[0] = center->x(); xyz[1] = center->y(); xyz[2] = center->z();
282     sgCartToGeod(xyz, &lat_rad, &lon_rad, &alt_m);
283
284                                 // LOD for the leaf
285                                 // max random object range: 20000m
286     float ranges[] = { 0, 20000, 1000000 };
287     ssgRangeSelector * lod = new ssgRangeSelector;
288     lod->setRanges(ranges, 3);
289     branch->addKid(lod);
290
291                                 // Create the in-range and out-of-range
292                                 // branches.
293     ssgBranch * in_range = new ssgBranch;
294     ssgBranch * out_of_range = new ssgBranch;
295     lod->addKid(in_range);
296     lod->addKid(out_of_range);
297
298     SGLeafUserData * data = new SGLeafUserData;
299     data->is_filled_in = false;
300     data->leaf = leaf;
301     data->mat = mat;
302     data->branch = in_range;
303     data->sin_lat = sin(lat_rad);
304     data->cos_lat = cos(lat_rad);
305     data->sin_lon = sin(lon_rad);
306     data->cos_lon = cos(lon_rad);
307
308     in_range->setUserData(data);
309     in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
310     out_of_range->setUserData(data);
311     out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
312                                    leaf_out_of_range_callback);
313     out_of_range
314       ->addKid(new SGDummyBSphereEntity(leaf->getBSphere()->getRadius()));
315 }
316
317
318 \f
319 ////////////////////////////////////////////////////////////////////////
320 // Scenery loaders.
321 ////////////////////////////////////////////////////////////////////////
322
323 // Load an Binary obj file
324 bool sgBinObjLoad( const string& path, const bool is_base,
325                    Point3D *center,
326                    double *bounding_radius,
327                    SGMaterialLib *matlib,
328                    bool use_random_objects,
329                    ssgBranch *geometry,
330                    ssgBranch *vasi_lights,
331                    ssgBranch *rwy_lights,
332                    ssgBranch *taxi_lights,
333                    ssgVertexArray *ground_lights )
334 {
335     SGBinObject obj;
336
337     if ( ! obj.read_bin( path ) ) {
338         return false;
339     }
340
341     ssgBranch *local_terrain = new ssgBranch;
342     local_terrain->setName( "LocalTerrain" );
343     geometry->addKid( local_terrain );
344
345     geometry->setName( (char *)path.c_str() );
346
347     // reference point (center offset/bounding sphere)
348     *center = obj.get_gbs_center();
349     *bounding_radius = obj.get_gbs_radius();
350
351     point_list const& nodes = obj.get_wgs84_nodes();
352     // point_list const& colors = obj.get_colors();
353     point_list const& normals = obj.get_normals();
354     point_list const& texcoords = obj.get_texcoords();
355
356     string material;
357     int_list tex_index;
358
359     group_list::size_type i;
360
361     // generate points
362     string_list const& pt_materials = obj.get_pt_materials();
363     group_list const& pts_v = obj.get_pts_v();
364     group_list const& pts_n = obj.get_pts_n();
365     for ( i = 0; i < pts_v.size(); ++i ) {
366         // cout << "pts_v.size() = " << pts_v.size() << endl;
367         if ( pt_materials[i].substr(0, 3) == "RWY" ) {
368             // airport environment lighting
369             sgdVec3 up;
370             sgdSetVec3( up, center->x(), center->y(), center->z() );
371             // returns a transform -> lod -> leaf structure
372             ssgBranch *branch = sgMakeDirectionalLights( nodes, normals,
373                                                          pts_v[i], pts_n[i],
374                                                          matlib,
375                                                          pt_materials[i], up );
376             if ( pt_materials[i] == "RWY_VASI_LIGHTS" ) {
377                 vasi_lights->addKid( branch );
378             } else if ( pt_materials[i] == "RWY_BLUE_TAXIWAY_LIGHTS"
379                 || pt_materials[i] == "RWY_GREEN_TAXIWAY_LIGHTS" )
380             {
381                 taxi_lights->addKid( branch );
382             } else {
383                 rwy_lights->addKid( branch );
384             }
385         } else {
386             // other geometry
387             material = pt_materials[i];
388             tex_index.clear();
389             ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
390                                         nodes, normals, texcoords,
391                                         pts_v[i], pts_n[i], tex_index,
392                                         false, ground_lights );
393             local_terrain->addKid( leaf );
394         }
395     }
396
397     // Put all randomly-placed objects under a separate branch
398     // (actually an ssgRangeSelector) named "random-models".
399     ssgBranch * random_object_branch = 0;
400     if (use_random_objects) {
401         float ranges[] = { 0, 20000 }; // Maximum 20km range for random objects
402         ssgRangeSelector * object_lod = new ssgRangeSelector;
403         object_lod->setRanges(ranges, 2);
404         object_lod->setName("random-models");
405         geometry->addKid(object_lod);
406         random_object_branch = new ssgBranch;
407         object_lod->addKid(random_object_branch);
408     }
409
410     typedef map<string,list<Leaf> > LeafMap;
411     LeafMap leafMap;
412     Leaf leaf;
413     leaf.type = GL_TRIANGLES;
414     string_list const& tri_materials = obj.get_tri_materials();
415     group_list const& tris_v = obj.get_tris_v();
416     group_list const& tris_n = obj.get_tris_n();
417     group_list const& tris_tc = obj.get_tris_tc();
418     for ( i = 0; i < tris_v.size(); i++ ) {
419         leaf.index = i;
420         leafMap[ tri_materials[i] ].push_back( leaf );
421     }
422     leaf.type = GL_TRIANGLE_STRIP;
423     string_list const& strip_materials = obj.get_strip_materials();
424     group_list const& strips_v = obj.get_strips_v();
425     group_list const& strips_n = obj.get_strips_n();
426     group_list const& strips_tc = obj.get_strips_tc();
427     for ( i = 0; i < strips_v.size(); i++ ) {
428         leaf.index = i;
429         leafMap[ strip_materials[i] ].push_back( leaf );
430     }
431     leaf.type = GL_TRIANGLE_FAN;
432     string_list const& fan_materials = obj.get_fan_materials();
433     group_list const& fans_v = obj.get_fans_v();
434     group_list const& fans_n = obj.get_fans_n();
435     group_list const& fans_tc = obj.get_fans_tc();
436     for ( i = 0; i < fans_v.size(); i++ ) {
437         leaf.index = i;
438         leafMap[ fan_materials[i] ].push_back( leaf );
439     }
440
441     LeafMap::iterator lmi = leafMap.begin();
442     while ( lmi != leafMap.end() ) {
443         list<Leaf> &leaf_list = lmi->second;
444         list<Leaf>::iterator li = leaf_list.begin();
445         while ( li != leaf_list.end() ) {
446             Leaf &leaf = *li;
447             int ind = leaf.index;
448             if ( leaf.type == GL_TRIANGLES ) {
449                 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
450                                             tri_materials[ind],
451                                             nodes, normals, texcoords,
452                                             tris_v[ind], tris_n[ind], tris_tc[ind],
453                                             is_base, ground_lights );
454                 if ( use_random_objects ) {
455                     SGMaterial *mat = matlib->find( tri_materials[ind] );
456                     if ( mat == NULL ) {
457                         SG_LOG( SG_INPUT, SG_ALERT,
458                                 "Unknown material for random surface objects = "
459                                 << tri_materials[ind] );
460                     } else {
461                         gen_random_surface_objects( leaf, random_object_branch,
462                                                     center, mat );
463                     }
464                 }
465                 local_terrain->addKid( leaf );
466             } else if ( leaf.type == GL_TRIANGLE_STRIP ) {
467                 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
468                                             matlib, strip_materials[ind],
469                                             nodes, normals, texcoords,
470                                             strips_v[ind], strips_n[ind], strips_tc[ind],
471                                             is_base, ground_lights );
472                 if ( use_random_objects ) {
473                     SGMaterial *mat = matlib->find( strip_materials[ind] );
474                     if ( mat == NULL ) {
475                         SG_LOG( SG_INPUT, SG_ALERT,
476                                 "Unknown material for random surface objects = "
477                                 << strip_materials[ind] );
478                     } else {
479                         gen_random_surface_objects( leaf, random_object_branch,
480                                                     center, mat );
481                     }
482                 }
483                 local_terrain->addKid( leaf );
484             } else {
485                 ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
486                                             matlib, fan_materials[ind],
487                                             nodes, normals, texcoords,
488                                             fans_v[ind], fans_n[ind], fans_tc[ind],
489                                             is_base, ground_lights );
490                 if ( use_random_objects ) {
491                     SGMaterial *mat = matlib->find( fan_materials[ind] );
492                     if ( mat == NULL ) {
493                         SG_LOG( SG_INPUT, SG_ALERT,
494                                 "Unknown material for random surface objects = "
495                                 << fan_materials[ind] );
496                     } else {
497                         gen_random_surface_objects( leaf, random_object_branch,
498                                                     center, mat );
499                     }
500                 }
501                 local_terrain->addKid( leaf );
502             }
503             ++li;
504         }
505         ++lmi;
506     }
507
508     return true;
509 }