作業一
1.複習Week04,先到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data、windows、glut32.dll
2.解壓縮到windows資料夾(如下圖)
下載data.zip(解壓縮到桌面\移到window資料夾裡\data\mtlobj)
window.zip(解壓縮到桌面並建一個新的資料夾window\window\Tranformation.exe)
glut32.dll(解壓縮到桌面\移到window資料夾裡\glut32.dll)

3.先打好茶壺的程式碼,在多加一行讓茶壺不要在正中央
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); //////備份 matrix
glTranslatef(0.5,0,0); ///茶壺所在的座標X、Y、Z(今天重要的程式碼)
glutSolidTeapot(0.3);
glPopMatrix(); ///回復 matrix
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160116 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
///glutMotionFunc(motion);
glutMainLoop();
}

4.但是這樣太麻煩了,需要一直改程式碼,所以多加Motion那段讓我們可以利用滑鼠點選移動茶壺
移動(Translate)
#include <GL/glut.h>float mouseX=0 ,mouseY=0; ///設滑鼠座標X、Y
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清空畫面
glPushMatrix(); ///備份 matrix
glTranslatef(mouseX,mouseY,0); ///茶壺所在的座標X、Y、Z(今天重要的程式碼)
glutSolidTeapot(0.3);
glPopMatrix(); ///回復 matrix
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX = (x-150)/150.0; ///滑鼠移動時的座標X x/300.0
mouseY = -(y-150)/150.0; ///滑鼠移動時的座標y(要為負) y/300.0
glutPostRedisplay();///貼個3M Post便利貼告訴GLUT要重畫畫面Re-display
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160116 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}

P.S mouseX、mouseY座標示意圖

小精靈

5.把Translatef改成Scalef,讓茶壺可以自由的拉高矮胖瘦
縮放(Scale)
#include <GL/glut.h>
float mouseX=0 ,mouseY=0; ///設滑鼠座標X、Y
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清空畫面
glPushMatrix(); ///備份 matrix
glScalef(mouseX,mouseY,0); ///茶壺所在的座標X、Y、Z(初始沒看到茶壺是因為MouseX、MouseY設定值為0)
glutSolidTeapot(0.3);
glPopMatrix(); ///回復 matrix
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX = (x-150)/150.0; ///滑鼠移動時的座標X x/300.0
mouseY = -(y-150)/150.0; ///滑鼠移動時的座標y(要為負) y/300.0
glutPostRedisplay(); ///貼個3M Post便利貼告訴GLUT要重畫畫面Re-display
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160116 Translate Rotate Scale");
glutDisplayFunc(display);
///glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
剛開始執行時,出始值為0沒有茶壺

滑鼠拉放後出現的茶壺


6.Zuvio : http://irs.zuvio.tw/student/clickers/37584
(複習上課內容,回答問卷)
沒有留言:
張貼留言