顯示具有 02160366_武鵬宇 標籤的文章。 顯示所有文章
顯示具有 02160366_武鵬宇 標籤的文章。 顯示所有文章

2015年6月3日 星期三

02160366 武鵬宇 week15

用按鈕5跟6來控制藍色的茶壺
7跟8控制綠色的茶壺
#include <GL/glut.h>
#include <stdio.h>
float  x1=-0.4, y1=0, x2=0, y2=0, angle=0, angle2=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(x2,y2,0);
        glRotatef(angle,0,0,1);
        glColor3f(0,0,1);
        glutSolidTeapot(0.2);
        glPushMatrix();
            glTranslatef(x1,y1,0);
            glRotatef(angle2,0,0,1);
            glTranslatef(-0.25,-0.11,0);
            glColor3f(0,1,0);
            glutSolidTeapot(0.2);
        glPopMatrix();
    glPopMatrix();
    glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') x1+=0.1;
    if(key=='2') x1-=0.1;
    if(key=='3') y1+=0.1;
    if(key=='4') y1-=0.1;
    if(key=='5') angle+=10;
    if(key=='6') angle-=10;
    if(key=='7') angle2+=10;
    if(key=='8') angle2-=10;
    printf("%f %f\n", x1, y1);
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot3D");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}
用鍵盤1 2 3 4跟滑鼠來控制兩隻手的四個關節
#include <GL/glut.h>
float oldX=0, oldY=0;
float angle[10]={0,0,0,0,0,0,0,0,0,0,};
int now=0;
void motion(int x,int y)
{
    angle[now] -=y -oldY;
    oldY=y; oldX=x;
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='1') now=1;
    if(key=='2') now=2;
    if(key=='3') now=3;
    if(key=='4') now=4;
    glutPostRedisplay();
}
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();
}
void display()
{
    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);
        glRotatef(angle[1],0,0,1);
        glTranslatef(0.25,0,0);
        glColor3f(0,0,1);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25,0,0);
            glRotatef(angle[2],0,0,1);
            glTranslatef(0.25,0,0);
            glColor3f(0,1,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle[3],0,0,1);
        glTranslatef(-0.25,0,0);
        glColor3f(0.5,0.5,1);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(-angle[4],0,0,1);
            glTranslatef(-0.25,0 ,0);
            glColor3f(0.5,1,0.5);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void mouse(int button,int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=x; }
}
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);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMainLoop();
}

2015年5月27日 星期三

02160366 武鵬宇 week14

今天老師教了我們如何做關節的旋轉

示範的時候是用了一個茶壺的身體
跟另一個茶壺去往右與往上移動到茶壺身體的右上方
就像是它的手 然後再另用 translate 跟 rotate 的程式碼去旋轉它
今天教得跟期末作業有很大的關係

#include <GL/glut.h>

float angle=0;
void display()
{
    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.6,0.2,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.3,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSolidTeapot(0.3);
    glFlush();
}
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);

    glutMainLoop();
}
改用一個CUBE來做測試
#include <GL/glut.h>
float angle=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();
}
void display()
{
    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);
        glRotatef(angle,0,0,1);
        glTranslatef(0.3,0,0);
        drawArm();
    glPopMatrix();
    drawBody();
    glFlush();
}
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);

    glutMainLoop();
}
加入了另一個手的關節
變成了兩截式 而且左右都各有一雙手臂
glPushMatrix();
        glTranslatef(0.5,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(-0.25,0,0);
            drawArm();
        glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
最後一步加入了滑鼠移動的程式碼 
可用滑鼠拖曳的方式讓手臂來轉動 !!!
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=x; }
}
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);
    glutMainLoop();
}
總結程式碼
#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();
}
void display()
{
    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);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(-0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
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=x; }
}
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);
    glutMainLoop();
}

2015年5月20日 星期三

02160366 武鵬宇 week13


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

void display()
{
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glFlush();
}

