2018年3月30日 星期五

Week05_陳示珮

第五週
1. 複習 : T,R,S (mouse , motion)
2. 主題 : T-R-T轉動
3. 主題 : 階層轉動
4. 矩陣
5. 回家作業


















(1)開啟上次的Transformation.exe
















(2)在下面視窗案右鍵選擇 [s]swap translate/rotate
可交換旋轉和移動的程式碼
















(3)由下往上看程式碼 畫車->放大縮小->旋轉->移動
    (自轉)















(4)由下往上看程式碼 畫車->放大縮小->移動->旋轉
    (公轉)




















(1)開啟GLUT project















(2)更改49~54行程式碼變動左上角圖形位子
把球移到(0,0,-6)

程式碼 :

glPushMatrix();
      ///glTranslated(-2.4,1.2,-6);///把球移到左上角
      ///glRotated(60,1,0,0);
      ///glRotated(a,0,0,1); ///轉動
        glTranslated(0,0,-6); ///球中心在(0,0,-6)
        glutSolidSphere(1,slices,stacks);
 glPopMatrix();

 glColor3f(1,1,1);  ///更改左上方圖形以外的顏色















(3)增加旋轉

程式碼 :

glPushMatrix();
        ///glTranslated(-2.4,1.2,-6);///把球移到左上角
        ///glRotated(60,1,0,0);
        glRotated(a,0,0,1); ///轉動
        glTranslated(0,0,-6); ///球中心在(0,0,-6)
        glutSolidSphere(1,slices,stacks);
    glPopMatrix();

   glColor3f(1,1,1);///更改左上方圖形以外的顏色















(4)把球移回原來的位子

程式碼 :

glPushMatrix();
        glTranslated(-2.4,1.2,-6);///把球移到左上角
        ///glRotated(60,1,0,0);
        glRotated(a,0,0,1); ///轉動
        ///glTranslated(0,0,-6); ///球中心在(0,0,-6)
        glutSolidSphere(1,slices,stacks);
  glPopMatrix();

  glColor3f(1,1,1);///更改左上方圖形以外的顏色






(5)球公轉

程式碼 :


 glPushMatrix();
        glRotated(a,0,0,1); ///轉動
        glTranslated(-2.4,1.2,-6);///把球移到左上角
        glutSolidSphere(1,slices,stacks);
  glPopMatrix();

  glColor3f(1,1,1);///更改左上方圖形以外的顏色




















(1)以茶壺握把為中心點旋轉茶壺(如右圖)

*小提醒 :
複製貼上程式碼後全選程式碼按右鍵 Format use AStyle 可以幫忙整理程式

程式碼 :

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now:
    glutMainLoop();
}









(2)以紅色茶壺中心為綠色茶壺握把的中心點旋轉

程式碼 :

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now:
    glutMainLoop();
}





(3)利用茶壺做出分別有關節的手臂及手肘做轉動

程式碼 :

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now:
    glutMainLoop();
}




















(4)改變angle方向讓手能同方向轉動

程式碼 :

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now:
    glutMainLoop();
}














沒有留言:

張貼留言