我只是想用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
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);
免责声明:所有代码未经测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句