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