source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp @ 3273

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