2019年3月14日 星期四

Week04_Summer

week04目標:
      1、主題:移動Translate
      2、示範:glTranslatef(x,y,z)




下載"win32"、"data"、"glut32.dll"三個檔案並解壓縮放入對應資料夾中



 02 打開windows.zip - Transformation.exe 中的模型




 04 至CodeBlock建立一個GLUT的project
 04 茶壺透過程式碼移動
讓茶壺透過程式碼移動:
#include <GL/glut.h>
void display()
{
    glPushMatrix();  ///備份矩陣
        glTranslatef(0.3,0.3,0);   ///移動(會改矩陣)
        glutSolidTeapot(0.3);
    glPopMatrix();   ///還原矩陣
    glutSwapBuffers();
}
int main(int argc, char**argv)
{

     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
     glutCreateWindow("week04");
     glutDisplayFunc(display);
     glutMainLoop();
}


 05 茶壺可跟著滑鼠移動


讓茶壺跟著滑鼠移動的程式碼:
#include <GL/glut.h>
float x=0,y=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  ///清圖
    glPushMatrix();  ///備份矩陣
        glTranslatef((x-150)/150.5,-(y-150)/150.0,0);   ///滑鼠MOTION
        glutSolidTeapot(0.3);
        ///glVertex2f ((x-150)/150.0,-(y-150)/150.0,0);
    glPopMatrix();   ///還原矩陣,才不會在下次有舊的殘值
    glutSwapBuffers();
}
void motion(int nowX,int nowY) ///滑鼠MOTION
{///滑鼠MOTION
    x=nowX;y=nowY;
    display();
}///滑鼠MOTION
int main(int argc, char**argv)
{

     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
     glutCreateWindow("week04");
     glutDisplayFunc(display);
     glutMotionFunc(motion);   ///滑鼠MOTION
     glutMainLoop();
}


****洩題:

      (1)glPushMatrix();   備份矩陣
      (2)glPopMatrix();     還原矩陣
      (3)glTranslatef();      滑鼠motion
      (4)glBegin(GL_POLYGON);     開始畫
           glEnd();                結束畫
      (5)glColor3f(1,0,0);   顏色
      (6)glVertex2f(1,0,0);  頂點

沒有留言:

張貼留言