Part1 範例:Transformation.exe
2.將data和windows兩個資料夾解壓縮,接著將glut32.dll和data資料夾兩個複製後,貼到windows資料夾裡面,點兩下Transformation就可以會跳出以上圖片的視窗
3.在右邊圖示按右鍵可以換Model,下面視窗按左鍵可以調整綠色的數字大小
Part2 移動和旋轉茶壺
1.先建立GLUT專案(可參考week01內容)
2.將上週的程式碼貼上(移動)
3.將茶壺做旋轉,程式碼如下:
#include <stdio.h>#include <GL/glut.h> //整GLUT外掛
float teapotX=0, teapotY=0, angle=0; ///Now:rotate旋轉要的angle
int oldX=0, oldY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標移動
glRotatef(angle,0,0,1);///Now:茶壺要對著(0,0,1)做旋轉angle移動
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) //mouse motion 事件
{
/*teapotX += (x-oldX)/150.0;///依照motion時的x來改teapot的座標
teapotY += (oldY-y)/150.0;///依照motion時的y來改teapot的座標
oldX = x; oldY = y;///新的動法,要記下來oldX,oldY在哪兒*/
angle = x;///Now:滑鼠的座標,就是轉的角度
glutPostRedisplay(); //重畫畫面
}
/*void mouse(int button, int state, int x, int y)
{
oldX = x; oldY = y;///新的動法,要記下來oldX,oldY在哪兒
}*/
4.將兩個合併起來(移動+旋轉),程式碼如下:
#include <stdio.h>
#include <GL/glut.h> //整GLUT外掛
float teapotX=0, teapotY=0, angle=0; ///Now:rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0; ///Now3: oldX, oldY幫忙,了解你走了多少地方
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
glTranslatef(teapotX, teapotY, 0); //依照茶壺的座標移動
glRotatef(angle,0,0,1);///Now:茶壺要對著(0,0,1)做旋轉angle移動
glutSolidTeapot(0.3);
glPopMatrix();//還原矩陣
glutSwapBuffers(); //搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) //mouse motion 事件
{
if(nowRotate==1) angle+=(x-oldX);///Now:滑鼠的座標,就是轉的角度
else
{
teapotX += (x-oldX)/150.0;///依照motion時的x來改teapot的座標
teapotY += (oldY-y)/150.0;///依照motion時的y來改teapot的座標
}
oldX = x; oldY = y;///新的動法,要記下來oldX,oldY在哪兒
//angle = x;///Now:滑鼠的座標,就是轉的角度
glutPostRedisplay(); //重畫畫面
}
void mouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;///Now2:左鍵做旋轉
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;///Now2:右鍵是移動
oldX = x; oldY = y;///新的動法,要記下來oldX,oldY在哪兒
}







沒有留言:
張貼留言