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