2022年3月8日 星期二

雪⛄電腦圖學筆記03~

2022/3/8 第三周 

1. 下載/執行範例 

(1)  https://jsyeh.org/3dcg10/ ,下載win32、data 並且解壓縮

(2)  執行transformation.exe : 右上角按右鍵可以換模型、正下方拉滑鼠上下可以調整數值

2. 寫上周的茶壺成績

    #include <GL/glut.h>

    void display()

    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glColor3f(1,1,0); ///黃色

        glutSolidTeapot(0.3); ///茶壺

        glutSwapBuffers();

    }

    int main(int argc, char** argv)

    {

        glutInit(&argc, argv);

        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

        glutCreateWindow("week03 移動");

        glutDisplayFunc(display);

        glutMainLoop();

    }

3. 讓黃色茶壺保持在右上角不會一直移動

    #include <GL/glut.h>

    void display()

    {

         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glPushMatrix(); ///備份矩陣(備份舊的位置)  ///移動會累積,因此他會修改矩陣

            glTranslatef(0.5,0.5,0); ///茶壺位置 : 右上角

            glColor3f(1,1,0); ///顏色

            glutSolidTeapot(0.3); ///茶壺

        glPopMatrix(); ///還原矩陣(還原舊的矩陣)

        glutSwapBuffers();

    }

     int main(int argc, char** argv)

    {

        glutInit(&argc, argv);

        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

        glutCreateWindow("week03 移動");

        glutDisplayFunc(display);

        glutMainLoop();

    }


4. 在四個方位上各產生出一個茶壺

    #include <GL/glut.h>

    void myTeapot(float x,float y)

    {

        glPushMatrix(); ///備份矩陣(備份舊的位置)  ///移動會累積,因此他會修改矩陣

            glTranslatef(x,y,0); ///右上角

            glColor3f(1,1,0); ///顏色

            glutSolidTeapot(0.3); ///茶壺

        glPopMatrix(); ///還原矩陣(還原舊的矩陣)

    }

    void display()

    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        myTeapot(0.5,0.5);   ///右上

        myTeapot(0.5,-0.5);  ///右下

        myTeapot(-0.5,-0.5); ///左下

        myTeapot(-0.5,0.5);  ///左上

        glutSwapBuffers();

    }

    int main(int argc, char** argv)

    {

        glutInit(&argc, argv);

        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

        glutCreateWindow("week03 移動");

        glutDisplayFunc(display);

        glutMainLoop();

    }



5. 用滑鼠控制茶壺的位置

    #include <GL/glut.h>

    float mouseX=0,mouseY=0;  ///new

    void myTeapot(float x,float y)

    {

        glPushMatrix();///備份矩陣(備份舊的位置)

            glTranslatef(x,y,0);///右上角

            glColor3f(1,1,0);///顏色

            glutSolidTeapot(0.3);///茶壺

        glPopMatrix();///還原矩陣(還原舊的矩陣)

    }

    void display()

    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        myTeapot((mouseX-150)/150.0,-(mouseY-150)/150.0); ///new

        glutSwapBuffers();

    }

    void mouse(int button, int state, int x,int y)///new

    {

        mouseX=x; mouseY=y;

    }

    int main(int argc, char** argv)

    {

        glutInit(&argc, argv);

        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

        glutCreateWindow("week03 移動");

       glutDisplayFunc(display);

        glutMouseFunc(mouse); ///new 滑鼠事件

        glutMainLoop();

    }

6. 印出滑鼠的座標位置

    #include <GL/glut.h>

    #include <stdio.h> ///new: printf()印東西用的

    float mouseX=0,mouseY=0;

    void myTeapot(float x,float y)

    {

        glPushMatrix();///備份矩陣(備份舊的位置)

            glTranslatef(x,y,0);///右上角

            glColor3f(1,1,0);///顏色

            glutSolidTeapot(0.1); ///new: 茶壺變小

        glPopMatrix();///還原矩陣(還原舊的矩陣)

    }

    void display()

    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        myTeapot((mouseX-150)/150.0,-(mouseY-150)/150.0);

        glutSwapBuffers();

    }

    void mouse(int button, int state, int x,int y)

    {

        printf("%d %d %d %d\n",button,state,x,y); ///new

        mouseX=x; mouseY=y;

    }

    int main(int argc, char** argv)

    {

        glutInit(&argc, argv);

        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

        glutCreateWindow("week03 移動");

        glutDisplayFunc(display);

        glutMouseFunc(mouse);///滑鼠事件

        glutMainLoop();

    }








沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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