0

刚开始学opengl,先贴一段代码,说明一下我的理解,能不能按照我的说明,指出有什么问题?

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.
4

1 回答 1

1

运算顺序似乎是倒置的,这是因为矩阵在与列向量相乘时是非交换的和右结合的。假设您在模型空间中有一个位置列向量↑p 。要将其带入世界空间,请将其与矩阵M相乘,即

↑p _world = M · ↑p

请注意,您不能更改操作顺序!列向量像键一样匹配到矩阵中,并且键从右侧匹配到矩阵锁中。

在下一步中,您要使用矩阵V转换为视图空间,因此您可以编写

↑p _view = V · ↑p _world

但是这个你可以用

↑p _view = V · M · ↑p

但是当然,如​​果你有很多↑p -s 你想节省计算,所以你将这两个矩阵MV压缩成一个你称为 modelview 的矩阵。当您使用 OpenGL 构建模型视图时,您可以像这样构建它:

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

由于列顺序矩阵乘法的右关联性,应用于向量的第一个变换是最后一个乘以到堆栈上的变换。


请注意,通过使用序矩阵数学,事物变得左关联,即事物按照您编写它们的顺序发生。但是列顺序右关联性非常有用,因为它使构建分支转换层次结构变得更加容易。

于 2013-01-29T01:20:52.500 回答