<<< The fragment shader code     Index     The fragment shader code, cont. >>>

12. Texture sampling in GLSL


  • Vertex shader

    
    // ...
    // UV coordinates:
    in vec2 s_vTexCoord;
    out vec2 texCoord;
    // ...
    void main () {
        // ...
        // Pass UV coordinates to
        // the fragment shader:
        texCoord = s_vTexCoord;
        // ...
    
    
  • Fragment shader

    
    // sampler2D provides access
    // to the texture image
    in vec2 texCoord;                
    uniform sampler2D texture;
    out vec4 fColor;
    void main () {
        // ...
        // Diffuse light is based on
        // normal N, light vector L,
        // and the texel color returned
        // by texture2D():
        float diffuse_intensity =
            max( dot( N, L ), 0.0 );
        vec4 diffuse_final =
            diffuse_intensity
            *
            texture2D( texture, texCoord )
            ;
        // ...
    
    

<<< The fragment shader code     Index     The fragment shader code, cont. >>>