2015年5月28日 星期四

02160136_陳威志 week14

(1)期末作品
(2)會跳舞的機械人
(3)T-R-T 對關節轉
(4)小考:T-R-T 觀念
(5)作業問題

複習 做出一個選轉的茶壺


以滑鼠(mouse)轉動茶壺



改變旋轉軸心的位置T-R-"T"

示意圖


做出可以旋轉的手臂關節


#include <GL/glut.h>
float angle = 0, oldx = 0, 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);
        glRotatef(angle, 0, 0, 1);
        glTranslatef(0.25, 0, 0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.15, 0, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(0.25, 0, 0);
            drawArm();
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.15, 0, 0);
        glRotatef(-angle, 0, 0, 1);
        glTranslatef(-0.25, 0, 0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.15, 0, 0);
            glRotatef(-angle, 0, 0, 1);
            glTranslatef(-0.25, 0, 0);
            drawArm();
        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("02160136 TRT 3D");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}



沒有留言:

張貼留言