
Retrieving Color data from textures...
Posted Monday, 13 December, 2010 - 20:52 by CartBlanche inHi all,
I'm working on the XNATouch project ( porting Microsoft's XNA api to mobile devices ). I'm fairly new to OpenGL ES so was hoping someone here with more experience could help.
Basically I need to implement the following fucntion
public void GetData<T>(T[] data) { }
This would be used in the following manner...
m_Texture.GetData<Color>(texColors);
I understand that I need to make a call to GL.GetTexImage, but I'm unsure about how to hook everything up to extract the Color information.
Can anyone point me in the right direction or post a link.
Thanks.


Comments
Re: Retrieving Color data from textures...
You need GL.GetTexImage().
Re: Retrieving Color data from textures...
Hi Fiddler,
Thanks for your help. I mentioned in my original post that I knew that I needed GL.GetTexImage() but not how to use it to extract the color info. Do you have any links with example code?
Thanks,
Dominique.
Re: Retrieving Color data from textures...
Sorry, I was distracted.
Assuming a 2d texture, BGRA format and 32bpp data, something like this should work:
For 1d and 3d textures just modify the conditional and the TextureTarget. For 8bpp, 16bpp or floating-point textures, you will need to modify the PixelType and the "4" in the conditional. I don't know what texture formats are available in XNA but they should correspond 1-1 to the ones available in OpenGL (the hardware is identical after all).
Re: Retrieving Color data from textures...
Wow I didn't realise it would be THAT easy.
But it seems GL.GetTexImage isn't available for OpenGL ES 1.1 or OpenGL 2.0, at least it isn't with MonoTouch.
Should it be?
Thanks.
Re: Retrieving Color data from textures...
I'm not all that familiar with OpenGL ES, but apparently GetTexImage does not exist there. According to this thread,
I found a solution that works. Bind the texture as if it were render to texture, then read the framebuffer pixels.
In other words, create a FBO, attach your texture and use GL.ReadPixels() to get the data. It's a round-about approach but still quite simple to implement:
GL.GenFramebuffers,GL.BindFramebuffer,GL.FramebufferTexture2D,GL.ReadBuffer, followed byGL.ReadPixelsandGL.DeleteFramebuffer. Check the code in the FBO documentation