2018年3月30日 星期五

Week05 吳姿諭

使用Transformation觀察旋轉

先到 http://jsyeh.org/3dcg10 下載 windowsdataglut32.dll.





接著把windows、data解壓縮,再把data、glut32.dll放入windows資料夾裡
























就可以開啟transformation.exe了



















程式碼解釋

glTranslatedf表位置glRotated     表旋轉

讓茶壺旋轉


#include <GL/glut.h>
float angle=0; 讓茶壼自動轉動的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); 備份矩陣
glRotatef(angle, 0,0,1); 轉動
glutSolidTeapot(0.3);
glPopMatrix(); 還原矩陣
glutSwapBuffers();
}
void motion(int x, int y)
{
angle=x; 改變角度
glutPostRedisplay(); 請 重畫畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week05 TRT");

glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}


茶壺旋轉及改變旋轉處

#include <GL/glut.h>
float angle=0; 讓茶壼自動轉動的角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); 備份矩陣
        glRotatef(angle, 0,0,1); 轉動
        glTranslatef(0.45, -0.10, 0);把茶壺柄放到中間
        glutSolidTeapot(0.3);
    glPopMatrix();還原矩陣
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle=x;改變角度
    glutPostRedisplay(); 請 重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week05 TRT");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}




增加茶壺及改變位置旋轉

#include <GL/glut.h>
float angle=0; 讓茶壼自動轉動的角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); 備份矩陣
        glColor3f(0,0 ,1); glutSolidTeapot(0.3);中間的茶壺
        glTranslatef(0.43,0.2,0);掛上去
        glRotatef(angle, 0,0,1);轉動
        glTranslatef(0.45, -0.10, 0);把茶壺柄放到中間
        glColor3f(1,0 ,0);glutSolidTeapot(0.3);會轉動的茶壺
    glPopMatrix();還原矩陣
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle=x;改變角度
    glutPostRedisplay(); 請 重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week05 TRT");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();

}




多增加一個轉動的茶壺


#include <GL/glut.h>
float angle=0; 讓茶壼自動轉動的角度
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); 
        glColor3f(0,0 ,1); glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(0.43,0.2,0);
            glRotatef(angle, 0,0,1); 
            glTranslatef(0.45, -0.10, 0);
            glColor3f(1,0 ,0);glutSolidTeapot(0.3);
         glPushMatrix();
            glTranslatef(0.43,0.2,0);
            glRotatef(angle, 0,0,1); 
            glTranslatef(0.45, -0.10, 0);
            glColor3f(1,0 ,0);glutSolidTeapot(0.3);
        glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle=x;改變角度
    glutPostRedisplay(); 請 重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week05 TRT");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

多增加另一邊轉動的茶壺


#include <GL/glut.h>
float angle=0; 讓茶壼自動轉動的角度
void display()
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); 
        glColor3f(0,0 ,1); glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(0.43,0.2,0);
            glRotatef(angle, 0,0,1);
            glTranslatef(0.45, -0.10, 0);
            glColor3f(1,0 ,0);glutSolidTeapot(0.3);
         glPushMatrix();
            glTranslatef(0.43,0.2,0);
            glRotatef(angle, 0,0,1);
            glTranslatef(0.45, -0.10, 0);
            glColor3f(1,0 ,0);glutSolidTeapot(0.3);
        glPopMatrix();
        glPopMatrix();
          glPushMatrix();
            glTranslatef(-0.43,0.2,0);
            glRotatef(angle, 0,0,1); 
            glTranslatef(-0.45, -0.10, 0);
            glColor3f(1,0 ,0);glutSolidTeapot(0.3);
         glPushMatrix();
            glTranslatef(-0.43,0.2,0);
            glRotatef(angle, 0,0,1); 
            glTranslatef(-0.45, -0.10, 0);
            glColor3f(1,0 ,0);glutSolidTeapot(0.3);
        glPopMatrix();
        glPopMatrix();/// 還原矩陣
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle=x;改變角度
    glutPostRedisplay(); 請 重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week05 TRT");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();

}

沒有留言:

張貼留言