1.移動
Translate 移動
glPushMatrix(); //備份矩陣
glTranslatef(x,y,z); //移動
glPopMatrix(); //還原矩陣,才不會有殘值
glPushMatrix(); //備份矩陣
glTranslatef(x,y,z); //移動
glPopMatrix(); //還原矩陣,才不會有殘值
2.範例:glTranslatef(x,y,z)
執行 Transformation.exe 檔案
glTranslatef(x,y,z); //移動
x:左右 ,y:上下 ,z:前後
glRotatef(0.0,0.0,0.0,0.0); //旋轉
glScalef(x,y,z); //縮放
開啟GLUT程式 (week01
進入transmission zero 網站,下載"freeglut 3.0.0 MinGW Package"在 CodeBlocks 開啟GLUTproject
寫移動程式:
#include <GL/glut.h>
void display(){
glPushMatrix(); //備份矩陣
glTranslatef( 0.3, 0.3, 0); //移動
glutSolidTeapot(0.3);
glPopMatrix(); //還原矩陣,才不會有殘值
glutSwapBuffers();
}
int main(int argc,char**argv){
...
}
-
3.主題:滑鼠
float x=0,y=0; ///滑鼠motion
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glPushMatrix(); //備份矩陣
glTranslatef( (x-150)/150.0, -(y-150)/150.0, 0); ///滑鼠motion,移動
glutSolidTeapot(0.3);
glPopMatrix(); //還原矩陣,才不會有殘值
glutSwapBuffers();
}
void motion(int nowX,int nowY){ ///滑鼠motion
x=nowX; y=nowY; ///滑鼠motion,座標
display(); ///滑鼠motion,馬上更新
}
int main(int argc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMotionFunc(motion); ///滑鼠motion
glutMainLoop();
}
沒有留言:
張貼留言