下載week05的專案並執行,結果只有白色的輪廓需要加入光源並開啟深度,程式碼如下:
GLfloat pos[] = {0.0, 0.0, -1.0, 0.0}; // 光點位置int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week05");
glEnable(GL_DEPTH_TEST); // 開啟深度
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos); // 打光
glutDisplayFunc(display);
glutMainLoop();
}
接下來鍵盤和滑鼠控制,程式碼如下:
void keybord(unsigned char key, int x, int y)
{
if(key == '1') rotateX++;
if(key == '2') rotateY++;
if(key == '3') rotateZ++; //按123分別轉動XYZ軸
glutPostRedisplay();
}
void mouse(int buttom, int state, int x, int y)
{
if(state == GLUT_DOWN)
{
oldX = x; oldY = y; 按下滑鼠時記錄當下滑鼠座標
}
}
void motion(int x, int y)
{
rotateX += (y-oldY);
rotateY += (x-oldX);
oldX = x; oldY = y; // 按下滑鼠移動時轉動XY軸
glutPostRedisplay();
}
double rotateX = 0, rotateY = 0, rotateZ = 0; //全域變數
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotateX, 1, 0, 0); //X軸旋轉rotateX度
glRotatef(rotateY, 0, 1, 0); //Y軸旋轉rotateY度
glRotatef(rotateZ, 0, 0, 1); //Z軸旋轉rotateZ度
drawmodel();
glPopMatrix();
glutSwapBuffers();
}
另外在main裡面需要開啟滑鼠和鍵盤的監聽,加入以下程式碼:
glutKeyboardFunc(keybord); // 鍵盤監聽glutMouseFunc(mouse); // 滑鼠按鍵監聽
glutMotionFunc(motion); // 滑鼠移動監聽
沒有留言:
張貼留言