課堂作業1:
一樣下載windows/data/glut32.dll
了解 先旋轉後移動 與 先移動再旋轉
首先,
因程式為由下往上做執行
所以圖中為先做縮放 再做轉動 再做移動
故車子僅是移至旁邊做自體旋轉
老師比喻:整個餐桌轉盤移動旋轉
再者,
經過右鍵Swap translate/rotate改變程式
使程式執行順序改變
變成先執行縮放 再執行移動 再執行旋轉
故車子是移至旁邊繞軸旋轉
老師比喻:鍋子裡的一小塊菜 在鍋子裡的邊邊 繞軸旋轉
使茶壺能繞Y軸旋轉
------------------------------------今日程式1------------------------------------
#include <GL/glut.h>
float rotX=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //備份Matrix (Matrix裡有translate,rotate,scale的量值)
glRotatef(rotX,0,1,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix(); //還原Matrix(回到剛剛push時的樣子)
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;
display(); //glutPostRedisplay();
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); //double buffers兩倍記憶體
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion); //呼叫函式
glutMainLoop();
}
-------------------------------------------------------------------------------------
課堂作業3:
使茶壺不只能單水平旋轉或垂直旋轉
#include <GL/glut.h>
float rotX=0,rotY=0; //同時能轉X也能轉Y
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotX,0,1,0);
glRotatef(rotY,1,0,0); //轉動X軸
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;
rotY=y; //代入滑鼠y值移動量
display();
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(400,400); //視窗大小glutInitWindowPosition(700,0); //視窗跳出之位置
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}






沒有留言:
張貼留言