如何使用键盘移动对象

How can i move the object using the keyboard?

本文关键字:对象 移动 键盘 何使用      更新时间:2023-10-16

我试图通过按键盘上的h键将整个桌子向左移动,同时旋转椅子,就好像整个场景都在向右移动一样。但似乎什么都没有发生。glpushmatrix() 和 glpopmatrix() 似乎最适合这项工作。我尝试使用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.5 添加到整数中。int 表示整数。所以你基本上在每次增量时都加 0 angleHead
  2. 调用glTranslatef()后,您再次致电glLoadIdentity(),这会重置您的翻译glTranslatef()