SO3Engine
SO3CompositorLogic.cpp
Go to the documentation of this file.
1
7
8namespace SO3
9{
10
11bool SHdrHandler::compositorCreated = false;
12
13SHdrHandler::SHdrHandler()
14{
15}
16
17SHdrHandler::SHdrHandler(Ogre::Viewport* targetViewportInstance)
18{
19 // Initialize viewport
20 isEnable = false;
21 targetViewport = targetViewportInstance;
22 targetViewportScheme = targetViewport->getMaterialScheme();
23 internalHack = false;
24
25 // Generate compositor
26 ogreCompositorManager = Ogre::CompositorManager::getSingletonPtr();
27 CreateCompositors();
28
29 inputCompositorInstance = ogreCompositorManager->addCompositor(targetViewport, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", 0);
30
31 // Get clear pass, and change the clear colour
32 cachedViewportBackgroundColor = targetViewport->getBackgroundColour();
33 inputCompositorInstance->getTechnique()->getTargetPass(0)->getPass(0)->setClearColour(cachedViewportBackgroundColor);
34}
35
37{
38 // Force disable
39 SetEnable(false);
40
41 // Release compositor
42 ogreCompositorManager->removeCompositor(targetViewport, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr");
43 inputCompositorInstance = 0;
44
45 Ogre::CompositorChain* compositorChain = ogreCompositorManager->getCompositorChain(targetViewport);
46 if(compositorChain)
47 if(compositorChain->getNumCompositors() == 0)
48 ogreCompositorManager->removeCompositorChain(targetViewport);
49
50 ogreCompositorManager = 0;
51}
52
54{
55 return isEnable;
56}
57
58void SHdrHandler::SetEnable(bool enable)
59{
60 if(enable != isEnable)
61 {
62 if(enable)
63 {
64 // Add a renderTarget manager to handle viewport background color change.
65 if(!internalHack)
66 targetViewport->getTarget()->addListener(this);
67
68 // Enable base compositor
69 ogreCompositorManager->setCompositorEnabled(targetViewport, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", true);
70
71 isEnable = enable;
72 }
73 else
74 {
75 // Remove render target listener
76 if(!internalHack)
77 targetViewport->getTarget()->removeListener(this);
78
79 // Stop others compositors
80 ogreCompositorManager->setCompositorEnabled(targetViewport, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", false);
81 isEnable = enable;
82 }
83 }
84}
85
86void SHdrHandler::CreateCompositors()
87{
88 // Generate Ogre's resources for HDR
89 if(!compositorCreated)
90 {
91 Ogre::StringVector resourceGroups = Ogre::ResourceGroupManager::getSingletonPtr()->getResourceGroups();
92 if(std::find(resourceGroups.begin(), resourceGroups.end(), SO3_INTERNAL_RESOURCE_GROUP) == resourceGroups.end())
93 Ogre::ResourceGroupManager::getSingletonPtr()->createResourceGroup(SO3_INTERNAL_RESOURCE_GROUP);
94
95 // Generate a compositor that will render the scene (without transparent objects), and will old a texture to the blurred SSAO
96 GenerateHdrCompositor();
97
98 // Everything was created.
99 compositorCreated = true;
100 }
101 else
102 {
103 Ogre::MaterialManager* ogreMaterialManager = Ogre::MaterialManager::getSingletonPtr();
104 }
105}
106
107void SHdrHandler::preViewportUpdate(const Ogre::RenderTargetViewportEvent& evt)
108{
109 if(evt.source == targetViewport)
110 {
111 // Check background color
112 if(cachedViewportBackgroundColor != targetViewport->getBackgroundColour())
113 {
114 // Get clear pass, and change the clear colour
115 cachedViewportBackgroundColor = targetViewport->getBackgroundColour();
116 inputCompositorInstance->getTechnique()->getTargetPass(0)->getPass(0)->setClearColour(cachedViewportBackgroundColor);
117
118 // Reinit compos.
119 internalHack = true;
120 SetEnable(false);
121 SetEnable(true);
122 internalHack = false;
123 }
124 }
125}
126
127void SHdrHandler::GenerateHdrCompositor()
128{
129 // Create compositor
130 Ogre::CompositorPtr hdrCompositor = ogreCompositorManager->create(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", SO3_INTERNAL_RESOURCE_GROUP);
131
132 // Create the technique
133 Ogre::CompositionTechnique* hdrCompositionTechnique = hdrCompositor->createTechnique();
134 hdrCompositionTechnique->setCompositorLogicName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Logic");
135
136 Ogre::PixelFormatList pixelFormatList;
137 pixelFormatList.push_back(Ogre::PF_FLOAT16_RGB);
138
139 // Create a texture that will hold solid objects RGB
140 Ogre::CompositionTechnique::TextureDefinition* solidsTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Solids");
141 solidsTexture->width = 0;
142 solidsTexture->height = 0;
143 solidsTexture->formatList = pixelFormatList;
144 solidsTexture->scope = Ogre::CompositionTechnique::TS_CHAIN;
145
146 // Create a texture for luminance evaluation
147 Ogre::CompositionTechnique::TextureDefinition* lum0Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum0");
148 lum0Texture->width = 1;
149 lum0Texture->height = 1;
150 lum0Texture->formatList = pixelFormatList;
151 lum0Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
152
153 Ogre::CompositionTechnique::TextureDefinition* lum1Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum1");
154 lum1Texture->width = 4;
155 lum1Texture->height = 4;
156 lum1Texture->formatList = pixelFormatList;
157 lum1Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
158
159 Ogre::CompositionTechnique::TextureDefinition* lum2Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum2");
160 lum2Texture->width = 16;
161 lum2Texture->height = 16;
162 lum2Texture->formatList = pixelFormatList;
163 lum2Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
164
165 Ogre::CompositionTechnique::TextureDefinition* lum3Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum3");
166 lum3Texture->width = 64;
167 lum3Texture->height = 64;
168 lum3Texture->formatList = pixelFormatList;
169 lum3Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
170
171 Ogre::CompositionTechnique::TextureDefinition* lum4Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum4");
172 lum4Texture->width = 128;
173 lum4Texture->height = 128;
174 lum4Texture->formatList = pixelFormatList;
175 lum4Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
176
177 pixelFormatList.clear();
178 pixelFormatList.push_back(Ogre::PF_R8G8B8);
179
180 //Bright-pass filtered target (tone mapped)
181 Ogre::CompositionTechnique::TextureDefinition* brightTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/brigth");
182 brightTexture->width = 128;
183 brightTexture->height = 128;
184 brightTexture->formatList = pixelFormatList;
185 brightTexture->scope = Ogre::CompositionTechnique::TS_CHAIN;
186
187 //Bloom filter targets
188 Ogre::CompositionTechnique::TextureDefinition* bloom0Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0");
189 bloom0Texture->width = 128;
190 bloom0Texture->height = 128;
191 bloom0Texture->formatList = pixelFormatList;
192 bloom0Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
193
194 Ogre::CompositionTechnique::TextureDefinition* bloom1Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom1");
195 bloom1Texture->width = 128;
196 bloom1Texture->height = 128;
197 bloom1Texture->formatList = pixelFormatList;
198 bloom1Texture->scope = Ogre::CompositionTechnique::TS_CHAIN;
199
200 // Create the target that will render solid objects
201 Ogre::CompositionTargetPass* compositionTargetSolid = hdrCompositionTechnique->createTargetPass();
202 compositionTargetSolid->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Solids");
203 compositionTargetSolid->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
204 compositionTargetSolid->setMaterialScheme("HDR");
205
206 // Create a "clear" pass on the solid target
207 Ogre::CompositionPass* compositionTargetSolidClearPass = compositionTargetSolid->createPass();
208 compositionTargetSolidClearPass->setType(Ogre::CompositionPass::PT_CLEAR);
209
210 // Create the render solid pass
211 Ogre::CompositionPass* compositionTargetSolidRenderScenePass = compositionTargetSolid->createPass();
212 compositionTargetSolidRenderScenePass->setType(Ogre::CompositionPass::PT_RENDERSCENE);
213
214
215 // Downsample the original HDR scene to extract luminence value
216 // lum4
217 Ogre::CompositionTargetPass* compositionTargetLum4 = hdrCompositionTechnique->createTargetPass();
218 compositionTargetLum4->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum4");
219 compositionTargetLum4->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
220
221 Ogre::CompositionPass* compositionTargetLum4QuadPass = compositionTargetLum4->createPass();
222 compositionTargetLum4QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
223 compositionTargetLum4QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample2x2Luminence");
224 compositionTargetLum4QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Solids");
225 compositionTargetLum4QuadPass->setIdentifier(994);
226
227 // lum3
228 Ogre::CompositionTargetPass* compositionTargetLum3 = hdrCompositionTechnique->createTargetPass();
229 compositionTargetLum3->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum3");
230 compositionTargetLum3->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
231
232 Ogre::CompositionPass* compositionTargetLum3QuadPass = compositionTargetLum3->createPass();
233 compositionTargetLum3QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
234 compositionTargetLum3QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
235 compositionTargetLum3QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum4");
236 compositionTargetLum3QuadPass->setIdentifier(993);
237
238 // lum2
239 Ogre::CompositionTargetPass* compositionTargetLum2 = hdrCompositionTechnique->createTargetPass();
240 compositionTargetLum2->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum2");
241 compositionTargetLum2->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
242
243 Ogre::CompositionPass* compositionTargetLum2QuadPass = compositionTargetLum2->createPass();
244 compositionTargetLum2QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
245 compositionTargetLum2QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
246 compositionTargetLum2QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum3");
247 compositionTargetLum2QuadPass->setIdentifier(992);
248
249 // lum1
250 Ogre::CompositionTargetPass* compositionTargetLum1 = hdrCompositionTechnique->createTargetPass();
251 compositionTargetLum1->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum1");
252 compositionTargetLum1->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
253
254 Ogre::CompositionPass* compositionTargetLum1QuadPass = compositionTargetLum1->createPass();
255 compositionTargetLum1QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
256 compositionTargetLum1QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
257 compositionTargetLum1QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum2");
258 compositionTargetLum1QuadPass->setIdentifier(991);
259
260 // lum0
261 Ogre::CompositionTargetPass* compositionTargetLum0 = hdrCompositionTechnique->createTargetPass();
262 compositionTargetLum0->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum0");
263 compositionTargetLum0->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
264
265 Ogre::CompositionPass* compositionTargetLum0QuadPass = compositionTargetLum0->createPass();
266 compositionTargetLum0QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
267 compositionTargetLum0QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
268 compositionTargetLum0QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum1");
269 compositionTargetLum0QuadPass->setIdentifier(990);
270
271 // Brigth pass
272 Ogre::CompositionTargetPass* compositionTargetBright = hdrCompositionTechnique->createTargetPass();
273 compositionTargetBright->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/brigth");
274 compositionTargetBright->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
275
276 Ogre::CompositionPass* compositionTargetBrightQuadPass = compositionTargetLum0->createPass();
277 compositionTargetBrightQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
278 compositionTargetBrightQuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3Brightpass");
279 compositionTargetBrightQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Solids");
280 compositionTargetBrightQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum0");
281 compositionTargetBrightQuadPass->setIdentifier(800);
282
283 // Bloom1 pass
284 Ogre::CompositionTargetPass* compositionTargetBloom1 = hdrCompositionTechnique->createTargetPass();
285 compositionTargetBloom1->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom1");
286 compositionTargetBloom1->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
287
288 Ogre::CompositionPass* compositionTargetBloom1QuadPass = compositionTargetLum0->createPass();
289 compositionTargetBloom1QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
290 compositionTargetBloom1QuadPass->setMaterialName("Ogre/Compositor/HDR/GaussianBloom");
291 compositionTargetBloom1QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/brigth");
292 compositionTargetBloom1QuadPass->setIdentifier(701);
293
294 // Bloom0 pass
295 Ogre::CompositionTargetPass* compositionTargetBloom0 = hdrCompositionTechnique->createTargetPass();
296 compositionTargetBloom0->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0");
297 compositionTargetBloom0->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
298
299 Ogre::CompositionPass* compositionTargetBloom0QuadPass = compositionTargetLum0->createPass();
300 compositionTargetBloom0QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
301 compositionTargetBloom0QuadPass->setMaterialName("Ogre/Compositor/HDR/GaussianBloom");
302 compositionTargetBloom0QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom1");
303 compositionTargetBloom0QuadPass->setIdentifier(700);
304
305 // Create the out target
306 Ogre::CompositionTargetPass* compositionOutTarget = hdrCompositionTechnique->getOutputTargetPass();
307 compositionOutTarget->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
308
309 Ogre::CompositionPass* compositionOutTargetQuadPass = compositionOutTarget->createPass();
310 compositionOutTargetQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
311 compositionOutTargetQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Solids");
312 compositionOutTargetQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0");
313 compositionOutTargetQuadPass->setInput(2, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum0");
314}
315
316}
void SetEnable(bool enable)