各地从原产地不同的一点旋转 [英] Rotating around a point different from origin

查看:206
本文介绍了各地从原产地不同的一点旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想codeA相机glTranslate / glRotate。为了实现该查找/俯视功能我需要在我的渲染空间中的所有对象到周围的点旋转(即,这里的相机是),点通常不同于原点。不过,事情一直绕原点。有没有一种方法来指定一个不同点?


编辑:添加code

感谢您的快速回复。看来我无法得到它的工作的权利,不管是什么,所以我决定把我的code;我更AP preciate如果有人可以看看,并告诉我需要以什么样的变化转换/旋转/转换回。

 的#include<的iostream>
#包括< CMATH>
#包括<转运蛋白/ GLUT.h>

常量双roaming_step = 0.13;
双z_offset = 0.0;
双y_offset = 0.0;
双x_offset = 0.0;

常量双angle_step = 1.5;
双angle_xz = 0.0;
双angle_yz = 0.0;

布尔keyStates [256] = {假};

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

无效drawBalls()
{
    的glTranslatef(-3.0,-0.5,-3.0);
    glColor3f(0.8,.1,0.1);

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

        的glTranslatef(2.0,-​​0.5,I * 3);

        为(诠释J = 0; J&所述; 3; J ++)
        {
            glPushMatrix();

            的glTranslatef(j * 3,2.0,0.0);
            glutSolidSphere(0.5,20,20);

            glPopMatrix();
        }

        glPopMatrix();
    }
}

无效键pressed(无符号字符键,诠释的x,int y)对
{
    keyStates [关键] =真;
}

无效的keyReleased(无符号字符键,诠释的x,int y)对
{
    keyStates [关键] = FALSE;
}

无效keyboardOperations()
{
    如果(keyStates ['瓦特'])
        z_offset + = roaming_step;

    如果(keyStates ['S'])
        z_offset  -  = roaming_step;

    如果(keyStates ['一'])
        x_offset + = roaming_step;

    如果(keyStates ['D'])
        x_offset  -  = roaming_step;

    如果(keyStates ['我'])
    {
        angle_xz  -  = angle_step;

        如果(angle_xz< .0)
            angle_xz + = 360.0;
    }

    如果(keyStates ['O'])
    {
        angle_xz + = angle_step;

        如果(angle_xz> = 360.0)
            angle_xz  -  = 360.0;
    }

    如果(keyStates ['U'])
    {
        angle_yz  -  = angle_step;

        如果(angle_yz< .0)
            angle_yz + = 360.0;
    }

    如果(keyStates ['J'])
    {
        angle_yz + = angle_step;

        如果(angle_yz> = 360.0)
            angle_yz  -  = 360.0;
    }

    如果(keyStates ['Q'])
        出口(0);
}

//我猜它必须在这个函数来完成
//但没有得到如何
无效摄像头()
{
    glLoadIdentity();

    //前进/后退
    glTranslated(2.0,2.0,z_offset);

    // 左右
    glTranslated(x_offset,2.0,0.0);

    // XZ旋转
    glRotated(angle_xz,2.0,1.0,0.0);

    // YZ旋转
    glRotated(angle_yz,1.0,0.0,0.0);
}

无效的显示(无效)
{
    keyboardOperations();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    相机();

    drawFloor();
    drawBalls();

    glutSwapBuffers();
}

无效重塑(INT宽度,高度INT)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0,0,宽度,高度);

    GLdouble方面=(GLdouble)宽/(GLdouble)高度;
    gluPerspective(60,一方面,1,100);

    glMatrixMode(GL_MODELVIEW);
}

INT主(INT ARGC,字符** argv的)
{
    glutInit(安培; ARGC,ARGV);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutCreateWindow(openGLtest3);
    glClearColor(2.0,2.0,2.0,0.0);

    过glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);

    glutDisplayFunc(显示);
    glutReshapeFunc(重塑);
    glutIdleFunc(显示);

    glutIgnoreKeyRepeat(真正的);
    glutKeyboardFunc(键pressed);
    glutKeyboardUpFunc(的keyReleased);

    glutMainLoop();

    返回0;
}
 

解决方案

在OpenGL中, glRotation 与作用需要原点作为参考。为了绕一个点(在这种情况下,你的相机坐标),你应把你的相机位置到原点(翻译所有对象相应的),然后应用旋转function.And那么你可以转换你的相机背面(与所有对象)

让我们说你的相机位置(A,B,C),这样您code应该是这样的:

 的foreach对象
{
    glPushMatrix();
    glTranslate(A,B,C);
    glRotate(...);
    glTranslate(-a,-b,-c);
    //渲染
    glPopMatrix();
}
 

I'm trying to code a camera with glTranslate/glRotate. To implement the look-up / look-down functions I need all the objects in my rendering space to rotate around a point (that is, where the "camera" is at), point which usually differs from the origin. Still, things keep rotating around the origin. Is there a way to specify a different point?


EDIT: Added code

Thanks for your fast reply. It seems that I can't get it working right no matter what, so I've decided to add my code; I'd much appreciate if someone could take a look at it and tell me what changes are needed in order to translate/rotate/translate back.

#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;
}

解决方案

In openGL, glRotation fuction takes origin as a reference. In order to rotate around a point (your camera coordinate in this case) you should translate your camera position to the origin (Translate all your objects accordingly) and then apply rotation function.And then you can translate your camera back (with all your objects)

lets say your camera position is (a,b,c) so your code should be something like :

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

这篇关于各地从原产地不同的一点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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