void reshape(int w,int h)
{
     glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w/2,w/2,-h/2,h/2,-1000,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);



    glutMainLoop();
}
攝影機.運鏡
#include <GL/glut.h>
float eyeX=-100,eyeY=80,eye2=-200;

void display()
{
    GLfloat pos[]={0,0,200,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        gluLookAt(eyeX,eyeY,eye2,0,0,0,0,1,0);
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion(int x,int y)
{
    eyeX=x-400;eyeY=300-y;
    glutPostRedisplay();
}

void reshape(int w,int h)
{
     glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.001,10000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}
void timer(int t )
{
    glutTimerFunc(20,timer,t+1);
    eyeX+=2;
    if(eyeX>300)eyeY+=4;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);



    glutMainLoop();
}


2015年5月13日 星期三

02160366 武鵬宇 week12

#include "glm.h"
GLMmodel* pmodel = NULL;

void display()
{
    GLfloat pos[] ={0.0,0.0,-1.0,0.0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glFlush();
}
int main(int argc, char** argv)
{

    pmodel = glmReadOBJ("porsche.obj");
    glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);

glutInit(&argc,argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMainLoop();
}



2015年5月6日 星期三

Week11 武鵬宇02160366 課堂作業

Timer計時器(等速)

TODO:下載freeglut
TODO:下載OpenCV2.1(安裝)
TODO:下載會動的地球
TODO上周Project設定
            目錄(a)compiler的include
                     (b)Linker的Lib
                     (c)Link參數
TODO:
            (1)glutTimerFunce(2000,timer,0)
           (2)void timer (int t)
                                      {
glutTimerFunc(20, timer, 0);
    glutPostRedisplay();
                                       }
#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;///自動轉很帥
}
void myInit()
{
    quad = gluNewQuadric();
    IplImage * img = cvLoadImage("image.jpg"); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定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, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}   ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec   50fps:20msec
    glutTimerFunc(20, timer, 0);
    //printf("timer now\n");
    glutPostRedisplay();
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    glutTimerFunc(0, timer, 0);

    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}


2015年4月29日 星期三

Week10 武鵬宇02160366 課堂作業

今天做了一個2D的貼圖 跟3D的茶壺放在同一個專案裡
其中設定了一堆麻煩的東西
但最後還是設定出來了 !!!
#include <GL/glut.h>
#include <opencv/highgui.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0,1,0);
        glutSolidTeapot(0.3);
     glPopMatrix();
     glFlush();
     angle+=0.1;
}
int main(int argc, char**argv)
{
   IplImage * img = cvLoadImage("fuck.jpg");
   cvNamedWindow("2D");
   cvShowImage("2D", img);
   cvWaitKey(1);

   glutInit(&argc, argv);
   glutCreateWindow("3D");
   glutDisplayFunc(display);
   glutIdleFunc(display);

   glutMainLoop();
}
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
float angle=0; ///自動轉很帥
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(angle, 0,1,0);///自動轉很帥
        glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
    angle+=0.1;///自動轉很帥
}
void myInit()
{   IplImage * img = cvLoadImage("fuck.jpg"); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定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, 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);///自動轉很帥 (閒閒沒事幹, 就重畫)
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}
把2D的貼圖跟3D的茶壺結合囉


2015年4月15日 星期三

Week08 武鵬宇02160366 課堂作業


#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
float potX=-1, potY=0;
void display()
{
    glClearColor(0.5, 0.5, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
       glTranslatef(potX, potY, 0);
       glutSolidTeapot(0.05);
    glPopMatrix();
    glFlush();
    potX+=0.001;
    if(potX> 1.1) potX=-1.1;

}
void keyboard(unsigned char key, int x, int y)
{
    exit(0);
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN){
        PlaySound("Data/Shot.wav", NULL, SND_ASYNC);
        printf("Shot!!!\n");
        float mouseX = 2*x/1280.0 -1, mouseY = -(2*y/1024.0 -1);
        if( abs(mouseX-potX)<0.01 && abs(mouseY-potY)<.001){
            printf("YA! Got it!\n");
            PlaySound("ahhh.wav", NULL, SND_ASYNC);
        }
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);

    glutCreateWindow("hello");

    glutFullScreen();

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    IplImage * img=cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/tulips.jpg")
    cvShowImage("a", img);
    cvWaitKey(10);
    glutMainLoop();
}
今天做的是一 個小互動遊戲 用游標打到茶壺就會發出聲音

