2017年10月19日 星期四

吳映廷 計算機圖學 week6

1.編譯上次的程式之後,在main加入GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };
    在glutCreateWindow("week05");和glutMainLoop();間加入以下程式碼
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_DEPTH_TEST);
執行後就可以看到有打光的人

2.在程式的開頭加入#include <stdio.h>
然後在那之後加入程式碼
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);和glutMainLoop();間加入
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
執行之後按鍵盤可以看到按的按鍵和滑鼠現在的位置
3.
在void mouse裡面加入 printf("button:%d state:%d (%d %d)\n", button, state, x, y);
按滑鼠按鍵可以印出按下的按鍵和滑鼠位置
之後在void keyboard內加入
if(key=='1') rotateX++;
    if(key=='2') rotateY++;
    if(key=='3') rotateZ++;
    glutPostRedisplay();
然後修改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);
    drawmodel();
    glPopMatrix();
    glutSwapBuffers();
這樣執行時按1,2,3鍵就可以旋轉模組

4.在main加入glutMotionFunc(motion);
修改mouse和motion

void mouse(int button, int state, int x, int y)
{
    printf("button:%d state:%d (%d %d)\n", button, state, x, y);
    if(state==GLUT_DOWN)
    {
        oldX=x, oldY=y;
    }
}
void motion(int x, int y)
{
    rotateX += -(x-oldX);
    rotateY += -(y-oldY);
    oldX=x, oldY=y;
    glutPostRedisplay();
}
這樣執行就可以用滑鼠旋轉模組了

沒有留言:

張貼留言