File: RendererPostEffects.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (699 lines) | stat: -rw-r--r-- 24,358 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#include "hpl1/engine/graphics/RendererPostEffects.h"
#include "hpl1/debug.h"
#include "hpl1/engine/graphics/GPUProgram.h"
#include "hpl1/engine/graphics/LowLevelGraphics.h"
#include "hpl1/engine/graphics/RenderList.h"
#include "hpl1/engine/graphics/Renderer3D.h"
#include "hpl1/engine/graphics/Texture.h"
#include "hpl1/engine/math/Math.h"
#include "hpl1/engine/resources/GpuProgramManager.h"
#include "hpl1/engine/resources/Resources.h"
#include "hpl1/engine/resources/low_level_resources.h"
#include "hpl1/engine/scene/Scene.h"
#include "hpl1/engine/system/low_level_system.h"

namespace hpl {

//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cRendererPostEffects::cRendererPostEffects(iLowLevelGraphics *apLowLevelGraphics, cResources *apResources,
										   cRenderList *apRenderList, cRenderer3D *apRenderer3D) {
	mpLowLevelGraphics = apLowLevelGraphics;
	mpLowLevelResources = apResources->GetLowLevel();
	mpResources = apResources;
	mpRenderer3D = apRenderer3D;

	mpGpuManager = mpResources->GetGpuProgramManager();

	mvScreenSize = mpLowLevelGraphics->GetScreenSize();

	mpRenderList = apRenderList;

	///////////////////////////////////////////
	// Create screen buffers
	Log(" Creating screen buffers size %s\n", mvScreenSize.ToString().c_str());
	for (int i = 0; i < 2; i++) {
		if (mpLowLevelGraphics->GetCaps(eGraphicCaps_TextureTargetRectangle)) {
			mpScreenBuffer[i] = mpLowLevelGraphics->CreateTexture(cVector2l(
																	  (int)mvScreenSize.x, (int)mvScreenSize.y),
																  32, cColor(0, 0, 0, 0), false,
																  eTextureType_Normal, eTextureTarget_Rect);

			if (mpScreenBuffer[i] == NULL) {
				Error("Couldn't create screenbuffer!\n");
				mpScreenBuffer[0] = NULL;
				mpScreenBuffer[1] = NULL;
				break;
			}
			mpScreenBuffer[i]->SetWrapS(eTextureWrap_ClampToEdge);
			mpScreenBuffer[i]->SetWrapT(eTextureWrap_ClampToEdge);

		} else {
			mpScreenBuffer[i] = NULL;
			Error("Texture rectangle not supported. Posteffects will be turned off.");
		}
	}

	///////////////////////////////////////////
	// Create programs
	Hpl1::logInfo(Hpl1::kDebugRenderer, "%s", "Creating RendererPostEffects programs");

	/////////////////
	// Blur programs
	mbBlurFallback = false; // Set to true if the fallbacks are used.
	_blur2DProgram = mpGpuManager->CreateProgram("hpl1_PostEffect_Blur", "hpl1_PostEffect_Blur_2D");
	_blurRectProgram = mpGpuManager->CreateProgram("hpl1_PostEffect_Blur", "hpl1_PostEffect_Blur_Rect");

	/////////////////
	// Bloom programs
	_bloomProgram = mpGpuManager->CreateProgram("hpl1_PostEffect_Bloom", "hpl1_PostEffect_Bloom");

	// Bloom blur textures
	mpBloomBlurTexture = mpLowLevelGraphics->CreateTexture(
		cVector2l(256, 256),
		32, cColor(0, 0, 0, 0), false,
		eTextureType_Normal, eTextureTarget_2D);

	if (mpBloomBlurTexture == NULL) {
		Error("Couldn't create bloom blur textures!\n");
	} else {
		mpBloomBlurTexture->SetWrapS(eTextureWrap_ClampToEdge);
		mpBloomBlurTexture->SetWrapT(eTextureWrap_ClampToEdge);
	}

	/////////////////
	// MotionBlur programs
	_motionBlurProgram = mpGpuManager->CreateProgram("hpl1_PostEffect_Motion", "hpl1_PostEffect_Motion"); // CHECK APPLE

	/////////////////
	// Depth of Field programs
	_depthOfFieldProgram = mpGpuManager->CreateProgram("hpl1_PostEffect_DoF", "hpl1_PostEffect_DoF");

	// Depth of Field blur textures
	mpDofBlurTexture = mpLowLevelGraphics->CreateTexture(cVector2l(256, 256),
														 32, cColor(0, 0, 0, 0), false,
														 eTextureType_Normal, eTextureTarget_2D);

	if (mpDofBlurTexture == NULL) {
		Error("Couldn't create Depth of Field blur textures!\n");
	} else {
		mpDofBlurTexture->SetWrapS(eTextureWrap_ClampToEdge);
		mpDofBlurTexture->SetWrapT(eTextureWrap_ClampToEdge);
	}

	Log("RendererPostEffects created\n");

	////////////////////////////////////
	// Variable setup

	// General
	mbActive = false;
	mvTexRectVtx.resize(4);

	// Bloom
	mbBloomActive = false;
	mfBloomSpread = 2.0f;

	// Motion blur
	mbMotionBlurActive = false;
	mfMotionBlurAmount = 1.0f;
	mbMotionBlurFirstTime = true;

	// Depth of Field
	mbDofActive = false;
	mfDofMaxBlur = 1.0f;
	mfDofFocalPlane = 1.0f;
	mfDofNearPlane = 2.0f;
	mfDofFarPlane = 3.0f;
}

