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.
24 #include <simgear/compiler.h>
26 #ifdef SG_MATH_EXCEPTION_CLASH
30 #include STL_FUNCTIONAL
31 #include STL_ALGORITHM
34 #include <simgear/bucket/newbucket.hxx>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/math/sg_random.h>
37 #include <simgear/misc/sgstream.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Include/general.hxx>
41 #include <Main/globals.hxx>
42 #include <Scenery/scenery.hxx>
43 #include <Time/light.hxx>
44 #include <Objects/matlib.hxx>
45 #include <Objects/newmat.hxx>
46 #include <Objects/obj.hxx>
48 #include "tileentry.hxx"
50 SG_USING_STD(for_each);
51 SG_USING_STD(mem_fun_ref);
56 FGTileEntry::FGTileEntry ( const SGBucket& b )
58 center( Point3D( 0.0 ) ),
60 terra_transform( new ssgTransform ),
61 terra_range( new ssgRangeSelector )
65 // update the contents
66 // if ( vec3_ptrs.size() || vec2_ptrs.size() || index_ptrs.size() ) {
67 // SG_LOG( SG_TERRAIN, SG_ALERT,
68 // "Attempting to overwrite existing or"
69 // << " not properly freed leaf data." );
76 FGTileEntry::~FGTileEntry () {
77 // cout << "nodes = " << nodes.size() << endl;;
82 // recurse an ssg tree and call removeKid() on every node from the
83 // bottom up. Leaves the original branch in existance, but empty so
84 // it can be removed by the calling routine.
85 static void my_remove_branch( ssgBranch * branch ) {
86 for ( ssgEntity *k = branch->getKid( 0 );
88 k = branch->getNextKid() )
90 if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
91 my_remove_branch( (ssgBranch *)k );
92 branch -> removeKid ( k );
93 } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
94 branch -> removeKid ( k ) ;
100 // Clean up the memory used by this tile and delete the arrays used by
101 // ssg as well as the whole ssg branch
102 void FGTileEntry::free_tile() {
104 SG_LOG( SG_TERRAIN, SG_DEBUG,
105 "FREEING TILE = (" << tile_bucket << ")" );
107 SG_LOG( SG_TERRAIN, SG_DEBUG,
108 " deleting " << nodes.size() << " nodes" );
111 // delete the ssg structures
112 SG_LOG( SG_TERRAIN, SG_DEBUG,
113 " deleting (leaf data) vertex, normal, and "
114 << " texture coordinate arrays" );
116 for ( i = 0; i < (int)vec3_ptrs.size(); ++i ) {
117 #if defined(macintosh) || defined(_MSC_VER)
118 delete [] vec3_ptrs[i]; //that's the correct version
125 for ( i = 0; i < (int)vec2_ptrs.size(); ++i ) {
126 #if defined(macintosh) || defined(_MSC_VER)
127 delete [] vec2_ptrs[i]; //that's the correct version
134 for ( i = 0; i < (int)index_ptrs.size(); ++i ) {
135 delete index_ptrs[i];
139 // delete the terrain branch
140 int pcount = terra_transform->getNumParents();
142 // find the first parent (should only be one)
143 ssgBranch *parent = terra_transform->getParent( 0 ) ;
145 // my_remove_branch( select_ptr );
146 parent->removeKid( terra_transform );
147 terra_transform = NULL;
149 SG_LOG( SG_TERRAIN, SG_ALERT,
150 "parent pointer is NULL! Dying" );
154 SG_LOG( SG_TERRAIN, SG_ALERT,
155 "Parent count is zero for an ssg tile! Dying" );
159 if ( lights_transform ) {
160 // delete the terrain lighting branch
161 pcount = lights_transform->getNumParents();
163 // find the first parent (should only be one)
164 ssgBranch *parent = lights_transform->getParent( 0 ) ;
166 parent->removeKid( lights_transform );
167 lights_transform = NULL;
169 SG_LOG( SG_TERRAIN, SG_ALERT,
170 "parent pointer is NULL! Dying" );
174 SG_LOG( SG_TERRAIN, SG_ALERT,
175 "Parent count is zero for an ssg light tile! Dying" );
182 // Update the ssg transform node for this tile so it can be
183 // properly drawn relative to our (0,0,0) point
184 void FGTileEntry::prep_ssg_node( const Point3D& p, float vis) {
187 // #define USE_UP_AND_COMING_PLIB_FEATURE
188 #ifdef USE_UP_AND_COMING_PLIB_FEATURE
189 terra_range->setRange( 0, SG_ZERO );
190 terra_range->setRange( 1, vis + bounding_radius );
191 lights_range->setRange( 0, SG_ZERO );
192 lights_range->setRange( 1, vis * 1.5 + bounding_radius );
196 ranges[1] = vis + bounding_radius;
197 terra_range->setRanges( ranges, 2 );
198 if ( lights_range ) {
199 ranges[1] = vis * 1.5 + bounding_radius;
200 lights_range->setRanges( ranges, 2 );
204 sgSetVec3( sgTrans, offset.x(), offset.y(), offset.z() );
205 terra_transform->setTransform( sgTrans );
207 if ( lights_transform ) {
208 // we need to lift the lights above the terrain to avoid
209 // z-buffer fighting. We do this based on our altitude and
210 // the distance this tile is away from scenery center.
213 sgCopyVec3( up, globals->get_current_view()->get_world_up() );
216 if ( current_aircraft.fdm_state ) {
217 agl = current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
223 // sgTrans just happens to be the
224 // vector from scenery center to the center of this tile which
225 // is what we want to calculate the distance of
227 sgCopyVec3( to, sgTrans );
228 double dist = sgLengthVec3( to );
230 if ( general.get_glDepthBits() > 16 ) {
231 sgScaleVec3( up, 10.0 + agl / 100.0 + dist / 10000 );
233 sgScaleVec3( up, 10.0 + agl / 20.0 + dist / 5000 );
235 sgAddVec3( sgTrans, up );
236 lights_transform->setTransform( sgTrans );
238 // select which set of lights based on sun angle
239 float sun_angle = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
240 if ( sun_angle > 95 ) {
241 lights_brightness->select(0x04);
242 } else if ( sun_angle > 92 ) {
243 lights_brightness->select(0x02);
244 } else if ( sun_angle > 89 ) {
245 lights_brightness->select(0x01);
247 lights_brightness->select(0x00);
253 ssgLeaf* FGTileEntry::gen_lights( ssgVertexArray *lights, int inc, float bright ) {
254 // generate a repeatable random seed
255 float *p1 = lights->get( 0 );
256 unsigned int *seed = (unsigned int *)p1;
259 int size = lights->getNum() / inc;
261 // Allocate ssg structure
262 ssgVertexArray *vl = new ssgVertexArray( size + 1 );
263 ssgNormalArray *nl = NULL;
264 ssgTexCoordArray *tl = NULL;
265 ssgColourArray *cl = new ssgColourArray( size + 1 );
268 for ( int i = 0; i < lights->getNum(); ++i ) {
269 // this loop is slightly less efficient than it otherwise
270 // could be, but we want a red light to always be red, and a
271 // yellow light to always be yellow, etc. so we are trying to
272 // preserve the random sequence.
273 float zombie = sg_random();
274 if ( i % inc == 0 ) {
275 vl->add( lights->get(i) );
277 // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
278 float factor = sg_random();
281 if ( zombie > 0.5 ) {
282 // 50% chance of yellowish
283 sgSetVec4( color, 0.9, 0.9, 0.3, bright - factor * 0.2 );
284 } else if ( zombie > 0.15 ) {
285 // 35% chance of whitish
286 sgSetVec4( color, 0.9, 0.9, 0.8, bright - factor * 0.2 );
287 } else if ( zombie > 0.05 ) {
288 // 10% chance of orangish
289 sgSetVec4( color, 0.9, 0.6, 0.2, bright - factor * 0.2 );
291 // 5% chance of redish
292 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
300 new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
303 FGNewMat *newmat = material_lib.find( "LIGHTS" );
304 leaf->setState( newmat->get_state() );
311 FGTileEntry::obj_load( const std::string& path,
312 ssgVertexArray* lights, bool is_base )
314 ssgBranch* result = 0;
316 // try loading binary format
317 result = fgBinObjLoad( path, this, lights, is_base );
318 if ( result == NULL ) {
319 // next try the older ascii format
320 result = fgAsciiObjLoad( path, this, lights, is_base );
321 if ( result == NULL ) {
322 // default to an ocean tile
323 result = fgGenTile( path, this );
332 FGTileEntry::load( SGPath& tile_path, bool is_base )
334 // a cheesy hack (to be fixed later)
335 extern ssgBranch *terrain;
336 extern ssgBranch *ground;
338 string index_str = tile_bucket.gen_index_str();
340 // Generate name of file to load.
341 tile_path.append( tile_bucket.gen_base_path() );
342 SGPath basename = tile_path;
343 basename.append( index_str );
344 string path = basename.str();
346 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << path );
348 // fgObjLoad will generate ground lighting for us ...
349 ssgVertexArray *light_pts = new ssgVertexArray( 100 );
351 ssgBranch* new_tile = obj_load( path, light_pts, is_base );
352 if ( new_tile != NULL ) {
353 terra_range->addKid( new_tile );
356 // load custom objects
357 SG_LOG( SG_TERRAIN, SG_DEBUG, "CUSTOM OBJECTS" );
359 SGPath index_path = tile_path;
360 index_path.append( index_str );
361 index_path.concat( ".ind" );
363 SG_LOG( SG_TERRAIN, SG_DEBUG, "Looking in " << index_path.str() );
365 sg_gzifstream in( index_path.str() );
367 if ( in.is_open() ) {
370 while ( ! in.eof() ) {
371 in >> token >> name >> ::skipws;
372 SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
373 << " name = " << name );
375 SGPath custom_path = tile_path;
376 custom_path.append( name );
377 ssgBranch *custom_obj = obj_load( custom_path.str(), NULL, false );
378 if ( (new_tile != NULL) && (custom_obj != NULL) ) {
379 new_tile -> addKid( custom_obj );
384 terra_transform->addKid( terra_range );
386 // calculate initial tile offset
387 SetOffset( scenery.center );
389 sgSetCoord( &sgcoord,
390 offset.x(), offset.y(), offset.z(),
392 terra_transform->setTransform( &sgcoord );
393 terrain->addKid( terra_transform );
395 lights_transform = NULL;
397 /* uncomment this section for testing ground lights */
398 if ( light_pts->getNum() ) {
399 SG_LOG( SG_TERRAIN, SG_DEBUG, "generating lights" );
400 lights_transform = new ssgTransform;
401 lights_range = new ssgRangeSelector;
402 lights_brightness = new ssgSelector;
405 lights = gen_lights( light_pts, 4, 0.7 );
406 lights_brightness->addKid( lights );
408 lights = gen_lights( light_pts, 2, 0.85 );
409 lights_brightness->addKid( lights );
411 lights = gen_lights( light_pts, 1, 1.0 );
412 lights_brightness->addKid( lights );
414 lights_range->addKid( lights_brightness );
415 lights_transform->addKid( lights_range );
416 lights_transform->setTransform( &sgcoord );
417 ground->addKid( lights_transform );
419 /* end of ground light section */