CartBlanche's picture

Retrieving Color data from textures...

Hi 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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

You need GL.GetTexImage().

CartBlanche's picture

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.

the Fiddler's picture

Sorry, I was distracted.

Assuming a 2d texture, BGRA format and 32bpp data, something like this should work:

public void GetData<T>(T[] data)
{
    // 4 bytes per pixel
    if (data.Length < Width * Height * 4)
        throw new ArgumentException("data");
 
    GL.GetTexImage(TextureTarget.Texture2d, 0, PixelFormat.Bgra,
        PixelType.UnsignedByte, data);
}

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).

CartBlanche's picture

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.

the Fiddler's picture

I'm not all that familiar with OpenGL ES, but apparently GetTexImage does not exist there. According to this thread,

Quote:

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 by GL.ReadPixels and GL.DeleteFramebuffer. Check the code in the FBO documentation