How to do it...

To discard fragments based on alpha data from a texture, use the following steps:

  1. Use the same vertex and fragment shaders from the Applying a 2D texture recipe. However, make the following modifications to the fragment shader.
  2. Replace the sampler2D uniform variable with the following:
layout(binding=0) uniform sampler2D BaseTex; 
layout(binding=1) uniform sampler2D AlphaTex;
  1. In the blinnPhong function, use BaseTex to look up the value of the ambient and diffuse reflectivity.
  2. 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)), ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.