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