了解移動和選轉順序不同時所造成的公轉和自轉
(1) 使用上週的 Transformation
(2) 在此程式碼使用到結合律必須由下往上看順序為:
畫一台車 -> 放大整台車 -> 選轉整台車 -> 移動整台車
(3) 此例子的效果為自轉,因為是先選轉後才移動(由下往上看)
(4) 在下方的 Command manipulation window 上按右鍵選(Swap translate/rotate) 調換 glTranslatef 和 glRotatef 的順序
(5) 在此程式碼使用到結合律必須由下往上看順序為:
畫一台車 -> 放大整台車 -> 移動整台車 -> 選轉整台車
(6) 此例子的效果為公轉,因為是先移動後才選轉(由下往上看),會繞著中心做旋轉
讓球在中心自轉
(1) 依照前幾週的步驟新增一個 glut 專案
(2) 更改 display 函式裡的程式碼
Code
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glTranslated(0,0,-6); //把球移動到畫面的中心
glRotated(a,0,0,1); //讓球沿著z軸自轉
glutSolidSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidTorus(0.2,0.8,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(-2.4,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireTorus(0.2,0.8,slices,stacks);
glPopMatrix();
glutSwapBuffers();
}
(3) 效果:
讓球繞著中心做公轉
(1) 更改 display 函式裡的程式碼
Code
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glRotated(a,0,0,1); //讓球沿著z軸做公轉
glTranslated(-2.4,1.2,-6); //先把球移動到左上角
glutSolidSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidTorus(0.2,0.8,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(-2.4,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireSphere(1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireCone(1,1,slices,stacks);
glPopMatrix();
glPushMatrix();
glTranslated(2.4,-1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutWireTorus(0.2,0.8,slices,stacks);
glPopMatrix();
glutSwapBuffers();
}
(3) 效果:
讓茶壺沿著中心做旋轉
Code
#include <GL/glut.h>
float angle=0; ///Now: 讓茶壼自動轉動的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///Now:備份矩陣
glRotatef(angle, 0,0,1); ///Now:轉動樓下全部
glTranslatef(0.45,-0.1,0); //把茶壺柄移到中間
glutSolidTeapot(0.3);
glPopMatrix(); ///Now:還原矩陣
glutSwapBuffers();
}
void motion(int x, int y)
{
angle=x; ///Now: 改變角度
glutPostRedisplay(); ///Now: 請 重畫畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week05 TRT");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
茶壺手臂
Code
#include <GL/glut.h>
float angle=0;///Now: 讓茶壼自動轉動的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///Now:備份矩陣
glColor3f(1,0,0); glutSolidTeapot(0.3); //中間的紅色茶壺
glPushMatrix();//右手臂
glTranslatef(0.4,0.13,0); //最後掛上去
glRotatef(angle, 0,0,1);///Now:轉動樓下全部
glTranslatef(0.45,-0.1,0);//把茶壺柄移到中間
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPushMatrix();//右手肘
glTranslatef(0.4,0.13,0); //最後掛上去
glRotatef(angle, 0,0,1);///Now:轉動樓下全部
glTranslatef(0.45,-0.1,0);//把茶壺柄移到中間
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左手臂
glTranslatef(-0.4,0.13,0); //最後掛上去
glRotatef(angle, 0,0,1);///Now:轉動樓下全部
glTranslatef(-0.45,-0.1,0);//把茶壺柄移到中間
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPushMatrix();//左手肘
glTranslatef(-0.4,0.13,0); //最後掛上去
glRotatef(angle, 0,0,1);///Now:轉動樓下全部
glTranslatef(-0.45,-0.1,0);//把茶壺柄移到中間
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPopMatrix();///Now:還原矩陣
glutSwapBuffers();
}
void motion(int x, int y)
{
angle=x;///Now: 改變角度
glutPostRedisplay();///Now: 請 重畫畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week05 TRT");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}








沒有留言:
張貼留言