WebGL glsl gl_FrontFacing

428 阅读1分钟
  • gl_FrontFacing变量能告诉我们当前片段是某个正面的一部分还是背面的一部分
#version 330 core
out vec4 color;
in vec2 TexCoords;

uniform sampler2D frontTexture;
uniform sampler2D backTexture;

void main()
{
    if(gl_FrontFacing)
        color = texture(frontTexture, TexCoords);
    else
        color = texture(backTexture, TexCoords);
}
  • 一般计算法线的时候用
if (gl_FrontFacing == false) //false
{
    t *= -1.0;
    b *= -1.0;
    ng *= -1.0;
}