2017年10月19日 星期四

7Hao-計算機圖學-Week06

一、複習上週進度
1.下載Week05.rar, 編譯上週程式
strdup() 無法辨識的解決方法:CodeBlocks設定中, C++11的設定勾勾 取消





第六週主題: 打光Lighting
1.增加打光功能
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 }; ///光位置
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");

    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);///打開打光功能
    glEnable(GL_LIGHT0); ///第一盞燈
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glutMainLoop();
    glDisable(GL_LIGHTING); ///關閉打光功能
}


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)
{
    printf("(%d %d %d %d)",button,state,x,y);  
    ///滑鼠點擊可以顯示x,y座標 , state=0是點擊, state=1 是放開滑鼠
    ///button=0滑鼠左鍵,=1滑鼠滾輪中鍵,=2滑鼠右鍵,=3滾輪往上,=4滾輪往下
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");


    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glutMainLoop();
}


3.增加鍵盤控制旋轉功能
int rotateX=0,rotateY=0,rotateZ=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotated(rotateX,1,0,0);
    glRotated(rotateY,0,1,0);
    glRotated(rotateZ,0,0,1);
    drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
    printf("now:%c (%d %d)\n",key,x,y);
    if(key=='1')
        rotateX++;
    if(key=='2')
        rotateY++;
    if(key=='3')
        rotateZ++;
    glutPostRedisplay(); ///重新畫

}


4.增加滑鼠控制旋轉功能
int rotateX=0,rotateY=0,rotateZ=0;
int oldX=0,oldY=0;
void mouse(int button, int state, int x, int y)
{
    printf("(%d %d %d %d)\n",button,state,x,y);
    if(state==GLUT_DOWN)
        oldX=x,oldY=y;
}
void motion(int x,int y)
{
    rotateX+= -(y-oldY);
    rotateY+= -(x-oldX);
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");


    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glutMainLoop();
}

沒有留言:

張貼留言