2016年5月16日 星期一

week13

1
建立一個新的貝殼專案
在build設freeglut include & freeglut lib




輸入5個咒語


先做2個茶壺
程式碼:
#include <GL/glut.h>
void display()
{
    glPushMatrix();
        glutSolidTeapot(0.3); //茶壺1號機
        glTranslatef(0.5, 0, 0);
        glutSolidTeapot(0.3); //茶壺2號機
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMainLoop();
}
改程式碼讓茶壺可以藉由滑鼠做旋轉
程式碼:
#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();
}
如下旋轉茶壺的公轉和自轉

然後加程式碼做關節可以轉動

結果如下

做機器人的關節
程式碼:
#include <GL/glut.h>
float rot1=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

    glPushMatrix();
        glutWireCube(0.3);///body
        glPushMatrix();
           glTranslatef(0,0.2,0);
           glutWireCube(0.1);///head
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.2,0.1,0);
            glutWireCube(0.1);///upper arm
            glTranslatef(0.1,0,0);
            glutWireCube(0.1);///lower arm
            glTranslatef(0.1,0,0);
            glutWireCube(0.1);///right head
        glPopMatrix();

         glPushMatrix();
            glTranslatef(-0.2,0.1,0);
            glutWireCube(0.1);///upper arm
            glTranslatef(-0.1,0,0);
            glutWireCube(0.1);///lower arm
            glTranslatef(-0.1,0,0);
            glutWireCube(0.1);///right head
        glPopMatrix();


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

然後加上關節可動
程式碼:
#include <GL/glut.h>
float rot1=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutWireCube(0.3);
        glPushMatrix();
            glTranslatef(0,0.2,0);
            glutWireCube(0.1);
         glPopMatrix();

         glPushMatrix();
            glTranslatef(0.15,0.1,0);
            glRotatef(rot1,0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(0.05,0,0);
            glRotatef(rot1,0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(0.05,0,0);
            glRotatef(rot1,0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);
          glPopMatrix();


         glPushMatrix();
            glTranslatef(-0.15,0.1,0);
            glRotatef(-rot1,0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(-0.05,0,0);
            glRotatef(-rot1,0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

            glTranslatef(-0.05,0,0);
            glRotatef(-rot1,0,0,1);
            glTranslatef(-0.05,0,0);
            glutWireCube(0.1);

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





1 則留言: