glBegin

glBegin是OpenGL里使用的函式。而OpenGL是一種高性能圖形算法行業標準,是行業領域中最為廣泛接納的 2D/3D 圖形 API,其自誕生至今已催生了各種計算機平台及設備上的數千優秀應用程式。glBegin是和glEnd結合起來使用的。

基本介紹

  • 中文名:函式
  • 外文名:glBegin
  • 範圍:glBegin是OpenGL里使用的函式
  • 作用:催生各種計算機平台及設應用程式
  • 函式原型:void glBegin void glEnd 
函式定義,用法小結,注意事項,英文說明,

函式定義

函式原型:
void glBegin(GLenummode)
void glEnd(void)
參數說明:
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.

相關詞條

熱門詞條

聯絡我們