[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"
|
---|
[3057] | 11 | #include "ShaderManager.h"
|
---|
[3213] | 12 | #include "Texture.h"
|
---|
| 13 |
|
---|
[3198] | 14 | #include <math.h>
|
---|
[2858] | 15 |
|
---|
[3003] | 16 | #include <IL/il.h>
|
---|
| 17 | #include <assert.h>
|
---|
[2859] | 18 |
|
---|
[3003] | 19 |
|
---|
[3021] | 20 | #ifdef _CRT_SET
|
---|
| 21 | #define _CRTDBG_MAP_ALLOC
|
---|
| 22 | #include <stdlib.h>
|
---|
| 23 | #include <crtdbg.h>
|
---|
| 24 |
|
---|
| 25 | // redefine new operator
|
---|
| 26 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 27 | #define new DEBUG_NEW
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
| 30 |
|
---|
[2858] | 31 | using namespace std;
|
---|
| 32 |
|
---|
| 33 |
|
---|
[3003] | 34 | static void startil()
|
---|
| 35 | {
|
---|
| 36 | ilInit();
|
---|
| 37 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | static void stopil()
|
---|
| 42 | {
|
---|
| 43 | ilShutDown();
|
---|
| 44 | assert(ilGetError() == IL_NO_ERROR);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[3198] | 47 |
|
---|
[2858] | 48 | namespace CHCDemoEngine
|
---|
| 49 | {
|
---|
| 50 |
|
---|
[3026] | 51 | static ShaderProgram *sCgSsaoProgram = NULL;
|
---|
| 52 | static ShaderProgram *sCgGiProgram = NULL;
|
---|
[2873] | 53 |
|
---|
[3026] | 54 | static ShaderProgram *sCgDeferredProgram = NULL;
|
---|
| 55 | static ShaderProgram *sCgAntiAliasingProgram = NULL;
|
---|
| 56 | static ShaderProgram *sCgDeferredShadowProgram = NULL;
|
---|
[2859] | 57 |
|
---|
[3026] | 58 | static ShaderProgram *sCgCombineSsaoProgram = NULL;
|
---|
| 59 | static ShaderProgram *sCgCombineIllumProgram = NULL;
|
---|
| 60 | static ShaderProgram *sCgLogLumProgram = NULL;
|
---|
| 61 | static ShaderProgram *sCgToneProgram = NULL;
|
---|
| 62 | static ShaderProgram *sCgDownSampleProgram = NULL;
|
---|
[3137] | 63 | static ShaderProgram *sCgScaleDepthProgram = NULL;
|
---|
[3155] | 64 | static ShaderProgram *sCgPrepareSsaoProgram = NULL;
|
---|
[3213] | 65 | static ShaderProgram *sCgLenseFlareProgram = NULL;
|
---|
[2869] | 66 |
|
---|
[3232] | 67 | static ShaderProgram *sCgDOFProgram = NULL;
|
---|
[2992] | 68 |
|
---|
[3232] | 69 |
|
---|
[3129] | 70 | static GLuint noiseTex2D = 0;
|
---|
| 71 | static GLuint noiseTex1D = 0;
|
---|
[2865] | 72 |
|
---|
[3128] | 73 |
|
---|
[2859] | 74 | // ssao random spherical samples
|
---|
[3246] | 75 | static Sample2 samples2[NUM_SAMPLES];
|
---|
| 76 | //#define NUM_PRECOMPUTED_SAMPLES 240
|
---|
| 77 | //static Sample2 samples2[NUM_PRECOMPUTED_SAMPLES];
|
---|
[3232] | 78 | // pcf samples
|
---|
[3103] | 79 | static Sample2 pcfSamples[NUM_PCF_TABS];
|
---|
[3232] | 80 | // dof samples
|
---|
| 81 | static Sample2 dofSamples[NUM_DOF_TABS];
|
---|
[2966] | 82 |
|
---|
[3026] | 83 |
|
---|
[3167] | 84 | static float ssaoFilterOffsets[NUM_SSAO_FILTER_SAMPLES * 2];
|
---|
| 85 | static float ssaoFilterWeights[NUM_SSAO_FILTER_SAMPLES];
|
---|
[3103] | 86 |
|
---|
[3214] | 87 | static Texture *sHaloTex[5];
|
---|
[3103] | 88 |
|
---|
[3118] | 89 | int DeferredRenderer::colorBufferIdx = 0;
|
---|
[2859] | 90 |
|
---|
[2992] | 91 |
|
---|
[3212] | 92 |
|
---|
| 93 |
|
---|
[3085] | 94 | /** Helper method that computes the view vectors in the corners of the current view frustum.
|
---|
| 95 | */
|
---|
| 96 | static void ComputeViewVectors(PerspectiveCamera *cam, Vector3 &bl, Vector3 &br, Vector3 &tl, Vector3 &tr)
|
---|
| 97 | {
|
---|
| 98 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
| 99 | cam->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
| 100 |
|
---|
| 101 | bl = Normalize(nbl - fbl);
|
---|
| 102 | br = Normalize(nbr - fbr);
|
---|
| 103 | tl = Normalize(ntl - ftl);
|
---|
| 104 | tr = Normalize(ntr - ftr);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
[3025] | 108 | static float GaussianDistribution(float x, float y, float rho)
|
---|
| 109 | {
|
---|
[3110] | 110 | float g = 1.0f / sqrtf(2.0f * M_PI * rho * rho);
|
---|
[3133] | 111 | g *= expf( -(x * x + y * y) / (2.0f * rho * rho));
|
---|
[3025] | 112 |
|
---|
| 113 | return g;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
[2859] | 117 | static void PrintGLerror(char *msg)
|
---|
| 118 | {
|
---|
| 119 | GLenum errCode;
|
---|
| 120 | const GLubyte *errStr;
|
---|
| 121 |
|
---|
| 122 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 123 | {
|
---|
| 124 | errStr = gluErrorString(errCode);
|
---|
| 125 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
[3206] | 130 | static Sample2 UnitTest(float x, float y, int wi, int he)
|
---|
[3198] | 131 | {
|
---|
| 132 | Sample2 s;
|
---|
| 133 |
|
---|
| 134 | s.x = float(floor(x * (float)wi - 0.5f) + 1.0f) / (float)wi;
|
---|
| 135 | s.y = float(floor(y * (float)he - 0.5f) + 1.0f) / (float)he;
|
---|
| 136 |
|
---|
| 137 | return s;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
[3134] | 141 | static void ComputeSampleOffsets(float *sampleOffsets,
|
---|
| 142 | int imageW, int imageH,
|
---|
| 143 | float width,
|
---|
| 144 | int samples)
|
---|
| 145 | {
|
---|
| 146 | const float xoffs = width / (float)imageW;
|
---|
| 147 | const float yoffs = width / (float)imageH;
|
---|
[3017] | 148 |
|
---|
[3134] | 149 | const int numSamples = (int)sqrt((float)samples);
|
---|
| 150 | const int startSamples = -numSamples / 2;
|
---|
| 151 | const int endSamples = numSamples + startSamples - 1;
|
---|
| 152 | //cout << startSamples << " " << endSamples << endl;
|
---|
[2976] | 153 |
|
---|
[3134] | 154 | int idx = 0;
|
---|
[3017] | 155 |
|
---|
[3134] | 156 | for (int x = startSamples; x <= endSamples; ++ x)
|
---|
[3017] | 157 | {
|
---|
[3134] | 158 | for (int y = startSamples; y <= endSamples; ++ y)
|
---|
[3017] | 159 | {
|
---|
[3134] | 160 | sampleOffsets[idx + 0] = (float)x * xoffs;
|
---|
| 161 | sampleOffsets[idx + 1] = (float)y * yoffs;
|
---|
[3017] | 162 | idx += 2;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[3026] | 167 |
|
---|
[3132] | 168 | void DeferredRenderer::FlipFbos(FrameBufferObject *fbo)
|
---|
| 169 | {
|
---|
| 170 | fbo->Bind();
|
---|
| 171 | colorBufferIdx = 3 - colorBufferIdx;
|
---|
| 172 | glDrawBuffers(1, mrt + colorBufferIdx);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
[3026] | 176 | void DeferredRenderer::DrawQuad(ShaderProgram *p)
|
---|
| 177 | {
|
---|
[3132] | 178 | if (p) p->Bind();
|
---|
[3026] | 179 |
|
---|
[3089] | 180 | // interpolate the view vector
|
---|
[3085] | 181 | Vector3 bl = mCornersView[0];
|
---|
| 182 | Vector3 br = mCornersView[1];
|
---|
| 183 | Vector3 tl = mCornersView[2];
|
---|
| 184 | Vector3 tr = mCornersView[3];
|
---|
| 185 |
|
---|
[3026] | 186 | // note: slightly larger texture could hide ambient occlusion error on border but costs resolution
|
---|
| 187 | glBegin(GL_QUADS);
|
---|
| 188 |
|
---|
[3093] | 189 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex2f( .0f, .0f);
|
---|
| 190 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex2f(1.0f, .0f);
|
---|
| 191 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex2f(1.0f, 1.0f);
|
---|
| 192 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex2f( .0f, 1.0f);
|
---|
[3026] | 193 |
|
---|
| 194 | glEnd();
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 |
|
---|
[2859] | 198 | /** Generate poisson disc distributed sample points on the unit disc
|
---|
| 199 | */
|
---|
[2887] | 200 | static void GenerateSamples(int sampling)
|
---|
[2859] | 201 | {
|
---|
[2887] | 202 | switch (sampling)
|
---|
| 203 | {
|
---|
[2930] | 204 | case DeferredRenderer::SAMPLING_POISSON:
|
---|
[2887] | 205 | {
|
---|
[3227] | 206 | static PoissonDiscSampleGenerator2D poisson(NUM_SAMPLES, 1.0f);
|
---|
[2900] | 207 | poisson.Generate((float *)samples2);
|
---|
[2887] | 208 | }
|
---|
| 209 | break;
|
---|
[2930] | 210 | case DeferredRenderer::SAMPLING_QUADRATIC:
|
---|
[2887] | 211 | {
|
---|
[3246] | 212 | //static QuadraticDiscSampleGenerator2D g(NUM_PRECOMPUTED_SAMPLES, 1.0f);
|
---|
| 213 | static QuadraticDiscSampleGenerator2D g(NUM_SAMPLES, 1.0f);
|
---|
[2930] | 214 | g.Generate((float *)samples2);
|
---|
[2887] | 215 | }
|
---|
| 216 | break;
|
---|
[2930] | 217 | default: // SAMPLING_DEFAULT
|
---|
[3026] | 218 | {
|
---|
[3227] | 219 | static RandomSampleGenerator2D g(NUM_SAMPLES, 1.0f);
|
---|
[3026] | 220 | g.Generate((float *)samples2);
|
---|
| 221 | }
|
---|
[2903] | 222 | }
|
---|
[2859] | 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
[2879] | 226 | static void CreateNoiseTex2D(int w, int h)
|
---|
| 227 | {
|
---|
| 228 | //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
|
---|
[3227] | 229 | Vector3 *randomNormals = new Vector3[w * h];
|
---|
[2879] | 230 |
|
---|
[3227] | 231 | for (int i = 0; i < w * h; ++ i)
|
---|
[2879] | 232 | {
|
---|
[2903] | 233 | // create random samples on a circle
|
---|
[3227] | 234 | const float r = RandomValue(0, 1);
|
---|
[2903] | 235 |
|
---|
[3227] | 236 | const float theta = 2.0f * acos(sqrt(1.0f - r));
|
---|
| 237 | //randomNormals[i] = Vector3(cos(theta), sin(theta), 0);
|
---|
| 238 | randomNormals[i] = Vector3(RandomValue(-M_PI, M_PI), 0, 0);
|
---|
| 239 | //Normalize(randomNormals[i]);
|
---|
[2879] | 240 | }
|
---|
| 241 |
|
---|
[3227] | 242 |
|
---|
[2879] | 243 | glEnable(GL_TEXTURE_2D);
|
---|
[3129] | 244 | glGenTextures(1, &noiseTex2D);
|
---|
| 245 | glBindTexture(GL_TEXTURE_2D, noiseTex2D);
|
---|
[2879] | 246 |
|
---|
[3227] | 247 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 248 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 249 |
|
---|
| 250 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 251 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
[2879] | 252 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
| 253 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
| 254 |
|
---|
[3227] | 255 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, (float *)randomNormals);
|
---|
[2879] | 256 |
|
---|
| 257 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 258 | glDisable(GL_TEXTURE_2D);
|
---|
| 259 |
|
---|
| 260 | delete [] randomNormals;
|
---|
| 261 |
|
---|
| 262 | cout << "created noise texture" << endl;
|
---|
| 263 |
|
---|
| 264 | PrintGLerror("noisetexture");
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 |
|
---|
[3213] | 268 | static void PrepareLenseFlare()
|
---|
| 269 | {
|
---|
[3214] | 270 | string textures[] = {"flare4.tga", "lens2.jpg", "lens3.jpg", "lens4.jpg", "lens1.jpg"};
|
---|
[3213] | 271 |
|
---|
[3270] | 272 | // todo: delete halo textures
|
---|
[3214] | 273 | for (int i = 0; i < 5; ++ i)
|
---|
[3213] | 274 | {
|
---|
| 275 | sHaloTex[i] = new Texture(model_path + textures[i]);
|
---|
| 276 |
|
---|
[3214] | 277 | sHaloTex[i]->SetBoundaryModeS(Texture::BORDER);
|
---|
| 278 | sHaloTex[i]->SetBoundaryModeT(Texture::BORDER);
|
---|
[3213] | 279 |
|
---|
| 280 | sHaloTex[i]->Create();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | cout << "prepared lense flare textures" << endl;
|
---|
| 284 |
|
---|
| 285 | PrintGLerror("prepare lense flare");
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
[3216] | 289 | DeferredRenderer::DeferredRenderer(int w, int h,
|
---|
| 290 | PerspectiveCamera *cam,
|
---|
| 291 | bool ssaoUseFullResolution):
|
---|
[2860] | 292 | mWidth(w), mHeight(h),
|
---|
| 293 | mCamera(cam),
|
---|
[2875] | 294 | mUseTemporalCoherence(true),
|
---|
[2895] | 295 | mRegenerateSamples(true),
|
---|
[2930] | 296 | mSamplingMethod(SAMPLING_POISSON),
|
---|
[2895] | 297 | mShadingMethod(DEFAULT),
|
---|
[3189] | 298 | mIllumFboIndex(0),
|
---|
[3212] | 299 | mSortSamples(true),
|
---|
| 300 | mKernelRadius(1e-8f),
|
---|
[3216] | 301 | mSsaoFilterRadius(12.0f),
|
---|
[3215] | 302 | mSampleIntensity(0.2f),
|
---|
[3219] | 303 | mSunVisiblePixels(0),
|
---|
[3220] | 304 | mSavedFrameNumber(-1),
|
---|
[3235] | 305 | mSavedFrameSuffix(""),
|
---|
[3242] | 306 | mMaxDistance(1e6f),
|
---|
| 307 | mTempCohFactor(.0f),
|
---|
| 308 | mUseToneMapping(false),
|
---|
| 309 | mUseAntiAliasing(false),
|
---|
| 310 | mUseDepthOfField(false)
|
---|
[2861] | 311 | {
|
---|
| 312 | ///////////
|
---|
| 313 | //-- the flip-flop fbos
|
---|
[2859] | 314 |
|
---|
[3216] | 315 | int downSampledWidth, downSampledHeight;
|
---|
| 316 |
|
---|
| 317 | if (ssaoUseFullResolution)
|
---|
| 318 | {
|
---|
| 319 | downSampledWidth = w; downSampledHeight = h;
|
---|
| 320 | cout << "using full resolution ssao" << endl;
|
---|
| 321 | }
|
---|
| 322 | else
|
---|
| 323 | {
|
---|
| 324 | downSampledWidth = w / 2; downSampledHeight = h / 2;
|
---|
| 325 | cout << "using half resolution ssao" << endl;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | mIllumFbo = new FrameBufferObject(downSampledWidth, downSampledHeight, FrameBufferObject::DEPTH_NONE);
|
---|
[3094] | 329 | //mIllumFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
| 330 |
|
---|
[3019] | 331 | mFBOs.push_back(mIllumFbo);
|
---|
[2891] | 332 |
|
---|
[3019] | 333 | for (int i = 0; i < 4; ++ i)
|
---|
| 334 | {
|
---|
[3088] | 335 | mIllumFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
[3117] | 336 | FrameBufferObject::InitBuffer(mIllumFbo, i);
|
---|
[3019] | 337 | }
|
---|
[3002] | 338 |
|
---|
[3117] | 339 |
|
---|
| 340 | ///////////////
|
---|
[3212] | 341 | //-- the downsampled ssao + color bleeding textures:
|
---|
| 342 | //-- as GI is mostly low frequency, we can use lower resolution toimprove performance
|
---|
[3117] | 343 |
|
---|
[3222] | 344 | mDownSampleFbo = new FrameBufferObject(downSampledWidth, downSampledHeight, FrameBufferObject::DEPTH_NONE);
|
---|
[3204] | 345 | // the downsampled color + depth buffer
|
---|
[3002] | 346 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
[3204] | 347 | // downsample buffer for the normal texture
|
---|
[3212] | 348 | mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
|
---|
[3019] | 349 |
|
---|
[3212] | 350 |
|
---|
[3199] | 351 | for (int i = 0; i < 2; ++ i)
|
---|
| 352 | {
|
---|
| 353 | FrameBufferObject::InitBuffer(mDownSampleFbo, i);
|
---|
| 354 | }
|
---|
[3117] | 355 |
|
---|
[3019] | 356 | mFBOs.push_back(mDownSampleFbo);
|
---|
[3038] | 357 |
|
---|
[3084] | 358 | // create noise texture for ssao
|
---|
[3150] | 359 | // for performance reasons we use a smaller texture and repeat it over the screen
|
---|
| 360 | CreateNoiseTex2D(mIllumFbo->GetWidth() / 4, mIllumFbo->GetWidth() / 4);
|
---|
[3227] | 361 | //CreateNoiseTex2D(mIllumFbo->GetWidth(), mIllumFbo->GetWidth());
|
---|
[3192] | 362 |
|
---|
[3085] | 363 | mProjViewMatrix = IdentityMatrix();
|
---|
| 364 | mOldProjViewMatrix = IdentityMatrix();
|
---|
| 365 |
|
---|
| 366 | for (int i = 0; i < 4; ++ i)
|
---|
| 367 | {
|
---|
| 368 | mCornersView[i] = mOldCornersView[i] = Vector3::UNIT_X();
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | mEyePos = mOldEyePos = Vector3::ZERO();
|
---|
| 372 |
|
---|
[3213] | 373 | PrepareLenseFlare();
|
---|
| 374 |
|
---|
[3038] | 375 | InitCg();
|
---|
[2861] | 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
[2896] | 379 | DeferredRenderer::~DeferredRenderer()
|
---|
[2861] | 380 | {
|
---|
[3019] | 381 | CLEAR_CONTAINER(mFBOs);
|
---|
[3206] | 382 |
|
---|
[3129] | 383 | glDeleteTextures(1, &noiseTex2D);
|
---|
| 384 | glDeleteTextures(1, &noiseTex1D);
|
---|
[2861] | 385 | }
|
---|
| 386 |
|
---|
| 387 |
|
---|
[3038] | 388 | void DeferredRenderer::InitCg()
|
---|
[3026] | 389 | {
|
---|
[3057] | 390 | ShaderManager *sm = ShaderManager::GetSingleton();
|
---|
[2873] | 391 |
|
---|
[3216] | 392 | sCgDeferredProgram = sm->CreateFragmentProgram("deferred", "main", "DeferredFrag");
|
---|
| 393 | sCgDeferredShadowProgram = sm->CreateFragmentProgram("deferred", "main_shadow", "DeferredFragShadow");
|
---|
| 394 | sCgSsaoProgram = sm->CreateFragmentProgram("ssao", "main", "SsaoFrag");
|
---|
| 395 | sCgGiProgram = sm->CreateFragmentProgram("globillum", "main", "GiFrag");
|
---|
| 396 | sCgCombineIllumProgram = sm->CreateFragmentProgram("globillum", "combine", "CombineGi");
|
---|
| 397 | sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "CombineSsaoHalfRes", "CombineSsao");
|
---|
| 398 | sCgAntiAliasingProgram = sm->CreateFragmentProgram("antialiasing", "main", "AntiAliasing");
|
---|
| 399 | sCgToneProgram = sm->CreateFragmentProgram("tonemap", "ToneMap", "ToneMap");
|
---|
[3137] | 400 | sCgDownSampleProgram = sm->CreateFragmentProgram("deferred", "Output", "Output");
|
---|
| 401 | sCgScaleDepthProgram = sm->CreateFragmentProgram("deferred", "ScaleDepth", "ScaleDepth");
|
---|
[3216] | 402 | sCgLogLumProgram = sm->CreateFragmentProgram("tonemap", "CalcAvgLogLum", "AvgLogLum");
|
---|
[3155] | 403 | sCgPrepareSsaoProgram = sm->CreateFragmentProgram("deferred", "PrepareSsao", "PrepareSsao");
|
---|
[3213] | 404 | sCgLenseFlareProgram = sm->CreateFragmentProgram("lenseFlare", "LenseFlare", "LenseFlare");
|
---|
[3232] | 405 | sCgDOFProgram = sm->CreateFragmentProgram("depthOfField", "DepthOfField", "DepthOfField");
|
---|
[3038] | 406 |
|
---|
[3034] | 407 |
|
---|
[3104] | 408 | ///////////////////
|
---|
| 409 | //-- initialize program parameters
|
---|
[3034] | 410 |
|
---|
[3104] | 411 | string ssaoParams[] =
|
---|
| 412 | {"colors", "normals", "oldTex", "noiseTex", "temporalCoherence",
|
---|
[3111] | 413 | "samples", "bl", "br", "tl", "tr",
|
---|
| 414 | "modelViewProj", "oldModelViewProj", "oldEyePos", "oldbl", "oldbr",
|
---|
[3212] | 415 | "oldtl", "oldtr", "attribsTex", "kernelRadius", "sampleIntensity"};
|
---|
| 416 | sCgSsaoProgram->AddParameters(ssaoParams, 0, 20);
|
---|
[3085] | 417 |
|
---|
[3104] | 418 | string giParams[] =
|
---|
| 419 | {"colors", "normals", "noiseTex", "oldSsaoTex", "oldIllumTex",
|
---|
[3111] | 420 | "temporalCoherence", "samples", "bl", "br", "tl",
|
---|
| 421 | "tr", "oldModelViewProj", "modelViewProj"};
|
---|
[3232] | 422 |
|
---|
[3104] | 423 | sCgGiProgram->AddParameters(giParams, 0, 13);
|
---|
[3034] | 424 |
|
---|
[3104] | 425 | string toneParams[] = {"colors", "imageKey", "whiteLum", "middleGrey"};
|
---|
| 426 | sCgToneProgram->AddParameters(toneParams, 0, 4);
|
---|
[3034] | 427 |
|
---|
| 428 |
|
---|
[3104] | 429 | ////////////////
|
---|
| 430 |
|
---|
| 431 | string deferredShadowParams[] =
|
---|
| 432 | {"colors", "normals", "shadowMap", "noiseTex", "shadowMatrix",
|
---|
| 433 | "sampleWidth", "lightDir", "eyePos", "samples", "weights"};
|
---|
[3035] | 434 |
|
---|
[3104] | 435 | sCgDeferredShadowProgram->AddParameters(deferredShadowParams, 0, 10);
|
---|
| 436 |
|
---|
| 437 | ////////////////
|
---|
[3034] | 438 |
|
---|
[3104] | 439 | string combineIllumParams[] = {"colorsTex", "ssaoTex", "illumTex"};
|
---|
| 440 | sCgCombineIllumProgram->AddParameters(combineIllumParams, 0, 3);
|
---|
[3035] | 441 |
|
---|
[3104] | 442 | ////////////////
|
---|
[3035] | 443 |
|
---|
[3155] | 444 | string combineSsaoParams[] =
|
---|
[3216] | 445 | {"colorsTex", "normalsTex", "ssaoTex", "filterOffs", "filterWeights",
|
---|
| 446 | "ssaoFilterRadius", "modelViewProj", "bl", "br", "tl",
|
---|
| 447 | "tr", "w", "h"};
|
---|
[3232] | 448 |
|
---|
[3216] | 449 | sCgCombineSsaoProgram->AddParameters(combineSsaoParams, 0, 13);
|
---|
[3035] | 450 |
|
---|
[3104] | 451 | //////////////
|
---|
[3036] | 452 |
|
---|
[3104] | 453 | string deferredParams[] = {"colors", "normals", "lightDir"};
|
---|
| 454 | sCgDeferredProgram->AddParameters(deferredParams, 0, 3);
|
---|
| 455 |
|
---|
| 456 | ///////////////////
|
---|
| 457 |
|
---|
[3136] | 458 | string aaParams[] = {"colors", "normals", "offsets"};
|
---|
| 459 | sCgAntiAliasingProgram->AddParameters(aaParams, 0, 3);
|
---|
[3104] | 460 |
|
---|
| 461 | /////////////////////
|
---|
| 462 |
|
---|
[3137] | 463 | string downSampleParams[] = {"colors"};
|
---|
| 464 | sCgDownSampleProgram->AddParameters(downSampleParams, 0, 1);
|
---|
[3104] | 465 |
|
---|
[3137] | 466 | /////////////////////
|
---|
| 467 |
|
---|
| 468 | string scaleDepthParams[] = {"colors"};
|
---|
| 469 | sCgScaleDepthProgram->AddParameters(scaleDepthParams, 0, 1);
|
---|
| 470 |
|
---|
[3104] | 471 | ////////////
|
---|
| 472 |
|
---|
| 473 | sCgLogLumProgram->AddParameter("colors", 0);
|
---|
[3128] | 474 |
|
---|
| 475 | ////////////////
|
---|
| 476 |
|
---|
[3155] | 477 |
|
---|
| 478 | string prepareSsaoParams[] =
|
---|
[3199] | 479 | {"colorsTex", "normalsTex", "diffVals", "oldTex",
|
---|
[3167] | 480 | "oldEyePos", "modelViewProj", "oldModelViewProj",
|
---|
| 481 | "oldbl", "oldbr", "oldtl", "oldtr"};
|
---|
[3104] | 482 |
|
---|
[3199] | 483 | sCgPrepareSsaoProgram->AddParameters(prepareSsaoParams, 0, 11);
|
---|
[3155] | 484 |
|
---|
| 485 |
|
---|
| 486 | ////////////////
|
---|
[3213] | 487 |
|
---|
| 488 | string lenseFlareParams[] =
|
---|
| 489 | {"colorsTex", "flareTex1", "flareTex2", "flareTex3", "flareTex4",
|
---|
[3215] | 490 | "flareTex5", "vectorToLight", "distanceToLight", "sunVisiblePixels"};
|
---|
[3213] | 491 |
|
---|
[3215] | 492 | sCgLenseFlareProgram->AddParameters(lenseFlareParams, 0, 9);
|
---|
[3213] | 493 |
|
---|
[3232] | 494 |
|
---|
| 495 | ////////////////
|
---|
| 496 |
|
---|
[3235] | 497 | string dofParams[] = {"colorsTex", "filterOffs", "sceneRange", "zFocus"};
|
---|
[3213] | 498 |
|
---|
[3235] | 499 | sCgDOFProgram->AddParameters(dofParams, 0, 4);
|
---|
[3232] | 500 |
|
---|
| 501 |
|
---|
[3213] | 502 | ////////////////
|
---|
[3232] | 503 | //-- prepare filter for ssao
|
---|
[3213] | 504 |
|
---|
[3216] | 505 | PrepareSsaoFilter();
|
---|
[3213] | 506 |
|
---|
[3155] | 507 |
|
---|
[3144] | 508 | /////////
|
---|
[3133] | 509 | //-- pcf tabs for shadowing
|
---|
| 510 |
|
---|
[3026] | 511 | float filterWeights[NUM_PCF_TABS];
|
---|
[3227] | 512 | PoissonDiscSampleGenerator2D poisson2(NUM_PCF_TABS, 1.0f);
|
---|
[3103] | 513 | poisson2.Generate((float *)pcfSamples);
|
---|
[2990] | 514 |
|
---|
[3026] | 515 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
[2880] | 516 | {
|
---|
[3026] | 517 | filterWeights[i] = GaussianDistribution(pcfSamples[i].x, pcfSamples[i].y, 1.0f);
|
---|
[2880] | 518 | }
|
---|
| 519 |
|
---|
[3035] | 520 | sCgDeferredShadowProgram->SetArray2f(8, (float *)pcfSamples, NUM_PCF_TABS);
|
---|
| 521 | sCgDeferredShadowProgram->SetArray1f(9, (float *)filterWeights, NUM_PCF_TABS);
|
---|
[2880] | 522 |
|
---|
[3232] | 523 |
|
---|
| 524 | /////////
|
---|
| 525 | //-- pcf tabs for depth of field
|
---|
| 526 |
|
---|
| 527 | // todo matt: it is stupid to put num samples and width of kernel into constructor => change this!!!
|
---|
| 528 | PoissonDiscSampleGenerator2D poisson3(NUM_DOF_TABS, 1.0f);
|
---|
[3235] | 529 | poisson3.Generate((float *)dofSamples);
|
---|
[3232] | 530 |
|
---|
[3235] | 531 | for (int i = 0; i < NUM_DOF_TABS; ++ i)
|
---|
| 532 | {
|
---|
| 533 | dofSamples[i].x *= 1.0f / mWidth;
|
---|
| 534 | dofSamples[i].y *= 1.0f / mHeight;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[3232] | 537 | //float dofWeights[NUM_PCF_TABS];
|
---|
| 538 | //sCgDOFProgram->SetArray1f(2, (float *)dofWeights, NUM_DOF_TABS);
|
---|
| 539 |
|
---|
[2859] | 540 | PrintGLerror("init");
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 |
|
---|
[2896] | 544 | void DeferredRenderer::Render(FrameBufferObject *fbo,
|
---|
[2952] | 545 | DirectionalLight *light,
|
---|
[2991] | 546 | ShadowMap *shadowMap
|
---|
| 547 | )
|
---|
[2859] | 548 | {
|
---|
[3085] | 549 | InitFrame();
|
---|
| 550 |
|
---|
[2944] | 551 | if (shadowMap)
|
---|
[2952] | 552 | FirstPassShadow(fbo, light, shadowMap);
|
---|
[2895] | 553 | else
|
---|
[2952] | 554 | FirstPass(fbo, light);
|
---|
[2944] | 555 |
|
---|
[3103] | 556 | if (mShadingMethod != 0)
|
---|
[2880] | 557 | {
|
---|
[3214] | 558 | PrepareSsao(fbo); // downsample fbo buffers
|
---|
[3103] | 559 | }
|
---|
[3006] | 560 |
|
---|
[3213] | 561 | // q: use antialiasing before or after ssao?
|
---|
[3193] | 562 | //if (useAntiAliasing) AntiAliasing(fbo, light);
|
---|
[3132] | 563 |
|
---|
[3103] | 564 | switch (mShadingMethod)
|
---|
| 565 | {
|
---|
| 566 | case SSAO:
|
---|
[3242] | 567 | ComputeSsao(fbo, mTempCohFactor);
|
---|
[2944] | 568 | CombineSsao(fbo);
|
---|
| 569 | break;
|
---|
| 570 | case GI:
|
---|
[3242] | 571 | ComputeGlobIllum(fbo, mTempCohFactor);
|
---|
[2944] | 572 | CombineIllum(fbo);
|
---|
| 573 | break;
|
---|
[3214] | 574 | default:
|
---|
[2944] | 575 | // do nothing: standard deferred shading
|
---|
| 576 | break;
|
---|
| 577 | }
|
---|
[2884] | 578 |
|
---|
[3242] | 579 | /// depth of field
|
---|
| 580 | if (mUseDepthOfField)
|
---|
| 581 | {
|
---|
| 582 | DepthOfField(fbo);
|
---|
| 583 | }
|
---|
[3235] | 584 |
|
---|
[3242] | 585 | if (mUseToneMapping)
|
---|
[2991] | 586 | {
|
---|
| 587 | float imageKey, whiteLum, middleGrey;
|
---|
| 588 |
|
---|
| 589 | ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
|
---|
[3007] | 590 | ToneMap(fbo, imageKey, whiteLum, middleGrey);
|
---|
[2991] | 591 | }
|
---|
| 592 |
|
---|
[3215] | 593 | /// compute lense flare
|
---|
| 594 | LenseFlare(fbo, light);
|
---|
[3213] | 595 |
|
---|
[3219] | 596 | const bool saveFrame = (mSavedFrameNumber != -1);
|
---|
| 597 | const bool displayAfterAA = !saveFrame;
|
---|
| 598 |
|
---|
[3175] | 599 | // multisampling is difficult / costly with deferred shading
|
---|
| 600 | // at least do some edge blurring
|
---|
[3242] | 601 | if (mUseAntiAliasing) AntiAliasing(fbo, light, displayAfterAA);
|
---|
[3219] | 602 |
|
---|
| 603 | /// store the current frame
|
---|
| 604 | if (saveFrame) SaveFrame(fbo);
|
---|
[3135] | 605 |
|
---|
[3219] | 606 | // if it hasn't been done yet => just output the latest buffer
|
---|
[3242] | 607 | if (!mUseAntiAliasing || !displayAfterAA)
|
---|
[3219] | 608 | Output(fbo);
|
---|
| 609 |
|
---|
[2867] | 610 | glEnable(GL_LIGHTING);
|
---|
| 611 | glDisable(GL_TEXTURE_2D);
|
---|
| 612 |
|
---|
| 613 | glMatrixMode(GL_PROJECTION);
|
---|
| 614 | glPopMatrix();
|
---|
| 615 |
|
---|
| 616 | glMatrixMode(GL_MODELVIEW);
|
---|
| 617 | glPopMatrix();
|
---|
| 618 |
|
---|
[3089] | 619 | // viewport
|
---|
[2867] | 620 | glPopAttrib();
|
---|
| 621 |
|
---|
[3007] | 622 | FrameBufferObject::Release();
|
---|
[3057] | 623 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
[2859] | 624 | }
|
---|
| 625 |
|
---|
| 626 |
|
---|
[3216] | 627 | void DeferredRenderer::PrepareSsaoFilter()
|
---|
| 628 | {
|
---|
| 629 | const float filterWidth = 1.0f;
|
---|
[3167] | 630 |
|
---|
[3227] | 631 | PoissonDiscSampleGenerator2D poisson(NUM_SSAO_FILTER_SAMPLES, 1.0f);
|
---|
[3216] | 632 | poisson.Generate((float *)ssaoFilterOffsets);
|
---|
| 633 |
|
---|
| 634 | const float xoffs = (float)filterWidth / mWidth;
|
---|
| 635 | const float yoffs = (float)filterWidth / mHeight;
|
---|
| 636 |
|
---|
| 637 | for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
|
---|
| 638 | {
|
---|
| 639 | float x = ssaoFilterOffsets[2 * i + 0];
|
---|
| 640 | float y = ssaoFilterOffsets[2 * i + 1];
|
---|
| 641 |
|
---|
| 642 | ssaoFilterWeights[i] = GaussianDistribution(x, y, 1.0f);
|
---|
| 643 | //ssaoFilterWeights[i] = 1.0f;
|
---|
| 644 |
|
---|
| 645 | ssaoFilterOffsets[2 * i + 0] *= xoffs;
|
---|
| 646 | ssaoFilterOffsets[2 * i + 1] *= yoffs;
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 |
|
---|
[3167] | 651 | static inline float SqrMag(const Sample2 &s)
|
---|
| 652 | {
|
---|
| 653 | return (s.x * s.x + s.y * s.y);
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 |
|
---|
| 657 | static inline float SqrDist(const Sample2 &a, const Sample2 &b)
|
---|
| 658 | {
|
---|
| 659 | float x = a.x - b.x;
|
---|
| 660 | float y = a.y - b.y;
|
---|
| 661 |
|
---|
| 662 | return x * x + y * y;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 | static inline bool lt(const Sample2 &a, const Sample2 &b)
|
---|
| 667 | {
|
---|
| 668 | return SqrMag(a) < SqrMag(b);
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 |
|
---|
| 672 | void DeferredRenderer::SortSamples()
|
---|
| 673 | {
|
---|
| 674 | static Sample2 tempSamples[NUM_SAMPLES];
|
---|
| 675 | static bool checked[NUM_SAMPLES];
|
---|
| 676 |
|
---|
| 677 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
| 678 | checked[i] = false;
|
---|
| 679 |
|
---|
| 680 | Sample2 currentSample;
|
---|
| 681 | currentSample.x = 0; currentSample.y = 0;
|
---|
| 682 | int ns = 0; // the next sample index
|
---|
| 683 |
|
---|
| 684 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
| 685 | {
|
---|
| 686 | float minLen = 1e20f;
|
---|
| 687 |
|
---|
| 688 | for (int j = 0; j < NUM_SAMPLES; ++ j)
|
---|
| 689 | {
|
---|
| 690 | if (checked[j]) continue;
|
---|
| 691 |
|
---|
| 692 | Sample2 s = samples2[j];
|
---|
| 693 | const float len = SqrDist(s, currentSample);
|
---|
| 694 |
|
---|
| 695 | if (len < minLen)
|
---|
| 696 | {
|
---|
| 697 | minLen = len;
|
---|
| 698 | ns = j;
|
---|
| 699 | }
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | tempSamples[i] = samples2[ns];
|
---|
| 703 | currentSample = samples2[ns];
|
---|
| 704 | checked[ns] = true;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
| 708 | samples2[i] = tempSamples[i];
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 |
|
---|
[3199] | 712 | void DeferredRenderer::ComputeSsao(FrameBufferObject *fbo, float tempCohFactor)
|
---|
[2859] | 713 | {
|
---|
[3117] | 714 | GLuint colorsTex, normalsTex, attribsTex;
|
---|
[3015] | 715 |
|
---|
[3133] | 716 | if (0)
|
---|
[3199] | 717 | {
|
---|
[3117] | 718 | colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[3199] | 719 | normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 720 | }
|
---|
[3117] | 721 | else
|
---|
[3199] | 722 | {
|
---|
| 723 | normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
|
---|
[3117] | 724 | colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
|
---|
[3199] | 725 | }
|
---|
| 726 |
|
---|
[3198] | 727 | attribsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[3117] | 728 |
|
---|
[3087] | 729 | // flip flop between illumination buffers
|
---|
[3019] | 730 | GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
[2993] | 731 |
|
---|
[3006] | 732 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[3019] | 733 | glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
|
---|
[3006] | 734 |
|
---|
[2861] | 735 | // read the second buffer, write to the first buffer
|
---|
[3019] | 736 | mIllumFbo->Bind();
|
---|
| 737 | glDrawBuffers(1, mrt + mIllumFboIndex);
|
---|
[2868] | 738 |
|
---|
[3095] | 739 | int i = 0;
|
---|
[3034] | 740 |
|
---|
[3095] | 741 | sCgSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
| 742 | sCgSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
| 743 | sCgSsaoProgram->SetTexture(i ++, oldTex);
|
---|
[3129] | 744 | sCgSsaoProgram->SetTexture(i ++, noiseTex2D);
|
---|
[3095] | 745 |
|
---|
| 746 | sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
[3035] | 747 |
|
---|
[3231] | 748 | static int currentPos = 0;
|
---|
| 749 |
|
---|
[3129] | 750 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
[2875] | 751 | {
|
---|
[2895] | 752 | mRegenerateSamples = false;
|
---|
[2859] | 753 |
|
---|
[2875] | 754 | // q: should we generate new samples or only rotate the old ones?
|
---|
| 755 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
| 756 | // needs longer to converge
|
---|
[3246] | 757 | //if (currentPos + NUM_SAMPLES >= NUM_PRECOMPUTED_SAMPLES) {
|
---|
[3231] | 758 | currentPos = 0;
|
---|
| 759 | GenerateSamples(mSamplingMethod);
|
---|
[3246] | 760 | //}
|
---|
[3167] | 761 |
|
---|
[3192] | 762 | //if (mSortSamples) { SortSamples(); }
|
---|
[3231] | 763 | sCgSsaoProgram->SetArray2f(i, (float *)samples2 + currentPos, NUM_SAMPLES);
|
---|
| 764 |
|
---|
| 765 | currentPos += NUM_SAMPLES;
|
---|
[2875] | 766 | }
|
---|
[3085] | 767 |
|
---|
[3095] | 768 | ++ i;
|
---|
[2859] | 769 |
|
---|
[3095] | 770 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
| 771 | sCgSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
|
---|
[2987] | 772 |
|
---|
[3095] | 773 | sCgSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
| 774 | sCgSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
[3062] | 775 |
|
---|
[3105] | 776 | Vector3 de;
|
---|
| 777 | de.x = mOldEyePos.x - mEyePos.x;
|
---|
| 778 | de.y = mOldEyePos.y - mEyePos.y;
|
---|
| 779 | de.z = mOldEyePos.z - mEyePos.z;
|
---|
[3035] | 780 |
|
---|
[3095] | 781 | sCgSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
|
---|
[3093] | 782 |
|
---|
[3095] | 783 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
[3212] | 784 | {
|
---|
[3095] | 785 | sCgSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
|
---|
[3212] | 786 | }
|
---|
[3085] | 787 |
|
---|
[3110] | 788 | sCgSsaoProgram->SetTexture(i ++, attribsTex);
|
---|
[3212] | 789 | sCgSsaoProgram->SetValue1f(i ++, mKernelRadius);
|
---|
[3227] | 790 | sCgSsaoProgram->SetValue1f(i ++, mSampleIntensity * mKernelRadius);
|
---|
[3110] | 791 |
|
---|
[3129] | 792 |
|
---|
[3026] | 793 | DrawQuad(sCgSsaoProgram);
|
---|
[2987] | 794 |
|
---|
[3006] | 795 | glPopAttrib();
|
---|
[2859] | 796 |
|
---|
| 797 | PrintGLerror("ssao first pass");
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 |
|
---|
[2865] | 801 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
| 802 | {
|
---|
| 803 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
| 804 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
| 805 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
| 806 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
| 807 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
| 808 |
|
---|
| 809 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
| 810 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
| 811 |
|
---|
[3089] | 812 | //glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
| 813 | glVertex2f(x, y);
|
---|
[2865] | 814 | }
|
---|
| 815 |
|
---|
| 816 |
|
---|
[3219] | 817 | void DeferredRenderer::AntiAliasing(FrameBufferObject *fbo,
|
---|
| 818 | DirectionalLight *light,
|
---|
| 819 | bool displayFrame)
|
---|
[2865] | 820 | {
|
---|
[2968] | 821 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
| 822 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
[3136] | 823 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
[3007] | 824 |
|
---|
[3132] | 825 | // read the second buffer, write to the first buffer
|
---|
[3219] | 826 | if (!displayFrame)
|
---|
| 827 | FlipFbos(fbo);
|
---|
| 828 | else
|
---|
| 829 | // end of the pipeline => just draw image to screen
|
---|
| 830 | FrameBufferObject::Release();
|
---|
[3132] | 831 |
|
---|
[3136] | 832 | // the neighbouring texels
|
---|
| 833 | float xOffs = 1.0f / fbo->GetWidth();
|
---|
| 834 | float yOffs = 1.0f / fbo->GetHeight();
|
---|
| 835 |
|
---|
[3034] | 836 | sCgAntiAliasingProgram->SetTexture(0, colorsTex);
|
---|
| 837 | sCgAntiAliasingProgram->SetTexture(1, normalsTex);
|
---|
[2865] | 838 |
|
---|
[3136] | 839 | float offsets[16];
|
---|
| 840 | int i = 0;
|
---|
[2868] | 841 |
|
---|
[3136] | 842 | offsets[i] = -xOffs; offsets[i + 1] = yOffs; i += 2; // left top
|
---|
| 843 | offsets[i] = xOffs; offsets[i + 1] = -yOffs; i += 2; // right bottom
|
---|
| 844 | offsets[i] = xOffs; offsets[i + 1] = yOffs; i += 2; // right top
|
---|
| 845 | offsets[i] = -xOffs; offsets[i + 1] = -yOffs; i += 2; // left bottom
|
---|
| 846 | offsets[i] = -xOffs; offsets[i + 1] = .0f; i += 2; // left
|
---|
| 847 | offsets[i] = xOffs; offsets[i + 1] = .0f; i += 2; // right
|
---|
| 848 | offsets[i] = .0f; offsets[i + 1] = yOffs; i += 2; // top
|
---|
| 849 | offsets[i] = .0f; offsets[i + 1] = -yOffs; i += 2; // bottom
|
---|
[2865] | 850 |
|
---|
[3136] | 851 | sCgAntiAliasingProgram->SetArray2f(2, offsets, 8);
|
---|
[2865] | 852 |
|
---|
[3136] | 853 | DrawQuad(sCgAntiAliasingProgram);
|
---|
[2865] | 854 |
|
---|
[2867] | 855 | PrintGLerror("antialiasing");
|
---|
[2865] | 856 | }
|
---|
| 857 |
|
---|
| 858 |
|
---|
[2952] | 859 | void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)
|
---|
[2868] | 860 | {
|
---|
[2973] | 861 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[3009] | 862 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
[2868] | 863 |
|
---|
[3132] | 864 | FlipFbos(fbo);
|
---|
[2868] | 865 |
|
---|
[3026] | 866 | const Vector3 lightDir = -light->GetDirection();
|
---|
[2868] | 867 |
|
---|
[3034] | 868 | sCgDeferredProgram->SetTexture(0, colorsTex);
|
---|
| 869 | sCgDeferredProgram->SetTexture(1, normalsTex);
|
---|
| 870 | sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z);
|
---|
[2868] | 871 |
|
---|
[3026] | 872 | DrawQuad(sCgDeferredProgram);
|
---|
[2952] | 873 |
|
---|
[2868] | 874 | PrintGLerror("deferred shading");
|
---|
| 875 | }
|
---|
| 876 |
|
---|
[2869] | 877 |
|
---|
[2896] | 878 | void DeferredRenderer::ComputeGlobIllum(FrameBufferObject *fbo,
|
---|
[3085] | 879 | float tempCohFactor)
|
---|
[2869] | 880 | {
|
---|
[3016] | 881 | #if 0
|
---|
| 882 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
| 883 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
| 884 | #else
|
---|
[3003] | 885 | GLuint colorsTex = mDownSampleFbo->GetColorBuffer(0)->GetTexture();
|
---|
[3009] | 886 | GLuint normalsTex = mDownSampleFbo->GetColorBuffer(1)->GetTexture();
|
---|
[3016] | 887 | #endif
|
---|
[2869] | 888 |
|
---|
[3006] | 889 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
[3019] | 890 | glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
|
---|
[2869] | 891 |
|
---|
[2873] | 892 | // read the second buffer, write to the first buffer
|
---|
[3019] | 893 | mIllumFbo->Bind();
|
---|
[2873] | 894 |
|
---|
[3019] | 895 | glDrawBuffers(2, mrt + mIllumFboIndex);
|
---|
[2873] | 896 |
|
---|
[3019] | 897 | GLuint oldSsaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
| 898 | GLuint oldIllumTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture();
|
---|
[2869] | 899 |
|
---|
[3160] | 900 | int i = 0;
|
---|
[2869] | 901 |
|
---|
[3160] | 902 | sCgGiProgram->SetTexture(i ++, colorsTex);
|
---|
| 903 | sCgGiProgram->SetTexture(i ++, normalsTex);
|
---|
| 904 | sCgGiProgram->SetTexture(i ++, noiseTex2D);
|
---|
| 905 | sCgGiProgram->SetTexture(i ++, oldSsaoTex);
|
---|
| 906 | sCgGiProgram->SetTexture(i ++, oldIllumTex);
|
---|
| 907 |
|
---|
| 908 | sCgGiProgram->SetValue1f(i ++,
|
---|
[3026] | 909 | (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
|
---|
[2869] | 910 |
|
---|
[2895] | 911 | if (mUseTemporalCoherence || mRegenerateSamples)
|
---|
[2875] | 912 | {
|
---|
[2895] | 913 | mRegenerateSamples = false;
|
---|
[2873] | 914 |
|
---|
[2875] | 915 | // q: should we generate new samples or only rotate the old ones?
|
---|
| 916 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
| 917 | // needs longer to converge
|
---|
[2895] | 918 | GenerateSamples(mSamplingMethod);
|
---|
[2903] | 919 |
|
---|
[3160] | 920 | sCgGiProgram->SetArray2f(i ++, (float *)samples2, NUM_SAMPLES);
|
---|
[2875] | 921 | }
|
---|
| 922 |
|
---|
[3085] | 923 | Vector3 bl = mCornersView[0];
|
---|
| 924 | Vector3 br = mCornersView[1];
|
---|
| 925 | Vector3 tl = mCornersView[2];
|
---|
| 926 | Vector3 tr = mCornersView[3];
|
---|
[2895] | 927 |
|
---|
[3160] | 928 | sCgGiProgram->SetValue3f(i ++, bl.x, bl.y, bl.z);
|
---|
| 929 | sCgGiProgram->SetValue3f(i ++, br.x, br.y, br.z);
|
---|
| 930 | sCgGiProgram->SetValue3f(i ++, tl.x, tl.y, tl.z);
|
---|
| 931 | sCgGiProgram->SetValue3f(i ++, tr.x, tr.y, tr.z);
|
---|
[2873] | 932 |
|
---|
[3160] | 933 | sCgGiProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
| 934 | sCgGiProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
[3035] | 935 |
|
---|
| 936 |
|
---|
[3026] | 937 | DrawQuad(sCgGiProgram);
|
---|
[2990] | 938 |
|
---|
[3006] | 939 | glPopAttrib();
|
---|
| 940 |
|
---|
[2873] | 941 | PrintGLerror("globillum first pass");
|
---|
[2869] | 942 | }
|
---|
| 943 |
|
---|
| 944 |
|
---|
[2896] | 945 | void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
|
---|
[2880] | 946 | {
|
---|
[2973] | 947 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[2884] | 948 |
|
---|
[3019] | 949 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
| 950 | GLuint illumTex = mIllumFbo->GetColorBuffer(mIllumFboIndex + 1)->GetTexture();
|
---|
[2880] | 951 |
|
---|
[3132] | 952 | FlipFbos(fbo);
|
---|
[2891] | 953 |
|
---|
[3036] | 954 | sCgCombineIllumProgram->SetTexture(0, colorsTex);
|
---|
| 955 | sCgCombineIllumProgram->SetTexture(1, ssaoTex);
|
---|
| 956 | sCgCombineIllumProgram->SetTexture(2, illumTex);
|
---|
[2880] | 957 |
|
---|
[3026] | 958 | DrawQuad(sCgCombineIllumProgram);
|
---|
[2880] | 959 |
|
---|
| 960 | PrintGLerror("combine");
|
---|
| 961 | }
|
---|
| 962 |
|
---|
| 963 |
|
---|
[2896] | 964 | void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
|
---|
[2880] | 965 | {
|
---|
[2973] | 966 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[3103] | 967 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
[3144] | 968 | GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
|
---|
[3132] | 969 |
|
---|
| 970 | FlipFbos(fbo);
|
---|
[2880] | 971 |
|
---|
[3104] | 972 | int i = 0;
|
---|
[3148] | 973 |
|
---|
[3104] | 974 | sCgCombineSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
[3120] | 975 | sCgCombineSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
[3104] | 976 | sCgCombineSsaoProgram->SetTexture(i ++, ssaoTex);
|
---|
[3017] | 977 |
|
---|
[3167] | 978 | sCgCombineSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTER_SAMPLES);
|
---|
| 979 | sCgCombineSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTER_SAMPLES);
|
---|
[3216] | 980 | sCgCombineSsaoProgram->SetValue1f(i ++, mSsaoFilterRadius);
|
---|
| 981 |
|
---|
[3160] | 982 | sCgCombineSsaoProgram->SetMatrix(i++, mProjViewMatrix);
|
---|
[3134] | 983 |
|
---|
[3163] | 984 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
| 985 | sCgCombineSsaoProgram->SetValue3f(i, mCornersView[j].x, mCornersView[j].y, mCornersView[j].z);
|
---|
[3134] | 986 |
|
---|
[3205] | 987 | sCgCombineSsaoProgram->SetValue1f(i ++, mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetWidth());
|
---|
| 988 | sCgCombineSsaoProgram->SetValue1f(i ++, mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetHeight());
|
---|
| 989 |
|
---|
[3026] | 990 | DrawQuad(sCgCombineSsaoProgram);
|
---|
[2974] | 991 |
|
---|
[2880] | 992 | PrintGLerror("combine ssao");
|
---|
| 993 | }
|
---|
| 994 |
|
---|
| 995 |
|
---|
[2952] | 996 | void DeferredRenderer::FirstPassShadow(FrameBufferObject *fbo,
|
---|
| 997 | DirectionalLight *light,
|
---|
| 998 | ShadowMap *shadowMap)
|
---|
[2895] | 999 | {
|
---|
[2973] | 1000 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[3009] | 1001 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
[2973] | 1002 |
|
---|
[2928] | 1003 | GLuint shadowTex = shadowMap->GetDepthTexture();
|
---|
[2895] | 1004 |
|
---|
| 1005 | Matrix4x4 shadowMatrix;
|
---|
| 1006 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
| 1007 |
|
---|
| 1008 |
|
---|
[3132] | 1009 | FlipFbos(fbo);
|
---|
| 1010 |
|
---|
[3035] | 1011 | sCgDeferredShadowProgram->SetTexture(0, colorsTex);
|
---|
| 1012 | sCgDeferredShadowProgram->SetTexture(1, normalsTex);
|
---|
| 1013 | sCgDeferredShadowProgram->SetTexture(2, shadowTex);
|
---|
[3129] | 1014 | sCgDeferredShadowProgram->SetTexture(3, noiseTex2D);
|
---|
[3035] | 1015 | sCgDeferredShadowProgram->SetMatrix(4, shadowMatrix);
|
---|
| 1016 | sCgDeferredShadowProgram->SetValue1f(5, 2.0f / shadowMap->GetSize());
|
---|
[3027] | 1017 |
|
---|
[3026] | 1018 | const Vector3 lightDir = -light->GetDirection();
|
---|
[3035] | 1019 | sCgDeferredShadowProgram->SetValue3f(6, lightDir.x, lightDir.y, lightDir.z);
|
---|
[3085] | 1020 | sCgDeferredShadowProgram->SetValue3f(7, mEyePos.x, mEyePos.y, mEyePos.z);
|
---|
[2952] | 1021 |
|
---|
[3026] | 1022 | DrawQuad(sCgDeferredShadowProgram);
|
---|
[2895] | 1023 |
|
---|
| 1024 | PrintGLerror("deferred shading + shadows");
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 |
|
---|
[2973] | 1028 | void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo,
|
---|
| 1029 | DirectionalLight *light,
|
---|
| 1030 | float &imageKey,
|
---|
| 1031 | float &whiteLum,
|
---|
| 1032 | float &middleGrey)
|
---|
[2972] | 1033 | {
|
---|
[2975] | 1034 | // hack: estimate value where sky burns out
|
---|
[3010] | 1035 | whiteLum = log(WHITE_LUMINANCE);
|
---|
[2973] | 1036 |
|
---|
[2975] | 1037 | ////////////////////
|
---|
| 1038 | //-- linear interpolate brightness key depending on the current sun position
|
---|
[2973] | 1039 |
|
---|
| 1040 | const float minKey = 0.09f;
|
---|
[3020] | 1041 | const float maxKey = 0.36f;
|
---|
[2973] | 1042 |
|
---|
| 1043 | const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
|
---|
| 1044 | middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
|
---|
[2991] | 1045 |
|
---|
[2993] | 1046 |
|
---|
[2991] | 1047 | //////////
|
---|
| 1048 | //-- compute avg loglum
|
---|
| 1049 |
|
---|
| 1050 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
[2992] | 1051 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
[2991] | 1052 |
|
---|
[3132] | 1053 | FlipFbos(fbo);
|
---|
[2991] | 1054 |
|
---|
[3035] | 1055 | sCgLogLumProgram->SetTexture(0, colorsTex);
|
---|
[3026] | 1056 | DrawQuad(sCgLogLumProgram);
|
---|
[2991] | 1057 |
|
---|
| 1058 | PrintGLerror("ToneMapParams");
|
---|
| 1059 |
|
---|
| 1060 |
|
---|
| 1061 | ///////////////////
|
---|
| 1062 | //-- compute avg loglum in scene using mipmapping
|
---|
| 1063 |
|
---|
[3008] | 1064 | glBindTexture(GL_TEXTURE_2D, fbo->GetColorBuffer(colorBufferIdx)->GetTexture());
|
---|
| 1065 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
[2972] | 1066 | }
|
---|
| 1067 |
|
---|
| 1068 |
|
---|
[3003] | 1069 | static void ExportData(float *data, int w, int h)
|
---|
| 1070 | {
|
---|
| 1071 | startil();
|
---|
| 1072 |
|
---|
| 1073 | cout << "w: " << w << " h: " << h << endl;
|
---|
| 1074 | ILstring filename = ILstring("downsample2.jpg");
|
---|
| 1075 | ilRegisterType(IL_FLOAT);
|
---|
| 1076 |
|
---|
| 1077 | const int depth = 1;
|
---|
| 1078 | const int bpp = 4;
|
---|
| 1079 |
|
---|
| 1080 | if (!ilTexImage(w, h, depth, bpp, IL_RGBA, IL_FLOAT, data))
|
---|
| 1081 | {
|
---|
| 1082 | cerr << "IL error " << ilGetError() << endl;
|
---|
| 1083 | stopil();
|
---|
| 1084 | return;
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
| 1087 | if (!ilSaveImage(filename))
|
---|
| 1088 | {
|
---|
| 1089 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 | stopil();
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 |
|
---|
[3155] | 1096 | void DeferredRenderer::PrepareSsao(FrameBufferObject *fbo)
|
---|
| 1097 | {
|
---|
[3198] | 1098 | GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
|
---|
[3199] | 1099 | GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
[3198] | 1100 | GLuint diffVals = fbo->GetColorBuffer(2)->GetTexture();
|
---|
[3155] | 1101 | // flip flop between illumination buffers
|
---|
[3198] | 1102 | GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
|
---|
[3155] | 1103 |
|
---|
| 1104 | int i = 0;
|
---|
| 1105 |
|
---|
| 1106 | sCgPrepareSsaoProgram->SetTexture(i ++, colorsTex);
|
---|
[3199] | 1107 | sCgPrepareSsaoProgram->SetTexture(i ++, normalsTex);
|
---|
[3167] | 1108 | sCgPrepareSsaoProgram->SetTexture(i ++, diffVals);
|
---|
[3155] | 1109 | sCgPrepareSsaoProgram->SetTexture(i ++, oldTex);
|
---|
| 1110 |
|
---|
| 1111 | Vector3 de;
|
---|
| 1112 | de.x = mOldEyePos.x - mEyePos.x;
|
---|
| 1113 | de.y = mOldEyePos.y - mEyePos.y;
|
---|
| 1114 | de.z = mOldEyePos.z - mEyePos.z;
|
---|
| 1115 |
|
---|
| 1116 | sCgPrepareSsaoProgram->SetValue3f(i ++, de.x, de.y, de.z);
|
---|
| 1117 |
|
---|
[3156] | 1118 | sCgPrepareSsaoProgram->SetMatrix(i ++, mProjViewMatrix);
|
---|
| 1119 | sCgPrepareSsaoProgram->SetMatrix(i ++, mOldProjViewMatrix);
|
---|
| 1120 |
|
---|
| 1121 | for (int j = 0; j < 4; ++ j, ++ i)
|
---|
| 1122 | sCgPrepareSsaoProgram->SetValue3f(i, mOldCornersView[j].x, mOldCornersView[j].y, mOldCornersView[j].z);
|
---|
| 1123 |
|
---|
[3155] | 1124 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1125 | glViewport(0, 0, mDownSampleFbo->GetWidth(), mDownSampleFbo->GetHeight());
|
---|
| 1126 |
|
---|
| 1127 | mDownSampleFbo->Bind();
|
---|
| 1128 |
|
---|
[3212] | 1129 | // prepare downsampled depth and normal texture for ssao
|
---|
[3199] | 1130 | glDrawBuffers(2, mrt);
|
---|
[3155] | 1131 |
|
---|
| 1132 | DrawQuad(sCgPrepareSsaoProgram);
|
---|
| 1133 |
|
---|
| 1134 | glPopAttrib();
|
---|
| 1135 | PrintGLerror("prepareSsao");
|
---|
| 1136 | }
|
---|
| 1137 |
|
---|
| 1138 |
|
---|
[3103] | 1139 | void DeferredRenderer::DownSample(FrameBufferObject *fbo,
|
---|
| 1140 | int bufferIdx,
|
---|
[3006] | 1141 | FrameBufferObject *downSampleFbo,
|
---|
[3137] | 1142 | int downSampleBufferIdx,
|
---|
| 1143 | ShaderProgram *program)
|
---|
[2972] | 1144 | {
|
---|
[3137] | 1145 | ColorBufferObject *buffer = fbo->GetColorBuffer(bufferIdx);
|
---|
| 1146 | GLuint tex = buffer->GetTexture();
|
---|
[3006] | 1147 |
|
---|
| 1148 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1149 | glViewport(0, 0, downSampleFbo->GetWidth(), downSampleFbo->GetHeight());
|
---|
[2972] | 1150 |
|
---|
[3137] | 1151 | downSampleFbo->Bind();
|
---|
[2994] | 1152 |
|
---|
[3137] | 1153 | program->SetTexture(0, tex);
|
---|
[3006] | 1154 | glDrawBuffers(1, mrt + downSampleBufferIdx);
|
---|
[2994] | 1155 |
|
---|
[3137] | 1156 | DrawQuad(program);
|
---|
[3005] | 1157 |
|
---|
[3006] | 1158 | glPopAttrib();
|
---|
[3005] | 1159 | PrintGLerror("downsample");
|
---|
[2972] | 1160 | }
|
---|
| 1161 |
|
---|
| 1162 |
|
---|
[2973] | 1163 | void DeferredRenderer::ToneMap(FrameBufferObject *fbo,
|
---|
| 1164 | float imageKey,
|
---|
| 1165 | float whiteLum,
|
---|
| 1166 | float middleGrey)
|
---|
[2972] | 1167 | {
|
---|
| 1168 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
[2994] | 1169 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
[3132] | 1170 | //FrameBufferObject::Release();
|
---|
[2972] | 1171 |
|
---|
[3132] | 1172 | FlipFbos(fbo);
|
---|
[3007] | 1173 |
|
---|
[3035] | 1174 | sCgToneProgram->SetTexture(0, colorsTex);
|
---|
| 1175 | sCgToneProgram->SetValue1f(1, imageKey);
|
---|
| 1176 | sCgToneProgram->SetValue1f(2, whiteLum);
|
---|
| 1177 | sCgToneProgram->SetValue1f(3, middleGrey);
|
---|
[3007] | 1178 |
|
---|
[3026] | 1179 | DrawQuad(sCgToneProgram);
|
---|
[2972] | 1180 |
|
---|
[2973] | 1181 | PrintGLerror("ToneMap");
|
---|
[2972] | 1182 | }
|
---|
| 1183 |
|
---|
| 1184 |
|
---|
[3132] | 1185 | void DeferredRenderer::Output(FrameBufferObject *fbo)
|
---|
| 1186 | {
|
---|
| 1187 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1188 | glViewport(0, 0, fbo->GetWidth(), fbo->GetHeight());
|
---|
| 1189 |
|
---|
| 1190 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
| 1191 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
| 1192 |
|
---|
| 1193 | sCgDownSampleProgram->SetTexture(0, colorsTex);
|
---|
| 1194 |
|
---|
| 1195 | FrameBufferObject::Release();
|
---|
| 1196 | DrawQuad(sCgDownSampleProgram);
|
---|
| 1197 |
|
---|
[3134] | 1198 | PrintGLerror("output");
|
---|
[3132] | 1199 | }
|
---|
| 1200 |
|
---|
| 1201 |
|
---|
[3085] | 1202 | void DeferredRenderer::InitFrame()
|
---|
| 1203 | {
|
---|
[3095] | 1204 | for (int i = 0; i < 4; ++ i)
|
---|
| 1205 | mOldCornersView[i] = mCornersView[i];
|
---|
| 1206 |
|
---|
[3085] | 1207 | mOldProjViewMatrix = mProjViewMatrix;
|
---|
[3095] | 1208 | mOldEyePos = mEyePos;
|
---|
[3106] | 1209 | mEyePos = mCamera->GetPosition();
|
---|
[3081] | 1210 |
|
---|
[3104] | 1211 | // hack: temporarily change far to improve precision
|
---|
| 1212 | const float oldFar = mCamera->GetFar();
|
---|
[3106] | 1213 | const float oldNear = mCamera->GetNear();
|
---|
[3132] | 1214 |
|
---|
[3104] | 1215 |
|
---|
[3106] | 1216 | Matrix4x4 matViewing, matProjection;
|
---|
[3104] | 1217 |
|
---|
[3106] | 1218 |
|
---|
| 1219 | ///////////////////
|
---|
| 1220 |
|
---|
[3213] | 1221 | // use view orientation as we assume that the eye point is always in the center
|
---|
| 1222 | // of our coordinate system, this way we have the highest precision near the eye point
|
---|
[3093] | 1223 | mCamera->GetViewOrientationMatrix(matViewing);
|
---|
[3085] | 1224 | mCamera->GetProjectionMatrix(matProjection);
|
---|
| 1225 |
|
---|
| 1226 | mProjViewMatrix = matViewing * matProjection;
|
---|
[3095] | 1227 | ComputeViewVectors(mCamera, mCornersView[0], mCornersView[1], mCornersView[2], mCornersView[3]);
|
---|
[3106] | 1228 |
|
---|
[3085] | 1229 |
|
---|
[3095] | 1230 | // switch roles of old and new fbo
|
---|
| 1231 | // the algorihm uses two input fbos, where the one
|
---|
| 1232 | // contais the color buffer from the last frame,
|
---|
| 1233 | // the other one will be written
|
---|
[3085] | 1234 |
|
---|
[3095] | 1235 | mIllumFboIndex = 2 - mIllumFboIndex;
|
---|
| 1236 |
|
---|
| 1237 | // enable fragment shading
|
---|
| 1238 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
| 1239 |
|
---|
| 1240 | glDisable(GL_ALPHA_TEST);
|
---|
| 1241 | glDisable(GL_TEXTURE_2D);
|
---|
| 1242 | glDisable(GL_LIGHTING);
|
---|
| 1243 | glDisable(GL_BLEND);
|
---|
| 1244 | glDisable(GL_DEPTH_TEST);
|
---|
| 1245 |
|
---|
| 1246 | glPolygonMode(GL_FRONT, GL_FILL);
|
---|
| 1247 |
|
---|
| 1248 | glMatrixMode(GL_PROJECTION);
|
---|
| 1249 | glPushMatrix();
|
---|
| 1250 | glLoadIdentity();
|
---|
| 1251 |
|
---|
| 1252 | gluOrtho2D(0, 1, 0, 1);
|
---|
| 1253 |
|
---|
| 1254 |
|
---|
| 1255 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1256 | glPushMatrix();
|
---|
| 1257 | glLoadIdentity();
|
---|
| 1258 |
|
---|
| 1259 |
|
---|
| 1260 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
| 1261 | glViewport(0, 0, mWidth, mHeight);
|
---|
[3128] | 1262 |
|
---|
| 1263 | // revert to old far and near plane
|
---|
[3104] | 1264 | mCamera->SetFar(oldFar);
|
---|
[3106] | 1265 | mCamera->SetNear(oldNear);
|
---|
[3085] | 1266 | }
|
---|
| 1267 |
|
---|
[3212] | 1268 |
|
---|
[3213] | 1269 | void DeferredRenderer::LenseFlare(FrameBufferObject *fbo,
|
---|
[3215] | 1270 | DirectionalLight *light
|
---|
[3213] | 1271 | )
|
---|
[3212] | 1272 | {
|
---|
[3242] | 1273 | // light source visible?
|
---|
[3215] | 1274 | if (!mSunVisiblePixels) return;
|
---|
| 1275 |
|
---|
[3213] | 1276 | // the sun is a large distance in the reverse light direction
|
---|
| 1277 | // => extrapolate light pos
|
---|
| 1278 | const Vector3 lightPos = light->GetDirection() * -1e3f;
|
---|
| 1279 |
|
---|
[3214] | 1280 | float w;
|
---|
| 1281 | Vector3 projLightPos = mProjViewMatrix.Transform(w, lightPos, 1.0f);
|
---|
| 1282 | projLightPos /= w;
|
---|
| 1283 | projLightPos = projLightPos * 0.5f + Vector3(0.5f);
|
---|
[3213] | 1284 |
|
---|
| 1285 | // vector to light from screen center in texture space
|
---|
| 1286 | Vector3 vectorToLight = projLightPos - Vector3(0.5f);
|
---|
| 1287 | vectorToLight.z = .0f;
|
---|
| 1288 |
|
---|
| 1289 | const float distanceToLight = Magnitude(vectorToLight);
|
---|
| 1290 | vectorToLight /= distanceToLight;
|
---|
| 1291 |
|
---|
[3214] | 1292 | //cout << "dist " << distanceToLight << " v " << vectorToLight << endl;
|
---|
[3213] | 1293 |
|
---|
| 1294 |
|
---|
[3212] | 1295 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
[3213] | 1296 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
| 1297 |
|
---|
[3212] | 1298 | FlipFbos(fbo);
|
---|
| 1299 |
|
---|
[3213] | 1300 | int i = 0;
|
---|
[3212] | 1301 |
|
---|
[3213] | 1302 | sCgLenseFlareProgram->SetTexture(i ++, colorsTex);
|
---|
| 1303 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[0]->GetId());
|
---|
| 1304 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[1]->GetId());
|
---|
| 1305 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[2]->GetId());
|
---|
| 1306 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[3]->GetId());
|
---|
[3214] | 1307 | sCgLenseFlareProgram->SetTexture(i ++, sHaloTex[4]->GetId());
|
---|
[3213] | 1308 | sCgLenseFlareProgram->SetValue2f(i ++, vectorToLight.x, vectorToLight.y);
|
---|
| 1309 | sCgLenseFlareProgram->SetValue1f(i ++, distanceToLight);
|
---|
[3215] | 1310 | sCgLenseFlareProgram->SetValue1f(i ++, (float)mSunVisiblePixels);
|
---|
[3213] | 1311 |
|
---|
[3212] | 1312 | DrawQuad(sCgLenseFlareProgram);
|
---|
| 1313 |
|
---|
| 1314 | PrintGLerror("LenseFlare");
|
---|
| 1315 | }
|
---|
| 1316 |
|
---|
| 1317 |
|
---|
[3232] | 1318 | void DeferredRenderer::DepthOfField(FrameBufferObject *fbo)
|
---|
| 1319 | {
|
---|
| 1320 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
| 1321 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
| 1322 |
|
---|
| 1323 | FlipFbos(fbo);
|
---|
| 1324 |
|
---|
| 1325 | int i = 0;
|
---|
| 1326 |
|
---|
[3235] | 1327 | const float zFocus = 7.0f;
|
---|
| 1328 |
|
---|
[3232] | 1329 | sCgDOFProgram->SetTexture(i ++, colorsTex);
|
---|
| 1330 | sCgDOFProgram->SetArray2f(i ++, (float *)dofSamples, NUM_DOF_TABS);
|
---|
[3235] | 1331 | sCgDOFProgram->SetValue1f(i ++, min(mCamera->GetFar(), mMaxDistance) - mCamera->GetNear());
|
---|
| 1332 | sCgDOFProgram->SetValue1f(i ++, zFocus);
|
---|
[3232] | 1333 |
|
---|
| 1334 | DrawQuad(sCgDOFProgram);
|
---|
| 1335 |
|
---|
| 1336 | PrintGLerror("LenseFlare");
|
---|
| 1337 | }
|
---|
| 1338 |
|
---|
| 1339 |
|
---|
[3219] | 1340 | void DeferredRenderer::SaveFrame(FrameBufferObject *fbo)
|
---|
| 1341 | {
|
---|
| 1342 | ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
|
---|
| 1343 | GLuint colorsTex = colorBuffer->GetTexture();
|
---|
[3215] | 1344 |
|
---|
[3219] | 1345 | GLubyte *data = new GLubyte[mWidth * mHeight * 4];
|
---|
| 1346 |
|
---|
| 1347 | // grab texture data
|
---|
| 1348 | glEnable(GL_TEXTURE_2D);
|
---|
| 1349 | glBindTexture(GL_TEXTURE_2D, colorsTex);
|
---|
| 1350 | glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
---|
| 1351 |
|
---|
| 1352 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 1353 | glDisable(GL_TEXTURE_2D);
|
---|
| 1354 |
|
---|
| 1355 | /////////////////
|
---|
| 1356 |
|
---|
| 1357 | startil();
|
---|
| 1358 |
|
---|
| 1359 | static char imageName[200];
|
---|
[3220] | 1360 | sprintf(imageName, "%s_%05d.bmp", mSavedFrameSuffix.c_str(), mSavedFrameNumber);
|
---|
[3219] | 1361 |
|
---|
| 1362 | ILstring fileName = ILstring(imageName);
|
---|
| 1363 | ilRegisterType(IL_FLOAT);
|
---|
| 1364 |
|
---|
| 1365 | const int depth = 1;
|
---|
| 1366 | const int bpp = 4;
|
---|
| 1367 |
|
---|
| 1368 | if (!ilTexImage(mWidth, mHeight, depth, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data))
|
---|
| 1369 | {
|
---|
| 1370 | cerr << "IL error " << ilGetError() << endl;
|
---|
| 1371 | stopil();
|
---|
| 1372 | return;
|
---|
| 1373 | }
|
---|
| 1374 |
|
---|
| 1375 | ilEnable(IL_FILE_OVERWRITE);
|
---|
| 1376 |
|
---|
| 1377 | if (!ilSaveImage(fileName))
|
---|
| 1378 | {
|
---|
| 1379 | cerr << "TGA write error " << ilGetError() << endl;
|
---|
| 1380 | }
|
---|
| 1381 |
|
---|
| 1382 | delete [] data;
|
---|
| 1383 |
|
---|
| 1384 | stopil();
|
---|
| 1385 |
|
---|
| 1386 | PrintGLerror("Store frame");
|
---|
| 1387 | }
|
---|
| 1388 |
|
---|
| 1389 |
|
---|
[3215] | 1390 | void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
|
---|
| 1391 | {
|
---|
| 1392 | mUseTemporalCoherence = temporal;
|
---|
| 1393 | }
|
---|
| 1394 |
|
---|
| 1395 |
|
---|
| 1396 | void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s)
|
---|
| 1397 | {
|
---|
| 1398 | if (s != mSamplingMethod)
|
---|
| 1399 | {
|
---|
| 1400 | mSamplingMethod = s;
|
---|
| 1401 | mRegenerateSamples = true;
|
---|
| 1402 | }
|
---|
| 1403 | }
|
---|
| 1404 |
|
---|
| 1405 |
|
---|
| 1406 | void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
|
---|
| 1407 | {
|
---|
| 1408 | if (s != mShadingMethod)
|
---|
| 1409 | {
|
---|
| 1410 | mShadingMethod = s;
|
---|
| 1411 | mRegenerateSamples = true;
|
---|
| 1412 | }
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
| 1415 |
|
---|
| 1416 | #if TODO
|
---|
| 1417 |
|
---|
| 1418 | void DeferredRenderer::SetNumSamples(int numSamples)
|
---|
| 1419 | {
|
---|
| 1420 | mNumSamples = numSamples;
|
---|
| 1421 | }
|
---|
| 1422 |
|
---|
| 1423 | #endif
|
---|
| 1424 |
|
---|
| 1425 |
|
---|
| 1426 | void DeferredRenderer::SetSampleIntensity(float sampleIntensity)
|
---|
| 1427 | {
|
---|
| 1428 | mSampleIntensity = sampleIntensity;
|
---|
[3247] | 1429 | mRegenerateSamples = true;
|
---|
[3215] | 1430 | }
|
---|
| 1431 |
|
---|
| 1432 |
|
---|
| 1433 | void DeferredRenderer::SetKernelRadius(float kernelRadius)
|
---|
| 1434 | {
|
---|
| 1435 | mKernelRadius = kernelRadius;
|
---|
[3247] | 1436 | mRegenerateSamples = true;
|
---|
[3215] | 1437 | }
|
---|
| 1438 |
|
---|
| 1439 |
|
---|
[3216] | 1440 | void DeferredRenderer::SetSsaoFilterRadius(float ssaoFilterRadius)
|
---|
| 1441 | {
|
---|
| 1442 | mSsaoFilterRadius = ssaoFilterRadius;
|
---|
[3247] | 1443 | mRegenerateSamples = true;
|
---|
[3216] | 1444 | }
|
---|
| 1445 |
|
---|
| 1446 |
|
---|
[3242] | 1447 | /*void DeferredRenderer::SetSortSamples(bool sortSamples)
|
---|
[3215] | 1448 | {
|
---|
| 1449 | mSortSamples = sortSamples;
|
---|
[3242] | 1450 | }*/
|
---|
[3215] | 1451 |
|
---|
| 1452 |
|
---|
| 1453 | void DeferredRenderer::SetSunVisiblePixels(int pixels)
|
---|
| 1454 | {
|
---|
| 1455 | mSunVisiblePixels = pixels;
|
---|
| 1456 | }
|
---|
| 1457 |
|
---|
| 1458 |
|
---|
[3235] | 1459 | void DeferredRenderer::SetMaxDistance(float maxDist)
|
---|
| 1460 | {
|
---|
| 1461 | mMaxDistance = maxDist;
|
---|
| 1462 | }
|
---|
| 1463 |
|
---|
| 1464 |
|
---|
[3242] | 1465 | void DeferredRenderer::SetUseToneMapping(bool toneMapping)
|
---|
| 1466 | {
|
---|
| 1467 | mUseToneMapping = toneMapping;
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 |
|
---|
| 1471 | void DeferredRenderer::SetUseAntiAliasing(bool antiAliasing)
|
---|
| 1472 | {
|
---|
| 1473 | mUseAntiAliasing = antiAliasing;
|
---|
| 1474 | }
|
---|
| 1475 |
|
---|
| 1476 |
|
---|
| 1477 | void DeferredRenderer::SetUseDepthOfField(bool dof)
|
---|
| 1478 | {
|
---|
| 1479 | mUseDepthOfField = dof;
|
---|
| 1480 | }
|
---|
| 1481 |
|
---|
| 1482 |
|
---|
| 1483 | void DeferredRenderer::SetTemporalCoherenceFactorForSsao(float factor)
|
---|
| 1484 | {
|
---|
| 1485 | mTempCohFactor = factor;
|
---|
| 1486 | }
|
---|
| 1487 |
|
---|
| 1488 |
|
---|
[3220] | 1489 | void DeferredRenderer::SetSaveFrame(const string &suffix, int frameNumber)
|
---|
[3219] | 1490 | {
|
---|
[3220] | 1491 | mSavedFrameSuffix = suffix;
|
---|
[3219] | 1492 | mSavedFrameNumber = frameNumber;
|
---|
| 1493 | }
|
---|
[3215] | 1494 |
|
---|
[3219] | 1495 |
|
---|
[2858] | 1496 | } // namespace
|
---|