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