2.主題:移動
主題:旋轉
主題:縮放
3.範例:Transformation.exe
(1)到 http://jsyeh.org/3dcg10 下載 data.zip, windows.zip, glut32.dll
(2)解壓縮data, windows,把data和glut32.dll 丟進 windows
(3)可以開始執行Transformation.exe
第一格是旋轉角度,Y軸為1的時候表示是以Y軸正向為中心,比個大拇指,朝著其他四指 旋轉。若為-1時,大拇指朝向Y軸負向。

4.讓茶壺可以用滑鼠移動、旋轉

程式碼:
#include <stdio.h>
#include <GL/glut.h>
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(); ///Now: 備份矩陣
glTranslatef(teapotX, teapotY, 0); ///Now: 依照 茶壺的座標(teapotX, teapotY) 移動
glRotatef(angle, 0, 0, 1); ///Now: 茶壺要對著 (0, 0, 1)坐旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix(); ///Now: 還原矩陣
glutSwapBuffers(); ///搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) ///mouse motion事件
{
if(nowRotate==1) angle += (x-oldX);///Now: 滑鼠的座標, 就是轉的角度 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
else{
teapotX +=(x-oldX)/150.0; ///依照motion時的x來改teapot的座標 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
teapotY +=(oldY-y)/150.0; ///依照motion時的y來改teapot的座標 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
}
oldX=x; oldY=y; ///Now3: 新的動法, 要記下來 oldX, oldY在哪兒
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; ///Now3: 新的動法, 要記下來 oldX, oldY在哪兒
}
int main(int argc, char**argv) ///完整的main()參數
{
glutInit(&argc, argv); ///初始的參數, 照著丟進去
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///2個顯示的參數
glutCreateWindow("Week03 Mouse"); ///本週的Window標題改一下
glutDisplayFunc(display); ///等一下樓上會有display()
glutMouseFunc(mouse); ///等一下樓上會有mouse()滑鼠的事件
glutMotionFunc(motion); ///Now: 等一下樓上會有mouse()滑鼠motion事件
glutMainLoop(); ///主要GLUT迴圈
}
主題:縮放
3.範例:Transformation.exe
(1)到 http://jsyeh.org/3dcg10 下載 data.zip, windows.zip, glut32.dll
(2)解壓縮data, windows,把data和glut32.dll 丟進 windows
(3)可以開始執行Transformation.exe
第一格是旋轉角度,Y軸為1的時候表示是以Y軸正向為中心,比個大拇指,朝著其他四指 旋轉。若為-1時,大拇指朝向Y軸負向。

4.讓茶壺可以用滑鼠移動、旋轉

程式碼:
#include <stdio.h>
#include <GL/glut.h>
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(); ///Now: 備份矩陣
glTranslatef(teapotX, teapotY, 0); ///Now: 依照 茶壺的座標(teapotX, teapotY) 移動
glRotatef(angle, 0, 0, 1); ///Now: 茶壺要對著 (0, 0, 1)坐旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix(); ///Now: 還原矩陣
glutSwapBuffers(); ///搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) ///mouse motion事件
{
if(nowRotate==1) angle += (x-oldX);///Now: 滑鼠的座標, 就是轉的角度 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
else{
teapotX +=(x-oldX)/150.0; ///依照motion時的x來改teapot的座標 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
teapotY +=(oldY-y)/150.0; ///依照motion時的y來改teapot的座標 Now2: 不好的移動 Now3: 新的動法, 加上新的增加量
}
oldX=x; oldY=y; ///Now3: 新的動法, 要記下來 oldX, oldY在哪兒
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; ///Now3: 新的動法, 要記下來 oldX, oldY在哪兒
}
int main(int argc, char**argv) ///完整的main()參數
{
glutInit(&argc, argv); ///初始的參數, 照著丟進去
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///2個顯示的參數
glutCreateWindow("Week03 Mouse"); ///本週的Window標題改一下
glutDisplayFunc(display); ///等一下樓上會有display()
glutMouseFunc(mouse); ///等一下樓上會有mouse()滑鼠的事件
glutMotionFunc(motion); ///Now: 等一下樓上會有mouse()滑鼠motion事件
glutMainLoop(); ///主要GLUT迴圈
}
沒有留言:
張貼留言