2017年3月14日 星期二

Week04

Week04

(ㄧ)畫出正圓形


程式碼:

#include <GL/glut.h>
#include <math.h>
void display()
{
    glBegin(GL_POLYGON);
    for(float angle=0; angle <= 3.1415926 *2; angle += 0.01){
        glVertex2f( cos(angle), sin(angle) );
    }
    glEnd();
    glutSwapBuffers();
}
int main(int argc, char*argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB |GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("GLUT shape");//視窗名稱
    glutDisplayFunc(display);
    glutMainLoop();

}

(二)圓形變色

程式碼:

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>//NOW for printf()
void display()
{
    glBegin(GL_POLYGON);
    for(float angle=0; angle <= 3.1415926 *2; angle += 0.01){
        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);//NOW printf the values
    glColor3f( 1, x/300.0 , y/300.0 );//NOW change color to any color
}
int main(int argc, char*argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB |GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("GLUT shape");//視窗名稱
    glutDisplayFunc(display);//NOW we are using mouse function
    glutMouseFunc(mouse);
    glutMainLoop();

}




(三) 滑鼠拖曳 讓圓形變色



程式碼:

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>//NOW for printf()
void display()
{
    glBegin(GL_POLYGON);
    for(float angle=0; angle <= 3.1415926 *2; angle += 0.01){
        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);//NOW printf the values
}
void motion(int x, int y)
{///NOW mouse Dragging!
      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 shape");//視窗名稱
    glutDisplayFunc(display);//NOW we are using mouse function
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();

}

(四)小精靈


程式碼:

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>//NOW for printf()
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);//NOW printf the values
}
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 shape");//視窗名稱
    glutDisplayFunc(display);//NOW we are using mouse function
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();

}









沒有留言:

張貼留言