網址 jsyeh.org/3dcg10
下載 windows.zip data.zip glut.dll
解壓縮後執行Transformation.exe
觀察 Translate(移動)和Rotate(旋轉)先後順序有何不同
(右鍵swap可以交換程式碼順序)
NO.02
程式碼讀取順序 ( 下至上 ) 所造成的影響 :
glTranslatef() /// 移動 旋轉 (縮放 (車子))
glRotatef() /// 旋轉 (縮放 (車子))
glScalef() /// 縮放 (車子)
glBegin() /// 車子
NO.03
旋轉的茶壺
#include<GL/glut.h>
float angle=0;///旋轉角度
void motion(int x,int y)///mouse motion拖著移動時,會叫motion()
{
angle=x;///老師利用上周比較差的方法,直接把mouse座標變成轉動角度
glutPostRedisplay();///請畫面重製
}
void display()///畫面要display時呼叫這個函式
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清洗畫面
glPushMatrix();///備份矩陣(乾淨的矩陣)
glRotatef(angle,0,0,1);///rotate/translate/scale都會把矩陣改變
glTranslatef(0.45,-0.08,0);///移動茶壺的柄至畫面中心
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣(乾淨的矩陣)
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05163046");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
NO.04
#include<GL/glut.h>
float angle=0;///旋轉角度
void motion(int x,int y)///mouse motion拖著移動時,會叫motion()
{
angle=x;///老師利用上周比較差的方法,直接把mouse座標變成轉動角度
glutPostRedisplay();///請畫面重製
}
void display()///畫面要display時呼叫這個函式
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清洗畫面
glPushMatrix();///備份矩陣(乾淨的矩陣)
glutSolidTeapot(0.3);///NEW 放在中間
glPushMatrix();///NEW 上手臂
glTranslatef(0.5,0.15,0);///NEW 把會旋轉的茶壺放到右上角
glRotatef(angle,0,0,1);///NEW 使其對整個畫面坐旋轉
glTranslatef(0.45,-0.08,0);///移動茶壺的柄至畫面中心
glutSolidTeapot(0.3);
glPushMatrix();///NEW 下手肘
glTranslatef(0.5,0.15,0);
glRotatef(angle,0,0,1);
glTranslatef(0.45,-0.08,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();///還原矩陣(乾淨的矩陣)
glPushMatrix();///NEW 上手臂
glTranslatef(-0.5,0.15,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.45,-0.08,0);
glutSolidTeapot(0.3);
glPushMatrix();///NEW 下手肘
glTranslatef(-0.5,0.15,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.45,-0.08,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();///還原矩陣(乾淨的矩陣)
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05163046");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言