2017年3月13日 星期一

Week03

week03_hw1

今天要教如何實作:glutMouseFunc();// gultMotionFunc();// 如何製作滑鼠動作(圓形/移動)
1. 主題:mouse interaction

step 1:由茶壺來修改



先打程式:
#include <GL/glut.h>
void display()
{
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}

int main(int argc, char*argv[])
{
    glutInit( &argc,argv);
    glutInitDisplayMode(GLUT_RGB| GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("0416273");

    glutDisplayFunc(display);
    // gultMouseFuc(mouse);
    // glutMotionFunc(motion);

    glutMainLoop();
}
step2:

void mouse(int button,int state,int x,int y) //use mouse
{
    printf("%d %d %d %d\n",button,state,x,y);// 測試mouse 正在做什麼
}



會產生四個數字 0 0 45 20   0 1 45 20
第一個0代表button 第二個0/1代表按下去/彈起來 45(x)/20(y)

step3:  
加入這兩行
 glColor3f(x/300.0,y/300.0,1); //讓mouse have color  RGB 必須要有三個參數
  glutPostOverlayRedisplay(); // output color


step4:

glutMotionFunc(motion); //兩個參數是因為按著滑鼠不動

glColor3f(x/300.0,y/300.0,1);  //300 代表視窗大小


  glutInitWindowSize(500,500); //小黑窗大小



step5:

記住你想要圖形的座標



step6:

void display()
{
    glBegin(GL_POLYGON);
    //glutSolidTeapot(0.3);

glVertex2f(-0.03,0.43);/* 打自己的座標
glVertex2f(-0.50,-0.29);
glVertex2f(0.43,-0.35);
glVertex2f(0.24,-0.04);
glVertex2f(-0.28,-0.02);
glVertex2f(-0.01,-0.32);
glVertex2f(-0.01,-0.58);
glVertex2f(-0.26,-0.45);
glVertex2f(0.24,-0.45);
glVertex2f(-0.25,0.49);*/
    glEnd();
    glutSwapBuffers();

}


step7:畫圓




void display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

glBegin(GL_POLYGON);

for(float angle=0 ; angle<3.1415926*2;angle+=0.01) // 修改
    {
        glVertex2f(cos(angle),sin(angle)); 圓 角度
    }
        glEnd();
        glutSwapBuffers();
}




沒有留言:

張貼留言