顯示具有 01160536_洪健翔 標籤的文章。 顯示所有文章
顯示具有 01160536_洪健翔 標籤的文章。 顯示所有文章

2015年6月17日 星期三

Week15

一開始先把老師給檔案開啟
可以看到下面的畫面


為了讓關節可以旋轉
如是加入了一些程式
於是當我們按下1 ,2,3,4時 
就可以旋轉不同的部位。

#include <GL/glut.h>
float angle=0, oldX=0, oldY=0;
void drawBody(){
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
float angle1=0,angle2=0,angle3=0,angle4=0;
void display()
{
    { ///Lighting
    glColor3ub(166,132,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    //glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(angle1, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Right Upper Arm
        glPushMatrix();
            glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle2, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Right Lower Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(-angle3, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Left Upper Arm
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(-angle4, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void Keyboard(unsigned char key,int x, int y)
{
    if(key=='1') angle1++;
    if(key=='2') angle2++;
    if(key=='3') angle3++;
    if(key=='4') angle4++;
}

void motion(int x, int y)
{   angle -= y - oldY;
    oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(Keyboard);
    glutMainLoop();
}

下面就是旋轉的效果





2015年5月28日 星期四

week 14

今天要講 TRT 關節旋轉的程式碼

首先來一個正常的茶壺做準備囉~


然後先做一個 timer 讓他自動旋轉,並且把旋轉軸心設在手把上

最後正式把TRT完成拉~ 自己畫的註解不好看
就借一下小葉老師的圖片拉~~~
畫得很不錯呢!


最後附上自己的程式碼囉!!!



2015年5月17日 星期日

Week10

那個禮拜,因為有事情所以沒到學校,所以在家自己看著做做看囉~

原來這次是 OpenCV 的下載安裝教學 和 貼圖的應用囉!!!
這下面就是安裝過程的詳細作法 和設定囉!!!
















至於後面的貼圖使用的部分 由於我有上11周的課
更詳盡的內容就在那裏了!!!
這裡就簡單放幾張完成的畫面










2015年5月7日 星期四

Week 11


製作出一個利用Timer自動旋轉的茶壺

#include <GL/glut.h>
float angle=0;


void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glFlush();
}
void time(int t)
{
    glutTimerFunc(10 ,time,0);
    angle+=1;
    glutPostRedisplay();
}
int main (int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutTimerFunc(10,time,0);
    glutMainLoop();
}


#include <GL/glut.h> ///使用 GLUT 3D 外掛
#include <opencv/highgui.h> ///要使用 OpenCV的 高級High圖形介面GUI
#include <opencv/cv.h> ///為了 cvCvtColor()而加上 cv.h
float angle=0; ///
void display()
{
    glEnable(GL_DEPTH_TEST); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glPushMatrix(); 
        glRotatef(angle, 0,1,0);  
        glutSolidTeapot(0.3); 
    glPopMatrix();
    glFlush(); 
    angle+=0.1;  
}
void init()
{   IplImage * img = cvLoadImage("image.jpg"); 
    cvCvtColor(img, img, CV_BGR2RGB);
    glEnable(GL_TEXTURE_2D); 
    GLuint id; 
    glGenTextures(1, & id ); 
    glBindTexture(GL_TEXTURE_2D, id); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}  
int main(int argc, char**argv) 
{
    glutInit(&argc, argv); 
    glutCreateWindow("3D");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    init();

    glutMainLoop();
}


#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; 
GLUquadric * quad;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(90, 1,0,0);
        glRotatef(angle, 0,0,1);
        gluQuadricTexture(quad, 1);
        gluSphere(quad, 0.5, 30, 30);///glutSolidTeapot(0.3);
    glPopMatrix();
    glFlush();
    angle+=1;
}
GLuint id;
GLuint id2;
void mouse(int button,int state, int x,int y)
{
    if(state==GLUT_UP)glBindTexture(GL_TEXTURE_2D,id2);
    else if(state==GLUT_DOWN)glBindTexture(GL_TEXTURE_2D,id);
}

void myInit()
{
    quad = gluNewQuadric();
    IplImage * img = cvLoadImage("image.jpg"); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能

    glGenTextures(1, &id); /// 產生貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);

    IplImage * img2 = cvLoadImage("image2.jpg"); ///OpenCV讀圖
    cvCvtColor(img2,img2, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能

    glGenTextures(1, &id2); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id2); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img2->width, img2->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img2->imageData);



}   
void timer(int t)
{/// 1000 msec   50fps:20msec
    glutTimerFunc(20, timer, 0);
    glutPostRedisplay();
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);
    glutTimerFunc(0, timer, 0);
    glutMouseFunc(mouse);
    myInit(); 
    glutMainLoop();
}


2015年4月16日 星期四

Week 08

NeHe Lesson32.zip 解壓縮,玩射擊遊戲。 

Light Material(打光)、Texture(貼圖)的範例程式



接下來就是要試著做上面那個射擊遊戲囉~~~~

當滑鼠按下時 發出槍聲 並印出Shot!
void mouse (int button, int state, int x,int y)
{
    if(state== GLUT_DOWN){
        PlaySound("Shot.wav",NULL,SND_ASYNC);
        printf("Shot!\n");
    }
}
任意按一鍵 跳出全螢幕
void keyboard(unsigned char key,int x,int y)
{
    exit(0);
}

設立浮點數 作為物體座標
float potX=-1 ,potY=0;
修改display 畫出茶壺 並且讓他依照座標跑
void display()
{
    potX+=0.01;
    if(potX>1.1) potX=-1.1;

    glClearColor(0.5,0.5,0.5,0);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
        glTranslated(potX,potY,0);
        glutSolidTeapot(0.05);
    glPopMatrix();

    glFlush();
}
在main() 裡面增加
glutIdleFunc(display);
在系統閒暇時 就執行display的函數


如果點擊位置接近 則有音效跑出
float mouseX=2*x/1280.0 -1, mouseY=2*y/1024 -1;
    if( fabs(mouseX - potX)< 0.1 && fabs(mouseY - potY)<0.1){
        PlaySound("glass.wav",NULL, SND_ASYNC);
        printf("You got it!");
    }


#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
#include <math.h>

float potX=-1 ,potY=0;
void mouse (int button, int state, int x,int y)
{
    if(state== GLUT_DOWN){
        PlaySound("Shot.wav",NULL,SND_ASYNC);
        printf("Shot!\n");
        }
    float mouseX=2*x/1280.0 -1, mouseY=2*y/1024 -1;
    if( fabs(mouseX - potX)< 0.1 && fabs(mouseY - potY)<0.1){
        PlaySound("glass.wav",NULL, SND_ASYNC);
        printf("You got it!");
    }
}
void keyboard(unsigned char key,int x,int y)
{
    exit(0);
}

void display()
{
    potX+=0.01;
    if(potX>1.1) potX=-1.1;

    glClearColor(0.5,0.5,0.5,0);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
        glTranslated(potX,potY,0);
        glutSolidTeapot(0.05);
    glPopMatrix();

    glFlush();
}
int main(int argc,char **argv)
{

    glutInit(&argc,argv);
    glutCreateWindow("01160536");

    glutFullScreen();

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}




2015年4月9日 星期四

Week 07

  • 用基本的 printf 顯示出按下鍵盤確定有效果
  • 加入標頭檔 使音效可以使用
  • 在按下keyboard 觸發音效 及印出字串
#include <GL/glut.h>
#include <stdio.h>

#include <mmsystem.h>

void display()
{
    glutSolidTeapot(0.3);
    glFinish();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a')
    {
        PlaySound("rocket.wav",NULL,SND_ASYNC);
        printf("小心!!! 火箭來囉~~~\n");
    }

}
 int main(int argc,char** argv)
 {
     glutInit(&argc,argv);
     glutCreateWindow("hello");

     glutDisplayFunc(display);
     glutKeyboardFunc(keyboard);

     glutMainLoop();
 }


  • 利用音效檔做出 piano 的模擬器
#include <GL/glut.h>
#include <stdio.h>

#include <mmsystem.h>

void display()
{
    glutSolidTeapot(0.3);
    glFinish();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a')
    {
        PlaySound("rocket.wav",NULL,SND_ASYNC);
        printf("小心!!! 火箭來囉~~~\n");
    }
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("Sol.wav",NULL,SND_ASYNC);
    if(key=='6') PlaySound("La.wav",NULL,SND_ASYNC);
    if(key=='7') PlaySound("Si.wav",NULL,SND_ASYNC);

}
 int main(int argc,char** argv)
 {
     glutInit(&argc,argv);
     glutCreateWindow("hello");

     glutDisplayFunc(display);
     glutKeyboardFunc(keyboard);

     glutMainLoop();
 }



#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>

float teaY=0, rotX=0,rotY=0,oldX=0,oldY=0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(0,teaY,0);
    glRotated(rotX,1,0,0);
    glRotated(rotY,0,1,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glFinish();
}
void special (int key,int x,int y)
{
    if(key==GLUT_KEY_DOWN)
    {
        teaY-=0.1; printf("down\n");
    } else if(key==GLUT_KEY_UP)
    {
        teaY+=0.1; printf("up\n");
    }
    glutPostRedisplay();

}
void mouse (int button,int state,int x,int y)
{
    oldX=x; oldY=y;
    if(GLUT_LEFT_BUTTON==button && GLUT_DOWN==state) printf("left down\n");
    if(GLUT_RIGHT_BUTTON==button && GLUT_DOWN==state) printf("right down\n");
}
void motion(int x,int y)
{
    rotX += y-oldY;
    rotY += x-oldX;
    oldX=x ; oldY=y;
    glutPostRedisplay();
}

void keyboard(unsigned char key,int x,int y)
{
    if(key=='a')
    {
        PlaySound("rocket.wav",NULL,SND_ASYNC);
        printf("小心!!! 火箭來囉~~~\n");
    }
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("Sol.wav",NULL,SND_ASYNC);
    if(key=='6') PlaySound("La.wav",NULL,SND_ASYNC);
    if(key=='7') PlaySound("Si.wav",NULL,SND_ASYNC);

}
 int main(int argc,char** argv)
 {
     glutInit(&argc,argv);
     glutCreateWindow("hello");

     glutDisplayFunc(display);
     glutKeyboardFunc(keyboard);
     glutSpecialFunc(special);
     glutMouseFunc(mouse);
     glutMotionFunc(motion);

     glutMainLoop();
 }




2015年3月26日 星期四

week 04


重點!!! 要對應的
glBegin( ) && glEnd( )
glPushMatrix( )  && glPopMatrix( )

需手動移動視窗 則可以轉動內建圖形
ps:點擊畫面也可以轉動喔!


自動轉動的自繪3D圖形
#include<GL/glut.h>
float angle=0;
void display()
{
        glClear(GL_COLOR_BUFFER_BIT);


        glPushMatrix();
            glRotated(angle,0,1,0);
            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0.5);
                glVertex3f( 0.3,-0.3,0.5);
                glVertex3f(-0.3,-0.3,0.5);
                glVertex3f(-0.3, 0.3,0.5);
            glEnd();
        glPopMatrix();
        glFlush();

        angle+=0.01;

}
int main(int  argc, char** argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("01160536");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}



建立出立體轉動圖形
並且劃出邊線
#include<GL/glut.h>

float angle=0;
void display()
{
        glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glRotated(angle,0,1,0.6);
            //面的程式碼
            glColor3f(0,0,1);//面的顏色設定
            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0.5);
                glVertex3f( 0.3,-0.3,0.5);
                glVertex3f(-0.3,-0.3,0.5);
                glVertex3f(-0.3, 0.3,0.5);
            glEnd();

            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,-0.5);
                glVertex3f( 0.3,-0.3,-0.5);
                glVertex3f(-0.3,-0.3,-0.5);
                glVertex3f(-0.3, 0.3,-0.5);
            glEnd();

            glBegin(GL_QUAD_STRIP);
                glVertex3f( 0.3, 0.3,0.5);
                glVertex3f( 0.3, 0.3,-0.5);
                glVertex3f( 0.3,-0.3,0.5);
                glVertex3f( 0.3,-0.3,-0.5);
                glVertex3f(-0.3,-0.3,0.5);
                glVertex3f(-0.3,-0.3,-0.5);
                glVertex3f(-0.3, 0.3,0.5);
                glVertex3f(-0.3, 0.3,-0.5);
            glEnd();
            //面的程式碼
            glColor3f(0,0,0);//線的顏色設定
            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0.5);
                glVertex3f( 0.3,-0.3,0.5);
                glVertex3f(-0.3,-0.3,0.5);
                glVertex3f(-0.3, 0.3,0.5);
            glEnd();

            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,-0.5);
                glVertex3f( 0.3,-0.3,-0.5);
                glVertex3f(-0.3,-0.3,-0.5);
                glVertex3f(-0.3, 0.3,-0.5);
            glEnd();

            glBegin(GL_LINES);
                glVertex3f( 0.3, 0.3,0.5);
                glVertex3f( 0.3, 0.3,-0.5);
                glVertex3f( 0.3,-0.3,0.5);
                glVertex3f( 0.3,-0.3,-0.5);
                glVertex3f(-0.3,-0.3,0.5);
                glVertex3f(-0.3,-0.3,-0.5);
                glVertex3f(-0.3, 0.3,0.5);
                glVertex3f(-0.3, 0.3,-0.5);
            glEnd();

        glPopMatrix();
        glFlush();

        angle+=0.01;

}
int main(int  argc, char** argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("01160536");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}

