glBegin

更新时间:2023-10-02 20:09

glBegin是OpenGL里使用的函数。而OpenGL是一种高性能图形算法行业标准,是行业领域中最为广泛接纳的 2D/3D 图形 API,其自诞生已催生了各种计算机平台及设备上的数千优秀应用程序。glBegin是和glEnd结合起来使用的。

函数定义

函数原型:

void glBegin(GLenummode)

void glEnd(void)

参数说明:

一共有N个顶点,n表示从1开始的第n个点。

mode:创建图元的类型。可以是以下数值

GL_POINTS:把每一个顶点作为一个点进行处理,顶点n即定义了点n,共绘制N个点

GL_LINES:把每一个顶点作为一个独立的线段,顶点2n-1和2n之间的是第n条线段,总共绘制N/2条线段

GL_LINE_STRIP:绘制从第一个顶点到最后一个顶点依次相连的一组线段,第n和n+1个顶点定义了第n线段,总共绘制 N-1条线段

GL_LINE_LOOP:绘制从第一个顶点到最后一个顶点依次相连的一组线段,然后最后一个顶点和第一个顶点相连,第n和n+1个顶点定义了线段n,总共绘制N条线段

GL_TRIANGLES:把每个顶点作为一个独立的三角形,顶点3n-2、3n-1和3n定义了第n个三角形,总共绘制N/3个三角形

GL_TRIANGLE_STRIP:绘制一组相连的三角形,对于奇数n,顶点n、n+1和n+2定义了第n个三角形;对于偶数n,顶点n+1、n和n+2定义了第n个三角形,总共绘制N-2个三角形

GL_TRIANGLE_FAN:绘制一组相连的三角形,三角形是由第一个顶点及其后给定的顶点确定,顶点1、n+1和n+2定义了第n个三角形,总共N-2个三角形

GL_QUADS:绘制由四个顶点组成的一组单独的四边形。顶点4n-3、4n-2、4n-1和4n定义了第n个四边形。总共绘制N/4个四边形

GL_QUAD_STRIP:绘制一组相连的四边形。每个四边形是由一对顶点及其后给定的一对顶点共同确定的。顶点2n-1、2n、2n+2和2n+1定义了第n个四边形,总共绘制N/2-1个四边形

GL_POLYGON:绘制一个凸多边形。顶点1到n定义了这个多边形。

函数说明:

glBegin和glEnd函数限定了一组或多组图元的定点定义。

用法小结

1.在glBegin( )和glEnd( )之间可调用的函数

函数 函数意义

glVertex*( ) 设置顶点坐标

glColor*( ) 设置当前颜色

glIndex*( ) 设置当前颜色表

glNormal*( ) 设置法向坐标

glEvalCoord*( ) 产生坐标

glCallList( ),glCallLists( ) 执行显示列表

glTexCoord*( ) 设置纹理坐标

glEdgeFlag*( ) 控制边界绘制

glMaterial*( ) 设置材质

表7-2 在glBegin( )和glEnd( )之间可调用的函数

glVertex3f( )表示了该函数属于 gl库,参数是三个float型参数指针。我们用glVertex*( )来表示这一类函数。

基本库

2.几何图元类型和说明

类型 说明

GL_POINTS 单个顶点集

GL_LINES 多组双顶点线段

GL_POLYGON 单个简单填充凸多边形

GL_TRIANGLES 多组独立填充三角形

GL_QUADS 多组独立填充四边形

GL_LINE_STRIP 不闭合折线

GL_LINE_LOOP 闭合折线

GL_TRIANGLE_STRIP 线型连续填充三角形串

GL_TRIANGLE_FAN 扇形连续填充三角形串

GL_QUAD_STRIP 连续填充四边形串

注意事项

glBegin( )与glEnd( )函数使用注意点:

在OpenGL最初的定义中,几何对象数据的输入是通过调用glBegin( )和glEnd( )接口对来实现的。glBegin( )的参数表示其下所接收的数据是何种类型,如点,线段,三角型,扇形三角行,多边形等等。

而如glTranslatef( ), glScalef( ), glRotatef( )等接口的作用是对当前模型空间进行几何转换。如glTranslatef(1,2,3);表示把模型的世界坐标原点从当前位置(系统总认为是(0,0,0))转到起相对位置(1,2,3)。注意,OpenGL空间使用的是右手系的定义,与屏幕水平方向一致,并方向向右的是x轴;与屏幕垂直方向一致,并方向向上的是y轴;与屏幕(平面)垂直一致,并方向向外的是z轴。

