2015年3月25日 星期三

week05 02160200 自己3D模型 MOUSE轉動

TODO:先畫號3D方塊
TODO: 畫面自動轉    glutIdleFunc(display)
TODO:用glBegin(GL_POLYGON);及glBegin(GL_QUAD_STRIP);畫長條形
TODO:glutMotionFunc(motion);///mouse的motion來轉動


程式碼
#include <GL/glut.h>

float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
                glPushMatrix();///備份Matrix
                glRotatef(angle,0,1,0.5);///旋轉
glBegin(GL_POLYGON);///正方形
    glColor3f(1,1,0);///(r,g,b)
    glVertex3f(0.1,0.1,0.8);///頂點
    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);
    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);///長條型
    glVertex3f(0.1,0.1,0.8);
     glVertex3f(0.1,0.1,-0.8);
    glVertex3f(-0.1,0.1,0.8);
     glVertex3f(-0.1,0.1,-0.8);
    glVertex3f(-0.1,-0.1,0.8);
    glVertex3f(-0.1,-0.1,-0.8);
    glVertex3f(0.1,-0.1,0.8);
    glVertex3f(0.1,-0.1,-0.8);
glEnd();
                glPopMatrix();///還原Matrix
                angle+=0.01;
glFlush();
}
int oldX=0;
void motion(int x,int y)
{
    angle+=(x-oldX);
    oldX=x;
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160200");
                glutIdleFunc(display);
    glutDisplayFunc(display);

    glutMotionFunc(motion);///mouse motion drag動作移動距離的函式
    glutMainLoop();
}

沒有留言:

張貼留言