第四週
1. 複習上週 mouse移動
2. 主題 : 移動,旋轉,縮放
3. 範例 : Transformation.exe
4. 期中考題
範例 : Transformation.exe
(1)進入連結網址下載data.zip, windows.zip, glut32.dll 三個檔案

(3)解壓縮windows.zip檔案

(4)把剛剛複製的glut32.dll貼上在解壓縮的windows檔案裡
(5) 也把data解壓縮並放進windows.zip檔案裡,即可開啟Transformation.exe

(6)開啟Transformation.exe
(7)在右邊視窗按右鍵難選擇不同角色
(8)此行程式碼控制攝影機上下左右前後移動

(9)後面三個數值分別代表X Y Z軸,當數值為1時代表固定相對應的軸
前面的數值代表旋轉角度
(10)此行程式碼可以調整X Y Z的大小
移動,旋轉,縮放
(1)按住滑鼠左鍵可旋轉
程式碼 :
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0,angle=0; //rotate旋轉要的angle
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix(); //備份矩陣
glTranslatef(teapotX,teapotY,0);
glRotatef(angle,0,0,1); //茶壺要對著(0,0,1)坐旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix(); //還原矩陣
glutSwapBuffers();
}
void motion(int x,int y)
{
angle=x; //滑鼠的座標就是轉的角度
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week03 Mouse");
glutDisplayFunc(display);
//glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
(2)按住滑鼠左鍵可旋轉,右鍵可移動茶壺
程式碼 :
#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); //依照茶壺的座標(teapotX,teapotY)移動
glRotatef(angle,0,0,1); //茶壺現在要對著(0,0,1)做旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
if(nowRotate==1)angle+=(x-oldX);
else
{
teapotX+=(x-oldX)/150.0; //依照motion時的x來改茶壺的座標
teapotY+=(oldY-y)/150.0; //依照motion時的y來改茶壺的座標
}
oldX=x;oldY=y; //新的動法要記下oldX oldY在哪裡
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();
}
(2)按住滑鼠左鍵可旋轉,右鍵可移動茶壺
程式碼 :
#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); //依照茶壺的座標(teapotX,teapotY)移動
glRotatef(angle,0,0,1); //茶壺現在要對著(0,0,1)做旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
if(nowRotate==1)angle+=(x-oldX);
else
{
teapotX+=(x-oldX)/150.0; //依照motion時的x來改茶壺的座標
teapotY+=(oldY-y)/150.0; //依照motion時的y來改茶壺的座標
}
oldX=x;oldY=y; //新的動法要記下oldX oldY在哪裡
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();
}








沒有留言:
張貼留言