Project

General

Profile

SO3Engine
SO3HdrHandler.cpp
Go to the documentation of this file.
1
8
9namespace SO3
10{
11
12bool SHdrHandler::compositorCreated = false;
13// global since the compositor logic can not be per SHdrHandler
14float SHdrHandler::mBlurAmount = 0.2f;
16float SHdrHandler::mGamma = 1.0f;
17
19{
20 compositorCreated = false;
21}
22
23SHdrHandler::SHdrHandler()
24{
25}
26
27SHdrHandler::SHdrHandler(SViewPort* targetViewportInstance)
28{
29 // Initialize
30 inputCompositorInstance = 0;
31 isEnable = false;
32 targetViewport = targetViewportInstance;
33 targetViewportScheme = targetViewport->GetOgreViewPortPointer()->getMaterialScheme();
34
35 // Generate compositor
36 ogreCompositorManager = Ogre::CompositorManager::getSingletonPtr();
37
38 //test if shaders are supported by the card
39 const Ogre::RenderSystemCapabilities* caps = SRoot::getSingleton().GetOgreRenderSystem()->getCapabilities();
40 isSupported = caps && caps->hasCapability(Ogre::RSC_VERTEX_PROGRAM) &&
41 caps->hasCapability(Ogre::RSC_FRAGMENT_PROGRAM) &&
42 caps->hasCapability(Ogre::RSC_NON_POWER_OF_2_TEXTURES) &&
43 !caps->hasCapability(Ogre::RSC_MRT_SAME_BIT_DEPTHS) &&
44 (caps->isShaderProfileSupported("ps_3_0") || caps->isShaderProfileSupported("ps_4_0") || (caps->isShaderProfileSupported("arbfp1")) || (caps->isShaderProfileSupported("glsles")) || (caps->isShaderProfileSupported("glsl300es")));
45
46 if (isSupported)
47 {
48 CreateCompositors();
49
50 //HDR must be first on chain
51 inputCompositorInstance = ogreCompositorManager->addCompositor(targetViewport->GetOgreViewPortPointer(), std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr");
52
53 // Get clear pass, and change the clear colour to automatic mode
54 if(inputCompositorInstance)
55 {
56 inputCompositorInstance->getTechnique()->getTargetPass(1)->getPasses()[0]->setAutomaticColour(true);
57 }
58 }
59}
60
62{
63 if (isSupported)
64 {
65 // Force disable
66 SetEnable(false);
67
68 // Release compositor
69 if (inputCompositorInstance)
70 {
71 ogreCompositorManager->removeCompositor(targetViewport->GetOgreViewPortPointer(), std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr");
72 inputCompositorInstance = 0;
73 }
74
75 Ogre::CompositorChain* compositorChain = ogreCompositorManager->getCompositorChain(targetViewport->GetOgreViewPortPointer());
76 if(compositorChain)
77 if(compositorChain->getCompositorInstances().empty())
78 ogreCompositorManager->removeCompositorChain(targetViewport->GetOgreViewPortPointer());
79
80 ogreCompositorManager = 0;
81 }
82}
83
85{
86 return isEnable;
87}
88
89void SHdrHandler::SetEnable(bool enable)
90{
91 if (isSupported)
92 {
93 if(enable != isEnable)
94 {
95 isEnable = enable;
96 // Enable base compositor
97 if (!targetViewport->UpdateStereoCompositorState(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", isEnable))
98 ogreCompositorManager->setCompositorEnabled(targetViewport->GetOgreViewPortPointer(), std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", isEnable);
99 }
100 }
101}
102
103
105{
106 mBlurAmount = amount;
107}
108
110{
111 mEffectAmount = amount;
112}
113
114void SHdrHandler::SetGamma(float gamma)
115{
116 mGamma = gamma;
117}
118
119void SHdrHandler::CreateCompositors()
120{
121 // Generate Ogre's resources for HDR
122 if(!compositorCreated)
123 {
124 Ogre::StringVector resourceGroups = Ogre::ResourceGroupManager::getSingletonPtr()->getResourceGroups();
125 if(std::find(resourceGroups.begin(), resourceGroups.end(), SO3_INTERNAL_RESOURCE_GROUP) == resourceGroups.end())
126 Ogre::ResourceGroupManager::getSingletonPtr()->createResourceGroup(SO3_INTERNAL_RESOURCE_GROUP);
127
128 // Generate a compositor that will render the scene (without transparent objects), and will old a texture to the blurred SSAO
129 GenerateHdrCompositor();
130
131 // Everything was created.
132 compositorCreated = true;
133 }
134}
135
136void SHdrHandler::GenerateHdrCompositor()
137{
138 // Create compositor
139 Ogre::CompositorPtr hdrCompositor = ogreCompositorManager->create(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr", SO3_INTERNAL_RESOURCE_GROUP);
140
141 // Create the technique
142 Ogre::CompositionTechnique* hdrCompositionTechnique = hdrCompositor->createTechnique();
143 hdrCompositionTechnique->setCompositorLogicName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Logic");
144
145 Ogre::PixelFormat rttformat = Ogre::PF_BYTE_RGB;
147
148 Ogre::PixelFormatList pixelFormatList;
149 pixelFormatList.push_back(rttformat);
150
151 // Create a texture that will hold the previous render
152 Ogre::CompositionTechnique::TextureDefinition* previousTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Previous");
153 previousTexture->width = 0;
154 previousTexture->height = 0;
155 previousTexture->formatList = pixelFormatList;
156
157 Ogre::CompositionTargetPass* compositionTargetPrevious = hdrCompositionTechnique->createTargetPass();
158 compositionTargetPrevious->setShadowsEnabled(false);
159 compositionTargetPrevious->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Previous");
160 compositionTargetPrevious->setInputMode(Ogre::CompositionTargetPass::IM_PREVIOUS);
161
162 SRoot::getSingleton().GetRttPixelFormat(rttformat, false, false);
163 pixelFormatList.clear();
164 pixelFormatList.push_back(rttformat);
165
166 // Create a texture for luminance evaluation
167 Ogre::CompositionTechnique::TextureDefinition* lum4Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum4");
168 lum4Texture->width = 128;
169 lum4Texture->height = 128;
170 lum4Texture->formatList = pixelFormatList;
171
172 Ogre::CompositionTechnique::TextureDefinition* lum3Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum3");
173 lum3Texture->width = 64;
174 lum3Texture->height = 64;
175 lum3Texture->formatList = pixelFormatList;
176
177 Ogre::CompositionTechnique::TextureDefinition* lum2Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum2");
178 lum2Texture->width = 16;
179 lum2Texture->height = 16;
180 lum2Texture->formatList = pixelFormatList;
181
182 Ogre::CompositionTechnique::TextureDefinition* lum1Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum1");
183 lum1Texture->width = 4;
184 lum1Texture->height = 4;
185 lum1Texture->formatList = pixelFormatList;
186
187 Ogre::CompositionTechnique::TextureDefinition* lum0Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum0");
188 lum0Texture->width = 1;
189 lum0Texture->height = 1;
190 lum0Texture->formatList = pixelFormatList;
191
192 // lum4
193 Ogre::CompositionTargetPass* compositionTargetLum4 = hdrCompositionTechnique->createTargetPass();
194 compositionTargetLum4->setShadowsEnabled(false);
195 compositionTargetLum4->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum4");
196 compositionTargetLum4->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
197
198 Ogre::CompositionPass* compositionTargetLum4QuadPass = compositionTargetLum4->createPass();
199 compositionTargetLum4QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
200 compositionTargetLum4QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample2x2Luminence");
201 compositionTargetLum4QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/Previous");
202 compositionTargetLum4QuadPass->setIdentifier(994);
203
204 // lum3
205 Ogre::CompositionTargetPass* compositionTargetLum3 = hdrCompositionTechnique->createTargetPass();
206 compositionTargetLum3->setShadowsEnabled(false);
207 compositionTargetLum3->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum3");
208 compositionTargetLum3->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
209
210 Ogre::CompositionPass* compositionTargetLum3QuadPass = compositionTargetLum3->createPass();
211 compositionTargetLum3QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
212 compositionTargetLum3QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
213 compositionTargetLum3QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum4");
214 compositionTargetLum3QuadPass->setIdentifier(993);
215
216 // lum2
217 Ogre::CompositionTargetPass* compositionTargetLum2 = hdrCompositionTechnique->createTargetPass();
218 compositionTargetLum2->setShadowsEnabled(false);
219 compositionTargetLum2->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum2");
220 compositionTargetLum2->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
221
222 Ogre::CompositionPass* compositionTargetLum2QuadPass = compositionTargetLum2->createPass();
223 compositionTargetLum2QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
224 compositionTargetLum2QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
225 compositionTargetLum2QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum3");
226 compositionTargetLum2QuadPass->setIdentifier(992);
227
228 // lum1
229 Ogre::CompositionTargetPass* compositionTargetLum1 = hdrCompositionTechnique->createTargetPass();
230 compositionTargetLum1->setShadowsEnabled(false);
231 compositionTargetLum1->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum1");
232 compositionTargetLum1->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
233
234 Ogre::CompositionPass* compositionTargetLum1QuadPass = compositionTargetLum1->createPass();
235 compositionTargetLum1QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
236 compositionTargetLum1QuadPass->setMaterialName("Ogre/Compositor/HDR/Downsample3x3");
237 compositionTargetLum1QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum2");
238 compositionTargetLum1QuadPass->setIdentifier(991);
239
240 // lum0
241 Ogre::CompositionTargetPass* compositionTargetLum0 = hdrCompositionTechnique->createTargetPass();
242 compositionTargetLum0->setShadowsEnabled(false);
243 compositionTargetLum0->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum0");
244 compositionTargetLum0->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
245
246 Ogre::CompositionPass* compositionTargetLum0QuadPass = compositionTargetLum0->createPass();
247 compositionTargetLum0QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
248 compositionTargetLum0QuadPass->setMaterialName("Ogre/Compositor/HDR/FinalLuminance");
249 compositionTargetLum0QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/lum1");
250 compositionTargetLum1QuadPass->setIdentifier(990);
251
252 // adated luminance
253 Ogre::CompositionTechnique::TextureDefinition* adaptLastlumTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptLastLum");
254 adaptLastlumTexture->width = 1;
255 adaptLastlumTexture->height = 1;
256 adaptLastlumTexture->formatList = pixelFormatList;
257
258 Ogre::CompositionTechnique::TextureDefinition* adaptedlumTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptedLum");
259 adaptedlumTexture->width = 1;
260 adaptedlumTexture->height = 1;
261 adaptedlumTexture->formatList = pixelFormatList;
262
263 Ogre::CompositionTargetPass* compositionTargetAdaptLastLum = hdrCompositionTechnique->createTargetPass();
264 compositionTargetAdaptLastLum->setShadowsEnabled(false);
265 compositionTargetAdaptLastLum->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptLastLum");
266 compositionTargetAdaptLastLum->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
267
268 Ogre::CompositionPass* compositionTargetAdaptLastLumQuadPass = compositionTargetAdaptLastLum->createPass();
269 compositionTargetAdaptLastLumQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
270 compositionTargetAdaptLastLumQuadPass->setMaterialName("Ogre/Compositor/HDR/Copy");
271 compositionTargetAdaptLastLumQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptedLum");
272
273 Ogre::CompositionTargetPass* compositionTargetAdaptedLum = hdrCompositionTechnique->createTargetPass();
274 compositionTargetAdaptedLum->setShadowsEnabled(false);
275 compositionTargetAdaptedLum->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptedLum");
276 compositionTargetAdaptedLum->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
277
278 Ogre::CompositionPass* compositionTargetAdaptedLumQuadPass = compositionTargetAdaptedLum->createPass();
279 compositionTargetAdaptedLumQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
280 compositionTargetAdaptedLumQuadPass->setMaterialName("Ogre/Compositor/HDR/AdaptLuminance");
281 compositionTargetAdaptedLumQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum0");
282 compositionTargetAdaptedLumQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptLastLum");
283
284 // key for tone mapping
285 Ogre::CompositionTechnique::TextureDefinition* keyTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/key");
286 keyTexture->width = 1;
287 keyTexture->height = 1;
288 keyTexture->formatList = pixelFormatList;
289
290 Ogre::CompositionTargetPass* compositionTargetKey = hdrCompositionTechnique->createTargetPass();
291 compositionTargetKey->setShadowsEnabled(false);
292 compositionTargetKey->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/key");
293 compositionTargetKey->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
294
295 Ogre::CompositionPass* compositionTargetKeyQuadPass = compositionTargetKey->createPass();
296 compositionTargetKeyQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
297 compositionTargetKeyQuadPass->setMaterialName("Ogre/Compositor/HDR/AutoKey");
298 compositionTargetKeyQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/lum0");
300 pixelFormatList.clear();
301 pixelFormatList.push_back(rttformat);
302
303 // bright pass
304 Ogre::CompositionTechnique::TextureDefinition* brightPassTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/brightpass");
305 brightPassTexture->width = 128;
306 brightPassTexture->height = 128;
307 brightPassTexture->formatList = pixelFormatList;
308
309 Ogre::CompositionTargetPass* compositionTargetBrightPass = hdrCompositionTechnique->createTargetPass();
310 compositionTargetBrightPass->setShadowsEnabled(false);
311 compositionTargetBrightPass->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/brightpass");
312 compositionTargetBrightPass->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
313
314 Ogre::CompositionPass* compositionTargetBrightQuadPass = compositionTargetBrightPass->createPass();
315 compositionTargetBrightQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
316 compositionTargetBrightQuadPass->setMaterialName("Ogre/Compositor/HDR/Brightpass");
317 compositionTargetBrightQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/Previous");
318 compositionTargetBrightQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/adaptedLum");
319 compositionTargetBrightQuadPass->setInput(2, std::string(SO3_INTERNAL_RESOURCE_GROUP) + "/Hdr/Input/Texture/key");
320 compositionTargetBrightQuadPass->setIdentifier(800);
321
322 //Bloom filter targets
323 Ogre::CompositionTechnique::TextureDefinition* bloom0Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0");
324 bloom0Texture->width = 128;
325 bloom0Texture->height = 128;
326 bloom0Texture->formatList = pixelFormatList;
327
328 Ogre::CompositionTechnique::TextureDefinition* bloom1Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom1");
329 bloom1Texture->width = 128;
330 bloom1Texture->height = 128;
331 bloom1Texture->formatList = pixelFormatList;
332
333 // Bloom1 pass
334 Ogre::CompositionTargetPass* compositionTargetBloom1 = hdrCompositionTechnique->createTargetPass();
335 compositionTargetBloom1->setShadowsEnabled(false);
336 compositionTargetBloom1->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom1");
337 compositionTargetBloom1->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
338
339 Ogre::CompositionPass* compositionTargetBloom1QuadPass = compositionTargetBloom1->createPass();
340 compositionTargetBloom1QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
341 compositionTargetBloom1QuadPass->setMaterialName("Ogre/Compositor/HDR/GaussianBloom");
342 compositionTargetBloom1QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/brightpass");
343 compositionTargetBloom1QuadPass->setIdentifier(701);
344
345 // Bloom0 pass
346 Ogre::CompositionTargetPass* compositionTargetBloom0 = hdrCompositionTechnique->createTargetPass();
347 compositionTargetBloom0->setShadowsEnabled(false);
348 compositionTargetBloom0->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0");
349 compositionTargetBloom0->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
350
351 Ogre::CompositionPass* compositionTargetBloom0QuadPass = compositionTargetBloom0->createPass();
352 compositionTargetBloom0QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
353 compositionTargetBloom0QuadPass->setMaterialName("Ogre/Compositor/HDR/GaussianBloom");
354 compositionTargetBloom0QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom1");
355 compositionTargetBloom0QuadPass->setIdentifier(700);
356
357 // Create the out target
358 Ogre::CompositionTargetPass* compositionOutTarget = hdrCompositionTechnique->getOutputTargetPass();
359 compositionOutTarget->setShadowsEnabled(false);
360 compositionOutTarget->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
361
362 Ogre::CompositionPass* compositionOutTargetQuadPass = compositionOutTarget->createPass();
363 compositionOutTargetQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
364
365 compositionOutTargetQuadPass->setIdentifier(1000);
366 compositionOutTargetQuadPass->setMaterialName("Ogre/Compositor/HDR/ToneMapping");
367 compositionOutTargetQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/Previous");
368 compositionOutTargetQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0");
369 compositionOutTargetQuadPass->setInput(2, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/adaptedLum");
370 compositionOutTargetQuadPass->setInput(3, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/key");
371
372 //debug
373 //compositionOutTargetQuadPass->setMaterialName("Ogre/Compositor/HDR/Copy");
374 //compositionOutTargetQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/brightpass");
375
376
377 //register the compositor
378 Ogre::CompositorManager& compMgr = Ogre::CompositorManager::getSingleton();
379 compMgr.registerCompositorLogic(std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Logic", new SHdrLogic);
380}
381
382//SHdrLogic
383Ogre::CompositorInstance::Listener* SHdrLogic::createListener(Ogre::CompositorInstance* instance)
384{
385 SHdrListener* listener = new SHdrListener();
386 listener->notifyCompositor(instance);
387 return listener;
388}
389
390//SHdrListener
394
398
399void SHdrListener::notifyCompositor(Ogre::CompositorInstance* instance)
400{
401 // Get some RTT dimensions for later calculations
402 Ogre::CompositionTechnique::TextureDefinitions defList = instance->getTechnique()->getTextureDefinitions();
403 Ogre::CompositionTechnique::TextureDefinitions::const_iterator defIter;
404 for (defIter = defList.begin(); defIter != defList.end(); ++defIter)
405 {
406 Ogre::CompositionTechnique::TextureDefinition* def = *defIter;
407
408 if(def->name == (std::string(SO3_INTERNAL_RESOURCE_GROUP)+"/Hdr/Input/Texture/bloom0"))
409 {
410 mBloomSize = (int)def->width; // should be square
411 // Calculate gaussian texture offsets & weights
412 float deviation = 5.0f;
413 float texelSize = 1.0f / (float)mBloomSize;
414
415 // central sample, no offset
416 mBloomTexOffsetsHorz[0][0] = 0.0f;
417 mBloomTexOffsetsHorz[0][1] = 0.0f;
418 mBloomTexOffsetsVert[0][0] = 0.0f;
419 mBloomTexOffsetsVert[0][1] = 0.0f;
420 mBloomTexWeights[0][0] = mBloomTexWeights[0][1] = mBloomTexWeights[0][2] = Ogre::Math::gaussianDistribution(0, 0, deviation);
421 mBloomTexWeights[0][3] = 1.0f;
422
423 // 'pre' samples
424 for(int i = 1; i < 8; ++i)
425 {
426 mBloomTexWeights[i][0] = mBloomTexWeights[i][1] = mBloomTexWeights[i][2] = 1.25f * Ogre::Math::gaussianDistribution((float)i, 0, deviation);
427 mBloomTexWeights[i][3] = 1.0f;
428 mBloomTexOffsetsHorz[i][0] = i * texelSize;
429 mBloomTexOffsetsHorz[i][1] = 0.0f;
430 mBloomTexOffsetsVert[i][0] = 0.0f;
431 mBloomTexOffsetsVert[i][1] = i * texelSize;
432 }
433 // 'post' samples
434 for(int i = 8; i < 15; ++i)
435 {
436 mBloomTexWeights[i][0] = mBloomTexWeights[i][1] = mBloomTexWeights[i][2] = mBloomTexWeights[i - 7][0];
437 mBloomTexWeights[i][3] = 1.0f;
438
440 mBloomTexOffsetsHorz[i][1] = 0.0f;
441 mBloomTexOffsetsVert[i][0] = 0.0f;
443 }
444 }
445 }
446}
447
448void SHdrListener::notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
449{
450 // Prepare the fragment params offsets
451 switch(pass_id)
452 {
453 case 993: // rt_lum3
454 case 992: // rt_lum2
455 case 991: // rt_lum1
456 case 990: // rt_lum0
457 break;
458 case 800: // rt_brightpass
459 break;
460 case 701: // rt_bloom1
461 {
462 // horizontal bloom
463 mat->load();
464 Ogre::GpuProgramParametersSharedPtr fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
465 fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsHorz[0], 15);
466 fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
467
468 break;
469 }
470 case 700: // rt_bloom0
471 {
472 // vertical bloom
473 mat->load();
474 Ogre::GpuProgramParametersSharedPtr fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
475 fparams->setNamedConstant("sampleOffsets", mBloomTexOffsetsVert[0], 15);
476 fparams->setNamedConstant("sampleWeights", mBloomTexWeights[0], 15);
477
478 break;
479 }
480 default:
481 break;
482 }
483}
484
485void SHdrListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
486{
487 if (pass_id == 1000 && mat->getSupportedTechniques().size() > 0)
488 {
489 Ogre::GpuProgramParametersSharedPtr toneShaderParams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
490 if (toneShaderParams)
491 {
492 if (toneShaderParams->_findNamedConstantDefinition("blurAmount"))
493 toneShaderParams->setNamedConstant("blurAmount", SHdrHandler::mBlurAmount);
494
495 if (toneShaderParams->_findNamedConstantDefinition("effectAmount"))
496 toneShaderParams->setNamedConstant("effectAmount",SHdrHandler::mEffectAmount);
497
498 if (toneShaderParams->_findNamedConstantDefinition("gamma"))
499 toneShaderParams->setNamedConstant("gamma", SHdrHandler::mGamma);
500 }
501 }
502}
503
504}
void SetEnable(bool enable)
void SetEffectAmount(float amount)
void SetBlurAmount(float amount)
static float mEffectAmount
static void Reset()
static float mGamma
void SetGamma(float gamma)
static float mBlurAmount
void notifyCompositor(Ogre::CompositorInstance *instance)
virtual void notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
float mBloomTexOffsetsVert[15][4]
virtual void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
float mBloomTexOffsetsHorz[15][4]
float mBloomTexWeights[15][4]
virtual Ogre::CompositorInstance::Listener * createListener(Ogre::CompositorInstance *instance)
bool GetRttPixelFormat(Ogre::PixelFormat &format, bool alpha=false, bool floattex=false)
Definition SO3Root.cpp:650
Ogre::RenderSystem * GetOgreRenderSystem()
Definition SO3Root.cpp:865
static SRoot & getSingleton()
Definition SO3Root.cpp:116
Ogre::Viewport * GetOgreViewPortPointer()
bool UpdateStereoCompositorState(const Ogre::String compname, bool state)