2015年3月25日 星期三

2015/3/25 week5 課堂作業 黃志楷

畫3D圖形

#include<GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);///清畫面
    glBegin(GL_POLYGON);///開始畫圖(POLYGON)
        glColor3f(1,0,0);///上色
        glVertex3f(0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,0.8);///畫點(3D)
    glEnd();///結束畫圖
    glFlush();

}
int main(int argc,char *argv[])
{
    glutInit(&argc,argv);///初始化
    glutCreateWindow("02160421");///畫視窗
    glutReshapeWindow(500,500);///改變視窗大小
    glutDisplayFunc(display);///呼叫函式
    glutMainLoop();///持續跑主程式
}

畫3D圖形並旋轉

#include<GL/glut.h>
float a=0;///宣告a
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);///清畫面
    glPushMatrix();///備份matrix
    glRotated(a,1,0,0);///旋轉設定(對x軸)
    glBegin(GL_POLYGON);///開始畫圖(POLYGON)
        glColor3f(1,0,0);///上色
        glVertex3f(0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,0.8);///畫點(3D)
    glEnd();///結束畫圖
    glPopMatrix();///還原matrix
    a+=0.01;///增加角度
    glFlush();

}
int main(int argc,char *argv[])
{
    glutInit(&argc,argv);///初始化
    glutCreateWindow("02160421");///畫視窗
    glutReshapeWindow(500,500);///改變視窗大小
    glutDisplayFunc(display);///呼叫函式
    glutIdleFunc(display);///閒閒沒事時呼叫display
    glutMainLoop();///持續跑主程式
}

畫立方體並旋轉
增加glBegin(GL_QUAD_STRIP);///連線


#include<GL/glut.h>
float a=0;///宣告a
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);///清畫面
    glPushMatrix();///備份matrix
    glRotated(a,1,0.5,0);///旋轉設定(對x軸)
    glBegin(GL_POLYGON);///開始畫圖(POLYGON)
        glColor3f(1,0,0);///上色
        glVertex3f(0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,0.8);///畫點(3D)
    glEnd();///結束畫圖
    glBegin(GL_POLYGON);///開始畫圖(POLYGON)
        glColor3f(1,1,0);///上色
        glVertex3f(0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,-0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,-0.8);///畫點(3D)
    glEnd();///結束畫圖
    glBegin(GL_QUAD_STRIP);///開始畫圖(GL_QUAD_STRIP)
        glColor3f(1,1,1);///上色
        glVertex3f(0.1,0.1,0.8);///畫點(3D)
        glVertex3f(0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,-0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,-0.8);///畫點(3D)
    glEnd();///結束畫圖
    glPopMatrix();///還原matrix
    a+=0.05;///增加角度
    glFlush();

}
int main(int argc,char *argv[])
{
    glutInit(&argc,argv);///初始化
    glutCreateWindow("02160421");///畫視窗
    glutReshapeWindow(500,500);///改變視窗大小
    glutDisplayFunc(display);///呼叫函式
    glutIdleFunc(display);///閒閒沒事時呼叫display
    glutMainLoop();///持續跑主程式
}


畫出立方體並用滑鼠旋轉

增加  滑鼠控制
void motion(int x,int y)
{
    a+=(x-oldx);
    oldx=x;
}



#include<GL/glut.h>
float a=0;///宣告a
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);///清畫面
    glPushMatrix();///備份matrix
    glRotated(a,1,0.5,0);///旋轉設定(對x軸)
    glBegin(GL_POLYGON);///開始畫圖(POLYGON)
        glColor3f(1,0,0);///上色
        glVertex3f(0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,0.8);///畫點(3D)
    glEnd();///結束畫圖
    glBegin(GL_POLYGON);///開始畫圖(POLYGON)
        glColor3f(1,1,0);///上色
        glVertex3f(0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,-0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,-0.8);///畫點(3D)
    glEnd();///結束畫圖
    glBegin(GL_QUAD_STRIP);///開始畫圖(POLYGON)
        glColor3f(1,1,1);///上色
        glVertex3f(0.1,0.1,0.8);///畫點(3D)
        glVertex3f(0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,0.1,-0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(-0.1,-0.1,-0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,0.8);///畫點(3D)
        glVertex3f(0.1,-0.1,-0.8);///畫點(3D)
    glEnd();///結束畫圖
    glPopMatrix();///還原matrix
    a+=0.00;///增加角度
    glFlush();

}
int oldx=0;///宣告
void motion(int x,int y)
{
    a+=(x-oldx);
    oldx=x;
}
int main(int argc,char *argv[])
{
    glutInit(&argc,argv);///初始化
    glutCreateWindow("02160421");///畫視窗
    glutReshapeWindow(500,500);///改變視窗大小
    glutDisplayFunc(display);///呼叫函式
    glutIdleFunc(display);///閒閒沒事時呼叫display
    glutMotionFunc(motion);///呼叫函式
    glutMainLoop();///持續跑主程式
}




沒有留言:

張貼留言