[2896] | 1 | #include "DeferredRenderer.h"
|
---|
[2859] | 2 | #include "FrameBufferObject.h"
|
---|
| 3 | #include "RenderState.h"
|
---|
| 4 | #include "SampleGenerator.h"
|
---|
[2860] | 5 | #include "Vector3.h"
|
---|
| 6 | #include "Camera.h"
|
---|
[2884] | 7 | #include "shaderenv.h"
|
---|
[2886] | 8 | #include "Halton.h"
|
---|
[2895] | 9 | #include "ShadowMapping.h"
|
---|
[2858] | 10 |
|
---|
[2859] | 11 |
|
---|
[2858] | 12 | using namespace std;
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | namespace CHCDemoEngine
|
---|
| 16 | {
|
---|
| 17 |
|
---|
[2859] | 18 | static CGprogram sCgSsaoProgram = NULL;
|
---|
[2873] | 19 | static CGprogram sCgGiProgram = NULL;
|
---|
| 20 |
|
---|
| 21 | static CGprogram sCgDeferredProgram = NULL;
|
---|
[2868] | 22 | static CGprogram sCgAntiAliasingProgram = NULL;
|
---|
[2895] | 23 | static CGprogram sCgDeferredShadowProgram = NULL;
|
---|
[2928] | 24 | static CGprogram sCgShadowDebugProgram = NULL;
|
---|
[2859] | 25 |
|
---|
[2869] | 26 | static CGparameter sColorsTexCombineParam;
|
---|
| 27 | static CGparameter sSsaoTexCombineParam;
|
---|
| 28 |
|
---|
[2868] | 29 | static CGparameter sColorsTexDeferredParam;
|
---|
| 30 | static CGparameter sPositionsTexDeferredParam;
|
---|
| 31 | static CGparameter sNormalsTexDeferredParam;
|
---|
| 32 |
|
---|
[2928] | 33 | static CGparameter sShadowMapDebugParam;
|
---|
| 34 | static CGparameter sShadowMatrixDebugParam;
|
---|
| 35 | static CGparameter sColorsTexDebugParam;
|
---|
[2873] | 36 |
|
---|
[2928] | 37 |
|
---|
[2880] | 38 | static CGprogram sCgCombinedSsaoProgram = NULL;
|
---|
| 39 | static CGprogram sCgCombinedIllumProgram = NULL;
|
---|
| 40 |
|
---|
[2873] | 41 | ///////////////////////////////////////7
|
---|
| 42 |
|
---|
| 43 |
|
---|
[2859] | 44 | static CGparameter sColorsTexParam;
|
---|
| 45 | static CGparameter sPositionsTexParam;
|
---|
| 46 | static CGparameter sNormalsTexParam;
|
---|
[2868] | 47 |
|
---|
[2859] | 48 | static CGparameter sOldModelViewProjMatrixParam;
|
---|
[2900] | 49 | static CGparameter sModelViewProjMatrixParam;
|
---|
[2859] | 50 | static CGparameter sMaxDepthParam;
|
---|
| 51 | static CGparameter sSamplesParam;
|
---|
| 52 | static CGparameter sOldTexParam;
|
---|
| 53 | static CGparameter sNoiseTexParam;
|
---|
[2897] | 54 | static CGparameter sTemporalCoherenceParam;
|
---|
[2859] | 55 |
|
---|
[2873] | 56 |
|
---|
| 57 | ///////////////////////////////////////
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | static CGparameter sColorsTexGiParam;
|
---|
| 61 | static CGparameter sPositionsTexGiParam;
|
---|
| 62 | static CGparameter sNormalsTexGiParam;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | static CGparameter sOldModelViewProjMatrixGiParam;
|
---|
| 66 | static CGparameter sMaxDepthGiParam;
|
---|
| 67 | static CGparameter sSamplesGiParam;
|
---|
| 68 | static CGparameter sOldSsaoTexGiParam;
|
---|
| 69 | static CGparameter sOldIllumTexGiParam;
|
---|
| 70 | static CGparameter sNoiseTexGiParam;
|
---|
[2897] | 71 | static CGparameter sTemporalCoherenceGiParam;
|
---|
[2873] | 72 |
|
---|
| 73 |
|
---|
[2880] | 74 | static CGparameter sColorsTexCombinedIllumParam;
|
---|
| 75 | static CGparameter sSsaoTexCombinedIllumParam;
|
---|
| 76 | static CGparameter sIllumTexCombinedIllumParam;
|
---|
| 77 |
|
---|
| 78 | static CGparameter sColorsTexCombinedSsaoParam;
|
---|
| 79 | static CGparameter sSsaoTexCombinedSsaoParam;
|
---|
| 80 |
|
---|
| 81 |
|
---|
[2873] | 82 | ////////////
|
---|
| 83 |
|
---|
[2865] | 84 | static CGparameter sColorsTexAntiAliasingParam;
|
---|
| 85 | static CGparameter sNormalsTexAntiAliasingParam;
|
---|
| 86 |
|
---|
[2895] | 87 |
|
---|
| 88 | static CGparameter sShadowMapParam;
|
---|
| 89 | static CGparameter sPositionsTexShadowParam;
|
---|
| 90 | static CGparameter sColorsTexShadowParam;
|
---|
| 91 | static CGparameter sNormalsTexShadowParam;
|
---|
| 92 |
|
---|
| 93 | static CGparameter sShadowMatrixParam;
|
---|
| 94 | static CGparameter sMaxDepthShadowParam;
|
---|
| 95 | static CGparameter sSampleWidthParam;
|
---|
| 96 |
|
---|
[2903] | 97 | //#define USE_3D_SSAO
|
---|
[2895] | 98 |
|
---|
| 99 |
|
---|
[2879] | 100 | static GLuint noiseTex = 0;
|
---|
[2865] | 101 |
|
---|
[2859] | 102 | // ssao random spherical samples
|
---|
[2903] | 103 | #ifdef USE_3D_SSAO
|
---|
| 104 |
|
---|
[2900] | 105 | static Sample2 samples3[NUM_SAMPLES];
|
---|
[2859] | 106 |
|
---|
[2903] | 107 | #else
|
---|
| 108 |
|
---|
| 109 | static Sample2 samples2[NUM_SAMPLES];
|
---|
| 110 |
|
---|
| 111 | #endif
|
---|
| 112 |
|
---|
[2895] | 113 | static int colorBufferIdx = 0;
|
---|
[2859] | 114 |
|
---|
| 115 | static void PrintGLerror(char *msg)
|
---|
| 116 | {
|
---|
| 117 | GLenum errCode;
|
---|
| 118 | const GLubyte *errStr;
|
---|
| 119 |
|
---|
| 120 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 121 | {
|
---|
| 122 | errStr = gluErrorString(errCode);
|
---|
| 123 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /** Generate poisson disc distributed sample points on the unit disc
|
---|
| 129 | */
|
---|
[2887] | 130 | static void GenerateSamples(int sampling)
|
---|
[2859] | 131 | {
|
---|
[2903] | 132 | #ifdef USE_3D_SSAO
|
---|
| 133 |
|
---|
| 134 | SphericalSampleGenerator sph(NUM_SAMPLES, 1.0f);
|
---|
| 135 | sph.Generate((float *)samples3);
|
---|
| 136 |
|
---|
| 137 | #else
|
---|
[2887] | 138 | switch (sampling)
|
---|
| 139 | {
|
---|
[2930] | 140 | case DeferredRenderer::SAMPLING_POISSON:
|
---|
[2887] | 141 | {
|
---|
[2930] | 142 | PoissonDiscSampleGenerator2 poisson(NUM_SAMPLES, 1.0f);
|
---|
[2900] | 143 | poisson.Generate((float *)samples2);
|
---|
[2887] | 144 | }
|
---|
| 145 | break;
|
---|
[2930] | 146 | case DeferredRenderer::SAMPLING_QUADRATIC:
|
---|
[2887] | 147 | {
|
---|
[2930] | 148 | QuadraticDiscSampleGenerator2 g(NUM_SAMPLES, 1.0f);
|
---|
| 149 | g.Generate((float *)samples2);
|
---|
[2887] | 150 | }
|
---|
| 151 | break;
|
---|
[2930] | 152 | default: // SAMPLING_DEFAULT
|
---|
| 153 |
|
---|
| 154 | RandomSampleGenerator2 g(NUM_SAMPLES, 1.0f);
|
---|
| 155 | g.Generate((float *)samples2);
|
---|
[2903] | 156 | }
|
---|
| 157 | #endif
|
---|
[2859] | 158 | }
|
---|
| 159 |
|
---|
| 160 |
|
---|
[2879] | 161 | static void CreateNoiseTex2D(int w, int h)
|
---|
| 162 | {
|
---|
| 163 | //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
|
---|
| 164 | float *randomNormals = new float[w * h * 3];
|
---|
| 165 |
|
---|
[2900] | 166 | static HaltonSequence halton;
|
---|
| 167 | float r[2];
|
---|
| 168 |
|
---|
[2879] | 169 | for (int i = 0; i < w * h * 3; i += 3)
|
---|
| 170 | {
|
---|
[2901] | 171 |
|
---|
[2903] | 172 | #ifdef USE_3D_SSAO
|
---|
| 173 | //halton.GetNext(2, r);
|
---|
| 174 | r[0] = RandomValue(0, 1);
|
---|
| 175 | r[1] = RandomValue(0, 1);
|
---|
[2879] | 176 |
|
---|
[2900] | 177 | const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
|
---|
| 178 | const float phi = 2.0f * M_PI * r[1];
|
---|
| 179 |
|
---|
| 180 | randomNormals[i + 0] = sin(theta) * cos(phi);
|
---|
| 181 | randomNormals[i + 1] = sin(theta) * sin(phi);
|
---|
| 182 | randomNormals[i + 2] = cos(theta);
|
---|
[2903] | 183 | #else
|
---|
| 184 | // create random samples on a circle
|
---|
| 185 | r[0] = RandomValue(0, 1);
|
---|
| 186 | //halton.GetNext(1, r);
|
---|
| 187 |
|
---|
| 188 | const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
|
---|
| 189 |
|
---|
| 190 | randomNormals[i + 0] = cos(theta);
|
---|
| 191 | randomNormals[i + 1] = sin(theta);
|
---|
| 192 | randomNormals[i + 2] = 0;
|
---|
| 193 | #endif
|
---|
[2879] | 194 | }
|
---|
| 195 |
|
---|
| 196 | glEnable(GL_TEXTURE_2D);
|
---|
| 197 | glGenTextures(1, &noiseTex);
|
---|
| 198 | glBindTexture(GL_TEXTURE_2D, noiseTex);
|
---|
| 199 |
|
---|
| 200 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 201 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 202 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
| 203 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
| 204 |
|
---|
[2884] | 205 | //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
|
---|
[2879] | 206 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
|
---|
| 207 |
|
---|
| 208 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 209 | glDisable(GL_TEXTURE_2D);
|
---|
| 210 |
|
---|
| 211 | delete [] randomNormals;
|
---|
| 212 |
|
---|
| 213 | cout << "created noise texture" << endl;
|
---|
| 214 |
|
---|
| 215 | PrintGLerror("noisetexture");
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
[2896] | 219 | DeferredRenderer::DeferredRenderer(int w, int h, Camera *cam, float scaleFactor):
|
---|
[2860] | 220 | mWidth(w), mHeight(h),
|
---|
| 221 | mCamera(cam),
|
---|
[2875] | 222 | mScaleFactor(scaleFactor),
|
---|
| 223 | mUseTemporalCoherence(true),
|
---|
[2895] | 224 | mRegenerateSamples(true),
|
---|
[2930] | 225 | mSamplingMethod(SAMPLING_POISSON),
|
---|
[2895] | 226 | mShadingMethod(DEFAULT),
|
---|
[2891] | 227 | mFboIndex(0)
|
---|
[2861] | 228 | {
|
---|
[2879] | 229 | // create noise texture for ssao
|
---|
| 230 | CreateNoiseTex2D(w, h);
|
---|
| 231 |
|
---|
[2861] | 232 | ///////////
|
---|
| 233 | //-- the flip-flop fbos
|
---|
[2859] | 234 |
|
---|
[2891] | 235 | mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
| 236 |
|
---|
| 237 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
| 238 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
| 239 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
| 240 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
[2861] | 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
[2896] | 244 | DeferredRenderer::~DeferredRenderer()
|
---|
[2861] | 245 | {
|
---|
[2879] | 246 | if (sCgSsaoProgram) cgDestroyProgram(sCgSsaoProgram);
|
---|
| 247 | if (sCgDeferredProgram) cgDestroyProgram(sCgDeferredProgram);
|
---|
| 248 | if (sCgSsaoProgram) cgDestroyProgram(sCgSsaoProgram);
|
---|
| 249 | if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
|
---|
| 250 | if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
|
---|
[2861] | 251 |
|
---|
[2891] | 252 | //DEL_PTR(mNewFbo);
|
---|
| 253 | //DEL_PTR(mOldFbo);
|
---|
| 254 | DEL_PTR(mFbo);
|
---|
[2861] | 255 |
|
---|
| 256 | glDeleteTextures(1, &noiseTex);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 |
|
---|
[2896] | 260 | void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
|
---|
[2875] | 261 | {
|
---|
| 262 | mUseTemporalCoherence = temporal;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
[2896] | 266 | void DeferredRenderer::Init(CGcontext context)
|
---|
[2861] | 267 | {
|
---|
[2873] | 268 | sCgDeferredProgram =
|
---|
[2868] | 269 | cgCreateProgramFromFile(context,
|
---|
| 270 | CG_SOURCE,
|
---|
| 271 | "src/shaders/deferred.cg",
|
---|
| 272 | RenderState::sCgFragmentProfile,
|
---|
[2873] | 273 | "main",
|
---|
[2868] | 274 | NULL);
|
---|
| 275 |
|
---|
[2873] | 276 | if (sCgDeferredProgram != NULL)
|
---|
[2868] | 277 | {
|
---|
[2873] | 278 | cgGLLoadProgram(sCgDeferredProgram);
|
---|
[2868] | 279 |
|
---|
| 280 | // we need size of texture for scaling
|
---|
[2873] | 281 | sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");
|
---|
| 282 | sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");
|
---|
| 283 | sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
|
---|
[2868] | 284 | }
|
---|
| 285 | else
|
---|
| 286 | cerr << "deferred program failed to load" << endl;
|
---|
| 287 |
|
---|
| 288 |
|
---|
[2859] | 289 | ///////////////
|
---|
| 290 |
|
---|
| 291 | sCgSsaoProgram =
|
---|
| 292 | cgCreateProgramFromFile(context,
|
---|
| 293 | CG_SOURCE,
|
---|
[2903] | 294 | #ifdef USE_3D_SSAO
|
---|
[2932] | 295 | "src/shaders/ssao3d.cg",
|
---|
[2903] | 296 | #else
|
---|
| 297 | "src/shaders/ssao.cg",
|
---|
| 298 | #endif
|
---|
[2859] | 299 | RenderState::sCgFragmentProfile,
|
---|
[2868] | 300 | "main",
|
---|
[2859] | 301 | NULL);
|
---|
| 302 |
|
---|
| 303 | if (sCgSsaoProgram != NULL)
|
---|
| 304 | {
|
---|
| 305 | cgGLLoadProgram(sCgSsaoProgram);
|
---|
| 306 |
|
---|
| 307 | sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions");
|
---|
| 308 | sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");
|
---|
| 309 | sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals");
|
---|
| 310 | sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
|
---|
[2868] | 311 |
|
---|
[2859] | 312 | sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
|
---|
[2900] | 313 | sModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "mymodelViewProj");
|
---|
[2859] | 314 | sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
|
---|
[2897] | 315 | sTemporalCoherenceParam = cgGetNamedParameter(sCgSsaoProgram, "temporalCoherence");
|
---|
[2859] | 316 |
|
---|
| 317 | sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");
|
---|
[2873] | 318 |
|
---|
[2884] | 319 | sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
|
---|
[2859] | 320 | }
|
---|
| 321 | else
|
---|
| 322 | cerr << "ssao program failed to load" << endl;
|
---|
| 323 |
|
---|
[2873] | 324 | sCgGiProgram =
|
---|
| 325 | cgCreateProgramFromFile(context,
|
---|
| 326 | CG_SOURCE,
|
---|
| 327 | "src/shaders/globillum.cg",
|
---|
| 328 | RenderState::sCgFragmentProfile,
|
---|
| 329 | "main",
|
---|
| 330 | NULL);
|
---|
| 331 |
|
---|
| 332 | if (sCgGiProgram != NULL)
|
---|
| 333 | {
|
---|
| 334 | cgGLLoadProgram(sCgGiProgram);
|
---|
| 335 |
|
---|
| 336 | // we need size of texture for scaling
|
---|
| 337 | sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");
|
---|
| 338 | sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");
|
---|
| 339 | sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");
|
---|
| 340 | sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
|
---|
| 341 |
|
---|
[2874] | 342 | sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
|
---|
[2873] | 343 | sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
|
---|
[2897] | 344 | sTemporalCoherenceGiParam = cgGetNamedParameter(sCgGiProgram, "temporalCoherence");
|
---|
[2873] | 345 |
|
---|
| 346 | sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
|
---|
| 347 |
|
---|
| 348 | sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");
|
---|
| 349 | sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");
|
---|
| 350 | }
|
---|
| 351 | else
|
---|
| 352 | cerr << "globillum program failed to load" << endl;
|
---|
| 353 |
|
---|
[2880] | 354 | sCgCombinedIllumProgram =
|
---|
| 355 | cgCreateProgramFromFile(context,
|
---|
| 356 | CG_SOURCE,
|
---|
| 357 | "src/shaders/globillum.cg",
|
---|
| 358 | RenderState::sCgFragmentProfile,
|
---|
| 359 | "combine",
|
---|
| 360 | NULL);
|
---|
| 361 |
|
---|
| 362 | if (sCgCombinedIllumProgram != NULL)
|
---|
| 363 | {
|
---|
| 364 | cgGLLoadProgram(sCgCombinedIllumProgram);
|
---|
| 365 |
|
---|
| 366 | sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors");
|
---|
| 367 | sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
|
---|
| 368 | sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
|
---|
| 369 | }
|
---|
| 370 | else
|
---|
| 371 | cerr << "combined illum program failed to load" << endl;
|
---|
| 372 |
|
---|
| 373 |
|
---|
| 374 | sCgCombinedSsaoProgram =
|
---|
| 375 | cgCreateProgramFromFile(context,
|
---|
| 376 | CG_SOURCE,
|
---|
| 377 | "src/shaders/ssao.cg",
|
---|
| 378 | RenderState::sCgFragmentProfile,
|
---|
| 379 | "combine",
|
---|
| 380 | NULL);
|
---|
| 381 |
|
---|
| 382 | if (sCgCombinedSsaoProgram != NULL)
|
---|
| 383 | {
|
---|
| 384 | cgGLLoadProgram(sCgCombinedSsaoProgram);
|
---|
| 385 |
|
---|
| 386 | sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");
|
---|
| 387 | sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
|
---|
| 388 | }
|
---|
| 389 | else
|
---|
| 390 | cerr << "combied illum program failed to load" << endl;
|
---|
| 391 |
|
---|
| 392 |
|
---|
[2865] | 393 | sCgAntiAliasingProgram =
|
---|
| 394 | cgCreateProgramFromFile(context,
|
---|
| 395 | CG_SOURCE,
|
---|
| 396 | "src/shaders/antialiasing.cg",
|
---|
| 397 | RenderState::sCgFragmentProfile,
|
---|
| 398 | "main",
|
---|
| 399 | NULL);
|
---|
| 400 |
|
---|
| 401 | if (sCgAntiAliasingProgram != NULL)
|
---|
| 402 | {
|
---|
| 403 | cgGLLoadProgram(sCgAntiAliasingProgram);
|
---|
| 404 |
|
---|
| 405 | sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");
|
---|
| 406 | sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
|
---|
| 407 | }
|
---|
| 408 | else
|
---|
| 409 | cerr << "antialiasing program failed to load" << endl;
|
---|
| 410 |
|
---|
[2895] | 411 | sCgDeferredShadowProgram =
|
---|
| 412 | cgCreateProgramFromFile(context,
|
---|
| 413 | CG_SOURCE,
|
---|
| 414 | "src/shaders/deferred.cg",
|
---|
| 415 | RenderState::sCgFragmentProfile,
|
---|
| 416 | "main_shadow",
|
---|
| 417 | NULL);
|
---|
| 418 |
|
---|
| 419 | if (sCgDeferredShadowProgram != NULL)
|
---|
| 420 | {
|
---|
| 421 | cgGLLoadProgram(sCgDeferredShadowProgram);
|
---|
| 422 |
|
---|
| 423 | // we need size of texture for scaling
|
---|
| 424 | sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");
|
---|
| 425 | sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");
|
---|
| 426 | sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
|
---|
| 427 |
|
---|
| 428 | sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");
|
---|
| 429 | sMaxDepthShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
|
---|
| 430 | sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
|
---|
| 431 | sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
|
---|
| 432 | }
|
---|
| 433 | else
|
---|
| 434 | cerr << "deferred program failed to load" << endl;
|
---|
[2928] | 435 |
|
---|
| 436 | sCgShadowDebugProgram =
|
---|
| 437 | cgCreateProgramFromFile(context,
|
---|
| 438 | CG_SOURCE,
|
---|
| 439 | "src/shaders/deferred.cg",
|
---|
| 440 | RenderState::sCgFragmentProfile,
|
---|
| 441 | "main_shadow_debug",
|
---|
| 442 | NULL);
|
---|
| 443 |
|
---|
| 444 | if (sCgShadowDebugProgram != NULL)
|
---|
| 445 | {
|
---|
| 446 | cgGLLoadProgram(sCgShadowDebugProgram);
|
---|
| 447 |
|
---|
| 448 | // we need size of texture for scaling
|
---|
| 449 | sShadowMapDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMap");
|
---|
| 450 | sShadowMatrixDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMatrix");
|
---|
| 451 | sColorsTexDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "colorTex");
|
---|
| 452 | }
|
---|
| 453 | else
|
---|
| 454 | cerr << "shadow debug program failed to load" << endl;
|
---|
| 455 |
|
---|
[2859] | 456 | PrintGLerror("init");
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 |
|
---|
[2896] | 460 | void DeferredRenderer::Render(FrameBufferObject *fbo,
|
---|
[2897] | 461 | const Matrix4x4 &oldProjViewMatrix,
|
---|
[2900] | 462 | const Matrix4x4 &projViewMatrix,
|
---|
[2901] | 463 | float tempCohFactor,
|
---|
[2897] | 464 | ShadowMap *shadowMap)
|
---|
[2859] | 465 | {
|
---|
[2868] | 466 | // switch roles of old and new fbo
|
---|
| 467 | // the algorihm uses two input fbos, where the one
|
---|
| 468 | // contais the color buffer from the last frame,
|
---|
| 469 | // the other one will be written
|
---|
[2897] | 470 |
|
---|
| 471 | mFboIndex = 2 - mFboIndex;
|
---|
[2891] | 472 | //swap(mNewFbo, mOldFbo);
|
---|
[2897] | 473 |
|
---|
[2868] | 474 | FrameBufferObject::Release();
|
---|
| 475 |
|
---|
[2867] | 476 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 477 |
|
---|
| 478 | glDisable(GL_ALPHA_TEST);
|
---|
| 479 | glDisable(GL_TEXTURE_2D);
|
---|
| 480 | glDisable(GL_LIGHTING);
|
---|
| 481 |
|
---|
[2880] | 482 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 483 | glViewport(0, 0, mWidth, mHeight);
|
---|
| 484 |
|
---|
[2867] | 485 | glMatrixMode(GL_PROJECTION);
|
---|
| 486 | glPushMatrix();
|
---|
| 487 | glLoadIdentity();
|
---|
| 488 |
|
---|
[2897] | 489 | const float offs = 0.5f;
|
---|
| 490 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
| 491 |
|
---|
[2867] | 492 | glMatrixMode(GL_MODELVIEW);
|
---|
| 493 | glPushMatrix();
|
---|
| 494 | glLoadIdentity();
|
---|
| 495 |
|
---|
[2928] | 496 | if (shadowMap && (mShadingMethod == GI))
|
---|
| 497 | {
|
---|
| 498 | ShadowDebug(fbo, shadowMap);
|
---|
| 499 | }
|
---|
[2895] | 500 | else
|
---|
[2880] | 501 | {
|
---|
[2928] | 502 | if (shadowMap)
|
---|
| 503 | FirstPassShadow(fbo, shadowMap);
|
---|
| 504 | else
|
---|
| 505 | FirstPass(fbo);
|
---|
[2884] | 506 |
|
---|
[2928] | 507 | switch (mShadingMethod)
|
---|
| 508 | {
|
---|
| 509 | case SSAO:
|
---|
| 510 | ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix);
|
---|
| 511 | CombineSsao(fbo);
|
---|
| 512 | break;
|
---|
| 513 | case GI:
|
---|
| 514 | ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix);
|
---|
| 515 | CombineIllum(fbo);
|
---|
| 516 | break;
|
---|
| 517 | default: // DEFAULT
|
---|
| 518 | // do nothing: standard deferred shading
|
---|
| 519 | break;
|
---|
| 520 | }
|
---|
[2867] | 521 |
|
---|
[2928] | 522 | AntiAliasing(fbo);
|
---|
| 523 | }
|
---|
[2867] | 524 | glEnable(GL_LIGHTING);
|
---|
| 525 | glDisable(GL_TEXTURE_2D);
|
---|
| 526 |
|
---|
| 527 | glMatrixMode(GL_PROJECTION);
|
---|
| 528 | glPopMatrix();
|
---|
| 529 |
|
---|
| 530 | glMatrixMode(GL_MODELVIEW);
|
---|
| 531 | glPopMatrix();
|
---|
| 532 |
|
---|
| 533 | glPopAttrib();
|
---|
| 534 |
|
---|
| 535 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2859] | 536 | }
|
---|
| 537 |
|
---|
| 538 |
|
---|
[2896] | 539 | void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo,
|
---|
[2901] | 540 | float tempCohFactor,
|
---|
[2900] | 541 | const Matrix4x4 &oldProjViewMatrix,
|
---|
| 542 | const Matrix4x4 &projViewMatrix
|
---|
| 543 | )
|
---|
[2859] | 544 | {
|
---|
[2903] | 545 | #ifdef USE_3D_SSAO
|
---|
[2900] | 546 | static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
|
---|
| 547 | 0.0f, 0.5f, 0.0f, 0.5f,
|
---|
| 548 | 0.0f, 0.0f, 0.5f, 0.5f,
|
---|
| 549 | 0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1]
|
---|
| 550 |
|
---|
| 551 | Matrix4x4 m = projViewMatrix * biasMatrix;
|
---|
| 552 |
|
---|
| 553 | cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)m.x);
|
---|
[2903] | 554 | #endif
|
---|
[2874] | 555 |
|
---|
[2903] | 556 | cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
|
---|
| 557 |
|
---|
[2879] | 558 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
[2861] | 559 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 560 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[2859] | 561 |
|
---|
[2867] | 562 | if (1)
|
---|
| 563 | {
|
---|
| 564 | // generate mip map levels for position texture
|
---|
| 565 | glBindTexture(GL_TEXTURE_2D, positionsTex);
|
---|
| 566 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 |
|
---|
[2861] | 570 | // read the second buffer, write to the first buffer
|
---|
[2891] | 571 | //mNewFbo->Bind();
|
---|
| 572 | mFbo->Bind();
|
---|
[2899] | 573 | glDrawBuffers(1, mrt + mFboIndex);
|
---|
[2868] | 574 |
|
---|
[2880] | 575 |
|
---|
[2891] | 576 | //GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
|
---|
| 577 | GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
|
---|
[2860] | 578 |
|
---|
[2859] | 579 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 580 |
|
---|
[2868] | 581 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
[2859] | 582 | cgGLBindProgram(sCgSsaoProgram);
|
---|
| 583 |
|
---|
| 584 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
| 585 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
| 586 |
|
---|
| 587 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
| 588 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
| 589 |
|
---|
| 590 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
| 591 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
| 592 |
|
---|
| 593 | cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
|
---|
| 594 | cgGLEnableTextureParameter(sNoiseTexParam);
|
---|
| 595 |
|
---|
| 596 | cgGLSetTextureParameter(sOldTexParam, oldTex);
|
---|
| 597 | cgGLEnableTextureParameter(sOldTexParam);
|
---|
| 598 |
|
---|
| 599 | cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
|
---|
[2875] | 600 |
|
---|
[2901] | 601 | cgGLSetParameter1f(sTemporalCoherenceParam, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
[2893] | 602 |
|
---|
[2895] | 603 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
[2875] | 604 | {
|
---|
[2895] | 605 | mRegenerateSamples = false;
|
---|
[2859] | 606 |
|
---|
[2875] | 607 | // q: should we generate new samples or only rotate the old ones?
|
---|
| 608 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
| 609 | // needs longer to converge
|
---|
[2895] | 610 | GenerateSamples(mSamplingMethod);
|
---|
[2900] | 611 |
|
---|
[2903] | 612 | #ifdef USE_3D_SSAO
|
---|
[2900] | 613 | cgGLSetParameterArray3f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples3);
|
---|
[2903] | 614 | #else
|
---|
| 615 | cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples2);
|
---|
| 616 | #endif
|
---|
[2875] | 617 | }
|
---|
[2859] | 618 |
|
---|
| 619 | Vector3 tl, tr, bl, br;
|
---|
| 620 | ComputeViewVectors(tl, tr, bl, br);
|
---|
| 621 |
|
---|
| 622 | glBegin(GL_QUADS);
|
---|
| 623 |
|
---|
| 624 | // note: slightly larger texture hides ambient occlusion error on border but costs resolution
|
---|
[2860] | 625 | //const float new_offs = 0.55f;
|
---|
| 626 | const float new_offs = 0.5f;
|
---|
[2891] | 627 |
|
---|
[2890] | 628 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
|
---|
| 629 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
|
---|
| 630 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs, new_offs, -0.5f);
|
---|
| 631 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs, new_offs, -0.5f);
|
---|
[2859] | 632 |
|
---|
| 633 | glEnd();
|
---|
| 634 |
|
---|
[2860] | 635 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
| 636 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
| 637 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
| 638 | cgGLDisableTextureParameter(sNoiseTexParam);
|
---|
| 639 | cgGLDisableTextureParameter(sOldTexParam);
|
---|
[2859] | 640 |
|
---|
[2880] | 641 |
|
---|
[2861] | 642 | FrameBufferObject::Release();
|
---|
[2859] | 643 |
|
---|
| 644 | PrintGLerror("ssao first pass");
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 |
|
---|
[2896] | 648 | void DeferredRenderer::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
|
---|
[2860] | 649 | {
|
---|
| 650 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
[2859] | 651 |
|
---|
[2860] | 652 | mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
| 653 |
|
---|
[2888] | 654 | bl = Normalize(nbl - fbl);
|
---|
| 655 | br = Normalize(nbr - fbr);
|
---|
| 656 | tl = Normalize(ntl - ftl);
|
---|
| 657 | tr = Normalize(ntr - ftr);
|
---|
[2860] | 658 | }
|
---|
| 659 |
|
---|
| 660 |
|
---|
| 661 |
|
---|
[2865] | 662 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
| 663 | {
|
---|
| 664 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
| 665 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
| 666 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
| 667 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
| 668 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
| 669 |
|
---|
| 670 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
| 671 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
| 672 |
|
---|
| 673 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 |
|
---|
[2896] | 677 | void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo)
|
---|
[2865] | 678 | {
|
---|
[2895] | 679 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[2865] | 680 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
| 681 |
|
---|
| 682 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 683 |
|
---|
| 684 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 685 | cgGLBindProgram(sCgAntiAliasingProgram);
|
---|
[2868] | 686 |
|
---|
[2865] | 687 | cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
|
---|
| 688 | cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
| 689 |
|
---|
| 690 | cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
|
---|
| 691 | cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
[2868] | 692 |
|
---|
[2865] | 693 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 694 |
|
---|
| 695 | float offs2 = 0.5f;
|
---|
| 696 |
|
---|
| 697 | glBegin(GL_QUADS);
|
---|
| 698 |
|
---|
| 699 | // the neighbouring texels
|
---|
| 700 | float x_offs = 1.0f / mWidth;
|
---|
| 701 | float y_offs = 1.0f / mHeight;
|
---|
| 702 |
|
---|
| 703 | SetVertex(0, 0, x_offs, y_offs);
|
---|
| 704 | SetVertex(1, 0, x_offs, y_offs);
|
---|
| 705 | SetVertex(1, 1, x_offs, y_offs);
|
---|
| 706 | SetVertex(0, 1, x_offs, y_offs);
|
---|
| 707 |
|
---|
| 708 | glEnd();
|
---|
| 709 |
|
---|
| 710 | cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
| 711 | cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
| 712 |
|
---|
[2867] | 713 | PrintGLerror("antialiasing");
|
---|
[2865] | 714 | }
|
---|
| 715 |
|
---|
| 716 |
|
---|
[2896] | 717 | void DeferredRenderer::FirstPass(FrameBufferObject *fbo)
|
---|
[2868] | 718 | {
|
---|
| 719 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
| 720 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 721 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
| 722 |
|
---|
[2879] | 723 | fbo->Bind();
|
---|
[2868] | 724 |
|
---|
[2895] | 725 | colorBufferIdx = 3;
|
---|
[2899] | 726 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
[2879] | 727 |
|
---|
[2868] | 728 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 729 |
|
---|
| 730 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 731 |
|
---|
[2873] | 732 | cgGLBindProgram(sCgDeferredProgram);
|
---|
[2868] | 733 |
|
---|
| 734 | cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
|
---|
| 735 | cgGLEnableTextureParameter(sColorsTexDeferredParam);
|
---|
| 736 |
|
---|
| 737 | cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
|
---|
| 738 | cgGLEnableTextureParameter(sPositionsTexDeferredParam);
|
---|
| 739 |
|
---|
| 740 | cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
|
---|
| 741 | cgGLEnableTextureParameter(sNormalsTexDeferredParam);
|
---|
| 742 |
|
---|
| 743 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 744 |
|
---|
| 745 | const float offs = 0.5f;
|
---|
| 746 |
|
---|
| 747 | glBegin(GL_QUADS);
|
---|
| 748 |
|
---|
| 749 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
| 750 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
| 751 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
| 752 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
| 753 |
|
---|
| 754 | glEnd();
|
---|
| 755 |
|
---|
| 756 | cgGLDisableTextureParameter(sColorsTexDeferredParam);
|
---|
| 757 | cgGLDisableTextureParameter(sPositionsTexDeferredParam);
|
---|
| 758 | cgGLDisableTextureParameter(sNormalsTexDeferredParam);
|
---|
| 759 |
|
---|
| 760 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 761 |
|
---|
| 762 | FrameBufferObject::Release();
|
---|
| 763 |
|
---|
| 764 | PrintGLerror("deferred shading");
|
---|
| 765 | }
|
---|
| 766 |
|
---|
[2869] | 767 |
|
---|
[2928] | 768 |
|
---|
[2896] | 769 | void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,
|
---|
[2901] | 770 | float tempCohFactor,
|
---|
| 771 | const Matrix4x4 &oldProjViewMatrix)
|
---|
[2869] | 772 | {
|
---|
[2874] | 773 | cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
|
---|
| 774 |
|
---|
[2879] | 775 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
[2873] | 776 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 777 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[2869] | 778 |
|
---|
[2873] | 779 | if (1)
|
---|
| 780 | {
|
---|
| 781 | // generate mip map levels for position texture
|
---|
| 782 | glBindTexture(GL_TEXTURE_2D, positionsTex);
|
---|
| 783 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
[2869] | 784 |
|
---|
[2873] | 785 | // generate mip map levels for position texture
|
---|
| 786 | glBindTexture(GL_TEXTURE_2D, colorsTex);
|
---|
| 787 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 |
|
---|
| 791 | // read the second buffer, write to the first buffer
|
---|
[2891] | 792 | mFbo->Bind();
|
---|
[2873] | 793 |
|
---|
[2899] | 794 | glDrawBuffers(2, mrt + mFboIndex);
|
---|
[2873] | 795 |
|
---|
[2891] | 796 | GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
|
---|
| 797 | GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
|
---|
| 798 |
|
---|
[2869] | 799 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 800 |
|
---|
| 801 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
[2873] | 802 | cgGLBindProgram(sCgGiProgram);
|
---|
[2869] | 803 |
|
---|
[2873] | 804 | cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
|
---|
| 805 | cgGLEnableTextureParameter(sPositionsTexGiParam);
|
---|
[2869] | 806 |
|
---|
[2873] | 807 | cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
|
---|
| 808 | cgGLEnableTextureParameter(sColorsTexGiParam);
|
---|
[2869] | 809 |
|
---|
[2873] | 810 | cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
|
---|
| 811 | cgGLEnableTextureParameter(sNormalsTexGiParam);
|
---|
[2869] | 812 |
|
---|
[2873] | 813 | cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
|
---|
| 814 | cgGLEnableTextureParameter(sNoiseTexGiParam);
|
---|
| 815 |
|
---|
| 816 | cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
|
---|
| 817 | cgGLEnableTextureParameter(sOldSsaoTexGiParam);
|
---|
| 818 |
|
---|
| 819 | cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
|
---|
| 820 | cgGLEnableTextureParameter(sOldIllumTexGiParam);
|
---|
| 821 |
|
---|
| 822 | cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
|
---|
| 823 |
|
---|
[2901] | 824 | cgGLSetParameter1f(sTemporalCoherenceGiParam, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
[2873] | 825 |
|
---|
[2897] | 826 |
|
---|
[2895] | 827 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
[2875] | 828 | {
|
---|
[2895] | 829 | mRegenerateSamples = false;
|
---|
[2873] | 830 |
|
---|
[2875] | 831 | // q: should we generate new samples or only rotate the old ones?
|
---|
| 832 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
| 833 | // needs longer to converge
|
---|
[2895] | 834 | GenerateSamples(mSamplingMethod);
|
---|
[2903] | 835 |
|
---|
| 836 | #ifdef USE_3D_SSAO
|
---|
| 837 | cgGLSetParameterArray3f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples3);
|
---|
| 838 | #else
|
---|
[2900] | 839 | cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples2);
|
---|
[2903] | 840 | #endif
|
---|
[2875] | 841 | }
|
---|
| 842 |
|
---|
[2895] | 843 |
|
---|
[2873] | 844 | Vector3 tl, tr, bl, br;
|
---|
| 845 | ComputeViewVectors(tl, tr, bl, br);
|
---|
| 846 |
|
---|
[2869] | 847 | glBegin(GL_QUADS);
|
---|
| 848 |
|
---|
[2873] | 849 | // note: slightly larger texture hides ambient occlusion error on border but costs resolution
|
---|
| 850 | //const float new_offs = 0.55f;
|
---|
| 851 | const float new_offs = 0.5f;
|
---|
[2891] | 852 |
|
---|
| 853 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
|
---|
| 854 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
|
---|
| 855 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs, new_offs, -0.5f);
|
---|
| 856 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs, new_offs, -0.5f);
|
---|
[2869] | 857 |
|
---|
| 858 | glEnd();
|
---|
| 859 |
|
---|
[2873] | 860 | cgGLDisableTextureParameter(sColorsTexGiParam);
|
---|
| 861 | cgGLDisableTextureParameter(sPositionsTexGiParam);
|
---|
| 862 | cgGLDisableTextureParameter(sNormalsTexGiParam);
|
---|
| 863 | cgGLDisableTextureParameter(sNoiseTexGiParam);
|
---|
| 864 | cgGLDisableTextureParameter(sOldSsaoTexGiParam);
|
---|
| 865 | cgGLDisableTextureParameter(sOldIllumTexGiParam);
|
---|
[2869] | 866 |
|
---|
| 867 | FrameBufferObject::Release();
|
---|
| 868 |
|
---|
[2873] | 869 | PrintGLerror("globillum first pass");
|
---|
[2869] | 870 | }
|
---|
| 871 |
|
---|
| 872 |
|
---|
[2896] | 873 | void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
|
---|
[2880] | 874 | {
|
---|
| 875 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
[2884] | 876 |
|
---|
[2891] | 877 | //GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
|
---|
| 878 | //GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
|
---|
| 879 | GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
|
---|
| 880 | GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
|
---|
[2880] | 881 |
|
---|
[2891] | 882 |
|
---|
[2880] | 883 | fbo->Bind();
|
---|
| 884 |
|
---|
[2884] | 885 | // overwrite old color texture
|
---|
[2895] | 886 | colorBufferIdx = 0;
|
---|
[2899] | 887 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
[2880] | 888 |
|
---|
| 889 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 890 |
|
---|
| 891 | cgGLBindProgram(sCgCombinedIllumProgram);
|
---|
| 892 |
|
---|
[2881] | 893 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 894 |
|
---|
| 895 |
|
---|
[2880] | 896 | cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
|
---|
| 897 | cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
|
---|
| 898 |
|
---|
| 899 | cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
|
---|
| 900 | cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
|
---|
| 901 |
|
---|
| 902 | cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
|
---|
| 903 | cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
|
---|
| 904 |
|
---|
| 905 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 906 |
|
---|
| 907 | const float offs = 0.5f;
|
---|
| 908 |
|
---|
| 909 | glBegin(GL_QUADS);
|
---|
| 910 |
|
---|
| 911 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
| 912 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
| 913 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
| 914 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
| 915 |
|
---|
| 916 | glEnd();
|
---|
| 917 |
|
---|
| 918 | cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
|
---|
| 919 | cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
|
---|
| 920 | cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
|
---|
| 921 |
|
---|
| 922 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 923 |
|
---|
| 924 | FrameBufferObject::Release();
|
---|
| 925 |
|
---|
| 926 | PrintGLerror("combine");
|
---|
| 927 | }
|
---|
| 928 |
|
---|
| 929 |
|
---|
[2896] | 930 | void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
|
---|
[2880] | 931 | {
|
---|
| 932 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
[2891] | 933 | GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
|
---|
[2880] | 934 |
|
---|
| 935 | fbo->Bind();
|
---|
| 936 |
|
---|
[2895] | 937 | // overwrite old color texture
|
---|
| 938 | colorBufferIdx = 0;
|
---|
[2899] | 939 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
[2880] | 940 |
|
---|
| 941 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 942 |
|
---|
| 943 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 944 |
|
---|
| 945 | cgGLBindProgram(sCgCombinedSsaoProgram);
|
---|
| 946 |
|
---|
| 947 | cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
|
---|
| 948 | cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
|
---|
| 949 |
|
---|
| 950 | cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
|
---|
| 951 | cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
|
---|
| 952 |
|
---|
| 953 |
|
---|
| 954 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 955 |
|
---|
| 956 | const float offs = 0.5f;
|
---|
| 957 |
|
---|
| 958 | glBegin(GL_QUADS);
|
---|
| 959 |
|
---|
| 960 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
| 961 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
| 962 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
| 963 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
| 964 |
|
---|
| 965 | glEnd();
|
---|
| 966 |
|
---|
| 967 | cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
|
---|
| 968 | cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
|
---|
| 969 |
|
---|
| 970 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
| 971 |
|
---|
| 972 | FrameBufferObject::Release();
|
---|
| 973 |
|
---|
| 974 | PrintGLerror("combine ssao");
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 |
|
---|
[2896] | 978 | void DeferredRenderer::FirstPassShadow(FrameBufferObject *fbo, ShadowMap *shadowMap)
|
---|
[2895] | 979 | {
|
---|
| 980 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
[2880] | 981 |
|
---|
[2895] | 982 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 983 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[2928] | 984 | GLuint shadowTex = shadowMap->GetDepthTexture();
|
---|
[2895] | 985 |
|
---|
| 986 | Matrix4x4 shadowMatrix;
|
---|
| 987 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
| 988 |
|
---|
| 989 | fbo->Bind();
|
---|
| 990 |
|
---|
| 991 | colorBufferIdx = 3;
|
---|
[2899] | 992 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
[2895] | 993 |
|
---|
| 994 |
|
---|
| 995 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 996 |
|
---|
| 997 | cgGLBindProgram(sCgDeferredShadowProgram);
|
---|
| 998 |
|
---|
| 999 | cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
|
---|
| 1000 | cgGLEnableTextureParameter(sColorsTexShadowParam);
|
---|
| 1001 |
|
---|
| 1002 | cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
|
---|
| 1003 | cgGLEnableTextureParameter(sPositionsTexShadowParam);
|
---|
| 1004 |
|
---|
| 1005 | cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
|
---|
| 1006 | cgGLEnableTextureParameter(sNormalsTexShadowParam);
|
---|
| 1007 |
|
---|
| 1008 | cgGLSetTextureParameter(sShadowMapParam, shadowTex);
|
---|
| 1009 | cgGLEnableTextureParameter(sShadowMapParam);
|
---|
| 1010 |
|
---|
| 1011 | cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
|
---|
| 1012 | cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
|
---|
| 1013 |
|
---|
| 1014 | cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
|
---|
| 1015 |
|
---|
| 1016 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 1017 |
|
---|
| 1018 | glBegin(GL_QUADS);
|
---|
| 1019 |
|
---|
| 1020 | float offs2 = 0.5f;
|
---|
| 1021 |
|
---|
| 1022 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 1023 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 1024 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 1025 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
| 1026 |
|
---|
| 1027 | glEnd();
|
---|
| 1028 |
|
---|
| 1029 | cgGLDisableTextureParameter(sColorsTexShadowParam);
|
---|
| 1030 | cgGLDisableTextureParameter(sPositionsTexShadowParam);
|
---|
| 1031 | cgGLDisableTextureParameter(sNormalsTexShadowParam);
|
---|
| 1032 | cgGLDisableTextureParameter(sShadowMapParam);
|
---|
| 1033 |
|
---|
| 1034 | FrameBufferObject::Release();
|
---|
| 1035 |
|
---|
| 1036 | PrintGLerror("deferred shading + shadows");
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 |
|
---|
[2928] | 1040 | void DeferredRenderer::ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap)
|
---|
| 1041 | {
|
---|
| 1042 | GLuint colorsTex = shadowMap->GetShadowColorTexture();
|
---|
| 1043 | GLuint shadowTex = shadowMap->GetDepthTexture();
|
---|
| 1044 |
|
---|
| 1045 |
|
---|
| 1046 | Matrix4x4 shadowMatrix;
|
---|
| 1047 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
| 1048 |
|
---|
| 1049 | //glMatrixMode(GL_PROJECTION);glLoadIdentity();
|
---|
| 1050 | //glViewport(0, 0, shadowMap->GetSize(), shadowMap->GetSize());
|
---|
| 1051 |
|
---|
| 1052 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1053 |
|
---|
| 1054 | cgGLBindProgram(sCgShadowDebugProgram);
|
---|
| 1055 |
|
---|
| 1056 | cgGLSetTextureParameter(sShadowMapDebugParam, shadowTex);
|
---|
| 1057 | cgGLSetTextureParameter(sColorsTexDebugParam, colorsTex);
|
---|
| 1058 |
|
---|
| 1059 | cgGLEnableTextureParameter(sShadowMapDebugParam);
|
---|
| 1060 |
|
---|
| 1061 | cgGLSetMatrixParameterfc(sShadowMatrixDebugParam, (const float *)shadowMatrix.x);
|
---|
| 1062 |
|
---|
| 1063 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
| 1064 |
|
---|
| 1065 | glBegin(GL_QUADS);
|
---|
| 1066 |
|
---|
| 1067 | float offs2 = 0.5f;
|
---|
| 1068 |
|
---|
| 1069 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
| 1070 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
| 1071 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
| 1072 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
| 1073 |
|
---|
| 1074 | glEnd();
|
---|
| 1075 |
|
---|
| 1076 | cgGLDisableTextureParameter(sColorsTexDebugParam);
|
---|
| 1077 | cgGLDisableTextureParameter(sShadowMapDebugParam);
|
---|
| 1078 |
|
---|
| 1079 | PrintGLerror("shadows debug");
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
| 1082 |
|
---|
[2896] | 1083 | void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s)
|
---|
[2895] | 1084 | {
|
---|
| 1085 | if (s != mSamplingMethod)
|
---|
| 1086 | {
|
---|
| 1087 | mSamplingMethod = s;
|
---|
| 1088 | mRegenerateSamples = true;
|
---|
| 1089 | }
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
[2897] | 1092 |
|
---|
| 1093 | void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
|
---|
| 1094 | {
|
---|
| 1095 | if (s != mShadingMethod)
|
---|
| 1096 | {
|
---|
| 1097 | mShadingMethod = s;
|
---|
| 1098 | mRegenerateSamples = true;
|
---|
| 1099 | }
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
[2858] | 1102 | } // namespace
|
---|