周围的OpenGL的一个支点旋转 [英] Rotation around a pivot point with OpenGL

查看:183
本文介绍了周围的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);

的OpenGL (扭转翻译/旋转的顺序),我用下面的code:

In OpenGL (reversing the order of translations/rotations), I used the following code:

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

不过,我画似乎没有矩形要围绕一个侧转的支点。 (尝试设置左侧为支点,并围绕它旋转)。我做了一个屏幕截图VID显示什么样的轮换我收到现在。这里的视频:

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

我如何设置支点此对象,使其在该点周围旋转?

How do I set the pivot for this object so that it rotated around that point?

推荐答案

问题来自

glTranslatef(0, 0, -10.0);

您所呼叫的 的旋转后。这意味着(0,0,-10)被施加到对象坐标(+ -50,+ - 50,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.

这里的code我用于测试,也许你得到的东西吧:

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

请注意,我没有使用任何矩阵堆栈功能,我只设置GL_MODELVIEW矩阵。

Note that I don't use any matrix stack functions, and I only set the GL_MODELVIEW matrix.

这篇关于周围的OpenGL的一个支点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