憤怒茶壺(鳥?)
#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,t+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 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;
}
}
void motion(int x,int y)
{
if(potState==1)
{
potX=(x-350)/350.0;
potY=-(y-350)/350.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();
}
沒有留言:
張貼留言