#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0;
int oldX=0, oldY=0, nowRotate=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y) //mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX);
else
{
teapotX += (x-oldX)/150.0;
teapotY += (oldY-y)/150.0;
}
oldX=x; oldY=y;
glutPostRedisplay();
}
void mouse (int button, int state ,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN) nowRotate=1;
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN) nowRotate=0;
oldX=x; oldY=y;
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week03 Mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言