Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenGL固定坐标变换_Opengl_Coordinate Systems_Rotational Matrices - Fatal编程技术网

OpenGL固定坐标变换

OpenGL固定坐标变换,opengl,coordinate-systems,rotational-matrices,Opengl,Coordinate Systems,Rotational Matrices,我只是想用OpenGL进行一些渲染,我碰巧想到了这一点:如何在固定坐标系下运行转换。换句话说,如果你绕一个轴旋转一个物体,你会看到其他的也会旋转?因此,将在新构造轴上进行以下旋转 我在文件中读到了这一点(见第9.070部分),但我不知道: 1.如果行得通的话 2.如何实现它,因为我真的不明白我应该做什么 我想我不会是唯一一个想这么做的人,我想这也是一个有趣的问题 编辑:这是一个不起作用的实现,我想修复它 evoid display() { glClear(GL_COLOR_BUFFER_BIT

我只是想用OpenGL进行一些渲染,我碰巧想到了这一点:如何在固定坐标系下运行转换。换句话说,如果你绕一个轴旋转一个物体,你会看到其他的也会旋转?因此,将在新构造轴上进行以下旋转

我在文件中读到了这一点(见第9.070部分),但我不知道: 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 modelview矩阵用作堆栈。每个变换都是通过将变换与当前MVM矩阵相乘来执行的。换句话说,它看起来是这样的:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
这会导致变换始终发生在局部坐标系中。正如本文所述,您需要在固定坐标系(“全局坐标”)中进行转换,因此您需要像这样进行预乘:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
请注意,您可以将对象的转换矩阵存储为数据成员,而不是像本文指定的那样从OpenGL检索MVM。每次要变换对象时,只需将新变换与旧变换相乘即可

通过预乘计算对象的最终变换矩阵后,可以通过调用glMultMatrix将其传递给OpenGL


编辑:因为你需要自己计算矩阵,所以你需要一个数学库。如果您正在使用,您可以这样做:

旋转对象时:

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

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

OpenGL modelview矩阵作为堆栈工作。每个变换都是通过将变换与当前MVM矩阵相乘来执行的。换句话说,它看起来是这样的:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
这会导致变换始终发生在局部坐标系中。正如本文所述,您需要在固定坐标系(“全局坐标”)中进行转换,因此您需要像这样进行预乘:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
请注意,您可以将对象的转换矩阵存储为数据成员,而不是像本文指定的那样从OpenGL检索MVM。每次要变换对象时,只需将新变换与旧变换相乘即可

通过预乘计算对象的最终变换矩阵后,可以通过调用glMultMatrix将其传递给OpenGL


编辑:因为你需要自己计算矩阵,所以你需要一个数学库。如果您正在使用,您可以这样做:

旋转对象时:

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

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

OpenGL modelview矩阵作为堆栈工作。每个变换都是通过将变换与当前MVM矩阵相乘来执行的。换句话说,它看起来是这样的:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
这会导致变换始终发生在局部坐标系中。正如本文所述,您需要在固定坐标系(“全局坐标”)中进行转换,因此您需要像这样进行预乘:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
请注意,您可以将对象的转换矩阵存储为数据成员,而不是像本文指定的那样从OpenGL检索MVM。每次要变换对象时,只需将新变换与旧变换相乘即可

通过预乘计算对象的最终变换矩阵后,可以通过调用glMultMatrix将其传递给OpenGL


编辑:因为你需要自己计算矩阵,所以你需要一个数学库。如果您正在使用,您可以这样做:

旋转对象时:

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

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

OpenGL modelview矩阵作为堆栈工作。每个变换都是通过将变换与当前MVM矩阵相乘来执行的。换句话说,它看起来是这样的:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
这会导致变换始终发生在局部坐标系中。正如本文所述,您需要在固定坐标系(“全局坐标”)中进行转换,因此您需要像这样进行预乘:

New_MVM = Old_MVM * Transformation
New_MVM = Transformation * Old_MVM
请注意,您可以将对象的转换矩阵存储为数据成员,而不是像本文指定的那样从OpenGL检索MVM。每次要变换对象时,只需将新变换与旧变换相乘即可

通过预乘计算对象的最终变换矩阵后,可以通过调用glMultMatrix将其传递给OpenGL


编辑:因为你需要自己计算矩阵,所以你需要一个数学库。如果您正在使用,您可以这样做:

旋转对象时:

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的实际矩阵堆栈部分和在乘法前后发生的类似堆栈的行为的方式来描述这一点。我想我理解。现在让我们想象一下,我有我的转换矩阵(我想应用的那个),我会编写它吗?glMultMatrix在这方面起作用