用程式碼寫出會旋轉的茶壺
程式碼:
#include <GL/glut.h>
float rotX=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotX,0,1,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
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("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
課堂問題
Q1; 什麼是glPushMatrix();
glPopMatrix();
A:
和資料結構的rush pop同意義,push為儲存資料 pop則為提出資料互為相反的涵式。
Q2: 什麼是 Buffers
Double
A:
buffer為一個電腦繪圖緩衝區,因怕圖形呈現時單一呈現不完整,所以在圖片顯示前會先在緩衝區繪製好全部圖形再一起承呈現。
double-buffer即為兩次緩衝,更為保險。
可用程式調整茶壺上下左右旋轉 並放大視窗
程式碼:
#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);
glutInitWindowSize(400,400);glutInitWindowPosition(600,0);
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}





沒有留言:
張貼留言