課本範例
至網址 https://jsyeh.org/3dcg10/
下載
data data.zip
win32 windows.zip
glut32.dill
解壓縮
-windows.zip => 下載/解壓縮至windows
-data.zip => 不用解壓縮,點開,裡面的data資
料夾直接拉到剛剛解壓縮的
windows資料夾
translate實作
1.glPushMatrix() 備份矩陣
glPopMatrix() 還原矩陣
因為如果沒有上面這兩個,茶壺移動後會跑到不知所蹤,所以需要用這兩個程式固定
2. mouse函式來幫忙
#include <GL/glut.h>
float X = 0, Y = 0; ///global變數,在函式之間傳值(大寫的)
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清背景
glPopMatrix(); ///備份矩陣
glTranslatef(X,Y,0);
glutSolidTeapot(0.3);
glPopMatrix(); ///還原矩陣
glutSwapBuffers();
}
///#include <stdio.h>
void mouse(int button, int state, int x, int y) ///小寫的
{
X = (x - 150) / 150.0;
Y = -(y - 150) / 150.0; ///y記得要負號
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week03");
glutDisplayFunc(display);
glutMouseFunc(mouse); ///請mouse函式幫忙
glutMainLoop();
}
沒有留言:
張貼留言