OpenGL门动画
我正在尝试为门设置动画,这样如果我按下按钮,门就会打开。目前它已编译,当我按下按钮时,它会移动相机,然后我无法再控制相机。代码有什么问题吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenGL 不是场景图,glTranslate、glRotate 和类似的不会移动对象,它们会更改应用于绘制到屏幕上的三角形的变换。因此,在事件处理程序中调用矩阵操作函数是没有意义的。实际上,大多数 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.