Ignore:
Timestamp:
08/22/08 13:20:06 (16 years ago)
Author:
mattausch
Message:

cleaned up code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2860 r2861  
    3333#include "SampleGenerator.h" 
    3434#include "FrameBufferObject.h" 
     35#include "SsaoShader.h" 
     36#include "DeferredShader.h" 
    3537 
    3638 
     
    4547//-- fbos 
    4648 
    47 GLuint fbo; 
    48 GLuint fbo1; 
    49 GLuint fbo2; 
     49FrameBufferObject *fbo; 
    5050 
    5151bool isFirstTexture = true; 
    52  
    53 ///////////// 
    54 //-- renderbuffers 
    55  
    56  
    57 GLuint depthBuffer; 
    58  
    59 GLuint positionsBuffer; 
    60 GLuint colorsBuffer; 
    61 GLuint colorsBuffer1; 
    62 GLuint colorsBuffer2; 
    63 GLuint normalsBuffer; 
    64  
    65  
    66 ///////////// 
    67 //-- textures 
    68  
    69  
    70 GLuint positionsTex; 
    71 GLuint colorsTex; 
    72 GLuint colorsTex1; 
    73 GLuint colorsTex2; 
    74  
    75 GLuint normalsTex; 
    76  
    77 GLuint noiseTex; 
    7852 
    7953GLuint fontTex; 
     
    177151 
    178152bool useSsao = false; 
     153static float ssaoExpFactor = 0.1f; 
    179154 
    180155bool showAlgorithmTime = false; 
     
    190165bool useFullScreen = false; 
    191166 
    192 // exp factor for ssao 
    193 float expFactor = 0.05f; 
    194  
    195 // ssao number of samples 
    196 //#define NUM_SAMPLES 8 
    197 #define NUM_SAMPLES 16 
    198  
    199 // ssao random spherical samples 
    200 static float samples[NUM_SAMPLES * 2]; 
    201  
    202167static Matrix4x4 matProjectionView = IdentityMatrix(); 
    203168 
    204 FrameBufferObject *myfbo = NULL; 
    205169 
    206170 
     
    241205 
    242206void PlaceViewer(const Vector3 &oldPos); 
    243 void DisplayRenderTexture(); 
    244207 
    245208 
     
    249212void InitFBO(); 
    250213 
    251 void CreateNoiseTex2D(); 
    252  
    253 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 
    254  
    255 void GenerateSamples(); 
    256  
     214SsaoShader *ssaoShader = NULL; 
     215DeferredShader *deferredShader = NULL; 
    257216 
    258217GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 
     
    264223static CGcontext sCgContext = NULL; 
    265224static CGprogram sCgMrtVertexProgram = NULL; 
    266 static CGprogram sCgSsaoProgram = NULL; 
    267 static CGprogram sCgDeferredProgram = NULL; 
    268  
    269 static CGparameter sColorsTexParam; 
    270 static CGparameter sPositionsTexParam; 
    271 static CGparameter sNormalsTexParam; 
    272  
    273 static CGparameter sColorsTexParamSsao; 
    274 static CGparameter sPositionsTexParamSsao; 
    275 static CGparameter sNormalsTexParamSsao; 
    276 static CGparameter sNoiseTexParamSsao; 
    277225 
    278226static CGparameter sModelViewProjMatrixParam; 
    279 static CGparameter sOldModelViewProjMatrixParam; 
    280227static CGparameter sMaxDepthParam; 
    281 static CGparameter sMaxDepthParamSsao; 
    282228static CGparameter sMaxDepthParamTex; 
    283229 
    284 static CGparameter sSamplesParamSsao;  
    285 static CGparameter sOldTexParamSsao; 
    286 static CGparameter sNoiseMultiplierParam; 
    287 static CGparameter sExpFactorParamSsao; 
     230static Matrix4x4 oldViewProjMatrix; 
    288231 
    289232 
     
    296239                printf("%s\n\n", cgGetErrorString(lastError)); 
    297240                printf("%s\n", cgGetLastListing(sCgContext)); 
     241                 
    298242                printf("Cg error, exiting...\n"); 
    299243 
     
    343287 
    344288                env.GetBoolParam(string("useFullScreen"), useFullScreen); 
    345                 env.GetFloatParam(string("expFactor"), expFactor); 
     289                env.GetFloatParam(string("expFactor"), ssaoExpFactor); 
    346290                env.GetVectorParam(string("camPosition"), camPos); 
    347291                env.GetVectorParam(string("camDirection"), camDir); 
     
    360304                cout << "useFullScreen: " << useFullScreen << endl; 
    361305                cout << "camPosition: " << camPos << endl; 
    362                 cout << "expFactor: " << expFactor << endl; 
     306                cout << "expFactor: " << ssaoExpFactor << endl; 
    363307                //cout << "model path: " << model_path << endl; 
    364308        } 
     
    466410        InitCg(); 
    467411 
    468         // create noise texture for ssao 
    469         CreateNoiseTex2D(); 
     412        SsaoShader::Init(sCgContext); 
     413        DeferredShader::Init(sCgContext); 
     414 
     415        ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f); 
     416        deferredShader = new DeferredShader(texWidth, texHeight); 
    470417 
    471418        // initialize the render traverser 
     
    499446        // get the best profile for this hardware 
    500447        RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT); 
    501         //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN); 
    502448        cgGLSetOptimalOptions(RenderState::sCgFragmentProfile); 
    503449 
     
    564510                cerr << "fragment program failed to load" << endl; 
    565511 
    566         PrintGLerror("test"); 
    567  
    568  
    569         /////////////// 
    570  
    571         sCgSsaoProgram =  
    572                 cgCreateProgramFromFile(sCgContext,  
    573                                                                 CG_SOURCE, 
    574                                                                 "src/shaders/deferred.cg",  
    575                                                                 RenderState::sCgFragmentProfile, 
    576                                                                 "main_ssao", 
    577                                                                 NULL); 
    578  
    579         if (sCgSsaoProgram != NULL) 
    580         { 
    581                 cgGLLoadProgram(sCgSsaoProgram); 
    582  
    583                 // we need size of texture for scaling 
    584                 sPositionsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "positions");   
    585                 sColorsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "colors");   
    586                 sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");   
    587                 sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture"); 
    588                 sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 
    589                 sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj"); 
    590                 sMaxDepthParamSsao = cgGetNamedParameter(sCgSsaoProgram, "maxDepth"); 
    591                 sExpFactorParamSsao = cgGetNamedParameter(sCgSsaoProgram, "expFactor"); 
    592  
    593                 cgGLSetParameter1f(sMaxDepthParamSsao, myfar / 10.0f); 
    594                 cgGLSetParameter1f(sExpFactorParamSsao, expFactor); 
    595  
    596                 sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples"); 
    597                 sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex");   
    598  
    599                 GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples); 
    600         } 
    601         else 
    602                 cerr << "ssao program failed to load" << endl; 
    603  
    604          
    605         sCgDeferredProgram =  
    606                 cgCreateProgramFromFile(sCgContext,  
    607                                                                 CG_SOURCE, 
    608                                                                 "src/shaders/deferred.cg",  
    609                                                                 RenderState::sCgFragmentProfile, 
    610                                                                 "main", 
    611                                                                 NULL); 
    612  
    613         if (sCgDeferredProgram != NULL) 
    614         { 
    615                 cgGLLoadProgram(sCgDeferredProgram); 
    616  
    617                 // we need size of texture for scaling 
    618                 sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");   
    619                 sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");   
    620                 sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");  
    621         } 
    622         else 
    623                 cerr << "deferred program failed to load" << endl; 
    624  
    625  
    626512        PrintGLerror("init"); 
    627513 
    628514        cout << "cg initialization successful" << endl; 
    629 } 
    630  
    631  
    632 void PrintFBOStatus(GLenum status) 
    633 { 
    634         switch(status) 
    635         { 
    636         case GL_FRAMEBUFFER_COMPLETE_EXT: 
    637                 cout << "frame buffer object created successfully" << endl; 
    638                 break; 
    639         case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: 
    640                 cerr << "incomplete attachment" << endl; 
    641                 break; 
    642         case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: 
    643                 cerr << "missing attachment" << endl; 
    644                 break; 
    645         case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: 
    646                 cerr << "incomplete dimensions" << endl; 
    647                 break; 
    648         case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: 
    649                 cerr << "incomplete formats" << endl; 
    650                 break; 
    651         case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: 
    652                 cerr << "incomplete draw buffer" << endl; 
    653                 break; 
    654         case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: 
    655                 cerr << "incomplete read buffer" << endl; 
    656                 break; 
    657         case GL_FRAMEBUFFER_UNSUPPORTED_EXT: 
    658                 cerr << "framebuffer unsupported" << endl; 
    659                 break; 
    660         default: 
    661                 cerr << "unknown status code " << status << endl; 
    662         } 
    663515} 
    664516 
     
    668520        // this fbo basicly stores the scene information we get from standard rendering of a frame 
    669521        // we store colors, normals, positions (for the ssao) 
    670         fbo = new FrameBufferObject(texWidth, texHeight, true, FrameBufferObject::DEPTH_24); 
    671  
     522        fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24); 
    672523 
    673524        // the diffuse color buffer 
    674525        fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 
    675  
    676526        // the positions buffer 
    677527        fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false); 
    678  
    679528        // the normals buffer 
    680529        fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false); 
    681  
    682  
    683  
    684         //////////////////////// 
    685  
    686         glGenFramebuffersEXT(1, &fbo); 
    687         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 
    688  
    689          
    690         //////////// 
    691         //-- colors buffer 
    692  
    693         glGenRenderbuffersEXT(1, &colorsBuffer); 
    694         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer); 
    695          
    696 #if 1 
    697         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight); 
    698 #else 
    699         int samples = 8; 
    700         glEnable(GL_MULTISAMPLE_ARB); 
    701         glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_RGBA8, texWidth, texHeight); 
    702 #endif 
    703  
    704         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer); 
    705          
    706         glGenTextures(1, &colorsTex); 
    707         glBindTexture(GL_TEXTURE_2D, colorsTex); 
    708  
    709         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
    710         //glGenerateMipmapEXT(GL_TEXTURE_2D); 
    711         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0); 
    712  
    713         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    714         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    715  
    716         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    717         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    718  
    719  
    720         ////////// 
    721         //-- positions buffer 
    722  
    723         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    724         glGenRenderbuffersEXT(1, &positionsBuffer); 
    725         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, positionsBuffer); 
    726         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight); 
    727  
    728         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_RENDERBUFFER_EXT, positionsBuffer); 
    729  
    730         glGenTextures(1, &positionsTex); 
    731         glBindTexture(GL_TEXTURE_2D, positionsTex); 
    732         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
    733         //glGenerateMipmapEXT(GL_TEXTURE_2D); 
    734         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, positionsTex, 0); 
    735  
    736         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    737         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    738  
    739         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    740         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    741  
    742         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    743          
    744          
    745         //////// 
    746         //-- normals buffer 
    747  
    748         glGenRenderbuffersEXT(1, &normalsBuffer); 
    749         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, normalsBuffer); 
    750         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, texWidth, texHeight); 
    751          
    752         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, normalsBuffer); 
    753  
    754         glGenTextures(1, &normalsTex); 
    755         glBindTexture(GL_TEXTURE_2D, normalsTex); 
    756         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
    757         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, normalsTex, 0); 
    758  
    759         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    760         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    761  
    762         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    763         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    764  
    765         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    766  
    767  
    768         /////////// 
    769         //-- create depth buffer 
    770  
    771         glGenRenderbuffersEXT(1, &depthBuffer);  
    772         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer); 
    773         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight); 
    774         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer); 
    775  
    776          
    777         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    778          
    779          
    780  
    781         ////////////////////////////////// 
    782  
    783  
    784  
    785         glGenFramebuffersEXT(1, &fbo1); 
    786         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1); 
    787  
    788          
    789         //////////// 
    790         //-- colors buffer 
    791  
    792         glGenRenderbuffersEXT(1, &colorsBuffer1); 
    793         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer1); 
    794          
    795         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight); 
    796         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer1); 
    797          
    798         glGenTextures(1, &colorsTex1); 
    799         glBindTexture(GL_TEXTURE_2D, colorsTex1); 
    800  
    801         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
    802         //glGenerateMipmapEXT(GL_TEXTURE_2D); 
    803         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex1, 0); 
    804  
    805         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    806         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
    807  
    808         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    809         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    810  
    811         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    812         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    813  
    814         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    815  
    816  
    817  
    818         ///////////////////////// 
    819  
    820         glGenFramebuffersEXT(1, &fbo2); 
    821         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 
    822  
    823          
    824         //////////// 
    825         //-- colors buffer 
    826  
    827         glGenRenderbuffersEXT(1, &colorsBuffer2); 
    828         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer2); 
    829          
    830         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight); 
    831         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer2); 
    832          
    833         glGenTextures(1, &colorsTex2); 
    834         glBindTexture(GL_TEXTURE_2D, colorsTex2); 
    835  
    836         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 
    837         //glGenerateMipmapEXT(GL_TEXTURE_2D); 
    838         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex2, 0); 
    839  
    840         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    841         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
    842  
    843         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    844         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    845  
    846         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    847         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    848  
    849         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    850         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    851  
    852         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    853  
    854         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
    855530 
    856531        PrintGLerror("fbo"); 
     
    1073748        Matrix4x4 matViewing, matProjection; 
    1074749 
    1075         if (renderType == RenderState::DEFERRED) 
    1076         { 
    1077                 cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x); 
    1078         } 
     750        // store matrix of last frame 
     751        oldViewProjMatrix = matProjectionView; 
    1079752 
    1080753        glMatrixMode(GL_PROJECTION); 
     
    1170843                //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 
    1171844 
    1172                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
     845                fbo->Release(); 
    1173846 
    1174847                state.SetRenderType(RenderState::FIXED); 
     
    1189862                //glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB); 
    1190863 
    1191                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
     864                fbo->Release(); 
    1192865 
    1193866                cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     
    1215888                state.SetRenderType(RenderState::DEFERRED); 
    1216889 
    1217                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 
     890                fbo->Bind(); 
    1218891         
    1219892                glPushAttrib(GL_VIEWPORT_BIT); 
     
    1226899                cgGLBindProgram(sCgMrtVertexProgram); 
    1227900 
     901                /// draw to 3 color buffers 
    1228902                glDrawBuffers(3, mrt); 
    1229903 
     
    1240914                 
    1241915 
     916        // reset lod levels for current frame 
    1242917        LODLevel::InitFrame(camera->GetPosition()); 
    1243918 
     
    1279954        if (renderType == RenderState::DEFERRED)  
    1280955        { 
    1281                 //glPopAttrib(); 
    1282                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
     956                fbo->Release(); 
    1283957 
    1284958                cgGLDisableProfile(RenderState::sCgVertexProfile); 
     
    1287961                glDrawBuffers(1, mrt); 
    1288962 
    1289                 DisplayRenderTexture(); 
    1290         } 
    1291  
     963                if (useSsao) 
     964                        ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor); 
     965                else 
     966                        deferredShader->Render(fbo); 
     967        } 
    1292968         
    1293969        /////////// 
     
    13791055                break; 
    13801056        case '7': 
    1381                 expFactor *= 0.5f; 
     1057                ssaoExpFactor *= 0.5f; 
    13821058                break; 
    13831059        case '8': 
    1384                 expFactor *= 2.0f; 
    1385                 if (expFactor > 1.0f) expFactor = 1.0f; 
     1060                ssaoExpFactor *= 2.0f; 
     1061                if (ssaoExpFactor > 1.0f) ssaoExpFactor = 1.0f; 
    13861062                break; 
    13871063        case 'o': 
     
    18191495        if (RenderState::sCgMrtFragmentTexProgram) 
    18201496                cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram); 
    1821         if (sCgSsaoProgram) 
    1822                 cgDestroyProgram(sCgSsaoProgram); 
    1823         if (sCgDeferredProgram) 
    1824                 cgDestroyProgram(sCgDeferredProgram); 
     1497         
    18251498        if (sCgContext) 
    18261499                cgDestroyContext(sCgContext); 
    1827  
    1828         glDeleteFramebuffersEXT(1, &fbo); 
    1829         glDeleteRenderbuffersEXT(1, &depthBuffer); 
    1830         glDeleteRenderbuffersEXT(1, &colorsBuffer); 
    1831         glDeleteRenderbuffersEXT(1, &normalsBuffer); 
    1832         glDeleteRenderbuffersEXT(1, &positionsBuffer); 
    1833  
    1834         glDeleteTextures(1, &colorsTex); 
    1835         glDeleteTextures(1, &normalsTex); 
    1836         glDeleteTextures(1, &positionsTex); 
    1837         glDeleteTextures(1, &noiseTex); 
    1838         glDeleteTextures(1, &fontTex); 
    18391500} 
    18401501 
     
    20651726        } 
    20661727} 
    2067  
    2068  
    2069 void DisplayRenderTexture3() 
    2070 { 
    2071         glEnable(GL_TEXTURE_2D); 
    2072  
    2073         if (isFirstTexture) 
    2074                 glBindTexture(GL_TEXTURE_2D, colorsTex1); 
    2075         else 
    2076                 glBindTexture(GL_TEXTURE_2D, colorsTex2); 
    2077  
    2078         glDisable(GL_LIGHTING); 
    2079          
    2080         glMatrixMode(GL_PROJECTION); 
    2081         glPushMatrix(); 
    2082         glLoadIdentity(); 
    2083  
    2084         glMatrixMode(GL_MODELVIEW); 
    2085         glPushMatrix(); 
    2086         glLoadIdentity(); 
    2087  
    2088         const float offs = 0.5f; 
    2089         glOrtho(-offs, offs, -offs, offs, 0, 1); 
    2090          
    2091         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    2092  
    2093         glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 
    2094         glBegin(GL_QUADS); 
    2095  
    2096         glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 
    2097         glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 
    2098         glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f); 
    2099         glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f); 
    2100  
    2101         glEnd(); 
    2102  
    2103         glEnable(GL_LIGHTING); 
    2104         glDisable(GL_TEXTURE_2D); 
    2105          
    2106         glMatrixMode(GL_PROJECTION); 
    2107         glPopMatrix(); 
    2108  
    2109         glMatrixMode(GL_MODELVIEW); 
    2110         glPopMatrix(); 
    2111  
    2112         PrintGLerror("displaytexture3"); 
    2113 } 
    2114  
    2115  
    2116 void DisplayRenderTexture() 
    2117 { 
    2118         GLuint oldTex; 
    2119  
    2120         if (isFirstTexture) 
    2121         { 
    2122                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1); 
    2123                 oldTex = colorsTex2; 
    2124         } 
    2125         else  
    2126         { 
    2127                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 
    2128                 oldTex = colorsTex1; 
    2129         } 
    2130  
    2131         glPushAttrib(GL_VIEWPORT_BIT); 
    2132         glViewport(0, 0, texWidth, texHeight); 
    2133  
    2134         glDrawBuffers(1, mrt); 
    2135  
    2136         glDisable(GL_ALPHA_TEST); 
    2137         glDisable(GL_TEXTURE_2D); 
    2138         glDisable(GL_LIGHTING); 
    2139          
    2140         glMatrixMode(GL_PROJECTION); 
    2141         glPushMatrix(); 
    2142         glLoadIdentity(); 
    2143  
    2144         glMatrixMode(GL_MODELVIEW); 
    2145         glPushMatrix(); 
    2146         glLoadIdentity(); 
    2147  
    2148         const float offs = 0.5f; 
    2149          
    2150         glOrtho(-offs, offs, -offs, offs, 0, 1); 
    2151          
    2152         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    2153  
    2154         cgGLEnableProfile(RenderState::sCgFragmentProfile); 
    2155  
    2156         if (useSsao) 
    2157         { 
    2158                 cgGLBindProgram(sCgSsaoProgram); 
    2159                  
    2160                 cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex); 
    2161                 cgGLEnableTextureParameter(sPositionsTexParamSsao); 
    2162  
    2163                 cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex); 
    2164                 cgGLEnableTextureParameter(sColorsTexParamSsao); 
    2165  
    2166                 cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex); 
    2167                 cgGLEnableTextureParameter(sNormalsTexParamSsao); 
    2168  
    2169                 cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex); 
    2170                 cgGLEnableTextureParameter(sNoiseTexParamSsao); 
    2171  
    2172                 cgGLSetTextureParameter(sOldTexParamSsao, oldTex); 
    2173                 cgGLEnableTextureParameter(sOldTexParamSsao); 
    2174  
    2175                 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 
    2176                 cgGLSetParameter1f(sExpFactorParamSsao, expFactor); 
    2177  
    2178                 GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples); 
    2179         } 
    2180         else 
    2181         { 
    2182                 cgGLBindProgram(sCgDeferredProgram); 
    2183  
    2184                 cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 
    2185                 cgGLEnableTextureParameter(sPositionsTexParam); 
    2186  
    2187                 cgGLSetTextureParameter(sColorsTexParam, colorsTex); 
    2188                 cgGLEnableTextureParameter(sColorsTexParam); 
    2189  
    2190                 cgGLSetTextureParameter(sNormalsTexParam, normalsTex); 
    2191                 cgGLEnableTextureParameter(sNormalsTexParam); 
    2192         } 
    2193  
    2194         Vector3 tl, tr, bl, br; 
    2195         ComputeViewVectors(tl, tr, bl, br); 
    2196  
    2197         glColor3f(1.0f, 1.0f, 1.0f); 
    2198  
    2199         glBegin(GL_QUADS); 
    2200  
    2201         // note: slightly larger texture hides ambient occlusion error on border but costs resolution 
    2202         //float offs2 = 0.55f; 
    2203         float offs2 = 0.5f; 
    2204  
    2205         glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f); 
    2206         glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f); 
    2207         glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f); 
    2208         glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f); 
    2209  
    2210         glEnd(); 
    2211  
    2212          
    2213         if (useSsao) 
    2214         { 
    2215                 cgGLDisableTextureParameter(sColorsTexParamSsao); 
    2216                 cgGLDisableTextureParameter(sPositionsTexParamSsao); 
    2217                 cgGLDisableTextureParameter(sNormalsTexParamSsao); 
    2218                 cgGLDisableTextureParameter(sNoiseTexParamSsao); 
    2219                 cgGLDisableTextureParameter(sOldTexParamSsao); 
    2220         } 
    2221         else 
    2222         { 
    2223                 cgGLDisableTextureParameter(sColorsTexParam); 
    2224                 cgGLDisableTextureParameter(sPositionsTexParam); 
    2225                 cgGLDisableTextureParameter(sNormalsTexParam); 
    2226         } 
    2227  
    2228         cgGLDisableProfile(RenderState::sCgFragmentProfile); 
    2229          
    2230         glEnable(GL_LIGHTING); 
    2231         glDisable(GL_TEXTURE_2D); 
    2232          
    2233         glMatrixMode(GL_PROJECTION); 
    2234         glPopMatrix(); 
    2235  
    2236         glMatrixMode(GL_MODELVIEW); 
    2237         glPopMatrix(); 
    2238  
    2239         glPopAttrib(); 
    2240  
    2241         PrintGLerror("displaytexture"); 
    2242  
    2243         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
    2244  
    2245         DisplayRenderTexture3(); 
    2246 } 
    2247  
    2248 #if 0 
    2249 // kustls magic sample positions 
    2250 void GenerateSamples() 
    2251 { 
    2252         static const float mysamples[] = 
    2253         {  
    2254                 -0.326212f, -0.405805f, 
    2255                 -0.840144f, -0.07358f, 
    2256                 -0.695914f, 0.457137f, 
    2257                 -0.203345f, 0.620716, 
    2258                 0.96234f, -0.194983f, 
    2259                 0.473434f, -0.480026f, 
    2260                 0.519456, 0.767022f, 
    2261                 0.185461f, -0.893124f, 
    2262                 0.507431f, 0.064425f, 
    2263                 0.89642f, 0.412458f, 
    2264                 -0.32194f, -0.932615f, 
    2265                 -0.791559f, -0.597705f, 
    2266                 0.326212f, 0.405805f, 
    2267                 0.840144f, 0.07358f, 
    2268                 0.695914f, -0.457137f, 
    2269                 0.203345f, -0.620716, 
    2270                 -0.96234f, 0.194983f, 
    2271                 -0.473434f, 0.480026f, 
    2272                 -0.519456, -0.767022f, 
    2273                 -0.185461f, 0.893124f, 
    2274                 -0.507431f, -0.064425f, 
    2275                 -0.89642f, -0.412458f, 
    2276                 0.32194f, 0.932615f, 
    2277                 0.791559f, 0.597705f 
    2278         }; 
    2279  
    2280         for (int i = 0; i < NUM_SAMPLES; ++ i) 
    2281         { 
    2282                 samples[i] = mysamples[i]; 
    2283         } 
    2284 } 
    2285  
    2286 #else 
    2287  
    2288  
    2289 void GenerateSamples() 
    2290 { 
    2291         /*static HaltonSequence halton; 
    2292  
    2293         float r[2]; 
    2294  
    2295         // generates poisson distribution on disc 
    2296         float minDist = 2.0f / sqrt((float)NUM_SAMPLES); 
    2297  
    2298         //cout << "minDist before= " << minDist << endl; 
    2299  
    2300         for (int i = 0; i < NUM_SAMPLES * 2; i += 2) 
    2301         { 
    2302                 int tries = 0, totalTries = 0; 
    2303  
    2304                 // repeat until valid sample was found 
    2305                 while (1) 
    2306                 { 
    2307                         ++ tries; 
    2308                         ++ totalTries; 
    2309  
    2310                         halton.GetNext(2, r); 
    2311  
    2312                         //const float rx = RandomValue(-1, 1); 
    2313                         //const float ry = RandomValue(-1, 1); 
    2314  
    2315                         const float rx = r[0] * 2.0f - 1.0f; 
    2316                         const float ry = r[1] * 2.0f - 1.0f; 
    2317  
    2318                         // check if in disk, else exit early 
    2319                         if (rx * rx + ry * ry > 1) 
    2320                                 continue; 
    2321  
    2322                         bool sampleValid = true; 
    2323  
    2324                         // check poisson property 
    2325                         for (int j = 0; ((j < i) && sampleValid); j += 2) 
    2326                         { 
    2327                                 const float dist =  
    2328                                         sqrt((samples[j] - rx) * (samples[j] - rx) + 
    2329                                              (samples[j + 1] - ry) * (samples[j + 1] - ry)); 
    2330                          
    2331                                 if (dist < minDist) 
    2332                                         sampleValid = false; 
    2333                         } 
    2334  
    2335                         if (sampleValid) 
    2336                         { 
    2337                                 samples[i] = rx; 
    2338                                 samples[i + 1]= ry; 
    2339                                 break; 
    2340                         } 
    2341  
    2342                         if (tries > 2000) 
    2343                         { 
    2344                                 minDist *= 0.9f; 
    2345                                 tries = 0; 
    2346                         } 
    2347                 } 
    2348         } 
    2349 */ 
    2350  
    2351         static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f); 
    2352         poisson.Generate((Sample2 *)samples); 
    2353 } 
    2354  
    2355  
    2356 #endif 
    2357  
    2358  
    2359 void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br) 
    2360 { 
    2361         float myfov = fov * M_PI / 180.0f; 
    2362  
    2363         const float w_far = 2.0f * tan(myfov / 2.0f); 
    2364         const float aspect = texWidth / texHeight; 
    2365  
    2366         const float h_far = w_far / aspect; 
    2367  
    2368         float t1 = h_far * 0.5f; 
    2369         float t2 = w_far * 0.5f; 
    2370  
    2371 #if 0 
    2372         bl = Normalize(-Vector3(-t1, -t2, 1.0f)); 
    2373         br = Normalize(-Vector3( t1, -t2, 1.0f)); 
    2374         tl = Normalize(-Vector3(-t1,  t2, 1.0f)); 
    2375         tr = Normalize(-Vector3( t1,  t2, 1.0f)); 
    2376 #else 
    2377         bl = -Normalize(camera->GetDirection()); 
    2378         br = -Normalize(camera->GetDirection()); 
    2379         tl = -Normalize(camera->GetDirection()); 
    2380         tr = -Normalize(camera->GetDirection()); 
    2381 #endif 
    2382         // normalize to 0 .. 1 
    2383         bl = bl * 0.5f + 0.5f; 
    2384         br = br * 0.5f + 0.5f; 
    2385         tl = tl * 0.5f + 0.5f; 
    2386         tr = tr * 0.5f + 0.5f; 
    2387 } 
    2388  
    2389  
    2390 void CreateNoiseTex2D() 
    2391 { 
    2392         randomNormals = new GLubyte[texWidth * texHeight * 3]; 
    2393  
    2394         for (int i = 0; i < texWidth * texHeight * 3; i += 3) 
    2395         { 
    2396                 // create random samples over sphere 
    2397                 const float rx = RandomValue(0, 1); 
    2398                 const float theta = 2.0f * acos(sqrt(1.0f - rx)); 
    2399  
    2400                 randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f); 
    2401                 randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f); 
    2402                 randomNormals[i + 2] = 0; 
    2403         } 
    2404  
    2405         glEnable(GL_TEXTURE_2D); 
    2406         glGenTextures(1, &noiseTex); 
    2407         glBindTexture(GL_TEXTURE_2D, noiseTex); 
    2408                  
    2409         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    2410         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    2411         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    2412         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    2413  
    2414         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals); 
    2415         //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texWidth, texHeight, GL_RGB, GL_UNSIGNED_BYTE, randomNormals); 
    2416  
    2417         glBindTexture(GL_TEXTURE_2D, 0); 
    2418         glDisable(GL_TEXTURE_2D); 
    2419  
    2420         cout << "created noise texture" << endl; 
    2421         PrintGLerror("noisetexture"); 
    2422 } 
Note: See TracChangeset for help on using the changeset viewer.