Understanding gl-Position: My First Real Vertex Shader
This is part 2 of learning about GlSL:
This is a very common vertex shader, especially in libraries like Three.js.
const vshader = `
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`;
Let's break it down:
vec4( position, 1.0 )
position is usually a vec3 attribute containing the vertex coordinates:
position = vec3(x, y, z)
By adding 1.0, it becomes:
vec4(x, y, z, 1.0)
The 1.0 is the w component, which allows the position to be transformed by matrices.
modelViewMatrix
modelViewMatrix * vec4(position, 1.0)
This combines two transformations:
- Model Matrix → moves, rotates, and scales the object.
- View Matrix → represents the camera's position and orientation.
After this multiplication, the vertex is transformed from its local object coordinates into camera space.
projectionMatrix
projectionMatrix * (...)
The projection matrix converts the 3D coordinates into clip space so they can be projected onto a 2D screen.
This is what creates perspective, making distant objects appear smaller than nearby ones.
gl_Position
The final result is assigned to:
gl_Position = ...
This tells the GPU where the vertex should appear on the screen.
You can think of the whole line as:
Object Space
↓
Model Matrix
↓
World Space
↓
View Matrix (Camera)
↓
Camera Space
↓
Projection Matrix
↓
Clip Space
↓
Screen
Or more simply:
gl_Position =
projectionMatrix *
modelViewMatrix *
vec4(position, 1.0);
means:
"Take this vertex, move and rotate it with the object, view it through the camera, apply perspective, and then draw it on the screen."
This single line is essentially the minimum code needed to display a 3D mesh correctly in Three.js. Once you understand this line, you're already touching the core of the graphics pipeline.
Leave Understanding gl-Position: My First Real Vertex Shader to:
Read more #learning posts
Best Posts From hey2d
We have not curated any of hey2d's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From hey2d
- Fragment Shaders Made Simple: What gl-FragColor Does
- Understanding gl-Position: My First Real Vertex Shader
- Creating Procedural Galaxies and Nebulas in GLSL
- Creating a Procedural Night Sky and Stars in GLSL
- Creating Procedural Clouds in GLSL
- Adding Reflections and Highlights to Procedural Water in GLSL
- Creating Procedural Water in GLSL
- Creating Procedural Smoke in GLSL
- Creating Procedural Fire in GLSL
- Creating Procedural Wood Grain in GLSL
- Creating a Procedural Marble Texture in GLSL
- Domain Warping in GLSL: Distorting Noise to Create Organic Worlds
- Fractal Brownian Motion in GLSL: Building Rich Procedural Noise
- Understanding Gradient Noise in GLSL
- Creating Smooth Value Noise in GLSL
- Creating Random Values in GLSL: The Building Blocks of Procedural Graphics
- Combining Translation, Rotation, and Scaling in GLSL
- Scaling Shapes in GLSL: Making Procedural Graphics Grow and Shrink
- Rotating Shapes in GLSL: Understanding Coordinate Rotation
- Moving Shapes in GLSL: Understanding Coordinate Translation