(1) 了解Transformation
1. 首先,跟上週一樣先去老師的網站下載下面這三個檔案
以及解壓縮,步驟都跟上禮拜教的一樣
2. 不同的點在於這次我們要使用的是Transformation
一樣可以調整數值大小來做移動,比較不同的在於
glRotatef → 第一個數值是大小,後面三個則分別是
X , Y , Z 軸的旋轉,如果還是不清楚他是如何轉的可
以用右手定則去看,大拇指是軸心,他會往其他四指
所指的方向去做轉動
(2) 複習上週的茶杯
1. 複習上週的茶杯,一樣可以移動,不過有小bug
2. 讓茶杯可以旋轉以及移動
code :
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0; //rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0; // oldX , oldY 幫助你了解你移動了多少
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(); //搭配GLUT_DOUBLE兩倍顯示
}
void motion(int x, int y) //mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX); // 新的移動方法,加上新的增加量
else{
teapotX += (x-oldX)/150.0; // 依照motion時的x來改teapot的座標,這是不好的動法。現 在加上新的增加量
teapotY += (oldY-y)/150.0; // 依照motion時的y來改teapot的座標,這是不好的動法。現 在加上新的增加量
}
oldX=x; oldY=y;
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();//主要GLUT迴圈
}




沒有留言:
張貼留言