2017年10月19日 星期四

黃筱婷計圖筆記week06

1.在week05檔案中加入
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///3D深度測試,清depth buffer才能正確繪圖
    drawmodel();
    glutSwapBuffers();
}
 GLfloat pos[]= {0.0,0.0,-1.0,0.0};///打光
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///3D深度測試,有準備好depth buffer記憶體
    glutCreateWindow("week05");

    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);3D深度測試,打開Enable
    glEnable(GL_LIGHTING);///打光,打開Enable
    glEnable(GL_LIGHT0);///打光,打開Enable
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glutMainLoop();
}


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)
{

}

glutMouseFunc(mouse);///41
glutKeyboardFunc(keyboard);///42


3.keyboard讓他轉動
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);
    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);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(rotateX,1,0,0);
    glRotatef(rotateY,0,1,0);
    drawmodel();
    glutSwapBuffers();
}

4.用mouse讓他轉
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)
{
    rotateY += -(x-oldX);
    rotateX += -(y-oldY);
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
main裡
glutKeyboardFunc(keyboard);///有mouse motion (dragging)


沒有留言:

張貼留言