1.主題 Rotate旋轉
jsyeh.org/3dcg 下載
data.zip=>windows=>data
windows.zip
2.測試Rotate功能
決定旋轉方向
利用國中學習的右手定則 來判定它旋轉的方向
x,y,z皆可用此方法來判斷旋轉方向
3.打開CODEBLOCKS利用上周的程式碼
多加一行glRotatef(90,0,0,1);
讓茶壺可以旋轉90度
3-1 motion
加入四行程式碼讓茶壺可以透過滑鼠拖動旋轉
motion與上節所學mouse不同,mouse是滑鼠點擊
3-2為了解決每次重新拖動都會在原點的問題,加入幾行上周的mouse程式碼,利用大象放進冰箱的原理,利用新的減去舊的位置,讓每次拖動能在上一次拖動的位置
#include <GL/glut.h>
float angle=0,oldX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void motion(int x,int y)
{
angle += (x-oldX);
oldX = x;
display();///重畫畫面
}
void mouse(int button, int state, int x, int y)
{
oldX = x;//定錨
}
int main(int argc, char**argv)
{
glutInit( &argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04 Rotate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);///上週教:mouse按下去 放開來
glutMainLoop();
}
沒有留言:
張貼留言