顯示具有 02161105_黃廷峯 標籤的文章。 顯示所有文章
顯示具有 02161105_黃廷峯 標籤的文章。 顯示所有文章

2015年6月10日 星期三

02161105 黃廷峯 課堂作業 WEEK16

茶壺噴紅氣旋轉

#include <GL/glut.h>
float angle1=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glColor3f(0 ,0, 1);
    glutSolidTeapot(0.3);

    glPushMatrix();
    glTranslatef(0.5, 0.15, 0);
    glRotatef(angle1, 0,0,1);
    glTranslatef(0.3, 0,0);

    glScalef(1, 0.3, 0.3);
    glColor3f(1,0,0);
    glutSolidSphere(0.3, 30, 30);
    glPopMatrix();
    glPopMatrix();

    glFlush();
}
void motion(int x, int y)
{
    angle1 = x;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    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()
{
    { ///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(angle, 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(angle, 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(-angle, 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(-angle, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Left Lower Arm
        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=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);
    glutMainLoop();
}

2015年6月3日 星期三

02161105 黃廷峯 課堂作業 WEEK15

#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();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
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') angle+=10;
    if(key=='8') angle-=10;
    printf("%f %f\n", x1, y1);
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02161105");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    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()
{
    { ///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(angle, 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(angle, 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(-angle, 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(-angle, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Left Lower Arm
        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=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);
    glutMainLoop();
}


2015年5月27日 星期三

02161105 黃廷峯 課堂作業 WEEK14

我的茶壺不只有手而且還會旋轉!!!!!!


#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("02161105");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);

    glutMainLoop();
}




2015年5月13日 星期三

02161105 黃廷峯 課堂作業 WEEK12

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

void display()
{
    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("GLUT");
    glutDisplayFunc(display);
    glutMainLoop();

}



#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);
    glEnable(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("GLUT");
    glutDisplayFunc(display);
    glutMainLoop();

}



2015年5月6日 星期三

02161105 黃廷峯 課堂作業 WEEK11

自動轉很帥

#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();
}




簡化版旋轉茶壺

#include <GL/glut.h>
float angle=0;
void timer(int t)
{
    glutTimerFunc(10, timer, t+1);
    angle+=1;
    glutPostRedisplay();
}
void display()
{

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0,1,0);
        glutSolidTeapot(0.5);
    glPopMatrix();
    glFlush();



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

    glutTimerFunc( 10, timer, 0);
    glutMainLoop();

}





憤怒壺


#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0, potVX=0, potVY=0; int potState;
void timer(int t)
{
    glutTimerFunc(10, timer, t+1);
    angle+=1;
    if(potState==0) potY=0.5*sin(angle/180.0*3.14);
    if(potState==2){ potX+=potVX; potY+=potVY; }
    glutPostRedisplay();
}
void display()
{

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.01);
    glPushMatrix();
        glTranslatef(potX, potY, 0);
        glRotatef(angle, 0,1,0);
        glutSolidTeapot(0.01);
    glPopMatrix();
    glFlush();





}
void mouse(int button, int state, int x, int y)
{if(state==GLUT_DOWN){ potState=1;}
 else if(state==GLUT_UP){ potState=2; potVX=-(x-350)/3500.0; potVY=-(y-350)/3500.0;
}
}
void motion(int x, int y)
{
    if(potState==1){potX=(x-350)/350.0; potY=(y-350)/350.0;}
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutReshapeWindow(700,700);
    glutDisplayFunc(display);
    glutTimerFunc( 10, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();

}



2015年4月29日 星期三

02161105 黃廷峯 課堂作業 WEEK10

旋轉貼圖茶壺

#include <opencv/highgui.h>
#include <opencv/cv.h>
#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();
    angle+=0.1;
}
void myInit()
{   IplImage * img = cvLoadImage("chloe.jpg.jpeg");
    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)
{
    /*IplImage * img = cvLoadImage("chloe.jpg");
    cvNamedWindow("2D");
    cvShowImage("2D", img);
    cvWaitKey(1);*/

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

    glutMainLoop();
}



2015年4月8日 星期三

02161105 黃廷峯 課堂作業 WEEK07

打出a b來顯示文字

#include <GL/glut.h>
#include <stdio.h>
void display()
{
    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='a'){
        printf("FK U\n");
    }else if(key=='b'){
    printf("FK M\n");
    }
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);

    glutCreateWindow("02161105");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();



}


播音樂

#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=='a'){
        printf("FK U\n");
    }else if(key=='b'){
    printf("FK M\n");
    }
}
void mouse(int button, int state, int x, int y)
{
    printf("now playing sound\n");
    PlaySound("thund.wav", NULL, SND_ASYNC);
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);

    glutCreateWindow("02161105");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMainLoop();



}



音符

#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 playing sound\n");
    PlaySound("thund.wav", NULL, SND_ASYNC);
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);

    glutCreateWindow("02161105");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMainLoop();



}


2015年3月25日 星期三

02161105 黃廷峯 課堂作業 WEEK05

今天我學到了目標
看到了我學到的東西感覺真好



移動

#include <Gl/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef( angle,  0, 1, 0);
    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();
    glPopMatrix();
    angle +=0.5;
    glFlush();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02161105");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();


}





3D旋轉移動

#include <Gl/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef( angle,  1, 1, 0);
    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.1;
    glFlush();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02161105");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();


}



滑鼠移動

#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.1;
    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("02161105");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();


}


2015年3月18日 星期三

02161105 黃廷峯 課堂作業 WEEK04

#include <GL/glut.h>
void display()
{

glutWireTorus(0.3, 0.7, 30,30);
glPushMatrix();
glTranslatef(0, 0, 0);
///glRotatef()
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02161105");
    glutDisplayFunc(display);

    glutMainLoop();
}



移動
#include <GL/glut.h>
void display()
{

glutWireTorus(0.3, 0.7, 30,30);
glPushMatrix();
glTranslatef(0.6, 0, 0);
///glRotatef()
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02161105");
    glutDisplayFunc(display);

    glutMainLoop();
}

旋轉
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.04, 0, 0);
glRotatef(angle, 1,0,1);
glutWireTorus(0.3, 0.7, 30,30);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();
angle+=1.5;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02161105");
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}