Ignore:
Timestamp:
08/21/08 20:17:46 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2859 r2860  
    33#include "RenderState.h" 
    44#include "SampleGenerator.h" 
     5#include "Vector3.h" 
     6#include "Camera.h" 
    57 
    68 
    79using namespace std; 
    810 
    9 GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 
     11static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 
    1012 
    1113namespace CHCDemoEngine 
    1214{ 
     15 
     16// number of ssao samples 
     17#define NUM_SAMPLES 16 
     18 
    1319 
    1420static CGprogram sCgSsaoProgram = NULL; 
     
    2531static CGparameter sExpFactorParam; 
    2632 
    27  
    28 #define NUM_SAMPLES 16 
     33static GLuint noiseTex; 
    2934 
    3035// ssao random spherical samples 
     
    5459 
    5560 
    56 SsaoShader::SsaoShader(int w, int h, float expFactor, float scaleFactor): 
    57 mWidth(w), mHeight(h), mExpFactor(expFactor), mScaleFactor(scaleFactor) 
     61SsaoShader::SsaoShader(int w, int h,  
     62                                           Camera *cam, 
     63                                           float expFactor,  
     64                                           float scaleFactor 
     65                                           ): 
     66mWidth(w), mHeight(h),  
     67mCamera(cam), 
     68mExpFactor(expFactor),  
     69mScaleFactor(scaleFactor) 
    5870{} 
    5971 
     
    113125        GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 
    114126 
     127        GLuint oldTex = fbo2->GetColorBuffer(0)->GetTexture(); 
     128 
    115129        glPushAttrib(GL_VIEWPORT_BIT); 
    116130        glViewport(0, 0, mWidth, mHeight); 
    117131 
    118         glDrawBuffers(1, mrt); 
     132        glDrawBuffers(1, mymrt); 
    119133 
    120134        glDisable(GL_ALPHA_TEST); 
     
    172186 
    173187        // 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); 
     188        //const float new_offs = 0.55f; 
     189        const float new_offs = 0.5f; 
     190         
     191        glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f); 
     192        glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f); 
     193        glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f); 
     194        glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f); 
    181195 
    182196        glEnd(); 
    183197 
    184         cgGLDisableTextureParameter(sColorsTexParamSsao); 
    185         cgGLDisableTextureParameter(sPositionsTexParamSsao); 
    186         cgGLDisableTextureParameter(sNormalsTexParamSsao); 
    187         cgGLDisableTextureParameter(sNoiseTexParamSsao); 
    188         cgGLDisableTextureParameter(sOldTexParamSsao); 
     198        cgGLDisableTextureParameter(sColorsTexParam); 
     199        cgGLDisableTextureParameter(sPositionsTexParam); 
     200        cgGLDisableTextureParameter(sNormalsTexParam); 
     201        cgGLDisableTextureParameter(sNoiseTexParam); 
     202        cgGLDisableTextureParameter(sOldTexParam); 
    189203 
    190204        cgGLDisableProfile(RenderState::sCgFragmentProfile); 
     
    254268 
    255269 
     270void SsaoShader::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br) 
     271{ 
     272        Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; 
     273 
     274        mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
     275 
     276#if 1 // matT: debug this!! 
     277         
     278        bl = Normalize(nbl - fbl); 
     279        br = Normalize(nbr - fbr); 
     280        tl = Normalize(ftl - ntl); 
     281        tr = Normalize(ftr - ntr); 
     282 
     283#else // just take camera direction 
     284         
     285        bl = -Normalize(mCamera->GetDirection()); 
     286        br = -Normalize(mCamera->GetDirection()); 
     287        tl = -Normalize(mCamera->GetDirection()); 
     288        tr = -Normalize(mCamera->GetDirection()); 
     289 
     290#endif 
     291 
     292        // normalize to 0 .. 1 
     293        bl = bl * 0.5f + 0.5f; 
     294        br = br * 0.5f + 0.5f; 
     295        tl = tl * 0.5f + 0.5f; 
     296        tr = tr * 0.5f + 0.5f; 
     297} 
     298 
     299 
     300void SsaoShader::CreateNoiseTex2D() 
     301{ 
     302        GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3]; 
     303 
     304        for (int i = 0; i < mWidth * mHeight * 3; i += 3) 
     305        { 
     306                // create random samples over sphere 
     307                const float rx = RandomValue(0, 1); 
     308                const float theta = 2.0f * acos(sqrt(1.0f - rx)); 
     309 
     310                randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f); 
     311                randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f); 
     312                randomNormals[i + 2] = 0; 
     313        } 
     314 
     315        glEnable(GL_TEXTURE_2D); 
     316        glGenTextures(1, &noiseTex); 
     317        glBindTexture(GL_TEXTURE_2D, noiseTex); 
     318                 
     319        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
     320        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     321        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
     322        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
     323 
     324        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mWidth, mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals); 
     325 
     326        glBindTexture(GL_TEXTURE_2D, 0); 
     327        glDisable(GL_TEXTURE_2D); 
     328 
     329        delete [] randomNormals; 
     330 
     331        cout << "created noise texture" << endl; 
     332 
     333        PrintGLerror("noisetexture"); 
     334} 
     335 
    256336 
    257337} // namespace 
Note: See TracChangeset for help on using the changeset viewer.