empedia's picture

Example of using ReadPixels<>

Hi,

I'm trying to use the generic overloads on GL.ReadPixels to read information from a texture attached to a frame buffer object

If I use this

GL.ReadPixels(0, 0, PIXSIZE, PIXSIZE, PixelFormat.Rgba, PixelType.UnsignedByte, ImgArray);

I get a 1D array of values but what I want to get is an array of struct containing separate RGBA channels for each pixel in the texture. I assume I can use ReadPixels<> for this.

I defined a struct

public struct RGBA
{
public byte R;
public byte G;
public byte B;
public byte A;
}

Then declared an array of this type

RGBA[,] pixelsxy = new RGBAByte[PIXSIZE, PIXSIZE];

And called the corresponding overload

GL.ReadPixels(0, 0, PIXSIZE, PIXSIZE, PixelFormat.Rgba, PixelType.UnsignedByte, pixelsxy);

All I get back is an empty array

What am I doing wrong - I could really do with an example of how this is supposed to work.

Thanks a lot for you help.

Comments

Comment viewing options

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

Isn't ReadPixels for reading values from the frame buffer..?

martinsm's picture

objarni: if framebuffe object has textures attached as color attachments, it will read pixels from that texture.

objarni's picture

Ah cool.

the Fiddler's picture

The code you posted should work, in theory. Try using a 1d array (RGBA[]).

If the latter works, this could indicate a bug in OpenTK: please post your framework version and architecture! (e.g. Mono 2.2 running on Windows x64).