2017年3月28日 星期二

WEEK06 劉育榕#


(1) 期中考題
(2) 主題:  階層轉動
(3) 實作:  T-R-T
(4) 複習:  矩陣
(5) 回家作業
----------------------------------------

課堂練習一


練習期中考題


glPushMatrix( );                            //備分矩陣 10%
   glRotatef(angle,x,y,z);              //旋轉 30%
   glTranslatef( x, y ,z );              //移動 40%
   glScalef( x , y , z );                //縮放50%

   glBegin( GL_POLYGON);  //開始畫 60%
      glNormal3f(nx ,ny ,nz ); //打光的法向量 90%
      glTexCoord2f( tx ,ty );  //貼圖座標100%
      glColor3f( r , g , b );     //顏色70%
      glVertex3f( x , y ,z );   //頂點80%
   glEnd( );                        //結束畫
glPopMatrix( );               //還原矩陣 20%


課堂練習二

理解車子模型的轉動 :

1. 解壓縮 => windows.zip  / data.zip / glut32.dll
2.打開Transformation.exe 
#小秘訣 : 如何思考,把程式碼從 glBegin();~glEnd(); 視為車體。之後在由下往上解讀程式碼。

   第一種  : 由下往上看程式碼:縮小 - 旋轉 - 往右移動


原圖


改變後


   第二種  :由下往上看程式碼:縮小 - 往左移動 -旋轉

                                          

原圖


改變後

 

課堂練習三



實作: T-R-T

#include <GL/glut.h>
float mouseX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
      glutSolidTeapot(0.3);    //茶壺
        glPushMatrix();
            glRotatef(mouseX,0,0,1);    //用滑鼠來轉動
           glTranslatef(0.5,0,0);            //往右移動
           glutSolidTeapot(0.3);//茶壺
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)     //用滑鼠操控
{
    mouseX=x;
    glutPostRedisplay();
}
int main(int argc ,char **argv)
{
    glutInit(&argc ,argv);
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("TRT robot");
    glutDisplayFunc(display);   
    glutMotionFunc(motion);     //滑鼠
    glutMainLoop();
}


失敗的轉動




#include <GL/glut.h>
float mouseX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
       glPushMatrix();
         glRotatef(mouseX,0,0,1);
         glColor3f(1,0,0); glutSolidTeapot(0.3);  //紅色茶壺
               glPushMatrix();
                      ///掛上茶壺嘴
                   glTranslatef(0.5, 0.14 , 0 ); ///3. 把整個樓下東西,移到紅色茶壺嘴
                   glRotatef( mouseX, 0 , 0 , 1 );  /// 2.會轉動
                   glTranslatef(0.41,-0.05,0); /// 1. 把手移到轉盤中心
                   glColor3f(1,1,1); glutSolidTeapot(0.3);    //白色茶壺

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



尚未轉動前

成功轉動






沒有留言:

張貼留言