- Timestamp:
- 08/21/08 20:17:46 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
r2859 r2860 3 3 #include "RenderState.h" 4 4 #include "SampleGenerator.h" 5 #include "Vector3.h" 6 #include "Camera.h" 5 7 6 8 7 9 using namespace std; 8 10 9 GLenummrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};11 static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT}; 10 12 11 13 namespace CHCDemoEngine 12 14 { 15 16 // number of ssao samples 17 #define NUM_SAMPLES 16 18 13 19 14 20 static CGprogram sCgSsaoProgram = NULL; … … 25 31 static CGparameter sExpFactorParam; 26 32 27 28 #define NUM_SAMPLES 16 33 static GLuint noiseTex; 29 34 30 35 // ssao random spherical samples … … 54 59 55 60 56 SsaoShader::SsaoShader(int w, int h, float expFactor, float scaleFactor): 57 mWidth(w), mHeight(h), mExpFactor(expFactor), mScaleFactor(scaleFactor) 61 SsaoShader::SsaoShader(int w, int h, 62 Camera *cam, 63 float expFactor, 64 float scaleFactor 65 ): 66 mWidth(w), mHeight(h), 67 mCamera(cam), 68 mExpFactor(expFactor), 69 mScaleFactor(scaleFactor) 58 70 {} 59 71 … … 113 125 GLuint normalsTex = fbo->GetColorBuffer(0)->GetTexture(); 114 126 127 GLuint oldTex = fbo2->GetColorBuffer(0)->GetTexture(); 128 115 129 glPushAttrib(GL_VIEWPORT_BIT); 116 130 glViewport(0, 0, mWidth, mHeight); 117 131 118 glDrawBuffers(1, m rt);132 glDrawBuffers(1, mymrt); 119 133 120 134 glDisable(GL_ALPHA_TEST); … … 172 186 173 187 // 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); 181 195 182 196 glEnd(); 183 197 184 cgGLDisableTextureParameter(sColorsTexParam Ssao);185 cgGLDisableTextureParameter(sPositionsTexParam Ssao);186 cgGLDisableTextureParameter(sNormalsTexParam Ssao);187 cgGLDisableTextureParameter(sNoiseTexParam Ssao);188 cgGLDisableTextureParameter(sOldTexParam Ssao);198 cgGLDisableTextureParameter(sColorsTexParam); 199 cgGLDisableTextureParameter(sPositionsTexParam); 200 cgGLDisableTextureParameter(sNormalsTexParam); 201 cgGLDisableTextureParameter(sNoiseTexParam); 202 cgGLDisableTextureParameter(sOldTexParam); 189 203 190 204 cgGLDisableProfile(RenderState::sCgFragmentProfile); … … 254 268 255 269 270 void 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 300 void 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 256 336 257 337 } // namespace
Note: See TracChangeset
for help on using the changeset viewer.