只是尝试用OpenGL做一些渲染,我碰巧想到了:你将如何在固定坐标系中运行转换。换句话说,如果围绕一个轴旋转对象,您会看到其他轴也将旋转?因此,将在新构造的轴上进行以下旋转。
我在文档上看到了这一点(参见9.070部分)https://www.opengl.org/archives/resources/faq/technical/transformations.htm,但我不知道: 1.如果有效 2.如何实施它因为我真的不明白我应该做什么。
我认为我不是唯一一个想要这样做的人,而且我猜这也是一个有趣的问题。
编辑:这是一个不起作用的实现,我想修复
evoid display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
//glRotatef(angleCube, 1.0f, 1.0f, 1.0f); // Rotate about (1,1,1)-axis [NEW]
//Matrix a transformation matrix obtained from a Translation, Scale and Rotation composition
// In an other piece of the code I wanted to try
// The matrix is a float[16] and the values are correct and updated every now and then to make the cube rotate
glMultMatrixf(matrix);
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
答案 0 :(得分:4)
OpenGL模型视图矩阵用作堆栈。通过将变换与当前MVM矩阵进行后乘以来执行每个变换。换句话说,它看起来像这样:
New_MVM = Old_MVM * Transformation
这会导致转换始终发生在局部坐标系中。正如文章所述,你需要转换在固定坐标系统(“全局坐标”)中工作,所以你需要像这样预乘:
New_MVM = Transformation * Old_MVM
请注意,您可以将对象的转换矩阵存储为数据成员,而不是像文章指定的那样从OpenGL检索MVM。每次要转换对象时,只需将新转换预先与旧转换相乘。
通过预乘后计算对象的最终变换矩阵后,可以通过调用glMultMatrix将其传递给OpenGL。
编辑:由于您需要自己计算矩阵,因此您需要一个数学库。如果您使用GLM,则可以执行以下操作:
旋转物体时:
glm::vec3 axis(x, y, z);
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), angle_in_degrees, axis);
totalTransform = rotation * totalTransform;
然后在你的'显示'方法中:
float *matPtr = glm::value_ptr(totalTransform);
glMultMatrixf(matPtr);
免责声明:所有代码均未经测试。