0

I am just starting the opengl study, I am going to post a segment of code, and explains my understanding, could you follow my explanation and point out any problems?

glMatrixMode(GL_MODELVIEW); 
glLoadIdentity();   //start with an identity matrix

glPushMatrix();  
//push the current identity matrix onto the stack, and start with a new identity matrix as 
  the transformation matrix

    glPushMatrix();  
    //copy the current matrix which is the identity as the new transformation matrix and then push the current transformation matrix onto stack

        glScalef(10, 10, 1.0);
        **Question 1**
        //I feels like the order which the image is built is kinda reversed
        //It's like drawSquare happens first, then color, then scale
        //can anyone clarify?
        //Second, the drawsquare defines the 4 vertices around the origin(+/-0.5,+/-0.5)
        //is the origin located at the center of the window by default?
        //what happen when it is scaled? does point (0.5,0.5) scaled to (5,5)?
        glColor3f(0.0, 1.0, 0.0);
        drawSquare(1.0);
    glPopMatrix(); 
    //forget the current transformation matrix, pop out the one on top of the stack
    //which is the identity matrix

    //In the code below:
    //my understanding is 4 vertices is defined around origin, but is this the same origin?
    //then the unit square is moved just below the X-axis
    //and the 4 vertices are scaled one by one?
    //ex: (0.5,0) -> (1,0)  (0.5,-1) -> (1,-2)
    glScalef(2, 2, 1.0);
    glTranslatef(0.0, -0.5, 0.0);
    glColor3f(1.0, 0.0, 0.0);
    drawSquare(1.0);
    //last question, if I want to make the second square rotate at a point along z-axis
    //do I have to specify it here? 
    //for example: add glRotatef(rotate_degree, 0.0, 0.0, 1.0); above the glScalef(2, 2, 1.0);
    //such that later i can change the value in the rotate_degree?

glPopMatrix(); //forget about the current transformation matrix, pop out the top matrix on the stack.
genpfault
  • 51,148
  • 11
  • 85
  • 139
fiftyplus
  • 561
  • 10
  • 18
  • 2
    Don't use the fixed function pipeline this is the first misstake. – Felix K. Jan 29 '13 at 01:07
  • @FelixK. could you explain what is fixed function pipeline? – fiftyplus Jan 29 '13 at 01:08
  • @fiftyplus: All the matrix functions built into OpenGL. They're outdated. Modern OpenGL is all about shaders and shaders get them as "Matrices-Ready-to-Eat" in the form of uniforms. Libraries like GLM, Eigen or linmath.h (the last one is my creation) help you setting up the matrices. – datenwolf Jan 29 '13 at 01:10
  • Look for my first comment of my answer: http://stackoverflow.com/questions/14573079/fragment-shader-inexplicable-bahaviour – Felix K. Jan 29 '13 at 01:10
  • @FelixK. yes, there is a modern way and old way...but for study purpose, it's more straight forward, could you just bare with the old style...and just let me know if my understanding is wrong. Regardless the modern way or old way, the mathematic concept should be same, isnt it? – fiftyplus Jan 29 '13 at 01:20

1 Answers1

1

That the order of operations seems inverted comes from the fact that matrices are non-commutative and right associative when multiplied with column vectors. Say you have a position column vector ↑p in model space. To bring it into world space you multiply it with matrix M, i.e

↑p_world = M · ↑p

Note that you can not change the order of operations! Column vectors match like a key into matrices and the key fits into the matrix-lock from the right.

In the next step you want to transform into view space, using matrix V so you write

↑p_view = V · ↑p_world

but this you can substitute with

↑p_view = V · M · ↑p

But of course if you have a lot of ↑p-s you'd want to save on computations, so you contract those two matrices M and V into a single matrix you call modelview. And when you build modelview with OpenGL you build it like this:

MV = 1
MV = MV · V
MV = MV · M

Due to the right associativity of column order matrix multiplication the first transformation applied to a vector is the last one multiplied onto the stack.


Note that by using row order matrix math, things become left associative, i.e. things happen in the order you write them. But column order right associativity is incredibly usefull, as it makes building branching transformation hierachies much, much easier.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • @fiftyplus: I suggest you think about how you'd animate a solar system. Start with the central star, then draw the planet, each planet's moons, and so on. For each body you build upon the transformations applied to the previous stages in the transform chain. If you follow the chain star-planet-moon, the transformations are multiplied upon in exactly that order with right associative multiplication. – datenwolf Jan 29 '13 at 01:57