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)
| Attribute | Description |
|---|---|
| Position | Local coordinates (vec3) |
| Normal | Surface normal for lighting (vec3) |
| Texture coordinates | UV mapping (vec2) |
| Colours | Vertex 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:
Coordinate Spaces
| Space | Description |
|---|---|
| Model/Object | Original coordinates |
| World | After model transformation |
| View/Camera | Relative to camera |
| Clip | After projection, before perspective divide |
| NDC | Normalised Device Coordinates (−1 to 1) |
| Screen | Final pixel coordinates |
Vertex Attribute Interpolation
Attributes are interpolated across the triangle using barycentric coordinates:
where .
CPU-Side Drawing Sequence
glUseProgram(shaderProgram): Activate shadersglBindVertexArray(vao): Bind vertex configurationglUniformMatrix4fv(...): Upload transformation matricesglDrawElements(...): Issue draw call
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.