OpenGL门动画

发布于 2024-11-30 11:08:26 字数 1594 浏览 0 评论 0原文

我正在尝试为门设置动画,这样如果我按下按钮,门就会打开。目前它已编译,当我按下按钮时,它会移动相机,然后我无法再控制相机。代码有什么问题吗?

glPushMatrix;
glTranslatef (door_Xpos,0.0, 0.0);
glRotatef (door_Angle, 1,0,0);

glBegin(GL_QUADS);

//Door left
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);

//door right
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);

glPopMatrix;

void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'a':
        case 'A':
            glTranslatef(5.0, 0.0, 0.0);
            break;

        case 'd':
        case 'D':
            glTranslatef(-5.0, 0.0, 0.0);
            break;

        case 'w':
        case 'W':
            glTranslatef(0.0, 0.0, 5.0);
            break;

        case 's':
        case 'S':
            glTranslatef(0.0, 0.0, -5.0);
            break;

        case 't':
        case 'T':
            if (is_depth)
            {
                is_depth = 0;
                glDisable(GL_DEPTH_TEST);
            }
            else
            {
                is_depth = 1;
                glEnable(GL_DEPTH_TEST);
            }

        #Fall-through...
        case 'o':
        case 'O':
            door_Xpos += 90.0;
            break;
    }
    display();
}

I'm trying to animate a door so that if I press a button the door will open. At the moment it compiles, and when I go to press the button it moves the camera and then I can't control the camera anymore. What is wrong with the code?

glPushMatrix;
glTranslatef (door_Xpos,0.0, 0.0);
glRotatef (door_Angle, 1,0,0);

glBegin(GL_QUADS);

//Door left
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(-10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(-10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);

//door right
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(10.0, 0.0, -25.0);
glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(10.0, 15.0, -25.0);
glColor3f(0.6f, 0.6f, 0.6f);
glVertex3f(0.0, 15.0, -25.0);
glColor3f(0.8f, 0.8f, 0.8f);
glVertex3f(0.0, 0.0, -25.0);

glPopMatrix;

void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 'a':
        case 'A':
            glTranslatef(5.0, 0.0, 0.0);
            break;

        case 'd':
        case 'D':
            glTranslatef(-5.0, 0.0, 0.0);
            break;

        case 'w':
        case 'W':
            glTranslatef(0.0, 0.0, 5.0);
            break;

        case 's':
        case 'S':
            glTranslatef(0.0, 0.0, -5.0);
            break;

        case 't':
        case 'T':
            if (is_depth)
            {
                is_depth = 0;
                glDisable(GL_DEPTH_TEST);
            }
            else
            {
                is_depth = 1;
                glEnable(GL_DEPTH_TEST);
            }

        #Fall-through...
        case 'o':
        case 'O':
            door_Xpos += 90.0;
            break;
    }
    display();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吲‖鸣 2024-12-07 11:08:26

OpenGL 不是场景图,glTranslateglRotate 和类似的不会移动对象,它们会更改应用于绘制到屏幕上的三角形的变换。因此,在事件处理程序中调用矩阵操作函数是没有意义的。实际上,大多数 OpenGL 函数(包括矩阵操作)都是从 display 函数调用的。此规则的唯一例外是数据上传,例如纹理或缓冲区对象数据。

尝试根据事件处理程序设置的变量在显示函数中为门设置动画。

OpenGL is not a scene graph, glTranslate, glRotate and similar don't move objects around, they change the transformation applied to triangles drawn to the screen. Thus it makes no sense to call matrix manipulations functions in an event handler. Actually most OpenGL functions, that includes matrix manipulation, are to be called from the display function. The only exception to this rule are data uploads like textures or buffer object data.

Try animating your door from within the display function based on variables set by the event handlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文