2015年3月26日 星期四

02160660_楊涵雯_Week05

1.自製3D模型
2.利用mouse旋轉
3.回家作業


複習上禮拜的旋轉甜甜圈


glVertex2f()畫4邊形



glVertex3f()畫空心4邊形

glBegin(GL_LINE_LOOP);
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2, 0.2, 0.8);
        glEnd();



glVertex3f() 再畫實心4邊形

glBegin(GL_POLYGON);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();


點跟點連起來變成長方體
glBegin(GL_QUAD_STRIP);
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2, 0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();

把長方形變成有顏色(紅底+黑邊)
glColor3f(1,0,0);                                      //紅色
        glBegin(GL_POLYGON);
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2, 0.2, 0.8);
        glEnd();
        glBegin(GL_POLYGON);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();
        glBegin(GL_QUAD_STRIP);              //面
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2, 0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();

        glColor3f(0,0,0);                                 //黑色
        glBegin(GL_LINE_LOOP);
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2, 0.2, 0.8);
        glEnd();
        glBegin(GL_LINE_LOOP);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();
        glBegin(GL_LINES);                            //線
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2, 0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();


用滑鼠來轉動長方體

#include  <GL/glut.h>
#include <stdio.h>
float angle=0,angle2=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotated(angle,0,1,0);        //繞Y軸轉
        glRotated(angle2,1,0,0);      //繞X軸轉

        glColor3f(1,0,0);//紅色
        glBegin(GL_POLYGON);
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2, 0.2, 0.8);
        glEnd();
        glBegin(GL_POLYGON);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();
        glBegin(GL_QUAD_STRIP);//面
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2, 0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();

        glColor3f(0,0,0);//黑色
        glBegin(GL_LINE_LOOP);
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2, 0.2, 0.8);
        glEnd();
        glBegin(GL_LINE_LOOP);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();
        glBegin(GL_LINES);//線
            glVertex3f( 0.2, 0.2, 0.8);
            glVertex3f( 0.2, 0.2,-0.8);
            glVertex3f( 0.2,-0.2, 0.8);
            glVertex3f( 0.2,-0.2,-0.8);
            glVertex3f(-0.2,-0.2, 0.8);
            glVertex3f(-0.2,-0.2,-0.8);
            glVertex3f(-0.2, 0.2, 0.8);
            glVertex3f(-0.2, 0.2,-0.8);
        glEnd();
    glPopMatrix();
    glFlush();
    //angle+=0.01;
}
void motion(int x,int y)
{
    printf("%d %d\n",x,y);
    angle=x;
    angle2=y;
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);                                  //main的參數
    glutCreateWindow("02160660楊涵雯");   //建立一個小視窗
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();                                        //不讓程式結束
}



讓長方體變不是透明的



沒有留言:

張貼留言