2017年10月19日 星期四

田園 計圖筆記 Week06

week05複習
一、開啟Lighting打光
1.到jsyeh.org/3dcg10下載source.zip內有transformation.c範例
search : light
glutCreateWindow("Transformation");
//
glEnable(GL_LIGHTING);
glEnable(GL_LIGHTQ);
glLightfv(GL_LIGHTQ,GL_POSITION,pos);
glEnable(GL_DEPTH_TEST);
//
glutMainLoop();
2.打開week05的專案檔,加入一行程式碼GLfloat pos[]={0.0,0.0,-1.0,0.0};在主程式前
 3.加入
glEnable(GL_LIGHTING);
glEnable(GL_LIGHTQ);
glLightfv(GL_LIGHTQ,GL_POSITION,pos);
glEnable(GL_DEPTH_TEST);
4.加入keyboard, mouse,motion
int rotateX=0,rotateY=0,rotateZ=0;
int oldx=0,oldy=0;


void keyboard (unsigned char key , int x , int y)
{
     printf("now : %c ( %d  %d)\n " ,key , x , y );
     if(key=='x')rotateX++;
     if(key=='y')rotateY++;
     if(key=='z')rotateZ++;
     glutPostRedisplay();  //貼出公告,請電腦有空時,重新展示畫面
}

void mouse ( int button , int state , int x , int y)
{  
   printf("button : %d  state: %d ( %d %d )\n ",button,state,x,y);
      //mouse drag (1) down (2) drag (3) up
   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();

}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    //3D深度測試,清 depth buffer,才能繪圖

    glPushMatrix();
    glRotatef(rotateX,1,0,0);
    glRotatef(rotateY,0,1,0);
    glRotatef(rotateZ,0,0,1);

    drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); 
     //3D深度測試,有準備好的depth buffer,才能繪圖
    glutCreateWindow("week05");

    glutDisplayFunc(display);

    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    
    
    glEnable(GL_LIGHTING);              //偷來的3D深度測試,打開Enable
    glEnable(GL_LIGHT0);                   //偷來的打光(2) ,打開Enable
    glEnable(GL_DEPTH_TEST);        //偷來的打光(1) ,打開Enable
    glLightfv(GL_LIGHT0,GL_POSITION,pos);

    glutMainLoop();
}
5.執行 就可以用滑鼠 鍵盤控制旋轉


沒有留言:

張貼留言