In p5.js vertex shaders, why sometimes there are matrices used and sometimes not to compute gl_Position?

15 Views Asked by At

I am trying to learn shaders and started looking at several examples.
As I am using p5.js, in some vertex shader examples I can see this kind of computation:

attribute vec3 aPosition;

void main() {
    vec4 positionVec4 = vec4(aPosition, 1.0);

    // scale the rect by two, and move it to the center of the screen
    // if we don't do this, it will appear with its bottom left corner in the center 
    // of the sketch
    positionVec4.xy = positionVec4.xy * 2.0 - 1.0;

    // send the vertex information on to the fragment shader
    gl_Position = positionVec4;
}

And in some others there is this:

attribute vec3 aPosition;

uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;

void main() {
  // Set the vertex position without any change besides the view transformations
  // Note: it is important to apply these matrices to get your shape to render in 
  // the correct location
  gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0);
}

I removed some attributes declarations that were not used (at least seemed to not be used) in the main() code. I am not looking for an in-depth explanation about shaders, only to roughly understand the reason of these 2 different approaches. Is this because of some 2D vs. 3D scene distinction?

0

There are 0 best solutions below