PDA

View Full Version : How do I read texel data from texture bound to FBO



CelticDaddio
07-30-2008, 06:27 PM
In my app, I render the scene to a FBO, which has two RGBA FP textures attached to it.
I use two GLSL shaders to write to the textures.
After I render to the textures, I render them on a quad to display the results.

So it goes something like this:

Render()
{
EnableFrameBuffers();
RenderScene();
DisableFrameBuffers();

RenderTexture1();
RenderTexture2();
RenderPlot();
}

RenderTexture1 and RenderTexture2 use a GLSL shader to map the texture to a quad.

The ReturnPlot function is supposed to take some data from each of the aforementioned textures, do some stuff to it, and render a plot. My problem is that I can't seem to read the data back from the texture correctly.

Here is what I am trying....


GLint texture1Size = 4*m_nxPixels*m_nyPixels*sizeof(float);
float* texture1Data = (float*)malloc(texture1Size);

GLint texture2Size = 4*m_nxPixels*m_nyPixels*sizeof(float);
float* texture2Data = (float*)malloc(texture2Size);

EnableFrameBuffers();

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindTexture(texTarget, textureID);
glReadPixels(0,0,m_nyPixels,m_nyPixels,texFormat,G L_FLOAT,texture1Data);

glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
glBindTexture(texTarget, textureID);
glReadPixels(0,0,m_nyPixels,m_nyPixels,texFormat,G L_FLOAT,texture2Data);

DisableFrameBuffers();

int x = 10;
int y = 10;

// get data at x,y
int index = x * m_nxPixels + y;
float red1 = texture1Data[index];
float green1 = texture1Data[index+1];
float blue1 = texture1Data[index+2];
float alpha1 = texture1Data[index+3];

float red2 = texture2Data[index];
float green2 = texture2Data[index+1];
float blue2 = texture2Data[index+2];
float alpha2 = texture2Data[index+3];

The data read back is not what I expect and not what is being rendered in the RenderTexture1 and RenderTexture2 methods. My question is, how do I read back the texel data from a texture which is currently bound to an FBO.

Thanks,CD

zed
07-31-2008, 02:26 PM
the texture attached to a FBO is just a standard texture
thus to see what it contains u just use the normal methods
eg
bind the texture + draw a polygon onscreen
or
glGetTexImage( ...., data )
save data to a file + look at it in photoshop/gimp

CelticDaddio
08-04-2008, 08:53 AM
I rendering it, but when I try to retrieve the data in numerical format is when I have the problem... I am using the glGetTexImage call but sounds like maybe I am not getting the data in the right format... GL_FLOAT vs. GL_RGBA, or type or something... In other words, I think GL is giving me what I ask for, I am just not asking for the right thing...

Thanks for the response,
CD


the texture attached to a FBO is just a standard texture
thus to see what it contains u just use the normal methods
eg
bind the texture + draw a polygon onscreen
or
glGetTexImage( ...., data )
save data to a file + look at it in photoshop/gimp

zed
08-04-2008, 12:21 PM
this doesnt make sense -> GL_FLOAT vs. GL_RGBA
perhaps float vs GLubyte etc, if youre saving a copy of the texture say as a *.tga texture file u most likely want to use GL_RGBA + GL_UNSIGNED_BYTE, beaware some systems have BGRA storage so u might wanna use GL_BGRA if the red + blue channels are swapped

anyways youve made an error above

int index = x * m_nxPixels + y;
float red1 = texture1Data[index];
float green1 = texture1Data[index+1];

this will only fill ~1/4 of the data
ie u need int index = (x * m_nxPixels + y) * 4;

also m_nxPixels is wrong u need m_nyPixels