[Patch] Compositor: texture_ref as rendertarget

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.

[Patch] Compositor: texture_ref as rendertarget

Postby moagames » Mon Mar 29, 2010 10:02 pm

Hi all,

following problem:

1. first compositor renders to a texture with the scope set to chain_scope
2. last compositor in the chain uses this texture for his calculation
3. one third compositor is optional. If activated, it comes in between the first two compositors and modifies the texture from the first one

The problem here was, that the texture referenced using texture_ref could only be used as source texture, but not as a render target.
So I modified Ogre in this way:

Code: Select all
diff --git a/OgreMain/src/OgreCompositorInstance.cpp b/OgreMain/src/OgreCompositorInstance.cpp
--- a/OgreMain/src/OgreCompositorInstance.cpp
+++ b/OgreMain/src/OgreCompositorInstance.cpp
@@ -962,8 +962,72 @@
    LocalMRTMap::iterator mi = mLocalMRTs.find(name);
    if (mi != mLocalMRTs.end())
       return mi->second;
-   else
-      OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Non-existent local texture name",
+   
+   //Find the instance and check if it is before us
+   CompositionTechnique::TextureDefinition* texDef = mTechnique->getTextureDefinition(name);
+   if (!texDef->refCompName.empty())
+   {
+      //This is a reference - find the compositor and referenced texture definition
+      const CompositorPtr& refComp = CompositorManager::getSingleton().getByName(texDef->refCompName);
+      if (refComp.isNull())
+      {
+         OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Referencing non-existent compositor",
+            "CompositorInstance::getTargetForTex");
+      }
+      CompositionTechnique::TextureDefinition* refTexDef = refComp->getSupportedTechnique()->getTextureDefinition(texDef->refTexName);
+      if (refTexDef == 0)
+      {
+         OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Referencing non-existent compositor texture",
+            "CompositorInstance::getTargetForTex");
+      }
+
+      switch (refTexDef->scope)
+      {
+         case CompositionTechnique::TS_CHAIN:
+         {
+            //Find the instance and check if it is before us
+            CompositorInstance* refCompInst = 0;
+            CompositorChain::InstanceIterator it = mChain->getCompositors();
+            bool beforeMe = true;
+            while (it.hasMoreElements())
+            {
+               CompositorInstance* nextCompInst = it.getNext();
+               if (nextCompInst->getCompositor()->getName() == texDef->refCompName)
+               {
+                  refCompInst = nextCompInst;
+                  break;
+               }
+               if (nextCompInst == this)
+               {
+                  //We encountered ourselves while searching for the compositor -
+                  //we are earlier in the chain.
+                  beforeMe = false;
+               }
+            }
+            
+            if (refCompInst == 0 || !refCompInst->getEnabled())
+            {
+               OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Referencing inactive compositor texture",
+                  "CompositorInstance::getTargetForTex");
+            }
+            if (!beforeMe)
+            {
+               OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Referencing compositor that is later in the chain",
+                  "CompositorInstance::getTargetForTex");
+            }
+            return refCompInst->getRenderTarget(texDef->refTexName);
+         }
+         case CompositionTechnique::TS_GLOBAL:
+            //Chain and global case - the referenced compositor will know how to handle
+            return refComp->getRenderTarget(texDef->refTexName);
+         case CompositionTechnique::TS_LOCAL:
+         default:
+            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Referencing local compositor texture",
+               "CompositorInstance::getTargetForTex");
+      }
+   }
+
+   OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Non-existent local texture name",
          "CompositorInstance::getTargetForTex");
 
 }



And now it works :)
Hope it's helpful for anyone
moagames
Halfling
 
Posts: 70
Kudos: 1
Joined: 10 Apr 2008

Re: [Patch] Compositor: texture_ref as rendertarget

Postby sinbad » Tue Mar 30, 2010 3:49 pm

Interesting - yes the texture_ref feature was envisaged to be for pulling in the results of previous compositors, rather than rendering to them again. But I don't see any problem immediately with allowing this.

Please can you officially submit this as a patch as described here: http://www.ogre3d.org/developers/submit-patch . The patch won't get lost if it's on the patch list, and also we'll need a contributor license from you to accept this into the core.

Thanks!
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
 
Posts: 19261
Kudos: 69
Joined: 06 Oct 2002
Location: Guernsey, Channel Islands

Re: [Patch] Compositor: texture_ref as rendertarget

Postby moagames » Tue Mar 30, 2010 7:38 pm

done :)
moagames
Halfling
 
Posts: 70
Kudos: 1
Joined: 10 Apr 2008

Re: [Patch] Compositor: texture_ref as rendertarget

Postby Noman » Sat May 15, 2010 9:10 pm

Applied!

Thanks!
User avatar
Noman
OGRE Retired Team Member
OGRE Retired Team Member
 
Posts: 714
Kudos: 2
Joined: 31 Jan 2005
Location: Israel


Return to Developer talk

Who is online

Users browsing this forum: No registered users and 1 guest