2016年5月16日 星期一

Week13

1. 兩個茶壺

將bin 中 freeglut.dll 複製加到資料夾中
打咒語  freeglut  opengl32  glu32  gdi32  winmm

程式碼:
#include <GL/glut.h>

void display()
{
    glPushMatrix();
        glutSolidTeapot(0.3);
        glTranslatef(0.5, 0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMainLoop();
}


2. 茶壺自轉 公轉

用上面成程式再加一些程式碼

程式碼:
#include <GL/glut.h>
float rot1=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);
        glRotatef(rot1, 0,0,1);
        glTranslatef(0.5, 0,0);
        glRotatef(rot1,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x, int y)
{
     rot1=x;
     glutPostRedisplay();
}


int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();

}


3.關節選轉T-R-T

#include <GL/glut.h>
float rot1=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.2);

       glTranslatef(0.33,0.05,0);//step3:定物體新定點的地方做下面的旋轉(即掛在新點做下面選轉)
        glRotatef(rot1,0,0,1); //step1:準備旋轉
        glTranslatef(0.25,0,0); //step2:讓茶壺先以此點做旋轉
        glutSolidTeapot(0.2);

        glTranslatef(0.33,0.05,0);  //每次新的茶壺都會多一動一些所以不會重疊
        glRotatef(rot1,0,0,1);
        glTranslatef(0.25,0,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x, int y)
{
     rot1=x;
     glutPostRedisplay();
}


int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();

}


4. 機器人製作


程式碼

#include <GL/glut.h>
float rotX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

    glPushMatrix();
        glutWireCube(0.3);///身體
        glPushMatrix();
            glTranslatef(0,0.2,0);
            glutWireCube(0.1);///頭
        glPopMatrix();

        glPushMatrix();///右邊
            glTranslatef(0.15,0,0);
            glRotatef(rotX,0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);///上臂

            glTranslatef(0.05,0,0);
            glRotatef(rotX,0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);///下臂

            glTranslatef(0.05,0,0);
            glRotatef(rotX,0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);///右手
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.15,0,0);
            glRotatef(-rotX,0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);///上臂

            glTranslatef(-0.05,0,0);
            glRotatef(-rotX,0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);///下臂

            glTranslatef(-0.05,0,0);
            glRotatef(-rotX,0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);///左手
        glPopMatrix();

    glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    rotX=x;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}


沒有留言:

張貼留言