I've read much about vectors, buffer objects etc., and reviewed much of example application implementing old/new opengl operations. My app, including object loading, transformations and vertex/fragment shaders works, in general, I understand how to pass various type of variables to shaders, and how to draw arrays and elements arrays, everything goes fine so far.
But there's one thing I don't understand - final gl_Position matrix used matrix I generated, passed to vertex shader, as some result of some mathematical operations ( actually I use SDL, glew and glm's functions like lookAt, translate, rotate etc. ).
what I do is shader
#version ...
uniform mvp..
main ..
{
gl_Position = mvp * ...;
}
cpp
load_object_vertices_indexes_etc;
install_shaders;
bind_buffers;
buffer_data;
enable_vertex_array_elements_etc;
mat4 aaa_proj = perspective(persp_params);
mat4 bbb_view = lookAt(look_params);
mat4 ccc_model = translate(translate_params);
mat4 ddd_anim = rotate(rotate_params);
/* then I do matrix multiply */
mat4 some_final = ..proj * ...view * ..model * anim;
glUniformMatrix4fv(location_in_shader, 1, GL_FALSE, ..val_pointer_of_some_final)
glClear(appropriate_bitfield);
glDraw...(spec);
The point is, that, actually, final matrix I can, of course, generate by any combination. Shader calls gl_Position with valid result matrix, no matter how it was generated. Now, where exactly do I switch from projection to view to model to anim? Of course matrix names doesn't matter, is there a switching somewhere inside perspective, lookAt, translate and rotate? I mean - if the stack contains actually four stages, each one of these stages can be translated, rotated, scaled, etc. As I understand it by now, actions I take would not matter, if there would be only one stage of matrix stack.
Theoretically, inside 3d scene, I should be able to get similar effect if I ( rotate view 45 degrees left, and then rotate model 45 degrees right ), or if I ( rotate view 45 right and then rotate model 45 degress left).
When I stand physically in front of teapot, I can turn it right 45 and walk around the table (translate and rotate myself) to left 45 to see it from the front - or turn it left 45 and walk 45 to right to see it from the front. It will differ for the rest of the world ( I will see left or right room wall behind teapot ), but obviously I will end seeing the front of the pot.
So, to conclude, if I would like to implement two routines - A( obj_turn_left, me_turn_right ) and B (obj_turn_right, me_turn_left), both rotating model first, and myself (eye, camera) later, how do I specify to which matrix mode particular rotation or transformation applies? Should I use glMatrixMode(APPROPRIATE_CONST) to force going a particular stage when I want? glMatrixMode isn't used in my code so far.
There is no switching at all happening. You multiply the whole transformation chain into one single compound matrix.
For the sake of making things easier when it comes to advanced stuff you normally separate the transformation into modelview and projection, so that you can perform e.g. lighting calculations in view space. But this is not a switch it's just an intermediary step.