2015年4月8日 星期三

Week07 武鵬宇02160366 課堂作業




#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void display()
{
    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key, int x,int y)
{
    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);
}
void mouse(int button, int state, int x,int y)
{
    printf("now play sound\n");
    PlaySound("BOTTLE.wav", NULL, SND_ASYNC);
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);

    glutCreateWindow("YAYA");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}

2015年3月25日 星期三

Week05 武鵬宇02160366 課堂作業

做一個3D旋轉的長方形
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();

    glRotatef( angle, 0,1,0.5);
    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_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();
    angle +=0.01;
    glFlush();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutCreateWindow("02160366");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
加了角度的參數和motion指令,讓用滑鼠拖曳可以讓3D模型轉動
int oldX=0;
void motion(int x,int y)
{
    angle +=(x-oldX);
    oldX=x;
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutCreateWindow("02160366");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}



2015年3月18日 星期三

week04 武鵬宇02160366 課堂作業

做一個3D會旋轉的茶壺

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glTranslated(-0.04,0,0);
        glRotatef(angle,1,1,0);

    glutWireTorus(0.3,0.7,30,30);
    glutSolidTeapot(0.2);
    glPopMatrix();


    glFlush();
    angle+=0.1;

}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutCreateWindow("02160366");
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}

2015年3月11日 星期三

week03 武鵬宇02160366 課堂作業

1.
#include <GL/glut.h>
void display()
{
    glClearColor(0,0,1,1);///設定清畫面的顏色
    glClear(GL_COLOR_BUFFER_BIT);///CLEAR清畫面
    glColor3f(1,1,0);///顏色
    glutSolidTeapot(0.5);
     glColor3f(1,0,0);///顏色
    glutSolidTeapot(0.4);
     glColor3f(1,0,1);///顏色
    glutSolidTeapot(0.3);
    glFlush();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutCreateWindow("02160366");
    glutDisplayFunc(display);
    glutMainLoop();
}
2.
#include <GL/glut.h>
void display()
{
    glClearColor(0,0,0.3,1);///設定清畫面的顏色
    glClear(GL_COLOR_BUFFER_BIT);///CLEAR清畫面
    glColor3f(1,1,0);///顏色

     glBegin(GL_POLYGON);
     glColor3f(1,0,0);///顏色
     glVertex2f(-1.0,-1.0);

     glColor3f(0,1,0);///顏色
     glVertex2f(1.0,0.0);

     glColor3f(0,0,1);///顏色
     glVertex2f(0.0,1.0);
   glEnd();

    glFlush();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);


    glutCreateWindow("02160366");

    glutDisplayFunc(display);
    glutMainLoop();
}
3.
#include <GL/glut.h>
void display()
{
    glClearColor(0,0,0.3,1);///設定清畫面的顏色
    glClear(GL_COLOR_BUFFER_BIT);///CLEAR清畫面
    glColor3f(1,1,0);///顏色

     glBegin(GL_POLYGON);
     glColor3f(0.39,0.75,0.84);///顏色
     glVertex2f(0.6,0.3);
     glVertex2f(1.0,0.3);
     glVertex2f(0.2,0.15);
     glVertex2f(0.9,0.84);
   glEnd();

    glFlush();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutCreateWindow("02160200");
    glutDisplayFunc(display);
    glutMainLoop();
}


2015年3月4日 星期三

week02 武鵬宇02160366 課堂作業

0行
0行
10行
#include <GL/glut.h>

void display()
{
    glutSolidTeapot(0.3);
    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    glutCreateWindow("MCUCCE");

    glutDisplayFunc(display);

    glutMainLoop();
}

這是我第二個作業 !