我刚开始进行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.
答案 0 :(得分:1)
操作的顺序似乎是反转的,因为矩阵是非交换的,当与列向量相乘时是右关联的。假设您在模型空间中有一个位置列向量↑p 。为了将它带入世界空间,你将它乘以矩阵 M ,即
↑p _world = M ·↑p
请注意,您无法更改操作顺序!列向量像键一样匹配到矩阵中,键从右侧进入矩阵锁。
在下一步中,您想要转换为视图空间,使用矩阵 V ,以便您编写
↑p _view = V ·↑p _world
但你可以用
代替↑p _view = V · M ·↑p
但是当然如果你有很多↑p -s你想要保存计算,那么你收缩这两个矩阵 M 和 V 到一个矩阵中,你称之为modelview。当您使用OpenGL构建modelview时,您可以像这样构建它:
MV = 1
MV = MV · V
MV = MV · M
由于列顺序矩阵乘法的正确关联性,应用于向量的第一个变换是最后一个乘以堆栈的变换。
请注意,通过使用行顺序矩阵数学,事物变为左关联,即事情按照您编写它们的顺序发生。但是,列顺序正确的关联性是非常有用的,因为它使得构建分支转换层次变得更加容易。