2017年3月14日 星期二

Week04 中澤的學習札記

一、HW01:  作業互評


(1)我覺得的3個美圖^0^!!!~~



可愛的大嘴鳥

 

童年的無面男


 


奇幻的幾何圖形



二、HW02:  畫正圓形

打開 OPEN glut

程式碼:
#include <GL/glut.h>
#include<math.h>
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();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04162710 Number 1");
    glutDisplayFunc(display);
    glutMainLoop();
    return EXIT_SUCCESS;
}
圖:

 
 

三、HW03:  加上Mouse函式

 (1)基本變色 滑鼠點擊

 
程式碼:
 
#include <GL/glut.h>
#include<math.h>
#include<stdio.h>///Now for printf()
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();
}
void mouse(int button, int state, int x, int y)
{///Now mouse function
    printf("%d %d %d %d\n", button, state, x, y);///Now print the values
    glColor3f(1, x/300.0, y/300.0);///Now change color to any color
    glutPostRedisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04162710 Number 1");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);///Now we are using mouse function
    ///glutMotionFunc(motion);
    glutMainLoop();
    return EXIT_SUCCESS;
}
圖:
 
 
 
 

(2)加上滑鼠拖曳:   點擊圖畫後 按住滑鼠拖曳 使其改變顏色



程式碼:

#include <GL/glut.h>
#include<math.h>
#include<stdio.h>///Now for printf()
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();
}
void mouse(int button, int state, int x, int y)
{///Now mouse function
    printf("%d %d %d %d\n", button, state, x, y);///Now print the values
    glColor3f(1, x/300.0, y/300.0);///Now change color to any color
    glutPostRedisplay();
}
void motion(int x, int y)
{///Now mouse Gragging!!!!!
    glColor3f(1, x/300.0, y/300.0);///Now change color ti any color
    glutPostRedisplay();///Post 貼個便利貼,告訴電腦,有空的時候 Re-display
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04162710 Number 1");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);///Now we are using mouse function
    glutMotionFunc(motion);
    glutMainLoop();
    return EXIT_SUCCESS;
}

(3)最終程式 畫書小精靈


程式碼:

#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 print the values
    ///glColor3f(1, x/300.0, y/300.0);///Now change color to any color
    ///glutPostRedisplay();
}
void motion(int x, int y)
{///Now mouse Gragging!!!!!
    mouth = x / 300.0;
    glColor3f(1, x/300.0, y/300.0);///Now change color ti any color
    glutPostRedisplay();///Post 貼個便利貼,告訴電腦,有空的時候 Re-display
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04162710 Number 1");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);///Now we are using mouse function
    glutMotionFunc(motion);
    glutMainLoop();
}
圖:



 

恭喜完成本次課堂練習!~~~^0^

沒有留言:

張貼留言