To discard fragments based on alpha data from a texture, use the following steps:
- Use the same vertex and fragment shaders from the Applying a 2D texture recipe. However, make the following modifications to the fragment shader.
- Replace the sampler2D uniform variable with the following:
layout(binding=0) uniform sampler2D BaseTex; layout(binding=1) uniform sampler2D AlphaTex;
- In the blinnPhong function, use BaseTex to look up the value of the ambient and diffuse reflectivity.
- Replace the contents of the main function with the following code:
void main() {
vec4 alphaMap = texture( AlphaTex, TexCoord );
if(alphaMap.a < 0.15 )
discard;
else {
if( gl_FrontFacing ) {
FragColor = vec4(
blinnPhong(Position,normalize(Normal)), 1.0 );
} else {
FragColor = vec4( blinnPhong(Position,normalize(-
Normal)), 1.0 );
}
}
}