2015年6月3日 星期三

02163051_邱冠程 Week15

固定旋轉軸,
讓數字鍵可以移動茶壺

固定關節,
讓特定關節移動。
#include <GL/glut.h>
float oldX=0, oldY;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;

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

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);///
        drawArm();
        //glutSolidTeapot(0.3);
    glPushMatrix();
        glTranslatef(0.25, 0, 0); ///旋轉軸
        glRotatef(angle[2], 0,0,1); ///角度
        glTranslatef(0.25, 0,0);///
        drawArm();
    glPopMatrix();
glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0); ///旋轉軸
        glRotatef(-angle[3], 0,0,1); ///角度
        glTranslatef(-0.25, 0,0);///
        drawArm();
    glPushMatrix();
        glTranslatef(-0.25, 0, 0); ///旋轉軸
        glRotatef(-angle[4], 0,0,1); ///角度
        glTranslatef(-0.25, 0,0);///
        drawArm();
    glPopMatrix();
    glPopMatrix();
    drawBody();

    glFlush();
}
void motion(int x, int y)
{
    angle[now] -= y - oldY;
    oldY=y; oldX=x;
}

void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    ///angle++; 要自己動,要刪掉
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("TRT test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}


沒有留言:

張貼留言