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