4
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.