Problems with buffers

OK, here’s what I want to do. I want to draw some stuff on a back buffer (which never gets displayed on screen) which I can read pixels from at a later date, and then I want to draw my main stuff to the normal buffer and display it on screen as usual.

I’m trying to draw my background stuff onto the back right buffer, then all the rest goes on the back left one and swapped over for the double buffer as usual. Here’s some simple code:

gl.glReadBuffer(GL_BACK_RIGHT);
gl.glDrawBuffer(GL_BACK_RIGHT);
    gl.glClearColor(1.0f, 1.0f, 0.0f, 0.0f);				// Set the clear color to yellow
    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);       		// Clear The Screen And The Depth Buffer

gl.glReadBuffer(GL_BACK_LEFT);
gl.glDrawBuffer(GL_BACK_LEFT);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);				// Set the clear color to black
    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);       		// Clear The Screen And The Depth Buffer

So if I now do the following with glReadPixels, should it be able to read the yellow color from the back right buffer?

gl.glReadBuffer(GL_BACK_RIGHT);
gl.glDrawBuffer(GL_BACK_RIGHT);

int viewport[] = new int[4];
byte pixel[] = new byte[3];
gl.glGetIntegerv(GL_VIEWPORT,viewport);

gl.glReadPixels(mouse_x,viewport[3]-mouse_y,1,1,GL_RGB,GL_BYTE,pixel);

System.out.println("color: " + pixel[0] + ", " + pixel[1] + ", " + pixel[2]);

Because it doesn’t! It only gives me the black color from the left buffer instead. :frowning:

Am I missing something simple?!

Many thanks!

Charles

The left and right buffer are only for stereo display, and afaik only professional graphic cards support this.

You could try to use aux buffers instead, or use the EXT_framebuffer_object extension.

Thanks for the reply.

But I thought the back-left buffer was the default that OpenGL always draws to, when using a double-buffered system? :confused:

Charles

Yes, but the problem is that the BACK_RIGHT buffer doesn’t exist when you don’t have a pixelformat with stereo support.

OK, thanks. :slight_smile: So what are my other options for doing this then? I’ve tried looking at the aux buffers by calling glGetIntegerv with GL_AUX_BUFFERS but it tells me I don’t have any.

And I’ve looked at the EXT_framebuffer_object extension mentioned above, but it seems a pretty new technology that isn’t that widely supported yet.

All I really need is a spare color buffer to draw my stuff into.

Are there any other ones GL supports which can do this? :confused:

Many thanks, again.

Charles

You’re right, EXT_framebuffer_object is fairly new and only supported on newer boards. The old way of doing offscreen rendering is pbuffers, but that involves loads of platform specific code and an additional rendering context. The advantage is that it’s supported on nearly every graphic card out there.

Another thing you should consider is if you really need another buffer. Perhaps with some rethinking of the order of operations and some clever use of textures you can avoid using more than the front and back buffer…

OK, thanks very much. It won’t be as easy as I thought then.

There are several ways I can actually get round it, but they’re all rather clumsy or wasteful of processing power. A separate buffer would have worked really well.

I’ll have to have a rethink and see what I can come up with. Pity the aux buffers don’t seem to work…

Thanks for your help. :wink:

Charles