GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, pos);

接著加入鍵盤與滑鼠功能
先加入這2個函式
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)
{
}
接著在 glutDisplayFunc(display);這行下加入
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
這樣使用鍵盤時 便能印出位置

加入 這幾行 使他可以案1往x軸轉 案2往y軸轉 案3往z軸轉
if(key=='1') rotateX++;
if(key=='2') rotateY++;
if(key=='3') rotateZ++;
glPushMatrix();
glRotatef(rotateX,1,0,0);
glRotatef(rotateY,0,1,0);
glRotatef(rotateZ,0,0,1);
drawmodel();
glPopMatrix();

加入這幾行 可以用滑鼠旋轉
void mouse(int button,int state,int x,int y)
{
printf("%d %d",x,y);
if(state==GLUT_DOWN)
{
oldX=x;
oldY=y;
}
}
void motion(int x,int y)
{
rotateY += -(x-oldX);
rotateX +=-(y-oldY);
oldX=x;
oldY=y;
glutPostRedisplay();
}
glutMotionFunc(motion);
沒有留言:
張貼留言