Skip to content
Part IA Michaelmas Term

Vertex Shader

Purpose

The vertex shader processes each vertex individually:

  • Transform vertex position to clip space
  • Pass attributes to fragment shader
  • Calculate lighting values (if using per-vertex lighting)

Inputs (Vertex Attributes)

AttributeDescription
PositionLocal coordinates (vec3)
NormalSurface normal for lighting (vec3)
Texture coordinatesUV mapping (vec2)
ColoursVertex colours (vec3/vec4)

Outputs

  • gl_Position: Clip-space position (required)
  • Varying/out variables: Interpolated values for fragment shader

Example GLSL

#version 330
in vec3 position;
in vec3 normal;
out vec3 frag_normal;
uniform mat4 mvp_matrix;

void main() {
    frag_normal = normal;
    gl_Position = mvp_matrix * vec4(position, 1.0);
}

Coordinate Transformations

Model-View-Projection Matrix

The MVP matrix transforms vertices through coordinate spaces:

MMVP=MprojectionMviewMmodel\mathbf{M}_{\text{MVP}} = \mathbf{M}_{\text{projection}} \cdot \mathbf{M}_{\text{view}} \cdot \mathbf{M}_{\text{model}}

Coordinate Spaces

SpaceDescription
Model/ObjectOriginal coordinates
WorldAfter model transformation
View/CameraRelative to camera
ClipAfter projection, before perspective divide
NDCNormalised Device Coordinates (−1 to 1)
ScreenFinal pixel coordinates

Vertex Attribute Interpolation

Attributes are interpolated across the triangle using barycentric coordinates:

v=αv0+βv1+γv2\mathbf{v} = \alpha \mathbf{v}_0 + \beta \mathbf{v}_1 + \gamma \mathbf{v}_2

where α+β+γ=1\alpha + \beta + \gamma = 1.

CPU-Side Drawing Sequence

  1. glUseProgram(shaderProgram): Activate shaders
  2. glBindVertexArray(vao): Bind vertex configuration
  3. glUniformMatrix4fv(...): Upload transformation matrices
  4. glDrawElements(...): Issue draw call

CPU-GPU communication showing data flow


Summary

  • Vertex shader processes each vertex once
  • Transforms position to clip space via gl_Position
  • Passes interpolated attributes to fragment shader
  • MVP matrix combines model, view, and projection transforms

Past Paper Questions

2025 Paper 3 Question 4(b)(ii): Give 4 examples of vertex attributes. [2 marks]

2023 Paper 3 Question 4: What is the purpose of a vertex shader? What are its inputs and outputs?

2022 Paper 3 Question 4: Explain the role of the vertex shader in the OpenGL pipeline.