#include <stdio.h>
#include <GL/glut.h>
void display();
void mouse(int,int,int,int);
void motion(int,int);
float potx=0,poty=0,oldx,oldy,oldpotx,oldpoty,angle=0,oldangle;
///oldx為用來記住原本滑鼠座標
///oldpotx為用來記住原本茶壺座標
///oldangle為用來記住原本茶壺旋轉角度
int main(int argc,char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04 Mouse");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glTranslatef(potx,poty,0);///移動的參數
glRotatef(angle,0,0,1);///旋轉的參數(旋轉角度,轉軸x向量,轉軸y向量,轉軸z向量)
glScalef(1,1,1);///縮放的參數
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
oldpotx=potx;///按下時實記住原本的位置、角度
oldpoty=poty;
oldangle=angle;
oldx=x;
oldy=y;
}
void motion(int x,int y)
{
potx=oldpotx+(x-oldx)/150.0;///新得數值等於原本的位置、角度加上移動量
poty=oldpoty+(oldy-y)/150.0;
angle=oldangle+(x-oldx);
glutPostRedisplay();///重畫畫面
}
沒有留言:
張貼留言