Graphics / Reference / A 2D vertex shader
A 2D vertex shader
The 2D vertex shader receives each position as a vec2, embeds it as a homogeneous vec4, applies the model matrix, then applies the projection matrix to produce gl_Position.
The vertex shader used here is:
layout(location = 0) in vec2 aPos; uniform mat4 uProj; uniform mat4 uModel; void main() { vec4 world = uModel * vec4(aPos,0.0,1.0); gl_Position = uProj * world; }
Attribute location 0 supplies the 2D position aPos.
vec4(aPos,0.0,1.0) expands position (x,y) to (x,y,0,1). z = 0 embeds the point in the 2D plane, while w = 1 gives the ordinary homogeneous-position form so translation in uModel affects it.
The model matrix acts first:
world = uModel * vec4(aPos,0,1)
The projection matrix then acts on that result:
gl_Position = uProj * world
gl_Position is a homogeneous clip-space position. For the simple setup here, uModel is initially identity and uProj is an orthographic projection spanning the framebuffer dimensions, so source positions expressed in pixel-scale coordinates are transformed into clip space before later pipeline stages perform clipping, perspective division and viewport mapping.
Builds on
Read
- Read the chapter: A 2D vertex shader