//-----------------------------------------------------------------------

cRendererPostEffects::~cRendererPostEffects() {
	for (int i = 0; i < 2; i++)
		if (mpScreenBuffer[i])
			hplDelete(mpScreenBuffer[i]);

	if (_blur2DProgram)
		mpGpuManager->Destroy(_blur2DProgram);
	if (_blurRectProgram)
		mpGpuManager->Destroy(_blurRectProgram);

	if (_bloomProgram)
		mpGpuManager->Destroy(_bloomProgram);

	if (_motionBlurProgram)
		mpGpuManager->Destroy(_motionBlurProgram);

	if (_depthOfFieldProgram)
		mpGpuManager->Destroy(_depthOfFieldProgram);

	if (mpBloomBlurTexture)
		hplDelete(mpBloomBlurTexture);
	if (mpDofBlurTexture)
		hplDelete(mpDofBlurTexture);
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

void cRendererPostEffects::SetBloomActive(bool abX) {
	mbBloomActive = abX;
}

//-----------------------------------------------------------------------

void cRendererPostEffects::SetMotionBlurActive(bool abX) {
	mbMotionBlurActive = abX;
	mbMotionBlurFirstTime = true;
}

//-----------------------------------------------------------------------

void cRendererPostEffects::Render() {
	if (mpScreenBuffer[0] == NULL || mpScreenBuffer[1] == NULL)
		return;
	if (!mpLowLevelGraphics->GetCaps(eGraphicCaps_TextureTargetRectangle))
		return;

	if (mbActive == false ||
		(mImageTrailData.mbActive == false && mbBloomActive == false &&
		 mbMotionBlurActive == false && mbDofActive == false)) {
		return;
	}

	mvScreenSize = mpLowLevelGraphics->GetScreenSize();

	RenderDepthOfField();
	RenderMotionBlur();

	mpLowLevelGraphics->SetDepthTestActive(false);
	mpLowLevelGraphics->PushMatrix(eMatrix_ModelView);
	mpLowLevelGraphics->SetIdentityMatrix(eMatrix_ModelView);
	mpLowLevelGraphics->SetOrthoProjection(mpLowLevelGraphics->GetVirtualSize(), -1000, 1000);

	RenderBloom();

	RenderImageTrail();

	mpLowLevelGraphics->PopMatrix(eMatrix_ModelView);
}

//-----------------------------------------------------------------------

// TODO: Support source texture as 2D

void cRendererPostEffects::RenderBlurTexture(iTexture *apDestination, iTexture *apSource,
											 float afBlurAmount) {
	bool bProgramsLoaded = false;
	if (_blur2DProgram && _blurRectProgram)
		bProgramsLoaded = true;

	iLowLevelGraphics *pLowLevel = mpLowLevelGraphics;
	cVector2l vBlurSize = cVector2l(apDestination->getWidth(), apDestination->getHeight());
	cVector2f vBlurDrawSize;
	vBlurDrawSize.x = ((float)vBlurSize.x / mvScreenSize.x) * mpLowLevelGraphics->GetVirtualSize().x;
	vBlurDrawSize.y = ((float)vBlurSize.y / mvScreenSize.y) * mpLowLevelGraphics->GetVirtualSize().y;

	pLowLevel->SetBlendActive(false);

	///////////////////////////////////////////
	// Horizontal blur pass

	// Shader setup
	if (bProgramsLoaded) {
		// Setup vertex program
		_blurRectProgram->Bind();
		_blurRectProgram->SetFloat("xOffset", 1);
		_blurRectProgram->SetFloat("yOffset", 0);
		_blurRectProgram->SetFloat("amount", afBlurAmount);
		_blurRectProgram->SetMatrixf("worldViewProj", eGpuProgramMatrix_ViewProjection, eGpuProgramMatrixOp_Identity);
	}

	// Draw the screen texture with blur
	pLowLevel->SetTexture(0, apSource);
	if (mbBlurFallback) {
		mpLowLevelGraphics->SetTexture(1, apSource);
		mpLowLevelGraphics->SetTexture(2, apSource);
	}

	{
		mvTexRectVtx[0] = cVertex(cVector3f(0, 0, 40), cVector2f(0, mvScreenSize.y), cColor(1, 1.0f));
		mvTexRectVtx[1] = cVertex(cVector3f(vBlurDrawSize.x, 0, 40), cVector2f(mvScreenSize.x, mvScreenSize.y), cColor(1, 1.0f));
		mvTexRectVtx[2] = cVertex(cVector3f(vBlurDrawSize.x, vBlurDrawSize.y, 40), cVector2f(mvScreenSize.x, 0), cColor(1, 1.0f));
		mvTexRectVtx[3] = cVertex(cVector3f(0, vBlurDrawSize.y, 40), cVector2f(0, 0), cColor(1, 1.0f));

		mpLowLevelGraphics->DrawQuad(mvTexRectVtx);
	}

	pLowLevel->CopyContextToTexure(apDestination, 0, vBlurSize);

	///////////////////////////////////////////
	// Vertical blur pass

	// Setup shaders
	// Shader setup
	if (bProgramsLoaded) {
		_blur2DProgram->Bind();
		_blur2DProgram->SetFloat("xOffset", 0);
		_blur2DProgram->SetFloat("yOffset", 1);
		_blur2DProgram->SetFloat("amount", (1 / pLowLevel->GetScreenSize().x) * afBlurAmount);
	}

	// Set texture and draw
	pLowLevel->SetTexture(0, apDestination);
	if (mbBlurFallback) {
		mpLowLevelGraphics->SetTexture(1, apDestination);
		mpLowLevelGraphics->SetTexture(2, apDestination);
	}

	{
		mvTexRectVtx[0] = cVertex(cVector3f(0, 0, 40), cVector2f(0, 1), cColor(1, 1.0f));
		mvTexRectVtx[1] = cVertex(cVector3f(vBlurDrawSize.x, 0, 40), cVector2f(1, 1), cColor(1, 1.0f));
		mvTexRectVtx[2] = cVertex(cVector3f(vBlurDrawSize.x, vBlurDrawSize.y, 40), cVector2f(1, 0), cColor(1, 1.0f));
		mvTexRectVtx[3] = cVertex(cVector3f(0, vBlurDrawSize.y, 40), cVector2f(0, 0), cColor(1, 1.0f));

		mpLowLevelGraphics->DrawQuad(mvTexRectVtx);
	}

	// Shader setup
	if (bProgramsLoaded) {
		_blur2DProgram->UnBind();
	}

	pLowLevel->CopyContextToTexure(apDestination, 0, vBlurSize);

	if (mbBlurFallback) {
		mpLowLevelGraphics->SetTexture(1, NULL);
		mpLowLevelGraphics->SetTexture(2, NULL);
	}
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

void cRendererPostEffects::RenderDepthOfField() {
	if (mbDofActive == false)
		return;

	if (!_depthOfFieldProgram)
		return;

	//////////////////////////////
	// Setup
	cCamera3D *pCam = mpRenderList->GetCamera();

	iTexture *pScreenTexture = mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0];

	// Size of the virtual screen
	cVector2f vVirtSize = mpLowLevelGraphics->GetVirtualSize();

	// Copy screen to texture
	mpLowLevelGraphics->CopyContextToTexure(pScreenTexture, 0, cVector2l((int)mvScreenSize.x, (int)mvScreenSize.y));

	// Set up things needed for blurring
	mpLowLevelGraphics->SetDepthWriteActive(false);
	mpLowLevelGraphics->SetDepthTestActive(false);
	mpLowLevelGraphics->SetIdentityMatrix(eMatrix_ModelView);
	mpLowLevelGraphics->SetOrthoProjection(mpLowLevelGraphics->GetVirtualSize(), -1000, 1000);

	// Render blur texture
	RenderBlurTexture(mpDofBlurTexture, pScreenTexture, 2.0f);

	// Set texture
	mpLowLevelGraphics->SetTexture(0, pScreenTexture);
	mpLowLevelGraphics->SetTexture(1, mpDofBlurTexture);

	// Render entire screen textur to screen, this to cover up what has been done by the
	// blur filter, should have support for frame_buffer_object so this can be skipped.
	if (mpRenderer3D->GetSkyBoxActive()) {
		tVector3fVec vUvVec;
		vUvVec.resize(4);

		vUvVec[0] = cVector2f(0, 1);
		vUvVec[1] = cVector2f(1, 1);
		vUvVec[2] = cVector2f(1, 0);
		vUvVec[3] = cVector2f(0, 0);

		mvTexRectVtx[0] = cVertex(cVector3f(0, 0, 40), cVector2f(0, mvScreenSize.y), cColor(1, 1.0f));
		mvTexRectVtx[1] = cVertex(cVector3f(vVirtSize.x, 0, 40), cVector2f(mvScreenSize.x, mvScreenSize.y), cColor(1, 1.0f));
		mvTexRectVtx[2] = cVertex(cVector3f(vVirtSize.x, vVirtSize.y, 40), cVector2f(mvScreenSize.x, 0), cColor(1, 1.0f));
		mvTexRectVtx[3] = cVertex(cVector3f(0, vVirtSize.y, 40), cVector2f(0, 0), cColor(1, 1.0f));

		mpLowLevelGraphics->SetActiveTextureUnit(1);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorSource0, eTextureSource_Texture);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorSource1, eTextureSource_Previous);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorSource2, eTextureSource_Constant);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorFunc, eTextureFunc_Interpolate);
		mpLowLevelGraphics->SetTextureConstantColor(cColor(mfDofMaxBlur, mfDofMaxBlur));

		mpLowLevelGraphics->DrawQuadMultiTex(mvTexRectVtx, vUvVec);

		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorFunc, eTextureFunc_Modulate);
	}

	// Set things back to normal
	mpLowLevelGraphics->SetMatrix(eMatrix_Projection, pCam->GetProjectionMatrix());

	///////////////////////////////////////////
	// Draw motion blur objects
	mpLowLevelGraphics->SetDepthTestFunc(eDepthTestFunc_Equal);
	mpLowLevelGraphics->SetDepthTestActive(true);
	mpLowLevelGraphics->SetBlendActive(false);

	// Setup
	_depthOfFieldProgram->Bind();
	_depthOfFieldProgram->SetVec3f("planes", cVector3f(mfDofNearPlane, mfDofFocalPlane, mfDofFarPlane));
	_depthOfFieldProgram->SetFloat("maxBlur", mfDofMaxBlur);
	_depthOfFieldProgram->SetVec2f("screenSize", mvScreenSize);

	//////////////////
	// Render objects
	cMotionBlurObjectIterator it = mpRenderList->GetMotionBlurIterator();
	while (it.HasNext()) {
		iRenderable *pObject = it.Next();
		cMatrixf *pMtx = pObject->GetModelMatrix(pCam);

		//////////////////
		// Non static models
		if (pMtx) {
			mpLowLevelGraphics->SetMatrix(eMatrix_ModelView, cMath::MatrixMul(pCam->GetViewMatrix(), *pMtx));

			_depthOfFieldProgram->SetMatrixf("worldViewProj", eGpuProgramMatrix_ViewProjection,
											 eGpuProgramMatrixOp_Identity);
		}
		//////////////////
		// NULL Model view matrix (static)
		else {
			mpLowLevelGraphics->SetMatrix(eMatrix_ModelView, pCam->GetViewMatrix());

			_depthOfFieldProgram->SetMatrixf("worldViewProj", eGpuProgramMatrix_ViewProjection,
											 eGpuProgramMatrixOp_Identity);
		}

		pObject->GetVertexBuffer()->Bind();

		pObject->GetVertexBuffer()->Draw();

		pObject->GetVertexBuffer()->UnBind();

		// Set the previous position to the current
		if (pMtx)
			pObject->SetPrevMatrix(*pMtx);
	}

	mpLowLevelGraphics->SetDepthTestFunc(eDepthTestFunc_LessOrEqual);
	mpLowLevelGraphics->SetDepthWriteActive(true);

	// Reset stuff
	_depthOfFieldProgram->UnBind();
	mpLowLevelGraphics->SetTexture(0, NULL);
	mpLowLevelGraphics->SetTexture(1, NULL);
}

