1. 打開上次的week05,並加上以下幾行 在程式中:
GLfloat pos[]={0.0, 0.0, -1.0, 0.0}; //偷來的打光(3)
glEnable(GL_DEPTH_TEST); //偷來的3D深度測試,打開Enable
glEnable(GL_LIGHTING); //偷來的打光(2),打開Enable
glEnable(GL_LIGHT0); //偷來的打光(1),打開Enable
glLightfv(GL_LIGHT0, GL_POSITION, pos);

2.並執行:

二、寫回家作業(keyboard、Mouse、轉動、移動):
1.加入keyboard、Mouse 函式讓他可以印滑鼠所在座標:
void keyboard(unsigned char key, int x, int y)
{
printf("now %c (%d,%d)\n", key, x, y);
}
void mouse(int button, int state, int x, int y)
{
}
int main(...)
{
.
.
.
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
}
2. 並且執行,滑鼠一直在畫面移動,並按鍵盤:

3.鍵盤讓他轉動:
int rotateX, rotateY, rotateZ; //旋轉軸
void keyboard(unsigned char key, int x, int y)
{
printf("now %c (%d,%d)\n", key, x, y);
if(key=='1')rotateX++; //控制旋轉方向
else if(key=='2')rotateY++;
else if(key=='3')rotateZ++;
glutPostRedisplay(); //把畫面重新re-display
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotateX, 1, 0, 0);
glRotatef(rotateY, 0, 1, 0);
glRotatef(rotateZ, 0, 0, 1);
drawmodel();
glPopMatrix();
glutSwapBuffers();
}

4.滑鼠讓他轉動:
int OldX=0, OldY=0; //舊座標
void mouse(int button, int state, int x, int y)
{
printf("button: %d state: %d (%d %d)\n", button, state, x, y);
if(state==GLUT_DOWN) //點下滑鼠
{
OldX=x, OldY=y; //
}
}
void motion(int x, int y)
{
rotateX+=-(y-OldY); //X方向轉動
rotateY+=-(x-OldX); //Y 方向轉動
OldX=x, OldY=y; //更新座標
glutPostRedisplay();
}
glutMouseFunc(mouse);
glutMotionFunc(motion);

沒有留言:
張貼留言