[英]Rotation around a pivot point with OpenGL
I am trying to rotate an object about one of its side and already tried the common approach as found on the forums: 我正在尝试旋转一个关于其中一个方面的对象,并且已经尝试了在论坛上找到的常用方法:
translate(-P);
rotate();
translate(P);
In OpenGL
(reversing the order of translations/rotations), I used the following code: 在
OpenGL
(颠倒翻译/旋转的顺序),我使用了以下代码:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(-50, 50, 0);
glRotatef(rotationCoord, 0, 1, 0);
glTranslatef(50, -50, 0);
glBegin(GL_QUADS);
glVertex3f(-50.0, 50.0, 0);
glVertex3f(50.0, 50.0, 0);
glVertex3f(50.0, -50.0, 0);
glVertex3f(-50.0, -50.0, 0);
glEnd();
glPopMatrix();
However, the rectangle that I am drawing doesn't seem to be rotating around one side as pivot. 但是,我正在绘制的矩形似乎不是围绕一侧作为枢轴旋转。 (trying to set the left side as the pivot point and rotate around it).
(尝试将左侧设置为枢轴点并围绕它旋转)。 I made a screen capture vid to show what kind of rotation I am getting right now.
我制作了一个屏幕截图视频,以显示我现在正在获得什么样的轮播。 Here's the video:
这是视频:
http://youtu.be/VgEZ_rsG3xU http://youtu.be/VgEZ_rsG3xU
How do I set the pivot for this object so that it rotated around that point? 如何为此对象设置枢轴以使其围绕该点旋转?
The problem comes from the 问题来自于
glTranslatef(0, 0, -10.0);
you're calling after the rotation. 你在轮换后打电话。 That means the (0,0,-10) is applied to the object coordinates (+-50,+-50,0) before they are transformed by anything else, so the quad is offset before it's being rotated.
这意味着(0,0,-10)在被其他任何东西变换之前应用于对象坐标(+ -50,+ - 50,0),因此四边形在旋转之前是偏移的。
Here's the code I used for testing, maybe you get something out of it: 这是我用于测试的代码,也许你从中获得了一些东西:
#include <glm/glm.hpp>
#include <glm/ext.hpp>
using namespace glm;
void display(void)
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat aspect = (float)g_Width / g_Height;
mat4 eyeToCamera = perspective(45.f, aspect, g_nearPlane, g_farPlane); // projection
mat4 cameraToWorld = lookAt(vec3(0, 0, -150), vec3(), vec3(0, 1, 0)); // view
vec3 pivot(50, -50, 0);
mat4 worldToObject = translate(pivot)
* rotate(rotationAngle, vec3(0, 1, 0))
* translate(-pivot); // model
glLoadMatrixf(value_ptr(eyeToCamera * cameraToWorld * worldToObject));
glBegin(GL_QUADS);
glVertex3f(-50.0, 50.0, 0);
glVertex3f(50.0, 50.0, 0);
glVertex3f(50.0, -50.0, 0);
glVertex3f(-50.0, -50.0, 0);
glEnd();
glutSwapBuffers();
}
Note that I don't use any matrix stack functions, and I only set the GL_MODELVIEW matrix. 请注意,我不使用任何矩阵堆栈函数,我只设置GL_MODELVIEW矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.