我的OpenGL项目中的动画有些麻烦(窗口在几秒钟后没有响应或点击时,动画被卡住并在看似随机的点上循环)所以我被建议添加一个空闲的回调然后使Display
函数只在每次调用时呈现“当前”帧。
虽然我不知道如何让我的Display
功能只渲染“当前”帧。
这是我的Display
功能:
// GLUT display callback function
void Display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); // transformations are represented by matrices
// for placing camera
gluLookAt(0,1,50,0,0,0,0,1,0);
// for mouse movement
glTranslatef(g_fTransX,g_fTransY,g_fZoom);
glRotatef (g_fSpinX,1.0f,0.0f,0.0f);
glRotatef (g_fSpinY,0.0f,1.0f,0.0f);
glLightfv(GL_LIGHT0,GL_POSITION,lpos);
// xyz axes
glDisable(GL_LIGHTING);
glLineWidth(2.0);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(20.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 20.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 20.0);
glEnd();
glEnable(GL_LIGHTING);
float Ambient_m[] = {0.0f,1.0f,0.0f,0.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,Ambient_m);
float Ambient_l[] = {0.2f,0.2f,0.2f,0.0f};
glLightfv(GL_LIGHT0,GL_AMBIENT,Ambient_l);
// implementing our custom cylinder function
//draw_cylinder(g_translate_x, g_translate_y, g_dof3_angle);
// our csg object with 4 DOF
GLUquadricObj * qobj = gluNewQuadric();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//draw_lamp(g_dof3_angle);
//glutSwapBuffers();
/*animate(time_from, time_to, dof1_from, dof1_to,
dof2_from, dof2_to, dof3_from, dof3_to,
dof4_from, dof4_to, dof5_from, dof5_to,
dof6_from, dof6_to, dof7_from, dof7_to)*/
animate_lamp(0.0f, 5.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 45.0f, 0.0f, -45.0f,
0.0f, 0.0f, 0.0f, 0.0f);
animate_lamp(5.0f, 7.0f, 0.0f, 5.0f,
0.0f, 5.0f, 0.0f, 0.0f,
45.0f, -45.0f, -45.0f, 45.0f,
0.0f, 0.0f, 0.0f, 0.0f);
animate_lamp(7.0f, 10.0f, 5.0f, 10.0f,
5.0f, 0.0f, 0.0f, 0.0f,
45.0f, -45.0f, -45.0f, 45.0f,
0.0f, 0.0f, 0.0f, 0.0f);
//glutSwapBuffers(); // swap back buffer to front buffer (which is drawn on screen)
}
答案 0 :(得分:2)
很简单 - 在某处存储动画的当前状态(因为你正在使用C ++,你可以为它创建一个对象),然后,在你的渲染函数中,从该对象中查询状态并渲染它。 / p>
这是一个简化的例子:
class AnimationData
{
float pos;
public:
void step() { pos += 0.1f; }
void render() const
{
glBegin(GL_TRIANGLES);
glVertex3f(pos, 1.f, 1.f);
glVertex3f(pos, -1.f, 1.f);
glVertex3f(pos + 1.f, 0.f, 1.f);
glEnd(GL_TRIANGLES);
}
};
AnimationData myAnimation;
void Display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); // transformations are represented by matrices
// for placing camera
gluLookAt(0,1,50,0,0,0,0,1,0);
// for mouse movement
glTranslatef(g_fTransX,g_fTransY,g_fZoom);
glRotatef (g_fSpinX,1.0f,0.0f,0.0f);
glRotatef (g_fSpinY,0.0f,1.0f,0.0f);
glLightfv(GL_LIGHT0,GL_POSITION,lpos);
// And any other setup
myAnimation.render();
glutSwapBuffers();
}
void timerCallbackFunction()
{
myAnimation.step();
glutPostRedisplay();
}
每次调用timerCallbackFunction()
时,都会通过向右移动一个三角形来设置动画。显示功能只是将三角形呈现在当前位置。