#include <stdio.h>//讓你的 printf 可以運作
float teapotX=0, teapotY=0;
float angleX=0, startX=0;
float angleY=0, startY=0;
void keyboard(unsigned char key, int x, int y)
{
printf("%c %d %d\n", key, x, y); //測試一下我們是按什麼鍵,在哪裡按
//但是要小心,在 dev c++ 的專案設定 (ALT+P) 要設成 Console 小黑主控台
if(key=='a') teapotX+=0.1;
if(key=='b') teapotX-=0.1;
if(key=='c') teapotY+=0.1;
if(key=='d') teapotY-=0.1;
glutPostRedisplay();//小心,要畫面重製才會更新!!!
}
void mouse(int button, int state, int x, int y)
{//這個 mouse 函示,只有在按下去/彈起來 (Press/Release) 才會被用到 (開始/結束)
printf("%d %d %d %d\n",button, state, x, y);
startX=x;
startY=y;
}
void motion(int x, int y)
{//但是中間的移動動作 (motion) 是在這裡, 把所有的 x,y 座標拿來用
printf("%d %d\n", x, y);
angleX+= (x-startX);
angleY+= (y-startY);
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glRotatef(angleX, 0,-1,0);
glRotatef(angleY, -1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("01160803");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);//new!!!!!!!
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
這次程式碼多了鍵盤和滑鼠的功能
可以用鍵盤上的按鍵和滑鼠
讓茶壺可以上下左右移動
更可以像MAYA一樣旋轉

沒有留言:
張貼留言