本週學習重點
1.複習上週進度
2.編譯上週Transformation程式(小心compiler設定 v.s. strdup() )
3.作業2 : Keyboard, Mouse, 轉動, 移動
4.主軸: 打光 Lighting
上課紀錄:
1.首先把Setting-Compiler設定的C++11選項取消掉 (和strdup() 衝突)
2.接著到老師網頁下來的source code的projection.c找和 light 相關的程式碼,複製貼上到上禮拜未完成的專案
總共有以下5行
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };
glEnable(GL_DEPTH_TEST); /// 此行是為了讓光線有錢後深淺的效果,否則會糊成一團
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
3.其中因為因為我們的程式相機的位置不一樣,要把GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };改成
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
然後試著執行看看
4.接下來試著練習看看作業的內容(keyboard和mouse等等函式)
int oldX, oldY;
int rotateX=0, rotateY=0, rotateZ=0;
double scale = 1.0;
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);
glScalef(scale, scale, scale);
drawmodel();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y){
static int d = 1, direction = 1;
if (key == 'q'){
direction = (direction== 1?-1:1);
printf("change direction: %d\n", direction);
}
if (key == '1')
rotateX += d*direction;
else if (key == '2')
rotateY += d*direction;
else if (key == '3')
rotateZ += d*direction;
if (key == '+' && d<10){
d++;
}
else if (key == '-' && d>1){
d--;
}
}
void mouse(int botton, int state, int x, int y){
if (botton == GLUT_LEFT_BUTTON){
if (state == GLUT_DOWN){
oldX = x;
oldY = y;
}
}
else if (botton == 3 && scale > 0){
scale -= 0.05;
}
else if (botton == 4 && scale < 3){
scale += 0.05;
}
printf("%d", botton);
}
void motion(int x, int y){
rotateX += (oldY-y);
rotateY += (oldX-x);
oldX = x;
oldY = y;
}
void idle(){
glutPostRedisplay();
}
記得要在main函式裡面註冊這些函式
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutIdleFunc(idle);



沒有留言:
張貼留言