若先寫旋轉再寫位移則會以原位置為中心旋轉,反之則以自身為中心旋轉
![]() |
| 以原位置為中心 |
![]() |
| 以自身為中心 |
其原理如下
越靠近BeginEnd的越優先,先縮放、再旋轉縮放後的物件、最後移動轉過的物件
程式碼:
#include <GL/glut.h>
#include <math.h>
float angle=0;///Now: 讓茶壼自動轉動的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.45,0.1,0);
glRotatef(angle, 0,0,1);
glTranslatef(0.45,-0.1,0);
glutSolidTeapot(0.3);
glPushMatrix();///Now:備份矩陣
glTranslatef(0.45,0.2,0);///移到要放的位置
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.1,0);///對齊轉軸
glutSolidTeapot(0.3);
glPopMatrix();///Now:還原矩陣
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
angle=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week05 TRT");
glutDisplayFunc(display);
glutMotionFunc(motion);///Now:
glutMainLoop();
}
#include <math.h>
float angle=0;///Now: 讓茶壼自動轉動的角度
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.45,0.1,0);
glRotatef(angle, 0,0,1);
glTranslatef(0.45,-0.1,0);
glutSolidTeapot(0.3);
glPushMatrix();///Now:備份矩陣
glTranslatef(0.45,0.2,0);///移到要放的位置
glRotatef(angle, 0,0,1);///Now:轉動
glTranslatef(0.45,-0.1,0);///對齊轉軸
glutSolidTeapot(0.3);
glPopMatrix();///Now:還原矩陣
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
angle=x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week05 TRT");
glutDisplayFunc(display);
glutMotionFunc(motion);///Now:
glutMainLoop();
}



沒有留言:
張貼留言