//-----------------------------------------------------------------------

void cRendererPostEffects::RenderMotionBlur() {
	if (mbMotionBlurActive == false)
		return;

	if (!_motionBlurProgram)
		return;

	//////////////////////////////
	// Setup
	iTexture *pScreenTexture = mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0];

	// Size of the virtual screen
	/*cVector2f vVirtSize = */ mpLowLevelGraphics->GetVirtualSize();

	// Copy screen to texture
	mpLowLevelGraphics->CopyContextToTexure(pScreenTexture, 0, cVector2l((int)mvScreenSize.x, (int)mvScreenSize.y));

	///////////////////////////////////////////
	// Draw motion blur objects
	mpLowLevelGraphics->SetDepthTestActive(true);
	mpLowLevelGraphics->SetBlendActive(false);

	cCamera3D *pCam = mpRenderList->GetCamera();
	if (mbMotionBlurFirstTime) {
		pCam->SetPrevView(pCam->GetViewMatrix());
		pCam->SetPrevProjection(pCam->GetProjectionMatrix());
		mbMotionBlurFirstTime = false;
	}

	cMotionBlurObjectIterator it = mpRenderList->GetMotionBlurIterator();

	// Setup
	_motionBlurProgram->Bind();
	_motionBlurProgram->SetFloat("blurScale", mfMotionBlurAmount);
	_motionBlurProgram->SetVec2f("halfScreenSize",
								 cVector2f((float)pScreenTexture->getWidth() / 2.0f,
										   (float)pScreenTexture->getHeight() / 2.0f));

	mpLowLevelGraphics->SetTexture(0, pScreenTexture);

	//////////////////
	// Render objects
	while (it.HasNext()) {
		iRenderable *pObject = it.Next();
		cMatrixf *pMtx = pObject->GetModelMatrix(pCam);

		//////////////////
		// Non static models
		if (pMtx) {
			cMatrixf mtxPrev = pObject->GetPrevMatrix();

			mpLowLevelGraphics->SetMatrix(eMatrix_ModelView, cMath::MatrixMul(pCam->GetViewMatrix(), *pMtx));

			_motionBlurProgram->SetMatrixf("worldViewProj", eGpuProgramMatrix_ViewProjection,
										   eGpuProgramMatrixOp_Identity);

			cMatrixf mtxModelView = cMath::MatrixMul(pCam->GetViewMatrix(), *pMtx);
			cMatrixf mtxPrevModelView = cMath::MatrixMul(pCam->GetPrevView(), mtxPrev);

			cMatrixf mtxPrevViewProj = cMath::MatrixMul(pCam->GetPrevProjection(), mtxPrevModelView);

			_motionBlurProgram->SetMatrixf("prevWorldViewProj", mtxPrevViewProj);
			_motionBlurProgram->SetMatrixf("modelView", mtxModelView);
			_motionBlurProgram->SetMatrixf("prevModelView", mtxPrevModelView);
		}
		//////////////////
		// NULL Model view matrix (static)
		else {
			mpLowLevelGraphics->SetMatrix(eMatrix_ModelView, pCam->GetViewMatrix());

			_motionBlurProgram->SetMatrixf("worldViewProj", eGpuProgramMatrix_ViewProjection,
										   eGpuProgramMatrixOp_Identity);

			const cMatrixf &mtxModelView = pCam->GetViewMatrix();
			const cMatrixf &mtxPrevModelView = pCam->GetPrevView();

			cMatrixf mtxPrevViewProj = cMath::MatrixMul(pCam->GetPrevProjection(), mtxPrevModelView);

			_motionBlurProgram->SetMatrixf("prevWorldViewProj", mtxPrevViewProj);
			_motionBlurProgram->SetMatrixf("modelView", mtxModelView);
			_motionBlurProgram->SetMatrixf("prevModelView", mtxPrevModelView);
		}

		pObject->GetVertexBuffer()->Bind();

		pObject->GetVertexBuffer()->Draw();

		pObject->GetVertexBuffer()->UnBind();

		// Set the previous position to the current
		if (pMtx)
			pObject->SetPrevMatrix(*pMtx);
	}

	// Reset stuff
	_motionBlurProgram->UnBind();
	mpLowLevelGraphics->SetTexture(0, NULL);
	mpLowLevelGraphics->SetTexture(1, NULL);

	// Set the new pervious values.
	pCam->SetPrevView(pCam->GetViewMatrix());
	pCam->SetPrevProjection(pCam->GetProjectionMatrix());
}

