gluPerspective(90.0f,(GLfloat)width/(GLfloat)height,1.0f,100.0f);
void gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear, GLdouble zFar);
其操作是创建一个对称的透视投影矩阵,并且用这个矩阵乘以当前矩阵。
参数fovy定义视野在X-Z平面的角度,范围是[0.0, 180.0];
参数aspect是投影平面宽度与高度的比率;
参数zNear和zFar分别是远近裁剪面沿Z负轴到视点的距离,它们总为正值
gluPerspectiv确定显示物体的3D空间(可以称为3D显示空间),即红色楔行空间内。
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
画一个三角形和一个正方形,大小整合和整个窗口匹配,这里主要是学习两个函数中参数的作用。
main.c
<span style="font-size:14px;">#include <stdlib.h> #include <stdio.h> #include <math.h> #include "GL/glut.h" int width = 500; int height = 500; // Called to draw scene void RenderScene(void) { // 1. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); //调节透视 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.0f,(GLfloat)width/(GLfloat)height,1.0f,100.0f); //观察点设定 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0); // 2. glColor3f(1.0, 0.0, 0.0); glTranslatef(-5.0f,0.0f,0.0f); glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 10.0f, 0.0f); glVertex3f(-5.0f,-10.0f, 0.0f); glVertex3f( 5.0f,-10.0f, 0.0f); glEnd(); glTranslatef(10.0f,0.0f,0.0f); glBegin(GL_QUADS); glVertex3f(-5.0f, 10.0f, 0.0f); glVertex3f( 5.0f, 10.0f, 0.0f); glVertex3f( 5.0f,-10.0f, 0.0f); glVertex3f(-5.0f,-10.0f, 0.0f); glEnd(); // 3. glutSwapBuffers(); } void initialize() { glClearColor(0.0, 0.0, 1.0, 1.0); glClearDepth(1.0);// [0.0,1.0] glShadeModel(GL_SMOOTH); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(width, height); // default value: 300x300 glutInitWindowPosition(100,100); // default value: (0,0) glutCreateWindow("OpengGL Example"); //glutReshapeFunc(ChangeSize); //glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); initialize(); glutMainLoop(); return 0; }</span>
Makefile
<span style="font-size:14px;">CC=gcc SRC=main.c OBJS=${SRC:%.c=%.o} TARGET=test RM=rm LIB_PATH= CFLAGS= LIBFLAGS= LIBS=-lGLU -lglut .PHONY:$(TARGET) $(TARGET): $(CC) $(SRC) -o $@ $(CFLAGS) $(LIBFLAGS) $(LIBS) clean: @$(RM) $(OBJS) $(TARGET) </span>