OpenGL(Open Graphics Library,开放式图形库)是用于渲染 2D、3D 矢量图形的跨语言、跨平台的应用程序编程接口。这个接口由近350个不同的函数调用组成,用来绘制从简单的图形到复杂的三维景象。OpenGL常用于CAD、虚拟现实、科学可视化程序和电子游戏开发。
这里使用 freeglut
库进行学习
glBegin()
- GL_POINTS: individual points
- GL_LINES: pairs of vertices interpreted as individual line segments
- GL_LINE_STRIP: series of connected line segments
- GL_LINE_LOOP: same as above, with a segment added between last and fist vertives.
- GL_TRIANGLES: triples of vertices interpreted as triangles
- GL_TRIANGLE_STRIP: linked strip of triangles
- GL_TRIANGLE_FAN: inked fan of triangles
- GL_QUADS: quadruples of vertices interpreted as four-sided polygons.
- GL_QUAD_STRIP: linked strip of quadrilaterals
- GL_POLYGON: boundary of a simple, convex polygon
示例:
glBegin(GL_POINTS);
glVertex2f(100.0f, 100.0f);
glEnd();
glBegin(GL_LINES);
glVertex2f(100.0f, 100.0f);
glVertex2f(200.0f, 140.0f);
glEnd();
OpenGL transformation functions (GL_MODELVIEW)
- glTranslate(x, y, z): 沿 (x, y, z) 方向平移
- glRotate(x, y, z): 以 (x, y, z) 为轴旋转
- glScale(x, y, z): 从原点向外缩放,(x, y, z) 为各方向缩放比例
- glMatrixMode: Specify current matrix for geometric-viewing, projection, texture or colour transformations
- glLoadIdentity: Set current matrix to identity
- glLoadMatrix: Set elements of current matrix
- glMultMatrix
- glGetIntegerv: Get max stack depth or current number of matrices in the stack for selected matrix mode
- glPushMatrix: Copy the top matrix in the stack and store copy in the second stack position
- glPopMatrix: Erase top matrix in stack and move second matrix to top stack
- glPixelZoom: Specify 2D scaling parameters for raster operations
Note that the last matrix specified in the program is the first applied.
Projection Functions (GL_PROJECTION)
gluLookAt(eye_position, look_at, look_up)
: 相机和参考点的坐标,look_up
是向上向量,用来投影出 y 轴正方向。glOrtho(left, right, bottom, top, near, far)
glFrustum(left, right, bottom, top, near, far)
gluPerspective(fov, aspect, near, far)
: less often used thanglFrustum()