Invalid Operation when using glReadPixels

Hello,

I’m using a FBO for render-to-texture. I want to print out the contents of what I just put into the texture. However, my program keeps throwing an error on the glReadPixels call.

I’ve checked to make sure that the framebuffer is complete (it is). The textures in the framebuffer (fbID[2] in the code below) have a target of GL_TEXTURE_RECTANGLE_ARB, internal format of GL_RGBA16F_ARB, and type of GL_FLOAT. Also, when I first initialize the textures, the format is GL_RED.

Here’s a snippet of my code:

// Render some points to a texture using VBO
// fbID[0] is the FBO that holds the position of the points
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbID[0]);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, vertBufID[0]);

// transfer the data from the FBO to the buffer object
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadPixels(0,0,pPosTexW, pPosTexH, GL_RGBA, GL_FLOAT, BUFFER_OFFSET(0));

// fbID[2] is the grid onto which the points are mapped
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbID[2]);
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);

// change the binding point of the buffer object to the
// vertex array binding point
glBindBufferARB(GL_ARRAY_BUFFER, vertBufID[0]);

// draw the vertices
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(4, GL_FLOAT, 0, NULL);
glPointSize(1);
glDrawArrays(GL_POINTS, 0, numParticles);

glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER, 0);


// read back results and print them out
float *outputData1;
outputData1 = new float[numRows*numCols];
glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
//!!!!!!======== THE OFFENDING COMMANG =======!!!!!!!
glReadPixels(0, 0, numCols, numRows,GL_RED,GL_FLOAT,outputData1);

for (i=0; i<numRows*numCols; i++)
{
	cout.precision(3);
	cout << outputData1[i] << " ";
}
cout << endl;
delete [] outputData1;

The thing is, I know that the part where I write the data into GL_COLOR_ATTACHMENT1_EXT is correct because if I draw it to the screen, the correct areas are colored. I just cant seem to print out the data so I can find out what the actual values are.

The GL_COLOR_ATTACHMENT1_EXT would be the second color buffer of your fbID[2]. If that’s not a multiple render target app, your framebuffers should not have a GL_COLOR_ATTACHMENT1_EXT. Try GL_COLOR_ATTACHMENT0_EXT there.

Well, I’m not using multiple render targets for that part of the program, but I do use it elsewhere. In any case, I’ve got 3 textures attached to that FBO, and I want to draw the output in the second one.

It seems that you should add glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0) before the second glReadPixels call.

Holy cow, that worked! I was under the impression that when I changed the binding of my buffer target (vertBufID[0]) from GL_PIXEL_PACK_BUFFER_ARB to GL_ARRAY_BUFFER, that the binding to GL_PIXEL_PACK_BUFFER_ARB would automatically be set back to 0. I guess I was wrong.

Thanks!