Search code examples
openglglslvertex-shader

Changing Vertex Shader does not effects


I have expected in the bellow code when multiplying gl_Position by 0.5 to change the position of a texture. But It does not happen. Vertex shader code:

       "uniform mat4 u_MVP;            \n" \
       "attribute vec4 a_Position;     \n" \
       "void main()                    \n" \
       "{                              \n" \
       "   gl_Position = u_MVP   * 0.5  * a_Position;   \n" \
       "}                              \n"; 

what is my wrong?

How can I change vertex shaders?


Solution

  • This scales the x, y, z and w all by 0.5, which means the x and y position on the screen stays the same, because x is divided by w and y is divided by w. 1/1 is 1, for example, and if you halve both sides, then 0.5/0.5 is also 1 so there's no difference.

    Instead, you could use for example vec4(0.5, 0.5, 0.5, 1.0) so that w is not affected.

    I'm also not sure whether doing the multiplication in this order makes sense. I would write it as:

    gl_Position = (u_MVP * a_Position) * vec4(0.5,0.5,0.5,1.0);
    

    This would halve the screen coordinates after the matrix, shrinking the whole picture towards the middle of the screen.

    Or you could halve the coordinates before the matrix, which generally does not do the same thing, depending on which matrix you are using. In most cases this would shrink each object towards its own origin (0,0,0 point).

    gl_Position = u_MVP * (a_Position * vec4(0.5,0.5,0.5,1.0));