- Timestamp:
- 08/25/08 17:34:34 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
r2861 r2865 24 24 25 25 26 26 27 static CGprogram sCgDeferredProgram; 28 static CGprogram sCgAntiAliasingProgram; 29 30 27 31 static CGparameter sColorsTexParam; 28 32 static CGparameter sPositionsTexParam; 29 33 static CGparameter sNormalsTexParam; 30 34 35 static CGparameter sColorsTexAntiAliasingParam; 36 static CGparameter sNormalsTexAntiAliasingParam; 37 31 38 32 39 DeferredShader::DeferredShader(int w, int h): 33 40 mWidth(w), mHeight(h) 34 {} 41 { 42 mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE); 43 // the diffuse color buffer 44 mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false); 45 } 35 46 36 47 … … 65 76 cerr << "deferred program failed to load" << endl; 66 77 78 sCgAntiAliasingProgram = 79 cgCreateProgramFromFile(context, 80 CG_SOURCE, 81 "src/shaders/antialiasing.cg", 82 RenderState::sCgFragmentProfile, 83 "main", 84 NULL); 85 86 if (sCgAntiAliasingProgram != NULL) 87 { 88 cgGLLoadProgram(sCgAntiAliasingProgram); 89 90 sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 91 sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals"); 92 } 93 else 94 cerr << "antialiasing program failed to load" << endl; 95 67 96 68 97 PrintGLerror("init"); … … 72 101 void DeferredShader::Render(FrameBufferObject *fbo) 73 102 { 74 FrameBufferObject::Release(); 75 103 FirstPass(fbo); 104 AntiAliasing(fbo); 105 } 106 107 108 void DeferredShader::FirstPass(FrameBufferObject *fbo) 109 { 76 110 GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture(); 77 111 GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture(); 78 112 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 113 114 // read the second buffer, write to the first buffer 115 mFbo->Bind(); 79 116 80 117 glPushAttrib(GL_VIEWPORT_BIT); … … 141 178 glPopAttrib(); 142 179 180 FrameBufferObject::Release(); 181 143 182 PrintGLerror("deferred shading"); 144 183 } 145 184 146 185 186 static void SetVertex(float x, float y, float x_offs, float y_offs) 187 { 188 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center 189 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top 190 glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom 191 glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top 192 glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom 193 194 glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right 195 glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom 196 197 glVertex3f(x - 0.5f, y - 0.5f, -0.5f); 198 } 199 200 201 void DeferredShader::AntiAliasing(FrameBufferObject *fbo) 202 { 203 GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture(); 204 GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture(); 205 206 glPushAttrib(GL_VIEWPORT_BIT); 207 glViewport(0, 0, mWidth, mHeight); 208 209 glDisable(GL_ALPHA_TEST); 210 glDisable(GL_TEXTURE_2D); 211 glDisable(GL_LIGHTING); 212 213 glMatrixMode(GL_PROJECTION); 214 glPushMatrix(); 215 glLoadIdentity(); 216 217 glMatrixMode(GL_MODELVIEW); 218 glPushMatrix(); 219 glLoadIdentity(); 220 221 const float offs = 0.5f; 222 223 glOrtho(-offs, offs, -offs, offs, 0, 1); 224 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 225 226 cgGLEnableProfile(RenderState::sCgFragmentProfile); 227 228 cgGLBindProgram(sCgAntiAliasingProgram); 229 230 cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex); 231 cgGLEnableTextureParameter(sColorsTexAntiAliasingParam); 232 233 cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex); 234 cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam); 235 236 glColor3f(1.0f, 1.0f, 1.0f); 237 238 float offs2 = 0.5f; 239 240 glBegin(GL_QUADS); 241 242 // the neighbouring texels 243 float x_offs = 1.0f / mWidth; 244 float y_offs = 1.0f / mHeight; 245 246 SetVertex(0, 0, x_offs, y_offs); 247 SetVertex(1, 0, x_offs, y_offs); 248 SetVertex(1, 1, x_offs, y_offs); 249 SetVertex(0, 1, x_offs, y_offs); 250 251 glEnd(); 252 253 cgGLDisableTextureParameter(sColorsTexAntiAliasingParam); 254 cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam); 255 256 cgGLDisableProfile(RenderState::sCgFragmentProfile); 257 258 glEnable(GL_LIGHTING); 259 glDisable(GL_TEXTURE_2D); 260 261 glMatrixMode(GL_PROJECTION); 262 glPopMatrix(); 263 264 glMatrixMode(GL_MODELVIEW); 265 glPopMatrix(); 266 267 glPopAttrib(); 268 269 PrintGLerror("deferred shading"); 270 } 271 272 147 273 } // namespace
Note: See TracChangeset
for help on using the changeset viewer.