1 // tile.cxx -- routines to handle a scenery tile
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998, 1999 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
30 #ifdef SG_MATH_EXCEPTION_CLASH
34 #include STL_FUNCTIONAL
35 #include STL_ALGORITHM
38 #include <simgear/bucket/newbucket.hxx>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/sg_random.h>
41 #include <simgear/misc/sgstream.hxx>
43 #include <Aircraft/aircraft.hxx>
44 #include <Include/general.hxx>
45 #include <Main/globals.hxx>
46 #include <Scenery/scenery.hxx>
47 #include <Time/light.hxx>
48 #include <Objects/matlib.hxx>
49 #include <Objects/newmat.hxx>
50 #include <Objects/obj.hxx>
52 #include "tileentry.hxx"
54 SG_USING_STD(for_each);
55 SG_USING_STD(mem_fun_ref);
60 FGTileEntry::FGTileEntry ( const SGBucket& b )
62 center( Point3D( 0.0 ) ),
64 terra_transform( new ssgTransform ),
65 terra_range( new ssgRangeSelector ),
70 // update the contents
71 // if ( vec3_ptrs.size() || vec2_ptrs.size() || index_ptrs.size() ) {
72 // SG_LOG( SG_TERRAIN, SG_ALERT,
73 // "Attempting to overwrite existing or"
74 // << " not properly freed leaf data." );
81 FGTileEntry::~FGTileEntry () {
82 // cout << "nodes = " << nodes.size() << endl;;
87 // recurse an ssg tree and call removeKid() on every node from the
88 // bottom up. Leaves the original branch in existance, but empty so
89 // it can be removed by the calling routine.
90 static void my_remove_branch( ssgBranch * branch ) {
91 for ( ssgEntity *k = branch->getKid( 0 );
93 k = branch->getNextKid() )
95 if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
96 my_remove_branch( (ssgBranch *)k );
97 branch -> removeKid ( k );
98 } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
99 branch -> removeKid ( k ) ;
105 // Clean up the memory used by this tile and delete the arrays used by
106 // ssg as well as the whole ssg branch
107 void FGTileEntry::free_tile() {
109 SG_LOG( SG_TERRAIN, SG_INFO,
110 "FREEING TILE = (" << tile_bucket << ")" );
112 SG_LOG( SG_TERRAIN, SG_DEBUG,
113 " deleting " << nodes.size() << " nodes" );
116 // delete the ssg structures
117 SG_LOG( SG_TERRAIN, SG_DEBUG,
118 " deleting (leaf data) vertex, normal, and "
119 << " texture coordinate arrays" );
121 for ( i = 0; i < (int)vec3_ptrs.size(); ++i ) {
122 delete [] vec3_ptrs[i];
126 for ( i = 0; i < (int)vec2_ptrs.size(); ++i ) {
127 delete [] vec2_ptrs[i];
131 for ( i = 0; i < (int)index_ptrs.size(); ++i ) {
132 delete index_ptrs[i];
136 // delete the terrain branch
137 int pcount = terra_transform->getNumParents();
139 // find the first parent (should only be one)
140 ssgBranch *parent = terra_transform->getParent( 0 ) ;
142 // my_remove_branch( select_ptr );
143 parent->removeKid( terra_transform );
144 terra_transform = NULL;
146 SG_LOG( SG_TERRAIN, SG_ALERT,
147 "parent pointer is NULL! Dying" );
151 SG_LOG( SG_TERRAIN, SG_ALERT,
152 "Parent count is zero for an ssg tile! Dying" );
156 if ( lights_transform ) {
157 // delete the terrain lighting branch
158 pcount = lights_transform->getNumParents();
160 // find the first parent (should only be one)
161 ssgBranch *parent = lights_transform->getParent( 0 ) ;
163 parent->removeKid( lights_transform );
164 lights_transform = NULL;
166 SG_LOG( SG_TERRAIN, SG_ALERT,
167 "parent pointer is NULL! Dying" );
171 SG_LOG( SG_TERRAIN, SG_ALERT,
172 "Parent count is zero for an ssg light tile! Dying" );
179 // Update the ssg transform node for this tile so it can be
180 // properly drawn relative to our (0,0,0) point
181 void FGTileEntry::prep_ssg_node( const Point3D& p, float vis) {
182 if ( !loaded ) return;
186 // #define USE_UP_AND_COMING_PLIB_FEATURE
187 #ifdef USE_UP_AND_COMING_PLIB_FEATURE
188 terra_range->setRange( 0, SG_ZERO );
189 terra_range->setRange( 1, vis + bounding_radius );
190 lights_range->setRange( 0, SG_ZERO );
191 lights_range->setRange( 1, vis * 1.5 + bounding_radius );
195 ranges[1] = vis + bounding_radius;
196 terra_range->setRanges( ranges, 2 );
197 if ( lights_range ) {
198 ranges[1] = vis * 1.5 + bounding_radius;
199 lights_range->setRanges( ranges, 2 );
203 sgSetVec3( sgTrans, offset.x(), offset.y(), offset.z() );
204 terra_transform->setTransform( sgTrans );
206 if ( lights_transform ) {
207 // we need to lift the lights above the terrain to avoid
208 // z-buffer fighting. We do this based on our altitude and
209 // the distance this tile is away from scenery center.
212 sgCopyVec3( up, globals->get_current_view()->get_world_up() );
215 if ( current_aircraft.fdm_state ) {
216 agl = current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
222 // sgTrans just happens to be the
223 // vector from scenery center to the center of this tile which
224 // is what we want to calculate the distance of
226 sgCopyVec3( to, sgTrans );
227 double dist = sgLengthVec3( to );
229 if ( general.get_glDepthBits() > 16 ) {
230 sgScaleVec3( up, 10.0 + agl / 100.0 + dist / 10000 );
232 sgScaleVec3( up, 10.0 + agl / 20.0 + dist / 5000 );
234 sgAddVec3( sgTrans, up );
235 lights_transform->setTransform( sgTrans );
237 // select which set of lights based on sun angle
238 float sun_angle = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
239 if ( sun_angle > 95 ) {
240 lights_brightness->select(0x04);
241 } else if ( sun_angle > 92 ) {
242 lights_brightness->select(0x02);
243 } else if ( sun_angle > 89 ) {
244 lights_brightness->select(0x01);
246 lights_brightness->select(0x00);
252 ssgLeaf* FGTileEntry::gen_lights( ssgVertexArray *lights, int inc, float bright ) {
253 // generate a repeatable random seed
254 float *p1 = lights->get( 0 );
255 unsigned int *seed = (unsigned int *)p1;
258 int size = lights->getNum() / inc;
260 // Allocate ssg structure
261 ssgVertexArray *vl = new ssgVertexArray( size + 1 );
262 ssgNormalArray *nl = NULL;
263 ssgTexCoordArray *tl = NULL;
264 ssgColourArray *cl = new ssgColourArray( size + 1 );
267 for ( int i = 0; i < lights->getNum(); ++i ) {
268 // this loop is slightly less efficient than it otherwise
269 // could be, but we want a red light to always be red, and a
270 // yellow light to always be yellow, etc. so we are trying to
271 // preserve the random sequence.
272 float zombie = sg_random();
273 if ( i % inc == 0 ) {
274 vl->add( lights->get(i) );
276 // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
277 float factor = sg_random();
280 if ( zombie > 0.5 ) {
281 // 50% chance of yellowish
282 sgSetVec4( color, 0.9, 0.9, 0.3, bright - factor * 0.2 );
283 } else if ( zombie > 0.15 ) {
284 // 35% chance of whitish
285 sgSetVec4( color, 0.9, 0.9, 0.8, bright - factor * 0.2 );
286 } else if ( zombie > 0.05 ) {
287 // 10% chance of orangish
288 sgSetVec4( color, 0.9, 0.6, 0.2, bright - factor * 0.2 );
290 // 5% chance of redish
291 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
299 new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
302 FGNewMat *newmat = material_lib.find( "LIGHTS" );
303 leaf->setState( newmat->get_state() );
310 FGTileEntry::obj_load( const std::string& path,
311 ssgVertexArray* lights, bool is_base )
313 ssgBranch* result = 0;
315 // try loading binary format
316 result = fgBinObjLoad( path, this, lights, is_base );
317 if ( result == NULL ) {
318 // next try the older ascii format
319 result = fgAsciiObjLoad( path, this, lights, is_base );
320 if ( result == NULL ) {
321 // default to an ocean tile
322 result = fgGenTile( path, this );
331 FGTileEntry::load( const SGPath& base, bool is_base )
333 string index_str = tile_bucket.gen_index_str();
335 SGPath tile_path = base;
336 // Generate name of file to load.
337 tile_path.append( tile_bucket.gen_base_path() );
338 SGPath basename = tile_path;
339 basename.append( index_str );
340 string path = basename.str();
342 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << path );
344 // fgObjLoad will generate ground lighting for us ...
345 ssgVertexArray *light_pts = new ssgVertexArray( 100 );
347 ssgBranch* new_tile = obj_load( path, light_pts, is_base );
348 if ( new_tile != NULL ) {
349 terra_range->addKid( new_tile );
352 // load custom objects
353 SG_LOG( SG_TERRAIN, SG_DEBUG, "CUSTOM OBJECTS" );
355 SGPath index_path = tile_path;
356 index_path.append( index_str );
357 index_path.concat( ".ind" );
359 SG_LOG( SG_TERRAIN, SG_DEBUG, "Looking in " << index_path.str() );
361 sg_gzifstream in( index_path.str() );
363 if ( in.is_open() ) {
366 while ( ! in.eof() ) {
367 in >> token >> name >> ::skipws;
368 SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
369 << " name = " << name );
371 SGPath custom_path = tile_path;
372 custom_path.append( name );
373 ssgBranch *custom_obj = obj_load( custom_path.str(), NULL, false );
374 if ( (new_tile != NULL) && (custom_obj != NULL) ) {
375 new_tile -> addKid( custom_obj );
380 terra_transform->addKid( terra_range );
382 // calculate initial tile offset
383 SetOffset( scenery.center );
385 sgSetCoord( &sgcoord,
386 offset.x(), offset.y(), offset.z(),
388 terra_transform->setTransform( &sgcoord );
389 // terrain->addKid( terra_transform );
391 lights_transform = NULL;
393 /* uncomment this section for testing ground lights */
394 if ( light_pts->getNum() ) {
395 SG_LOG( SG_TERRAIN, SG_DEBUG, "generating lights" );
396 lights_transform = new ssgTransform;
397 lights_range = new ssgRangeSelector;
398 lights_brightness = new ssgSelector;
401 lights = gen_lights( light_pts, 4, 0.7 );
402 lights_brightness->addKid( lights );
404 lights = gen_lights( light_pts, 2, 0.85 );
405 lights_brightness->addKid( lights );
407 lights = gen_lights( light_pts, 1, 1.0 );
408 lights_brightness->addKid( lights );
410 lights_range->addKid( lights_brightness );
411 lights_transform->addKid( lights_range );
412 lights_transform->setTransform( &sgcoord );
413 // ground->addKid( lights_transform );
415 /* end of ground light section */
419 FGTileEntry::add_ssg_nodes( ssgBranch* terrain, ssgBranch* ground )
421 terrain->addKid( terra_transform );
422 if (lights_transform != 0)
423 ground->addKid( lights_transform );