# OpenGL: Purpose of transform matrix in Vertex shader where transform*vertex

I'm trying to understand OpenGL by using Processing OpenGL ES.

If I have an example Cube to render.

I've read that a vertex shader will be run once for each Vertex. The fragment shader will be run (at least) once for each pixel.

In a vertex shader I have:

#define PROCESSING_COLOR_SHADER

uniform mat4 transform;

attribute vec4 vertex;
attribute vec4 color;

varying vec4 col;

void main(){

gl_Position = vertex;

col = color;
}


In a fragment shader I have:

gl_FragColor = col;


When ran, it creates a simple painted cube of the colour defined in the Processing sketch. However, if I change the vertex shader to...

gl_Position = vertex


(instead of transform*vertex) it draws a wireframe cube in exactly the same place. What exactly does the transform matrix do?

## 2 Answers

The transform matrix allows you to translate, rotate or resize your model. From the model's point of view, its vertices always are at the same position. But if you multiply the vertices with, let us say a translation matrix, they all get moved. Seen from the model's origin, they are still at the same position. But in world space, your whole model has moved. The reason that changing the code did nothing to how the cube was presented to you lies in your transformation matrix not applying any changes to the cube's model data. If you choose a different transformation matrix, things will look differently.

The transform matrix will be a uniform which is easier to update and upload to the gpu (just a single glUniform call) than applying the transform to all vertices on the cpu and uploading the new vertices. Especially with large models.

• Altough it could be used to other things – joojaa Sep 11 '16 at 20:09