
理解(A) Translate
Rotate
Scale
(B) Rotate
Translate
Scale
的差別
(A)是以車子為中心自轉
(B)是以整個畫面的中間為中心點做旋轉

用滑鼠控制茶壺左右旋轉
程式碼:
#include <GL/glut.h>
float rotX=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();///備份Matrix
glRotatef(rotX, 0,1,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();///還原Matrix
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;
display();///
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello 3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
多了Y軸選轉
程式碼:
#include <GL/glut.h>
float rotX=0,rotY=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotY, 1,0,0);
glRotatef(rotX, 0,1,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x; rotY=y;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello 3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}

沒有留言:
張貼留言