PDA

View Full Version : Problems with buffers



10-31-2005, 04:19 AM
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. :(

Am I missing something simple?!

Many thanks!

Charles

Overmind
10-31-2005, 04:45 AM
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.

10-31-2005, 06:10 AM
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

Overmind
10-31-2005, 10:00 AM
Yes, but the problem is that the BACK_RIGHT buffer doesn't exist when you don't have a pixelformat with stereo support.

10-31-2005, 01:44 PM
OK, thanks. :) 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

Overmind
10-31-2005, 02:10 PM
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...

10-31-2005, 03:35 PM
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. ;)

Charles