//-----------------------------------------------------------------------

void cRendererPostEffects::RenderBloom() {
	if (mbBloomActive == false)
		return;

	if (!_bloomProgram)
		return;

	//////////////////////////////
	// Setup

	iTexture *pScreenTexture = mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0];

	// Copy screen to texture
	mpLowLevelGraphics->CopyContextToTexure(pScreenTexture, 0,
											cVector2l((int)mvScreenSize.x, (int)mvScreenSize.y));

	// Get the blur texture
	RenderBlurTexture(mpBloomBlurTexture, pScreenTexture, mfBloomSpread);

	// Size of blur texture
	/*cVector2f vBlurSize = */ cVector2f((float)mpBloomBlurTexture->getWidth(), (float)mpBloomBlurTexture->getHeight());

	// Size of the virtual screen
	cVector2f vVirtSize = mpLowLevelGraphics->GetVirtualSize();

	///////////////////////////////////////////
	// Draw Bloom

	// Setup bloom program
	_bloomProgram->Bind();
	_bloomProgram->SetMatrixf("worldViewProj", eGpuProgramMatrix_ViewProjection, eGpuProgramMatrixOp_Identity);

	mpLowLevelGraphics->SetTexture(0, mpBloomBlurTexture);
	mpLowLevelGraphics->SetTexture(1, pScreenTexture);

	// Draw
	{
		tVector3fVec vUvVec;
		vUvVec.resize(4);

		vUvVec[0] = cVector2f(0, mvScreenSize.y);
		vUvVec[1] = cVector2f(mvScreenSize.x, mvScreenSize.y);
		vUvVec[2] = cVector2f(mvScreenSize.x, 0);
		vUvVec[3] = cVector2f(0, 0);

		mvTexRectVtx[0] = cVertex(cVector3f(0, 0, 40), cVector2f(0, 1), cColor(1, 1.0f));
		mvTexRectVtx[1] = cVertex(cVector3f(vVirtSize.x, 0, 40), cVector2f(1, 1), cColor(1, 1.0f));
		mvTexRectVtx[2] = cVertex(cVector3f(vVirtSize.x, vVirtSize.y, 40), cVector2f(1, 0), cColor(1, 1.0f));
		mvTexRectVtx[3] = cVertex(cVector3f(0, vVirtSize.y, 40), cVector2f(0, 0), cColor(1, 1.0f));

		mpLowLevelGraphics->DrawQuadMultiTex(mvTexRectVtx, vUvVec);
	}

	_bloomProgram->UnBind();

	mpLowLevelGraphics->SetTexture(0, NULL);
	mpLowLevelGraphics->SetTexture(1, NULL);
}

