【问题标题】:OpenGL fixed coordinate transformationsOpenGL固定坐标变换
【发布时间】:2015-05-29 14:48:05
【问题描述】:

我只是想用 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 coordinate-systems rotational-matrices


    【解决方案1】:

    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);
    

    免责声明:所有代码未经测试。

    【讨论】:

    • 我想我明白了。现在让我们想象一下我有我的转换矩阵(我想应用的那个)我会编码吗? glMultMatrix 在这种情况下有效吗?
    • 是的,glMultMatrix 将在您获得对象的最终转换矩阵后正常工作。
    • 好的。我现在完全理解了这个理论,但我坚持实施。如果我用一段代码编辑我的问题,你介意帮助我吗?
    • +1 很好的描述;我见过人们用一种将 API 的实际矩阵堆栈部分与在乘法前和后乘法中发生的 stack-like 行为混淆的方式来描述这一点。
    猜你喜欢
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 2012-10-13
    • 1970-01-01
    相关资源
    最近更新 更多