如何使用键盘移动对象?

时间:2015-04-11 18:05:16

标签: c++ opengl graphics 3d

我试图通过按键盘上的h键将整个桌子移动到左侧,同时还旋转椅子,好像整个场景向右移动一样。但似乎没有发生任何事情。 glpushmatrix()和glpopmatrix()似乎最适合这项工作。 我试过" group"每个整个对象(椅子和桌子)使用auto []函数组成各自的结构组。 任何帮助或建议表示赞赏。

 #include <GLUT/GLUT.h>

char press;
int anglex,angley,x,y,xold,yold;
int angleHead = 0;



void display();
void keyboard(unsigned char touche,int x,int y);
void reshape(int x,int y);
void idle();
void mouse(int bouton,int etat,int x,int y);
void mousemotion(int x,int y);

int main(int argc,char **argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow("Scene");


    glClearColor(0.0,0.0,0.0,0.0);
    glColor3f(1.0,1.0,1.0);
    glPointSize(2.0);
    glEnable(GL_DEPTH_TEST);


    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMotionFunc(mousemotion);


    glutMainLoop();
    return 0;
}

void display() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    auto chair = [] {
        glColor3f(0.0, 0.0, 1.0);
        glLoadIdentity();
        glRotatef(-angley,1.0,0.0,0.0);
        glRotatef(-anglex,0.0,1.0,0.0);
        glPushMatrix();
        glScalef(0.15, 1.65, 0.6);
        glTranslatef(5, 0.0, 0.0);
        glutWireCube(0.5);
        glPopMatrix();
        glPushMatrix();
        glScalef(0.5, 0.2, 0.6);
        glTranslatef(1.18, -0.5, 0);
        glutWireCube(0.5);
        glPopMatrix();
        glScalef(0.2, 0.5, 0.6);
        glTranslatef(2.59, -0.55, 0);
        glutWireCube(0.5);
    };

    auto table =[] {
        glColor3f(0.36, 0.25, 0.20);
        glLoadIdentity();
        glRotatef(-angley,1.0,0.0,0.0);
        glRotatef(-anglex,0.0,1.0,0.0);
        glPushMatrix();
        glScalef(1.5, 0.15, 0.6);
        glTranslatef(-0.055, 0.26, 0.0);
        glutWireCube(0.5);
        glPopMatrix();
        glPushMatrix();
        glScalef(0.15, 0.8, 0.6);
        glTranslatef(1.7, -0.25, 0.0);
        glutWireCube(0.5);
        glPopMatrix();
        glScalef(0.15, 0.8, 0.6);
        glTranslatef(-2.8, -0.25, 0.0);
        glutWireCube(0.5);

        auto box = [] {
            glColor3f(0.0, 1.0, 0.0);
            glLoadIdentity();
            glRotatef(-angley,1.0,0.0,0.0);
            glRotatef(-anglex,0.0,1.0,0.0);
            glPushMatrix();
            glScalef(0.75, 0.2, 0.6);
            glTranslatef(-0.1, 0.65, 0.0);
            glutWireCube(0.5);
            glPopMatrix();
            glPushMatrix();
            glColor3f(1.0, 0.0, 0.0);
            glScalef(0.2, 0.2, 0.2);
            glTranslatef(0.1, 1, 0.0);
            glutWireSphere(0.4, 30, 30);
            glPopMatrix();
            glColor3f(1.0, 1.0, 0.0);
            glScalef(0.2, 0.2, 0.2);
            glTranslatef(-0.4, 1, 0.0);
            glutWireSphere(0.4, 30, 30);
        };
        box();

    };
    glPushMatrix();
    glTranslatef(-0.5 - angleHead, 0.0, 0.0);
    chair();
    table();
    glPopMatrix();
    glLoadIdentity();
    glColor3f(1.0, 1.0, 1.0);
    glTranslatef(0.0, -1.0, 0.0);
    glScalef(7, 0.1, 5);
    glutWireCube(1);


    glutSwapBuffers();
}


void keyboard(unsigned char touche,int x,int y)
{

    if (touche =='h'){
            angleHead=angleHead + 0.5;
            glutPostRedisplay();
    }
}

void reshape(int x,int y)
{
    if (x<y)
        glViewport(0,(y-x)/2,x,x);
    else
        glViewport((x-y)/2,0,y,y);
}

void mouse(int button, int state,int x,int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        press = 1;
        xold = x;
        yold=y;
    }

    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
        press=0; 
}

void mousemotion(int x,int y)
{
    if (press)
    {

        anglex=anglex+(x-xold);
        angley=angley+(y-yold);
        glutPostRedisplay();
    }

    xold=x;
    yold=y;
}

1 个答案:

答案 0 :(得分:0)

有两个原因没有发生:

  1. 您将0添加到int。 int表示整数。所以你基本上每增加angleHead
  2. 就加0
  3. 致电glTranslatef()后,再次致电glLoadIdentity(),重置glTranslatef()
  4. 中的翻译