2015年5月27日 星期三

Week14_02161052_葉采晴

一開始先看了學長姊的作品
今天要做的是機器人
先觀察角度旋轉和位移讓關節做旋轉



 首先先製造一個茶壺
並且打光 寫深度
(目前還不會移動喔!)




利用body和arm
改成cube

製造兩個茶壺
其中一個茶壺當作手臂

#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);  ///step4:把"會對茶把手做旋轉的東西"再移到右上 掛上去
        glRotatef(angle,0,0,1); ///step3:把茶壺都對中間旋轉
        glTranslatef(0.3,0,0);  ///step2:往右移
        glutSolidTeapot(0.3);  ///step1:這是一個茶壺
    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();
}



#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);  ///step4:把"會對茶把手做旋轉的東西"再移到右上 掛上去
        glRotatef(angle,0,0,1); ///step3:把茶壺都對中間旋轉
        glTranslatef(0.3,0,0);  ///step2:往右移*/
        drawArm();  ///step1:這是一個茶壺
    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();
}



















使用兩個cube
當作上手臂和下手臂
就可以產生多個關節

#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);  ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
        glRotatef(angle,0,0,1); ///step3:把下面整團都對中間旋轉
        glTranslatef(0.25,0,0);  ///step2:把他往右移,Arm Joint在中間
        drawArm();  ///step1:這是一個Arm
        glPushMatrix();
            glTranslatef(0.25,0,0);  ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
            glRotatef(angle,0,0,1); ///step3:把下面整團都對中間旋轉
            glTranslatef(0.25,0,0);  ///step2:把他往右移,Arm Joint在中間
            drawArm();  ///step1:這是一個Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);  ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
        glRotatef(-angle,0,0,1); ///step3:把下面整團都對中間旋轉
        glTranslatef(-0.25,0,0);  ///step2:把他往右移,Arm Joint在中間
        drawArm();  ///step1:這是一個Arm
        glPushMatrix();
            glTranslatef(-0.25,0,0);  ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
            glRotatef(-angle,0,0,1); ///step3:把下面整團都對中間旋轉
            glTranslatef(-0.25,0,0);  ///step2:把他往右移,Arm Joint在中間
            drawArm();  ///step1:這是一個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();
}


沒有留言:

張貼留言