//-----------------------------------------------------------------------

void cRendererPostEffects::RenderImageTrail() {
	if (mImageTrailData.mbActive == false)
		return;

	tVertexVec vVtx;
	vVtx.push_back(cVertex(cVector3f(0, 0, 40), cVector2f(0, mvScreenSize.y), cColor(1, 0.6f)));
	vVtx.push_back(cVertex(cVector3f(800, 0, 40), cVector2f(mvScreenSize.x, mvScreenSize.y), cColor(1, 0.6f)));
	vVtx.push_back(cVertex(cVector3f(800, 600, 40), cVector2f(mvScreenSize.x, 0), cColor(1, 0.6f)));
	vVtx.push_back(cVertex(cVector3f(0, 600, 40), cVector2f(0, 0), cColor(1, 0.6f)));

	mpLowLevelGraphics->CopyContextToTexure(mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0], 0, cVector2l((int)mpLowLevelGraphics->GetScreenSize().x, (int)mpLowLevelGraphics->GetScreenSize().y));
	{
		if (mImageTrailData.mbActive) {
			if (mImageTrailData.mbFirstPass) {
				mpLowLevelGraphics->SetBlendActive(false);
				mpLowLevelGraphics->SetTexture(0, mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0]);
			} else {

				mpLowLevelGraphics->SetClearDepthActive(false);
				mpLowLevelGraphics->SetClearColor(cColor(0, 0));
				mpLowLevelGraphics->ClearScreen();
				mpLowLevelGraphics->SetClearDepthActive(true);

				// Draw the new image to screen transparently
				mpLowLevelGraphics->SetBlendActive(true);
				mpLowLevelGraphics->SetBlendFunc(eBlendFunc_SrcAlpha, eBlendFunc_Zero);

				mpLowLevelGraphics->SetTexture(0, mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0]);

				mpLowLevelGraphics->SetTextureConstantColor(cColor(1, 1.0f - mImageTrailData.mfAmount));
				mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaSource0, eTextureSource_Constant);
				mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaFunc, eTextureFunc_Replace);
			}
		} else {
			mpLowLevelGraphics->SetBlendActive(false);
			mpLowLevelGraphics->SetTexture(0, mpScreenBuffer[mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0]);
		}

		mpLowLevelGraphics->DrawQuad(vVtx);

		if (mImageTrailData.mbActive) {
			if (!mImageTrailData.mbFirstPass) {
				// Draw the old Blurred image to screen
				mpLowLevelGraphics->SetBlendFunc(eBlendFunc_SrcAlpha, eBlendFunc_One);

				mpLowLevelGraphics->SetTexture(0, mpScreenBuffer[mImageTrailData.mlCurrentBuffer]);

				mpLowLevelGraphics->SetTextureConstantColor(cColor(1, mImageTrailData.mfAmount));
				mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaSource0, eTextureSource_Constant);
				mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaFunc, eTextureFunc_Replace);

				mpLowLevelGraphics->DrawQuad(vVtx);
			} else {
				mImageTrailData.mbFirstPass = false;
			}
			mImageTrailData.mlCurrentBuffer = mImageTrailData.mlCurrentBuffer == 0 ? 1 : 0;

			// Copy screen to new blur buffer
			mpLowLevelGraphics->CopyContextToTexure(mpScreenBuffer[mImageTrailData.mlCurrentBuffer], 0, cVector2l((int)mpLowLevelGraphics->GetScreenSize().x, (int)mpLowLevelGraphics->GetScreenSize().y));
		}

		mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaSource0, eTextureSource_Texture);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaSource1, eTextureSource_Previous);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_AlphaFunc, eTextureFunc_Modulate);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorSource0, eTextureSource_Texture);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorSource1, eTextureSource_Previous);
		mpLowLevelGraphics->SetTextureEnv(eTextureParam_ColorFunc, eTextureFunc_Modulate);

		mpLowLevelGraphics->SetBlendActive(false);
		mpLowLevelGraphics->SetTexture(0, NULL);
	}
}

//-----------------------------------------------------------------------

} // namespace hpl