What is a Vertex Shader in OpenGL?

The Vertex Shader is responsible for transforming the coordinate space of a model into Clip Space.

It is also responsible for forwarding these coordinates, to the Tessellation, Geometry and Fragment shader.

Vertex Shader Inputs

Inputs to vertex shaders can be Attributes and Uniforms. Both attributes and Uniforms are treated as Read-Only variables by the vertex shader.

Attributes variables, such as positions, normals and uv-coordinates for each vertex, are considered read-only to the vertex shader.

Uniform variables are constant across a graphics primitive and are read-only to ALL shaders. Thus, Uniform variables can be read by the Vertex Shader, Tessellation Shader, Geometry Shader and Fragment Shader but can't be directly modified.

Vertex Shader Outputs

As mentioned above, the main resposibility of a vertex shader is to compute the coordinates of a model into Clip Space. This calculation is placed in a required variable called gl_Position. This variable holds the 4D vertex position in clip coordinates.

Shader output.jpeg

Usually, what is sent to a vertex shader from the CPU is not just the coordinates of a vertex, but also the normals of the vertex, color of the vertex, etc. These variables may be used in the Fragment Shader for lighting computations.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.