Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How put different colors at front and back sides of quad

Tags:

opengl

Actually, my question is in header. I use "modern opengl" approach with shaders and buffers and I wonder is it exist any way to get different colors on two sides of one quad.

Desired result (back and front sides)

enter image description hereenter image description here

like image 638
frankie Avatar asked Feb 12 '23 10:02

frankie


1 Answers

GLSL provides the built-in gl_FrontFacing variable that determines if the fragment is a front-facing fragment or a back-facing fragment (is true if front facing) e.g. if it is a fragment of the front side of the quad or a fragment of the back side of the quad.

Within the fragment shader you can then check for this variable and set the corresponding colors per face direction:

if(gl_FrontFacing)
{
    outColor = color1;
}
else    // Fragment is back facing fragment
{
    outColor = color2;
}
like image 78
Joey Dewd Avatar answered Mar 04 '23 01:03

Joey Dewd