15#include <OgreDepthBuffer.h>
20SRenderToTexture::SRenderToTexture() : SNode(0,
"", SNode::RENDER_TO_TEXTURE_ID)
25SRenderToTexture::SRenderToTexture(
SScene* parent,
const std::string& rttName,
SCamera* povCamera) :
SNode(parent, rttName,
SNode::RENDER_TO_TEXTURE_ID)
37 needTextureUpdate =
false;
38 renderingEnable =
false;
39 cameraOriginalRatio = 1.0f;
40 materialScheme = Ogre::MSN_SHADERGEN;
46 _CreateRenderTexture(size);
49 Ogre::Root::getSingleton().addFrameListener(
this);
66 Ogre::Root::getSingleton().removeFrameListener(
this);
70 _DeleteRenderTexture();
78 double result = log((
double)newSize)/log((
double)2.0);
79 double fractpart, intpart;
80 fractpart = modf(result, &intpart);
83 bool savedState = state;
88 _CreateRenderTexture(newSize);
94 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS,
"Render to texture size must be a power of 2 number!",
"SRenderToTexture::SetSize");
106 if (targetMaterial != 0)
112 Ogre::Pass* tmpPass = tmpMaterial->getTechnique(targetTechnique)->getPass(targetPass);
114 bool savedState = state;
116 material = tmpMaterial;
119 int numTextureUnits = tmpPass->getNumTextureUnitStates();
121 if ((numTextureUnits>0) && (targetTextureUnit<numTextureUnits))
123 Ogre::TextureUnitState* tmpTextureUnitState = tmpPass->getTextureUnitState(targetTextureUnit);
124 if((tmpTextureUnitState != 0) && ((textureUnitState == 0) || (textureUnitState != tmpTextureUnitState)))
126 textureUnitState = tmpTextureUnitState;
131 textureUnitState = 0;
139 if (scheme.empty() || (scheme ==
"Default") || (scheme ==
"DefaultScheme"))
140 materialScheme = Ogre::MSN_SHADERGEN;
142 materialScheme = scheme;
145 rttTarget->getViewport(0)->setMaterialScheme(materialScheme);
148void SRenderToTexture::SetTextureUnit()
150 if (textureUnitState != 0)
152 oldTexture = textureUnitState->_getTexturePtr();
153 textureUnitState->setTextureName(
name +
"_texture");
160void SRenderToTexture::ResetTextureUnit()
162 if (textureUnitState != 0)
167 textureUnitState->setBlank();
169 textureUnitState->setTexture(oldTexture);
171 catch(Ogre::Exception&)
187 if ((newState ==
true) && (state ==
false))
196 else if ((newState ==
false) && (state ==
true))
212 if ((newAutoUpdateValue ==
true) && (autoUpdate ==
false))
215 Ogre::Root::getSingleton().removeFrameListener(
this);
218 renderingEnable =
false;
221 else if ((newAutoUpdateValue ==
false) && (autoUpdate ==
true))
227 Ogre::Root::getSingleton().addFrameListener(
this);
241void SRenderToTexture::EnableRendering()
245 rttTarget->setAutoUpdated(
true);
246 renderingEnable =
true;
250void SRenderToTexture::DisableRendering()
254 rttTarget->setAutoUpdated(
false);
255 renderingEnable =
false;
259void SRenderToTexture::_CreateRenderTexture(
const unsigned int& newSize)
261 _DeleteRenderTexture();
264 Ogre::PixelFormat rttformat;
267 rttTexture = Ogre::TextureManager::getSingleton().createManual(
name +
"_texture", SO3_INTERNAL_DYNAMIC_READABLE_RESOURCE_GROUP, Ogre::TEX_TYPE_2D, newSize, newSize, 0, rttformat, Ogre::TU_RENDERTARGET,
this);
268 rttTarget = rttTexture->getBuffer(0)->getRenderTarget();
269 rttTarget->setAutoUpdated(
false);
270 rttTarget->addListener(
this);
272 rttTarget->setDepthBufferPool(Ogre::DepthBuffer::POOL_NO_DEPTH);
276 cameraOriginalRatio = rttCamera->getAspectRatio();
277 Ogre::Viewport* viewport = rttTarget->addViewport(rttCamera);
278 rttCamera->setAspectRatio(cameraOriginalRatio);
280 viewport->setMaterialScheme(materialScheme);
282 UpdateCompositorViewport(viewport);
285 viewport->setOverlaysEnabled(
false);
287 catch (Ogre::Exception &e)
289 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"An exception has occurred: "+ e.getDescription());
294void SRenderToTexture::_DeleteRenderTexture()
299 rttTarget->removeListener(
this);
300 rttTarget->removeAllViewports();
301 Ogre::Root::getSingleton().detachRenderTarget(rttTarget);
304 Ogre::TextureManager::getSingleton().remove(rttTexture->getHandle());
311 if(state && needUpdate)
313 if (needTextureUpdate)
315 _CreateRenderTexture(size);
316 needTextureUpdate =
false;
326 if (state && needUpdate)
355 Ogre::Camera* targetCamera = evt.source->getCamera();
356 if(targetCamera != 0)
359 cameraOriginalRatio = targetCamera->getAspectRatio();
362 targetCamera->setAspectRatio(1.0f);
371 Ogre::Camera* targetCamera = evt.source->getCamera();
372 if(targetCamera != 0)
375 targetCamera->setAspectRatio(cameraOriginalRatio);
382 SCompositorMap::iterator iCompositorSearched = compList.find(compositorName);
383 if (iCompositorSearched != compList.end())
384 return iCompositorSearched->second;
397 if (newCompositor == 0)
399 newCompositor =
new SCompositor(newCompositorName, newSchemeName);
401 newCompositor->
SetViewport(rttTarget->getViewport(0));
405 return newCompositor;
416 SO3_SAFE_DELETE(existingCompositor);
422 SCompositorMap::iterator iCompositorList = compList.begin();
425 while (iCompositorList != compList.end())
427 SO3_SAFE_DELETE(iCompositorList->second);
436 SCompositorMap::iterator iCompositorSearched = compList.find(
name);
437 if (iCompositorSearched == compList.end())
439 compList.insert(SCompositorMap::value_type(
name, existingCompositor));
444 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM,
"Can not add Compositor named \"" +
name +
"\", an element with the same name already exist!",
"SRenderToTexture::AddCompositor");
455 SCompositorMap::iterator iCompositorSearched = compList.find(compositorName);
456 if (iCompositorSearched != compList.end())
458 compList.erase(iCompositorSearched);
463 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND,
"Can not remove Compositor named \"" + compositorName +
"\", element not found!",
"SRenderToTexture::RemoveCompositor");
467void SRenderToTexture::UpdateCompositorViewport(Ogre::Viewport* vp)
469 SCompositorMap::iterator iCompositorList = compList.begin();
470 while (iCompositorList != compList.end())
472 iCompositorList->second->SetViewport(vp);
Ogre::Camera * GetOgreCameraPointer()
void SetViewport(Ogre::Viewport *vp)
std::string GetName() const
Ogre::MaterialPtr getOgreMaterialPointer()
SCompositor * CreateCompositor(const std::string &newCompositorName, const std::string &newSchemeName)
void SetEnable(const bool &newState)
virtual void windowResized(Ogre::RenderWindow *rw)
void RemoveCompositor(SCompositor *existingCompositor)
virtual void postViewportUpdate(const Ogre::RenderTargetViewportEvent &evt)
void SetAutoUpdate(const bool &newAutoUpdateValue)
virtual void loadResource(Ogre::Resource *resource)
void AddCompositor(SCompositor *existingCompositor)
void SetMaterial(SMaterial *targetMaterial, const int &targetTechnique, const int &targetPass, const int &targetTextureUnit)
void SetSize(const unsigned int &newSize)
virtual bool frameEnded(const Ogre::FrameEvent &evt)
const SCompositorMap & GetCompositorList() const
void DeleteCompositor(SCompositor *existingCompositor)
void SetMaterialScheme(std::string scheme)
virtual void preViewportUpdate(const Ogre::RenderTargetViewportEvent &evt)
SCompositor * GetCompositor(const std::string &compositorName)
virtual bool frameStarted(const Ogre::FrameEvent &evt)
bool GetRttPixelFormat(Ogre::PixelFormat &format, bool alpha=false, bool floattex=false)
void RemoveGeneratedMaterial(Ogre::Material *mat)
static SRoot & getSingleton()
static SRoot * getSingletonPtr()
std::unordered_map< std::string, SCompositor * > SCompositorMap