[2858] | 1 | #include "DeferredShader.h"
|
---|
[2859] | 2 | #include "FrameBufferObject.h"
|
---|
| 3 | #include "RenderState.h"
|
---|
[2892] | 4 | #include "ShadowMapping.h"
|
---|
[2858] | 5 |
|
---|
| 6 |
|
---|
| 7 | using namespace std;
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | namespace CHCDemoEngine
|
---|
| 11 | {
|
---|
| 12 |
|
---|
[2859] | 13 |
|
---|
| 14 | static void PrintGLerror(char *msg)
|
---|
| 15 | {
|
---|
| 16 | GLenum errCode;
|
---|
| 17 | const GLubyte *errStr;
|
---|
| 18 |
|
---|
| 19 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 20 | {
|
---|
| 21 | errStr = gluErrorString(errCode);
|
---|
| 22 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 |
|
---|
[2879] | 27 | static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
|
---|
[2865] | 28 |
|
---|
[2879] | 29 |
|
---|
[2859] | 30 | static CGprogram sCgDeferredProgram;
|
---|
[2892] | 31 | static CGprogram sCgDeferredShadowProgram;
|
---|
[2865] | 32 | static CGprogram sCgAntiAliasingProgram;
|
---|
| 33 |
|
---|
| 34 |
|
---|
[2859] | 35 | static CGparameter sColorsTexParam;
|
---|
| 36 | static CGparameter sPositionsTexParam;
|
---|
| 37 | static CGparameter sNormalsTexParam;
|
---|
| 38 |
|
---|
[2865] | 39 | static CGparameter sColorsTexAntiAliasingParam;
|
---|
| 40 | static CGparameter sNormalsTexAntiAliasingParam;
|
---|
[2859] | 41 |
|
---|
[2892] | 42 | static CGparameter sShadowMapParam;
|
---|
| 43 | static CGparameter sPositionsTexShadowParam;
|
---|
| 44 | static CGparameter sColorsTexShadowParam;
|
---|
| 45 | static CGparameter sNormalsTexShadowParam;
|
---|
[2865] | 46 |
|
---|
[2892] | 47 | static CGparameter sShadowMatrixParam;
|
---|
[2893] | 48 | static CGparameter sMaxDepthParam;
|
---|
[2895] | 49 | static CGparameter sSampleWidthParam;
|
---|
[2892] | 50 |
|
---|
| 51 |
|
---|
[2893] | 52 | DeferredShader::DeferredShader(int w, int h, float scaleFactor):
|
---|
| 53 | mWidth(w), mHeight(h), mScaleFactor(scaleFactor)
|
---|
[2865] | 54 | {
|
---|
[2879] | 55 | //mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
| 56 | //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
[2865] | 57 | }
|
---|
[2859] | 58 |
|
---|
| 59 |
|
---|
[2861] | 60 | DeferredShader::~DeferredShader()
|
---|
| 61 | {
|
---|
| 62 | if (sCgDeferredProgram)
|
---|
| 63 | cgDestroyProgram(sCgDeferredProgram);
|
---|
[2879] | 64 |
|
---|
| 65 | //DEL_PTR(mFbo);
|
---|
[2861] | 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[2859] | 69 | void DeferredShader::Init(CGcontext context)
|
---|
| 70 | {
|
---|
| 71 | sCgDeferredProgram =
|
---|
| 72 | cgCreateProgramFromFile(context,
|
---|
| 73 | CG_SOURCE,
|
---|
| 74 | "src/shaders/deferred.cg",
|
---|
| 75 | RenderState::sCgFragmentProfile,
|
---|
| 76 | "main",
|
---|
| 77 | NULL);
|
---|
| 78 |
|
---|
| 79 | if (sCgDeferredProgram != NULL)
|
---|
| 80 | {
|
---|
| 81 | cgGLLoadProgram(sCgDeferredProgram);
|
---|
| 82 |
|
---|
| 83 | // we need size of texture for scaling
|
---|
| 84 | sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");
|
---|
| 85 | sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");
|
---|
| 86 | sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
|
---|
| 87 | }
|
---|
| 88 | else
|
---|
| 89 | cerr << "deferred program failed to load" << endl;
|
---|
| 90 |
|
---|
[2865] | 91 | sCgAntiAliasingProgram =
|
---|
| 92 | cgCreateProgramFromFile(context,
|
---|
| 93 | CG_SOURCE,
|
---|
| 94 | "src/shaders/antialiasing.cg",
|
---|
| 95 | RenderState::sCgFragmentProfile,
|
---|
| 96 | "main",
|
---|
| 97 | NULL);
|
---|
[2859] | 98 |
|
---|
[2865] | 99 | if (sCgAntiAliasingProgram != NULL)
|
---|
| 100 | {
|
---|
| 101 | cgGLLoadProgram(sCgAntiAliasingProgram);
|
---|
| 102 |
|
---|
| 103 | sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");
|
---|
| 104 | sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
|
---|
| 105 | }
|
---|
| 106 | else
|
---|
| 107 | cerr << "antialiasing program failed to load" << endl;
|
---|
| 108 |
|
---|
[2892] | 109 | sCgDeferredShadowProgram =
|
---|
| 110 | cgCreateProgramFromFile(context,
|
---|
| 111 | CG_SOURCE,
|
---|
| 112 | "src/shaders/deferred.cg",
|
---|
| 113 | RenderState::sCgFragmentProfile,
|
---|
| 114 | "main_shadow",
|
---|
| 115 | NULL);
|
---|
[2865] | 116 |
|
---|
[2892] | 117 | if (sCgDeferredShadowProgram != NULL)
|
---|
| 118 | {
|
---|
| 119 | cgGLLoadProgram(sCgDeferredShadowProgram);
|
---|
| 120 |
|
---|
| 121 | // we need size of texture for scaling
|
---|
| 122 | sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");
|
---|
| 123 | sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");
|
---|
[2895] | 124 | sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
|
---|
| 125 |
|
---|
[2892] | 126 | sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");
|
---|
[2893] | 127 | sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
|
---|
[2895] | 128 | sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
|
---|
[2892] | 129 | sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
|
---|
| 130 | }
|
---|
| 131 | else
|
---|
| 132 | cerr << "deferred program failed to load" << endl;
|
---|
| 133 |
|
---|
[2859] | 134 | PrintGLerror("init");
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[2858] | 138 | void DeferredShader::Render(FrameBufferObject *fbo)
|
---|
| 139 | {
|
---|
[2892] | 140 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 141 |
|
---|
| 142 | glDisable(GL_ALPHA_TEST);
|
---|
| 143 | glDisable(GL_TEXTURE_2D);
|
---|
| 144 | glDisable(GL_LIGHTING);
|
---|
| 145 |
|
---|
| 146 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 147 | glViewport(0, 0, mWidth, mHeight);
|
---|
| 148 |
|
---|
| 149 | glMatrixMode(GL_PROJECTION);
|
---|
| 150 | glPushMatrix();
|
---|
| 151 | glLoadIdentity();
|
---|
| 152 |
|
---|
| 153 | glMatrixMode(GL_MODELVIEW);
|
---|
| 154 | glPushMatrix();
|
---|
| 155 | glLoadIdentity();
|
---|
| 156 |
|
---|
| 157 | const float offs = 0.5f;
|
---|
| 158 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 159 |
|
---|
[2865] | 160 | FirstPass(fbo);
|
---|
| 161 | AntiAliasing(fbo);
|
---|
[2892] | 162 |
|
---|
| 163 | glEnable(GL_LIGHTING);
|
---|
[2895] | 164 |
|
---|
[2892] | 165 | glMatrixMode(GL_PROJECTION);
|
---|
| 166 | glPopMatrix();
|
---|
| 167 |
|
---|
| 168 | glMatrixMode(GL_MODELVIEW);
|
---|
| 169 | glPopMatrix();
|
---|
| 170 |
|
---|
| 171 | glPopAttrib();
|
---|
| 172 |
|
---|
| 173 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2895] | 174 | FrameBufferObject::Release();
|
---|
[2865] | 175 | }
|
---|
[2861] | 176 |
|
---|
[2865] | 177 |
|
---|
[2892] | 178 | void DeferredShader::Render(FrameBufferObject *fbo,
|
---|
[2894] | 179 | ShadowMap *shadowMap)
|
---|
[2865] | 180 | {
|
---|
[2895] | 181 | FrameBufferObject::Release();
|
---|
| 182 |
|
---|
[2892] | 183 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
[2859] | 184 |
|
---|
[2892] | 185 | glDisable(GL_ALPHA_TEST);
|
---|
| 186 | glDisable(GL_TEXTURE_2D);
|
---|
| 187 | glDisable(GL_LIGHTING);
|
---|
[2895] | 188 | //glDepthMask(GL_FALSE);
|
---|
[2893] | 189 |
|
---|
[2865] | 190 |
|
---|
[2858] | 191 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[2859] | 192 | glViewport(0, 0, mWidth, mHeight);
|
---|
[2858] | 193 |
|
---|
| 194 | glMatrixMode(GL_PROJECTION);
|
---|
| 195 | glPushMatrix();
|
---|
| 196 | glLoadIdentity();
|
---|
| 197 |
|
---|
| 198 | glMatrixMode(GL_MODELVIEW);
|
---|
| 199 | glPushMatrix();
|
---|
| 200 | glLoadIdentity();
|
---|
| 201 |
|
---|
| 202 | const float offs = 0.5f;
|
---|
[2892] | 203 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 204 |
|
---|
[2894] | 205 | FirstPassShadow(fbo, shadowMap);
|
---|
[2892] | 206 | AntiAliasing(fbo);
|
---|
| 207 |
|
---|
| 208 | glEnable(GL_LIGHTING);
|
---|
[2895] | 209 | //glEnable(GL_TEXTURE_2D);
|
---|
| 210 | //glDepthMask(GL_TRUE);
|
---|
[2858] | 211 |
|
---|
[2892] | 212 | glMatrixMode(GL_PROJECTION);
|
---|
| 213 | glPopMatrix();
|
---|
| 214 |
|
---|
| 215 | glMatrixMode(GL_MODELVIEW);
|
---|
| 216 | glPopMatrix();
|
---|
| 217 |
|
---|
| 218 | glPopAttrib();
|
---|
| 219 |
|
---|
| 220 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2895] | 221 | FrameBufferObject::Release();
|
---|
[2892] | 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | void DeferredShader::FirstPass(FrameBufferObject *fbo)
|
---|
| 226 | {
|
---|
| 227 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
| 228 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 229 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
| 230 |
|
---|
| 231 | fbo->Bind();
|
---|
| 232 | glDrawBuffers(1, mymrt + 3);
|
---|
| 233 |
|
---|
[2858] | 234 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 235 |
|
---|
| 236 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 237 | cgGLBindProgram(sCgDeferredProgram);
|
---|
| 238 |
|
---|
[2859] | 239 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
| 240 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
| 241 |
|
---|
[2858] | 242 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
| 243 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
| 244 |
|
---|
| 245 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
| 246 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
| 247 |
|
---|
| 248 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
[2861] | 249 |
|
---|
[2858] | 250 | glBegin(GL_QUADS);
|
---|
| 251 |
|
---|
| 252 | float offs2 = 0.5f;
|
---|
| 253 |
|
---|
| 254 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 255 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 256 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 257 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
| 258 |
|
---|
| 259 | glEnd();
|
---|
| 260 |
|
---|
| 261 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
| 262 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
| 263 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
| 264 |
|
---|
[2865] | 265 | FrameBufferObject::Release();
|
---|
| 266 |
|
---|
[2858] | 267 | PrintGLerror("deferred shading");
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
[2865] | 271 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
| 272 | {
|
---|
| 273 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
| 274 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
| 275 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
| 276 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
| 277 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
| 278 |
|
---|
| 279 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
| 280 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
| 281 |
|
---|
| 282 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 |
|
---|
| 286 | void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
|
---|
| 287 | {
|
---|
[2879] | 288 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
[2865] | 289 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
| 290 |
|
---|
| 291 |
|
---|
| 292 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 293 |
|
---|
| 294 | cgGLBindProgram(sCgAntiAliasingProgram);
|
---|
| 295 |
|
---|
| 296 | cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
|
---|
| 297 | cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
| 298 |
|
---|
| 299 | cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
|
---|
| 300 | cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
| 301 |
|
---|
| 302 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
[2895] | 303 |
|
---|
[2865] | 304 | glBegin(GL_QUADS);
|
---|
| 305 |
|
---|
| 306 | // the neighbouring texels
|
---|
| 307 | float x_offs = 1.0f / mWidth;
|
---|
| 308 | float y_offs = 1.0f / mHeight;
|
---|
| 309 |
|
---|
| 310 | SetVertex(0, 0, x_offs, y_offs);
|
---|
| 311 | SetVertex(1, 0, x_offs, y_offs);
|
---|
| 312 | SetVertex(1, 1, x_offs, y_offs);
|
---|
| 313 | SetVertex(0, 1, x_offs, y_offs);
|
---|
| 314 |
|
---|
| 315 | glEnd();
|
---|
| 316 |
|
---|
| 317 | cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
| 318 | cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
| 319 |
|
---|
[2892] | 320 | PrintGLerror("antialiasing");
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 | void DeferredShader::FirstPassShadow(FrameBufferObject *fbo,
|
---|
[2894] | 325 | ShadowMap *shadowMap)
|
---|
[2892] | 326 | {
|
---|
| 327 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
| 328 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 329 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[2894] | 330 | GLuint shadowTex = shadowMap->GetShadowTexture();
|
---|
[2892] | 331 |
|
---|
[2894] | 332 | Matrix4x4 shadowMatrix;
|
---|
| 333 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
[2892] | 334 |
|
---|
| 335 | fbo->Bind();
|
---|
| 336 | glDrawBuffers(1, mymrt + 3);
|
---|
| 337 |
|
---|
[2865] | 338 |
|
---|
[2892] | 339 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 340 |
|
---|
| 341 | cgGLBindProgram(sCgDeferredShadowProgram);
|
---|
| 342 |
|
---|
| 343 | cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
|
---|
| 344 | cgGLEnableTextureParameter(sColorsTexShadowParam);
|
---|
| 345 |
|
---|
| 346 | cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
|
---|
| 347 | cgGLEnableTextureParameter(sPositionsTexShadowParam);
|
---|
| 348 |
|
---|
| 349 | cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
|
---|
| 350 | cgGLEnableTextureParameter(sNormalsTexShadowParam);
|
---|
[2865] | 351 |
|
---|
[2894] | 352 | cgGLSetTextureParameter(sShadowMapParam, shadowTex);
|
---|
[2892] | 353 | cgGLEnableTextureParameter(sShadowMapParam);
|
---|
[2865] | 354 |
|
---|
[2893] | 355 | cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
|
---|
[2895] | 356 | cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
|
---|
| 357 |
|
---|
[2892] | 358 | cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
|
---|
[2865] | 359 |
|
---|
[2892] | 360 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 361 |
|
---|
| 362 | glBegin(GL_QUADS);
|
---|
[2865] | 363 |
|
---|
[2892] | 364 | float offs2 = 0.5f;
|
---|
| 365 |
|
---|
| 366 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 367 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 368 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 369 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
| 370 |
|
---|
| 371 | glEnd();
|
---|
| 372 |
|
---|
| 373 | cgGLDisableTextureParameter(sColorsTexShadowParam);
|
---|
| 374 | cgGLDisableTextureParameter(sPositionsTexShadowParam);
|
---|
| 375 | cgGLDisableTextureParameter(sNormalsTexShadowParam);
|
---|
| 376 | cgGLDisableTextureParameter(sShadowMapParam);
|
---|
| 377 |
|
---|
| 378 | FrameBufferObject::Release();
|
---|
| 379 |
|
---|
[2865] | 380 | PrintGLerror("deferred shading");
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 |
|
---|
[2892] | 384 |
|
---|
[2858] | 385 | } // namespace
|
---|