2022年3月15日 星期二

Ru的電腦圖學筆記>


💩Step1 主題:Rotate旋轉
    1.到 https://jsyeh.org/3dcg10/ 下載
       -windows.zip => 解壓縮至 windows =>下載/windows/Transformation
        -data.zip  => 點開/data/丟進去 windows 的資料夾
    2.打開 Transformation.exe,從 glRotate 更改,第1項是旋轉,第2、3、4項分別是(x,y,z),
        舉例:當y=1時,圖中的人會以y軸為主旋轉,如圖
            旋轉後的樣子

    3.打開 Codeblocks 建立新的 GLUT 專案
        -檔名:week04_rotate
        -Build&Run 會出現一個按一次旋轉90度的白色茶壺
        #include <GL/glut.h>
        void display()
        {
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glRotatef(90,0,0,1); ///以z軸為主做90度的旋轉
                glutSolidTeapot(0.3);
            glPushMatrix();
            glutSwapBuffers();
        }
        int main(int argc, char** argv)
        {
            glutInit( &argc, argv );
            glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
            glutCreateWindow("第04週的程式哦!");
            glutDisplayFunc(display);
            glutMainLoop();
        }
    4.讓茶壺旋轉:滑鼠點選往上拖曳的時候,茶壺會往順時鐘方向旋轉;滑鼠往下的時候,
        會逆時針旋轉!
       #include <GL/glut.h>
        float angle=0;
        void display()
        {
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glRotatef(angle,0,0,1);
                glutSolidTeapot(0.3);
            glPushMatrix();
            glutSwapBuffers();
        }
        void motion(int x,int y)///mouse按一下改變,motion拖曳改變
        {
            angle=x;
            display();
        }
        int main(int argc, char** argv)
        {
            glutInit( &argc, argv );
            glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
            glutCreateWindow("week04 Rotate");
            glutDisplayFunc(display);
            glutMotionFunc(motion);
            glutMainLoop();
        }


    5.讓滑鼠放開的時候不會跑掉
      #include <GL/glut.h>
        float angle=0, oldx=0;
        void display()
        {
            glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glRotatef(angle,0,0,1);
                glutSolidTeapot(0.3);
            glPushMatrix();
            glutSwapBuffers();
        }
        void motion(int x,int y)
        {
            angle += (x-oldx);
            oldx=x;
            display();
        }
        void mouse(int button, int state, int x, int y)
        {
            oldx=x;
        }
        int main(int argc, char** argv)
        {
            glutInit( &argc, argv );
            glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
            glutCreateWindow("week04 Rotate");
            glutDisplayFunc(display);
            glutMotionFunc(motion);
            glutMouseFunc(mouse);
            glutMainLoop();
        }

完成!!💘💘💘💘💘💘💘

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...