(1)開起上周的Transformation檔案
(2)點選右鍵swap能夠切換Rotate&Translate的位置
(3)觀察其公轉、自轉的差異
*觀察時有個小技巧:從裡到外觀察,愈裡面的愈先被執行*
![]() |
| 先轉動後移動,所以此為自轉 |
![]() |
| 先移動後轉動,所以此為公轉 |
(1)先開啟openGL
(2)找到第50~52行的程式碼,可以先試看看各個程式碼的意思
Translated:位置;Rotated:旋轉
(3)先把除了球體外其他顏色變成無顏色
(4)將Rotate和Translate的程式碼互換即可讓球體公轉
![]() |
| 運用第一個範例的技巧 |
(1)先叫出茶壺的程式碼:
#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:轉動
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");
float angle=0;///Now: 讓茶壼自動轉動的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///Now:備份矩陣
glRotatef(angle, 0,0,1);///Now:轉動
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");
#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.10,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);///Now:
glutMainLoop();
}
(3)增加一個茶壺,使它為中心點
#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);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.10,0);
glColor3f(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);///Now:
glutMainLoop();
}
(4)再增加translatef把綠茶壺位置改到紅茶壺嘴口
*此方法就是T-R-T的轉動,T是Translatef,R是Rotate*
#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);
glTranslatef(0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.10,0);
glColor3f(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);///Now:
glutMainLoop();
}
(5)應用
#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();///Now:備份矩陣
glTranslatef(0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPushMatrix();///Now:備份矩陣
glTranslatef(0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPopMatrix();///Now:還原矩陣
glPopMatrix();///Now:還原矩陣
glPushMatrix();///Now:備份矩陣
glTranslatef(-0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(-0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPushMatrix();///Now:備份矩陣
glTranslatef(-0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(-0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPopMatrix();///Now:還原矩陣
glPopMatrix();///Now:還原矩陣
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);///Now:
glutMainLoop();
}
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();///Now:備份矩陣
glTranslatef(0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPushMatrix();///Now:備份矩陣
glTranslatef(0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPopMatrix();///Now:還原矩陣
glPopMatrix();///Now:還原矩陣
glPushMatrix();///Now:備份矩陣
glTranslatef(-0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(-0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPushMatrix();///Now:備份矩陣
glTranslatef(-0.4,0.13,0);
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(-0.45,-0.10,0);
glColor3f(0,1,0); glutSolidTeapot(0.3);
glPopMatrix();///Now:還原矩陣
glPopMatrix();///Now:還原矩陣
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);///Now:
glutMainLoop();
}
![]() |
| 能連接成像機械手臂般 |













沒有留言:
張貼留言