glTranslatef( )等此类几何转换接口在glBegin( )和glEnd( )之间是无效的。因此,如果想对模型的位置进行转换,要在调用glBegin( )和glEnd( )接口对之前行处理。这是许多初学者特别容易犯错的地方,而且这个错误不容易找出来。

例如,以下的代码,并不会把模型的位置进行转移:

glBegin(GL_TRIANGLES)

glTranslatef(1,2,3);

render model......

glEnd( )

而应该这样调用:

glTranslatef(1,2,3);

glBegin(GL_TRIANGLES)

render model......

glEnd( )

英文说明

glBegin

NAME

glBegin, glEnd -- delimit the vertices of a primitive or a group of like primitives

C SPECIFICATION

void glBegin(GLenum mode)

PARAMETERS

mode

Specifies the primitive or primitives that will be created from vertices presented between glBegin and the subsequent glEnd. Ten symbolic constants are accepted: GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, and GL_POLYGON.

C SPECIFICATION

void glEnd(void)

DESCRIPTION

glBegin and glEnd delimit the verties that define a primitive or a group of like primitives. glBegin accepts a single argument that specifies which of ten ways the vertices are interpreted. Taking n as the integer count starting at one, and N as the total number of vertices specified, the interpretations are as follows:

GL_POINTS

Treats each vertex as a single point. Vertex n defines point n. N points are drawn.

GL_LINES

Treats each pair of vertices as an independent line segment. Vertices 2n-1 and 2n define line n. N/2 lines are drawn.

GL_LINE_STRIP

Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n. N-1 lines are drawn.

GL_LINE_LOOP

Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line however, is defined by vertices N and 1. N lines are drawn.

GL_TRIANGLES

Treats each triplet of vertices as an independent triangle. Vertices 3n-2, 3n-1, and 3n define triangle n. N/3 triangles are drawn.

GL_TRIANGLE_STRIP

Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N-2 triangles are drawn.

GL_TRIANGLE_FAN

Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. Vertices 1, n+1, and n+2 define triangle n. N-2 triangles are drawn.

GL_QUADS

Treats each group of four vertices as an independent quadriliteral. Vertices 4n-3, 4n-2, 4n-1, and 4n define quadriliteral n. N/4 quadriliterals are drawn.

GL_QUAD_STRIP

Draws a connected group of quadriliterals. One quadriliteral is defined for each pair of vertices presented after the first pair. Vertices 2n-1, 2n, 2n+2, and 2n+1 define quadriliteral n. N/2-1 quadriliterals are drawn. Note that the order in which vertices are used to construct a quadriliteral from strip data is different from that used with independent data.

GL_POLYGON

Draws a single, convex polygon. Vertices 1 through N define this polygon.

Only a subset of GL commands can be used betwen glBegin and glEnd. The commands are glVertex, glColor, glIndex, glNormal, glTexCoord, glEvalCoord, glEvalPoint, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceeding commands. If any other GL command is called between glBegin and glEnd, the error flag is set and the command is ignored.

Regardless of the value chosen for mode, there is no limit to the number of vertices that can be defined betwen glBegin and glEnd. Lines, triangles, quadriliterals, and polygons that are incompletely specified are not drawn. Incomplete specification results when either too few vertices are provided to specify even a single primitive or when an incorrect multiple of vertices is specified. The incomplete primitive is ignored; the rest are drawn.

The minimum specification of vertices for each primitive is as follows: 1 for a point, 2 for a line, 3 for a triangle, 4 for a quadriliteral, and 3 for a polygon. Modes that require a certain multiple of vertices are GL_LINES (2), GL_TRIANGLES (3), GL_QUADS (4), and GL_QUAD_STRIP (2).

ERRORS

GL_INVALID_ENUM is generated if mode is set to an unaccepted value.

GL_INVALID_OPERATION is generated if a command other than glVertex, glColor, glIndex, glNormal, glTexCoord, glEvalCoord, glEvalPoint, glMaterial, glEdgeFlag, glCallList or glCallLists is called between glBegin and the corresponding glEnd.

GL_INVALID_OPERATION is generated if glEnd is called before the corresponding glBegin is called.

免责声明
隐私政策
用户协议
目录 22
0{{catalogNumber[index]}}. {{item.title}}
{{item.title}}