減少一個頂點做出三角形
#include<GL/glut.h>

float angle=0;
void display()
{
        glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glRotated(angle,0,1,0.6);
            //面
            glColor3f(0,0,1);
            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0.1);

            glEnd();

            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

            glBegin(GL_QUAD_STRIP);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0);

            glEnd();
            //邊
            glColor3f(0,0,0);
            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0.1);

            glEnd();

            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

            glBegin(GL_LINES);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

        glPopMatrix();
        glFlush();

        angle+=0.01;

}
int main(int  argc, char** argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("01160536");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}

#include<GL/glut.h>
#include<stdio.h>
float angle=0,angle2=0;

void motion(int x,int y)
{
    printf("%d,%d\n",x,y);
    angle=x;
    angle2=y;
}

void display()
{
        glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glRotated(angle,0,1,0);
            glRotated(angle2,1,0,0);
            //面
            glColor3f(0,0,1);
            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0.1);

            glEnd();

            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

            glBegin(GL_QUAD_STRIP);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0);

            glEnd();
            //邊
            glColor3f(0,0,0);
            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0.1);

            glEnd();

            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

            glBegin(GL_LINES);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

        glPopMatrix();
        glFlush();

        //angle+=0.01;

}
int main(int  argc, char** argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("01160536");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    //glutMotionFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}





使用深度測試 讓圖形更符合立體感!!!
#include<GL/glut.h>
#include<stdio.h>
float angle=0,angle2=0;

void motion(int x,int y)
{
    printf("%d,%d\n",x,y);
    angle=x;
    angle2=y;
}

void display()
{
        glEnable(GL_DEPTH_TEST);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

            glPushMatrix();
            glRotated(angle,0,1,0);
            glRotated(angle2,1,0,0);
            //面
            glColor3f(0,0,1);
            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0.1);

            glEnd();

            glBegin(GL_POLYGON);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

            glBegin(GL_QUAD_STRIP);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0);

            glEnd();
            //邊
            glColor3f(0,0,0);
            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0.1);

            glEnd();

            glBegin(GL_LINE_LOOP);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

            glBegin(GL_LINES);
                glVertex3f( 0.3, 0.3,0.1);
                glVertex3f( 0.3, 0.3,0);
                glVertex3f( 0.3,-0.3,0.1);
                glVertex3f( 0.3,-0.3,0);
                glVertex3f(-0.3,-0.3,0.1);
                glVertex3f(-0.3,-0.3,0);

            glEnd();

        glPopMatrix();
        glFlush();

        //angle+=0.01;

}
int main(int  argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DEPTH);
    glutCreateWindow("01160536");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    //glutMotionFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}