View Full Version : Reading from and Writing to renderbuffers
03-01-2009, 03:54 AM
In order to implement object picking, I have set up an FBO with one color and one depth renderbuffer.
Now my questions are:
1) How should I read from and write to the color renderbuffer (although the latter is not required for picking... just wondering)? Do I need to make any API calls besides glReadPixels and glDrawPixels (like setting glReadBuffer and glDrawBuffer, etc.)? Please be as specific as you can.
2) Besides, in case the FBO is not active anymore (the backbuffer is set as the active framebuffer for instance), should I re-bind the FBO every time I need to read from these renderbuffers? Can't I just rebind the renderbuffer itself (via glBindRenderbuffer( GL_RENDERBUFFER, 0 )) and be done with it?
03-01-2009, 07:29 PM
I would suggest not doing picking this way. Shooting a ray through your geometry and doing ray to Sphere, or even ray to triangle intersections is a much better solution for a few reasons. One is that AFAIK picking is deprecated in GL3.0, and another is that you are throwing away a lot of GPU bandwidth doing picking.
wrt to FBOs. Much better to attach a texture for the buffers, and then read from that texture IMO.
03-01-2009, 11:50 PM
Thanks for your help.
Actually, I already have geometry-based picking in place with support for ray-sphere, ray-AABB and ray-OBB intersections. The problem is that our scene is very dense which makes picking small objects very hard if not impossible. All geometry-based approaches approximate the object to some extent. Some are better, some are worse. But at the end of the day, they are all approximations and as such they all introduce errors of some sort. I need pixel-perfect accuracy.
As for choosing textures over renderbuffers, I'm not sure if that's going to improve performance. If I'm not mistaken, glReadPixels and glDrawPixels go through a plethora of unnecessary transformations which makes them quite slow (not to mention the fact that read-backs are slow in nature due to GPU-CPU synchronization issues they cause), so that's a minus for glReadPixels and renderbuffers, but the problem with textures is that glGetTexImage doesn't allow read-backs of sub-regions. All I need is the value of a single pixel. Considering that this texture must be the size of the window, which can be quite large at times, downloading millions of pixels is a hefty price to pay. I think using glReadPixels would result in better performance.
With regarding to picking being deprecated in GL3.0, I suspect that refers to picking using the selection buffer, not FBOs.
Any more ideas?
03-02-2009, 01:19 AM
There is glTexSubImage2D function that will allow to read only part of texture. Also you can create only 1x1 size texture (or renderbuffer) for picking rendering, just adjust projection matrix accordingly (gluPickMatrix).
03-02-2009, 01:50 AM
glTexSubImage2D is used for **submitting** texels to a sub-region of a mip level of a texture. In other words, it's used for uploading texels to the card not reading it back. Am I missing something?
The 1x1 texture trick you mentioned is something I hadn't thought of. Very nice indeed. Thanks man.
03-02-2009, 02:58 AM
Uh, sorry, I don't know what I was thinking when I suggested to use glTexSubImage2D :) Ignore that part.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.