-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDeferredLightCP.cpp
134 lines (114 loc) · 4.01 KB
/
DeferredLightCP.cpp
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
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2009 Torus Knot Software Ltd
Also see acknowledgements in Readme.html
You may use this sample code for anything you like, it is not covered by the
same license as the rest of the engine.
-----------------------------------------------------------------------------
*/
#include "DeferredLightCP.h"
#include "Ogre.h"
using namespace Ogre;
#include "LightMaterialGenerator.h"
//-----------------------------------------------------------------------
DeferredLightRenderOperation::DeferredLightRenderOperation(
CompositorInstance* instance, const CompositionPass* pass)
{
mViewport = instance->getChain()->getViewport();
//Get the names of the GBuffer textures
const CompositionPass::InputTex& input0 = pass->getInput(0);
mTexName0 = instance->getTextureInstanceName(input0.name, input0.mrtIndex);
const CompositionPass::InputTex& input1 = pass->getInput(1);
mTexName1 = instance->getTextureInstanceName(input1.name, input1.mrtIndex);
// Create lights material generator
mLightMaterialGenerator = new LightMaterialGenerator();
// Create the ambient light
mAmbientLight = new AmbientLight();
const MaterialPtr& mat = mAmbientLight->getMaterial();
mat->load();
}
//-----------------------------------------------------------------------
DLight* DeferredLightRenderOperation::createDLight(Ogre::Light* light)
{
DLight *rv = new DLight(mLightMaterialGenerator,light);
mLights[light] = rv;
return rv;
}
//-----------------------------------------------------------------------
void injectTechnique(SceneManager* sm, Technique* tech, Renderable* rend, const Ogre::LightList* lightList)
{
for(unsigned short i=0; i<tech->getNumPasses(); ++i)
{
Ogre::Pass* pass = tech->getPass(i);
if (lightList != 0)
{
sm->_injectRenderWithPass(pass, rend, false, false, lightList);
}
else
{
sm->_injectRenderWithPass(pass, rend, false);
}
}
}
//-----------------------------------------------------------------------
void DeferredLightRenderOperation::execute(SceneManager *sm, RenderSystem *rs)
{
Ogre::Camera* cam = mViewport->getCamera();
mAmbientLight->updateFromCamera(cam);
Technique* tech = mAmbientLight->getMaterial()->getBestTechnique();
injectTechnique(sm, tech, mAmbientLight, 0);
const LightList& lightList = sm->_getLightsAffectingFrustum();
for (LightList::const_iterator it = lightList.begin(); it != lightList.end(); it++)
{
Light* light = *it;
Ogre::LightList ll;
ll.push_back(light);
//if (++i != 2) continue;
//if (light->getType() != Light::LT_DIRECTIONAL) continue;
//if (light->getDiffuseColour() != ColourValue::Red) continue;
LightsMap::iterator dLightIt = mLights.find(light);
DLight* dLight = 0;
if (dLightIt == mLights.end())
{
dLight = createDLight(light);
}
else
{
dLight = dLightIt->second;
dLight->updateFromParent();
}
dLight->updateFromCamera(cam);
tech = dLight->getMaterial()->getBestTechnique();
//Update shadow texture
if (dLight->getCastChadows())
{
SceneManager::RenderContext* context = sm->_pauseRendering();
sm->prepareShadowTextures(cam, mViewport, &ll);
sm->_resumeRendering(context);
Pass* pass = tech->getPass(0);
TextureUnitState* tus = pass->getTextureUnitState("ShadowMap");
assert(tus);
const TexturePtr& shadowTex = sm->getShadowTexture(0);
if (tus->_getTexturePtr() != shadowTex)
{
tus->_setTexturePtr(shadowTex);
}
}
injectTechnique(sm, tech, dLight, &ll);
}
}
//-----------------------------------------------------------------------
DeferredLightRenderOperation::~DeferredLightRenderOperation()
{
for (LightsMap::iterator it = mLights.begin(); it != mLights.end(); ++it)
{
delete it->second;
}
mLights.clear();
delete mAmbientLight;
delete mLightMaterialGenerator;
}
//-----------------------------------------------------------------------