用timer當計時器會比Idle更好
因問Idle會忽快忽慢
#include <GL/glut.h>
float angle=0;
void timer(int t)
{
glutTimerFunc(10, timer, 1);
angle+=1;
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10, timer, 0);
glutMainLoop();
}
憤怒的茶壺
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0, potVX=0, potVY=0;
int potState=0;
void timer(int t)
{
glutTimerFunc(10, timer, 1);
angle+=1;
if(potState==0) potY=0.5*sin(angle/180.0*3.14);
if(potState==2){ potX+=potVX; potY+=potVY; }
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void motion(int x, int y)
{
if(potState==1){potX=(x-350)/350.0; potY=-(y-350)/350.0;}
}
void mouse(int button, int state, int x ,int y)
{
if(state==GLUT_DOWN){ potState=1;}
else if(state==GLUT_UP){potState=2; potVX=-(x-350)/3500.0 ; potVY=(y-350)/3500.0;}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10, timer, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言