2017年3月14日 星期二

Week 04 彣禎的上課筆記

1.作業互評.評分

 挑出3個最好的作品上傳Moodle
 

 

2.用GLUT方案畫正圓

                                                           static void display(void)
                                                           {
                                                                    glBegin(GL_POLYGON);
                                                                    for(float angle=0;angle<=3.1415926*2;angle+=0.01)
                                                                   {
                                                                            glVertex2f(cos(angle),sin(angle));
                                                                    }
                                                                    glEnd();
                                                                    glutSwapBuffers();
                                                             }

3.加上Mouse函式

void mouse(int button,int state,int x,int y)
{
    printf("%d %d %d %d\n",button,state,x,y);
   
}
(1)在圓上點一下 會有兩排數值出現
 
(2)再多加 glColor3f(1,x/300.0,y/300.0);
在圓上點一下會出現顏色
 
(3)別的地方再點一次會變色
 
 

4.加上Motion函式

多加void motion(int x,int y)
{
    glColor3f(1,x/300.0,y/300.0);
    glutPostRedisplay();
}
可以用滑鼠按著拖移來變色
 

5.會動的小精靈嘴巴(用滑鼠拖移)

#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
float mouth=0;
void display()
{
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   glBegin(GL_TRIANGLE_FAN);
   glVertex2f(0,0);
   for(float angle=0+mouth;angle<=3.1415926*2-mouth;angle+=3.1415926/100)
   {
       glVertex2f(cos(angle),sin(angle));
   }
    glEnd();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
///NOW mouse function   

     //printf("%d %d %d %d\n",button,state,x,y);
    //glColor3f(1,x/300.0,y/300.0);

}
void motion(int x,int y)
///NOW mouse Dragging(拖移)!!!    mouth=x/300.0;
    glColor3f(1,x/300.0,y/300.0);  ///NOW change color to any color   

    glutPostRedisplay();  ///Post 貼個便利貼,告訴電腦,有空的時候 Re-display
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);  ///Now we are using mouse function
    glutMotionFunc(motion);
    glutMainLoop();
}

 
 
 
 

沒有留言:

張貼留言