2017年3月21日 星期二

Week05 劉育榕

WEEK05  重點週 

主題: (1)移動 Translate
          (2)旋轉 Rotate
          (3)縮放 Scale
實作 : PushMatrix , PopMatrix
----------------------------------------------------------------------------------------------------------

課堂作業一



下載 到桌面 :  data.zip /  windows.zip / glut32.dll  


打開Transformation.exe 檔案 

  
 完成圖


課堂作業二: 實作  

用程式碼移動茶壺:

#include <GL/glut.h>
void display()
{
    glPushMatrix();  // 備份起來
        glTranslatef(0,1,0);            //移動  f>>為浮點數
        glutSolidTeapot(0.3);  //茶壺
    glPopMatrix();   //還原回去
    glutSwapBuffers();
}
int main( int argc, char **argv )
{
    glutInit(&argc ,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("04160533 translate rotate scale");
    glutDisplayFunc(display);
    glutMainLoop();
}             
                 叫出茶壺程式碼 


加入移動程式碼

 完成圖

 使用滑鼠移動茶壺

#include <GL/glut.h>
float mouseX=0,mouseY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  //清畫面 殘影
    glPushMatrix();  // 備份起來
        glTranslatef(mouseX,mouseY,0);  // 跟著滑鼠移動
        glutSolidTeapot(0.3);
    glPopMatrix();   //還原回去
    glutSwapBuffers();
}
void motion(int x,int y)
{
    mouseX=(x-150)/150.0;   //扣-150 後要/150.0 是因為所得的直要介於-1~1之間   

    mouseY= - (y-150)/150.0;  // Y 值前要加負號 >>因為上下位址顛倒  
    glutPostRedisplay();
}

int main( int argc, char **argv// *argv[ ] == **argv
{
    glutInit(&argc ,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("04160533 translate rotate scale");
    glutDisplayFunc(display);
    glutMotionFunc(motion);  //使用滑鼠移動呼叫函式
    glutMainLoop();
}
 
老 師 上 課 圖 檔

 
自己試做


完成圖


 使用滑鼠縮放茶壺 :

       把上一步的程式碼的glTranslatef(mouseX,mouseY,0);  // 跟著滑鼠移動 改成
       glScalef(mouseX,mouseY,0);   // 跟著滑鼠縮放

  
完成圖

使用滑鼠旋轉茶壺 :

      把上一步的程式碼的glScalef(mouseX,mouseY,0);   // 跟著滑鼠縮放 註解後
      改成glRotatef ( rotX,0,0,1 );

-------------------------------------------------------------------------------------------------------------

#include <GL/glut.h>
float mouseX=0,mouseY=0 , rotX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  //清畫面 殘影
    glPushMatrix();  // 備份起來
        glScalef(mouseX,mouseY,0);   // 跟著滑鼠縮放
        glRotatef ( rotX ,0 , 0 , 1 )  ;     //以z軸旋轉
        glutSolidTeapot(0.3);
    glPopMatrix();   //還原回去
    glutSwapBuffers();
}
void motion(int x,int y)
{
    mouseX=(x-150)/150.0;
   //扣-150 後要/150.0 是因為所得的直要介於-1~1之間   

    mouseY= - (y-150)/150.0;  // Y 值前要加負號 >>因為上下位址顛倒  
    rotX= x ;
    glutPostRedisplay();
}

int main( int argc, char **argv// *argv[ ] == **argv
{
    glutInit(&argc ,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("04160533 translate rotate scale");
    glutDisplayFunc(display);
    glutMotionFunc(motion);  //使用滑鼠移動呼叫函式
    glutMainLoop();



       
完成圖 


課堂作業三: 試改Transformation.exe 檔案的(x,y,z)軸


z軸旋轉

若值為正數則圖形會往右旋轉 →



值為負數則圖形往左轉 ←


完成圖

1 則留言: