Changeset 2861


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

cleaned up code

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
12 edited

Legend:

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

    r2859 r2861  
    793793                </Filter> 
    794794                <File 
     795                        RelativePath=".\default.env" 
     796                        > 
     797                </File> 
     798                <File 
    795799                        RelativePath=".\ReadMe.txt" 
    796800                        > 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env

    r2846 r2861  
    1818# shader stuff 
    1919 
     20# ssao exponential smoothing factor 
    2021expFactor=0.1f 
    2122#numSsaoSamples=8 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp

    r2859 r2861  
    3535 
    3636 
     37DeferredShader::~DeferredShader() 
     38{ 
     39        if (sCgDeferredProgram) 
     40                cgDestroyProgram(sCgDeferredProgram); 
     41} 
     42 
     43 
    3744void DeferredShader::Init(CGcontext context) 
    3845{ 
     
    6572void DeferredShader::Render(FrameBufferObject *fbo) 
    6673{ 
    67         GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     74        FrameBufferObject::Release(); 
     75 
    6876        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    69         GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     77        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     78        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
    7079 
    7180        glPushAttrib(GL_VIEWPORT_BIT); 
     
    103112         
    104113        glColor3f(1.0f, 1.0f, 1.0f); 
    105  
     114         
    106115        glBegin(GL_QUADS); 
    107116 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h

    r2859 r2861  
    2424        DeferredShader(int w, int h); 
    2525 
     26        ~DeferredShader(); 
    2627        /** The algorithm renders the scene given an fbo. 
    2728                The fbo must have color buffer, position buffer, normal buffer. 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp

    r2859 r2861  
    99{ 
    1010 
     11GLenum color_attachment[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT}; 
    1112 
    1213void PrintFBOStatus(GLenum status) 
     
    1516        { 
    1617        case GL_FRAMEBUFFER_COMPLETE_EXT: 
    17                 cout << "frame buffer object created successfully" << endl; 
     18                cout << "frame buffer object complete" << endl; 
    1819                break; 
    1920        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: 
     
    4546 
    4647ColorBufferObject::ColorBufferObject(int w, int h,  
    47                                                                          FORMAT c,  
     48                                                                         FORMAT format,  
    4849                                                                         WRAP_TYPE wrapType,  
    4950                                                                         FILTER_TYPE filterType,  
    5051                                                                         bool useMipMap,  
    51                                                                          bool useMultiSampling) 
     52                                                                         bool useMultiSampling, 
     53                                                                         int attachment_idx) 
    5254{ 
     55        GLuint glformat, internalFormat; 
     56 
     57 
     58        switch (format) 
     59        { 
     60        case BUFFER_UBYTE: 
     61                glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; break; 
     62        case BUFFER_FLOAT_16: 
     63                glformat = GL_FLOAT; internalFormat = GL_RGBA16F_ARB; break; 
     64        case BUFFER_FLOAT_32:  
     65                glformat = GL_FLOAT; internalFormat = GL_RGBA32F_ARB; break; 
     66        default: 
     67                glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; 
     68                cerr << "should not come here" << endl; 
     69        } 
     70 
    5371        glGenRenderbuffersEXT(1, &mId); 
    5472        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId); 
    55         glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, w, h); 
     73        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h); 
    5674         
    57         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, mId); 
     75        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId); 
    5876 
    5977        glGenTextures(1, &mTexId); 
    6078        glBindTexture(GL_TEXTURE_2D, mTexId); 
    61         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
    62         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, mTexId, 0); 
     79        //glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL); 
     80        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL); 
     81        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0); 
    6382 
    6483        GLuint filterParam; 
     
    7291        case FILTER_MIPMAP_LINEAR:  
    7392                filterParam = GL_LINEAR_MIPMAP_LINEAR; break; 
     93        default: 
     94                filterParam = GL_NEAREST; 
     95                cerr << "should not come here" << endl; 
    7496        } 
    7597 
     
    96118 
    97119 
    98 FrameBufferObject::FrameBufferObject(int w, int h, bool useDepth, DEPTH_FORMAT d) 
     120FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d) 
    99121: mWidth(w), mHeight(h) 
    100122{ 
     
    102124        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId); 
    103125 
    104          
     126        cout << "creating fbo" << endl; 
     127 
    105128        /////////// 
    106129        //-- create depth buffer 
    107130 
    108         if (useDepth) 
     131        if (d != DEPTH_NONE) 
    109132        { 
    110133                glGenRenderbuffersEXT(1, &mDepthId);     
    111134                glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId); 
     135 
     136                cout << "adding depth buffer" << endl; 
    112137 
    113138                GLuint depthFormat; 
     
    121146                case DEPTH_32:  
    122147                        depthFormat = GL_DEPTH_COMPONENT32; break; 
     148                default:         
     149                        cerr << "should not come here" << endl; 
    123150                } 
    124151 
     
    126153                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId); 
    127154        } 
    128  
    129         // print status 
    130         PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 
    131155} 
    132156 
     
    138162                                                                          bool useMultiSampling) 
    139163{ 
     164        cout << "adding color buffer" << endl; 
     165 
     166        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId); 
     167 
     168        int idx = (int)mColorBuffers.size(); 
    140169        ColorBufferObject *colorBuf =  
    141                 new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, useMultiSampling); 
     170                new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, useMultiSampling, idx); 
     171 
    142172        mColorBuffers.push_back(colorBuf); 
    143173 
    144         return (int)mColorBuffers.size() - 1; 
     174        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
     175 
     176        return idx; 
     177} 
     178 
     179 
     180void FrameBufferObject::Bind() const 
     181{ 
     182        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId); 
     183} 
     184 
     185 
     186void FrameBufferObject::Release() 
     187{ 
     188        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
    145189} 
    146190 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h

    r2859 r2861  
    2525                                          FILTER_TYPE filterType,  
    2626                                          bool useMipMap,  
    27                                           bool useMultiSampling); 
     27                                          bool useMultiSampling, 
     28                                          int attachment_idx); 
    2829 
    2930        unsigned int GetTexture() const {return mTexId;} 
     
    4243public: 
    4344 
    44         enum DEPTH_FORMAT { DEPTH_16, DEPTH_24, DEPTH_32 }; 
     45        enum DEPTH_FORMAT { DEPTH_NONE, DEPTH_16, DEPTH_24, DEPTH_32 }; 
    4546 
    4647        /** constructor requesting an opengl occlusion query. 
    4748        */ 
    48         FrameBufferObject(int w, int h, bool useDepth, DEPTH_FORMAT d); 
     49        FrameBufferObject(int w, int h, DEPTH_FORMAT d); 
    4950        /** Creates and adds a color buffer to the current frame buffer object. 
    5051                Returns the index that allows to retrieve the color buffer object. 
     
    5960        */ 
    6061        ColorBufferObject *GetColorBuffer(int i) const { return mColorBuffers[i]; } 
    61  
     62        /** Binds this frame buffer object. 
     63        */ 
     64        void Bind() const; 
     65        /** Releases any bound frame buffer object. 
     66        */ 
     67        static void Release(); 
    6268 
    6369protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp

    r2851 r2861  
    33#include "Material.h" 
    44 
     5using namespace std; 
    56 
    67namespace CHCDemoEngine  
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp

    r2853 r2861  
    6969                str.read(reinterpret_cast<char *>(&dist), sizeof(float)); 
    7070 
     71                if (i>=3) dist=2e20; 
    7172                int numShapes; 
    7273                str.read(reinterpret_cast<char *>(&numShapes), sizeof(int)); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp

    r2860 r2861  
    6161SsaoShader::SsaoShader(int w, int h,  
    6262                                           Camera *cam, 
    63                                            float expFactor,  
    6463                                           float scaleFactor 
    6564                                           ): 
    6665mWidth(w), mHeight(h),  
    6766mCamera(cam), 
    68 mExpFactor(expFactor),  
    6967mScaleFactor(scaleFactor) 
    70 {} 
     68{ 
     69         
     70        /////////// 
     71        //-- the flip-flop fbos 
     72 
     73        mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     74        // the diffuse color buffer 
     75        mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 
     76         
     77        mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 
     78        // the diffuse color buffer 
     79        mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 
     80         
     81 
     82        // create noise texture for ssao 
     83        CreateNoiseTex2D(); 
     84} 
     85 
     86 
     87SsaoShader::~SsaoShader()  
     88{ 
     89        if (sCgSsaoProgram) 
     90                cgDestroyProgram(sCgSsaoProgram); 
     91 
     92        DEL_PTR(mNewFbo); 
     93        DEL_PTR(mOldFbo); 
     94 
     95        glDeleteTextures(1, &noiseTex); 
     96} 
    7197 
    7298 
    7399void SsaoShader::Init(CGcontext context) 
    74 { 
    75          
     100{        
    76101        /////////////// 
    77102 
     
    112137 
    113138 
    114 void SsaoShader::Render(FrameBufferObject *fbo, FrameBufferObject *fbo2) 
    115 { 
    116         FirstPass(fbo, fbo2); 
    117         SecondPass(fbo, fbo2); 
    118 } 
    119  
    120  
    121 void SsaoShader::FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 
    122 { 
    123         GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     139void SsaoShader::Render(FrameBufferObject *fbo,  
     140                                                const Matrix4x4 &oldProjViewMatrix, 
     141                                                float expFactor) 
     142{ 
     143        cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x); 
     144 
     145        // switch roles of old and new fbo 
     146        // the algorihm uses two input fbos, where the one 
     147        // contais the color buffer from the last frame,  
     148        // the other one will be written 
     149        swap(mNewFbo, mOldFbo);  
     150 
     151        FirstPass(fbo, expFactor); 
     152        // the second pass just renders the combined solution 
     153        SecondPass(fbo); 
     154} 
     155 
     156 
     157void SsaoShader::FirstPass(FrameBufferObject *fbo, float expFactor) 
     158{ 
    124159        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    125         GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    126  
    127         GLuint oldTex = fbo2->GetColorBuffer(0)->GetTexture(); 
     160        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 
     161        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 
     162 
     163        // read the second buffer, write to the first buffer 
     164        mNewFbo->Bind(); 
     165        GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture(); 
    128166 
    129167        glPushAttrib(GL_VIEWPORT_BIT); 
     
    172210         
    173211        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor); 
    174         cgGLSetParameter1f(sExpFactorParam, mExpFactor); 
     212        cgGLSetParameter1f(sExpFactorParam, expFactor); 
    175213 
    176214 
     
    215253        glPopAttrib(); 
    216254 
    217         PrintGLerror("displaytexture"); 
    218  
    219         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
    220  
     255        FrameBufferObject::Release(); 
    221256 
    222257        PrintGLerror("ssao first pass"); 
     
    224259 
    225260 
    226 void SsaoShader::SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 
     261void SsaoShader::SecondPass(FrameBufferObject *fbo) 
    227262{ 
    228263        glEnable(GL_TEXTURE_2D); 
    229264 
    230         glBindTexture(GL_TEXTURE_2D, fbo2->GetColorBuffer(0)->GetTexture()); 
     265        glBindTexture(GL_TEXTURE_2D, mNewFbo->GetColorBuffer(0)->GetTexture()); 
    231266 
    232267        glDisable(GL_LIGHTING); 
     
    274309        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
    275310 
    276 #if 1 // matT: debug this!! 
     311#if 0 // matT: debug this!! 
    277312         
    278313        bl = Normalize(nbl - fbl); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h

    r2860 r2861  
    1414class Vector3; 
    1515class Camera; 
    16  
     16class Matrix4x4; 
    1717 
    1818/** This class implements a deferred shading algorithm that takes 
     
    2323public: 
    2424        /** constructor for a deferred shader taking the requested output image size, 
    25                 the current camera, the exponential smoothing factor for temporal reprojection,  
    26                 and a scaling factor. 
     25                the current camera,     and a scaling factor. 
    2726                         
    2827                The parameter scaleFactor must be reciprocal value of the  
     
    3029                exact scene size that was scaled in order to improve floating point precision. 
    3130        */ 
    32         SsaoShader(int w, int h, Camera *cam, float expFactor, float scaleFactor); 
    33         /** The algorithm renders the scene given an fbo. 
    34                 The fbo must have color buffer, position buffer, normal buffer. 
     31        SsaoShader(int w, int h, Camera *cam, float scaleFactor); 
     32        /**  
     33         
     34                The algorithm renders the scene given an fbo consists of 1 color buffer,  
     35                1 position buffer, and 1 normal buffer. 
     36                We also need the  projection view matrix of the last frame for reprojection, and 
     37                a smoothing factor for temporal coherence 
    3538        */ 
    36         void Render(FrameBufferObject *fbo, FrameBufferObject *fbo2); 
     39        void Render(FrameBufferObject *fbo, const Matrix4x4 &oldProjViewMatrix, float expFactor); 
    3740 
    3841        /** Initialises the deferred shader and loads the required shaders: 
     
    4144        static void Init(CGcontext context); 
    4245 
     46        ~SsaoShader(); 
    4347 
    4448protected: 
    4549 
    46         void FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 
     50        void FirstPass(FrameBufferObject *fbo, float expFactor); 
    4751 
    48         void SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 
     52        void SecondPass(FrameBufferObject *fbo); 
     53 
     54        void CreateNoiseTex2D(); 
     55        void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 
    4956 
    5057 
     
    5461        int mHeight; 
    5562 
    56         /// the temporal smoothing factor 
    57         float mExpFactor; 
    5863        /// this is just a scale factor for the scene depth in order to get better float precision in the shader 
    5964        float mScaleFactor; 
     
    6166        Camera *mCamera; 
    6267 
    63  
    64 private: 
    65  
    66         void CreateNoiseTex2D(); 
    67         void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); 
    68  
     68        FrameBufferObject *mOldFbo; 
     69        FrameBufferObject *mNewFbo; 
    6970}; 
    7071 
  • 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 } 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg

    r2854 r2861  
    289289 
    290290        float4 col = shade(IN, colors, positions, normal.xyz, amb); 
     291         
    291292        OUT.color = col; 
    292293 
Note: See TracChangeset for help on using the changeset viewer.