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

Revision 3285, 68.6 KB checked in by mattausch, 15 years ago (diff)

working on viz for submission

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