I have couple questions that would GREATLY assist me in understanding opengl. Some questions pertain to basic glsl functions, but i think the majority are opengl related, so i posted this in opengl thread instead of graphics.
(i) Each gl_ModelViewMatrix matrix needs to correspond to a gl_PushMatrix, because a gl_PushMatrix "creates" a new modelviewmatrix on the stack, right?(assuming matrixmode=gl_modelview)
There is one modelview matrix stack, and commands such as glPushMatrix, glTranslate, glRotate all affect the topmost matrix on the modelview matrix stack. Within the shader, gl_ModelViewMatrix reflects the value of the topmost matrix. Same goes for the other matrix stacks such as the projection matrix, the texture matrix and the color matrix.
If so, does the glsl shader program receive the corresponding vertex's modelviewmatrix when vertices are being passed to the shader?
To Further explain,
If i say
In the resulting modelviewmatrix from the above code, the components in the matrix would show a +10 x-axis translation. But that translation is only true for the second cube, while the first cube only has a +5 x-axis translation. So, how am i supposed to, for example, transform from object-eye coordinate space in the following code if i recieve a vertex from the first cube, while the corresponding modelviewmatrix has a 10 translation, when i really need the 5 translation:
v = vec3(gl_ModelViewMatrix * gl_Vertex);
When you draw the first cube, the matrix on the top of the modelvire matrix stack has the value of containing a translation by 5 units along the Y-axis in addition to what's before the glPushMatrix. When you draw the second cube, the topmost matrix on the modelview matrix stack now has an additional translation of 5 along the Y-axis.
Again, the value of gl_ModelViewMatrix reflects the value of the top of the modelview matrix stack. The matrix thus contains 5 and 10 units of translation when drawing the first and second cube, respectively, because that is what is on top of the matrix stack at the corresponding times.
(ii) In glsl, are "gl_LightSource[x]", where 'x' is 0 to max_lights, coordinates already in eye view when being passed to glsl?
I appreciate any answers, thanks!
The value of gl_LightSource is the vector you pass to glLight with the parameter GL_POSITION multiplied by the modelview matrix at the time the call was made. Thus:
glTranslate(...); // matrix A
glLight(GL_POSITION, ...); // position v
// draw 1
glTranslate(...); // matrix B
// draw 2
In draw 1: the value of gl_LightSource is A*v, and the value gl_ModelViewMatrix is A.
In draw 2: the value of gl_LightSource is A*v, and the value gl_ModelViewMatrix is A*B.