1_登入Zuvio回答問題
登入Zuvio
進入電腦圖學的課程回答尚未回答的週次
2_嘗試範例檔Transformation.exe
3_移動Translate (利用程式碼)
下載freeglut(老師提供的版本):
從FB 2017電腦圖學的社團中,可以找到檔案並且下載下來,待會會使用到
開啟CodeBlocks,點選File/New/Project... 新增一個新的專案
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();
}
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軸當成轉軸,觀察車子轉動方向
挖哈哈
回覆刪除繼儀妳做什麼?
刪除