glRotatef(...);
glScalef();
glBegin();
車子會在原地旋轉(自轉)
glRotatef(...);
glTranslatef(...);
glScalef();
glBegin();
車子會繞著中心軸旋轉(公轉)
#include <GL/glut.h>
float angle=0;///旋轉角度
void motion(int x, int y)///mouse motion拖著移動時,會叫motion()
{
angle=x;///老師用上週較差的方法,直接把mouse座標變轉動角度
glutPostRedisplay();///畫面重畫
}
void display()///畫面要display時呼叫這個函式
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///先清畫面
glPushMatrix();///備份矩陣(乾淨的矩陣)
glutSolidTeapot(0.3);///放在中間的茶壺
glTranslatef(0.5, 0.15, 0);///把會旋轉的茶壺,柄放到右上角
glRotatef(angle, 0,0,1);///把茶壺對畫面中心做旋轉
glRotatef(angle, 0,0,1);///rotate/translate/scale都會把矩陣改變
glTranslatef(0.45, -0.08, 0);///移動:讓茶壺的柄,放到畫面中心
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣(乾淨的矩陣)
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week05 TPT");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}




沒有留言:
張貼留言