Changeset 3207 for GTP/trunk/App


Ignore:
Timestamp:
12/03/08 01:14:40 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln

    r3107 r3207  
    1212EndProject 
    1313Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VboFormatConverter", "VboFormatConverter.vcproj", "{93A522E1-76F0-4D46-9C97-30DC2DDB531B}" 
     14EndProject 
     15Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IrradianceMapping", "IrradianceMapping.vcproj", "{91680C49-A358-48AE-A02C-66CB884B7D7F}" 
    1416EndProject 
    1517Global 
     
    3537                {93A522E1-76F0-4D46-9C97-30DC2DDB531B}.Release|Win32.ActiveCfg = Release|Win32 
    3638                {93A522E1-76F0-4D46-9C97-30DC2DDB531B}.Release|Win32.Build.0 = Release|Win32 
     39                {91680C49-A358-48AE-A02C-66CB884B7D7F}.Debug|Win32.ActiveCfg = Debug|Win32 
     40                {91680C49-A358-48AE-A02C-66CB884B7D7F}.Debug|Win32.Build.0 = Debug|Win32 
     41                {91680C49-A358-48AE-A02C-66CB884B7D7F}.Release|Win32.ActiveCfg = Release|Win32 
     42                {91680C49-A358-48AE-A02C-66CB884B7D7F}.Release|Win32.Build.0 = Release|Win32 
    3743        EndGlobalSection 
    3844        GlobalSection(SolutionProperties) = preSolution 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp

    r3037 r3207  
    1818#include <time.h> 
    1919#include "glInterface.h" 
    20  
    21 #include <Cg/cg.h> 
    22 #include <Cg/cgGL.h> 
    2320 
    2421 
     
    5350#include "Light.h" 
    5451#include "SceneEntityConverter.h" 
    55 #include "ObjConverter.h" 
    5652#include "SkyPreetham.h" 
    5753#include "Texture.h" 
    58 #include "EntityMerger.h" 
     54#include "ShaderManager.h" 
     55#include "MotionPath.h" 
     56#include "ShaderProgram.h" 
     57#include "Shape.h" 
     58 
    5959 
    6060using namespace std; 
     
    6262 
    6363 
     64/// the environment for the program parameter 
    6465static Environment env; 
    6566 
    66 #define MAX_DEPTH_CONST 10.0f 
    67  
    68 // fbo 
     67 
     68GLuint fontTex; 
     69/// the fbo used for MRT 
    6970FrameBufferObject *fbo = NULL; 
    70  
    71 GLuint fontTex; 
    72  
    7371/// the renderable scene geometry 
    7472SceneEntityContainer sceneEntities; 
    75  
     73SceneEntityContainer dynamicObjects; 
    7674// traverses and renders the hierarchy 
    7775RenderTraverser *traverser = NULL; 
     
    7977Bvh *bvh = NULL; 
    8078/// handles scene loading 
    81 ResourceManager *loader = NULL; 
     79ResourceManager *resourceManager = NULL; 
     80/// handles scene loading 
     81ShaderManager *shaderManager = NULL; 
    8282/// the scene camera 
    83 Camera *camera = NULL; 
     83PerspectiveCamera *camera = NULL; 
    8484/// the scene camera 
    85 Camera *visCamera = NULL; 
     85PerspectiveCamera *visCamera = NULL; 
    8686/// the visualization 
    8787Visualization *visualization = NULL; 
    88 /// the current render state 
    89 RenderState state; 
     88/// the current render renderState 
     89RenderState renderState; 
    9090/// the rendering algorithm 
    9191int renderMode = RenderTraverser::CHCPLUSPLUS; 
    92 // eye near plane distance 
    93 float nearDist = 0.2f; 
     92/// eye near plane distance 
     93const float nearDist = 0.2f; 
     94//const float nearDist = 1.0f; 
     95/// eye far plane distance 
    9496float farDist = 1e6f; 
    9597/// the field of view 
    96 float fov = 50.0f; 
    97 /// the pixel threshold where a node is still considered invisible 
    98 int threshold; 
    99  
    100 int assumedVisibleFrames = 10; 
    101 int maxBatchSize = 50; 
    102  
    103 int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES; 
     98const float fov = 50.0f; 
    10499 
    105100SceneQuery *sceneQuery = NULL; 
    106101RenderQueue *renderQueue = NULL; 
    107  
    108 // traverses and renders the hierarchy 
     102/// traverses and renders the hierarchy 
    109103RenderTraverser *shadowTraverser = NULL; 
    110  
     104/// the skylight + skydome model 
    111105SkyPreetham *preetham = NULL; 
     106 
     107MotionPath *motionPath = NULL; 
     108/// max depth where candidates for tighter bounds are searched 
     109int maxDepthForTestingChildren = 3; 
     110 
     111 
     112/// the technique used for rendering 
     113enum RenderTechnique 
     114{ 
     115        FORWARD, 
     116        DEFERRED, 
     117        DEPTH_PASS 
     118}; 
    112119 
    113120 
     
    115122enum RenderMethod 
    116123{ 
    117         RENDER_FIXED, 
     124        RENDER_FORWARD, 
    118125        RENDER_DEPTH_PASS, 
    119126        RENDER_DEFERRED, 
     
    123130 
    124131/// one of four possible render methods 
    125 int renderMethod = RENDER_FIXED; 
    126  
     132int renderMethod = RENDER_FORWARD; 
     133 
     134static int winWidth = 1024; 
     135static int winHeight = 768; 
     136static float winAspectRatio = 1.0f; 
    127137 
    128138/// these values get scaled with the frame rate 
     
    131141 
    132142/// elapsed time in milliseconds 
    133 double elapsedTime = 1000.0f; 
    134 double algTime = 1000.0f; 
    135  
    136 static int winWidth = 1024; 
    137 static int winHeight = 768; 
     143double elapsedTime = 1000.0; 
     144double algTime = 1000.0; 
     145double accumulatedTime = 1000.0; 
     146float fps = 1e3f; 
     147float turbitity = 5.0f; 
    138148 
    139149int shadowSize = 2048; 
    140  
    141 static float winAspectRatio = 1.0f; 
    142  
    143 double accumulatedTime = 1000; 
    144 float fps = 1e3f; 
    145  
     150/// the hud font 
    146151glfont::GLFont myfont; 
    147152 
     
    161166int numBatches = 0; 
    162167 
    163  
    164 // mouse navigation state 
     168// mouse navigation renderState 
    165169int xEyeBegin = 0; 
    166170int yEyeBegin = 0; 
     
    168172int verticalMotionBegin = 0; 
    169173int horizontalMotionBegin = 0; 
    170  
    171174 
    172175bool leftKeyPressed = false; 
     
    176179bool descendKeyPressed = false; 
    177180bool ascendKeyPressed = false; 
     181bool leftStrafeKeyPressed = false; 
     182bool rightStrafeKeyPressed = false; 
     183 
     184bool altKeyPressed = false; 
    178185 
    179186bool showHelp = false; 
     
    200207bool showShadowMap = false; 
    201208bool renderLightView = false; 
    202  
    203 bool altKeyPressed = false; 
    204  
    205209bool useHDR = true; 
    206  
    207 static float ssaoTempCohFactor = 255.0; 
    208  
     210bool useAntiAliasing = true; 
    209211 
    210212PerfTimer frameTimer, algTimer; 
    211  
    212 static int sCurrentMrtSet = 0; 
    213  
     213/// the performance window 
    214214PerformanceGraph *perfGraph = NULL; 
     215 
     216float ssaoTempCohFactor = 255.0; 
     217bool sortSamples = true; 
     218int sCurrentMrtSet = 0; 
     219 
     220static Matrix4x4 invTrafo = IdentityMatrix(); 
     221 
     222 
     223////////////// 
     224//-- algorithm parameters 
     225 
     226/// the pixel threshold where a node is still considered invisible  
     227/// (should be zero for conservative visibility) 
     228int threshold; 
     229int assumedVisibleFrames = 10; 
     230int maxBatchSize = 50; 
     231int trianglesPerVirtualLeaf = INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES; 
     232 
     233////////////// 
     234 
     235enum {CAMERA_PASS = 0, LIGHT_PASS = 1}; 
    215236 
    216237 
     
    220241ShadowMap *shadowMap = NULL; 
    221242DirectionalLight *light = NULL; 
    222 DeferredRenderer *ssaoShader = NULL; 
    223  
    224 SceneEntity *cube = NULL; 
    225 SceneEntity *aeroplane = NULL; 
     243DeferredRenderer *deferredShader = NULL; 
     244 
     245 
     246SceneEntity *buddha = NULL; 
    226247SceneEntity *skyDome = NULL; 
    227248 
    228249 
    229  
    230 // function forward declarations 
     250//////////////////// 
     251//--- function forward declarations 
     252 
    231253void InitExtensions(); 
     254void InitGLstate(); 
     255 
    232256void DisplayVisualization(); 
    233 void InitGLstate(); 
    234 void InitRenderTexture(); 
    235 void InitCg(); 
     257/// destroys all allocated resources 
    236258void CleanUp(); 
    237259void SetupEyeView(); 
    238 void UpdateEyeMtx(); 
    239260void SetupLighting(); 
    240261void DisplayStats(); 
    241 void Output(int x, int y, const char *string); 
     262/// draw the help screen 
    242263void DrawHelpMessage(); 
     264/// render the sky dome 
    243265void RenderSky(); 
     266/// render the objects found visible in the depth pass 
    244267void RenderVisibleObjects(); 
    245268 
    246269void Begin2D(); 
    247270void End2D(); 
     271/// the main loop 
     272void MainLoop(); 
     273 
    248274void KeyBoard(unsigned char c, int x, int y); 
    249 void DrawStatistics(); 
    250 void Display(); 
    251275void Special(int c, int x, int y); 
    252276void KeyUp(unsigned char c, int x, int y); 
    253277void SpecialKeyUp(int c, int x, int y); 
    254278void Reshape(int w, int h); 
    255 void Mouse(int button, int state, int x, int y); 
     279void Mouse(int button, int renderState, int x, int y); 
    256280void LeftMotion(int x, int y); 
    257281void RightMotion(int x, int y); 
    258282void MiddleMotion(int x, int y); 
    259 void CalcDecimalPoint(string &str, int d); 
    260  
    261 RenderTraverser *CreateTraverser(Camera *cam); 
    262  
    263283void KeyHorizontalMotion(float shift); 
    264284void KeyVerticalMotion(float shift); 
    265  
     285/// returns the string representation of a number with the decimal points 
     286void CalcDecimalPoint(string &str, int d); 
     287/// Creates the traversal method (vfc, stopandwait, chc, chc++) 
     288RenderTraverser *CreateTraverser(PerspectiveCamera *cam); 
     289/// place the viewer on the floor plane 
    266290void PlaceViewer(const Vector3 &oldPos); 
     291// initialise the frame buffer objects 
     292void InitFBO(); 
     293/// changes the sunlight direction 
     294void RightMotionLight(int x, int y); 
     295/// render the shader map 
     296void RenderShadowMap(float newfar); 
     297/// function that touches each material once in order to accelarate render queue 
     298void PrepareRenderQueue(); 
     299/// loads the specified model 
     300void LoadModel(const string &model, SceneEntityContainer &entities); 
    267301 
    268302inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; } 
    269303inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; } 
    270 // initialise the frame buffer objects 
    271 void InitFBO(); 
    272  
    273 void RightMotionLight(int x, int y); 
    274  
    275 void RenderShadowMap(float newfar); 
    276  
    277  
    278  
    279 ///////// 
    280 //-- cg stuff 
    281  
    282 static CGcontext sCgContext = NULL; 
    283  
    284 static CGparameter sEyePosParamTex; 
    285 static CGparameter sEyePosParam; 
    286  
     304 
     305void CreateAnimation(); 
     306 
     307SceneQuery *GetOrCreateSceneQuery(); 
     308 
     309 
     310// new view projection matrix of the camera 
    287311static Matrix4x4 viewProjMat = IdentityMatrix(); 
     312// the old view projection matrix of the camera 
    288313static Matrix4x4 oldViewProjMat = IdentityMatrix(); 
    289314 
    290  
    291  
    292 static void cgErrorCallback() 
    293 { 
    294         CGerror lastError = cgGetError(); 
    295  
    296         if(lastError) 
    297         { 
    298                 printf("%s\n\n", cgGetErrorString(lastError)); 
    299                 printf("%s\n", cgGetLastListing(sCgContext)); 
    300                  
    301                 printf("Cg error, exiting...\n"); 
    302  
    303                 exit(0); 
    304         } 
    305 } 
    306315 
    307316 
     
    321330int main(int argc, char* argv[]) 
    322331{ 
    323 /* 
    324332#ifdef _CRT_SET 
    325  
    326333        //Now just call this function at the start of your program and if you're 
    327334        //compiling in debug mode (F5), any leaks will be displayed in the Output 
     
    334341        _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);  
    335342#endif 
    336 */ 
     343 
    337344        cout << "=== reading environment file ===" << endl << endl; 
    338345 
     
    355362                env.GetIntParam(string("maxBatchSize"), maxBatchSize); 
    356363                env.GetIntParam(string("trianglesPerVirtualLeaf"), trianglesPerVirtualLeaf); 
     364                env.GetIntParam(string("winWidth"), winWidth); 
     365                env.GetIntParam(string("winHeight"), winHeight); 
     366                env.GetIntParam(string("shadowSize"), shadowSize); 
     367                env.GetIntParam(string("maxDepthForTestingChildren"), maxDepthForTestingChildren); 
    357368 
    358369                env.GetFloatParam(string("keyForwardMotion"), keyForwardMotion); 
    359370                env.GetFloatParam(string("keyRotation"), keyRotation); 
    360  
    361                 env.GetIntParam(string("winWidth"), winWidth); 
    362                 env.GetIntParam(string("winHeight"), winHeight); 
    363  
    364                 env.GetBoolParam(string("useFullScreen"), useFullScreen); 
    365371                env.GetFloatParam(string("tempCohFactor"), ssaoTempCohFactor); 
     372                env.GetFloatParam(string("turbitity"), turbitity); 
     373                 
    366374                env.GetVectorParam(string("camPosition"), camPos); 
    367375                env.GetVectorParam(string("camDirection"), camDir); 
    368376                env.GetVectorParam(string("lightDirection"), lightDir); 
    369377 
     378                env.GetBoolParam(string("useFullScreen"), useFullScreen); 
    370379                env.GetBoolParam(string("useLODs"), useLODs); 
    371                 env.GetIntParam(string("shadowSize"), shadowSize); 
    372  
    373380                env.GetBoolParam(string("useHDR"), useHDR); 
     381                env.GetBoolParam(string("useAA"), useAntiAliasing); 
     382                env.GetBoolParam(string("useAdvancedShading"), useAdvancedShading); 
     383 
     384                env.GetIntParam(string("renderMethod"), renderMethod); 
    374385 
    375386                //env.GetStringParam(string("modelPath"), model_path); 
     
    389400                cout << "temporal coherence: " << ssaoTempCohFactor << endl; 
    390401                cout << "shadow size: " << shadowSize << endl; 
     402                cout << "render method: " << renderMethod << endl; 
     403                cout << "use antialiasing: " << useAntiAliasing << endl; 
     404                cout << "use advanced shading: " << useAdvancedShading << endl; 
     405                cout << "turbitity: " << turbitity << endl; 
    391406 
    392407                //cout << "model path: " << model_path << endl; 
    393  
    394408                cout << "**** end parameters ****" << endl << endl; 
    395409        } 
     
    397411        /////////////////////////// 
    398412 
    399         camera = new Camera(winWidth, winHeight, fov); 
     413        camera = new PerspectiveCamera(winWidth / winHeight, fov); 
    400414        camera->SetNear(nearDist); 
    401415        camera->SetFar(1000); 
     
    404418        camera->SetPosition(camPos); 
    405419 
    406         visCamera = new Camera(winWidth, winHeight, fov); 
     420        visCamera = new PerspectiveCamera(winWidth / winHeight, fov); 
    407421        visCamera->SetNear(0.0f); 
    408422        visCamera->Yaw(.5 * M_PI); 
     
    410424        // create a new light 
    411425        light = new DirectionalLight(lightDir, RgbaColor(1, 1, 1, 1), RgbaColor(1, 1, 1, 1)); 
    412  
    413  
    414         renderQueue = new RenderQueue(&state, camera); 
     426        // the render queue for material sorting 
     427        renderQueue = new RenderQueue(&renderState); 
    415428 
    416429        glutInitWindowSize(winWidth, winHeight); 
     
    432445        } 
    433446 
    434         glutDisplayFunc(Display); 
     447        glutDisplayFunc(MainLoop); 
    435448        glutKeyboardFunc(KeyBoard); 
    436449        glutSpecialFunc(Special); 
    437450        glutReshapeFunc(Reshape); 
    438451        glutMouseFunc(Mouse); 
    439         glutIdleFunc(Display); 
     452        glutIdleFunc(MainLoop); 
    440453        glutKeyboardUpFunc(KeyUp); 
    441454        glutSpecialUpFunc(SpecialKeyUp); 
     
    452465        MiddleMotion(0, 0); 
    453466 
    454         // init cg shader programs 
    455         InitCg(); 
    456  
    457467        perfGraph = new PerformanceGraph(1000); 
    458468 
    459         loader = ResourceManager::GetSingleton(); 
    460  
    461         const string filename = string(model_path + "city.dem"); 
    462          
    463         if (loader->Load(filename, sceneEntities)) 
    464                 cout << "model " << filename << " loaded" << endl; 
    465         else 
    466         { 
    467                 cerr << "loading model " << filename << " failed" << endl; 
    468                 CleanUp(); 
    469                 exit(0); 
    470         } 
    471          
     469        resourceManager = ResourceManager::GetSingleton(); 
     470        shaderManager = ShaderManager::GetSingleton(); 
     471 
     472        /////////// 
     473        //-- load the static scene geometry 
     474 
     475        LoadModel("city.dem", sceneEntities); 
     476 
     477 
     478        ////////// 
     479        //-- load some dynamic stuff 
     480 
     481        //resourceManager->mUseNormalMapping = true; 
     482        //resourceManager->mUseNormalMapping = false; 
     483 
     484        //LoadModel("fisch.dem", dynamicObjects); 
     485        LoadModel("hbuddha.dem", dynamicObjects); 
     486        //LoadModel("venusm.dem", dynamicObjects); 
     487        //LoadModel("camel.dem", dynamicObjects); 
     488        //LoadModel("toyplane2.dem", dynamicObjects); 
     489        //LoadModel("elephal.dem", dynamicObjects); 
     490 
     491        resourceManager->mUseNormalMapping = false; 
     492 
     493        buddha = dynamicObjects.back(); 
     494         
     495        const Vector3 sceneCenter(470.398f, 240.364f, 181.7f); 
     496        //const Vector3 sceneCenter(470.398f, 240.364f, 180.3); 
     497         
     498        Matrix4x4 transl = TranslationMatrix(sceneCenter); 
     499        buddha->GetTransform()->SetMatrix(transl); 
     500 
     501        for (int i = 0; i < 10; ++ i) 
     502        { 
     503                SceneEntity *ent = new SceneEntity(*buddha); 
     504                resourceManager->AddSceneEntity(ent); 
     505 
     506                Vector3 offs = Vector3::ZERO(); 
     507 
     508                offs.x = RandomValue(.0f, 50.0f); 
     509                offs.y = RandomValue(.0f, 50.0f); 
     510 
     511                Vector3 newPos = sceneCenter + offs; 
     512 
     513                transl = TranslationMatrix(newPos); 
     514                Transform3 *transform = resourceManager->CreateTransform(transl); 
     515 
     516                ent->SetTransform(transform); 
     517                dynamicObjects.push_back(ent); 
     518        } 
     519 
     520 
     521        /////////// 
     522        //-- load the associated static bvh 
    472523 
    473524        const string bvh_filename = string(model_path + "city.bvh"); 
     525 
    474526        BvhLoader bvhLoader; 
    475         bvh = bvhLoader.Load(bvh_filename, sceneEntities); 
     527        bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects, maxDepthForTestingChildren); 
    476528 
    477529        if (!bvh) 
     
    482534        } 
    483535 
     536        /// set the depth of the bvh depending on the triangles per leaf node 
     537        bvh->SetVirtualLeaves(trianglesPerVirtualLeaf); 
     538 
    484539        // set far plane based on scene extent 
    485540        farDist = 10.0f * Magnitude(bvh->GetBox().Diagonal()); 
    486         bvh->SetVirtualLeaves(trianglesPerVirtualLeaf); 
    487  
    488         camera->SetFar(Magnitude(bvh->GetBox().Diagonal())); 
    489          
    490         Vector3 cubeCenter(470.398f, 240.364f, 182.5f); 
    491          
    492         Matrix4x4 transl = TranslationMatrix(cubeCenter); 
    493  
    494         SceneEntityContainer dummy; 
    495          
    496         string skyDomeStr(model_path + "sky.dem"); 
    497  
    498         if (loader->Load(skyDomeStr, sceneEntities)) 
    499         { 
    500                 cout << "successfully loaded " << sceneEntities.size() << " scene entities" << endl; 
    501         } 
    502         else 
    503         { 
    504                 cerr << "loading file " << skyDomeStr << " failed" << endl; 
    505  
    506                 CleanUp(); 
    507                 exit(0); 
    508         } 
    509  
     541        camera->SetFar(farDist); 
     542 
     543 
     544        ////////////////// 
     545        //-- setup the skydome model 
     546 
     547        LoadModel("sky.dem", sceneEntities); 
    510548        skyDome = sceneEntities.back(); 
    511549 
    512         //InitCg(); 
    513  
    514         const float turbitiy = 5.0f; 
    515         preetham = new SkyPreetham(turbitiy, skyDome); 
    516  
    517         // initialize the render traverser 
     550        /// the turbitity of the sky (from clear to hazy, use <3 for clear sky) 
     551        preetham = new SkyPreetham(turbitity, skyDome); 
     552 
     553        CreateAnimation(); 
     554 
     555 
     556        ////////// 
     557        //-- initialize the traversal algorithm 
     558 
    518559        traverser = CreateTraverser(camera); 
    519  
    520         visualization = new Visualization(bvh, camera, NULL, &state); 
    521          
    522         state.SetRenderPassType(RenderState::FIXED); 
    523  
     560         
     561        // the bird-eye visualization 
     562        visualization = new Visualization(bvh, camera, NULL, &renderState); 
     563 
     564        // this function assign the render queue bucket ids of the materials in beforehand 
     565        // => probably a little less overhead for new parts of the scene that are not yet assigned 
     566        PrepareRenderQueue(); 
     567        /// forward rendering is the default 
     568        renderState.SetRenderTechnique(FORWARD); 
    524569        // frame time is restarted every frame 
    525570        frameTimer.Start(); 
     
    527572        // the rendering loop 
    528573        glutMainLoop(); 
    529  
     574         
    530575        // clean up 
    531576        CleanUp(); 
     
    535580 
    536581 
    537 void InitCg(void)  
    538 { 
    539         // setup cg 
    540         cgSetErrorCallback(cgErrorCallback); 
    541  
    542         // create context. 
    543         sCgContext = cgCreateContext(); 
    544  
    545         // get the optimal profile 
    546         RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 
    547         cgGLSetOptimalOptions(RenderState::sCgFragmentProfile); 
    548  
    549         RenderState::sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX); 
    550         cgGLSetOptimalOptions(RenderState::sCgVertexProfile); 
    551  
    552         // set textures to auto load 
    553         cgGLSetManageTextureParameters(sCgContext, false); 
    554  
    555         RenderState::sCgMrtVertexProgram =  
    556                 new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgVertexProfile, "vtx"); 
    557  
    558         if (!RenderState::sCgMrtVertexProgram->IsValid()) 
    559                 cerr << "fragment program failed to load" << endl; 
    560  
    561         RenderState::sCgMrtFragmentTexProgram =  
    562                 new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "fragtex"); 
    563  
    564          
    565         if (!RenderState::sCgMrtFragmentTexProgram->IsValid()) 
    566                 cerr << "fragment tex program failed to load" << endl; 
    567  
    568         RenderState::sCgMrtFragmentTexProgram->AddParameter("tex", 0); 
    569  
    570         RenderState::sCgMrtFragmentProgram =  
    571                 new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "frag"); 
    572  
    573         if (!RenderState::sCgMrtFragmentProgram->IsValid()) 
    574                 cerr << "fragment program failed to load" << endl; 
    575          
    576  
    577         PrintGLerror("init"); 
    578  
    579         DeferredRenderer::InitCG(sCgContext); 
    580         SkyPreetham::InitCG(sCgContext); 
    581  
    582         cout << "cg initialization successful" << endl; 
    583 } 
    584  
    585  
    586582void InitFBO() 
    587583{ 
     
    589585 
    590586        // this fbo basicly stores the scene information we get from standard rendering of a frame 
    591         // we store colors, normals, positions (for the ssao) 
     587        // we store diffuse colors, eye space depth and normals 
    592588        fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32); 
    593         //fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24); 
    594589 
    595590        // the diffuse color buffer 
    596591        fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST); 
    597592        // the normals buffer 
    598         fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
    599         // the positions buffer 
    600         //fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
    601         fbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
     593        //fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
     594        fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
     595        // a rgb buffer which could hold material properties 
     596        //fbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST); 
     597        // buffer holding the difference vector to the old frame 
     598        fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR); 
    602599        // another color buffer 
    603600        fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST); 
    604601 
    605         PrintGLerror("fbo"); 
     602        for (int i = 0; i < 4; ++ i) 
     603                FrameBufferObject::InitBuffer(fbo, i); 
     604         
     605        PrintGLerror("init fbo"); 
    606606} 
    607607 
     
    741741 
    742742 
    743 RenderTraverser *CreateTraverser(Camera *cam) 
     743RenderTraverser *CreateTraverser(PerspectiveCamera *cam) 
    744744{ 
    745745        RenderTraverser *tr; 
    746746         
    747         bvh->ResetNodeClassifications(); 
    748  
    749747        switch (renderMode) 
    750748        { 
     
    769767        tr->SetHierarchy(bvh); 
    770768        tr->SetRenderQueue(renderQueue); 
    771         tr->SetRenderState(&state); 
     769        tr->SetRenderState(&renderState); 
    772770        tr->SetUseOptimization(useOptimization); 
    773771        tr->SetUseRenderQueue(useRenderQueue); 
     
    779777        tr->SetUseDepthPass((renderMethod == RENDER_DEPTH_PASS) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED)); 
    780778        tr->SetRenderQueue(renderQueue); 
     779        tr->SetShowBounds(showBoundingVolumes); 
     780 
     781        bvh->ResetNodeClassifications(); 
     782 
    781783 
    782784        return tr; 
    783785} 
    784786 
    785  
     787/** Setup sunlight 
     788*/ 
    786789void SetupLighting() 
    787790{ 
     
    800803         
    801804 
    802         const bool useToneMapping = ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED)) && useHDR; 
     805        const bool useHDRValues =  
     806                ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) ||  
     807                 (renderMethod == RENDER_DEFERRED)) && useHDR; 
     808 
    803809 
    804810        Vector3 sunAmbient; 
    805811        Vector3 sunDiffuse; 
    806812 
    807         preetham->ComputeSunColor(lightDir, sunAmbient, sunDiffuse, !useToneMapping); 
     813#if 1 
     814        preetham->ComputeSunColor(lightDir, sunAmbient, sunDiffuse, !useHDRValues); 
    808815 
    809816        ambient[0] = sunAmbient.x; 
     
    811818        ambient[2] = sunAmbient.z; 
    812819 
    813         // no tone mapping => scale 
    814         if (!useToneMapping) 
    815         { 
    816                 const float maxComponent = sunDiffuse.MaxComponent(); 
    817                 sunDiffuse /= maxComponent; 
    818         } 
    819  
    820820        diffuse[0] = sunDiffuse.x; 
    821821        diffuse[1] = sunDiffuse.y; 
    822822        diffuse[2] = sunDiffuse.z; 
    823823 
     824#else 
     825         
     826        ambient[0] = .2f; 
     827        ambient[1] = .2f; 
     828        ambient[2] = .2f; 
     829 
     830        diffuse[0] = 1.0f; 
     831        diffuse[1] = 1.0f; 
     832        diffuse[2] = 1.0f; 
     833 
     834#endif 
     835 
    824836        glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); 
    825837        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); 
     
    836848        oldViewProjMat = viewProjMat; 
    837849 
    838         glMatrixMode(GL_PROJECTION); 
    839         glLoadIdentity(); 
    840         gluPerspective(fov, winAspectRatio, nearDist, farDist); 
    841  
    842         glMatrixMode(GL_MODELVIEW); 
    843          
    844         // set up the camera view 
    845         camera->SetupCameraView(); 
    846  
    847                  
     850        camera->SetupViewProjection(); 
     851 
     852 
    848853        ///////////////// 
     854        //-- compute view projection matrix and store for later use 
    849855 
    850856        Matrix4x4 matViewing, matProjection; 
     
    853859        camera->GetProjectionMatrix(matProjection); 
    854860 
    855         // store matrix for later use 
    856861        viewProjMat = matViewing * matProjection; 
    857862} 
     
    881886 
    882887 
    883 static void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br) 
    884 { 
    885         Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; 
    886  
    887         camera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
    888  
    889         bl = Normalize(nbl - fbl); 
    890         br = Normalize(nbr - fbr); 
    891         tl = Normalize(ntl - ftl); 
    892         tr = Normalize(ntr - ftr); 
    893 } 
    894  
    895  
     888void KeyStrafe(float shift) 
     889{ 
     890        Vector3 viewDir = camera->GetDirection(); 
     891        Vector3 pos = camera->GetPosition(); 
     892 
     893        // the 90 degree rotated view vector  
     894        // z zero so we don't move in the vertical 
     895        Vector3 rVec(viewDir[0], viewDir[1], 0); 
     896 
     897        Matrix4x4 rot = RotationZMatrix(M_PI * 0.5f); 
     898        rVec = rot * rVec; 
     899        pos += rVec * shift; 
     900 
     901        camera->SetPosition(pos); 
     902} 
     903 
     904 
     905/** Initialize the deferred rendering pass. 
     906*/ 
    896907void InitDeferredRendering() 
    897908{ 
     
    901912        // multisampling does not work with deferred shading 
    902913        glDisable(GL_MULTISAMPLE_ARB); 
    903  
    904         state.SetRenderPassType(RenderState::DEFERRED); 
    905  
    906         cgGLEnableProfile(RenderState::sCgVertexProfile); 
    907         RenderState::sCgMrtVertexProgram->Bind(); 
    908  
    909         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    910         RenderState::sCgMrtFragmentProgram->Bind(); 
    911  
    912         const Vector3 pos = camera->GetPosition(); 
    913  
    914  
    915         // draw to 3 color buffers 
    916         // a color, normal, and positions buffer 
     914        renderState.SetRenderTechnique(DEFERRED); 
     915 
     916 
     917        // draw to 3 render targets 
    917918        if (sCurrentMrtSet == 0) 
    918919        { 
    919920                DeferredRenderer::colorBufferIdx = 0; 
    920                 glDrawBuffers(2, mrt); 
     921                glDrawBuffers(3, mrt); 
    921922        } 
    922923        else  
    923924        { 
    924925                DeferredRenderer::colorBufferIdx = 3; 
    925                 glDrawBuffers(2, mrt2); 
     926                glDrawBuffers(3, mrt2); 
    926927        } 
    927928 
     
    930931 
    931932 
    932 // the main rendering loop 
    933 void Display()  
     933/** the main rendering loop 
     934*/ 
     935void MainLoop()  
    934936{        
     937#if 1 
     938        GPUProgramParameters *vtxParams =  
     939                buddha->GetShape(0)->GetMaterial()->GetTechnique(1)->GetVertexProgramParameters(); 
     940 
     941        Matrix4x4 oldTrafo = buddha->GetTransform()->GetMatrix(); 
     942        Vector3 buddhaPos = motionPath->GetCurrentPosition(); 
     943        Matrix4x4 trafo = TranslationMatrix(buddhaPos); 
     944         
     945        buddha->GetTransform()->SetMatrix(trafo); 
     946 
     947        /*for (int i = 0; i < 10; ++ i) 
     948        { 
     949                SceneEntity *ent = dynamicObjects[i]; 
     950                Vector3 newPos = ent->GetWorldCenter(); 
     951 
     952                if (GetOrCreateSceneQuery()->CalcIntersection(newPos)) 
     953                { 
     954                        Matrix4x4 mat = TranslationMatrix(newPos - ent->GetCenter()); 
     955                        ent->GetTransform()->SetMatrix(mat); 
     956                } 
     957        }*/ 
     958 
     959        Matrix4x4 rotMatrix = RotationZMatrix(M_PI * 1e-3f); 
     960        dynamicObjects[1]->GetTransform()->MultMatrix(rotMatrix); 
     961 
     962 
     963        ///////////////////////// 
     964        //-- update animations 
     965         
     966        motionPath->Move(0.01f); 
     967 
     968#endif 
     969 
     970 
     971        ///////////// 
     972 
    935973        Vector3 oldPos = camera->GetPosition(); 
    936974 
     
    947985        if (descendKeyPressed) 
    948986                KeyVerticalMotion(-KeyShift()); 
     987        if (leftStrafeKeyPressed) 
     988                KeyStrafe(KeyShift()); 
     989        if (rightStrafeKeyPressed) 
     990                KeyStrafe(-KeyShift()); 
     991 
    949992 
    950993        // place view on ground 
     
    9581001         
    9591002 
    960  
    9611003        if ((!shadowMap || !shadowTraverser) && (showShadowMap || renderLightView)) 
    9621004        { 
     
    9711013        // bring eye modelview matrix up-to-date 
    9721014        SetupEyeView(); 
    973  
    974         // hack 
     1015        // set frame related parameters for GPU programs 
     1016        GPUProgramParameters::InitFrame(camera, light); 
     1017 
     1018        // hack: store current rendering method and restore later 
    9751019        int oldRenderMethod = renderMethod; 
    976  
    977         if (renderLightView) 
    978                 renderMethod = RenderState::FIXED; 
    979  
     1020        // for rendering the light view, we use forward rendering 
     1021        if (renderLightView) renderMethod = FORWARD; 
     1022 
     1023        /// enable vbo vertex array 
    9801024        glEnableClientState(GL_VERTEX_ARRAY); 
    981  
    9821025 
    9831026        // render with the specified method (forward rendering, forward + depth, deferred) 
    9841027        switch (renderMethod) 
    9851028        { 
    986         case RENDER_FIXED: 
     1029        case RENDER_FORWARD: 
    9871030         
    9881031                glEnable(GL_MULTISAMPLE_ARB); 
    989                  
    990                 state.SetRenderPassType(RenderState::FIXED); 
    991                 glEnable(GL_LIGHTING); 
    992  
    993                 cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    994                 cgGLDisableProfile(RenderState::sCgVertexProfile); 
     1032                renderState.SetRenderTechnique(FORWARD); 
     1033                //glEnable(GL_LIGHTING); 
    9951034 
    9961035                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    997  
    9981036                glEnableClientState(GL_NORMAL_ARRAY); 
    999  
    10001037                break; 
    10011038 
     
    10031040 
    10041041                glDisable(GL_MULTISAMPLE_ARB); 
    1005                 state.SetUseAlphaToCoverage(false); 
    1006  
    1007                 state.SetRenderPassType(RenderState::DEPTH_PASS); 
    1008  
    1009                 if (!fbo) InitFBO(); fbo->Bind(); 
     1042                renderState.SetUseAlphaToCoverage(false); 
     1043                renderState.SetRenderTechnique(DEPTH_PASS); 
     1044 
     1045                if (!fbo) InitFBO();  
     1046                fbo->Bind(); 
    10101047 
    10111048                glDrawBuffers(1, mrt); 
    10121049 
    1013                 cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    1014                 cgGLDisableProfile(RenderState::sCgVertexProfile); 
    1015  
    10161050                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    10171051 
    1018                 // the scene is rendered withouth any shading    
     1052                // the scene is rendered withouth any shading  
     1053                // (should be handled by render renderState) 
    10191054                glShadeModel(GL_FLAT); 
    1020                 glDisable(GL_LIGHTING); 
    1021                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    1022  
    1023  
    10241055                break; 
    10251056 
     
    10271058 
    10281059                glEnable(GL_MULTISAMPLE_ARB); 
    1029  
    1030                 cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    1031                 cgGLDisableProfile(RenderState::sCgVertexProfile); 
    1032  
    1033                 state.SetRenderPassType(RenderState::DEPTH_PASS); 
    1034  
    1035                 // the scene is rendered withouth any shading    
     1060                renderState.SetRenderTechnique(DEPTH_PASS); 
     1061 
     1062                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     1063 
     1064                // the scene is rendered withouth any shading  
     1065                // (should be handled by render renderState) 
    10361066                glShadeModel(GL_FLAT); 
    1037                 glDisable(GL_LIGHTING); 
    1038          
    1039                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    1040                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    1041  
    1042  
    1043                 break; 
    1044          
    1045         case RenderState::DEFERRED: 
     1067                break; 
     1068         
     1069        case RENDER_DEFERRED: 
    10461070 
    10471071                if (showShadowMap && !renderLightView) 
     
    10551079                glEnableClientState(GL_NORMAL_ARRAY); 
    10561080                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    1057  
    10581081                break; 
    10591082        } 
    10601083 
    10611084        glDepthFunc(GL_LESS); 
    1062  
    10631085        glDisable(GL_TEXTURE_2D); 
    10641086        glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
    10651087                 
    10661088 
    1067         // reset lod levels for current frame 
     1089        // set proper lod levels for current frame using current eye point 
    10681090        LODLevel::InitFrame(camera->GetPosition()); 
    1069  
    1070         // set up lights 
     1091        // set up sunlight 
    10711092        SetupLighting(); 
    10721093 
     
    10741095        if (renderLightView) 
    10751096        { 
    1076                 // change CHC++ set of state variables (must be done for each change of camera because 
    1077                 // otherwise the temporal coherency is broken 
    1078                 BvhNode::SetCurrentState(1); 
     1097                // change CHC++ set of renderState variables:  
     1098                // must be done for each change of camera because otherwise  
     1099                // the temporal coherency is broken 
     1100                BvhNode::SetCurrentState(LIGHT_PASS); 
    10791101                shadowMap->RenderShadowView(shadowTraverser, viewProjMat); 
    1080                 BvhNode::SetCurrentState(0); 
     1102                BvhNode::SetCurrentState(CAMERA_PASS); 
    10811103        } 
    10821104        else 
     
    11121134                FrameBufferObject::Release(); 
    11131135 
    1114                 cgGLDisableProfile(RenderState::sCgVertexProfile); 
    1115                 cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    1116  
    1117                 if (!ssaoShader) ssaoShader =  
    1118                         new DeferredRenderer(texWidth, texHeight, camera, farDist / MAX_DEPTH_CONST); 
     1136                if (!deferredShader) deferredShader =  
     1137                        new DeferredRenderer(texWidth, texHeight, camera); 
    11191138                 
    11201139                DeferredRenderer::SHADING_METHOD shadingMethod; 
     
    11301149                        shadingMethod = DeferredRenderer::DEFAULT; 
    11311150 
    1132  
    1133                 ssaoShader->SetShadingMethod(shadingMethod); 
    1134                 ssaoShader->SetSamplingMethod(samplingMethod); 
    1135                 ssaoShader->SetUseTemporalCoherence(useTemporalCoherence); 
     1151                deferredShader->SetShadingMethod(shadingMethod); 
     1152                deferredShader->SetSamplingMethod(samplingMethod); 
     1153                deferredShader->SetUseTemporalCoherence(useTemporalCoherence); 
     1154                deferredShader->SetSortSamples(sortSamples); 
    11361155 
    11371156                ShadowMap *sm = showShadowMap ? shadowMap : NULL; 
    1138                 ssaoShader->Render(fbo, oldViewProjMat, viewProjMat, ssaoTempCohFactor, light, useHDR, sm); 
    1139         } 
    1140  
    1141  
    1142         state.SetRenderPassType(RenderState::FIXED); 
    1143         state.Reset(); 
     1157                deferredShader->Render(fbo, ssaoTempCohFactor, light, useHDR, useAntiAliasing, sm); 
     1158        } 
     1159 
     1160 
     1161        renderState.SetRenderTechnique(FORWARD); 
     1162        renderState.Reset(); 
    11441163 
    11451164 
     
    11841203        { 
    11851204        case 27: 
     1205                Debug << "camPosition=" << camera->GetPosition().x << " " << camera->GetPosition().y << " " << camera->GetPosition().z << endl; 
     1206                Debug << "camDirection=" << camera->GetDirection().x << " " << camera->GetDirection().y << " " << camera->GetDirection().z << endl; 
     1207 
    11861208                CleanUp(); 
    11871209                exit(0); 
    1188                 break; 
    11891210        case 32: // space 
    11901211                renderMode = (renderMode + 1) % RenderTraverser::NUM_TRAVERSAL_TYPES; 
     
    12501271                //if (ssaoTempCohFactor > 1.0f) ssaoExpFactor = 1.0f; 
    12511272                break; 
    1252         case '9': 
     1273        case 'l': 
     1274        case 'L': 
    12531275                useLODs = !useLODs; 
    12541276                SceneEntity::SetUseLODs(useLODs); 
     
    12921314                downKeyPressed = true; 
    12931315                break; 
     1316        case 'j': 
     1317        case 'J': 
     1318                leftStrafeKeyPressed = true; 
     1319                break; 
     1320        case 'k': 
     1321        case 'K': 
     1322                rightStrafeKeyPressed = true; 
     1323                break; 
    12941324        case 'r': 
    12951325        case 'R': 
    12961326                useRenderQueue = !useRenderQueue; 
    12971327                traverser->SetUseRenderQueue(useRenderQueue); 
    1298                  
    12991328                break; 
    13001329        case 'b': 
     
    13031332                traverser->SetUseTightBounds(useTightBounds); 
    13041333                break; 
    1305         case 'l': 
    1306         case 'L': 
     1334        case 'v': 
     1335        case 'V': 
    13071336                renderLightView = !renderLightView; 
    13081337                break; 
     
    13111340                useHDR = !useHDR; 
    13121341                break; 
     1342        case 'i': 
     1343        case 'I': 
     1344                useAntiAliasing = !useAntiAliasing; 
     1345                break; 
     1346        case '9': 
     1347                sortSamples = !sortSamples; 
     1348                break; 
    13131349        default: 
    13141350                return; 
     
    13711407                ascendKeyPressed = false; 
    13721408                break; 
    1373          
     1409        case 'j': 
     1410        case 'J': 
     1411                leftStrafeKeyPressed = false; 
     1412                break; 
     1413        case 'k': 
     1414        case 'K': 
     1415                rightStrafeKeyPressed = false; 
     1416                break; 
    13741417        default: 
    13751418                return; 
     
    14791522 
    14801523 
    1481 void Mouse(int button, int state, int x, int y)  
    1482 { 
    1483         if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) 
     1524void Mouse(int button, int renderState, int x, int y)  
     1525{ 
     1526        if ((button == GLUT_LEFT_BUTTON) && (renderState == GLUT_DOWN)) 
    14841527        { 
    14851528                xEyeBegin = x; 
     
    14881531                glutMotionFunc(LeftMotion); 
    14891532        } 
    1490         else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) 
     1533        else if ((button == GLUT_RIGHT_BUTTON) && (renderState == GLUT_DOWN)) 
    14911534        { 
    14921535                xEyeBegin = x; 
     
    14991542                        glutMotionFunc(RightMotionLight); 
    15001543        } 
    1501         else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN)) 
     1544        else if ((button == GLUT_MIDDLE_BUTTON) && (renderState == GLUT_DOWN)) 
    15021545        { 
    15031546                horizontalMotionBegin = x; 
     
    15791622 
    15801623 
    1581 // strafe 
     1624/** strafe 
     1625*/ 
    15821626void MiddleMotion(int x, int y)  
    15831627{ 
     
    16831727 
    16841728        glLoadIdentity(); 
    1685          
    16861729        glOrtho(-offs, offs, -offs, offs, 0.0f, box.Size().z * 100.0f);  
    16871730 
     
    17111754        //-- visualization of the occlusion culling 
    17121755 
    1713         visualization->Render(); 
     1756        visualization->Render(showShadowMap); 
    17141757 
    17151758         
     
    17351778        DEL_PTR(perfGraph); 
    17361779        DEL_PTR(fbo); 
    1737         DEL_PTR(ssaoShader); 
     1780        DEL_PTR(deferredShader); 
    17381781        DEL_PTR(light); 
    17391782        DEL_PTR(visCamera); 
     
    17411784        DEL_PTR(shadowMap); 
    17421785        DEL_PTR(shadowTraverser); 
    1743         DEL_PTR(RenderState::sCgMrtFragmentProgram); 
    1744         DEL_PTR(RenderState::sCgMrtFragmentTexProgram); 
    1745         DEL_PTR(RenderState::sCgMrtVertexProgram); 
    1746          
     1786        DEL_PTR(motionPath); 
     1787 
    17471788        ResourceManager::DelSingleton(); 
    1748         loader = NULL; 
    1749  
    1750         DeferredRenderer::ReleaseCG(); 
    1751         SkyPreetham::ReleaseCG(); 
    1752  
    1753         if (sCgContext) 
    1754                 cgDestroyContext(sCgContext); 
     1789        ShaderManager::DelSingleton(); 
     1790 
     1791        resourceManager = NULL; 
     1792        shaderManager = NULL; 
    17551793} 
    17561794 
     
    18781916 
    18791917                glEnable(GL_TEXTURE_2D); 
    1880  
    18811918                myfont.Begin(); 
    18821919 
     
    18841921                { 
    18851922                        glColor3f(0.0f, 1.0f, 0.0f); 
    1886  
    18871923                        int i = 0; 
    18881924 
    1889                         static char *renderMethodStr[] = {"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"}; 
    1890          
     1925                        static char *renderMethodStr[] =  
     1926                                {"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"}; 
    18911927                        sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d",  
    1892                                                         useMultiQueries, useTightBounds, useRenderQueue); 
    1893  
     1928                                        useMultiQueries, useTightBounds, useRenderQueue); 
    18941929                        sprintf(msg[i ++], "render technique: %s, SSAO: %d", renderMethodStr[renderMethod], useAdvancedShading); 
    1895  
    18961930                        sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf); 
    1897  
    18981931                        sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d",  
    18991932                                assumedVisibleFrames, maxBatchSize); 
     
    19121945                        int len = 10; 
    19131946                        CalcDecimalPoint(objStr, renderedObjects, len); 
    1914                         CalcDecimalPoint(totalObjStr, (int)loader->GetNumEntities(), len); 
     1947                        CalcDecimalPoint(totalObjStr, (int)resourceManager->GetNumEntities(), len); 
    19151948 
    19161949                        CalcDecimalPoint(triStr, renderedTriangles, len); 
     
    19321965                        sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d", 
    19331966                                traversedNodes, frustumCulledNodes, queryCulledNodes); 
    1934  
    1935                         sprintf(msg[i ++], "issued queries: %5d, state changes: %5d, render batches: %5d",  
     1967                        sprintf(msg[i ++], "issued queries: %5d, renderState changes: %5d, render batches: %5d",  
    19361968                                issuedQueries, stateChanges, numBatches); 
    19371969 
     
    19441976                 
    19451977                if (!showAlgorithmTime) 
    1946                                 sprintf(msg[7], "%s:  %6.1f fps", alg_str[renderMode], fps); 
     1978                        sprintf(msg[7], "%s:  %6.1f fps", alg_str[renderMode], fps); 
    19471979                else 
    19481980                        sprintf(msg[7], "%s:  %6.1f ms", alg_str[renderMode], rTime); 
     
    19631995void RenderSky() 
    19641996{ 
    1965         cgGLEnableProfile(RenderState::sCgVertexProfile); 
    1966  
    19671997        if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED)) 
    1968                 state.SetRenderPassType(RenderState::DEFERRED); 
     1998                renderState.SetRenderTechnique(DEFERRED); 
    19691999 
    19702000        const bool useToneMapping =  
    19712001                ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) ||  
    19722002                 (renderMethod == RENDER_DEFERRED)) && useHDR; 
    1973  
    1974         preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping); 
    1975  
    1976         cgGLDisableProfile(RenderState::sCgVertexProfile); 
    1977         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     2003         
     2004        preetham->RenderSkyDome(-light->GetDirection(), camera, &renderState, !useToneMapping); 
     2005        /// once again reset the renderState 
     2006        renderState.Reset(); 
    19782007} 
    19792008 
     
    19862015                if (showShadowMap && !renderLightView) 
    19872016                { 
    1988                         float minVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance()); 
    1989                         RenderShadowMap(minVisibleDist); 
     2017                        // usethe maximal visible distance to focus shadow map 
     2018                        float maxVisibleDist = min(camera->GetFar(), traverser->GetMaxVisibleDistance()); 
     2019                        RenderShadowMap(maxVisibleDist); 
    19902020                } 
    1991  
    1992                 glDisable(GL_LIGHTING); 
    1993                 glViewport(0, 0, texWidth, texHeight); 
    1994  
     2021                // initialize deferred rendering 
    19952022                InitDeferredRendering(); 
    19962023        } 
    19972024        else 
    19982025        { 
    1999                 glEnable(GL_LIGHTING); 
    2000                 state.SetRenderPassType(RenderState::FIXED); 
    2001         } 
    2002  
     2026                renderState.SetRenderTechnique(FORWARD); 
     2027        } 
     2028 
     2029 
     2030        ///////////////// 
     2031        //-- reset gl renderState before the final visible objects pass 
     2032 
     2033        renderState.Reset(); 
     2034 
     2035        glEnableClientState(GL_NORMAL_ARRAY); 
     2036        /// switch back to smooth shading 
    20032037        glShadeModel(GL_SMOOTH); 
    2004         glEnableClientState(GL_NORMAL_ARRAY); 
    2005  
    2006         // draw all objects that have exactly the same depth as the current sample 
    2007         glDepthFunc(GL_LEQUAL); 
    2008         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    2009  
     2038        /// reset alpha to coverage flag 
     2039        renderState.SetUseAlphaToCoverage(true); 
     2040        // clear color 
    20102041        glClear(GL_COLOR_BUFFER_BIT); 
    2011  
    2012         state.SetUseAlphaToCoverage(true); 
    2013         state.Reset(); 
     2042         
     2043        // draw only objects having exactly the same depth as the current sample 
     2044        glDepthFunc(GL_EQUAL); 
    20142045 
    20152046        //cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl; 
    20162047 
    2017         SceneEntityContainer::const_iterator sit,  
     2048        SceneEntityContainer::const_iterator sit, 
    20182049                sit_end = traverser->GetVisibleObjects().end(); 
    20192050 
     
    20222053                renderQueue->Enqueue(*sit); 
    20232054        } 
    2024  
     2055        /// now render out everything in one giant pass 
    20252056        renderQueue->Apply(); 
    20262057 
     2058        // switch back to standard depth func 
    20272059        glDepthFunc(GL_LESS); 
    2028         state.Reset(); 
     2060        renderState.Reset(); 
    20292061 
    20302062        PrintGLerror("visibleobjects"); 
     
    20322064 
    20332065 
     2066SceneQuery *GetOrCreateSceneQuery() 
     2067{ 
     2068        if (!sceneQuery) 
     2069                sceneQuery = new SceneQuery(bvh->GetBox(), traverser, &renderState); 
     2070 
     2071        return sceneQuery; 
     2072} 
     2073 
     2074 
    20342075void PlaceViewer(const Vector3 &oldPos) 
    20352076{ 
    2036         if (!sceneQuery) 
    2037                 sceneQuery = new SceneQuery(bvh->GetBox(), traverser); 
    2038  
    20392077        Vector3 playerPos = camera->GetPosition(); 
    2040         bool validIntersect = sceneQuery->CalcIntersection(playerPos); 
     2078        bool validIntersect = GetOrCreateSceneQuery()->CalcIntersection(playerPos); 
    20412079 
    20422080        if (validIntersect)  
     
    20502088void RenderShadowMap(float newfar) 
    20512089{ 
    2052         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    2053         cgGLDisableProfile(RenderState::sCgVertexProfile); 
    2054  
    20552090        glDisableClientState(GL_NORMAL_ARRAY); 
    2056  
    2057         state.SetRenderPassType(RenderState::DEPTH_PASS); 
    2058         state.LockCullFaceEnabled(true); 
    2059         state.SetUseAlphaToCoverage(false); 
    2060  
    2061         // change CHC++ set of state variables  
     2091        renderState.SetRenderTechnique(DEPTH_PASS); 
     2092         
     2093        // hack: disable cull face because of alpha textured balconies 
     2094        glDisable(GL_CULL_FACE); 
     2095        renderState.LockCullFaceEnabled(true); 
     2096 
     2097        /// don't use alpha to coverage for the depth map (problems with fbo rendering) 
     2098        renderState.SetUseAlphaToCoverage(false); 
     2099 
     2100        // change CHC++ set of renderState variables  
    20622101        // this must be done for each change of camera because 
    20632102        // otherwise the temporal coherency is broken 
    2064         BvhNode::SetCurrentState(1); 
    2065  
     2103        BvhNode::SetCurrentState(LIGHT_PASS); 
    20662104        // hack: temporarily change camera far plane 
    20672105        camera->SetFar(newfar); 
    2068         glDisable(GL_CULL_FACE); 
    2069          
    20702106        // the scene is rendered withouth any shading    
    20712107        shadowMap->ComputeShadowMap(shadowTraverser, viewProjMat); 
    20722108 
     2109        camera->SetFar(farDist); 
     2110 
     2111        renderState.SetUseAlphaToCoverage(true); 
     2112        renderState.LockCullFaceEnabled(false); 
    20732113        glEnable(GL_CULL_FACE); 
    2074         camera->SetFar(farDist); 
    2075  
    2076         state.SetUseAlphaToCoverage(true); 
    2077         state.LockCullFaceEnabled(false); 
    20782114 
    20792115        glEnableClientState(GL_NORMAL_ARRAY); 
    2080  
    2081         // change back state 
    2082         BvhNode::SetCurrentState(0); 
    2083 } 
     2116        // change back renderState 
     2117        BvhNode::SetCurrentState(CAMERA_PASS); 
     2118} 
     2119 
     2120 
     2121/** Touch each material once in order to preload the render queue  
     2122        bucket id of each material 
     2123*/ 
     2124void PrepareRenderQueue() 
     2125{ 
     2126        for (int i = 0; i < 3; ++ i) 
     2127        { 
     2128                renderState.SetRenderTechnique(i); 
     2129 
     2130                // fill all shapes into the render queue        once so we can establish the buckets 
     2131                ShapeContainer::const_iterator sit, sit_end = (*resourceManager->GetShapes()).end(); 
     2132 
     2133                for (sit = (*resourceManager->GetShapes()).begin(); sit != sit_end; ++ sit) 
     2134                { 
     2135                        static Transform3 dummy(IdentityMatrix()); 
     2136                        renderQueue->Enqueue(*sit, NULL); 
     2137                } 
     2138         
     2139                // just clear queue again 
     2140                renderQueue->Clear(); 
     2141        } 
     2142} 
     2143 
     2144 
     2145void LoadModel(const string &model, SceneEntityContainer &entities) 
     2146{ 
     2147        const string filename = string(model_path + model); 
     2148 
     2149        cout << "\nloading model " << filename << endl; 
     2150        if (resourceManager->Load(filename, entities)) 
     2151                cout << "model " << filename << " successfully loaded" << endl; 
     2152        else 
     2153        { 
     2154                cerr << "loading model " << filename << " failed" << endl; 
     2155                CleanUp(); 
     2156                exit(0); 
     2157        } 
     2158} 
     2159 
     2160 
     2161void CreateAnimation() 
     2162{ 
     2163        const float radius = 5.0f; 
     2164        const Vector3 center(480.398f, 268.364f, 181.3); 
     2165 
     2166        VertexArray vertices; 
     2167 
     2168        /*for (int i = 0; i < 360; ++ i) 
     2169        { 
     2170                float angle = (float)i * M_PI / 180.0f; 
     2171 
     2172                Vector3 offs = Vector3(cos(angle) * radius, sin(angle) * radius, 0); 
     2173                vertices.push_back(center + offs); 
     2174        }*/ 
     2175 
     2176        for (int i = 0; i < 5; ++ i) 
     2177        { 
     2178                Vector3 offs = Vector3(i, 0, 0); 
     2179                vertices.push_back(center + offs); 
     2180        } 
     2181 
     2182         
     2183        for (int i = 0; i < 5; ++ i) 
     2184        { 
     2185                Vector3 offs = Vector3(4 -i, 0, 0); 
     2186                vertices.push_back(center + offs); 
     2187        } 
     2188 
     2189        motionPath = new MotionPath(vertices); 
     2190} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r3206 r3207  
    267267        // actually 0 means pixel is valid 
    268268        const float pixelIsValid = 0.0f; 
     269        // means that we only use slight temporal coherence over some frames 
     270        // so that there si no noticeable drag 
    269271        const float pixelCouldBeValid = 2.0f; 
     272        // this pixel information has to be discarded in order to not create artifacts 
    270273        const float pixelNotValid = 10.0f; 
    271274 
Note: See TracChangeset for help on using the changeset viewer.