(2)主題 : 移動
主題 : 旋轉
主題 : 縮放
(3)範例 : Transformation.exe
(4)其中考題
(1)複習上周 : mouse移動
code:
#include <stdio.h>
#include <GL/glut.h> //整GLUT外掛
float teapotX=0, teapotY=0; //茶壺的座標
int oldX=0, oldY=0; //紀錄mouse在哪裡按下去
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_B UFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標移動
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
void mouse(int button, int state, int x, int y)
{
oldX = x;
oldY = y;
//紀錄按下去的點在哪裡
}
void motion(int x, int y) //mouse motion 事件
{
teapotX += (x-oldX)/150.0;
teapotY += (oldY-y)/150.0;
oldX = x; oldY = y; //紀錄移動後新的位置#include <stdio.h>
#include <GL/glut.h> //整GLUT外掛
float teapotX=0, teapotY=0; //茶壺的座標
int oldX=0, oldY=0; //紀錄mouse在哪裡按下去
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標移動
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
void mouse(int button, int state, int x, int y)
{
oldX = x;
oldY = y;
//紀錄按下去的點在哪裡
}
void motion(int x, int y) //mouse motion 事件
{
teapotX += (x-oldX)/150.0;
teapotY += (oldY-y)/150.0;
oldX = x; oldY = y; //紀錄移動後新的位置
glutPostRedisplay(); //重畫畫面
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);//初始的參數,照著丟進去
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);//2個顯示的參數
glutCreateWindow("Week03 Mouse");//設定視窗的名稱
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();//主要GLUT迴圈
}
glutPostRedisplay(); //重畫畫面
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);//初始的參數,照著丟進去
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);//2個顯示的參數
glutCreateWindow("Week03 Mouse");//設定視窗的名稱
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();//主要GLUT迴圈
}

沒有留言:
張貼留言