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