下載week5.zip
從setting -> compiler 取消C++11 即可編譯打光Lighting
在main裡面glutMainloop();前加入程式碼
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
並在main之前加上
GLfloat pos[]={0.0,0.0,-1.0,0.0};
編譯執行結果
加入Keyboard,Mouse
#include<stdio.h>
int rotateX=0,rotateY=0,rotateZ=0;
void keyboard(unsigned char key,int x,int y)
{
printf("now: %c (%d %d)\n",key,x,y);
}
在main中加入
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
編譯執行結果
在void keyboard();中加入
if(key=='1')rotateX++;
if(key=='2')rotateY++;
if(key=='3')rotateZ++;
glutPostRedisplay();
加上
void mouse(int button,int state,int x,int y)
{
printf("button:%d state:%d (%d %d)\n",button,state,x,y);
}
在display();中加入
glPushMatrix();
glRotatef(rotateX,1,0,0);
glRotatef(rotateY,0,1,0);
drawmodel();
glPopMatrix();
即可用按鍵1,2,3對x,y,z軸做即時旋轉
Mouse motion
在mouse();中加入
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();
}
在main中加入
glutMotionFunc(motion);
編譯執行結果










沒有留言:
張貼留言