2015年5月28日 星期四

week14_T-R-T關節移動_呂登祐

1)期末作品
2)會跳舞的機器人
3)T-R-T觀念
*****************************************************
1.茶壺旋轉並移動旋轉軸

#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();
    glRotatef(angle,0,0,1); ///v3: rotating teapot on the handle
    glTranslatef(0.5,0,0);  ///v2: teapot on the right
    glutSolidTeapot(0.3);  ///v1: a teapot
    glPopMatrix();
    glFlush();
}
void timer(int t)
{
    glutTimerFunc(20, timer, 0);
    angle++;
    glutPostRedisplay();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("3D_0031");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}

2.滑鼠拖曳旋轉位


#include <GL/glut.h>
float angle, oldX, oldY=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();
    glRotatef(angle,0,0,1);///v3: rotating teapot on the handle
    glTranslatef(0.5,0,0);///v2: teapot on the right
    glutSolidTeapot(0.3);///v1: a teapot
    glPopMatrix();
    glFlush();
}
void timer(int t)
{
    glutTimerFunc(20, timer, 0);
    ///angle++;
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) {oldX=x; oldY=y;}
}
void motion(int x, int y)
{
    angle+=(x-oldX);
    oldX=x;
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("3D_0031");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

3.掛軸心

#include <GL/glut.h>
float angle, oldX, oldY=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.45, 0.14, 0);///v4: 掛上去
    glRotatef(angle,0,0,1);///v3: rotating teapot on the handle
    glTranslatef(0.45, -0.1, 0);///v2: teapot on the right
    glutSolidTeapot(0.3);///v1: a teapot
    glPopMatrix();
    glutSolidTeapot(0.3);
    glFlush();
}
void timer(int t)
{
    glutTimerFunc(20, timer, 0);
    ///angle++;
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) {oldX=x; oldY=y;}
}
void motion(int x, int y)
{
    angle+=(x-oldX);
    oldX=x;
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("3D_0031");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

簡單來說T-R-T,第一個T就是移動整個物體,第二個R就是轉動角度。第三個T就是移動軸心


4.滑鼠控制手臂

#include <GL/glut.h>

float angle, oldX, oldY=0;

void drawArm()
{
    glPushMatrix();
      glScalef(0.5, 0.25, 0.25);
      glutSolidCube(1);
    glPopMatrix();
}
void drawBody()
{
    glPushMatrix();
      glTranslatef(0, -0.5, 0);
      glRotatef(-90, 1, 0, 0);
      glutSolidCone(0.3, 0.8, 30, 30);
    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.15, 0, 0);///v4: 掛上去
        glRotatef(angle,0,0,1);///v3: rotating teapot on the handle
        glTranslatef(0.25, 0, 0);///v2: teapot on the right
        drawArm();/// right upper-arm
      glPushMatrix();
        glTranslatef(0.25, 0, 0);///v4: 掛上去
        glRotatef(angle,0,0,1);///v3: rotating teapot on the handle
        glTranslatef(0.25, 0, 0);///v2: teapot on the right
        drawArm();/// right lower-arm
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.15, 0, 0);///v4: 掛上去
        glRotatef(-angle,0,0,1);///v3: rotating teapot on the handle
        glTranslatef(-0.25, 0, 0);///v2: teapot on the right
        drawArm();/// left upper-arm
      glPushMatrix();
        glTranslatef(-0.25, 0, 0);///v4: 掛上去
        glRotatef(-angle,0,0,1);///v3: rotating teapot on the handle
        glTranslatef(-0.25, 0, 0);///v2: teapot on the right
        drawArm();/// left lower-arm
       glPopMatrix();
    glPopMatrix();

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



沒有留言:

張貼留言