[SOLVED] Issue with RenderTexture and copyContentsToMemory

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Maestro81
Gnoblar
Posts: 23
Joined: Wed Jul 02, 2008 7:30 pm
x 3

[SOLVED] Issue with RenderTexture and copyContentsToMemory

Post by Maestro81 »

I render to a texture and would like to access the R, G, B and A values of the resulting image. When writing the contents of my RenderTexture to a file (using writeContentsToFile) it works nicely, including the transparancy.

However, I'm not able to retrieve the pixel info in my application, the resulting ColourValue contains nothing but zero's.

I set it up as follows:

Code: Select all


Ogre::TexturePtr rttTexture = Ogre::TextureManager::getSingleton().createManual( "RttTex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
		Ogre::TEX_TYPE_2D, 480, 640, 0, Ogre::PF_R8G8B8A8, Ogre::TU_RENDERTARGET);

m_overlayTexture = rttTexture->getBuffer()->getRenderTarget();
m_overlayTexture->addViewport(m_overlayCamera);
m_overlayTexture->getViewport(0)->setClearEveryFrame(true);
m_overlayTexture->getViewport(0)->setBackgroundColour(Ogre::ColourValue(0, 0, 0, 0));
m_overlayTexture->getViewport(0)->setOverlaysEnabled(false);

And try to retrieve the pixel value using copyContentsToMemory (in the meantime the RenderTexture is updated)

Code: Select all

static int counter = 0;
m_overlayTexture->writeContentsToFile("c:/temp/screenshot" + StringOperations::ToString(counter++) + ".png"); // this works, including transparancy
		
Ogre::PixelFormat format = Ogre::PF_R8G8B8A8;
int outBytesPerPixel = Ogre::PixelUtil::getNumElemBytes(format);

unsigned char *data = new unsigned char [480*640*outBytesPerPixel];
		
Ogre::Box extents(0, 0, 480, 640);
Ogre::PixelBox pb(extents, format, data);

m_overlayTexture->copyContentsToMemory(pb, Ogre::RenderTarget::FB_AUTO);

for (int u = 0; u < 480; u++)
{
	for ( int v = 0; v < 640; v++)
	{
		Ogre::ColourValue tempColor = pb.getColourAt(u, v, 0); // ALWAYS CONTAINS ZERO'S
	}
}
Anyone knowing what I'm doing wrong here? (Using Ogre 1.8 on Windows 7, D3D9 rendersystem)
Last edited by Maestro81 on Fri Apr 01, 2016 10:41 am, edited 2 times in total.
Maestro81
Gnoblar
Posts: 23
Joined: Wed Jul 02, 2008 7:30 pm
x 3

Re: Issue with RenderTexture and copyContentsToMemory

Post by Maestro81 »

Never mind, got it to work..

One additional question though,

If I do not provide a pixelData buffer in the PixelBox constructor (NULL will be passed, as this is the default value), Ogre crashes... Is this a bug? I just don't see why this would be required..
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 139

Re: Issue with RenderTexture and copyContentsToMemory

Post by c6burns »

Yes you have to allocate in this instance. Just crack open the writeContentsToFile definition and you will see that's how it works also.
Maestro81
Gnoblar
Posts: 23
Joined: Wed Jul 02, 2008 7:30 pm
x 3

Re: Issue with RenderTexture and copyContentsToMemory

Post by Maestro81 »

Tnx