2019年3月14日 星期四

呼呼呼 電腦圖學的世界 Week-04

1.主題:移動

2.範例:glTranslatef(x,y,z)

與上次一樣點擊http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/網頁下載data、win32、glut32.dll檔案
將win32與data壓縮檔解壓縮  data資料夾與glut32.dll檔案丟進windows資料夾裡面


這一次開啟Transformation.exe檔







一樣可以使用滑鼠調整位置角度大小

glTranslatef(x,y,z)為移動
x軸為左右移動
y軸為上下移動
z軸為前後移動

glRotatef(x,y,z)為旋轉
glScalef(x,y,z)為縮放大小



先開啟glut程式檔
先進入freeglut windows   這個網址點選

freeglut 3.0.0 MinGW Package 下載


下載完後複製libfreeglut.a檔改名為libglut32
在codeblock點選project後開啟glut後選取C:Users\user\Desktop\freeglut為路徑
將舊有的程式碼刪除
改成
#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();
}
印出一個固定的茶壺


若想要讓茶壺移動須更改程式碼為
#include <GL/glut.h>
float x=0,y=0;///滑鼠motion
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((x-150)/150.0,-(y-150)/150.0,0);///滑鼠motion
        glutSolidTeapot(0.3);
        glPopMatrix();   ///還原矩陣,才不會下次有瑕疵的殘值
        glutSwapBuffers();


}
void motion(int nowX,int nowY)  ///滑鼠motion
{ ///滑鼠motion
    x=nowX;y=nowY; ///滑鼠motion
    display(); ///滑鼠motion 馬上更新
} ///滑鼠motion
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04");
    glutDisplayFunc(display);
    glutMotionFunc(motion);///滑鼠motion
    glutMainLoop();
}









沒有留言:

張貼留言