围绕与原点不同的点旋转

时间:2014-11-04 19:17:11

标签: c++ opengl 3d rotation translation

我试图使用glTranslate / glRotate编写摄像头代码。为了实现查找/查找功能,我需要渲染空间中的所有对象围绕一个点(即"相机"所在的位置)旋转,这点通常与原点不同。尽管如此,事物仍在原点周围旋转。有没有办法指定不同的点?


编辑:添加了代码

感谢您的快速回复。似乎无论如何我都无法正常工作,所以我决定添加我的代码;如果有人可以看一下并告诉我需要进行哪些更改才能翻译/旋转/翻译,我会非常感激。

#include <iostream>
#include <cmath>
#include <GLUT/GLUT.h>

const double roaming_step = .13;
double z_offset = .0;
double y_offset = .0;
double x_offset = .0;

const double angle_step = 1.5;
double angle_xz = .0;
double angle_yz = .0;

bool keyStates[256] = { false };

void drawFloor()
{
    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_QUADS);
        glVertex3f(-3.0, -1.0, 3.0);
        glVertex3f(-3.0, -1.0, -3.0);
        glVertex3f(3.0, -1.0, -3.0);
        glVertex3f(3.0, -1.0, 3.0);
    glEnd();
}

void drawBalls()
{           
    glTranslatef(-3.0, -.5, -3.0);
    glColor3f(.8, .1, .1);

    for(int i = 0; i < 3; i++)
    {
        glPushMatrix();

        glTranslatef(.0, -.5, i * 3);

        for(int j = 0; j < 3; j++)
        {
            glPushMatrix();

            glTranslatef(j * 3, .0, .0);
            glutSolidSphere(.5, 20, 20);

            glPopMatrix();
        }

        glPopMatrix();
    }
}

void keyPressed(unsigned char key, int x, int y)
{
    keyStates[key] = true;
}

void keyReleased(unsigned char key, int x, int y)
{
    keyStates[key] = false;
}

void keyboardOperations()
{   
    if(keyStates['w'])
        z_offset += roaming_step;

    if(keyStates['s'])
        z_offset -= roaming_step;

    if(keyStates['a'])
        x_offset += roaming_step;

    if(keyStates['d'])
        x_offset -= roaming_step;

    if(keyStates['i'])
    {
        angle_xz -= angle_step;

        if(angle_xz < .0)
            angle_xz += 360.0;
    }

    if(keyStates['o'])
    {
        angle_xz += angle_step;

        if(angle_xz >= 360.0)
            angle_xz -= 360.0;
    }

    if(keyStates['u'])
    {
        angle_yz -= angle_step;

        if(angle_yz < .0)
            angle_yz += 360.0;
    }

    if(keyStates['j'])
    {
        angle_yz += angle_step;

        if(angle_yz >= 360.0)
            angle_yz -= 360.0;
    }

    if(keyStates['q'])
        exit(0);
}

// I guess it has to be done in this function
// but didn't get how
void camera()
{
    glLoadIdentity();

    // Forward / Backward
    glTranslated(.0, .0, z_offset);

    // Left / Right
    glTranslated(x_offset, .0, .0);

    // XZ Rotation
    glRotated(angle_xz, .0, 1.0, .0);

    // YZ Rotation
    glRotated(angle_yz, 1.0, .0, .0);
}

void display(void)
{   
    keyboardOperations();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    camera();

    drawFloor();
    drawBalls();

    glutSwapBuffers();
}

void reshape(int width, int height)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0, 0, width, height);

    GLdouble aspect = (GLdouble) width / (GLdouble) height;
    gluPerspective(60, aspect, 1, 100);

    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{   
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("openGLtest3");
    glClearColor(.0, .0, .0, .0);

    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);

    glutIgnoreKeyRepeat(true);
    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyReleased);

    glutMainLoop();

    return 0;
}

1 个答案:

答案 0 :(得分:2)

在openGL中,glRotation功能将原点作为参考。为了围绕一个点(在这种情况下是您的相机坐标)旋转,您应该将相机位置转换为原点(相应地翻译所有对象),然后应用旋转功能。然后您可以将相机翻转回来(包含所有对象) )

假设你的相机位置是(a,b,c),所以你的代码应该是这样的:

foreach object
{
    glPushMatrix();
    glTranslate(a,b,c);
    glRotate(...);
    glTranslate(-a,-b,-c);
    //render
    glPopMatrix();
}