2019年3月21日 星期四

Week05_Summer

  今日目標  
1、主題:旋轉Rotation
2、實作:glRotatef()
3、滑鼠轉、自動轉
4、滑鼠事件:把大象放入冰箱( 滑鼠按住-拖曳-滑鼠彈開 )



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



 02 再解壓縮data資料夾並放入windows資料夾中
把glut32.dll也放入windows資料夾中

複製libfreeglut.a且重新命名為libglut32.a




 03 打開windows.zip - Transformation.exe 中的模型,並且試一下glRotatef()

↑旋轉軸在x軸向上時glRotate(50.00,0.00,1.00,0.00)=逆時針轉動
↓旋轉軸在x軸向下時glRotate(50.00,0.00,-1.00,0.00)=順時針轉動
旋轉軸向右下時glRotate(50.00,1.00,1.00,0.00)=向右下方旋轉
旋轉軸在y軸向前時glRotate(50.00,1.00,0.00,0.00)=向前轉(敬禮)




 04 到CodeBlock建一個新的GLUT檔案
打入可以讓物件旋轉的程式碼
並觀察轉動方向

讓茶壺自動轉動的程式碼:

#include <GL/glut.h>
float angle=0;   ///今天新教的!!!
void display()
{     ///沒有使用glClear()去清畫面,會有殘影
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();      ///Push備份保護Matrix
        glRotatef(angle,0,1,0);      ///今天新教的!!! 對y軸,轉angle度
        glutSolidTeapot( 0.3 );
        glPopMatrix();    ///Pop還原Matrix
     glColor3f(1.00 ,1.00 ,1.00);
     glutSwapBuffers();
     angle+=3;   ///今天新教的!!!每次執行到這行,都加一點點
}
int main(int argc, char**argv)
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
     glutCreateWindow("week05_rotate");
     glutDisplayFunc(display);
     glutIdleFunc(display);    ///今天新教的!!!閒閒沒事就重畫
     glutMainLoop();
}



 05 讓滑鼠可以控制茶壺的旋轉方向

讓滑鼠可以控制茶壺旋轉的程式碼:

#include <GL/glut.h>
float angle=0;   ///今天教的!!!
void display()
{     ///沒有使用glClear()去清畫面,會有殘影
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();      ///Push備份保護Matrix
        glRotatef(angle,0,1,0);      ///今天教的!!! 對y軸,轉angle度
        glutSolidTeapot( 0.3 );
        glPopMatrix();    ///Pop還原Matrix
     glColor3f(1.00 ,1.00 ,1.00);
     glutSwapBuffers();
     ///angle+=3;   ///今天教的!!!
}
void motion(int x,int y)///現在新加的!!!
{///當你的mouse去drag做motion動作時,
    angle=x;   ///現在新加的!!!
    display();   ///現在新加的!!!
}
int main(int argc, char**argv)
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
     glutCreateWindow("week05_rotate");
     glutDisplayFunc(display);
     glutIdleFunc(display);    ///今天教的!!!
     glutMotionFunc(motion);   ///現在新加的!!!
     glutMainLoop();
}



 06 用滑鼠 按下-拖曳-彈開 控制茶壺旋轉

用滑鼠 按下-拖曳-彈開 控制茶壺旋轉並記錄:
#include <GL/glut.h>
float angle=0;   ///
void display()
{     ///沒有使用glClear()去清畫面,會有殘影
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();      ///Push備份保護Matrix
        glRotatef(angle,0,1,0);      /// 對y軸,轉angle度
        glutSolidTeapot( 0.3 );
        glPopMatrix();    ///Pop還原Matrix
     glColor3f(1.00 ,1.00 ,1.00);
     glutSwapBuffers();
     ///angle+=3;
}
void motion(int x,int y)///現在新加的!!!
{///當你的mouse去drag做motion動作時,
    angle=x;       ///(2)mouse移動
    display();
}
#include <stdio.h>
void mouse(int button,int state,int x,int y)///現在新加的!!!
{///(1)mouse 按下去   (3)mouse放開來
    printf("%d %d %d %d\n",button,state,x,y);   ///現在新加的!!!
}
int main(int argc, char**argv)
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
     glutCreateWindow("week05_rotate");
     glutDisplayFunc(display);
     glutIdleFunc(display);
     glutMotionFunc(motion);
     glutMouseFunc(mouse);///現在新加的!!!
     glutMainLoop();
}



****洩題:

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

沒有留言:

張貼留言