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