[3020] | 1 | // chcdemo.cpp : Defines the entry point for the console application.
|
---|
| 2 | //
|
---|
[3037] | 3 |
|
---|
| 4 |
|
---|
| 5 | #include "common.h"
|
---|
| 6 |
|
---|
| 7 | #ifdef _CRT_SET
|
---|
| 8 | #define _CRTDBG_MAP_ALLOC
|
---|
| 9 | #include <stdlib.h>
|
---|
| 10 | #include <crtdbg.h>
|
---|
| 11 |
|
---|
| 12 | // redefine new operator
|
---|
| 13 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 14 | #define new DEBUG_NEW
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
[3020] | 17 | #include <math.h>
|
---|
| 18 | #include <time.h>
|
---|
[3037] | 19 | #include "glInterface.h"
|
---|
| 20 |
|
---|
| 21 | #include <Cg/cg.h>
|
---|
| 22 | #include <Cg/cgGL.h>
|
---|
| 23 |
|
---|
| 24 |
|
---|
[3020] | 25 | #include "RenderTraverser.h"
|
---|
| 26 | #include "SceneEntity.h"
|
---|
| 27 | #include "Vector3.h"
|
---|
| 28 | #include "Matrix4x4.h"
|
---|
| 29 | #include "ResourceManager.h"
|
---|
| 30 | #include "Bvh.h"
|
---|
| 31 | #include "Camera.h"
|
---|
| 32 | #include "Geometry.h"
|
---|
| 33 | #include "BvhLoader.h"
|
---|
| 34 | #include "FrustumCullingTraverser.h"
|
---|
| 35 | #include "StopAndWaitTraverser.h"
|
---|
| 36 | #include "CHCTraverser.h"
|
---|
| 37 | #include "CHCPlusPlusTraverser.h"
|
---|
| 38 | #include "Visualization.h"
|
---|
| 39 | #include "RenderState.h"
|
---|
| 40 | #include "Timer/PerfTimer.h"
|
---|
| 41 | #include "SceneQuery.h"
|
---|
| 42 | #include "RenderQueue.h"
|
---|
| 43 | #include "Material.h"
|
---|
| 44 | #include "glfont2.h"
|
---|
| 45 | #include "PerformanceGraph.h"
|
---|
| 46 | #include "Environment.h"
|
---|
| 47 | #include "Halton.h"
|
---|
| 48 | #include "Transform3.h"
|
---|
| 49 | #include "SampleGenerator.h"
|
---|
| 50 | #include "FrameBufferObject.h"
|
---|
| 51 | #include "DeferredRenderer.h"
|
---|
| 52 | #include "ShadowMapping.h"
|
---|
| 53 | #include "Light.h"
|
---|
| 54 | #include "SceneEntityConverter.h"
|
---|
| 55 | #include "ObjConverter.h"
|
---|
| 56 | #include "SkyPreetham.h"
|
---|
| 57 | #include "Texture.h"
|
---|
| 58 | #include "EntityMerger.h"
|
---|
| 59 |
|
---|
| 60 | using namespace std;
|
---|
| 61 | using namespace CHCDemoEngine;
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | static Environment env;
|
---|
| 65 |
|
---|
| 66 | #define MAX_DEPTH_CONST 10.0f
|
---|
| 67 |
|
---|
| 68 | // fbo
|
---|
| 69 | FrameBufferObject *fbo = NULL;
|
---|
| 70 |
|
---|
| 71 | GLuint fontTex;
|
---|
| 72 |
|
---|
| 73 | /// the renderable scene geometry
|
---|
| 74 | SceneEntityContainer sceneEntities;
|
---|
[3037] | 75 |
|
---|
[3020] | 76 | // traverses and renders the hierarchy
|
---|
| 77 | RenderTraverser *traverser = NULL;
|
---|
| 78 | /// the hierarchy
|
---|
| 79 | Bvh *bvh = NULL;
|
---|
| 80 | /// handles scene loading
|
---|
| 81 | ResourceManager *loader = NULL;
|
---|
| 82 | /// the scene camera
|
---|
| 83 | Camera *camera = NULL;
|
---|
| 84 | /// the scene camera
|
---|
| 85 | Camera *visCamera = NULL;
|
---|
| 86 | /// the visualization
|
---|
| 87 | Visualization *visualization = NULL;
|
---|
| 88 | /// the current render state
|
---|
| 89 | RenderState state;
|
---|
| 90 | /// the rendering algorithm
|
---|
| 91 | int renderMode = RenderTraverser::CHCPLUSPLUS;
|
---|
| 92 | // eye near plane distance
|
---|
| 93 | float nearDist = 0.2f;
|
---|
| 94 | float farDist = 1e6f;
|
---|
| 95 | /// the field of view
|
---|
| 96 | float fov = 50.0f;
|
---|
| 97 | /// the pixel threshold where a node is still considered invisible
|
---|
| 98 | int threshold;
|
---|
| 99 |
|
---|
| 100 | int assumedVisibleFrames = 10;
|
---|
| 101 | int maxBatchSize = 50;
|
---|
| 102 |
|
---|
| 103 | int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES;
|
---|
| 104 |
|
---|
| 105 | SceneQuery *sceneQuery = NULL;
|
---|
| 106 | RenderQueue *renderQueue = NULL;
|
---|
| 107 |
|
---|
| 108 | // traverses and renders the hierarchy
|
---|
| 109 | RenderTraverser *shadowTraverser = NULL;
|
---|
| 110 |
|
---|
| 111 | SkyPreetham *preetham = NULL;
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | /// the used render type for this render pass
|
---|
| 115 | enum RenderMethod
|
---|
| 116 | {
|
---|
| 117 | RENDER_FIXED,
|
---|
| 118 | RENDER_DEPTH_PASS,
|
---|
| 119 | RENDER_DEFERRED,
|
---|
| 120 | RENDER_DEPTH_PASS_DEFERRED,
|
---|
| 121 | RENDER_NUM_RENDER_TYPES
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 | /// one of four possible render methods
|
---|
| 125 | int renderMethod = RENDER_FIXED;
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /// these values get scaled with the frame rate
|
---|
| 129 | static float keyForwardMotion = 30.0f;
|
---|
| 130 | static float keyRotation = 1.5f;
|
---|
| 131 |
|
---|
| 132 | /// elapsed time in milliseconds
|
---|
| 133 | double elapsedTime = 1000.0f;
|
---|
| 134 | double algTime = 1000.0f;
|
---|
| 135 |
|
---|
| 136 | static int winWidth = 1024;
|
---|
| 137 | static int winHeight = 768;
|
---|
| 138 |
|
---|
| 139 | int shadowSize = 2048;
|
---|
| 140 |
|
---|
| 141 | static float winAspectRatio = 1.0f;
|
---|
| 142 |
|
---|
| 143 | double accumulatedTime = 1000;
|
---|
| 144 | float fps = 1e3f;
|
---|
| 145 |
|
---|
| 146 | glfont::GLFont myfont;
|
---|
| 147 |
|
---|
| 148 | // rendertexture
|
---|
| 149 | static int texWidth = 1024;
|
---|
| 150 | static int texHeight = 768;
|
---|
| 151 |
|
---|
| 152 | int renderedObjects = 0;
|
---|
| 153 | int renderedNodes = 0;
|
---|
| 154 | int renderedTriangles = 0;
|
---|
| 155 |
|
---|
| 156 | int issuedQueries = 0;
|
---|
| 157 | int traversedNodes = 0;
|
---|
| 158 | int frustumCulledNodes = 0;
|
---|
| 159 | int queryCulledNodes = 0;
|
---|
| 160 | int stateChanges = 0;
|
---|
| 161 | int numBatches = 0;
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | // mouse navigation state
|
---|
| 165 | int xEyeBegin = 0;
|
---|
| 166 | int yEyeBegin = 0;
|
---|
| 167 | int yMotionBegin = 0;
|
---|
| 168 | int verticalMotionBegin = 0;
|
---|
| 169 | int horizontalMotionBegin = 0;
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | bool leftKeyPressed = false;
|
---|
| 173 | bool rightKeyPressed = false;
|
---|
| 174 | bool upKeyPressed = false;
|
---|
| 175 | bool downKeyPressed = false;
|
---|
| 176 | bool descendKeyPressed = false;
|
---|
| 177 | bool ascendKeyPressed = false;
|
---|
| 178 |
|
---|
| 179 | bool showHelp = false;
|
---|
| 180 | bool showStatistics = false;
|
---|
| 181 | bool showOptions = true;
|
---|
| 182 | bool showBoundingVolumes = false;
|
---|
| 183 | bool visMode = false;
|
---|
| 184 |
|
---|
| 185 | bool useOptimization = false;
|
---|
| 186 | bool useTightBounds = true;
|
---|
| 187 | bool useRenderQueue = true;
|
---|
| 188 | bool useMultiQueries = true;
|
---|
| 189 | bool flyMode = true;
|
---|
| 190 |
|
---|
| 191 | bool useGlobIllum = false;
|
---|
| 192 | bool useTemporalCoherence = true;
|
---|
| 193 | bool showAlgorithmTime = false;
|
---|
| 194 |
|
---|
| 195 | bool useFullScreen = false;
|
---|
| 196 | bool useLODs = true;
|
---|
| 197 | bool moveLight = false;
|
---|
| 198 |
|
---|
| 199 | bool useAdvancedShading = false;
|
---|
| 200 | bool showShadowMap = false;
|
---|
| 201 | bool renderLightView = false;
|
---|
| 202 |
|
---|
| 203 | bool altKeyPressed = false;
|
---|
| 204 |
|
---|
| 205 | bool useHDR = true;
|
---|
| 206 |
|
---|
| 207 | static float ssaoTempCohFactor = 255.0;
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | PerfTimer frameTimer, algTimer;
|
---|
| 211 |
|
---|
| 212 | static int sCurrentMrtSet = 0;
|
---|
| 213 |
|
---|
| 214 | PerformanceGraph *perfGraph = NULL;
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | //DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_POISSON;
|
---|
| 218 | DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_QUADRATIC;
|
---|
| 219 |
|
---|
| 220 | ShadowMap *shadowMap = NULL;
|
---|
| 221 | DirectionalLight *light = NULL;
|
---|
| 222 | DeferredRenderer *ssaoShader = NULL;
|
---|
| 223 |
|
---|
| 224 | SceneEntity *cube = NULL;
|
---|
| 225 | SceneEntity *aeroplane = NULL;
|
---|
| 226 | SceneEntity *skyDome = NULL;
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | // function forward declarations
|
---|
| 231 | void InitExtensions();
|
---|
| 232 | void DisplayVisualization();
|
---|
| 233 | void InitGLstate();
|
---|
| 234 | void InitRenderTexture();
|
---|
| 235 | void InitCg();
|
---|
| 236 | void CleanUp();
|
---|
| 237 | void SetupEyeView();
|
---|
| 238 | void UpdateEyeMtx();
|
---|
| 239 | void SetupLighting();
|
---|
| 240 | void DisplayStats();
|
---|
| 241 | void Output(int x, int y, const char *string);
|
---|
| 242 | void DrawHelpMessage();
|
---|
| 243 | void RenderSky();
|
---|
| 244 | void RenderVisibleObjects();
|
---|
| 245 |
|
---|
| 246 | void Begin2D();
|
---|
| 247 | void End2D();
|
---|
| 248 | void KeyBoard(unsigned char c, int x, int y);
|
---|
| 249 | void DrawStatistics();
|
---|
| 250 | void Display();
|
---|
| 251 | void Special(int c, int x, int y);
|
---|
| 252 | void KeyUp(unsigned char c, int x, int y);
|
---|
| 253 | void SpecialKeyUp(int c, int x, int y);
|
---|
| 254 | void Reshape(int w, int h);
|
---|
| 255 | void Mouse(int button, int state, int x, int y);
|
---|
| 256 | void LeftMotion(int x, int y);
|
---|
| 257 | void RightMotion(int x, int y);
|
---|
| 258 | void MiddleMotion(int x, int y);
|
---|
| 259 | void CalcDecimalPoint(string &str, int d);
|
---|
| 260 |
|
---|
| 261 | RenderTraverser *CreateTraverser(Camera *cam);
|
---|
| 262 |
|
---|
| 263 | void KeyHorizontalMotion(float shift);
|
---|
| 264 | void KeyVerticalMotion(float shift);
|
---|
| 265 |
|
---|
| 266 | void PlaceViewer(const Vector3 &oldPos);
|
---|
| 267 |
|
---|
| 268 | inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; }
|
---|
| 269 | inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; }
|
---|
[3037] | 270 | // initialise the frame buffer objects
|
---|
[3020] | 271 | void InitFBO();
|
---|
| 272 |
|
---|
| 273 | void RightMotionLight(int x, int y);
|
---|
| 274 |
|
---|
| 275 | void RenderShadowMap(float newfar);
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | /////////
|
---|
| 280 | //-- cg stuff
|
---|
| 281 |
|
---|
| 282 | static CGcontext sCgContext = NULL;
|
---|
| 283 |
|
---|
| 284 | static CGparameter sEyePosParamTex;
|
---|
| 285 | static CGparameter sEyePosParam;
|
---|
| 286 |
|
---|
| 287 | static Matrix4x4 viewProjMat = IdentityMatrix();
|
---|
| 288 | static Matrix4x4 oldViewProjMat = IdentityMatrix();
|
---|
| 289 |
|
---|
| 290 |
|
---|
| 291 |
|
---|
| 292 | static void cgErrorCallback()
|
---|
| 293 | {
|
---|
| 294 | CGerror lastError = cgGetError();
|
---|
| 295 |
|
---|
| 296 | if(lastError)
|
---|
| 297 | {
|
---|
| 298 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
| 299 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
| 300 |
|
---|
| 301 | printf("Cg error, exiting...\n");
|
---|
| 302 |
|
---|
| 303 | exit(0);
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 |
|
---|
| 308 | static void PrintGLerror(char *msg)
|
---|
| 309 | {
|
---|
| 310 | GLenum errCode;
|
---|
| 311 | const GLubyte *errStr;
|
---|
| 312 |
|
---|
| 313 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 314 | {
|
---|
| 315 | errStr = gluErrorString(errCode);
|
---|
| 316 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | int main(int argc, char* argv[])
|
---|
| 322 | {
|
---|
[3037] | 323 | /*
|
---|
| 324 | #ifdef _CRT_SET
|
---|
| 325 |
|
---|
| 326 | //Now just call this function at the start of your program and if you're
|
---|
| 327 | //compiling in debug mode (F5), any leaks will be displayed in the Output
|
---|
| 328 | //window when the program shuts down. If you're not in debug mode this will
|
---|
| 329 | //be ignored. Use it as you will!
|
---|
| 330 | //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
|
---|
| 331 |
|
---|
| 332 | _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
---|
| 333 | _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
|
---|
| 334 | _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
|
---|
| 335 | #endif
|
---|
| 336 | */
|
---|
| 337 | cout << "=== reading environment file ===" << endl << endl;
|
---|
| 338 |
|
---|
[3020] | 339 | int returnCode = 0;
|
---|
| 340 |
|
---|
| 341 | Vector3 camPos(.0f, .0f, .0f);
|
---|
| 342 | Vector3 camDir(.0f, 1.0f, .0f);
|
---|
| 343 | Vector3 lightDir(-0.8f, 1.0f, -0.7f);
|
---|
| 344 |
|
---|
| 345 | cout << "=== reading environment file ===" << endl << endl;
|
---|
| 346 |
|
---|
[3037] | 347 | const string envFileName = "default.env";
|
---|
[3020] | 348 | if (!env.Read(envFileName))
|
---|
| 349 | {
|
---|
| 350 | cerr << "loading environment " << envFileName << " failed!" << endl;
|
---|
| 351 | }
|
---|
| 352 | else
|
---|
| 353 | {
|
---|
| 354 | env.GetIntParam(string("assumedVisibleFrames"), assumedVisibleFrames);
|
---|
| 355 | env.GetIntParam(string("maxBatchSize"), maxBatchSize);
|
---|
| 356 | env.GetIntParam(string("trianglesPerVirtualLeaf"), trianglesPerVirtualLeaf);
|
---|
| 357 |
|
---|
| 358 | env.GetFloatParam(string("keyForwardMotion"), keyForwardMotion);
|
---|
| 359 | env.GetFloatParam(string("keyRotation"), keyRotation);
|
---|
| 360 |
|
---|
| 361 | env.GetIntParam(string("winWidth"), winWidth);
|
---|
| 362 | env.GetIntParam(string("winHeight"), winHeight);
|
---|
| 363 |
|
---|
| 364 | env.GetBoolParam(string("useFullScreen"), useFullScreen);
|
---|
| 365 | env.GetFloatParam(string("tempCohFactor"), ssaoTempCohFactor);
|
---|
| 366 | env.GetVectorParam(string("camPosition"), camPos);
|
---|
| 367 | env.GetVectorParam(string("camDirection"), camDir);
|
---|
| 368 | env.GetVectorParam(string("lightDirection"), lightDir);
|
---|
| 369 |
|
---|
| 370 | env.GetBoolParam(string("useLODs"), useLODs);
|
---|
| 371 | env.GetIntParam(string("shadowSize"), shadowSize);
|
---|
| 372 |
|
---|
| 373 | env.GetBoolParam(string("useHDR"), useHDR);
|
---|
| 374 |
|
---|
| 375 | //env.GetStringParam(string("modelPath"), model_path);
|
---|
| 376 | //env.GetIntParam(string("numSssaoSamples"), numSsaoSamples);
|
---|
| 377 |
|
---|
| 378 | cout << "assumedVisibleFrames: " << assumedVisibleFrames << endl;
|
---|
| 379 | cout << "maxBatchSize: " << maxBatchSize << endl;
|
---|
| 380 | cout << "trianglesPerVirtualLeaf: " << trianglesPerVirtualLeaf << endl;
|
---|
| 381 |
|
---|
| 382 | cout << "keyForwardMotion: " << keyForwardMotion << endl;
|
---|
| 383 | cout << "keyRotation: " << keyRotation << endl;
|
---|
| 384 | cout << "winWidth: " << winWidth << endl;
|
---|
| 385 | cout << "winHeight: " << winHeight << endl;
|
---|
| 386 | cout << "useFullScreen: " << useFullScreen << endl;
|
---|
| 387 | cout << "useLODs: " << useLODs << endl;
|
---|
| 388 | cout << "camPosition: " << camPos << endl;
|
---|
| 389 | cout << "temporal coherence: " << ssaoTempCohFactor << endl;
|
---|
| 390 | cout << "shadow size: " << shadowSize << endl;
|
---|
| 391 |
|
---|
| 392 | //cout << "model path: " << model_path << endl;
|
---|
| 393 |
|
---|
| 394 | cout << "**** end parameters ****" << endl << endl;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | ///////////////////////////
|
---|
| 398 |
|
---|
| 399 | camera = new Camera(winWidth, winHeight, fov);
|
---|
| 400 | camera->SetNear(nearDist);
|
---|
| 401 | camera->SetFar(1000);
|
---|
| 402 |
|
---|
| 403 | camera->SetDirection(camDir);
|
---|
| 404 | camera->SetPosition(camPos);
|
---|
| 405 |
|
---|
| 406 | visCamera = new Camera(winWidth, winHeight, fov);
|
---|
| 407 | visCamera->SetNear(0.0f);
|
---|
| 408 | visCamera->Yaw(.5 * M_PI);
|
---|
| 409 |
|
---|
| 410 | // create a new light
|
---|
| 411 | light = new DirectionalLight(lightDir, RgbaColor(1, 1, 1, 1), RgbaColor(1, 1, 1, 1));
|
---|
| 412 |
|
---|
| 413 |
|
---|
| 414 | renderQueue = new RenderQueue(&state, camera);
|
---|
| 415 |
|
---|
| 416 | glutInitWindowSize(winWidth, winHeight);
|
---|
| 417 | glutInit(&argc, argv);
|
---|
| 418 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
|
---|
| 419 | //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
|
---|
| 420 | //glutInitDisplayString("samples=2");
|
---|
| 421 |
|
---|
| 422 | SceneEntity::SetUseLODs(useLODs);
|
---|
| 423 |
|
---|
| 424 | if (!useFullScreen)
|
---|
| 425 | {
|
---|
| 426 | glutCreateWindow("FriendlyCulling");
|
---|
| 427 | }
|
---|
| 428 | else
|
---|
| 429 | {
|
---|
| 430 | glutGameModeString( "1024x768:32@75" );
|
---|
| 431 | glutEnterGameMode();
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | glutDisplayFunc(Display);
|
---|
| 435 | glutKeyboardFunc(KeyBoard);
|
---|
| 436 | glutSpecialFunc(Special);
|
---|
| 437 | glutReshapeFunc(Reshape);
|
---|
| 438 | glutMouseFunc(Mouse);
|
---|
| 439 | glutIdleFunc(Display);
|
---|
| 440 | glutKeyboardUpFunc(KeyUp);
|
---|
| 441 | glutSpecialUpFunc(SpecialKeyUp);
|
---|
| 442 | glutIgnoreKeyRepeat(true);
|
---|
| 443 |
|
---|
| 444 | // initialise gl graphics
|
---|
| 445 | InitExtensions();
|
---|
| 446 | InitGLstate();
|
---|
| 447 |
|
---|
| 448 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 449 | glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
|
---|
| 450 |
|
---|
| 451 | LeftMotion(0, 0);
|
---|
| 452 | MiddleMotion(0, 0);
|
---|
| 453 |
|
---|
[3037] | 454 | // init cg shader programs
|
---|
| 455 | InitCg();
|
---|
| 456 |
|
---|
[3020] | 457 | perfGraph = new PerformanceGraph(1000);
|
---|
| 458 |
|
---|
[3037] | 459 | loader = ResourceManager::GetSingleton();
|
---|
[3020] | 460 |
|
---|
| 461 | const string filename = string(model_path + "city.dem");
|
---|
[3037] | 462 |
|
---|
[3020] | 463 | if (loader->Load(filename, sceneEntities))
|
---|
| 464 | cout << "model " << filename << " loaded" << endl;
|
---|
| 465 | else
|
---|
| 466 | {
|
---|
| 467 | cerr << "loading model " << filename << " failed" << endl;
|
---|
| 468 | CleanUp();
|
---|
| 469 | exit(0);
|
---|
| 470 | }
|
---|
[3037] | 471 |
|
---|
[3020] | 472 |
|
---|
| 473 | const string bvh_filename = string(model_path + "city.bvh");
|
---|
| 474 | BvhLoader bvhLoader;
|
---|
| 475 | bvh = bvhLoader.Load(bvh_filename, sceneEntities);
|
---|
| 476 |
|
---|
| 477 | if (!bvh)
|
---|
| 478 | {
|
---|
| 479 | cerr << "loading bvh " << bvh_filename << " failed" << endl;
|
---|
| 480 | CleanUp();
|
---|
| 481 | exit(0);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | // set far plane based on scene extent
|
---|
| 485 | farDist = 10.0f * Magnitude(bvh->GetBox().Diagonal());
|
---|
| 486 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 487 |
|
---|
| 488 | camera->SetFar(Magnitude(bvh->GetBox().Diagonal()));
|
---|
| 489 |
|
---|
| 490 | Vector3 cubeCenter(470.398f, 240.364f, 182.5f);
|
---|
| 491 |
|
---|
| 492 | Matrix4x4 transl = TranslationMatrix(cubeCenter);
|
---|
| 493 |
|
---|
| 494 | SceneEntityContainer dummy;
|
---|
| 495 |
|
---|
| 496 | string skyDomeStr(model_path + "sky.dem");
|
---|
| 497 |
|
---|
[3037] | 498 | if (loader->Load(skyDomeStr, sceneEntities))
|
---|
| 499 | {
|
---|
| 500 | cout << "successfully loaded " << sceneEntities.size() << " scene entities" << endl;
|
---|
| 501 | }
|
---|
[3020] | 502 | else
|
---|
| 503 | {
|
---|
| 504 | cerr << "loading file " << skyDomeStr << " failed" << endl;
|
---|
| 505 |
|
---|
| 506 | CleanUp();
|
---|
| 507 | exit(0);
|
---|
| 508 | }
|
---|
| 509 |
|
---|
[3037] | 510 | skyDome = sceneEntities.back();
|
---|
[3020] | 511 |
|
---|
[3037] | 512 | //InitCg();
|
---|
[3020] | 513 |
|
---|
| 514 | const float turbitiy = 5.0f;
|
---|
| 515 | preetham = new SkyPreetham(turbitiy, skyDome);
|
---|
| 516 |
|
---|
| 517 | // initialize the render traverser
|
---|
| 518 | traverser = CreateTraverser(camera);
|
---|
| 519 |
|
---|
| 520 | visualization = new Visualization(bvh, camera, NULL, &state);
|
---|
| 521 |
|
---|
| 522 | state.SetRenderPassType(RenderState::FIXED);
|
---|
| 523 |
|
---|
| 524 | // frame time is restarted every frame
|
---|
| 525 | frameTimer.Start();
|
---|
| 526 |
|
---|
| 527 | // the rendering loop
|
---|
| 528 | glutMainLoop();
|
---|
| 529 |
|
---|
| 530 | // clean up
|
---|
| 531 | CleanUp();
|
---|
[3037] | 532 |
|
---|
[3020] | 533 | return 0;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 |
|
---|
| 537 | void InitCg(void)
|
---|
| 538 | {
|
---|
[3037] | 539 | // setup cg
|
---|
[3020] | 540 | cgSetErrorCallback(cgErrorCallback);
|
---|
| 541 |
|
---|
[3037] | 542 | // create context.
|
---|
[3020] | 543 | sCgContext = cgCreateContext();
|
---|
| 544 |
|
---|
[3037] | 545 | // get the optimal profile
|
---|
[3020] | 546 | RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
| 547 | cgGLSetOptimalOptions(RenderState::sCgFragmentProfile);
|
---|
| 548 |
|
---|
| 549 | RenderState::sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
|
---|
| 550 | cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
|
---|
| 551 |
|
---|
[3037] | 552 | // set textures to auto load
|
---|
| 553 | cgGLSetManageTextureParameters(sCgContext, false);
|
---|
[3020] | 554 |
|
---|
[3037] | 555 | RenderState::sCgMrtVertexProgram =
|
---|
| 556 | new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgVertexProfile, "vtx");
|
---|
[3020] | 557 |
|
---|
[3037] | 558 | if (!RenderState::sCgMrtVertexProgram->IsValid())
|
---|
| 559 | cerr << "fragment program failed to load" << endl;
|
---|
[3020] | 560 |
|
---|
| 561 | RenderState::sCgMrtFragmentTexProgram =
|
---|
[3037] | 562 | new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "fragtex");
|
---|
[3020] | 563 |
|
---|
| 564 |
|
---|
[3037] | 565 | if (!RenderState::sCgMrtFragmentTexProgram->IsValid())
|
---|
[3020] | 566 | cerr << "fragment tex program failed to load" << endl;
|
---|
| 567 |
|
---|
[3037] | 568 | RenderState::sCgMrtFragmentTexProgram->AddParameter("tex", 0);
|
---|
| 569 |
|
---|
[3020] | 570 | RenderState::sCgMrtFragmentProgram =
|
---|
[3037] | 571 | new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "frag");
|
---|
[3020] | 572 |
|
---|
[3037] | 573 | if (!RenderState::sCgMrtFragmentProgram->IsValid())
|
---|
[3020] | 574 | cerr << "fragment program failed to load" << endl;
|
---|
| 575 |
|
---|
| 576 |
|
---|
| 577 | PrintGLerror("init");
|
---|
| 578 |
|
---|
[3037] | 579 | DeferredRenderer::InitCG(sCgContext);
|
---|
| 580 | SkyPreetham::InitCG(sCgContext);
|
---|
| 581 |
|
---|
[3020] | 582 | cout << "cg initialization successful" << endl;
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 |
|
---|
| 586 | void InitFBO()
|
---|
| 587 | {
|
---|
| 588 | PrintGLerror("fbo start");
|
---|
| 589 |
|
---|
| 590 | // this fbo basicly stores the scene information we get from standard rendering of a frame
|
---|
| 591 | // we store colors, normals, positions (for the ssao)
|
---|
| 592 | fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32);
|
---|
| 593 | //fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24);
|
---|
| 594 |
|
---|
| 595 | // the diffuse color buffer
|
---|
[3037] | 596 | fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST);
|
---|
[3020] | 597 | // the normals buffer
|
---|
[3037] | 598 | fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
|
---|
[3020] | 599 | // the positions buffer
|
---|
| 600 | //fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
|
---|
| 601 | fbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
|
---|
| 602 | // another color buffer
|
---|
[3037] | 603 | fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST);
|
---|
[3020] | 604 |
|
---|
| 605 | PrintGLerror("fbo");
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 |
|
---|
| 609 | bool InitFont(void)
|
---|
| 610 | {
|
---|
| 611 | glEnable(GL_TEXTURE_2D);
|
---|
| 612 |
|
---|
| 613 | glGenTextures(1, &fontTex);
|
---|
| 614 | glBindTexture(GL_TEXTURE_2D, fontTex);
|
---|
[3037] | 615 |
|
---|
[3020] | 616 | if (!myfont.Create("data/fonts/verdana.glf", fontTex))
|
---|
| 617 | return false;
|
---|
| 618 |
|
---|
| 619 | glDisable(GL_TEXTURE_2D);
|
---|
| 620 |
|
---|
| 621 | return true;
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 |
|
---|
| 625 | void InitGLstate()
|
---|
| 626 | {
|
---|
| 627 | glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
|
---|
| 628 |
|
---|
| 629 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
---|
| 630 | glPixelStorei(GL_PACK_ALIGNMENT,1);
|
---|
| 631 |
|
---|
| 632 | glDepthFunc(GL_LESS);
|
---|
| 633 | glEnable(GL_DEPTH_TEST);
|
---|
| 634 |
|
---|
| 635 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 636 | glShadeModel(GL_SMOOTH);
|
---|
| 637 |
|
---|
| 638 | glMaterialf(GL_FRONT, GL_SHININESS, 64);
|
---|
| 639 | glEnable(GL_NORMALIZE);
|
---|
| 640 |
|
---|
| 641 | glDisable(GL_ALPHA_TEST);
|
---|
| 642 | glAlphaFunc(GL_GEQUAL, 0.5f);
|
---|
| 643 |
|
---|
| 644 | glFrontFace(GL_CCW);
|
---|
| 645 | glCullFace(GL_BACK);
|
---|
| 646 | glEnable(GL_CULL_FACE);
|
---|
| 647 |
|
---|
| 648 | glDisable(GL_TEXTURE_2D);
|
---|
| 649 |
|
---|
| 650 | GLfloat ambientColor[] = {0.2, 0.2, 0.2, 1.0};
|
---|
| 651 | GLfloat diffuseColor[] = {1.0, 0.0, 0.0, 1.0};
|
---|
| 652 | GLfloat specularColor[] = {0.0, 0.0, 0.0, 1.0};
|
---|
| 653 |
|
---|
| 654 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambientColor);
|
---|
| 655 | glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor);
|
---|
| 656 | glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
|
---|
| 657 |
|
---|
| 658 | glDepthFunc(GL_LESS);
|
---|
| 659 |
|
---|
| 660 | if (!InitFont())
|
---|
| 661 | cerr << "font creation failed" << endl;
|
---|
| 662 | else
|
---|
| 663 | cout << "successfully created font" << endl;
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 | //////////////////////////////
|
---|
| 667 |
|
---|
| 668 | //GLfloat lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
---|
| 669 | GLfloat lmodel_ambient[] = {0.7f, 0.7f, 0.8f, 1.0f};
|
---|
| 670 |
|
---|
| 671 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
---|
| 672 | //glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
---|
| 673 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
|
---|
| 674 | glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SINGLE_COLOR_EXT);
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 |
|
---|
| 678 | void DrawHelpMessage()
|
---|
| 679 | {
|
---|
| 680 | const char *message[] =
|
---|
| 681 | {
|
---|
| 682 | "Help information",
|
---|
| 683 | "",
|
---|
| 684 | "'F1' - shows/dismisses this message",
|
---|
| 685 | "'F2' - shows/hides bird eye view",
|
---|
| 686 | "'F3' - shows/hides bounds (boxes or tight bounds)",
|
---|
| 687 | "'F4', - shows/hides parameters",
|
---|
| 688 | "'F5' - shows/hides statistics",
|
---|
| 689 | "'F6', - toggles between fly/walkmode",
|
---|
| 690 | "'F7', - cycles throw render modes",
|
---|
| 691 | "'F8', - enables/disables ambient occlusion (only deferred)",
|
---|
| 692 | "'F9', - shows pure algorithm render time (using glFinish)",
|
---|
| 693 | "'SPACE' - cycles through occlusion culling algorithms",
|
---|
| 694 | "",
|
---|
| 695 | "'MOUSE LEFT' - turn left/right, move forward/backward",
|
---|
| 696 | "'MOUSE RIGHT' - turn left/right, move forward/backward",
|
---|
| 697 | "'MOUSE MIDDLE' - move up/down, left/right",
|
---|
| 698 | "'CURSOR UP/DOWN' - move forward/backward",
|
---|
| 699 | "'CURSOR LEFT/RIGHT' - turn left/right",
|
---|
| 700 | "",
|
---|
| 701 | "'-'/'+' - decreases/increases max batch size",
|
---|
| 702 | "'1'/'2' - downward/upward motion",
|
---|
| 703 | "'3'/'4' - decreases/increases triangles per virtual bvh leaf (sets bvh depth)",
|
---|
| 704 | "'5'/'6' - decreases/increases assumed visible frames",
|
---|
| 705 | "",
|
---|
| 706 | "'R' - use render queue",
|
---|
| 707 | "'B' - use tight bounds",
|
---|
| 708 | "'M' - use multiqueries",
|
---|
| 709 | "'O' - use CHC optimization (geometry queries for leaves)",
|
---|
| 710 | 0,
|
---|
| 711 | };
|
---|
| 712 |
|
---|
| 713 |
|
---|
| 714 | glColor4f(0.0f, 1.0f , 0.0f, 0.2f); // 20% green.
|
---|
| 715 |
|
---|
| 716 | glRecti(30, 30, winWidth - 30, winHeight - 30);
|
---|
| 717 |
|
---|
| 718 | glEnd();
|
---|
| 719 |
|
---|
| 720 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 721 |
|
---|
| 722 | glEnable(GL_TEXTURE_2D);
|
---|
| 723 | myfont.Begin();
|
---|
| 724 |
|
---|
| 725 | int x = 40, y = 30;
|
---|
| 726 |
|
---|
| 727 | for(int i = 0; message[i] != 0; ++ i)
|
---|
| 728 | {
|
---|
| 729 | if(message[i][0] == '\0')
|
---|
| 730 | {
|
---|
| 731 | y += 15;
|
---|
| 732 | }
|
---|
| 733 | else
|
---|
| 734 | {
|
---|
| 735 | myfont.DrawString(message[i], x, winHeight - y);
|
---|
| 736 | y += 25;
|
---|
| 737 | }
|
---|
| 738 | }
|
---|
| 739 | glDisable(GL_TEXTURE_2D);
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 |
|
---|
| 743 | RenderTraverser *CreateTraverser(Camera *cam)
|
---|
| 744 | {
|
---|
| 745 | RenderTraverser *tr;
|
---|
| 746 |
|
---|
| 747 | bvh->ResetNodeClassifications();
|
---|
| 748 |
|
---|
| 749 | switch (renderMode)
|
---|
| 750 | {
|
---|
| 751 | case RenderTraverser::CULL_FRUSTUM:
|
---|
| 752 | tr = new FrustumCullingTraverser();
|
---|
| 753 | break;
|
---|
| 754 | case RenderTraverser::STOP_AND_WAIT:
|
---|
| 755 | tr = new StopAndWaitTraverser();
|
---|
| 756 | break;
|
---|
| 757 | case RenderTraverser::CHC:
|
---|
| 758 | tr = new CHCTraverser();
|
---|
| 759 | break;
|
---|
| 760 | case RenderTraverser::CHCPLUSPLUS:
|
---|
| 761 | tr = new CHCPlusPlusTraverser();
|
---|
| 762 | break;
|
---|
| 763 |
|
---|
| 764 | default:
|
---|
| 765 | tr = new FrustumCullingTraverser();
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | tr->SetCamera(cam);
|
---|
| 769 | tr->SetHierarchy(bvh);
|
---|
| 770 | tr->SetRenderQueue(renderQueue);
|
---|
| 771 | tr->SetRenderState(&state);
|
---|
| 772 | tr->SetUseOptimization(useOptimization);
|
---|
| 773 | tr->SetUseRenderQueue(useRenderQueue);
|
---|
| 774 | tr->SetVisibilityThreshold(threshold);
|
---|
| 775 | tr->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 776 | tr->SetMaxBatchSize(maxBatchSize);
|
---|
| 777 | tr->SetUseMultiQueries(useMultiQueries);
|
---|
| 778 | tr->SetUseTightBounds(useTightBounds);
|
---|
| 779 | tr->SetUseDepthPass((renderMethod == RENDER_DEPTH_PASS) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED));
|
---|
| 780 | tr->SetRenderQueue(renderQueue);
|
---|
| 781 |
|
---|
| 782 | return tr;
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 |
|
---|
| 786 | void SetupLighting()
|
---|
| 787 | {
|
---|
| 788 | glEnable(GL_LIGHT0);
|
---|
| 789 | glDisable(GL_LIGHT1);
|
---|
| 790 |
|
---|
| 791 | Vector3 lightDir = -light->GetDirection();
|
---|
| 792 |
|
---|
| 793 |
|
---|
| 794 | ///////////
|
---|
| 795 | //-- first light: sunlight
|
---|
| 796 |
|
---|
| 797 | GLfloat ambient[] = {0.25f, 0.25f, 0.3f, 1.0f};
|
---|
| 798 | GLfloat diffuse[] = {1.0f, 0.95f, 0.85f, 1.0f};
|
---|
| 799 | GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
---|
| 800 |
|
---|
| 801 |
|
---|
| 802 | const bool useToneMapping = ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED)) && useHDR;
|
---|
| 803 |
|
---|
| 804 | Vector3 sunAmbient;
|
---|
| 805 | Vector3 sunDiffuse;
|
---|
| 806 |
|
---|
| 807 | preetham->ComputeSunColor(lightDir, sunAmbient, sunDiffuse, !useToneMapping);
|
---|
| 808 |
|
---|
| 809 | ambient[0] = sunAmbient.x;
|
---|
| 810 | ambient[1] = sunAmbient.y;
|
---|
| 811 | ambient[2] = sunAmbient.z;
|
---|
| 812 |
|
---|
| 813 | // no tone mapping => scale
|
---|
| 814 | if (!useToneMapping)
|
---|
| 815 | {
|
---|
| 816 | const float maxComponent = sunDiffuse.MaxComponent();
|
---|
| 817 | sunDiffuse /= maxComponent;
|
---|
| 818 | }
|
---|
| 819 |
|
---|
| 820 | diffuse[0] = sunDiffuse.x;
|
---|
| 821 | diffuse[1] = sunDiffuse.y;
|
---|
| 822 | diffuse[2] = sunDiffuse.z;
|
---|
| 823 |
|
---|
| 824 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
|
---|
| 825 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
|
---|
| 826 | glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
|
---|
| 827 |
|
---|
| 828 | GLfloat position[] = {lightDir.x, lightDir.y, lightDir.z, 0.0f};
|
---|
| 829 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
| 830 | }
|
---|
| 831 |
|
---|
| 832 |
|
---|
| 833 | void SetupEyeView()
|
---|
| 834 | {
|
---|
| 835 | // store matrix of last frame
|
---|
| 836 | oldViewProjMat = viewProjMat;
|
---|
| 837 |
|
---|
| 838 | glMatrixMode(GL_PROJECTION);
|
---|
| 839 | glLoadIdentity();
|
---|
| 840 | gluPerspective(fov, winAspectRatio, nearDist, farDist);
|
---|
| 841 |
|
---|
| 842 | glMatrixMode(GL_MODELVIEW);
|
---|
| 843 |
|
---|
| 844 | // set up the camera view
|
---|
| 845 | camera->SetupCameraView();
|
---|
| 846 |
|
---|
| 847 |
|
---|
| 848 | /////////////////
|
---|
| 849 |
|
---|
| 850 | Matrix4x4 matViewing, matProjection;
|
---|
| 851 |
|
---|
| 852 | camera->GetModelViewMatrix(matViewing);
|
---|
| 853 | camera->GetProjectionMatrix(matProjection);
|
---|
| 854 |
|
---|
| 855 | // store matrix for later use
|
---|
| 856 | viewProjMat = matViewing * matProjection;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 |
|
---|
| 860 | void KeyHorizontalMotion(float shift)
|
---|
| 861 | {
|
---|
| 862 | Vector3 hvec = -camera->GetDirection();
|
---|
| 863 | hvec.z = 0;
|
---|
| 864 |
|
---|
| 865 | Vector3 pos = camera->GetPosition();
|
---|
| 866 | pos += hvec * shift;
|
---|
| 867 |
|
---|
| 868 | camera->SetPosition(pos);
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 |
|
---|
| 872 | void KeyVerticalMotion(float shift)
|
---|
| 873 | {
|
---|
| 874 | Vector3 uvec = Vector3(0, 0, shift);
|
---|
| 875 |
|
---|
| 876 | Vector3 pos = camera->GetPosition();
|
---|
| 877 | pos += uvec;
|
---|
| 878 |
|
---|
| 879 | camera->SetPosition(pos);
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 |
|
---|
| 883 | static void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
|
---|
| 884 | {
|
---|
| 885 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
| 886 |
|
---|
| 887 | camera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
| 888 |
|
---|
| 889 | bl = Normalize(nbl - fbl);
|
---|
| 890 | br = Normalize(nbr - fbr);
|
---|
| 891 | tl = Normalize(ntl - ftl);
|
---|
| 892 | tr = Normalize(ntr - ftr);
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 |
|
---|
| 896 | void InitDeferredRendering()
|
---|
| 897 | {
|
---|
| 898 | if (!fbo) InitFBO();
|
---|
| 899 | fbo->Bind();
|
---|
| 900 |
|
---|
| 901 | // multisampling does not work with deferred shading
|
---|
| 902 | glDisable(GL_MULTISAMPLE_ARB);
|
---|
| 903 |
|
---|
| 904 | state.SetRenderPassType(RenderState::DEFERRED);
|
---|
| 905 |
|
---|
| 906 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
[3037] | 907 | RenderState::sCgMrtVertexProgram->Bind();
|
---|
[3020] | 908 |
|
---|
| 909 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
[3037] | 910 | RenderState::sCgMrtFragmentProgram->Bind();
|
---|
[3020] | 911 |
|
---|
| 912 | const Vector3 pos = camera->GetPosition();
|
---|
| 913 |
|
---|
| 914 |
|
---|
| 915 | // draw to 3 color buffers
|
---|
| 916 | // a color, normal, and positions buffer
|
---|
| 917 | if (sCurrentMrtSet == 0)
|
---|
| 918 | {
|
---|
| 919 | DeferredRenderer::colorBufferIdx = 0;
|
---|
| 920 | glDrawBuffers(2, mrt);
|
---|
| 921 | }
|
---|
| 922 | else
|
---|
| 923 | {
|
---|
| 924 | DeferredRenderer::colorBufferIdx = 3;
|
---|
| 925 | glDrawBuffers(2, mrt2);
|
---|
| 926 | }
|
---|
| 927 |
|
---|
| 928 | sCurrentMrtSet = 1 - sCurrentMrtSet;
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 |
|
---|
| 932 | // the main rendering loop
|
---|
| 933 | void Display()
|
---|
| 934 | {
|
---|
| 935 | Vector3 oldPos = camera->GetPosition();
|
---|
| 936 |
|
---|
| 937 | if (leftKeyPressed)
|
---|
| 938 | camera->Pitch(KeyRotationAngle());
|
---|
| 939 | if (rightKeyPressed)
|
---|
| 940 | camera->Pitch(-KeyRotationAngle());
|
---|
| 941 | if (upKeyPressed)
|
---|
| 942 | KeyHorizontalMotion(-KeyShift());
|
---|
| 943 | if (downKeyPressed)
|
---|
| 944 | KeyHorizontalMotion(KeyShift());
|
---|
| 945 | if (ascendKeyPressed)
|
---|
| 946 | KeyVerticalMotion(KeyShift());
|
---|
| 947 | if (descendKeyPressed)
|
---|
| 948 | KeyVerticalMotion(-KeyShift());
|
---|
| 949 |
|
---|
| 950 | // place view on ground
|
---|
| 951 | if (!flyMode) PlaceViewer(oldPos);
|
---|
| 952 |
|
---|
| 953 | if (showAlgorithmTime)
|
---|
| 954 | {
|
---|
| 955 | glFinish();
|
---|
| 956 | algTimer.Start();
|
---|
| 957 | }
|
---|
| 958 |
|
---|
| 959 |
|
---|
| 960 |
|
---|
| 961 | if ((!shadowMap || !shadowTraverser) && (showShadowMap || renderLightView))
|
---|
| 962 | {
|
---|
| 963 | if (!shadowMap)
|
---|
| 964 | shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera);
|
---|
| 965 |
|
---|
| 966 | if (!shadowTraverser)
|
---|
| 967 | shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());
|
---|
| 968 |
|
---|
| 969 | }
|
---|
| 970 |
|
---|
| 971 | // bring eye modelview matrix up-to-date
|
---|
| 972 | SetupEyeView();
|
---|
| 973 |
|
---|
| 974 | // hack
|
---|
| 975 | int oldRenderMethod = renderMethod;
|
---|
| 976 |
|
---|
| 977 | if (renderLightView)
|
---|
| 978 | renderMethod = RenderState::FIXED;
|
---|
| 979 |
|
---|
| 980 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 981 |
|
---|
| 982 |
|
---|
| 983 | // render with the specified method (forward rendering, forward + depth, deferred)
|
---|
| 984 | switch (renderMethod)
|
---|
| 985 | {
|
---|
| 986 | case RENDER_FIXED:
|
---|
| 987 |
|
---|
| 988 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 989 |
|
---|
| 990 | state.SetRenderPassType(RenderState::FIXED);
|
---|
| 991 | glEnable(GL_LIGHTING);
|
---|
| 992 |
|
---|
| 993 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 994 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 995 |
|
---|
| 996 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 997 |
|
---|
| 998 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 999 |
|
---|
| 1000 | break;
|
---|
| 1001 |
|
---|
| 1002 | case RENDER_DEPTH_PASS_DEFERRED:
|
---|
| 1003 |
|
---|
| 1004 | glDisable(GL_MULTISAMPLE_ARB);
|
---|
| 1005 | state.SetUseAlphaToCoverage(false);
|
---|
| 1006 |
|
---|
| 1007 | state.SetRenderPassType(RenderState::DEPTH_PASS);
|
---|
| 1008 |
|
---|
| 1009 | if (!fbo) InitFBO(); fbo->Bind();
|
---|
| 1010 |
|
---|
| 1011 | glDrawBuffers(1, mrt);
|
---|
| 1012 |
|
---|
| 1013 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1014 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 1015 |
|
---|
| 1016 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1017 |
|
---|
| 1018 | // the scene is rendered withouth any shading
|
---|
| 1019 | glShadeModel(GL_FLAT);
|
---|
| 1020 | glDisable(GL_LIGHTING);
|
---|
| 1021 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 1022 |
|
---|
| 1023 |
|
---|
| 1024 | break;
|
---|
| 1025 |
|
---|
| 1026 | case RENDER_DEPTH_PASS:
|
---|
| 1027 |
|
---|
| 1028 | glEnable(GL_MULTISAMPLE_ARB);
|
---|
| 1029 |
|
---|
| 1030 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1031 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 1032 |
|
---|
| 1033 | state.SetRenderPassType(RenderState::DEPTH_PASS);
|
---|
| 1034 |
|
---|
| 1035 | // the scene is rendered withouth any shading
|
---|
| 1036 | glShadeModel(GL_FLAT);
|
---|
| 1037 | glDisable(GL_LIGHTING);
|
---|
| 1038 |
|
---|
| 1039 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1040 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 1041 |
|
---|
| 1042 |
|
---|
| 1043 | break;
|
---|
| 1044 |
|
---|
| 1045 | case RenderState::DEFERRED:
|
---|
| 1046 |
|
---|
| 1047 | if (showShadowMap && !renderLightView)
|
---|
| 1048 | RenderShadowMap(camera->GetFar());
|
---|
| 1049 |
|
---|
| 1050 | //glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1051 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 1052 |
|
---|
| 1053 | InitDeferredRendering();
|
---|
| 1054 |
|
---|
| 1055 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 1056 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1057 |
|
---|
| 1058 | break;
|
---|
| 1059 | }
|
---|
| 1060 |
|
---|
| 1061 | glDepthFunc(GL_LESS);
|
---|
| 1062 |
|
---|
| 1063 | glDisable(GL_TEXTURE_2D);
|
---|
| 1064 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
| 1065 |
|
---|
| 1066 |
|
---|
| 1067 | // reset lod levels for current frame
|
---|
| 1068 | LODLevel::InitFrame(camera->GetPosition());
|
---|
| 1069 |
|
---|
| 1070 | // set up lights
|
---|
| 1071 | SetupLighting();
|
---|
| 1072 |
|
---|
| 1073 |
|
---|
| 1074 | if (renderLightView)
|
---|
| 1075 | {
|
---|
| 1076 | // change CHC++ set of state variables (must be done for each change of camera because
|
---|
| 1077 | // otherwise the temporal coherency is broken
|
---|
| 1078 | BvhNode::SetCurrentState(1);
|
---|
| 1079 | shadowMap->RenderShadowView(shadowTraverser, viewProjMat);
|
---|
| 1080 | BvhNode::SetCurrentState(0);
|
---|
| 1081 | }
|
---|
| 1082 | else
|
---|
| 1083 | {
|
---|
| 1084 | // actually render the scene geometry using the specified algorithm
|
---|
| 1085 | traverser->RenderScene();
|
---|
| 1086 | }
|
---|
| 1087 |
|
---|
| 1088 |
|
---|
| 1089 | /////////
|
---|
| 1090 | //-- do the rest of the rendering
|
---|
| 1091 |
|
---|
| 1092 | // reset depth pass and render visible objects
|
---|
| 1093 | if ((renderMethod == RENDER_DEPTH_PASS) ||
|
---|
| 1094 | (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
|
---|
| 1095 | {
|
---|
| 1096 | RenderVisibleObjects();
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 |
|
---|
| 1100 | ///////////////
|
---|
| 1101 | //-- render sky
|
---|
| 1102 |
|
---|
| 1103 | // q: should we render sky after deferred shading?
|
---|
| 1104 | // this would conveniently solves some issues (e.g, skys without shadows)
|
---|
| 1105 |
|
---|
| 1106 | RenderSky();
|
---|
| 1107 |
|
---|
| 1108 |
|
---|
| 1109 | if ((renderMethod == RENDER_DEFERRED) ||
|
---|
| 1110 | (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
|
---|
| 1111 | {
|
---|
| 1112 | FrameBufferObject::Release();
|
---|
| 1113 |
|
---|
| 1114 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 1115 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1116 |
|
---|
| 1117 | if (!ssaoShader) ssaoShader =
|
---|
| 1118 | new DeferredRenderer(texWidth, texHeight, camera, farDist / MAX_DEPTH_CONST);
|
---|
| 1119 |
|
---|
| 1120 | DeferredRenderer::SHADING_METHOD shadingMethod;
|
---|
| 1121 |
|
---|
| 1122 | if (useAdvancedShading)
|
---|
| 1123 | {
|
---|
| 1124 | if (useGlobIllum)
|
---|
| 1125 | shadingMethod = DeferredRenderer::GI;
|
---|
| 1126 | else
|
---|
| 1127 | shadingMethod = DeferredRenderer::SSAO;
|
---|
| 1128 | }
|
---|
| 1129 | else
|
---|
| 1130 | shadingMethod = DeferredRenderer::DEFAULT;
|
---|
| 1131 |
|
---|
| 1132 |
|
---|
| 1133 | ssaoShader->SetShadingMethod(shadingMethod);
|
---|
| 1134 | ssaoShader->SetSamplingMethod(samplingMethod);
|
---|
| 1135 | ssaoShader->SetUseTemporalCoherence(useTemporalCoherence);
|
---|
| 1136 |
|
---|
| 1137 | ShadowMap *sm = showShadowMap ? shadowMap : NULL;
|
---|
| 1138 | ssaoShader->Render(fbo, oldViewProjMat, viewProjMat, ssaoTempCohFactor, light, useHDR, sm);
|
---|
| 1139 | }
|
---|
| 1140 |
|
---|
| 1141 |
|
---|
| 1142 | state.SetRenderPassType(RenderState::FIXED);
|
---|
| 1143 | state.Reset();
|
---|
| 1144 |
|
---|
| 1145 |
|
---|
| 1146 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 1147 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 1148 |
|
---|
| 1149 | renderMethod = oldRenderMethod;
|
---|
| 1150 |
|
---|
| 1151 |
|
---|
| 1152 | ///////////
|
---|
| 1153 |
|
---|
| 1154 |
|
---|
| 1155 | if (showAlgorithmTime)
|
---|
| 1156 | {
|
---|
| 1157 | glFinish();
|
---|
| 1158 |
|
---|
| 1159 | algTime = algTimer.Elapsedms();
|
---|
| 1160 | perfGraph->AddData(algTime);
|
---|
| 1161 |
|
---|
| 1162 | perfGraph->Draw();
|
---|
| 1163 | }
|
---|
| 1164 | else
|
---|
| 1165 | {
|
---|
| 1166 | if (visMode) DisplayVisualization();
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | glFlush();
|
---|
| 1170 |
|
---|
| 1171 | const bool restart = true;
|
---|
| 1172 | elapsedTime = frameTimer.Elapsedms(restart);
|
---|
| 1173 |
|
---|
| 1174 | DisplayStats();
|
---|
| 1175 |
|
---|
| 1176 | glutSwapBuffers();
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
| 1179 |
|
---|
| 1180 | #pragma warning( disable : 4100 )
|
---|
| 1181 | void KeyBoard(unsigned char c, int x, int y)
|
---|
| 1182 | {
|
---|
| 1183 | switch(c)
|
---|
| 1184 | {
|
---|
| 1185 | case 27:
|
---|
| 1186 | CleanUp();
|
---|
| 1187 | exit(0);
|
---|
| 1188 | break;
|
---|
| 1189 | case 32: // space
|
---|
| 1190 | renderMode = (renderMode + 1) % RenderTraverser::NUM_TRAVERSAL_TYPES;
|
---|
| 1191 |
|
---|
| 1192 | DEL_PTR(traverser);
|
---|
| 1193 | traverser = CreateTraverser(camera);
|
---|
| 1194 |
|
---|
| 1195 | if (shadowTraverser)
|
---|
| 1196 | {
|
---|
| 1197 | // shadow traverser has to be recomputed
|
---|
| 1198 | DEL_PTR(shadowTraverser);
|
---|
| 1199 | shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
| 1202 | break;
|
---|
| 1203 | case '+':
|
---|
| 1204 | if (maxBatchSize < 10)
|
---|
| 1205 | maxBatchSize = 10;
|
---|
| 1206 | else
|
---|
| 1207 | maxBatchSize += 10;
|
---|
| 1208 |
|
---|
| 1209 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
| 1210 | break;
|
---|
| 1211 | case '-':
|
---|
| 1212 | maxBatchSize -= 10;
|
---|
| 1213 | if (maxBatchSize < 0) maxBatchSize = 1;
|
---|
| 1214 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
| 1215 | break;
|
---|
| 1216 | case 'M':
|
---|
| 1217 | case 'm':
|
---|
| 1218 | useMultiQueries = !useMultiQueries;
|
---|
| 1219 | traverser->SetUseMultiQueries(useMultiQueries);
|
---|
| 1220 | break;
|
---|
| 1221 | case '1':
|
---|
| 1222 | descendKeyPressed = true;
|
---|
| 1223 | break;
|
---|
| 1224 | case '2':
|
---|
| 1225 | ascendKeyPressed = true;
|
---|
| 1226 | break;
|
---|
| 1227 | case '3':
|
---|
| 1228 | if (trianglesPerVirtualLeaf >= 100)
|
---|
| 1229 | trianglesPerVirtualLeaf -= 100;
|
---|
| 1230 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 1231 | break;
|
---|
| 1232 | case '4':
|
---|
| 1233 | trianglesPerVirtualLeaf += 100;
|
---|
| 1234 | bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
|
---|
| 1235 | break;
|
---|
| 1236 | case '5':
|
---|
| 1237 | assumedVisibleFrames -= 1;
|
---|
| 1238 | if (assumedVisibleFrames < 1) assumedVisibleFrames = 1;
|
---|
| 1239 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 1240 | break;
|
---|
| 1241 | case '6':
|
---|
| 1242 | assumedVisibleFrames += 1;
|
---|
| 1243 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
| 1244 | break;
|
---|
| 1245 | case '7':
|
---|
| 1246 | ssaoTempCohFactor *= 0.5f;
|
---|
| 1247 | break;
|
---|
| 1248 | case '8':
|
---|
| 1249 | ssaoTempCohFactor *= 2.0f;
|
---|
| 1250 | //if (ssaoTempCohFactor > 1.0f) ssaoExpFactor = 1.0f;
|
---|
| 1251 | break;
|
---|
| 1252 | case '9':
|
---|
| 1253 | useLODs = !useLODs;
|
---|
| 1254 | SceneEntity::SetUseLODs(useLODs);
|
---|
| 1255 | break;
|
---|
| 1256 | case 'P':
|
---|
| 1257 | case 'p':
|
---|
| 1258 | samplingMethod = DeferredRenderer::SAMPLING_METHOD((samplingMethod + 1) % 3);
|
---|
| 1259 | cout << "ssao sampling method: " << samplingMethod << endl;
|
---|
| 1260 | break;
|
---|
| 1261 | case 'Y':
|
---|
| 1262 | case 'y':
|
---|
| 1263 | showShadowMap = !showShadowMap;
|
---|
| 1264 | break;
|
---|
| 1265 | case 'g':
|
---|
| 1266 | case 'G':
|
---|
| 1267 | useGlobIllum = !useGlobIllum;
|
---|
| 1268 | break;
|
---|
| 1269 | case 't':
|
---|
| 1270 | case 'T':
|
---|
| 1271 | useTemporalCoherence = !useTemporalCoherence;
|
---|
| 1272 | break;
|
---|
| 1273 | case 'o':
|
---|
| 1274 | case 'O':
|
---|
| 1275 | useOptimization = !useOptimization;
|
---|
| 1276 | traverser->SetUseOptimization(useOptimization);
|
---|
| 1277 | break;
|
---|
| 1278 | case 'a':
|
---|
| 1279 | case 'A':
|
---|
| 1280 | leftKeyPressed = true;
|
---|
| 1281 | break;
|
---|
| 1282 | case 'd':
|
---|
| 1283 | case 'D':
|
---|
| 1284 | rightKeyPressed = true;
|
---|
| 1285 | break;
|
---|
| 1286 | case 'w':
|
---|
| 1287 | case 'W':
|
---|
| 1288 | upKeyPressed = true;
|
---|
| 1289 | break;
|
---|
| 1290 | case 's':
|
---|
| 1291 | case 'S':
|
---|
| 1292 | downKeyPressed = true;
|
---|
| 1293 | break;
|
---|
| 1294 | case 'r':
|
---|
| 1295 | case 'R':
|
---|
| 1296 | useRenderQueue = !useRenderQueue;
|
---|
| 1297 | traverser->SetUseRenderQueue(useRenderQueue);
|
---|
| 1298 |
|
---|
| 1299 | break;
|
---|
| 1300 | case 'b':
|
---|
| 1301 | case 'B':
|
---|
| 1302 | useTightBounds = !useTightBounds;
|
---|
| 1303 | traverser->SetUseTightBounds(useTightBounds);
|
---|
| 1304 | break;
|
---|
| 1305 | case 'l':
|
---|
| 1306 | case 'L':
|
---|
| 1307 | renderLightView = !renderLightView;
|
---|
| 1308 | break;
|
---|
| 1309 | case 'h':
|
---|
| 1310 | case 'H':
|
---|
| 1311 | useHDR = !useHDR;
|
---|
| 1312 | break;
|
---|
| 1313 | default:
|
---|
| 1314 | return;
|
---|
| 1315 | }
|
---|
| 1316 |
|
---|
| 1317 | glutPostRedisplay();
|
---|
| 1318 | }
|
---|
| 1319 |
|
---|
| 1320 |
|
---|
| 1321 | void SpecialKeyUp(int c, int x, int y)
|
---|
| 1322 | {
|
---|
| 1323 | switch (c)
|
---|
| 1324 | {
|
---|
| 1325 | case GLUT_KEY_LEFT:
|
---|
| 1326 | leftKeyPressed = false;
|
---|
| 1327 | break;
|
---|
| 1328 | case GLUT_KEY_RIGHT:
|
---|
| 1329 | rightKeyPressed = false;
|
---|
| 1330 | break;
|
---|
| 1331 | case GLUT_KEY_UP:
|
---|
| 1332 | upKeyPressed = false;
|
---|
| 1333 | break;
|
---|
| 1334 | case GLUT_KEY_DOWN:
|
---|
| 1335 | downKeyPressed = false;
|
---|
| 1336 | break;
|
---|
| 1337 | case GLUT_ACTIVE_ALT:
|
---|
| 1338 | altKeyPressed = false;
|
---|
| 1339 | break;
|
---|
| 1340 | default:
|
---|
| 1341 | return;
|
---|
| 1342 | }
|
---|
| 1343 | }
|
---|
| 1344 |
|
---|
| 1345 |
|
---|
| 1346 | void KeyUp(unsigned char c, int x, int y)
|
---|
| 1347 | {
|
---|
| 1348 | switch (c)
|
---|
| 1349 | {
|
---|
| 1350 |
|
---|
| 1351 | case 'A':
|
---|
| 1352 | case 'a':
|
---|
| 1353 | leftKeyPressed = false;
|
---|
| 1354 | break;
|
---|
| 1355 | case 'D':
|
---|
| 1356 | case 'd':
|
---|
| 1357 | rightKeyPressed = false;
|
---|
| 1358 | break;
|
---|
| 1359 | case 'W':
|
---|
| 1360 | case 'w':
|
---|
| 1361 | upKeyPressed = false;
|
---|
| 1362 | break;
|
---|
| 1363 | case 'S':
|
---|
| 1364 | case 's':
|
---|
| 1365 | downKeyPressed = false;
|
---|
| 1366 | break;
|
---|
| 1367 | case '1':
|
---|
| 1368 | descendKeyPressed = false;
|
---|
| 1369 | break;
|
---|
| 1370 | case '2':
|
---|
| 1371 | ascendKeyPressed = false;
|
---|
| 1372 | break;
|
---|
| 1373 |
|
---|
| 1374 | default:
|
---|
| 1375 | return;
|
---|
| 1376 | }
|
---|
| 1377 | //glutPostRedisplay();
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
| 1380 |
|
---|
| 1381 | void Special(int c, int x, int y)
|
---|
| 1382 | {
|
---|
| 1383 | switch(c)
|
---|
| 1384 | {
|
---|
| 1385 | case GLUT_KEY_F1:
|
---|
| 1386 | showHelp = !showHelp;
|
---|
| 1387 | break;
|
---|
| 1388 | case GLUT_KEY_F2:
|
---|
| 1389 | visMode = !visMode;
|
---|
| 1390 | break;
|
---|
| 1391 | case GLUT_KEY_F3:
|
---|
| 1392 | showBoundingVolumes = !showBoundingVolumes;
|
---|
| 1393 | traverser->SetShowBounds(showBoundingVolumes);
|
---|
| 1394 | break;
|
---|
| 1395 | case GLUT_KEY_F4:
|
---|
| 1396 | showOptions = !showOptions;
|
---|
| 1397 | break;
|
---|
| 1398 | case GLUT_KEY_F5:
|
---|
| 1399 | showStatistics = !showStatistics;
|
---|
| 1400 | break;
|
---|
| 1401 | case GLUT_KEY_F6:
|
---|
| 1402 | flyMode = !flyMode;
|
---|
| 1403 | break;
|
---|
| 1404 | case GLUT_KEY_F7:
|
---|
| 1405 |
|
---|
| 1406 | renderMethod = (renderMethod + 1) % 4;
|
---|
| 1407 |
|
---|
| 1408 | traverser->SetUseDepthPass(
|
---|
| 1409 | (renderMethod == RENDER_DEPTH_PASS) ||
|
---|
| 1410 | (renderMethod == RENDER_DEPTH_PASS_DEFERRED)
|
---|
| 1411 | );
|
---|
| 1412 |
|
---|
| 1413 | break;
|
---|
| 1414 | case GLUT_KEY_F8:
|
---|
| 1415 | useAdvancedShading = !useAdvancedShading;
|
---|
| 1416 |
|
---|
| 1417 | break;
|
---|
| 1418 | case GLUT_KEY_F9:
|
---|
| 1419 | showAlgorithmTime = !showAlgorithmTime;
|
---|
| 1420 | break;
|
---|
| 1421 | case GLUT_KEY_F10:
|
---|
| 1422 | moveLight = !moveLight;
|
---|
| 1423 | break;
|
---|
| 1424 | case GLUT_KEY_LEFT:
|
---|
| 1425 | {
|
---|
| 1426 | leftKeyPressed = true;
|
---|
| 1427 | camera->Pitch(KeyRotationAngle());
|
---|
| 1428 | }
|
---|
| 1429 | break;
|
---|
| 1430 | case GLUT_KEY_RIGHT:
|
---|
| 1431 | {
|
---|
| 1432 | rightKeyPressed = true;
|
---|
| 1433 | camera->Pitch(-KeyRotationAngle());
|
---|
| 1434 | }
|
---|
| 1435 | break;
|
---|
| 1436 | case GLUT_KEY_UP:
|
---|
| 1437 | {
|
---|
| 1438 | upKeyPressed = true;
|
---|
| 1439 | KeyHorizontalMotion(KeyShift());
|
---|
| 1440 | }
|
---|
| 1441 | break;
|
---|
| 1442 | case GLUT_KEY_DOWN:
|
---|
| 1443 | {
|
---|
| 1444 | downKeyPressed = true;
|
---|
| 1445 | KeyHorizontalMotion(-KeyShift());
|
---|
| 1446 | }
|
---|
| 1447 | break;
|
---|
| 1448 | default:
|
---|
| 1449 | return;
|
---|
| 1450 |
|
---|
| 1451 | }
|
---|
| 1452 |
|
---|
| 1453 | glutPostRedisplay();
|
---|
| 1454 | }
|
---|
| 1455 |
|
---|
| 1456 | #pragma warning( default : 4100 )
|
---|
| 1457 |
|
---|
| 1458 |
|
---|
| 1459 | void Reshape(int w, int h)
|
---|
| 1460 | {
|
---|
| 1461 | winAspectRatio = 1.0f;
|
---|
| 1462 |
|
---|
| 1463 | glViewport(0, 0, w, h);
|
---|
| 1464 |
|
---|
| 1465 | winWidth = w;
|
---|
| 1466 | winHeight = h;
|
---|
| 1467 |
|
---|
| 1468 | if (w) winAspectRatio = (float) w / (float) h;
|
---|
| 1469 |
|
---|
| 1470 | glMatrixMode(GL_PROJECTION);
|
---|
| 1471 | glLoadIdentity();
|
---|
| 1472 |
|
---|
| 1473 | gluPerspective(fov, winAspectRatio, nearDist, farDist);
|
---|
| 1474 |
|
---|
| 1475 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1476 |
|
---|
| 1477 | glutPostRedisplay();
|
---|
| 1478 | }
|
---|
| 1479 |
|
---|
| 1480 |
|
---|
| 1481 | void Mouse(int button, int state, int x, int y)
|
---|
| 1482 | {
|
---|
| 1483 | if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
|
---|
| 1484 | {
|
---|
| 1485 | xEyeBegin = x;
|
---|
| 1486 | yMotionBegin = y;
|
---|
| 1487 |
|
---|
| 1488 | glutMotionFunc(LeftMotion);
|
---|
| 1489 | }
|
---|
| 1490 | else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
|
---|
| 1491 | {
|
---|
| 1492 | xEyeBegin = x;
|
---|
| 1493 | yEyeBegin = y;
|
---|
| 1494 | yMotionBegin = y;
|
---|
| 1495 |
|
---|
| 1496 | if (!moveLight)
|
---|
| 1497 | glutMotionFunc(RightMotion);
|
---|
| 1498 | else
|
---|
| 1499 | glutMotionFunc(RightMotionLight);
|
---|
| 1500 | }
|
---|
| 1501 | else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN))
|
---|
| 1502 | {
|
---|
| 1503 | horizontalMotionBegin = x;
|
---|
| 1504 | verticalMotionBegin = y;
|
---|
| 1505 | glutMotionFunc(MiddleMotion);
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 | glutPostRedisplay();
|
---|
| 1509 | }
|
---|
| 1510 |
|
---|
| 1511 |
|
---|
| 1512 | /** rotation for left/right mouse drag
|
---|
| 1513 | motion for up/down mouse drag
|
---|
| 1514 | */
|
---|
| 1515 | void LeftMotion(int x, int y)
|
---|
| 1516 | {
|
---|
| 1517 | Vector3 viewDir = camera->GetDirection();
|
---|
| 1518 | Vector3 pos = camera->GetPosition();
|
---|
| 1519 |
|
---|
| 1520 | // don't move in the vertical direction
|
---|
| 1521 | Vector3 horView(viewDir[0], viewDir[1], 0);
|
---|
| 1522 |
|
---|
| 1523 | float eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
| 1524 |
|
---|
| 1525 | camera->Pitch(eyeXAngle);
|
---|
| 1526 |
|
---|
| 1527 | pos += horView * (yMotionBegin - y) * 0.2f;
|
---|
| 1528 |
|
---|
| 1529 | camera->SetPosition(pos);
|
---|
| 1530 |
|
---|
| 1531 | xEyeBegin = x;
|
---|
| 1532 | yMotionBegin = y;
|
---|
| 1533 |
|
---|
| 1534 | glutPostRedisplay();
|
---|
| 1535 | }
|
---|
| 1536 |
|
---|
| 1537 |
|
---|
| 1538 | void RightMotionLight(int x, int y)
|
---|
| 1539 | {
|
---|
| 1540 | float theta = 0.2f * M_PI * (xEyeBegin - x) / 180.0f;
|
---|
| 1541 | float phi = 0.2f * M_PI * (yMotionBegin - y) / 180.0f;
|
---|
| 1542 |
|
---|
| 1543 | Vector3 lightDir = light->GetDirection();
|
---|
| 1544 |
|
---|
| 1545 | Matrix4x4 roty = RotationYMatrix(theta);
|
---|
| 1546 | Matrix4x4 rotx = RotationXMatrix(phi);
|
---|
| 1547 |
|
---|
| 1548 | lightDir = roty * lightDir;
|
---|
| 1549 | lightDir = rotx * lightDir;
|
---|
| 1550 |
|
---|
| 1551 | // normalize to avoid accumulating errors
|
---|
| 1552 | lightDir.Normalize();
|
---|
| 1553 |
|
---|
| 1554 | light->SetDirection(lightDir);
|
---|
| 1555 |
|
---|
| 1556 | xEyeBegin = x;
|
---|
| 1557 | yMotionBegin = y;
|
---|
| 1558 |
|
---|
| 1559 | glutPostRedisplay();
|
---|
| 1560 | }
|
---|
| 1561 |
|
---|
| 1562 |
|
---|
| 1563 | /** rotation for left / right mouse drag
|
---|
| 1564 | motion for up / down mouse drag
|
---|
| 1565 | */
|
---|
| 1566 | void RightMotion(int x, int y)
|
---|
| 1567 | {
|
---|
| 1568 | float eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
| 1569 | float eyeYAngle = -0.2f * M_PI * (yEyeBegin - y) / 180.0;
|
---|
| 1570 |
|
---|
| 1571 | camera->Yaw(eyeYAngle);
|
---|
| 1572 | camera->Pitch(eyeXAngle);
|
---|
| 1573 |
|
---|
| 1574 | xEyeBegin = x;
|
---|
| 1575 | yEyeBegin = y;
|
---|
| 1576 |
|
---|
| 1577 | glutPostRedisplay();
|
---|
| 1578 | }
|
---|
| 1579 |
|
---|
| 1580 |
|
---|
| 1581 | // strafe
|
---|
| 1582 | void MiddleMotion(int x, int y)
|
---|
| 1583 | {
|
---|
| 1584 | Vector3 viewDir = camera->GetDirection();
|
---|
| 1585 | Vector3 pos = camera->GetPosition();
|
---|
| 1586 |
|
---|
| 1587 | // the 90 degree rotated view vector
|
---|
| 1588 | // y zero so we don't move in the vertical
|
---|
| 1589 | Vector3 rVec(viewDir[0], viewDir[1], 0);
|
---|
| 1590 |
|
---|
| 1591 | Matrix4x4 rot = RotationZMatrix(M_PI * 0.5f);
|
---|
| 1592 | rVec = rot * rVec;
|
---|
| 1593 |
|
---|
| 1594 | pos -= rVec * (x - horizontalMotionBegin) * 0.1f;
|
---|
| 1595 | pos[2] += (verticalMotionBegin - y) * 0.1f;
|
---|
| 1596 |
|
---|
| 1597 | camera->SetPosition(pos);
|
---|
| 1598 |
|
---|
| 1599 | horizontalMotionBegin = x;
|
---|
| 1600 | verticalMotionBegin = y;
|
---|
| 1601 |
|
---|
| 1602 | glutPostRedisplay();
|
---|
| 1603 | }
|
---|
| 1604 |
|
---|
| 1605 |
|
---|
| 1606 | void InitExtensions(void)
|
---|
| 1607 | {
|
---|
| 1608 | GLenum err = glewInit();
|
---|
| 1609 |
|
---|
| 1610 | if (GLEW_OK != err)
|
---|
| 1611 | {
|
---|
| 1612 | // problem: glewInit failed, something is seriously wrong
|
---|
| 1613 | fprintf(stderr,"Error: %s\n", glewGetErrorString(err));
|
---|
| 1614 | exit(1);
|
---|
| 1615 | }
|
---|
| 1616 | if (!GLEW_ARB_occlusion_query)
|
---|
| 1617 | {
|
---|
| 1618 | printf("I require the GL_ARB_occlusion_query to work.\n");
|
---|
| 1619 | exit(1);
|
---|
| 1620 | }
|
---|
| 1621 | }
|
---|
| 1622 |
|
---|
| 1623 |
|
---|
| 1624 | void Begin2D()
|
---|
| 1625 | {
|
---|
| 1626 | glDisable(GL_LIGHTING);
|
---|
| 1627 | glDisable(GL_DEPTH_TEST);
|
---|
| 1628 |
|
---|
| 1629 | glMatrixMode(GL_PROJECTION);
|
---|
| 1630 | glPushMatrix();
|
---|
| 1631 | glLoadIdentity();
|
---|
| 1632 |
|
---|
| 1633 | gluOrtho2D(0, winWidth, 0, winHeight);
|
---|
| 1634 |
|
---|
| 1635 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1636 | glPushMatrix();
|
---|
| 1637 | glLoadIdentity();
|
---|
| 1638 | }
|
---|
| 1639 |
|
---|
| 1640 |
|
---|
| 1641 | void End2D()
|
---|
| 1642 | {
|
---|
| 1643 | glMatrixMode(GL_PROJECTION);
|
---|
| 1644 | glPopMatrix();
|
---|
| 1645 |
|
---|
| 1646 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1647 | glPopMatrix();
|
---|
| 1648 |
|
---|
| 1649 | glEnable(GL_LIGHTING);
|
---|
| 1650 | glEnable(GL_DEPTH_TEST);
|
---|
| 1651 | }
|
---|
| 1652 |
|
---|
| 1653 |
|
---|
| 1654 | // displays the visualisation of culling algorithm
|
---|
| 1655 | void DisplayVisualization()
|
---|
| 1656 | {
|
---|
| 1657 | visualization->SetFrameId(traverser->GetCurrentFrameId());
|
---|
| 1658 |
|
---|
| 1659 | Begin2D();
|
---|
| 1660 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 1661 | glEnable(GL_BLEND);
|
---|
| 1662 | glColor4f(0.0f ,0.0f, 0.0f, 0.5f);
|
---|
| 1663 |
|
---|
| 1664 | glRecti(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth, winHeight);
|
---|
| 1665 | glDisable(GL_BLEND);
|
---|
| 1666 | End2D();
|
---|
| 1667 |
|
---|
| 1668 |
|
---|
| 1669 | AxisAlignedBox3 box = bvh->GetBox();
|
---|
| 1670 |
|
---|
| 1671 | const float offs = box.Size().x * 0.3f;
|
---|
| 1672 |
|
---|
| 1673 | Vector3 vizpos = Vector3(box.Min().x, box.Min().y - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50);
|
---|
| 1674 |
|
---|
| 1675 | visCamera->SetPosition(vizpos);
|
---|
| 1676 | visCamera->ResetPitchAndYaw();
|
---|
| 1677 |
|
---|
| 1678 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1679 | glViewport(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth / 3, winHeight / 3);
|
---|
| 1680 |
|
---|
| 1681 | glMatrixMode(GL_PROJECTION);
|
---|
| 1682 | glPushMatrix();
|
---|
| 1683 |
|
---|
| 1684 | glLoadIdentity();
|
---|
| 1685 |
|
---|
| 1686 | glOrtho(-offs, offs, -offs, offs, 0.0f, box.Size().z * 100.0f);
|
---|
| 1687 |
|
---|
| 1688 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1689 | glPushMatrix();
|
---|
| 1690 |
|
---|
| 1691 | visCamera->SetupCameraView();
|
---|
| 1692 |
|
---|
| 1693 | Matrix4x4 rotZ = RotationZMatrix(-camera->GetPitch());
|
---|
| 1694 | glMultMatrixf((float *)rotZ.x);
|
---|
| 1695 |
|
---|
| 1696 | // inverse translation in order to fix current position
|
---|
| 1697 | Vector3 pos = camera->GetPosition();
|
---|
| 1698 | glTranslatef(-pos.x, -pos.y, -pos.z);
|
---|
| 1699 |
|
---|
| 1700 |
|
---|
| 1701 | GLfloat position[] = {0.8f, 1.0f, 1.5f, 0.0f};
|
---|
| 1702 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
| 1703 |
|
---|
| 1704 | GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Max().y, bvh->GetBox().Center().z, 1.0f};
|
---|
| 1705 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
| 1706 |
|
---|
| 1707 | glClear(GL_DEPTH_BUFFER_BIT);
|
---|
| 1708 |
|
---|
| 1709 |
|
---|
| 1710 | ////////////
|
---|
| 1711 | //-- visualization of the occlusion culling
|
---|
| 1712 |
|
---|
| 1713 | visualization->Render();
|
---|
| 1714 |
|
---|
| 1715 |
|
---|
| 1716 | // reset previous settings
|
---|
| 1717 | glPopAttrib();
|
---|
| 1718 |
|
---|
| 1719 | glMatrixMode(GL_PROJECTION);
|
---|
| 1720 | glPopMatrix();
|
---|
| 1721 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1722 | glPopMatrix();
|
---|
| 1723 | }
|
---|
| 1724 |
|
---|
| 1725 |
|
---|
| 1726 | // cleanup routine after the main loop
|
---|
| 1727 | void CleanUp()
|
---|
| 1728 | {
|
---|
| 1729 | DEL_PTR(traverser);
|
---|
| 1730 | DEL_PTR(sceneQuery);
|
---|
| 1731 | DEL_PTR(bvh);
|
---|
| 1732 | DEL_PTR(visualization);
|
---|
| 1733 | DEL_PTR(camera);
|
---|
| 1734 | DEL_PTR(renderQueue);
|
---|
| 1735 | DEL_PTR(perfGraph);
|
---|
| 1736 | DEL_PTR(fbo);
|
---|
| 1737 | DEL_PTR(ssaoShader);
|
---|
[3037] | 1738 | DEL_PTR(light);
|
---|
| 1739 | DEL_PTR(visCamera);
|
---|
| 1740 | DEL_PTR(preetham);
|
---|
| 1741 | DEL_PTR(shadowMap);
|
---|
| 1742 | DEL_PTR(shadowTraverser);
|
---|
| 1743 | DEL_PTR(RenderState::sCgMrtFragmentProgram);
|
---|
| 1744 | DEL_PTR(RenderState::sCgMrtFragmentTexProgram);
|
---|
| 1745 | DEL_PTR(RenderState::sCgMrtVertexProgram);
|
---|
[3020] | 1746 |
|
---|
[3037] | 1747 | ResourceManager::DelSingleton();
|
---|
| 1748 | loader = NULL;
|
---|
| 1749 |
|
---|
| 1750 | DeferredRenderer::ReleaseCG();
|
---|
| 1751 | SkyPreetham::ReleaseCG();
|
---|
| 1752 |
|
---|
[3020] | 1753 | if (sCgContext)
|
---|
| 1754 | cgDestroyContext(sCgContext);
|
---|
| 1755 | }
|
---|
| 1756 |
|
---|
| 1757 |
|
---|
| 1758 | // this function inserts a dezimal point after each 1000
|
---|
| 1759 | void CalcDecimalPoint(string &str, int d, int len)
|
---|
| 1760 | {
|
---|
| 1761 | static vector<int> numbers;
|
---|
| 1762 | numbers.clear();
|
---|
| 1763 |
|
---|
| 1764 | static string shortStr;
|
---|
| 1765 | shortStr.clear();
|
---|
| 1766 |
|
---|
| 1767 | static char hstr[100];
|
---|
| 1768 |
|
---|
| 1769 | while (d != 0)
|
---|
| 1770 | {
|
---|
| 1771 | numbers.push_back(d % 1000);
|
---|
| 1772 | d /= 1000;
|
---|
| 1773 | }
|
---|
| 1774 |
|
---|
| 1775 | // first element without leading zeros
|
---|
| 1776 | if (numbers.size() > 0)
|
---|
| 1777 | {
|
---|
| 1778 | sprintf(hstr, "%d", numbers.back());
|
---|
| 1779 | shortStr.append(hstr);
|
---|
| 1780 | }
|
---|
| 1781 |
|
---|
| 1782 | for (int i = (int)numbers.size() - 2; i >= 0; i--)
|
---|
| 1783 | {
|
---|
| 1784 | sprintf(hstr, ",%03d", numbers[i]);
|
---|
| 1785 | shortStr.append(hstr);
|
---|
| 1786 | }
|
---|
| 1787 |
|
---|
| 1788 | int dif = len - (int)shortStr.size();
|
---|
| 1789 |
|
---|
| 1790 | for (int i = 0; i < dif; ++ i)
|
---|
| 1791 | {
|
---|
| 1792 | str += " ";
|
---|
| 1793 | }
|
---|
| 1794 |
|
---|
| 1795 | str.append(shortStr);
|
---|
| 1796 | }
|
---|
| 1797 |
|
---|
| 1798 |
|
---|
| 1799 | void DisplayStats()
|
---|
| 1800 | {
|
---|
| 1801 | static char msg[9][300];
|
---|
| 1802 |
|
---|
| 1803 | static double frameTime = elapsedTime;
|
---|
| 1804 | static double renderTime = algTime;
|
---|
| 1805 |
|
---|
| 1806 | const float expFactor = 0.1f;
|
---|
| 1807 |
|
---|
| 1808 | // if some strange render time spike happened in this frame => don't count
|
---|
| 1809 | if (elapsedTime < 500) frameTime = elapsedTime * expFactor + (1.0f - expFactor) * elapsedTime;
|
---|
| 1810 |
|
---|
| 1811 | static float rTime = 1000.0f;
|
---|
| 1812 |
|
---|
| 1813 | if (showAlgorithmTime)
|
---|
| 1814 | {
|
---|
| 1815 | if (algTime < 500) renderTime = algTime * expFactor + (1.0f - expFactor) * renderTime;
|
---|
| 1816 | }
|
---|
| 1817 |
|
---|
| 1818 | accumulatedTime += elapsedTime;
|
---|
| 1819 |
|
---|
| 1820 | if (accumulatedTime > 500) // update every fraction of a second
|
---|
| 1821 | {
|
---|
| 1822 | accumulatedTime = 0;
|
---|
| 1823 |
|
---|
| 1824 | if (frameTime) fps = 1e3f / (float)frameTime;
|
---|
| 1825 |
|
---|
| 1826 | rTime = renderTime;
|
---|
| 1827 |
|
---|
| 1828 | if (renderLightView && shadowTraverser)
|
---|
| 1829 | {
|
---|
| 1830 | renderedTriangles = shadowTraverser->GetStats().mNumRenderedTriangles;
|
---|
| 1831 | renderedObjects = shadowTraverser->GetStats().mNumRenderedGeometry;
|
---|
| 1832 | renderedNodes = shadowTraverser->GetStats().mNumRenderedNodes;
|
---|
| 1833 | }
|
---|
| 1834 | else if (showShadowMap && shadowTraverser)
|
---|
| 1835 | {
|
---|
| 1836 | renderedNodes = traverser->GetStats().mNumRenderedNodes + shadowTraverser->GetStats().mNumRenderedNodes;
|
---|
| 1837 | renderedObjects = traverser->GetStats().mNumRenderedGeometry + shadowTraverser->GetStats().mNumRenderedGeometry;
|
---|
| 1838 | renderedTriangles = traverser->GetStats().mNumRenderedTriangles + shadowTraverser->GetStats().mNumRenderedTriangles;
|
---|
| 1839 | }
|
---|
| 1840 | else
|
---|
| 1841 | {
|
---|
| 1842 | renderedTriangles = traverser->GetStats().mNumRenderedTriangles;
|
---|
| 1843 | renderedObjects = traverser->GetStats().mNumRenderedGeometry;
|
---|
| 1844 | renderedNodes = traverser->GetStats().mNumRenderedNodes;
|
---|
| 1845 | }
|
---|
| 1846 |
|
---|
| 1847 | traversedNodes = traverser->GetStats().mNumTraversedNodes;
|
---|
| 1848 | frustumCulledNodes = traverser->GetStats().mNumFrustumCulledNodes;
|
---|
| 1849 | queryCulledNodes = traverser->GetStats().mNumQueryCulledNodes;
|
---|
| 1850 | issuedQueries = traverser->GetStats().mNumIssuedQueries;
|
---|
| 1851 | stateChanges = traverser->GetStats().mNumStateChanges;
|
---|
| 1852 | numBatches = traverser->GetStats().mNumBatches;
|
---|
| 1853 | }
|
---|
| 1854 |
|
---|
| 1855 |
|
---|
| 1856 | Begin2D();
|
---|
| 1857 |
|
---|
| 1858 | glEnable(GL_BLEND);
|
---|
| 1859 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 1860 |
|
---|
| 1861 | if (showHelp)
|
---|
| 1862 | {
|
---|
| 1863 | DrawHelpMessage();
|
---|
| 1864 | }
|
---|
| 1865 | else
|
---|
| 1866 | {
|
---|
| 1867 | if (showOptions)
|
---|
| 1868 | {
|
---|
| 1869 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
| 1870 | glRecti(5, winHeight - 95, winWidth * 2 / 3 - 5, winHeight - 5);
|
---|
| 1871 | }
|
---|
| 1872 |
|
---|
| 1873 | if (showStatistics)
|
---|
| 1874 | {
|
---|
| 1875 | glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
|
---|
| 1876 | glRecti(5, winHeight - 165, winWidth * 2 / 3 - 5, winHeight - 100);
|
---|
| 1877 | }
|
---|
| 1878 |
|
---|
| 1879 | glEnable(GL_TEXTURE_2D);
|
---|
| 1880 |
|
---|
| 1881 | myfont.Begin();
|
---|
| 1882 |
|
---|
| 1883 | if (showOptions)
|
---|
| 1884 | {
|
---|
| 1885 | glColor3f(0.0f, 1.0f, 0.0f);
|
---|
| 1886 |
|
---|
| 1887 | int i = 0;
|
---|
| 1888 |
|
---|
| 1889 | static char *renderMethodStr[] = {"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"};
|
---|
| 1890 |
|
---|
| 1891 | sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d",
|
---|
| 1892 | useMultiQueries, useTightBounds, useRenderQueue);
|
---|
| 1893 |
|
---|
| 1894 | sprintf(msg[i ++], "render technique: %s, SSAO: %d", renderMethodStr[renderMethod], useAdvancedShading);
|
---|
| 1895 |
|
---|
| 1896 | sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf);
|
---|
| 1897 |
|
---|
| 1898 | sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d",
|
---|
| 1899 | assumedVisibleFrames, maxBatchSize);
|
---|
| 1900 |
|
---|
| 1901 | for (int j = 0; j < 4; ++ j)
|
---|
| 1902 | myfont.DrawString(msg[j], 10.0f, winHeight - 5 - j * 20);
|
---|
| 1903 | }
|
---|
| 1904 |
|
---|
| 1905 | if (showStatistics)
|
---|
| 1906 | {
|
---|
| 1907 | glColor3f(1.0f, 1.0f, 0.0f);
|
---|
| 1908 |
|
---|
| 1909 | string objStr, totalObjStr;
|
---|
| 1910 | string triStr, totalTriStr;
|
---|
| 1911 |
|
---|
| 1912 | int len = 10;
|
---|
| 1913 | CalcDecimalPoint(objStr, renderedObjects, len);
|
---|
| 1914 | CalcDecimalPoint(totalObjStr, (int)loader->GetNumEntities(), len);
|
---|
| 1915 |
|
---|
| 1916 | CalcDecimalPoint(triStr, renderedTriangles, len);
|
---|
| 1917 | CalcDecimalPoint(totalTriStr, bvh->GetBvhStats().mTriangles, len);
|
---|
| 1918 |
|
---|
| 1919 | int i = 4;
|
---|
| 1920 |
|
---|
| 1921 | if (0)
|
---|
| 1922 | {
|
---|
| 1923 | sprintf(msg[i ++], "rendered: %s of %s objects, %s of %s triangles",
|
---|
| 1924 | objStr.c_str(), totalObjStr.c_str(), triStr.c_str(), totalTriStr.c_str());
|
---|
| 1925 | }
|
---|
| 1926 | else
|
---|
| 1927 | {
|
---|
| 1928 | sprintf(msg[i ++], "rendered: %6d of %6d nodes, %s of %s triangles",
|
---|
| 1929 | renderedNodes, bvh->GetNumVirtualNodes(), triStr.c_str(), totalTriStr.c_str());
|
---|
| 1930 | }
|
---|
| 1931 |
|
---|
| 1932 | sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d",
|
---|
| 1933 | traversedNodes, frustumCulledNodes, queryCulledNodes);
|
---|
| 1934 |
|
---|
| 1935 | sprintf(msg[i ++], "issued queries: %5d, state changes: %5d, render batches: %5d",
|
---|
| 1936 | issuedQueries, stateChanges, numBatches);
|
---|
| 1937 |
|
---|
| 1938 | for (int j = 4; j < 7; ++ j)
|
---|
| 1939 | myfont.DrawString(msg[j], 10.0f, winHeight - (j + 1) * 20);
|
---|
| 1940 | }
|
---|
| 1941 |
|
---|
| 1942 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 1943 | static char *alg_str[] = {"Frustum Cull", "Stop and Wait", "CHC", "CHC ++"};
|
---|
| 1944 |
|
---|
| 1945 | if (!showAlgorithmTime)
|
---|
| 1946 | sprintf(msg[7], "%s: %6.1f fps", alg_str[renderMode], fps);
|
---|
| 1947 | else
|
---|
| 1948 | sprintf(msg[7], "%s: %6.1f ms", alg_str[renderMode], rTime);
|
---|
| 1949 |
|
---|
| 1950 | myfont.DrawString(msg[7], 1.3f, 690.0f, 760.0f);//, top_color, bottom_color);
|
---|
| 1951 |
|
---|
| 1952 | //sprintf(msg[8], "algorithm time: %6.1f ms", rTime);
|
---|
| 1953 | //myfont.DrawString(msg[8], 720.0f, 730.0f);
|
---|
| 1954 | }
|
---|
| 1955 |
|
---|
| 1956 | glDisable(GL_BLEND);
|
---|
| 1957 | glDisable(GL_TEXTURE_2D);
|
---|
| 1958 |
|
---|
| 1959 | End2D();
|
---|
| 1960 | }
|
---|
| 1961 |
|
---|
| 1962 |
|
---|
| 1963 | void RenderSky()
|
---|
| 1964 | {
|
---|
| 1965 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
| 1966 |
|
---|
| 1967 | if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
|
---|
| 1968 | state.SetRenderPassType(RenderState::DEFERRED);
|
---|
| 1969 |
|
---|
[3037] | 1970 | const bool useToneMapping =
|
---|
| 1971 | ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) ||
|
---|
| 1972 | (renderMethod == RENDER_DEFERRED)) && useHDR;
|
---|
[3020] | 1973 |
|
---|
| 1974 | preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping);
|
---|
| 1975 |
|
---|
| 1976 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 1977 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 1978 | }
|
---|
| 1979 |
|
---|
| 1980 |
|
---|
| 1981 | // render visible object from depth pass
|
---|
| 1982 | void RenderVisibleObjects()
|
---|
| 1983 | {
|
---|
| 1984 | if (renderMethod == RENDER_DEPTH_PASS_DEFERRED)
|
---|
| 1985 | {
|
---|
| 1986 | if (showShadowMap && !renderLightView)
|
---|
| 1987 | {
|
---|
| 1988 | float minVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance());
|
---|
| 1989 | RenderShadowMap(minVisibleDist);
|
---|
| 1990 | }
|
---|
| 1991 |
|
---|
| 1992 | glDisable(GL_LIGHTING);
|
---|
| 1993 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 1994 |
|
---|
| 1995 | InitDeferredRendering();
|
---|
| 1996 | }
|
---|
| 1997 | else
|
---|
| 1998 | {
|
---|
| 1999 | glEnable(GL_LIGHTING);
|
---|
| 2000 | state.SetRenderPassType(RenderState::FIXED);
|
---|
| 2001 | }
|
---|
| 2002 |
|
---|
| 2003 | glShadeModel(GL_SMOOTH);
|
---|
| 2004 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 2005 |
|
---|
| 2006 | // draw all objects that have exactly the same depth as the current sample
|
---|
| 2007 | glDepthFunc(GL_LEQUAL);
|
---|
| 2008 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 2009 |
|
---|
| 2010 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
| 2011 |
|
---|
| 2012 | state.SetUseAlphaToCoverage(true);
|
---|
| 2013 | state.Reset();
|
---|
| 2014 |
|
---|
| 2015 | //cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl;
|
---|
| 2016 |
|
---|
| 2017 | SceneEntityContainer::const_iterator sit,
|
---|
| 2018 | sit_end = traverser->GetVisibleObjects().end();
|
---|
| 2019 |
|
---|
| 2020 | for (sit = traverser->GetVisibleObjects().begin(); sit != sit_end; ++ sit)
|
---|
| 2021 | {
|
---|
| 2022 | renderQueue->Enqueue(*sit);
|
---|
| 2023 | }
|
---|
| 2024 |
|
---|
| 2025 | renderQueue->Apply();
|
---|
| 2026 |
|
---|
| 2027 | glDepthFunc(GL_LESS);
|
---|
| 2028 | state.Reset();
|
---|
| 2029 |
|
---|
| 2030 | PrintGLerror("visibleobjects");
|
---|
| 2031 | }
|
---|
| 2032 |
|
---|
| 2033 |
|
---|
| 2034 | void PlaceViewer(const Vector3 &oldPos)
|
---|
| 2035 | {
|
---|
[3037] | 2036 | if (!sceneQuery)
|
---|
| 2037 | sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
|
---|
| 2038 |
|
---|
[3020] | 2039 | Vector3 playerPos = camera->GetPosition();
|
---|
| 2040 | bool validIntersect = sceneQuery->CalcIntersection(playerPos);
|
---|
| 2041 |
|
---|
| 2042 | if (validIntersect)
|
---|
| 2043 | // && ((playerPos.z - oldPos.z) < bvh->GetBox().Size(2) * 1e-1f))
|
---|
| 2044 | {
|
---|
| 2045 | camera->SetPosition(playerPos);
|
---|
| 2046 | }
|
---|
| 2047 | }
|
---|
| 2048 |
|
---|
| 2049 |
|
---|
| 2050 | void RenderShadowMap(float newfar)
|
---|
| 2051 | {
|
---|
| 2052 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 2053 | cgGLDisableProfile(RenderState::sCgVertexProfile);
|
---|
| 2054 |
|
---|
| 2055 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 2056 |
|
---|
| 2057 | state.SetRenderPassType(RenderState::DEPTH_PASS);
|
---|
| 2058 | state.LockCullFaceEnabled(true);
|
---|
| 2059 | state.SetUseAlphaToCoverage(false);
|
---|
| 2060 |
|
---|
| 2061 | // change CHC++ set of state variables
|
---|
| 2062 | // this must be done for each change of camera because
|
---|
| 2063 | // otherwise the temporal coherency is broken
|
---|
| 2064 | BvhNode::SetCurrentState(1);
|
---|
| 2065 |
|
---|
| 2066 | // hack: temporarily change camera far plane
|
---|
| 2067 | camera->SetFar(newfar);
|
---|
| 2068 | glDisable(GL_CULL_FACE);
|
---|
| 2069 |
|
---|
| 2070 | // the scene is rendered withouth any shading
|
---|
| 2071 | shadowMap->ComputeShadowMap(shadowTraverser, viewProjMat);
|
---|
| 2072 |
|
---|
| 2073 | glEnable(GL_CULL_FACE);
|
---|
| 2074 | camera->SetFar(farDist);
|
---|
| 2075 |
|
---|
| 2076 | state.SetUseAlphaToCoverage(true);
|
---|
| 2077 | state.LockCullFaceEnabled(false);
|
---|
| 2078 |
|
---|
| 2079 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 2080 |
|
---|
| 2081 | // change back state
|
---|
| 2082 | BvhNode::SetCurrentState(0);
|
---|
| 2083 | }
|
---|