Hello Guest

Draw something in front of every thing else

  • 1 Replies
  • 142 Views
Draw something in front of every thing else
« on: January 08, 2021, 21:20:40 »
How to draw an object even if it is behind another object?

Re: Draw something in front of every thing else
« Reply #1 on: February 08, 2021, 08:04:27 »
Code: [Select]
// 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
« Last Edit: February 08, 2021, 08:08:05 by Aisaaax »