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