Draw something in front of every thing else

Started by Mihai_Ionut_Floares, January 08, 2021, 21:20:40

Previous topic - Next topic

Mihai_Ionut_Floares

How to draw an object even if it is behind another object?

Aisaaax

// draw some objects

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);

// Draw the topmost object

glDisable(GL_BLEND)


This is the simplest example.
Basically when you render an object, it renders into a separate buffer, and then gets "Mixed" with what is currently in the frame buffer (i.e. the results of your previous renders). glBlendFunc(GL_ONE, GL_ZERO); says that when mixing, the current render call will be at strength 1-out-of-1 and the current buffer contents will be at 0-out-of-1 strengths.

Don't forget to restore the blendFunc to the default values before your next rendering call, because otherwise the same rule will affect everything in your scene.
Also you can either enable blending only for one object, draw it and disable it, like I did. Or you can enable some rules for all the objects, which could either make your code more or less efficient. The thing is, in some scenes blending could allow you to omit drawing a lot of fragments, especially when you aren't using depth testing.

You can get a lot with blending, including drawing objects "Below" everything as well as above, and of course draw transparency.

https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBlendFunc.xhtml
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBlendFuncSeparate.xhtml