2017年3月21日 星期二

Week05 嗯對,這是賴小沫的

 1_登入Zuvio回答問題

登入Zuvio
 
進入電腦圖學的課程回答尚未回答的週次 

 
依據自己的狀況回答問題

 2_嘗試範例檔Transformation.exe

下載範例檔:到http://jsyeh.org/3dcg10 ,下載data,win32,glut32.dll檔案

 
檔案處理:將windows.zip解壓縮
 
glut32.dll複製到windows資料夾中,data.zip也解壓縮複製到windows資料夾裡面
 
打開範例檔:點開一個名叫Transformation.exe的執行檔,隨意調整數值,並觀察其變化
 
移動 glTranslatef(x,y,z)
 
旋轉 glRotatef(angle,x,y,z)
 
縮放 glScalef(x,y,z)

 3_移動Translate (利用程式碼)

下載freeglut(老師提供的版本):
        從FB 2017電腦圖學的社團中,可以找到檔案並且下載下來,待會會使用到
 
 將freeglut存放位置路徑複製,以便建立一個新的GLUT專案
 
開啟CodeBlocks,點選File/New/Project... 新增一個新的專案 
 
 新增一個GLUT的專案
 
 檔案名稱與存放位置的設定,一定要記得設定,以免找不到檔案
 
 將剛剛複製的freeglut路徑貼上
 
 不須做任何的修改,按下Finish後便會新增一個GLUT專案
 
將原本的程式碼刪減後,並增加畫茶壺及移動的程式碼指令,內容如下
 
#include <GL/glut.h>

static void display(void)
{
    glPushMatrix();  //備份矩陣
    glTranslatef(0,1,0);  //中心位置移動至(0,1,0)
    glutSolidTeapot(0.3);  //畫茶壺
    glPopMatrix();  //還原矩陣
    glutSwapBuffers();  //交換繪畫記憶體,才能畫出圖形
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160223");  //視窗名稱
    glutDisplayFunc(display);
    glutMainLoop();
}
 
按下F9執行結果,會看到一個偏上的白色茶壺
 

 4_移動Translate (利用滑鼠)

延續前面的程式碼,增加motion函式,利用滑鼠拖曳的方式,移動茶壺位置

#include <GL/glut.h>
float mouseX=0,mouseY=0;
static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   //清空畫面避免殘影
    glPushMatrix();  //備份
    glTranslatef(mouseX,mouseY,0);  //中心位置移動至(mouseX,mouseY,0)
    glutSolidTeapot(0.3);
    glPopMatrix();  //還原
    glutSwapBuffers();
}

void motion(int x,int y)
{
    mouseX= (x-150)/150.0;  //變更為motion的X座標
    mouseY= -(y-150)/150.0;  //變更為motion的Y座標
    glutPostRedisplay();  //告訴電腦的GLUT要重畫畫面Redisplay
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160223"); //視窗名稱
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

按下F9執行,結果如下:
 

 5_縮放 Scale (利用滑鼠)

將剛剛的程式碼中,glTranslatef(x,y,z)改成glScalef(x,y,z)
便可利用滑鼠拖曳的方式縮放茶壺大小

#include <GL/glut.h>
float mouseX=0,mouseY=0;
static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();  //備份
    glScalef(mouseX,mouseY,0);  //依據(mouseX,mouseY,0)進行縮放
    glutSolidTeapot(0.3);
    glPopMatrix();  //還原
    glutSwapBuffers();
}
void motion(int x,int y)
{
    mouseX= (x-150)/150.0;  //變更為motion的X座標
    mouseY= -(y-150)/150.0;  //變更為motion的Y座標
    glutPostRedisplay();  //告訴電腦的GLUT要重畫畫面Redisplay
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160223");  //視窗名稱
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

按下F9執行,結果如下:

 

 6_旋轉 Rotate (利用滑鼠)

將剛剛的程式碼中,glScalef(x,y,z)改成glRotatef(angle,x,y,z)
便可利用滑鼠拖曳的方式旋轉茶壺角度

#include <GL/glut.h>
float mouseX=0,mouseY=0,rotX=0;
static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); //備份
    glRotatef(rotX,0,0,1);  //依據(角度,x,y,z)做旋轉的動作
    glutSolidTeapot(0.3);
    glPopMatrix(); //還原
    glutSwapBuffers();
}
void motion(int x,int y)
{
    mouseX=(x-150)/150.0; //變更為motion的X座標
    mouseY=-(y-150)/150.0; //變更為motion的Y座標
    rotX=x;  //旋轉角度設定
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160223"); //視窗名稱
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

按下F9執行,結果如下:

 7_利用Transformation.exe觀察旋轉結果

改變軸(x,y,z)並猜測會向何處轉動(初始狀態 v.s 執行結果)

y軸當成轉軸,觀察車子轉動方向
 
z軸當成轉軸,觀察車子轉動方向
 
x軸當成轉軸,觀察車子轉動方向
 
x,y軸當成轉軸,觀察車子轉動方向
 
y,z軸當成轉軸,觀察車子轉動方向
 
x,z軸當成轉軸,觀察車子轉動方向
 
x,y,z軸當成轉軸,觀察車子轉動方向

2 則留言: