2015年5月27日 星期三

02160366 武鵬宇 week14

今天老師教了我們如何做關節的旋轉

示範的時候是用了一個茶壺的身體
跟另一個茶壺去往右與往上移動到茶壺身體的右上方
就像是它的手 然後再另用 translate 跟 rotate 的程式碼去旋轉它
今天教得跟期末作業有很大的關係

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

    glutMainLoop();
}
改用一個CUBE來做測試
#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);
        glRotatef(angle,0,0,1);
        glTranslatef(0.3,0,0);
        drawArm();
    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();
}
加入了另一個手的關節
變成了兩截式 而且左右都各有一雙手臂
glPushMatrix();
        glTranslatef(0.5,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(-0.25,0,0);
            drawArm();
        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=x; }
}
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();
}
總結程式碼
#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);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.25,0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(-0.25,0,0);
            drawArm();
        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=x; }
}
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();
}

沒有留言:

張貼留言