I am working on a 3d project using openGL. I am looking for a way to optimize my renderings. Is there a way to tell if an object is behind another object and thus not visible to not waste time rendering it ? I am already working on a frustrum culling implementation to not display what is not visible because out of the viewing frustrum, but I didn't find a way to know if an object in located behind another object. Can any of you help me about this, please ?
If you aren't doing any depth testing, then you can use the OpenGL depth test functionality to prevent further away texels from getting filled in (relative to the view port), this way only the texels that are closest to the view frustum are rendered.
To do this you execute:
And when you clear your screen at the start of every frame, also perform this line (after the lines posted above)
Further, if you render this scene to an custom Frame Buffer Object (FBO) (rather than your default FBO), and employ a deferred shading technique, you could substantially improve performance by only applying fragment shader calculations to the physical 2D image of your scene stored in the FBO.
That technique can be used like this:
- render the scene to an FBO
- Output fragment world positions to a texture, only closest fragments to the camera get used
- Output scene textures merged into a screen-space texture
- render a quad in front of the camera
- quad's fragment shader sample's from the screen-space texture given the positions in the position texture
That technique is quite useful for when there is lighting and other effects going on, as the lighting calculations are bounded to areas on influence and only occur on the small regions of pixels that actually make it to the screen.