14#include <OgreDepthBuffer.h>
19 SDynamicCubeMap::SDynamicCubeMap() : SNode(0,
"", SNode::DYNAMIC_CUBE_MAP_ID)
24 SDynamicCubeMap::SDynamicCubeMap(
SScene* parent,
const std::string& dynamicCubeMapName) :
SNode(parent, dynamicCubeMapName,
SNode::DYNAMIC_CUBE_MAP_ID)
27 cubeMapScheme = dynamicCubeMapName +
"_SO3_DynamicCubeMap_Scheme";
33 needTextureUpdate =
false;
34 renderingEnable =
false;
42 camNode->attachObject(cubeMapCamera);
45 cubeMapCamera->setFOVy(Ogre::Degree(90));
46 cubeMapCamera->setAspectRatio(1);
47 cubeMapCamera->setNearClipDistance(Ogre::Real(0.001));
50 _CreateCubeMapTexture(size);
53 materialTransparent = Ogre::MaterialManager::getSingleton().create(
"SO3_Material_" + cubeMapScheme, SO3_INTERNAL_DYNAMIC_RESOURCE_GROUP);
54 materialTransparent->getTechnique(0)->setColourWriteEnabled(
false);
55 materialTransparent->getTechnique(0)->setDepthCheckEnabled(
false);
56 materialTransparent->getTechnique(0)->setDepthWriteEnabled(
false);
57 materialTransparent->getTechnique(0)->getPass(0)->setName(
"SO3/TRANSPARENT/CUBEMAP");
58 materialTransparent->getTechnique(0)->getPass(0)->setVertexProgram(
"SO3/Internal/Default_nolight_vp");
59 materialTransparent->getTechnique(0)->getPass(0)->setFragmentProgram(
"SO3/Internal/Default_fp");
60 materialTransparent->load();
63 Ogre::Root::getSingleton().addFrameListener(
this);
76 Ogre::Root::getSingleton().removeFrameListener(
this);
80 _DeleteCubeMapTexture();
85 Ogre::MaterialManager::getSingleton().remove(materialTransparent->getHandle());
86 materialTransparent.reset();
98 double result = log((
double)newSize) / log((
double)2.0);
99 double fractpart, intpart;
100 fractpart = modf(result, &intpart);
103 bool savedState = state;
108 _CreateCubeMapTexture(newSize);
114 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS,
"Cube map texture size must be a power of 2 number!",
"SDynamicCubeMap::SetSize");
126 if (targetMaterial != 0)
128 bool savedState = state;
131 techIndex = targetTechnique;
132 passIndex = targetPass;
133 unitIndex = targetTextureUnit;
138 void SDynamicCubeMap::SetTextureUnit()
143 Ogre::Material::Techniques suptechs = material->getSupportedTechniques();
144 Ogre::Material::Techniques techs = material->getTechniques();
146 if (techIndex >= techs.size() || (suptechs.size() == 0))
149 Ogre::Technique::Passes matPasses;
150 Ogre::Pass* tmpPass = 0;
152 if (caps && caps->hasCapability(Ogre::RSC_FIXED_FUNCTION) ==
false)
154 matPasses = suptechs[0]->getPasses();
155 if (passIndex >= matPasses.size())
158 tmpPass = matPasses[passIndex];
162 matPasses = techs[techIndex]->getPasses();
163 if (techIndex >= matPasses.size())
166 tmpPass = matPasses[techIndex];
170 Ogre::TextureUnitState* texState = 0;
171 Ogre::Pass::TextureUnitStates texs = tmpPass->getTextureUnitStates();
172 if ((texs.size() > 0) && (unitIndex < texs.size()))
173 texState = texs[unitIndex];
177 oldTexture = texState->_getTexturePtr();
178 texState->setTexture(cubeMapTexture);
180 Ogre::Any bindedSPass = tmpPass->getUserObjectBindings().getUserAny(
"SPass");
181 if (bindedSPass.has_value())
183 SPass* pass = Ogre::any_cast<SPass*> (bindedSPass);
190 void SDynamicCubeMap::ResetTextureUnit()
195 Ogre::Material::Techniques suptechs = material->getSupportedTechniques();
196 Ogre::Material::Techniques techs = material->getTechniques();
198 if (techIndex >= techs.size() || (suptechs.size() == 0))
201 Ogre::Technique::Passes matPasses;
202 Ogre::Pass* tmpPass = 0;
204 if (caps && caps->hasCapability(Ogre::RSC_FIXED_FUNCTION) ==
false)
206 matPasses = suptechs[0]->getPasses();
207 if (passIndex >= matPasses.size())
210 tmpPass = matPasses[passIndex];
214 matPasses = techs[techIndex]->getPasses();
215 if (techIndex >= matPasses.size())
218 tmpPass = matPasses[techIndex];
222 Ogre::TextureUnitState* texState = 0;
223 Ogre::Pass::TextureUnitStates texs = tmpPass->getTextureUnitStates();
224 if ((texs.size() > 0) && (unitIndex < texs.size()))
225 texState = texs[unitIndex];
230 texState->setBlank();
232 texState->setTexture(oldTexture);
234 Ogre::Any bindedSPass = tmpPass->getUserObjectBindings().getUserAny(
"SPass");
235 if (bindedSPass.has_value())
237 SPass* pass = Ogre::any_cast<SPass*> (bindedSPass);
254 if ((newState ==
true) && (state ==
false))
263 else if ((newState ==
false) && (state ==
true))
279 if ((newAutoUpdateValue ==
true) && (autoUpdate ==
false))
282 Ogre::Root::getSingleton().removeFrameListener(
this);
285 renderingEnable =
false;
288 else if ((newAutoUpdateValue ==
false) && (autoUpdate ==
true))
294 Ogre::Root::getSingleton().addFrameListener(
this);
308 void SDynamicCubeMap::EnableRendering()
310 if (!renderingEnable && cubeMapTexture)
312 for (
unsigned int i = 0; i < 6; i++)
314 if (cubeMapTargets[i])
316 cubeMapTargets[i]->setAutoUpdated(
true);
317 cubeMapTargets[i]->addListener(
this);
321 Ogre::MaterialManager::getSingleton().addListener(
this);
322 renderingEnable =
true;
326 void SDynamicCubeMap::DisableRendering()
328 if (renderingEnable && cubeMapTexture)
330 for (
unsigned int i = 0; i < 6; i++)
332 if (cubeMapTargets[i])
334 cubeMapTargets[i]->setAutoUpdated(
false);
335 cubeMapTargets[i]->removeListener(
this);
339 Ogre::MaterialManager::getSingleton().removeListener(
this);
340 renderingEnable =
false;
344 void SDynamicCubeMap::_CreateCubeMapTexture(
const unsigned int& newSize)
346 _DeleteCubeMapTexture();
349 Ogre::PixelFormat rttformat;
351 cubeMapTexture = Ogre::TextureManager::getSingleton().createManual(
"texture_" + cubeMapScheme, SO3_INTERNAL_DYNAMIC_READABLE_RESOURCE_GROUP, Ogre::TEX_TYPE_CUBE_MAP, newSize, newSize, 0, rttformat, Ogre::TU_RENDERTARGET);
353 for (
unsigned int i = 0; i < 6; i++)
355 cubeMapTargets[i] = cubeMapTexture->getBuffer(i)->getRenderTarget();
356 cubeMapTargets[i]->removeAllViewports();
359 cubeMapTargets[i]->setAutoUpdated(
false);
361 cubeMapTargets[i]->setDepthBufferPool(Ogre::DepthBuffer::POOL_NO_DEPTH);
364 Ogre::Viewport* viewport = cubeMapTargets[i]->addViewport(cubeMapCamera);
365 viewport->setOverlaysEnabled(
false);
366 viewport->setMaterialScheme(cubeMapScheme);
369 catch (Ogre::Exception &e)
371 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"An exception has occurred: " + e.getDescription());
372 cubeMapTexture.reset();
376 void SDynamicCubeMap::_DeleteCubeMapTexture()
381 for (
unsigned int i = 0; i < 6; i++)
383 cubeMapTargets[i]->removeAllViewports();
384 Ogre::Root::getSingleton().detachRenderTarget(cubeMapTargets[i]);
385 cubeMapTargets[i] = 0;
388 Ogre::TextureManager::getSingleton().remove(cubeMapTexture->getHandle());
389 cubeMapTexture.reset();
396 camNode->setOrientation((camNode->getParentSceneNode()->_getDerivedOrientation().Inverse() * Ogre::Quaternion::IDENTITY));
397 if (evt.source == cubeMapTargets[0])
398 camNode->yaw(Ogre::Degree(-90));
399 else if (evt.source == cubeMapTargets[1])
400 camNode->yaw(Ogre::Degree(90));
401 else if (evt.source == cubeMapTargets[2])
402 camNode->pitch(Ogre::Degree(90));
403 else if (evt.source == cubeMapTargets[3])
404 camNode->pitch(Ogre::Degree(-90));
405 else if (evt.source == cubeMapTargets[5])
406 camNode->yaw(Ogre::Degree(180));
413 Ogre::Technique*
SDynamicCubeMap::handleSchemeNotFound(
unsigned short schemeIndex,
const Ogre::String& schemeName, Ogre::Material* originalMaterial,
unsigned short lodIndex,
const Ogre::Renderable* rend)
415 Ogre::Material::Techniques suptechs = materialTransparent->getTechniques();
416 if ((cubeMapScheme == schemeName) && (originalMaterial == material.get()) && (suptechs.size() > 0))
427 if (state && needUpdate)
429 if (needTextureUpdate)
431 _CreateCubeMapTexture(size);
432 needTextureUpdate =
false;
443 if (state && needUpdate)
void SetEnable(const bool &newState)
virtual void preRenderTargetUpdate(const Ogre::RenderTargetEvent &evt)
virtual void postRenderTargetUpdate(const Ogre::RenderTargetEvent &evt)
virtual bool frameEnded(const Ogre::FrameEvent &evt)
void SetMaterial(SMaterial *targetMaterial, const int &targetTechnique, const int &targetPass, const int &targetTextureUnit)
void SetSize(const unsigned int &newSize)
virtual bool frameStarted(const Ogre::FrameEvent &evt)
void SetAutoUpdate(const bool &newAutoUpdateValue)
virtual Ogre::Technique * handleSchemeNotFound(unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial, unsigned short lodIndex, const Ogre::Renderable *rend)
virtual void windowResized(Ogre::RenderWindow *rw)
Ogre::MaterialPtr getOgreMaterialPointer()
Ogre::SceneNode * O3SceneNode
bool GetRttPixelFormat(Ogre::PixelFormat &format, bool alpha=false, bool floattex=false)
Ogre::RenderSystem * GetOgreRenderSystem()
void RemoveGeneratedMaterial(Ogre::Material *mat)
static SRoot & getSingleton()
static SRoot * getSingletonPtr()
Ogre::SceneManager * GetOgreScenePointer()