1. 主題:階層移動2. 旋轉 Rotate3. 實作:T-R-T4. 複習:矩陣程式碼:
glPushMatrix(); // 備份矩陣glRotatef(angle,x,y,z); //旋轉glTranslatef(x,y,z); //移動glScalef(x,y,z); //縮放glBegin(GL_POLYGON); //開始畫glNormal3f(nx,ny,nz); //打光法向量glTexCoord2f(tx,ty); //貼圖座標glColor3f(r,g,b); //顏色glVertex3f(x,y,z); //頂點glEnd(); //結束畫glPopMatrix(); //還原矩陣
2.理解程式碼
3.茶壺旋轉
自轉
#include <GL/glut.h>
float mouseX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glRotatef(mouseX, 0,0,1);
glTranslatef(0.5, 0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
mouseX = x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("TRT robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
公轉
#include <GL/glut.h>
float mouseX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(mouseX, 0,0,1);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5, 0.14,0);///白色茶壺置於紅色茶壺的壺嘴
glRotatef(mouseX, 0,0,1);///白色茶壺轉動
glTranslatef(0.41, -0.05,0);///把手定位於中心
glColor3f(1,1,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
mouseX = x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("TRT robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言