2015年3月25日 星期三

Week05 課堂作業, 許志遙

畫一個正方形+轉動
#include <GL/glut.h>
float angle=0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT);       //清畫面

    glPushMatrix();                     //備份
        glRotatef(angle,0,1,0);         //旋轉
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,0.8);    //x,y,z
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);
        glEnd();
    glPopMatrix();                      //還原

    angle +=0.01;                       //選轉角度
    glFlush();
}

int main(int argc, char **argv){
        glutInit(&argc, argv);
        glutCreateWindow("02160163 go fight");
        glutDisplayFunc(display);
        glutIdleFunc(display);
        glutMainLoop();
}



連接兩個正方形+轉動
#include <GL/glut.h>
float angle=0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT);       //清畫面

    glPushMatrix();                     //備份
        glRotatef(angle,1,1,0);         //旋轉
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);           //紅
            glVertex3f(0.1,0.1,0.8);    //x,y,z
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);
        glEnd();

        glBegin(GL_POLYGON);
            glColor3f(1,0,0);            //紅
            glVertex3f(0.1,0.1,-0.8);    //x,y,z
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();

        glBegin(GL_QUAD_STRIP);         //點點接起來
            glColor3f(0,1,0);           //綠
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(0.1,0.1,-0.8);
            glColor3f(0,0,1);           //藍
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glColor3f(0,0,1);           //藍
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glColor3f(0,1,0);           //綠
            glVertex3f(0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();

    glPopMatrix();                      //還原

    angle +=0.01;                       //選轉角度
    glFlush();
}

int main(int argc, char **argv){
        glutInit(&argc, argv);
        glutCreateWindow("02160163 go fight");
        glutDisplayFunc(display);
        glutIdleFunc(display);
        glutMainLoop();
}



滑鼠轉動畫面

#include <GL/glut.h>
float angle=0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT);       //清畫面

    glPushMatrix();                     //備份
        glRotatef(angle,1,1,0);         //旋轉
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);           //紅
            glVertex3f(0.1,0.1,0.8);    //x,y,z
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);
        glEnd();

        glBegin(GL_POLYGON);
            glColor3f(1,0,0);            //紅
            glVertex3f(0.1,0.1,-0.8);    //x,y,z
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();

        glBegin(GL_QUAD_STRIP);         //點點接起來
            glColor3f(0,1,0);           //綠
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(0.1,0.1,-0.8);
            glColor3f(0,0,1);           //藍
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glColor3f(0,0,1);           //藍
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glColor3f(0,1,0);           //綠
            glVertex3f(0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();

    glPopMatrix();                      //還原

    //angle +=0.01;                       //自動選轉角度,有手動就能關掉
    glFlush();
}

int oldX=0;
void motion(int x,int y){              //x,y座標==滑鼠座標
    angle+=(x-oldX);
    oldX=x;
}

int main(int argc, char **argv){
        glutInit(&argc, argv);
        glutCreateWindow("02160163 go fight");
        glutDisplayFunc(display);
        glutIdleFunc(display);
        glutMotionFunc(motion);   //mouse motion drag動作移動函式
        glutMainLoop();
}





沒有留言:

張貼留言