Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the gl_NormalMatrix and gl_ModelViewMatrix in shaders?

A lot of google searches make the assumption that you a basic knowledge of what these matrices represent, while i do not.

I have knowledge of vectors and matrices and their operations.

I want to refer to the most simple shader code that i cannot understand, putting normals and vertices in eye-space:

//vertex shader
varying vec4 color;
varying vec3 N;
varying vec3 v;
void main(void)
{

    v = vec3(gl_ModelViewMatrix * gl_Vertex);
    N = normalize(gl_NormalMatrix * gl_Normal);

    color = gl_Color;

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
} 

My questions:

(i) As the vertex shader loops through each vertex, does the gl_ModelViewMatrix change with every vertex loop? Or does the gl_ModelViewMatrix remain constant throughout each vertex iteration and can transform any vertex into eye space?

(ii) Similar to my first question, but what information does the gl_NormalMatrix hold? Does it hold information on every normal in the scene and thats how it can transform the current vertex's normal that comes into the vertex shader into eye space? Or does the matrix change as each vertex comes into the vertex shader and thats how it can transform the current vertex's normal that comes into the vertex shader into eye space?

like image 731
shawn a Avatar asked Apr 22 '14 05:04

shawn a


1 Answers

Both specified matrices are uniforms. Uniform values are constants, they don't change between vertices (as opposed to varying variables, which change for each fragment, and attribute - for each vertex).

gl_ModelViewMatrix is pre-defined uniform variable set from GL_MODELVIEW matrix (affected by glLoadIdentity, glTranslate, glRotate, glScale or glLoadMatrix).

gl_NormalMatrix is transpose(inverse(gl_ModelViewMatrix)), which is the same matrix but with inverted scale factors. Good explanation of this technique could be found at http://www.arcsynthesis.org/gltut/Illumination/Tut09%20Normal%20Transformation.html

Please note that this pre-defined uniforms are deprecated in newer versions of OpenGL (and matrix manipulation functions are deprecated too).

like image 156
keltar Avatar answered Sep 21 '22 03:09

keltar