PDA

View Full Version : Counter problems when reading texture from FBO



SagoO
03-30-2011, 07:16 AM
Hi,


/*Read values back*/
/*Method 1*/
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,textureID);
glGetTexImage(GL_TEXTURE_RECTANGLE_ARB,0,GL_LUMINA NCE,GL_UNSIGNED_BYTE,data);

printf("Texture:\n");
for (i=0; i<2048*2048; i++)
printf("%c",data[i]);

printf("\n\n");

/*Method 2*/
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadPixels(0, 0, 2048, 2048, GL_LUMINANCE, GL_UNSIGNED_BYTE, result);
printf("Data after roundtrip:\n");
for (i=0; i<2048*2048; i++)
printf("%c",result[i]);

I use 2 methods to read my texture. The first method works perfectly and it gives me the exact texture. But I not sure why method 2 give me nothing?My program just end after printing the message "Data after roundtrip". Why?

Here is the code of FBO:


glGenFramebuffersEXT(1, &amp;fboID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, textureID, 0);

Is it the texture fails to attach to FBO?

I check the status of FBO using the following code:


glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(GL_FRAMEBUFFER_COMPLETE_EXT)
{
printf("FBO complete!!");
}


And it shows the message "FBO complete!!". So, it means my FBO should be correct. But why I cant read it?

mobeen
03-30-2011, 11:06 AM
R u sure that the fbo is bound before the glReadBuffer call?

SagoO
03-30-2011, 07:43 PM
I check the status already and it shows FBO complete.It means no problems for the FBO right?

Alfonse Reinheart
03-30-2011, 08:12 PM
You still have to bind it to the context.

SagoO
03-30-2011, 08:33 PM
You still have to bind it to the context.

Do you means bind the FBO to context?Like this:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboID);


I try ade but it still not works. It does not print out anything.

Dan Bartlett
03-30-2011, 09:24 PM
I check the status of FBO using the following code:


glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(GL_FRAMEBUFFER_COMPLETE_EXT)
{
printf("FBO complete!!");
}


And it shows the message "FBO complete!!". So, it means my FBO should be correct. But why I cant read it?

This isn't correct, it should be something like:


GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT)
{
printf("FBO complete!!");
}


ps. http://www.songho.ca/opengl/gl_fbo.html has a nice tutorial for FBOs

SagoO
03-31-2011, 05:08 AM
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT)
{
printf("FBO complete!!");
}


I think it should be the same. But I try this code also, it shows FBO complete. What is the problem actually?

mhagain
03-31-2011, 06:14 AM
Checking "if(GL_FRAMEBUFFER_COMPLETE_EXT)" will always result in the condition being evaluated to true because what is actually being evaluated is "if (0x8CD5 != 0)".

SagoO
03-31-2011, 06:35 AM
Checking "if(GL_FRAMEBUFFER_COMPLETE_EXT)" will always result in the condition being evaluated to true because what is actually being evaluated is "if (0x8CD5 != 0)".

Okay..But I had tried to following code and it return the same output.



GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT)
{
printf("FBO complete!!");

After I put this code, it gives me FBO complete message also.
}

Dan Bartlett
03-31-2011, 06:44 AM
If your application is terminating unexpectedly, then maybe "result" isn't large enough to store all the data? This would mean that when OpenGL tries to write to "result", it would end up overwriting other application data.

Have you also tried calling glGetError to see if any OpenGL errors have occurred?

SagoO
03-31-2011, 06:50 AM
If your application is terminating unexpectedly, then maybe "result" isn't large enough to store all the data? This would mean that when OpenGL tries to write to "result", it would end up overwriting other application data.

Have you also tried calling glGetError to see if any OpenGL errors have occurred?

Hem...The size of the result and the data that I sent into is the same. So, should be enough. But anyway, I will give this a try later.

So, my code does not have any error right? I means programming error.

Alfonse Reinheart
03-31-2011, 12:14 PM
So, my code does not have any error right?

The 20 or so lines you posted seem to be fine. But that doesn't mean anything, since that's likely less than 5% of your program.

SagoO
03-31-2011, 08:23 PM
I check my code again using glGetError().



GLenum e;
if ((e=glGetError())!= GL_NO_ERROR)
printf("GL Error: (%s)\n", gluErrorString(e));


I found this error.

GL Error: invalid operation (Readpixel)



glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadPixels(0, 0, size, size, GL_LUMINANCE, GL_UNSIGNED_BYTE, result);


The glReadPixels there prompt error. Invalid operation. Does glReadBuffer bind the fbo to the context? Even though it does not, i already add glBindFramebufferEXT to bind the fbo. However, still have the same problem. I not sure how to troubleshoot the problem now. What I know now is the glReadPixels generates an error.

Alfonse Reinheart
03-31-2011, 10:49 PM
Here (http://www.opengl.org/sdk/docs/man3/) are a list of causes that would lead to an invalid operation for glReadPixels.

SagoO
03-31-2011, 11:01 PM
Here (http://www.opengl.org/sdk/docs/man3/) are a list of causes that would lead to an invalid operation for glReadPixels.

I check already. I use the correct format. But why there are 2 different version?

glReadPixels (http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml)

The above link shows that GL_LUMINANCE is the supported format. But according to the link you give me, seems like GL_LUMINANCE does not support in glReadPixels.

Dan Bartlett
04-01-2011, 01:21 AM
Are you using the core profile? If so GL_LUMINANCE is no longer supported, compare:
http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml
and
http://www.opengl.org/sdk/docs/man3/xhtml/glReadPixels.xml

SagoO
04-01-2011, 06:53 AM
Are you using the core profile? If so GL_LUMINANCE is no longer supported, compare:
http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml
and
http://www.opengl.org/sdk/docs/man3/xhtml/glReadPixels.xml

Then which format should I use since I jz want to use one component only in every pixels instead of using RGB format that have 3 components. I just want every pixel store one byte of component instead of 3 bytes.

BionicBytes
04-01-2011, 09:26 AM
for GL 3.x core profiles try using GL_RED

SagoO
04-01-2011, 09:35 AM
for GL 3.x core profiles try using GL_RED

Actually I not sure what is core profiles, my openGL version is just 1.4 only.

I have another question. Let's say if i use one red component only in my texture, can I read the red component out only from the frame buffer?

Alfonse Reinheart
04-01-2011, 12:51 PM
Actually I not sure what is core profiles, my openGL version is just 1.4 only.

Then none of what he said matters to you. It only matters if you're using GL 3.1 and above, and then only if you don't want to use the compatibility profile.