Texture Shading

We've seen shading with a constant color for the entire model, and we've seen shading with a different color for each vertex. Texture mapping enables a different color for each fragment.

This entails the addition of a texture coordinate attribute to the vertex shader, plus a texture coordinate varying to communicate this attribute from the vertex shader to the fragment shader.

〈Vertex Shader〉 ≡
  precision mediump float

  uniform mat4 projectionMatrix
  uniform mat4 viewMatrix
  uniform mat4 modelMatrix

  attribute vec4 vertexPosition
  attribute vec2 vertexTexCoord

  varying vec2 fragmentTexCoord
  function main()
    gl_PositionprojectionMatrix · viewMatrix · modelMatrix · vertexPosition

The image reference is then made per-fragment using a sampler uniform.

〈Fragment Shader〉 ≡
  precision mediump float

  varying vec2 fragmentTexCoord
  uniform sampler2D modelTexture

  function main()
    modelColortexture2D(modelTexture, fragmentTexCoord)