Changeset 2859


Ignore:
Timestamp:
08/21/08 16:31:03 (16 years ago)
Author:
mattausch
Message:
 
Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
8 edited

Legend:

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

    r2857 r2859  
    316316                        </File> 
    317317                        <File 
     318                                RelativePath=".\src\SsaoShader.cpp" 
     319                                > 
     320                        </File> 
     321                        <File 
     322                                RelativePath=".\src\SsaoShader.h" 
     323                                > 
     324                        </File> 
     325                        <File 
    318326                                RelativePath=".\src\Triangle3.cpp" 
    319327                                > 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp

    r2858 r2859  
    11#include "DeferredShader.h" 
    2 #include "glInterface.h" 
     2#include "FrameBufferObject.h" 
     3#include "RenderState.h" 
    34 
    45 
     
    910{ 
    1011 
     12 
     13static void PrintGLerror(char *msg) 
     14{ 
     15        GLenum errCode; 
     16        const GLubyte *errStr; 
     17         
     18        if ((errCode = glGetError()) != GL_NO_ERROR)  
     19        { 
     20                errStr = gluErrorString(errCode); 
     21                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); 
     22        } 
     23} 
     24 
     25 
     26static CGprogram sCgDeferredProgram; 
     27static CGparameter sColorsTexParam; 
     28static CGparameter sPositionsTexParam; 
     29static CGparameter sNormalsTexParam; 
     30 
     31 
     32DeferredShader::DeferredShader(int w, int h): 
     33mWidth(w), mHeight(h) 
     34{} 
     35 
     36 
     37void DeferredShader::Init(CGcontext context) 
     38{ 
     39         
     40        sCgDeferredProgram =  
     41                cgCreateProgramFromFile(context,  
     42                                                                CG_SOURCE, 
     43                                                                "src/shaders/deferred.cg",  
     44                                                                RenderState::sCgFragmentProfile, 
     45                                                                "main", 
     46                                                                NULL); 
     47 
     48        if (sCgDeferredProgram != NULL) 
     49        { 
     50                cgGLLoadProgram(sCgDeferredProgram); 
     51 
     52                // we need size of texture for scaling 
     53                sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");   
     54                sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");   
     55                sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");  
     56        } 
     57        else 
     58                cerr << "deferred program failed to load" << endl; 
     59 
     60 
     61        PrintGLerror("init"); 
     62} 
     63 
     64 
    1165void DeferredShader::Render(FrameBufferObject *fbo) 
    1266{ 
     67        GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     68        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     69        GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     70 
    1371        glPushAttrib(GL_VIEWPORT_BIT); 
    14         glViewport(0, 0, fbo->GetWidth(), fbo->GetHeight()); 
     72        glViewport(0, 0, mWidth, mHeight); 
    1573 
    1674        glDisable(GL_ALPHA_TEST); 
     
    3593        cgGLBindProgram(sCgDeferredProgram); 
    3694 
     95        cgGLSetTextureParameter(sColorsTexParam, colorsTex); 
     96        cgGLEnableTextureParameter(sColorsTexParam); 
     97 
    3798        cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 
    3899        cgGLEnableTextureParameter(sPositionsTexParam); 
    39  
    40         cgGLSetTextureParameter(sColorsTexParam, colorsTex); 
    41         cgGLEnableTextureParameter(sColorsTexParam); 
    42100 
    43101        cgGLSetTextureParameter(sNormalsTexParam, normalsTex); 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h

    r2858 r2859  
    33 
    44#include "common.h" 
     5#include "glInterface.h" 
     6#include <Cg/cg.h> 
     7#include <Cg/cgGL.h> 
     8 
    59 
    610namespace CHCDemoEngine  
     
    913class FrameBufferObject; 
    1014 
    11 /** This class implements a deferred shading algorithm. 
     15 
     16/** This class implements a deferred shading algorithm that takes 
     17        a frame buffer object as input and outputs an image in the given size 
    1218*/ 
    1319class DeferredShader 
    1420{ 
    1521public: 
    16         /** constructor requesting an opengl occlusion query. 
     22        /** constructor for a deferred shader taking the requested output image size 
    1723        */ 
    18         DeferredShader(); 
     24        DeferredShader(int w, int h); 
    1925 
    2026        /** The algorithm renders the scene given an fbo. 
     
    2228        */ 
    2329        void Render(FrameBufferObject *fbo); 
     30 
     31        /** Initialises the deferred shader and loads the required shaders: 
     32                This function has to be called only once. 
     33        */ 
     34        static void Init(CGcontext context); 
     35 
     36 
     37protected: 
     38 
     39        int mWidth; 
     40        int mHeight; 
    2441}; 
    2542 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp

    r2857 r2859  
    132132 
    133133 
    134 ColorBufferObject *FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,  
    135                                                                                                         ColorBufferObject::WRAP_TYPE wrapType,  
    136                                                                                                         ColorBufferObject::FILTER_TYPE filterType,  
    137                                                                                                         bool useMipMap,  
    138                                                                                                         bool useMultiSampling) 
     134int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,  
     135                                                                          ColorBufferObject::WRAP_TYPE wrapType,  
     136                                                                          ColorBufferObject::FILTER_TYPE filterType,  
     137                                                                          bool useMipMap,  
     138                                                                          bool useMultiSampling) 
    139139{ 
    140140        ColorBufferObject *colorBuf =  
     
    142142        mColorBuffers.push_back(colorBuf); 
    143143 
    144         return colorBuf; 
     144        return (int)mColorBuffers.size() - 1; 
    145145} 
    146146 
     147 
    147148} // namespace 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h

    r2857 r2859  
    2727                                          bool useMultiSampling); 
    2828 
     29        unsigned int GetTexture() const {return mTexId;} 
     30 
    2931protected: 
    3032 
     
    4143 
    4244        enum DEPTH_FORMAT { DEPTH_16, DEPTH_24, DEPTH_32 }; 
     45 
    4346        /** constructor requesting an opengl occlusion query. 
    4447        */ 
    4548        FrameBufferObject(int w, int h, bool useDepth, DEPTH_FORMAT d); 
    4649        /** Creates and adds a color buffer to the current frame buffer object. 
    47                 Returns a pointer to the buffer object 
     50                Returns the index that allows to retrieve the color buffer object. 
    4851        */ 
    49         ColorBufferObject *AddColorBuffer(ColorBufferObject::FORMAT col,  
    50                                               ColorBufferObject::WRAP_TYPE wrapType,  
    51                                                                           ColorBufferObject::FILTER_TYPE filterType,  
    52                                                                           bool useMipMap,  
    53                                                                           bool useMultiSampling); 
     52        int AddColorBuffer(ColorBufferObject::FORMAT col,  
     53                               ColorBufferObject::WRAP_TYPE wrapType,  
     54                                           ColorBufferObject::FILTER_TYPE filterType,  
     55                                           bool useMipMap,  
     56                                           bool useMultiSampling); 
     57 
     58        /** Returns the color buffer object on position i. 
     59        */ 
     60        ColorBufferObject *GetColorBuffer(int i) const { return mColorBuffers[i]; } 
     61 
    5462 
    5563protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp

    r2858 r2859  
    1 #include "DeferredShader.h" 
     1#include "SsaoShader.h" 
     2#include "FrameBufferObject.h" 
     3#include "RenderState.h" 
     4#include "SampleGenerator.h" 
     5 
    26 
    37using namespace std; 
    48 
     9GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 
    510 
    611namespace CHCDemoEngine 
    712{ 
    813 
     14static CGprogram sCgSsaoProgram = NULL; 
     15 
     16static CGparameter sColorsTexParam; 
     17static CGparameter sPositionsTexParam; 
     18static CGparameter sNormalsTexParam; 
     19static CGparameter sOldModelViewProjMatrixParam; 
     20static CGparameter sMaxDepthParam; 
     21static CGparameter sSamplesParam;  
     22static CGparameter sOldTexParam; 
     23static CGparameter sNoiseTexParam; 
     24static CGparameter sNoiseMultiplierParam; 
     25static CGparameter sExpFactorParam; 
     26 
     27 
     28#define NUM_SAMPLES 16 
     29 
     30// ssao random spherical samples 
     31static Sample2 samples[NUM_SAMPLES]; 
     32 
     33 
     34static void PrintGLerror(char *msg) 
     35{ 
     36        GLenum errCode; 
     37        const GLubyte *errStr; 
     38         
     39        if ((errCode = glGetError()) != GL_NO_ERROR)  
     40        { 
     41                errStr = gluErrorString(errCode); 
     42                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); 
     43        } 
     44} 
     45 
     46 
     47/** Generate poisson disc distributed sample points on the unit disc 
     48*/ 
     49static void GenerateSamples() 
     50{ 
     51        static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f); 
     52        poisson.Generate((Sample2 *)samples); 
     53} 
     54 
     55 
     56SsaoShader::SsaoShader(int w, int h, float expFactor, float scaleFactor): 
     57mWidth(w), mHeight(h), mExpFactor(expFactor), mScaleFactor(scaleFactor) 
     58{} 
     59 
     60 
     61void SsaoShader::Init(CGcontext context) 
     62{ 
     63         
     64        /////////////// 
     65 
     66        sCgSsaoProgram =  
     67                cgCreateProgramFromFile(context,  
     68                                                                CG_SOURCE, 
     69                                                                "src/shaders/deferred.cg",  
     70                                                                RenderState::sCgFragmentProfile, 
     71                                                                "main_ssao", 
     72                                                                NULL); 
     73 
     74        if (sCgSsaoProgram != NULL) 
     75        { 
     76                cgGLLoadProgram(sCgSsaoProgram); 
     77 
     78                // we need size of texture for scaling 
     79                sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions");   
     80                sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");   
     81                sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals");   
     82                sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture"); 
     83                sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 
     84                sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj"); 
     85                sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth"); 
     86                sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor"); 
     87 
     88                sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples"); 
     89                sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");   
     90 
     91                // generate samples for ssao kernel 
     92                GenerateSamples();  
     93                cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 
     94        } 
     95        else 
     96                cerr << "ssao program failed to load" << endl; 
     97 
     98        PrintGLerror("init"); 
     99} 
     100 
     101 
     102void SsaoShader::Render(FrameBufferObject *fbo, FrameBufferObject *fbo2) 
     103{ 
     104        FirstPass(fbo, fbo2); 
     105        SecondPass(fbo, fbo2); 
     106} 
     107 
     108 
     109void SsaoShader::FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 
     110{ 
     111        GLuint positionsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     112        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     113        GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 
     114 
     115        glPushAttrib(GL_VIEWPORT_BIT); 
     116        glViewport(0, 0, mWidth, mHeight); 
     117 
     118        glDrawBuffers(1, mrt); 
     119 
     120        glDisable(GL_ALPHA_TEST); 
     121        glDisable(GL_TEXTURE_2D); 
     122        glDisable(GL_LIGHTING); 
     123 
     124        glMatrixMode(GL_PROJECTION); 
     125        glPushMatrix(); 
     126        glLoadIdentity(); 
     127 
     128        glMatrixMode(GL_MODELVIEW); 
     129        glPushMatrix(); 
     130        glLoadIdentity(); 
     131 
     132        const float offs = 0.5f; 
     133 
     134        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     135 
     136        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     137 
     138        cgGLEnableProfile(RenderState::sCgFragmentProfile); 
     139 
     140        cgGLBindProgram(sCgSsaoProgram); 
     141 
     142        cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 
     143        cgGLEnableTextureParameter(sPositionsTexParam); 
     144 
     145        cgGLSetTextureParameter(sColorsTexParam, colorsTex); 
     146        cgGLEnableTextureParameter(sColorsTexParam); 
     147 
     148        cgGLSetTextureParameter(sNormalsTexParam, normalsTex); 
     149        cgGLEnableTextureParameter(sNormalsTexParam); 
     150 
     151        cgGLSetTextureParameter(sNoiseTexParam, noiseTex); 
     152        cgGLEnableTextureParameter(sNoiseTexParam); 
     153 
     154        cgGLSetTextureParameter(sOldTexParam, oldTex); 
     155        cgGLEnableTextureParameter(sOldTexParam); 
     156 
     157        cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 
     158         
     159        cgGLSetParameter1f(sMaxDepthParam, mScaleFactor); 
     160        cgGLSetParameter1f(sExpFactorParam, mExpFactor); 
     161 
     162 
     163        GenerateSamples();  
     164        cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples); 
     165 
     166        Vector3 tl, tr, bl, br; 
     167        ComputeViewVectors(tl, tr, bl, br); 
     168 
     169        glColor3f(1.0f, 1.0f, 1.0f); 
     170 
     171        glBegin(GL_QUADS); 
     172 
     173        // note: slightly larger texture hides ambient occlusion error on border but costs resolution 
     174        //float offs2 = 0.55f; 
     175        float offs2 = 0.5f; 
     176 
     177        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f); 
     178        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f); 
     179        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f); 
     180        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f); 
     181 
     182        glEnd(); 
     183 
     184        cgGLDisableTextureParameter(sColorsTexParamSsao); 
     185        cgGLDisableTextureParameter(sPositionsTexParamSsao); 
     186        cgGLDisableTextureParameter(sNormalsTexParamSsao); 
     187        cgGLDisableTextureParameter(sNoiseTexParamSsao); 
     188        cgGLDisableTextureParameter(sOldTexParamSsao); 
     189 
     190        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     191 
     192        glEnable(GL_LIGHTING); 
     193        glDisable(GL_TEXTURE_2D); 
     194 
     195        glMatrixMode(GL_PROJECTION); 
     196        glPopMatrix(); 
     197 
     198        glMatrixMode(GL_MODELVIEW); 
     199        glPopMatrix(); 
     200 
     201        glPopAttrib(); 
     202 
     203        PrintGLerror("displaytexture"); 
     204 
     205        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
     206 
     207 
     208        PrintGLerror("ssao first pass"); 
     209} 
     210 
     211 
     212void SsaoShader::SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2) 
     213{ 
     214        glEnable(GL_TEXTURE_2D); 
     215 
     216        glBindTexture(GL_TEXTURE_2D, fbo2->GetColorBuffer(0)->GetTexture()); 
     217 
     218        glDisable(GL_LIGHTING); 
     219         
     220        glMatrixMode(GL_PROJECTION); 
     221        glPushMatrix(); 
     222        glLoadIdentity(); 
     223 
     224        glMatrixMode(GL_MODELVIEW); 
     225        glPushMatrix(); 
     226        glLoadIdentity(); 
     227 
     228        const float offs = 0.5f; 
     229        glOrtho(-offs, offs, -offs, offs, 0, 1); 
     230         
     231        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     232 
     233        glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 
     234        glBegin(GL_QUADS); 
     235 
     236        glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 
     237        glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 
     238        glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f); 
     239        glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f); 
     240 
     241        glEnd(); 
     242 
     243        glEnable(GL_LIGHTING); 
     244        glDisable(GL_TEXTURE_2D); 
     245         
     246        glMatrixMode(GL_PROJECTION); 
     247        glPopMatrix(); 
     248 
     249        glMatrixMode(GL_MODELVIEW); 
     250        glPopMatrix(); 
     251 
     252        PrintGLerror("ssao second pass"); 
     253} 
     254 
     255 
     256 
    9257} // namespace 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h

    r2858 r2859  
    1 #ifndef _DeferredShader_H__ 
    2 #define _DeferredShader_H__ 
     1#ifndef _SsaoShader_H__ 
     2#define _SsaoShader_H__ 
    33 
    44#include "common.h" 
     5#include "glInterface.h" 
     6#include <Cg/cg.h> 
     7#include <Cg/cgGL.h> 
     8 
    59 
    610namespace CHCDemoEngine  
     
    913class FrameBufferObject; 
    1014 
    11 /** This class implements a deferred shading algorithm. 
     15 
     16/** This class implements a deferred shading algorithm that takes 
     17        a frame buffer object as input and outputs an image in the given size 
    1218*/ 
    13 class DeferredShader 
     19class SsaoShader 
    1420{ 
    1521public: 
    16         /** constructor requesting an opengl occlusion query. 
     22        /** constructor for a deferred shader taking the requested output image size, 
     23                the exponential smoothing factor for temporal reprojection. 
     24                 
     25                The last parameter is a just a scale factor 
     26                for the scene depth in order to get better floating point precision in the shader 
     27                This must be reciprocal value of the scale factor used in the mrt shader. 
    1728        */ 
    18         DeferredShader(); 
    19  
     29        SsaoShader(int w, int h, float expFactor, float scaleFactor); 
    2030        /** The algorithm renders the scene given an fbo. 
    2131                The fbo must have color buffer, position buffer, normal buffer. 
    2232        */ 
    23         void Render(FrameBufferObject *fbo); 
     33        void Render(FrameBufferObject *fbo, FrameBufferObject *fbo2); 
     34 
     35        /** Initialises the deferred shader and loads the required shaders: 
     36                This function has to be called only once. 
     37        */ 
     38        static void Init(CGcontext context); 
     39 
     40 
     41protected: 
     42 
     43        void FirstPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 
     44 
     45        void SecondPass(FrameBufferObject *fbo, FrameBufferObject *fbo2); 
     46 
     47 
     48        //////////// 
     49 
     50        int mWidth; 
     51        int mHeight; 
     52 
     53        /// the temporal smoothing factor 
     54        float mExpFactor; 
     55        /// this is just a scale factor for the scene depth in order to get better float precision in the shader 
     56        float mScaleFactor; 
    2457}; 
    2558 
    2659} // namespace  
    27 #endif // _DeferredShader_H__ 
     60#endif // _SsaoShader_H__ 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2857 r2859  
    671671 
    672672 
    673         ColorBufferObject *diffuseBuffer =  
    674                 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32,  
    675                                       ColorBufferObject::WRAP_CLAMP_TO_EDGE,  
    676                                                           ColorBufferObject::FILTER_LINEAR,  
    677                                                           false, false); 
    678  
    679         ColorBufferObject *mypositionBuffer =  
    680                 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32,  
    681                                       ColorBufferObject::WRAP_CLAMP_TO_EDGE,  
    682                                                           ColorBufferObject::FILTER_NEAREST,  
    683                                                           false, false); 
    684  
    685  
    686         ColorBufferObject *mynormalsBuffer =  
    687                 myfbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE,  
    688                                       ColorBufferObject::WRAP_CLAMP_TO_EDGE,  
    689                                                           ColorBufferObject::FILTER_NEAREST,  
    690                                                           false, false); 
     673        // the diffuse color buffer 
     674        myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32,  
     675                                                  ColorBufferObject::WRAP_CLAMP_TO_EDGE,  
     676                                                  ColorBufferObject::FILTER_LINEAR,  
     677                                                  false, false); 
     678 
     679        // the positions buffer 
     680        myfbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32,  
     681                              ColorBufferObject::WRAP_CLAMP_TO_EDGE,  
     682                                                  ColorBufferObject::FILTER_NEAREST,  
     683                                                  false, false); 
     684 
     685        // the normals buffer 
     686        myfbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE,  
     687                              ColorBufferObject::WRAP_CLAMP_TO_EDGE,  
     688                                                  ColorBufferObject::FILTER_NEAREST,  
     689                                                  false, false); 
    691690 
    692691         
Note: See TracChangeset for help on using the changeset viewer.