| 1 | /*************************************************************************** |
| 2 | renderer.cpp - description |
| 3 | ------------------- |
| 4 | begin : may 29th, 2003 |
| 5 | copyright : (C) 2003-2008 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: renderer.cpp 450 2010-11-21 19:11:43Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | //======================================================================== |
| 21 | /* General OpenGL development notes. The subroutines must save and restore |
| 22 | that default states when they terminate. |
| 23 | |
| 24 | Here are the default OpenGL states when the renderer is initialized |
| 25 | for the first time: |
| 26 | - Default matrix mode: |
| 27 | glMatrixMode( GL_MODELVIEW ); |
| 28 | |
| 29 | - Default color mode: |
| 30 | glEnable( GL_COLOR_MATERIAL ); |
| 31 | |
| 32 | - Default blending function: |
| 33 | glDisable( GL_BLEND ); |
| 34 | glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); |
| 35 | |
| 36 | - Default shade mode: |
| 37 | glShadeMode( GL_FLAT ); |
| 38 | |
| 39 | - Default structure display translation vector: |
| 40 | glTranslatef( 0., 0.05, 0. ); |
| 41 | |
| 42 | - Default lighting: |
| 43 | glEnable( GL_LIGHTING ); |
| 44 | glEnable( GL_LIGHT0 ); |
| 45 | |
| 46 | - Default culling mode: |
| 47 | glDisable( GL_CULL_FACE ); |
| 48 | glCullFace( GL_BACK ); |
| 49 | glFrontFace( GL_CCW ); |
| 50 | */ |
| 51 | //======================================================================== |
| 52 | |
| 53 | #include <climits> // need for UINT_MAX |
| 54 | |
| 55 | // Useful enumerations |
| 56 | #include "opencity_direction.h" |
| 57 | #include "opencity_structure_type.h" |
| 58 | |
| 59 | // OpenCity headers |
| 60 | #include "renderer.h" |
| 61 | #include "layer.h" |
| 62 | #include "font_8x8.h" // 8x8 font definition |
| 63 | #include "model.h" // Display list mask definitions |
| 64 | #include "structure.h" |
| 65 | |
| 66 | /* Test, dec 28th, 06 |
| 67 | #include "font_mini_4x6.h" |
| 68 | #include "font_7x14.h" |
| 69 | #include "font_acorn_8x8.h" |
| 70 | #include "font_pearl_8x8.h" |
| 71 | */ |
| 72 | |
| 73 | // Global settings |
| 74 | #include "globalvar.h" |
| 75 | extern GlobalVar gVars; |
| 76 | |
| 77 | |
| 78 | // OpenGL viewport default parameters |
| 79 | #define OC_VIEW_ANGLE 50.0 |
| 80 | #define OC_Z_NEAR 1.0 |
| 81 | #define OC_Z_FAR 400.0 |
| 82 | #define OC_Z_NEAR_ORTHO -10000.0 |
| 83 | #define OC_Z_FAR_ORTHO 1000.0 |
| 84 | |
| 85 | // OpenGL other view parameters |
| 86 | #define OC_INITIAL_SCALE 12.0 |
| 87 | #define OC_INITIAL_DELTA_X -25.0 // used for the translation |
| 88 | #define OC_INITIAL_DELTA_Z -30.0 |
| 89 | #define OC_DELTA_X_STEP 2.0 |
| 90 | #define OC_DELTA_Z_STEP 2.0 |
| 91 | |
| 92 | #define OC_Y_ROTATE_ANGLE 0.0 // used for the rotation |
| 93 | #define OC_Y_ROTATE_STEP 10.0 |
| 94 | |
| 95 | // OpenGL projection type paramaters |
| 96 | #define OC_PERSPECTIVE 1 |
| 97 | #define OC_ORTHOGONAL 2 |
| 98 | |
| 99 | |
| 100 | /*=====================================================================*/ |
| 101 | Renderer::Renderer |
| 102 | ( |
| 103 | const uint cityW, |
| 104 | const uint cityL |
| 105 | ): |
| 106 | bHeightChange( true ), |
| 107 | bMinimapChange( true ), |
| 108 | _bDisplayTerrain( true ), |
| 109 | _bDisplayWater( true ), |
| 110 | _bDisplayStructure( true ), |
| 111 | _bDisplayGrid( true ), |
| 112 | _bDisplayCompass( true ), |
| 113 | _bWireFrame( false ), |
| 114 | _ubProjectionType( OC_PERSPECTIVE ), |
| 115 | _uiMinimapTex( 0 ), |
| 116 | _bCalculateCulling( true ), |
| 117 | _uiCityWidth( cityW ), |
| 118 | _uiCityLength( cityL ) |
| 119 | { |
| 120 | OPENCITY_DEBUG( "Renderer ctor" ); |
| 121 | |
| 122 | // Load frequently used textures |
| 123 | moTextureCloud = Texture( ocDataDirPrefix( "graphism/texture/cloud.png" )); |
| 124 | moTextureTerrain = Texture( ocDataDirPrefix( "graphism/texture/terrain_64x4096_texture.png" ), true); |
| 125 | moTextureWater = Texture( ocDataDirPrefix( "graphism/texture/blue_water_512.png" )); |
| 126 | |
| 127 | // Initialize the culled grid |
| 128 | uint size = (_uiCityWidth+1) * (_uiCityLength+1); |
| 129 | _baCulledGrid = new bool[size]; |
| 130 | for ( uint i = 0; i < size; i++ ) { |
| 131 | _baCulledGrid[i] = true; |
| 132 | } |
| 133 | |
| 134 | // Initialize the culled models grid |
| 135 | size = _uiCityWidth * _uiCityLength; |
| 136 | _baCulledModel = new bool[size]; |
| 137 | for ( uint i = 0; i < size; i++ ) { |
| 138 | _baCulledModel[i] = false; |
| 139 | } |
| 140 | |
| 141 | // Initialize the window's size, the viewport |
| 142 | // and set the perspective's ratio |
| 143 | assert( gVars.gpVideoSrf != NULL ); |
| 144 | Renderer::SetWinSize( gVars.gpVideoSrf->w, gVars.gpVideoSrf->h ); |
| 145 | |
| 146 | // Settle "home" down ;) |
| 147 | this->Home(); |
| 148 | |
| 149 | /* not need for the moment |
| 150 | //--- translate all the scence according to _dDeltaX & _dDeltaZ |
| 151 | glTranslated( _dDeltaX, 0.0, _dDeltaZ ); |
| 152 | |
| 153 | //--- rotate the scence to the required angle |
| 154 | glRotated( _dYRotateAngle, 0.0, 1.0, 0.0 ); |
| 155 | */ |
| 156 | |
| 157 | |
| 158 | // Enable polygon culling. This can effect the texture alpha blending |
| 159 | // glEnable( GL_CULL_FACE ); |
| 160 | glCullFace( GL_BACK ); |
| 161 | glFrontFace( GL_CCW ); |
| 162 | |
| 163 | |
| 164 | // Select the flat rendering mode, default is GL_SMOOTH |
| 165 | glShadeModel( GL_FLAT ); |
| 166 | |
| 167 | |
| 168 | //debug testing |
| 169 | //the maximal number of lights we can set is: 8 |
| 170 | /* GLint nbLight; |
| 171 | glGetIntegerv( GL_MAX_LIGHTS, &nbLight ); |
| 172 | cout << "max lights : " << (int) nbLight << endl; |
| 173 | */ |
| 174 | //debug testing end |
| 175 | |
| 176 | // Create 8x8 font |
| 177 | glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); |
| 178 | this->_uiFontBase = glGenLists( 256 ); |
| 179 | for ( uint i = 0; i < 256; i++ ) { |
| 180 | glNewList( this->_uiFontBase + i, GL_COMPILE ); |
| 181 | glBitmap( 8, 8, .0, .0, 10., .0, fontdata_8x8 + i*8 ); |
| 182 | /* Test, dec 28th, 06 |
| 183 | // glBitmap( 8, 8, .0, .0, 10., .0, fontdata_acorn_8x8 + i*8 ); |
| 184 | // glBitmap( 8, 8, .0, .0, 10., .0, fontdata_pearl_8x8 + i*8 ); |
| 185 | // glBitmap( 7, 14, .0, .0, 9., .0, fontdata_7x14 + i*14 ); |
| 186 | // glBitmap( 4, 6, .0, .0, 6., .0, fontdata_mini_4x6 + i*6 ); |
| 187 | */ |
| 188 | glEndList(); |
| 189 | } |
| 190 | |
| 191 | // initialize few display lists |
| 192 | _uiGridList = glGenLists( 1 ); |
| 193 | _uiTerrainList = glGenLists( 1 ); |
| 194 | _uiWaterList = glGenLists( 1 ); |
| 195 | |
| 196 | // Define the global ambient light value |
| 197 | // NOTE: we declare the variables here for better readable codes |
| 198 | GLfloat fvLightModelAmbient[] = { OC_LIGHT_MODEL_AMBIENT }; |
| 199 | GLfloat fvLightAmbient [] = { .6, .6, .4, 1. }; |
| 200 | GLfloat fvLightDiffuse [] = { .8, .8, .6, 1. }; |
| 201 | GLfloat fvLightSpecular [] = { .4, .4, .4, 1. }; |
| 202 | |
| 203 | glLightModelfv( GL_LIGHT_MODEL_AMBIENT, fvLightModelAmbient ); |
| 204 | glLightfv( GL_LIGHT0, GL_AMBIENT, fvLightAmbient ); |
| 205 | glLightfv( GL_LIGHT0, GL_DIFFUSE, fvLightDiffuse ); |
| 206 | glLightfv( GL_LIGHT0, GL_SPECULAR, fvLightSpecular ); |
| 207 | glLighti( GL_LIGHT0, GL_SPOT_CUTOFF, 180 ); |
| 208 | |
| 209 | // Enable lighting |
| 210 | // WARNING: the light position is transformed by the current MODELVIEW matrix |
| 211 | static GLint ivLightPos [] = { 0, 2, 1, 0}; // Directional light |
| 212 | static GLint ivLightDir [] = { 0, 2, 0 }; |
| 213 | glLightiv( GL_LIGHT0, GL_POSITION, ivLightPos ); |
| 214 | glLightiv( GL_LIGHT0, GL_SPOT_DIRECTION, ivLightDir ); |
| 215 | |
| 216 | glEnable( GL_LIGHTING ); |
| 217 | glEnable( GL_LIGHT0 ); |
| 218 | |
| 219 | // Enable the GL automatic normals rescaling it's useful when the user zooms |
| 220 | // in and out ( zooming is uniform here, in OpenCity) |
| 221 | // The surface's normal needs to be unit length |
| 222 | glEnable( GL_RESCALE_NORMAL ); |
| 223 | // GL_NORMALIZE is replaced by the RESCALE_NORMAL provided by OpenGL 1.2 |
| 224 | // glEnable( GL_NORMALIZE ); |
| 225 | |
| 226 | // Allow the material to track glColor() |
| 227 | // WARNING: glEnable( GL_COLOR_MATERIAL ) tracks immediatly the current color |
| 228 | // The default settings are GL_FRONT_AND_BACK and GL_AMBIENT_AND_DIFFUSE |
| 229 | glColor4ub( 255, 255, 255, 255 ); |
| 230 | glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE ); |
| 231 | glEnable( GL_COLOR_MATERIAL ); |
| 232 | |
| 233 | // This can help improving texture lighting |
| 234 | // glLightModelf( GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR ); |
| 235 | |
| 236 | // Enable depth test |
| 237 | glDepthFunc( GL_LEQUAL ); |
| 238 | glEnable( GL_DEPTH_TEST ); |
| 239 | |
| 240 | // Choose the right blending function |
| 241 | glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /*=====================================================================*/ |
| 246 | Renderer::~Renderer( ) |
| 247 | { |
| 248 | OPENCITY_DEBUG("dtor"); |
| 249 | |
| 250 | // Free GL list |
| 251 | glDeleteLists( _uiFontBase, 256 ); |
| 252 | |
| 253 | if (glIsList( _uiGridList )) |
| 254 | glDeleteLists( _uiGridList, 1 ); |
| 255 | if (glIsList( _uiTerrainList )) |
| 256 | glDeleteLists( _uiTerrainList, 1 ); |
| 257 | if (glIsList( _uiWaterList )) |
| 258 | glDeleteLists( _uiWaterList, 1 ); |
| 259 | |
| 260 | // Free textures |
| 261 | glDeleteTextures( 1, &_uiMinimapTex ); |
| 262 | |
| 263 | // Free the culled grid and the models grid |
| 264 | delete [] _baCulledModel; |
| 265 | delete [] _baCulledGrid; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /*=====================================================================*/ |
| 270 | void |
| 271 | Renderer::RotateLeft( const uint & factor ) |
| 272 | { |
| 273 | GLdouble dYCurrentAngle = OC_Y_ROTATE_STEP*factor; |
| 274 | _dYRotateAngle = (GLint)(_dYRotateAngle + dYCurrentAngle) % 360; |
| 275 | |
| 276 | // Toggle to the model view matrix |
| 277 | glMatrixMode( GL_MODELVIEW ); |
| 278 | glLoadIdentity(); |
| 279 | |
| 280 | // Rotate the scence to the required angle |
| 281 | glRotated( dYCurrentAngle, 0.0, 1.0, 0.0 ); |
| 282 | |
| 283 | // Translate all the scence according to _dDeltaX & _dDeltaZ |
| 284 | glTranslated( _dDeltaX, 0.0, _dDeltaZ ); |
| 285 | |
| 286 | // Apply the new changes to the old rotation's matrix |
| 287 | glMultMatrixd( _dmatrixRotate ); |
| 288 | |
| 289 | // Clear the translation |
| 290 | _dDeltaX = 0.0; |
| 291 | _dDeltaZ = 0.0; |
| 292 | |
| 293 | // Save the calculated rotation matrix |
| 294 | glGetDoublev( GL_MODELVIEW_MATRIX, _dmatrixRotate ); |
| 295 | |
| 296 | // The culling grid must be recalculated |
| 297 | _bCalculateCulling = true; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /*=====================================================================*/ |
| 302 | void |
| 303 | Renderer::RotateRight( const uint & factor ) |
| 304 | { |
| 305 | GLdouble dYCurrentAngle = -OC_Y_ROTATE_STEP*factor; |
| 306 | _dYRotateAngle = (GLint)(_dYRotateAngle + dYCurrentAngle) % 360; |
| 307 | |
| 308 | // Toggle to the model view matrix |
| 309 | glMatrixMode( GL_MODELVIEW ); |
| 310 | glLoadIdentity(); |
| 311 | |
| 312 | // Rotate the scence to the required angle |
| 313 | glRotated( dYCurrentAngle, 0.0, 1.0, 0.0 ); |
| 314 | |
| 315 | // Translate all the scence according to _dDeltaX & _dDeltaZ |
| 316 | glTranslated( _dDeltaX, 0.0, _dDeltaZ ); |
| 317 | |
| 318 | // Apply the new changes to the old rotation's matrix |
| 319 | glMultMatrixd( _dmatrixRotate ); |
| 320 | |
| 321 | // Clear the translation |
| 322 | _dDeltaX = 0.0; |
| 323 | _dDeltaZ = 0.0; |
| 324 | |
| 325 | // Save the calculated rotation matrix |
| 326 | glGetDoublev( GL_MODELVIEW_MATRIX, _dmatrixRotate ); |
| 327 | |
| 328 | // The culling grid must be recalculated |
| 329 | _bCalculateCulling = true; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | /*=====================================================================*/ |
| 334 | void |
| 335 | Renderer::MoveLeft( const uint & factor ) |
| 336 | { |
| 337 | _dDeltaX -= _fXTransDelta*factor; |
| 338 | |
| 339 | // The culling grid must be recalculated |
| 340 | _bCalculateCulling = true; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /*=====================================================================*/ |
| 345 | void |
| 346 | Renderer::MoveRight( const uint & factor ) |
| 347 | { |
| 348 | _dDeltaX += _fXTransDelta*factor; |
| 349 | |
| 350 | // The culling grid must be recalculated |
| 351 | _bCalculateCulling = true; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /*=====================================================================*/ |
| 356 | void |
| 357 | Renderer::MoveUp( const uint & factor ) |
| 358 | { |
| 359 | _dDeltaZ -= _fZTransDelta*factor; |
| 360 | |
| 361 | // The culling grid must be recalculated |
| 362 | _bCalculateCulling = true; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /*=====================================================================*/ |
| 367 | void |
| 368 | Renderer::MoveDown( const uint & factor ) |
| 369 | { |
| 370 | _dDeltaZ += _fZTransDelta*factor; |
| 371 | |
| 372 | // The culling grid must be recalculated |
| 373 | _bCalculateCulling = true; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /*=====================================================================*/ |
| 378 | void |
| 379 | Renderer::Home() |
| 380 | { |
| 381 | _fScaleRatio = OC_INITIAL_SCALE; |
| 382 | _fXTransDelta = OC_DELTA_X_STEP; |
| 383 | _fZTransDelta = OC_DELTA_Z_STEP; |
| 384 | _dDeltaX = OC_INITIAL_DELTA_X; |
| 385 | _dDeltaZ = OC_INITIAL_DELTA_Z; |
| 386 | _dYRotateAngle = OC_Y_ROTATE_ANGLE; |
| 387 | |
| 388 | // Reinit the rotation matrix |
| 389 | glMatrixMode( GL_MODELVIEW ); |
| 390 | glLoadIdentity(); |
| 391 | glGetDoublev( GL_MODELVIEW_MATRIX, _dmatrixRotate ); |
| 392 | |
| 393 | // Reset the LOD |
| 394 | _SetLOD(); |
| 395 | |
| 396 | // The culling grid must be recalculated |
| 397 | _bCalculateCulling = true; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /*=====================================================================*/ |
| 402 | void |
| 403 | Renderer::ZoomIn( ) |
| 404 | { |
| 405 | if (_fScaleRatio >= 140) |
| 406 | return; |
| 407 | |
| 408 | _fScaleRatio += 1; |
| 409 | if (_fScaleRatio < 30) { |
| 410 | _fXTransDelta -= .09; |
| 411 | _fZTransDelta -= .09; |
| 412 | } |
| 413 | _SetLOD(); |
| 414 | |
| 415 | // The culling grid must be recalculated |
| 416 | _bCalculateCulling = true; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | /*=====================================================================*/ |
| 421 | void Renderer::ZoomOut( ) |
| 422 | { |
| 423 | if (_fScaleRatio <= 2) |
| 424 | return; |
| 425 | |
| 426 | _fScaleRatio -= 1; |
| 427 | if (_fScaleRatio < 30) { |
| 428 | _fXTransDelta += .09; |
| 429 | _fZTransDelta += .09; |
| 430 | } |
| 431 | _SetLOD(); |
| 432 | |
| 433 | // The culling grid must be recalculated |
| 434 | _bCalculateCulling = true; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | /*=====================================================================*/ |
| 439 | void |
| 440 | Renderer::ToggleTerrain() |
| 441 | { |
| 442 | _bDisplayTerrain = !_bDisplayTerrain; |
| 443 | |
| 444 | // If we turn the terrain display back on, we need to update the display list |
| 445 | if (_bDisplayTerrain) |
| 446 | bHeightChange = true; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /*=====================================================================*/ |
| 451 | void |
| 452 | Renderer::ToggleWater() |
| 453 | { |
| 454 | _bDisplayWater = !_bDisplayWater; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /*=====================================================================*/ |
| 459 | void |
| 460 | Renderer::ToggleStructure() |
| 461 | { |
| 462 | _bDisplayStructure = !_bDisplayStructure; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /*=====================================================================*/ |
| 467 | void |
| 468 | Renderer::ToggleGrid() |
| 469 | { |
| 470 | _bDisplayGrid = !_bDisplayGrid; |
| 471 | |
| 472 | // If we turn the grid display back on, we need to update the display list |
| 473 | if (_bDisplayGrid) |
| 474 | bHeightChange = true; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /*=====================================================================*/ |
| 479 | void |
| 480 | Renderer::ToggleCompass() |
| 481 | { |
| 482 | _bDisplayCompass = !_bDisplayCompass; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /*=====================================================================*/ |
| 487 | void |
| 488 | Renderer::ToggleProjection() |
| 489 | { |
| 490 | if (_ubProjectionType == OC_PERSPECTIVE) { |
| 491 | _ubProjectionType = OC_ORTHOGONAL; |
| 492 | } |
| 493 | else { |
| 494 | _ubProjectionType = OC_PERSPECTIVE; |
| 495 | } |
| 496 | |
| 497 | // Reinit the projection matrix; |
| 498 | SetWinSize( _iWinWidth, _iWinHeight ); |
| 499 | |
| 500 | // The culling grid must be recalculated |
| 501 | _bCalculateCulling = true; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | /*=====================================================================*/ |
| 506 | void |
| 507 | Renderer::ToggleWireFrame() |
| 508 | { |
| 509 | _bWireFrame = !_bWireFrame; |
| 510 | if (_bWireFrame) { |
| 511 | //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); |
| 512 | glPolygonMode( GL_FRONT, GL_LINE ); |
| 513 | glPolygonMode( GL_BACK, GL_POINT ); |
| 514 | } |
| 515 | else { |
| 516 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
| 521 | /*=====================================================================*/ |
| 522 | void |
| 523 | Renderer::DisplaySplash( |
| 524 | const uint& rcuiX, |
| 525 | const uint& rcuiY ) |
| 526 | { |
| 527 | static uint w, h; |
| 528 | |
| 529 | if (!glIsTexture(moTextureSplash.GetName())) { |
| 530 | moTextureSplash = Texture( ocDataDirPrefix( "graphism/gui/splash.png") ); |
| 531 | moTextureSplash.GetSize( w, h ); |
| 532 | } |
| 533 | |
| 534 | // Store and translate the splash to the specified OpenGL coordinates |
| 535 | glPushAttrib( GL_ENABLE_BIT ); |
| 536 | glDisable( GL_LIGHTING ); |
| 537 | glEnable( GL_BLEND ); |
| 538 | glEnable( GL_TEXTURE_2D ); |
| 539 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); |
| 540 | glBindTexture( GL_TEXTURE_2D, moTextureSplash.GetName() ); |
| 541 | |
| 542 | glPushMatrix(); |
| 543 | glLoadIdentity(); |
| 544 | glTranslatef( rcuiX + (_iWinWidth-w) / 2, rcuiY + (_iWinHeight-h) / 2, 0 ); |
| 545 | glMatrixMode( GL_PROJECTION ); |
| 546 | glPushMatrix(); |
| 547 | glLoadIdentity(); |
| 548 | gluOrtho2D( 0, _iWinWidth-1, 0, _iWinHeight-1 ); |
| 549 | |
| 550 | // Display the textured quad |
| 551 | glBegin( GL_QUADS ); |
| 552 | glTexCoord2i( 0, 0 ); glVertex2i( 1, 1 ); |
| 553 | glTexCoord2i( 0, 1 ); glVertex2i( 1, h ); |
| 554 | glTexCoord2i( 1, 1 ); glVertex2i( w, h ); |
| 555 | glTexCoord2i( 1, 0 ); glVertex2i( w, 1 ); |
| 556 | glEnd(); |
| 557 | |
| 558 | // Retore the old matrix and attribs |
| 559 | glPopMatrix(); |
| 560 | glMatrixMode( GL_MODELVIEW ); |
| 561 | glPopMatrix(); |
| 562 | glPopAttrib(); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | /*=====================================================================*/ |
| 567 | void |
| 568 | Renderer::Display |
| 569 | ( |
| 570 | const Map* pcMap, |
| 571 | const Layer* pcLayer |
| 572 | ) |
| 573 | { |
| 574 | static uint linear, oldmask, newmask; |
| 575 | static uint w, l; |
| 576 | static const Structure* pStructure; |
| 577 | |
| 578 | // Clear the color buffer ( the screen ) and the depth buffer |
| 579 | glClearColor( OC_CLEAR_COLOR ); |
| 580 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 581 | |
| 582 | // Prepare the world for rendering: calculate the view volume, culling |
| 583 | _PrepareView(); |
| 584 | |
| 585 | // Calculate the culling grid and the culled models if it's requested |
| 586 | if (_bCalculateCulling) { |
| 587 | _CalculateCulledGrid( 0, 0, _uiCityWidth+1, _uiCityLength+1, true); |
| 588 | _bCalculateCulling = false; |
| 589 | } |
| 590 | _CalculateCulledModel( pcLayer ); |
| 591 | |
| 592 | // IF the graphic manager is created AND display structure is requested |
| 593 | // THEN display all _opaque_ structures built on the layer |
| 594 | if (_bDisplayStructure && gVars.gpGraphicMgr != NULL) { |
| 595 | // Build the opaque display list mask |
| 596 | oldmask = gVars.gpGraphicMgr->GetListMask(); |
| 597 | newmask = oldmask & ( UINT_MAX xor OC_ALPHA_LIST); |
| 598 | gVars.gpGraphicMgr->SetListMask( newmask ); |
| 599 | |
| 600 | // Display the structures |
| 601 | glPushMatrix(); |
| 602 | glTranslatef( 0., 0.05, 0. ); |
| 603 | linear = 0; |
| 604 | for (l = 0; l < _uiCityLength; l++) { |
| 605 | for (w = 0; w < _uiCityWidth; w++, linear++) { |
| 606 | // IF the model is not selected then continue |
| 607 | if (!_baCulledModel[linear]) |
| 608 | continue; |
| 609 | |
| 610 | // The structure is not NULL here (see _CalculateCulledModel) |
| 611 | pStructure = pcLayer->GetLinearStructure( linear ); |
| 612 | assert( pStructure != NULL ); |
| 613 | gVars.gpGraphicMgr->DisplayStructure( pStructure, w, l ); |
| 614 | } |
| 615 | } |
| 616 | glPopMatrix(); |
| 617 | } |
| 618 | |
| 619 | // Displays the grids of the map if the user wants it |
| 620 | // it is translated up a little along the Oz axis |
| 621 | if (_bDisplayGrid) { |
| 622 | _DisplayMapGrid( pcMap ); |
| 623 | } |
| 624 | |
| 625 | // Calculate the minimap |
| 626 | // NOTE: IF the compass is not displayed THEN it is not necessary to calculate the minimap |
| 627 | if (bMinimapChange && _bDisplayCompass) { |
| 628 | Texture::Building2Texture( (BuildingLayer*)pcLayer, _uiMinimapTex ); |
| 629 | bMinimapChange = false; |
| 630 | } |
| 631 | |
| 632 | // Draw the compass and the cloud |
| 633 | _DisplayCompass(); |
| 634 | |
| 635 | // Call the private method to display the height map |
| 636 | if (_bDisplayTerrain) { |
| 637 | _DisplayTerrain(); |
| 638 | } |
| 639 | |
| 640 | // Display the water texture |
| 641 | if (_bDisplayWater) { |
| 642 | _DisplayWater(); |
| 643 | } |
| 644 | |
| 645 | // IF the graphic manager is created AND display structure is requested |
| 646 | // THEN display all _alpha_ structures built on the layer |
| 647 | if (_bDisplayStructure && gVars.gpGraphicMgr != NULL) { |
| 648 | // oldmask = gVars.gpGraphicMgr->GetListMask(); // previously set |
| 649 | newmask = oldmask & OC_ALPHA_LIST; |
| 650 | gVars.gpGraphicMgr->SetListMask( newmask ); |
| 651 | |
| 652 | // Display the structures |
| 653 | glPushMatrix(); |
| 654 | glTranslatef( 0., 0.05, 0. ); |
| 655 | linear = 0; |
| 656 | for (l = 0; l < _uiCityLength; l++) { |
| 657 | for (w = 0; w < _uiCityWidth; w++, linear++) { |
| 658 | // IF the model is not selected then continue |
| 659 | if (!_baCulledModel[linear]) |
| 660 | continue; |
| 661 | |
| 662 | // The structure is not NULL here (see _CalculateCulledModel) |
| 663 | pStructure = pcLayer->GetLinearStructure( linear ); |
| 664 | assert( pStructure != NULL ); |
| 665 | gVars.gpGraphicMgr->DisplayStructure( pStructure, w, l ); |
| 666 | } |
| 667 | } |
| 668 | glPopMatrix(); |
| 669 | |
| 670 | // Restore the old display list mask |
| 671 | gVars.gpGraphicMgr->SetListMask( oldmask ); |
| 672 | } |
| 673 | |
| 674 | // The height map changes have been memorized in the grid and the terrain displaylist |
| 675 | bHeightChange = false; |
| 676 | |
| 677 | // GL error checking |
| 678 | glFlush(); |
| 679 | static GLint glerr = glGetError(); |
| 680 | if (glerr != GL_NO_ERROR) { |
| 681 | OPENCITY_ERROR( "GL error: " << glerr ); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | |
| 686 | /*=====================================================================*/ |
| 687 | void |
| 688 | Renderer::DisplayHighlight( |
| 689 | const Map* pcMap, |
| 690 | const Layer* pcLayer, |
| 691 | const OPENCITY_TOOL_CODE & enumTool ) |
| 692 | { |
| 693 | OPENCITY_DEBUG( "Not implemented" ); |
| 694 | assert( 0 ); |
| 695 | |
| 696 | DisplayHighlight( |
| 697 | pcMap, pcLayer, |
| 698 | 0, 0, |
| 699 | _uiCityWidth-1, _uiCityLength-1, |
| 700 | enumTool |
| 701 | ); |
| 702 | } |
| 703 | |
| 704 | |
| 705 | /*=====================================================================*/ |
| 706 | void |
| 707 | Renderer::DisplayHighlight( |
| 708 | const Map* pcMap, |
| 709 | const Layer* pcLayer, |
| 710 | uint uiW1, |
| 711 | uint uiL1, |
| 712 | uint uiW2, |
| 713 | uint uiL2, |
| 714 | const OPENCITY_TOOL_CODE & enumTool ) |
| 715 | { |
| 716 | uint linear; |
| 717 | uint w, l; |
| 718 | const Structure* pStructure; |
| 719 | |
| 720 | // Doing some swapping |
| 721 | OPENCITY_SWAP( uiW1, uiW2, uint ); |
| 722 | OPENCITY_SWAP( uiL1, uiL2, uint ); |
| 723 | |
| 724 | // Display the current layer & map first |
| 725 | Display( pcMap, pcLayer ); |
| 726 | |
| 727 | // Prepare the world for rendering |
| 728 | _PrepareView(); |
| 729 | |
| 730 | // Now let's display all the structures in selection mode |
| 731 | glPushAttrib( GL_ENABLE_BIT ); |
| 732 | glDisable( GL_LIGHTING ); |
| 733 | glPushMatrix(); |
| 734 | glTranslatef( 0., 0.1, 0. ); |
| 735 | for (l = uiL1; l <= uiL2; l++) { |
| 736 | linear = l*_uiCityWidth + uiW2; |
| 737 | for (w = uiW1; w <= uiW2; w++) { |
| 738 | pStructure = pcLayer->GetLinearStructure( linear ); |
| 739 | // Display the correction structure/terrain with "linear" as objectID |
| 740 | if ( pStructure == NULL) |
| 741 | gVars.gpGraphicMgr->DisplayTerrainHighlight( w, l, enumTool ); |
| 742 | else |
| 743 | gVars.gpGraphicMgr-> |
| 744 | DisplayStructureHighlight( pStructure, w, l, enumTool ); |
| 745 | |
| 746 | ++linear; |
| 747 | } |
| 748 | } |
| 749 | glPopMatrix(); |
| 750 | glPopAttrib(); |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /*=====================================================================*/ |
| 755 | void |
| 756 | Renderer::DisplayBuildPreview( |
| 757 | const uint & uiW, |
| 758 | const uint & uiL, |
| 759 | const GLubyte & rcubR, |
| 760 | const GLubyte & rcubG, |
| 761 | const GLubyte & rcubB, |
| 762 | const OPENCITY_GRAPHIC_CODE & gcode ) const |
| 763 | { |
| 764 | static uint sw, sl, sh; |
| 765 | static int iH; |
| 766 | |
| 767 | // Save the current model view matrix |
| 768 | glPushMatrix(); |
| 769 | |
| 770 | // Display the graphic code |
| 771 | glTranslatef( 0., 0.15, 0. ); |
| 772 | gVars.gpGraphicMgr->DisplayGC( gcode, uiW, uiL ); |
| 773 | |
| 774 | // Get the maximum square height |
| 775 | iH = gVars.gpMapMgr->GetSquareMaxHeight( uiW, uiL ); |
| 776 | |
| 777 | // Get the graphic code dimensions in order to draw the bounding rectangle |
| 778 | gVars.gpPropertyMgr->GetWLH( gcode, sw, 4, sl, 4, sh, 1 ); |
| 779 | glEnable( GL_BLEND ); |
| 780 | glTranslatef( .0f, 0.1f, .0f ); |
| 781 | glColor4ub( rcubR, rcubG, rcubB, 64 ); |
| 782 | glBegin( GL_QUADS ); |
| 783 | glVertex3i( uiW, iH, uiL ); |
| 784 | glVertex3i( uiW, iH, uiL+sl ); |
| 785 | glVertex3i( uiW+sw, iH, uiL+sl ); |
| 786 | glVertex3i( uiW+sw, iH, uiL ); |
| 787 | glEnd(); |
| 788 | glDisable( GL_BLEND ); |
| 789 | |
| 790 | // Restore the model view matrix |
| 791 | glPopMatrix(); |
| 792 | } |
| 793 | |
| 794 | |
| 795 | /*=====================================================================*/ |
| 796 | void |
| 797 | Renderer::DisplaySelection( |
| 798 | const Map* pcMap, |
| 799 | const Layer* pcLayer ) |
| 800 | { |
| 801 | DisplaySelection( |
| 802 | pcMap, pcLayer, |
| 803 | 0, 0, |
| 804 | _uiCityWidth-1, _uiCityLength-1 |
| 805 | ); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /*=====================================================================*/ |
| 810 | void |
| 811 | Renderer::DisplaySelection( |
| 812 | const Map* pcMap, |
| 813 | const Layer* pcLayer, |
| 814 | const uint & rcuiW1, |
| 815 | const uint & rcuiL1, |
| 816 | const uint & rcuiW2, |
| 817 | const uint & rcuiL2 ) |
| 818 | { |
| 819 | OPENCITY_DEBUG( "Not implemented" ); |
| 820 | assert( 0 ); |
| 821 | } |
| 822 | |
| 823 | |
| 824 | /*=====================================================================*/ |
| 825 | void |
| 826 | Renderer::DisplayText( |
| 827 | const uint & rcuiX, |
| 828 | const uint & rcuiY, |
| 829 | const GLubyte & rcubR, |
| 830 | const GLubyte & rcubG, |
| 831 | const GLubyte & rcubB, |
| 832 | const string & rcText) |
| 833 | { |
| 834 | // Render in 2D, orthogonal projection |
| 835 | glPushMatrix(); |
| 836 | glLoadIdentity(); |
| 837 | glMatrixMode( GL_PROJECTION ); |
| 838 | glPushMatrix(); |
| 839 | glLoadIdentity(); |
| 840 | gluOrtho2D( 0, _iWinWidth-1, 0, _iWinHeight-1 ); |
| 841 | |
| 842 | // Save the list base |
| 843 | glTranslatef( .0, .0, .1 ); |
| 844 | glPushAttrib( GL_LIST_BIT | GL_ENABLE_BIT ); |
| 845 | glDisable( GL_LIGHTING ); |
| 846 | glListBase( this->_uiFontBase ); |
| 847 | |
| 848 | glColor4ub( rcubR, rcubG, rcubB, 255 ); |
| 849 | glRasterPos2i( rcuiX, rcuiY ); |
| 850 | glCallLists( (GLsizei)rcText.size(), GL_UNSIGNED_BYTE, (GLubyte*)rcText.c_str() ); |
| 851 | glPopAttrib(); |
| 852 | |
| 853 | glPopMatrix(); |
| 854 | glMatrixMode( GL_MODELVIEW ); |
| 855 | glPopMatrix(); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | /*=====================================================================*/ |
| 860 | const bool |
| 861 | Renderer::GetSelectedWLFrom |
| 862 | ( |
| 863 | const uint & rcuiMouseX, |
| 864 | const uint & rcuiMouseY, |
| 865 | uint & ruiW, |
| 866 | uint & ruiL, |
| 867 | const Map* pcMap, |
| 868 | const Layer* pcLayer |
| 869 | ) |
| 870 | { |
| 871 | // call the right method with appropriate values |
| 872 | return GetSelectedWLFrom( |
| 873 | rcuiMouseX, |
| 874 | rcuiMouseY, |
| 875 | ruiW, |
| 876 | ruiL, |
| 877 | pcMap, |
| 878 | pcLayer, |
| 879 | 0, 0, |
| 880 | _uiCityWidth-1, _uiCityLength-1 |
| 881 | ); |
| 882 | } |
| 883 | |
| 884 | |
| 885 | /*=====================================================================*/ |
| 886 | // Old version, kept for reference, march 24th, 2008 |
| 887 | /* |
| 888 | const bool |
| 889 | Renderer::GetSelectedWLFrom |
| 890 | ( |
| 891 | const uint & rcuiMouseX, |
| 892 | const uint & rcuiMouseY, |
| 893 | uint & ruiW, |
| 894 | uint & ruiL, |
| 895 | const Map* pcMap, |
| 896 | const Layer* pcLayer, |
| 897 | const uint & rcuiW1, |
| 898 | const uint & rcuiL1, |
| 899 | const uint & rcuiW2, |
| 900 | const uint & rcuiL2 |
| 901 | ) |
| 902 | { |
| 903 | static uint id = 0; |
| 904 | static uint linear = 0; |
| 905 | static uint w = 0, l = 0; |
| 906 | static const Structure* pStructure = NULL; |
| 907 | |
| 908 | #define OC_SELECT_BUFFER_SIZE 100 |
| 909 | static GLuint selectBuffer[OC_SELECT_BUFFER_SIZE]; |
| 910 | static GLuint uiDepthMin; |
| 911 | static GLint iHits; |
| 912 | static GLint viewport[4] = {0, 0, 0, 0}; |
| 913 | |
| 914 | // Prepare the select buffer and enter selection mode |
| 915 | glSelectBuffer( OC_SELECT_BUFFER_SIZE, selectBuffer ); |
| 916 | (void)glRenderMode( GL_SELECT ); |
| 917 | glInitNames(); |
| 918 | glPushName(0); |
| 919 | |
| 920 | // Create the pick matrix |
| 921 | glMatrixMode( GL_PROJECTION ); |
| 922 | glPushMatrix(); |
| 923 | glLoadIdentity(); |
| 924 | viewport[2] = _iWinWidth; |
| 925 | viewport[3] = _iWinHeight; |
| 926 | gluPickMatrix( rcuiMouseX, _iWinHeight-rcuiMouseY, 2, 2, viewport ); |
| 927 | if ( _ubProjectionType == OC_PERSPECTIVE ) { |
| 928 | gluPerspective( |
| 929 | OC_VIEW_ANGLE, |
| 930 | (GLfloat)_iWinWidth / (GLfloat)_iWinHeight, |
| 931 | OC_Z_NEAR, OC_Z_FAR ); |
| 932 | } |
| 933 | else { |
| 934 | glOrtho( |
| 935 | 0.0, (GLdouble)_iWinWidth, |
| 936 | 0.0, (GLdouble)_iWinHeight, |
| 937 | OC_Z_NEAR_ORTHO, OC_Z_FAR_ORTHO); |
| 938 | } |
| 939 | |
| 940 | // Save all the enabled states |
| 941 | glPushAttrib( GL_ENABLE_BIT ); |
| 942 | glDisable( GL_LIGHTING ); |
| 943 | |
| 944 | // Clear the color buffer ( the screen ) |
| 945 | glClearColor( 0.0, 0.0, 0.0, 0.0 ); |
| 946 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 947 | |
| 948 | // Prepare the world for rendering without culling calculation because |
| 949 | // calculating the culling with the pick matrix gives unpredictable results |
| 950 | _PrepareView(); |
| 951 | |
| 952 | // Now let's display all the structures in selection mode |
| 953 | linear = 0; |
| 954 | for (l = 0; l < _uiCityLength; l++) { |
| 955 | for (w = 0; w < _uiCityWidth; w++) { |
| 956 | pStructure = pcLayer->GetLinearStructure( linear ); |
| 957 | // Display the correction structure/terrain with "linear" as objectID |
| 958 | // Note: |
| 959 | // linear = 0 is not used since it means blank |
| 960 | // blank = there's no structure under the selection |
| 961 | if ( pStructure == NULL) |
| 962 | gVars.gpGraphicMgr->DisplayTerrainSelection( w, l, ++linear ); |
| 963 | else |
| 964 | gVars.gpGraphicMgr->DisplayStructureSelection( pStructure, w, l, ++linear ); |
| 965 | //ATTENTION: "linear++;" already done ! |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | // Restore all the enabled states |
| 970 | glPopAttrib(); |
| 971 | glMatrixMode( GL_PROJECTION ); |
| 972 | glPopMatrix(); |
| 973 | glMatrixMode( GL_MODELVIEW ); |
| 974 | glFlush(); |
| 975 | |
| 976 | // GL error checking |
| 977 | if (glGetError() != GL_NO_ERROR) { |
| 978 | OPENCITY_DEBUG( "GL ERROR" ); |
| 979 | } |
| 980 | |
| 981 | // Process the hits |
| 982 | iHits = glRenderMode( GL_RENDER ); |
| 983 | //debug cout << "Number of hits: " << iHits << endl; |
| 984 | if (iHits > 0) { |
| 985 | // We only consider the hit which is nearest to the user |
| 986 | uiDepthMin = 0xFFFFFFFF - 1; |
| 987 | while (iHits-- > 0) { |
| 988 | if (uiDepthMin > selectBuffer[ iHits*4 + 1 ]) { |
| 989 | uiDepthMin = selectBuffer[ iHits*4 + 1 ]; |
| 990 | id = selectBuffer[ iHits*4 + 3 ] - 1; |
| 991 | } |
| 992 | } |
| 993 | //debug // cout << "Min depth: " << uiDepthMin << endl; |
| 994 | ruiL = id / _uiCityWidth; |
| 995 | ruiW = id % _uiCityWidth; |
| 996 | return true; |
| 997 | } |
| 998 | |
| 999 | return false; // couldn't determine the W & L values |
| 1000 | } |
| 1001 | */ |
| 1002 | |
| 1003 | |
| 1004 | /*=====================================================================*/ |
| 1005 | const bool |
| 1006 | Renderer::GetSelectedWLFrom |
| 1007 | ( |
| 1008 | const uint & rcuiMouseX, |
| 1009 | const uint & rcuiMouseY, |
| 1010 | uint & ruiW, |
| 1011 | uint & ruiL, |
| 1012 | const Map* pcMap, |
| 1013 | const Layer* pcLayer, |
| 1014 | const uint & rcuiW1, |
| 1015 | const uint & rcuiL1, |
| 1016 | const uint & rcuiW2, |
| 1017 | const uint & rcuiL2 |
| 1018 | ) |
| 1019 | { |
| 1020 | static uint id = 0; |
| 1021 | static uint linear = 0; |
| 1022 | static uint w = 0, l = 0; |
| 1023 | static const Structure* pStructure = NULL; |
| 1024 | |
| 1025 | #define OC_SELECT_BUFFER_SIZE 100 |
| 1026 | static GLuint selectBuffer[OC_SELECT_BUFFER_SIZE]; |
| 1027 | static GLuint uiDepthMin; |
| 1028 | static GLint iHits; |
| 1029 | static GLint viewport[4] = {0, 0, 0, 0}; |
| 1030 | |
| 1031 | // Prepare the world for rendering: calculate the view volume, culling |
| 1032 | _PrepareView(); |
| 1033 | |
| 1034 | // Calculate the culling grid and the culled models if it's requested |
| 1035 | if (_bCalculateCulling) { |
| 1036 | _CalculateCulledGrid( 0, 0, _uiCityWidth+1, _uiCityLength+1, true); |
| 1037 | _bCalculateCulling = false; |
| 1038 | } |
| 1039 | _CalculateCulledModel( pcLayer ); |
| 1040 | |
| 1041 | // Prepare the select buffer and enter selection mode |
| 1042 | glSelectBuffer( OC_SELECT_BUFFER_SIZE, selectBuffer ); |
| 1043 | (void)glRenderMode( GL_SELECT ); |
| 1044 | glInitNames(); |
| 1045 | glPushName(0); |
| 1046 | |
| 1047 | // Create the pick matrix |
| 1048 | glMatrixMode( GL_PROJECTION ); |
| 1049 | glPushMatrix(); |
| 1050 | glLoadIdentity(); |
| 1051 | viewport[2] = _iWinWidth; |
| 1052 | viewport[3] = _iWinHeight; |
| 1053 | gluPickMatrix( rcuiMouseX, _iWinHeight-rcuiMouseY, 2, 2, viewport ); |
| 1054 | if ( _ubProjectionType == OC_PERSPECTIVE ) { |
| 1055 | gluPerspective( |
| 1056 | OC_VIEW_ANGLE, |
| 1057 | (GLfloat)_iWinWidth / (GLfloat)_iWinHeight, |
| 1058 | OC_Z_NEAR, OC_Z_FAR ); |
| 1059 | } |
| 1060 | else { |
| 1061 | glOrtho( |
| 1062 | 0.0, (GLdouble)_iWinWidth, |
| 1063 | 0.0, (GLdouble)_iWinHeight, |
| 1064 | OC_Z_NEAR_ORTHO, OC_Z_FAR_ORTHO); |
| 1065 | } |
| 1066 | |
| 1067 | // Save all the enabled states |
| 1068 | glPushAttrib( GL_ENABLE_BIT ); |
| 1069 | glDisable( GL_LIGHTING ); |
| 1070 | |
| 1071 | // Clear the color buffer ( the screen ) |
| 1072 | glClearColor( 0.0, 0.0, 0.0, 0.0 ); |
| 1073 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 1074 | |
| 1075 | // Prepare the world for rendering without culling calculation because |
| 1076 | // calculating the culling with the pick matrix gives unpredictable results |
| 1077 | _PrepareView(); |
| 1078 | |
| 1079 | // Display the structures |
| 1080 | glTranslatef( 0., 0.05, 0. ); |
| 1081 | linear = 0; |
| 1082 | for (l = 0; l < _uiCityLength; l++) { |
| 1083 | for (w = 0; w < _uiCityWidth; w++, linear++) { |
| 1084 | // Display the correction structure/terrain with "linear" as objectID |
| 1085 | // Note: |
| 1086 | // linear = 0 is not used since it means blank |
| 1087 | // blank = there's no structure under the selection |
| 1088 | // In any case, let the user select the terrain square |
| 1089 | gVars.gpGraphicMgr->DisplayTerrainSelection( w, l, linear+1 ); |
| 1090 | if (!_baCulledModel[linear]) |
| 1091 | continue; |
| 1092 | |
| 1093 | // The structure is not NULL here (see _CalculateCulledModel) |
| 1094 | pStructure = pcLayer->GetLinearStructure( linear ); |
| 1095 | assert( pStructure != NULL ); |
| 1096 | gVars.gpGraphicMgr->DisplayStructureSelection( pStructure, w, l, linear+1 ); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | // Restore all the enabled states |
| 1101 | glPopAttrib(); |
| 1102 | glMatrixMode( GL_PROJECTION ); |
| 1103 | glPopMatrix(); |
| 1104 | glMatrixMode( GL_MODELVIEW ); |
| 1105 | glFlush(); |
| 1106 | |
| 1107 | // GL error checking |
| 1108 | if (glGetError() != GL_NO_ERROR) { |
| 1109 | OPENCITY_DEBUG( "GL ERROR" ); |
| 1110 | } |
| 1111 | |
| 1112 | // Process the hits |
| 1113 | iHits = glRenderMode( GL_RENDER ); |
| 1114 | //debug cout << "Number of hits: " << iHits << endl; |
| 1115 | if (iHits > 0) { |
| 1116 | // We only consider the hit which is nearest to the user |
| 1117 | uiDepthMin = 0xFFFFFFFF - 1; |
| 1118 | while (iHits-- > 0) { |
| 1119 | if (uiDepthMin > selectBuffer[ iHits*4 + 1 ]) { |
| 1120 | uiDepthMin = selectBuffer[ iHits*4 + 1 ]; |
| 1121 | id = selectBuffer[ iHits*4 + 3 ] - 1; |
| 1122 | } |
| 1123 | } |
| 1124 | //debug // cout << "Min depth: " << uiDepthMin << endl; |
| 1125 | ruiL = id / _uiCityWidth; |
| 1126 | ruiW = id % _uiCityWidth; |
| 1127 | return true; |
| 1128 | } |
| 1129 | |
| 1130 | return false; // couldn't determine the W & L values |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | /*=====================================================================*/ |
| 1135 | void |
| 1136 | Renderer::SetWinSize( |
| 1137 | const int & rciWidth, |
| 1138 | const int & rciHeight ) |
| 1139 | { |
| 1140 | _iWinWidth = rciWidth; |
| 1141 | _iWinHeight = rciHeight; |
| 1142 | |
| 1143 | // Set the view port |
| 1144 | glViewport( 0, 0, rciWidth, rciHeight ); |
| 1145 | |
| 1146 | // Set the projection matrix |
| 1147 | glMatrixMode( GL_PROJECTION ); |
| 1148 | glLoadIdentity(); |
| 1149 | if ( _ubProjectionType == OC_PERSPECTIVE ) { |
| 1150 | gluPerspective( |
| 1151 | OC_VIEW_ANGLE, |
| 1152 | (GLfloat)_iWinWidth / (GLfloat)_iWinHeight, |
| 1153 | OC_Z_NEAR, OC_Z_FAR ); |
| 1154 | } |
| 1155 | else { |
| 1156 | glOrtho( |
| 1157 | 0.0, (GLdouble)_iWinWidth, |
| 1158 | 0.0, (GLdouble)_iWinHeight, |
| 1159 | OC_Z_NEAR_ORTHO, OC_Z_FAR_ORTHO); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | /*=====================================================================*/ |
| 1165 | void |
| 1166 | Renderer::_DisplayTerrain() const |
| 1167 | { |
| 1168 | #define OC_TERRAIN_TEXTURE_DEPTH 64 |
| 1169 | |
| 1170 | if (bHeightChange == false) |
| 1171 | goto displayterrain_return; |
| 1172 | |
| 1173 | static signed char tabH[4]; // Terrain height |
| 1174 | static GLfloat tabR[4]; // Texture R (depth) coordinate |
| 1175 | static GLfloat ax, ay, az; |
| 1176 | static GLfloat bx, by, bz; |
| 1177 | static GLfloat n1x, n1y, n1z; // normal 1 coordinates |
| 1178 | static GLfloat n2x, n2y, n2z; // normal 2 coordinates |
| 1179 | static int l, w; // WARNING: yes, we use INT not UINT |
| 1180 | |
| 1181 | // Reserve a new display list for the terrain |
| 1182 | glNewList( _uiTerrainList, GL_COMPILE ); |
| 1183 | |
| 1184 | // WARNING: this is used to calculated the final textured fragment. |
| 1185 | glColor4f( .8, .7, .6, .8 ); |
| 1186 | |
| 1187 | // Enable terrain texturing |
| 1188 | glPushAttrib( GL_ENABLE_BIT ); |
| 1189 | glEnable( GL_CULL_FACE ); |
| 1190 | glEnable( GL_TEXTURE_3D ); |
| 1191 | glBindTexture( GL_TEXTURE_3D, moTextureTerrain.GetName() ); |
| 1192 | glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); |
| 1193 | glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); |
| 1194 | glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); |
| 1195 | glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
| 1196 | glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
| 1197 | glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE ); |
| 1198 | |
| 1199 | // BEGIN, draw the terrain as an unique TRIANGLE_STRIP |
| 1200 | // which is known as the fastest figure in OpenGL |
| 1201 | /* This is the secret formula: c = a^b ;) |
| 1202 | cx = ay * bz - by * az; |
| 1203 | cy = bx * az - ax * bz; |
| 1204 | cz = ax * by - bx * ay; |
| 1205 | */ |
| 1206 | glBegin( GL_TRIANGLE_STRIP ); |
| 1207 | |
| 1208 | for (l = 0; l < (int)_uiCityLength; l++) { |
| 1209 | // IF we draw the squares from left to right THEN |
| 1210 | if (l % 2 == 0) { |
| 1211 | // Get the 4 heights of the current square |
| 1212 | w = 0; |
| 1213 | gVars.gpMapMgr->GetSquareHeight( w, l, tabH ); |
| 1214 | for (int i = 0; i < 4; i++) { |
| 1215 | if (tabH[i] >= 0) |
| 1216 | tabR[i] = (((float)tabH[i] / OC_MAP_HEIGHT_MAX) * 55 / 64) + 9.0 / 64; |
| 1217 | else |
| 1218 | tabR[i] = ((float)OC_MAP_HEIGHT_MAX+tabH[i]) / OC_MAP_HEIGHT_MAX * 8 / 64; |
| 1219 | } |
| 1220 | |
| 1221 | // calculate the new normal 1 (the cross product) |
| 1222 | ax = 0.; ay = (GLfloat)(tabH[1]-tabH[0]); az = 1.; |
| 1223 | bx = 1.; by = (GLfloat)(tabH[3]-tabH[0]); bz = .0; |
| 1224 | n1x = -by; n1y = 1.; n1z = -ay; |
| 1225 | // calculate the new normal 2 (the cross product) |
| 1226 | ax = 1.; ay = (GLfloat)(tabH[2]-tabH[1]); az = .0; |
| 1227 | bx = .0; by = (GLfloat)(tabH[3]-tabH[2]); bz = -1.; |
| 1228 | n2x = -ay; n2y = 1.; n2z = by; |
| 1229 | |
| 1230 | // Set the first normal and the first pair of vertices |
| 1231 | glNormal3f( n1x, n1y, n1z ); |
| 1232 | glTexCoord3f( w, l, tabR[0] ); |
| 1233 | glVertex3i( w, tabH[0], l ); |
| 1234 | glTexCoord3f( w, l+1, tabR[1] ); |
| 1235 | glVertex3i( w, tabH[1], l+1 ); |
| 1236 | |
| 1237 | for (w = 1; w < (int)_uiCityWidth; w++) { |
| 1238 | // Get the 4 heights of the current square |
| 1239 | gVars.gpMapMgr->GetSquareHeight( w, l, tabH ); |
| 1240 | for (int i = 0; i < 4; i++) { |
| 1241 | if (tabH[i] >= 0) |
| 1242 | tabR[i] = (((float)tabH[i] / OC_MAP_HEIGHT_MAX) * 55 / 64) + 9.0 / 64; |
| 1243 | else |
| 1244 | tabR[i] = ((float)OC_MAP_HEIGHT_MAX+tabH[i]) / OC_MAP_HEIGHT_MAX * 8 / 64; |
| 1245 | } |
| 1246 | |
| 1247 | // draw the stuff |
| 1248 | glNormal3f( n1x, n1y, n1z ); |
| 1249 | glTexCoord3f( w, l, tabR[0] ); |
| 1250 | glVertex3i( w, tabH[0], l ); |
| 1251 | glNormal3f( n2x, n2y, n2z ); |
| 1252 | glTexCoord3f( w, l+1, tabR[1] ); |
| 1253 | glVertex3i( w, tabH[1], l+1 ); |
| 1254 | |
| 1255 | // calculate the new normal 1 (the cross product) |
| 1256 | ax = 0.; ay = (GLfloat)(tabH[1]-tabH[0]); az = 1.; |
| 1257 | bx = 1.; by = (GLfloat)(tabH[3]-tabH[0]); bz = .0; |
| 1258 | n1x = -by; n1y = 1.; n1z = -ay; |
| 1259 | // calculate the new normal 2 (the cross product) |
| 1260 | ax = 1.; ay = (GLfloat)(tabH[2]-tabH[1]); az = .0; |
| 1261 | bx = .0; by = (GLfloat)(tabH[3]-tabH[2]); bz = -1.; |
| 1262 | n2x = -ay; n2y = 1.; n2z = by; |
| 1263 | } // for |
| 1264 | |
| 1265 | // Draw the last edge |
| 1266 | glNormal3f( n1x, n1y, n1z ); |
| 1267 | glTexCoord3f( w, l, tabR[3] ); |
| 1268 | glVertex3i( w, tabH[3], l ); |
| 1269 | glNormal3f( n2x, n2y, n2z ); |
| 1270 | glTexCoord3f( w, l+1, tabR[2] ); |
| 1271 | glVertex3i( w, tabH[2], l+1 ); |
| 1272 | // Then prepare the triangles for the next line |
| 1273 | glVertex3i( w, tabH[2], l+1 ); |
| 1274 | } |
| 1275 | else { |
| 1276 | // WARNING: repeated codes as above ================================ |
| 1277 | // We draw the square from right to left |
| 1278 | // Get the 4 heights of the current square |
| 1279 | w = _uiCityWidth-1; |
| 1280 | gVars.gpMapMgr->GetSquareHeight( w, l, tabH ); |
| 1281 | for (int i = 0; i < 4; i++) { |
| 1282 | if (tabH[i] >= 0) |
| 1283 | tabR[i] = (((float)tabH[i] / OC_MAP_HEIGHT_MAX) * 55 / 64) + 9.0 / 64; |
| 1284 | else |
| 1285 | tabR[i] = ((float)OC_MAP_HEIGHT_MAX+tabH[i]) / OC_MAP_HEIGHT_MAX * 8 / 64; |
| 1286 | } |
| 1287 | |
| 1288 | // calculate the new normal 1 (the cross product) |
| 1289 | ax = -1.; ay = (GLfloat)(tabH[0]-tabH[3]); az = .0; |
| 1290 | bx = .0; by = (GLfloat)(tabH[2]-tabH[3]); bz = 1.; |
| 1291 | n1x = ay; n1y = 1.; n1z = -by; |
| 1292 | // calculate the new normal 2 (the cross product) |
| 1293 | ax = 1.; ay = (GLfloat)(tabH[2]-tabH[1]); az = .0; |
| 1294 | bx = .0; by = (GLfloat)(tabH[0]-tabH[1]); bz = -1.; |
| 1295 | n2x = -ay; n2y = 1.; n2z = by; |
| 1296 | |
| 1297 | // Set the first normal and the first pair of vertices |
| 1298 | glNormal3f( n1x, n1y, n1z ); |
| 1299 | glTexCoord3f( w+1, l, tabR[3] ); |
| 1300 | glVertex3i( w+1, tabH[3], l ); |
| 1301 | glTexCoord3f( w+1, l+1, tabR[2] ); |
| 1302 | glVertex3i( w+1, tabH[2], l+1 ); |
| 1303 | |
| 1304 | for (w = _uiCityWidth-2; w >= 0; w--) { |
| 1305 | // Get the 4 heights of the current square |
| 1306 | gVars.gpMapMgr->GetSquareHeight( w, l, tabH ); |
| 1307 | for (int i = 0; i < 4; i++) { |
| 1308 | if (tabH[i] >= 0) |
| 1309 | tabR[i] = (((float)tabH[i] / OC_MAP_HEIGHT_MAX) * 55 / 64) + 9.0 / 64; |
| 1310 | else |
| 1311 | tabR[i] = ((float)OC_MAP_HEIGHT_MAX+tabH[i]) / OC_MAP_HEIGHT_MAX * 8 / 64; |
| 1312 | } |
| 1313 | |
| 1314 | // draw the stuff |
| 1315 | glNormal3f( n1x, n1y, n1z ); |
| 1316 | glTexCoord3f( w+1, l, tabR[3] ); |
| 1317 | glVertex3i( w+1, tabH[3], l ); |
| 1318 | glNormal3f( n2x, n2y, n2z ); |
| 1319 | glTexCoord3f( w+1, l+1, tabR[2] ); |
| 1320 | glVertex3i( w+1, tabH[2], l+1 ); |
| 1321 | |
| 1322 | // calculate the new normal 1 (the cross product) |
| 1323 | ax = -1.; ay = (GLfloat)(tabH[0]-tabH[3]); az = .0; |
| 1324 | bx = .0; by = (GLfloat)(tabH[2]-tabH[3]); bz = 1.; |
| 1325 | n1x = ay; n1y = 1.; n1z = -by; |
| 1326 | // calculate the new normal 2 (the cross product) |
| 1327 | ax = 1.; ay = (GLfloat)(tabH[2]-tabH[1]); az = .0; |
| 1328 | bx = .0; by = (GLfloat)(tabH[0]-tabH[1]); bz = -1.; |
| 1329 | n2x = -ay; n2y = 1.; n2z = by; |
| 1330 | } |
| 1331 | |
| 1332 | // Draw the last edge |
| 1333 | glNormal3f( n1x, n1y, n1z ); |
| 1334 | glTexCoord3f( w+1, l, tabR[0] ); |
| 1335 | glVertex3i( w+1, tabH[0], l ); |
| 1336 | glNormal3f( n2x, n2y, n2z ); |
| 1337 | glTexCoord3f( w+1, l+1, tabR[1] ); |
| 1338 | glVertex3i( w+1, tabH[1], l+1 ); |
| 1339 | // Then prepare the triangles for the next line |
| 1340 | glVertex3i( w+1, tabH[1], l+1 ); |
| 1341 | } |
| 1342 | } |
| 1343 | // then restore the default normal |
| 1344 | glNormal3f( 0., 0., 1. ); |
| 1345 | glEnd(); |
| 1346 | // END: Draw the terrain by using GL_TRIANGLE_STRIP |
| 1347 | |
| 1348 | // Restore old attribs |
| 1349 | glPopAttrib(); |
| 1350 | glEndList(); |
| 1351 | |
| 1352 | displayterrain_return: |
| 1353 | glCallList( _uiTerrainList ); |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | /*=====================================================================*/ |
| 1358 | // old version, kept for reference, jan 27th, 08 |
| 1359 | /* |
| 1360 | void |
| 1361 | Renderer::_DisplayWater() const |
| 1362 | { |
| 1363 | #define WATER_HEIGHT -.15 |
| 1364 | |
| 1365 | static bool initialized = false; |
| 1366 | |
| 1367 | // IF the display list is already initialized THEN |
| 1368 | if (initialized) |
| 1369 | goto displaywater_return; |
| 1370 | else |
| 1371 | initialized = true; |
| 1372 | |
| 1373 | static GLfloat ax, ay, az; |
| 1374 | static GLfloat bx, by, bz; |
| 1375 | static GLfloat n1x, n1y, n1z; // normal 1 coordinates |
| 1376 | static GLfloat n2x, n2y, n2z; // normal 2 coordinates |
| 1377 | static int l, w; // WARNING: yes, we use INT not UINT |
| 1378 | |
| 1379 | // Reserve a new display list for the terrain |
| 1380 | glNewList( _uiWaterList, GL_COMPILE ); |
| 1381 | |
| 1382 | // WARNING: this is used to calculated the final textured fragment. |
| 1383 | glColor4f( .3, .25, .2, 1. ); |
| 1384 | |
| 1385 | // Enable terrain texturing |
| 1386 | glPushAttrib( GL_ENABLE_BIT ); |
| 1387 | glEnable( GL_BLEND ); |
| 1388 | glEnable( GL_TEXTURE_2D ); |
| 1389 | glBindTexture( GL_TEXTURE_2D, _uiWaterTex ); |
| 1390 | glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); |
| 1391 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
| 1392 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
| 1393 | |
| 1394 | // BEGIN, draw the terrain as an unique TRIANGLE_STRIP |
| 1395 | // which is known as the fastest figure in OpenGL |
| 1396 | // This is the secret formula: c = a^b ;) |
| 1397 | // cx = ay * bz - by * az; |
| 1398 | // cy = bx * az - ax * bz; |
| 1399 | // cz = ax * by - bx * ay; |
| 1400 | |
| 1401 | glBegin( GL_TRIANGLE_STRIP ); |
| 1402 | |
| 1403 | for (l = 0; l < (int)_uiCityLength; l++) { |
| 1404 | // IF we draw the squares from left to right THEN |
| 1405 | if (l % 2 == 0) { |
| 1406 | // Get the 4 heights of the current square |
| 1407 | w = 0; |
| 1408 | // calculate the new normal 1 (the cross product) |
| 1409 | ax = 0.; ay = WATER_HEIGHT; az = 1.; |
| 1410 | bx = 1.; by = WATER_HEIGHT; bz = .0; |
| 1411 | n1x = -by; n1y = 1.; n1z = -ay; |
| 1412 | // calculate the new normal 2 (the cross product) |
| 1413 | ax = 1.; ay = WATER_HEIGHT; az = .0; |
| 1414 | bx = .0; by = WATER_HEIGHT; bz = -1.; |
| 1415 | n2x = -ay; n2y = 1.; n2z = by; |
| 1416 | |
| 1417 | // Set the first normal and the first pair of vertices |
| 1418 | glNormal3f( n1x, n1y, n1z ); |
| 1419 | glTexCoord2i( w, l ); |
| 1420 | glVertex3f( w, WATER_HEIGHT, l ); |
| 1421 | glTexCoord2i( w, l+1 ); |
| 1422 | glVertex3f( w, WATER_HEIGHT, l+1 ); |
| 1423 | |
| 1424 | for (w = 1; w < (int)_uiCityWidth; w++) { |
| 1425 | // draw the stuff |
| 1426 | glNormal3f( n1x, n1y, n1z ); |
| 1427 | glTexCoord2i( w, l ); |
| 1428 | glVertex3f( w, WATER_HEIGHT, l ); |
| 1429 | glNormal3f( n2x, n2y, n2z ); |
| 1430 | glTexCoord2i( w, l+1 ); |
| 1431 | glVertex3f( w, WATER_HEIGHT, l+1 ); |
| 1432 | |
| 1433 | // calculate the new normal 1 (the cross product) |
| 1434 | ax = 0.; ay = WATER_HEIGHT; az = 1.; |
| 1435 | bx = 1.; by = WATER_HEIGHT; bz = .0; |
| 1436 | n1x = -by; n1y = 1.; n1z = -ay; |
| 1437 | // calculate the new normal 2 (the cross product) |
| 1438 | ax = 1.; ay = WATER_HEIGHT; az = .0; |
| 1439 | bx = .0; by = WATER_HEIGHT; bz = -1.; |
| 1440 | n2x = -ay; n2y = 1.; n2z = by; |
| 1441 | } // for |
| 1442 | |
| 1443 | // Draw the last edge |
| 1444 | glNormal3f( n1x, n1y, n1z ); |
| 1445 | glTexCoord2i( w, l ); |
| 1446 | glVertex3f( w, WATER_HEIGHT, l ); |
| 1447 | glNormal3f( n2x, n2y, n2z ); |
| 1448 | glTexCoord2i( w, l+1 ); |
| 1449 | glVertex3f( w, WATER_HEIGHT, l+1 ); |
| 1450 | // Then prepare the triangles for the next line |
| 1451 | glVertex3f( w, WATER_HEIGHT, l+1 ); |
| 1452 | } |
| 1453 | else { |
| 1454 | // WARNING: repeated codes as above ================================ |
| 1455 | // We draw the square from right to left |
| 1456 | // Get the 4 heights of the current square |
| 1457 | w = _uiCityWidth-1; |
| 1458 | |
| 1459 | // calculate the new normal 1 (the cross product) |
| 1460 | ax = -1.; ay = WATER_HEIGHT; az = .0; |
| 1461 | bx = .0; by = WATER_HEIGHT; bz = 1.; |
| 1462 | n1x = ay; n1y = 1.; n1z = -by; |
| 1463 | // calculate the new normal 2 (the cross product) |
| 1464 | ax = 1.; ay = WATER_HEIGHT; az = .0; |
| 1465 | bx = .0; by = WATER_HEIGHT; bz = -1.; |
| 1466 | n2x = -ay; n2y = 1.; n2z = by; |
| 1467 | |
| 1468 | // Set the first normal and the first pair of vertices |
| 1469 | glNormal3f( n1x, n1y, n1z ); |
| 1470 | glTexCoord2i( w+1, l ); |
| 1471 | glVertex3f( w+1, WATER_HEIGHT, l ); |
| 1472 | glTexCoord2i( w+1, l+1 ); |
| 1473 | glVertex3f( w+1, WATER_HEIGHT, l+1 ); |
| 1474 | |
| 1475 | for (w = _uiCityWidth-2; w >= 0; w--) { |
| 1476 | // draw the stuff |
| 1477 | glNormal3f( n1x, n1y, n1z ); |
| 1478 | glTexCoord2i( w+1, l ); |
| 1479 | glVertex3f( w+1, WATER_HEIGHT, l ); |
| 1480 | glNormal3f( n2x, n2y, n2z ); |
| 1481 | glTexCoord2i( w+1, l+1 ); |
| 1482 | glVertex3f( w+1, WATER_HEIGHT, l+1 ); |
| 1483 | |
| 1484 | // calculate the new normal 1 (the cross product) |
| 1485 | ax = -1.; ay = WATER_HEIGHT; az = .0; |
| 1486 | bx = .0; by = WATER_HEIGHT; bz = 1.; |
| 1487 | n1x = ay; n1y = 1.; n1z = -by; |
| 1488 | // calculate the new normal 2 (the cross product) |
| 1489 | ax = 1.; ay = WATER_HEIGHT; az = .0; |
| 1490 | bx = .0; by = WATER_HEIGHT; bz = -1.; |
| 1491 | n2x = -ay; n2y = 1.; n2z = by; |
| 1492 | } |
| 1493 | |
| 1494 | // Draw the last edge |
| 1495 | glNormal3f( n1x, n1y, n1z ); |
| 1496 | glTexCoord2i( w+1, l ); |
| 1497 | glVertex3f( w+1, WATER_HEIGHT, l ); |
| 1498 | glNormal3f( n2x, n2y, n2z ); |
| 1499 | glTexCoord2i( w+1, l+1 ); |
| 1500 | glVertex3f( w+1, WATER_HEIGHT, l+1 ); |
| 1501 | // Then prepare the triangles for the next line |
| 1502 | glVertex3f( w+1, WATER_HEIGHT, l+1 ); |
| 1503 | } |
| 1504 | } |
| 1505 | // then restore the default normal |
| 1506 | glNormal3f( 0., 0., 1. ); |
| 1507 | glEnd(); |
| 1508 | // END: Draw the terrain by using GL_TRIANGLE_STRIP |
| 1509 | |
| 1510 | // Restore old attribs |
| 1511 | glPopAttrib(); |
| 1512 | glEndList(); |
| 1513 | |
| 1514 | displaywater_return: |
| 1515 | glCallList( _uiWaterList ); |
| 1516 | } |
| 1517 | */ |
| 1518 | |
| 1519 | |
| 1520 | /*=====================================================================*/ |
| 1521 | void |
| 1522 | Renderer::_DisplayWater() const |
| 1523 | { |
| 1524 | #define WATER_HEIGHT -.15 |
| 1525 | |
| 1526 | static bool initialized = false; |
| 1527 | |
| 1528 | // IF the display list is already initialized THEN |
| 1529 | if (initialized) |
| 1530 | goto displaywater_return; |
| 1531 | else |
| 1532 | initialized = true; |
| 1533 | |
| 1534 | // Reserve a new display list for the terrain |
| 1535 | glNewList( _uiWaterList, GL_COMPILE ); |
| 1536 | |
| 1537 | // WARNING: this is used to calculated the final textured fragment. |
| 1538 | glColor4f( .3, .25, .2, 1. ); |
| 1539 | |
| 1540 | // Enable terrain texturing |
| 1541 | glPushAttrib( GL_ENABLE_BIT ); |
| 1542 | glEnable( GL_BLEND ); |
| 1543 | glEnable( GL_TEXTURE_2D ); |
| 1544 | glBindTexture( GL_TEXTURE_2D, moTextureWater.GetName() ); |
| 1545 | glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); |
| 1546 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
| 1547 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
| 1548 | |
| 1549 | // Render the triangle strip |
| 1550 | glBegin( GL_TRIANGLE_STRIP ); |
| 1551 | glNormal3i( 0, 1, 0 ); |
| 1552 | |
| 1553 | glTexCoord2i( 0, 0 ); |
| 1554 | glVertex3f ( 0, WATER_HEIGHT, 0 ); |
| 1555 | glTexCoord2i( 0, _uiCityLength ); |
| 1556 | glVertex3f ( 0, WATER_HEIGHT, _uiCityLength ); |
| 1557 | glTexCoord2i( _uiCityWidth, 0 ); |
| 1558 | glVertex3f ( _uiCityWidth, WATER_HEIGHT, 0 ); |
| 1559 | glTexCoord2i( _uiCityWidth, _uiCityLength ); |
| 1560 | glVertex3f ( _uiCityWidth, WATER_HEIGHT, _uiCityLength ); |
| 1561 | |
| 1562 | // then restore the default normal |
| 1563 | glNormal3i( 0, 0, 1 ); |
| 1564 | glEnd(); |
| 1565 | |
| 1566 | // Restore old attribs |
| 1567 | glPopAttrib(); |
| 1568 | glEndList(); |
| 1569 | |
| 1570 | displaywater_return: |
| 1571 | glCallList( _uiWaterList ); |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | /*=====================================================================*/ |
| 1576 | void |
| 1577 | Renderer::_DisplayMapGrid( const Map* pcmap ) |
| 1578 | { |
| 1579 | if (bHeightChange == false) |
| 1580 | goto displaymapgrid_return; |
| 1581 | |
| 1582 | uint linear; |
| 1583 | uint w, l; |
| 1584 | |
| 1585 | // Create a new display list for the grid |
| 1586 | glNewList( _uiGridList, GL_COMPILE ); |
| 1587 | |
| 1588 | // Enable line stipple |
| 1589 | glPushAttrib( GL_ENABLE_BIT ); |
| 1590 | glDisable( GL_LIGHTING ); |
| 1591 | glDisable( GL_BLEND ); |
| 1592 | glLineStipple( 1, 0x3333 ); |
| 1593 | glEnable( GL_LINE_STIPPLE ); |
| 1594 | |
| 1595 | // Translate it a bit for a better view |
| 1596 | glPushMatrix(); |
| 1597 | glTranslatef( 0.0, 0.05, 0.0 ); |
| 1598 | glColor3ub( 120, 120, 120 ); // bright silver |
| 1599 | |
| 1600 | //--- horizontal lines --- |
| 1601 | linear = 0; |
| 1602 | for (l = 0; l <= _uiCityLength; l++) { |
| 1603 | glBegin( GL_LINE_STRIP ); |
| 1604 | for (w = 0; w <= _uiCityWidth; w++) { |
| 1605 | glVertex3s( w, pcmap->GetLinearHeight( linear++ ), l ); |
| 1606 | } |
| 1607 | glEnd(); |
| 1608 | } |
| 1609 | |
| 1610 | //--- vertical lines --- |
| 1611 | for (w = 0; w <= _uiCityWidth; w++) { |
| 1612 | glBegin( GL_LINE_STRIP ); |
| 1613 | linear = w; |
| 1614 | for (l = 0; l <= _uiCityLength; l++) { |
| 1615 | glVertex3s( w, pcmap->GetLinearHeight( linear ), l ); |
| 1616 | linear += _uiCityWidth + 1; |
| 1617 | } |
| 1618 | glEnd(); |
| 1619 | } |
| 1620 | |
| 1621 | // Restore the old matrix and attribs |
| 1622 | glPopMatrix(); |
| 1623 | glPopAttrib(); |
| 1624 | glEndList(); |
| 1625 | |
| 1626 | displaymapgrid_return: |
| 1627 | glCallList( _uiGridList ); |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | /*=====================================================================*/ |
| 1632 | void |
| 1633 | Renderer::_DisplayCompass() const |
| 1634 | { |
| 1635 | // Save the OpenGL attributes |
| 1636 | glPushAttrib( GL_ENABLE_BIT ); |
| 1637 | glDisable( GL_LIGHTING ); |
| 1638 | |
| 1639 | // Save the old projection matrix before processing |
| 1640 | glMatrixMode( GL_PROJECTION ); |
| 1641 | glPushMatrix(); |
| 1642 | glLoadIdentity(); |
| 1643 | gluOrtho2D( 0, _iWinWidth-1, 0, _iWinHeight-1 ); |
| 1644 | |
| 1645 | // We save the modelview matrix |
| 1646 | glMatrixMode( GL_MODELVIEW ); |
| 1647 | glPushMatrix(); |
| 1648 | |
| 1649 | // Do not display the compass if unrequired |
| 1650 | if (!_bDisplayCompass) |
| 1651 | goto _displayCloud; |
| 1652 | |
| 1653 | // Display the compass |
| 1654 | glLoadIdentity(); |
| 1655 | glTranslatef( _iWinWidth/2 + 223, 32, 0 ); |
| 1656 | glRotated( _dYRotateAngle, .0, .0, 1. ); |
| 1657 | glBegin( GL_LINES ); |
| 1658 | glColor4f( .5, .5, .5, 1. ); |
| 1659 | glVertex2i( -20, 0 ); |
| 1660 | glVertex2i( 20, 0 ); |
| 1661 | glColor4f( .8, .8, .8, 1. ); |
| 1662 | glVertex2i( 0, -20 ); |
| 1663 | glVertex2i( 0, 20 ); |
| 1664 | glEnd(); |
| 1665 | glBegin( GL_TRIANGLES ); |
| 1666 | glVertex2i( -5, 14 ); |
| 1667 | glVertex2i( 5, 14 ); |
| 1668 | glVertex2i( 0, 20 ); |
| 1669 | glEnd(); |
| 1670 | |
| 1671 | // Display the minimap |
| 1672 | glEnable( GL_BLEND ); |
| 1673 | glEnable( GL_TEXTURE_2D ); |
| 1674 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); |
| 1675 | glBindTexture( GL_TEXTURE_2D, _uiMinimapTex ); |
| 1676 | glLoadIdentity(); |
| 1677 | glTranslatef( _iWinWidth/2 + 195, 3, 0 ); |
| 1678 | |
| 1679 | // Display the textured quad |
| 1680 | glBegin( GL_QUADS ); |
| 1681 | glTexCoord2i( 0, 0 ); glVertex2i( 0, 0 ); |
| 1682 | glTexCoord2i( 0, 1 ); glVertex2i( 0, 56 ); |
| 1683 | glTexCoord2i( 1, 1 ); glVertex2i( 56, 56 ); |
| 1684 | glTexCoord2i( 1, 0 ); glVertex2i( 56, 0 ); |
| 1685 | glEnd(); |
| 1686 | |
| 1687 | // Display the cloud |
| 1688 | _displayCloud: |
| 1689 | glDisable( GL_BLEND ); |
| 1690 | glEnable( GL_TEXTURE_2D ); |
| 1691 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); |
| 1692 | glBindTexture( GL_TEXTURE_2D, moTextureCloud.GetName() ); |
| 1693 | glLoadIdentity(); |
| 1694 | glTranslatef( 0, 0, -1 ); |
| 1695 | |
| 1696 | // Display the textured quad |
| 1697 | glBegin( GL_QUADS ); |
| 1698 | glTexCoord2i( 0, 0 ); glVertex2i( 0 , 0 ); |
| 1699 | glTexCoord2i( 0, 1 ); glVertex2i( 0 , _iWinHeight ); |
| 1700 | glTexCoord2i( 1, 1 ); glVertex2i( _iWinWidth, _iWinHeight ); |
| 1701 | glTexCoord2i( 1, 0 ); glVertex2i( _iWinWidth, 0 ); |
| 1702 | glEnd(); |
| 1703 | |
| 1704 | // Restore the old values |
| 1705 | glPopMatrix(); |
| 1706 | glMatrixMode( GL_PROJECTION ); |
| 1707 | glPopMatrix(); |
| 1708 | glPopAttrib(); |
| 1709 | |
| 1710 | // Restore the modelview matrix |
| 1711 | glMatrixMode( GL_MODELVIEW ); |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | /*=====================================================================*/ |
| 1716 | void |
| 1717 | Renderer::_PrepareView() |
| 1718 | { |
| 1719 | // Clear the ModelView matrix |
| 1720 | glMatrixMode( GL_MODELVIEW ); |
| 1721 | glLoadIdentity(); |
| 1722 | |
| 1723 | // Translate & rotate the map to create an view angle |
| 1724 | if ( _ubProjectionType == OC_PERSPECTIVE ) { |
| 1725 | glRotated( 35, 1., .0, .0 ); |
| 1726 | glTranslated( .0, -18.0, -24.0 ); |
| 1727 | } |
| 1728 | else { |
| 1729 | glRotated( 45, 1., .0, .0 ); |
| 1730 | glTranslated( _iWinWidth / 2, -5.0, -_iWinHeight / 2 ); |
| 1731 | glScalef( 24., 24., 24. ); |
| 1732 | } |
| 1733 | float ratio = _fScaleRatio / 10; |
| 1734 | glScalef( ratio, ratio, ratio); |
| 1735 | |
| 1736 | |
| 1737 | /* you can replace the above commands by this // absolete, outdated |
| 1738 | gluLookAt( 0.0, 80.0, 200.0, |
| 1739 | 0.0, 0.0, 0.0, |
| 1740 | 0.0, 1.0, 0.0 ); |
| 1741 | */ |
| 1742 | |
| 1743 | // Translate all the scence according to _dDeltaX & _dDeltaZ |
| 1744 | // this translation is due to the direction keys |
| 1745 | glTranslated( _dDeltaX, 0.0, _dDeltaZ ); |
| 1746 | |
| 1747 | // Rotate the scence to the required angle |
| 1748 | glMultMatrixd( _dmatrixRotate ); |
| 1749 | } |
| 1750 | |
| 1751 | |
| 1752 | /*=====================================================================*/ |
| 1753 | void |
| 1754 | Renderer::_CalculateCulledGrid |
| 1755 | ( |
| 1756 | uint w1, uint l1, |
| 1757 | uint w2, uint l2, |
| 1758 | bool init |
| 1759 | ) |
| 1760 | { |
| 1761 | #define MIN_SUBDIVISION_SIZE 4 |
| 1762 | #define WINDOW_DELTA 0 |
| 1763 | #define IS_CULLED( x, y ) (x > x1) and (x < x2) and (y > y1) and (y < y2) |
| 1764 | |
| 1765 | static GLdouble daModelViewMatrix[16], daProjectionMatrix[16]; |
| 1766 | static GLint iaViewport[4]; |
| 1767 | static int x1, y1, x2, y2; |
| 1768 | |
| 1769 | // IF the initialization operation is requested THEN get the OpenGL matrices |
| 1770 | if (init) { |
| 1771 | glGetDoublev( GL_MODELVIEW_MATRIX, daModelViewMatrix ); |
| 1772 | glGetDoublev( GL_PROJECTION_MATRIX, daProjectionMatrix ); |
| 1773 | glGetIntegerv( GL_VIEWPORT, iaViewport); |
| 1774 | x1 = -WINDOW_DELTA; |
| 1775 | y1 = -WINDOW_DELTA; |
| 1776 | x2 = _iWinWidth + WINDOW_DELTA; |
| 1777 | y2 = _iWinHeight + WINDOW_DELTA; |
| 1778 | } |
| 1779 | |
| 1780 | double x, y, z; // Window coordinates |
| 1781 | uint gridWidth = _uiCityWidth+1; |
| 1782 | bool a, b, c, d; // True if the corresponding part is culled (selected) |
| 1783 | bool culled; |
| 1784 | |
| 1785 | // First part A |
| 1786 | gluProject( w1, 0, l1, daModelViewMatrix, daProjectionMatrix, iaViewport, &x, &y, &z ); |
| 1787 | a = IS_CULLED( x, y ); |
| 1788 | // OPENCITY_DEBUG( "a/x/y/z " << a << " / " << x << "/" << y << "/" << z ); |
| 1789 | // Second part B |
| 1790 | gluProject( w1, 0, l2, daModelViewMatrix, daProjectionMatrix, iaViewport, &x, &y, &z ); |
| 1791 | b = IS_CULLED( x, y ); |
| 1792 | // OPENCITY_DEBUG( "b/x/y/z " << b << " / " << x << "/" << y << "/" << z ); |
| 1793 | // Third part C |
| 1794 | gluProject( w2, 0, l2, daModelViewMatrix, daProjectionMatrix, iaViewport, &x, &y, &z ); |
| 1795 | c = IS_CULLED( x, y ); |
| 1796 | // OPENCITY_DEBUG( "c/x/y/z " << c << " / " << x << "/" << y << "/" << z ); |
| 1797 | // Fourth part D |
| 1798 | gluProject( w2, 0, l1, daModelViewMatrix, daProjectionMatrix, iaViewport, &x, &y, &z ); |
| 1799 | d = IS_CULLED( x, y ); |
| 1800 | // OPENCITY_DEBUG( "d/x/y/z " << d << " / " << x << "/" << y << "/" << z ); |
| 1801 | |
| 1802 | // IF the working surface is small enough THEN |
| 1803 | uint linear; // Array linear index |
| 1804 | if ((w2-w1 <= MIN_SUBDIVISION_SIZE) || (l2-l1 <= MIN_SUBDIVISION_SIZE)) { |
| 1805 | culled = a or b or c or d; |
| 1806 | // OPENCITY_DEBUG( "culled? w1/l1_w2/l2: " << culled << " " << w1 << "/" << l1 << " _ " << w2 << "/" << l2 ); |
| 1807 | for (uint l = l1; l < l2; l++) { |
| 1808 | linear = l*gridWidth + w1; |
| 1809 | for (uint w = w1; w < w2; w++, linear++) |
| 1810 | _baCulledGrid[linear] = culled; |
| 1811 | } |
| 1812 | } |
| 1813 | else { |
| 1814 | // IF all culled (selected) THEN |
| 1815 | culled = a and b and c and d; |
| 1816 | if (culled) { |
| 1817 | // OPENCITY_DEBUG( "culled: w1/l1_w2/l2: " << w1 << "/" << l1 << " _ " << w2 << "/" << l2 ); |
| 1818 | for (uint l = l1; l < l2; l++) { |
| 1819 | linear = l*gridWidth + w1; |
| 1820 | for (uint w = w1; w < w2; w++, linear++) |
| 1821 | _baCulledGrid[linear] = true; |
| 1822 | } |
| 1823 | } else |
| 1824 | // IF all not culled (not selected) THEN divide the grid into four parts |
| 1825 | { |
| 1826 | // OPENCITY_DEBUG( "unknown: w1/l1_w2/l2: " << w1 << "/" << l1 << " _ " << w2 << "/" << l2 ); |
| 1827 | uint mw = (w2-w1)/2; |
| 1828 | uint ml = (l2-l1)/2; |
| 1829 | _CalculateCulledGrid(w1, l1, w1+mw, l1+ml); |
| 1830 | _CalculateCulledGrid(w1, l1+ml, w1+mw, l2); |
| 1831 | _CalculateCulledGrid(w1+mw, l1+ml, w2, l2); |
| 1832 | _CalculateCulledGrid(w1+mw, l1, w2, l1+ml); |
| 1833 | } |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | |
| 1838 | /*=====================================================================*/ |
| 1839 | void |
| 1840 | Renderer::_CalculateCulledModel( const Layer* pcLayer ) |
| 1841 | { |
| 1842 | uint linear, w, l; |
| 1843 | uint sw, sl, sh; // Structure's width, length and height |
| 1844 | uint gridWidth = _uiCityWidth+1; |
| 1845 | bool x0y0, x1y0, x1y1, x0y1; // True if the corresponding part is culled (selected) |
| 1846 | bool culled; |
| 1847 | const Structure* pStructure; |
| 1848 | |
| 1849 | linear = 0; |
| 1850 | for (l = 0; l < _uiCityLength; l++) { |
| 1851 | for (w = 0; w < _uiCityWidth; w++, linear++) { |
| 1852 | // We process main structures only |
| 1853 | pStructure = pcLayer->GetLinearStructure( linear ); |
| 1854 | if (pStructure == NULL || pStructure->GetMain() != NULL) { |
| 1855 | _baCulledModel[linear] = false; |
| 1856 | continue; |
| 1857 | } |
| 1858 | |
| 1859 | // 4 taps culling calculation from the structure's dimensions |
| 1860 | gVars.gpPropertyMgr->GetWLH( pStructure->GetGraphicCode(), sw, 1, sl, 1, sh, 1 ); |
| 1861 | x0y0 = _baCulledGrid[ w + (l * gridWidth)]; |
| 1862 | x1y0 = _baCulledGrid[(w+sw) + (l * gridWidth)]; |
| 1863 | x1y1 = _baCulledGrid[(w+sw) + ((l+sl) * gridWidth)]; |
| 1864 | x0y1 = _baCulledGrid[ w + ((l+sl) * gridWidth)]; |
| 1865 | culled = x0y0 or x1y0 or x1y1 or x0y1; |
| 1866 | _baCulledModel[linear] = culled; |
| 1867 | } |
| 1868 | } // for |
| 1869 | } |
| 1870 | |
| 1871 | |
| 1872 | /*=====================================================================*/ |
| 1873 | void |
| 1874 | Renderer::_SetLOD() const |
| 1875 | { |
| 1876 | if (gVars.gpGraphicMgr == NULL) |
| 1877 | return; |
| 1878 | |
| 1879 | if (_fScaleRatio >= OC_INITIAL_SCALE) |
| 1880 | gVars.gpGraphicMgr->SetLOD(OC_LOD_HIGH); |
| 1881 | else if (_fScaleRatio >= OC_INITIAL_SCALE - 8) |
| 1882 | gVars.gpGraphicMgr->SetLOD(OC_LOD_MEDIUM); |
| 1883 | else if (_fScaleRatio >= OC_INITIAL_SCALE - 10) |
| 1884 | gVars.gpGraphicMgr->SetLOD(OC_LOD_LOW); |
| 1885 | } |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | |
| 1908 |