Sign in Join
Wiki copytemporary

OpenGL Tutorial/zh CN: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11OpenGL 是开发可移植的交互式2D和3D图形应用的首选环境。自从1992年问世以来,OpenGL已成为业内最广泛使用和最广为支持的2D和3D图形应用编程接口(API),为各种计算平台带来成千的应用。OpenGL通过包含一系列渲染,纹理映射,特殊效果和其他强大的视觉功能极大促进了创新并加速应用开发。开发人员可以在各种主流桌面及工作站平台发挥OpenGL的强大功能确保广泛部署。
22
33在以下联接您会得到更多关于OpenGl信息。[here](http://www.opengl.org/about/overview/).
44
55# GLUT
66
77GLUT (pronounced like the glut in gluttony) 全称 OpenGL Utility Toolkit, 写OpenGL 程序的窗口系统独立工具包。 它实现了一个简单的OpenGL窗口的应用程序编程接口(API). GLUT 让OpenGL编程更加容易理解,GLUT 具有可移植性,可以在不同平台编译。
88
99你能得到更多关于 GLUT的信息: [here](http://www.opengl.org/resources/libraries/glut/).
1010
1111许多系统已预安装了GLUT,但如果你的系统没有的话你可以轻松用Google搜索到 [Google](http://www.google.com/).
1212
1313编译好的Windows可执行文件可在这里找到 [www.xmission.com](http://www.xmission.com/~nate/glut.html).
1414
1515`GLUT FPC 单元信息在这里 `[[OpenGL]]`.`
1616
1717# LCL
1818
1919Lzazrus组件库也可以使用OpenGL。Lazarus包含了一个OpenGL的LCL控件TOpenGLControl。Lazarus包的位置:lazarus/components/opengl/lazopenglcontext.lpk。有一个实例是:lazarus/examples/openglcontrol/openglcontrol_demo.lpi。
2020
2121# LCL / GLUT
2222
2323你应该使用GLUT还是LCL?
2424
2525- 如果你想自己画所有的东西,GLUT会更好。
2626- 一般应用的话LCL更好。例如一个3D编辑器需要亦歌OpenGL窗口和剩余的一般应用如使用一般的按钮,组合框,窗口,窗口模块等。
2727
2828OpenGL部分大致相同。 在windows下GLUT需要一个dll(动态链接库) , where LCL typically runs out of the box, but a LCL executable is bigger.
2929
3030# 建立您的首个 GLUT 程序
3131
32-为了使用GLUT,您必须首先初始化它。这可以用<b>glutInit</b>函数完成。该函数可处理主窗体的命令行和设置其参数,但需要使用C/C++风格的输入。您需要编写自己的函数来完成从ParamCount 和 ParamStr 到C/C++风格命令行参数的转换。
32+为了使用GLUT,您必须首先初始化它。这可以用glutInit函数完成。该函数可处理主窗体的命令行和设置其参数,但需要使用C/C++风格的输入。您需要编写自己的函数来完成从ParamCount 和 ParamStr 到C/C++风格命令行参数的转换。
3333
3434``` pascal
3535 procedure glutInitPascal(ParseCmdLine: Boolean);
3636 var
3737 Cmd: array of PChar;
3838 CmdCount, I: Integer;
3939 begin
4040 if ParseCmdLine then
4141 CmdCount := ParamCount + 1
4242 else
4343 CmdCount := 1;
4444 SetLength(Cmd, CmdCount);
4545 for I := 0 to CmdCount - 1 do
4646 Cmd[I] := PChar(ParamStr(I));
4747 glutInit(@CmdCount, @Cmd);
4848 end;
4949```
5050
5151本质上,您创建一个数组,并用来自ParamStr的字符串填充。该函数也接受可控制传递到glutInit的参数,无论是整个命令行或仅仅是可执行文件名
5252
5353ToDo: probably **glutInit(@argc, @argv);** is enough.
5454
5555更多有关 glutInit 函数: <http://www.opengl.org/resources/libraries/glut/spec3/node10.html>
5656
57-下一步,您需要创建主窗体 使用<b>glutInitDisplayMode</b>设置主窗体显示模式。该函数只接受一个标志位组合为参数。通常您只需要<b>GLUT_DOUBLE 或 GLUT_RGB 或 GLUT_DEPTH</b> 。
57+下一步,您需要创建主窗体 使用glutInitDisplayMode设置主窗体显示模式。该函数只接受一个标志位组合为参数。通常您只需要GLUT_DOUBLE 或 GLUT_RGB 或 GLUT_DEPTH 。
5858
5959更多关于 glutInitDisplayMode 参见: <http://www.opengl.org/resources/libraries/glut/spec3/node12.html>
6060
61-窗体位置和大小使用<b>glutInitWindowPosition</b> 和 <b>glutInitWindowSize</b>来控制。他们接受2个参数。前一个参数是X和Y坐标,后一个是宽度和长度。您可使用<b>glutGet</b>以返回窗体大小和中心。
61+窗体位置和大小使用glutInitWindowPosition 和 glutInitWindowSize来控制。他们接受2个参数。前一个参数是X和Y坐标,后一个是宽度和长度。您可使用glutGet以返回窗体大小和中心。
6262
6363更多关于 glutInitWindowPosition, glutInitWindowSize 和 glutGet: <http://www.opengl.org/resources/libraries/glut/spec3/node11.html> <http://www.opengl.org/documentation/specs/glut/spec3/node70.html>
6464
65-最后,窗体应该采用<b>glutCreateWindow</b>创建。该函数将创建窗体并通过一参数设置其标题。因此它将返回该窗体的句柄。句柄可在其他需要该句柄的函数中使用。
65+最后,窗体应该采用glutCreateWindow创建。该函数将创建窗体并通过一参数设置其标题。因此它将返回该窗体的句柄。句柄可在其他需要该句柄的函数中使用。
6666
6767更多关于 glutCreateWindow: <http://www.opengl.org/resources/libraries/glut/spec3/node16.html>
6868
69-在您的程序进入主循环前,您必须设置些回调。您需要一个回调来画窗体,改变窗体尺寸及获取键盘输入。这些回调使用<b>glutDisplayFunc</b>, <b>glutReshapeFunc</b> 和 <b>glutKeyboardFunc</b>来设置。
69+在您的程序进入主循环前,您必须设置些回调。您需要一个回调来画窗体,改变窗体尺寸及获取键盘输入。这些回调使用glutDisplayFunc, glutReshapeFunc 和 glutKeyboardFunc来设置。
7070
7171更多有关设置回调,请参见: <http://www.opengl.org/resources/libraries/glut/spec3/node45.html#SECTION00080000000000000000>
7272
7373您的绘图函数可能看起来像这样子:
7474
7575`procedure DrawGLScene; cdecl;`\
7676`begin`\
7777`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
7878`  glutSwapBuffers;`\
7979`end;`
8080
8181这只能清空窗体为背景色并复位zbuffer。(别担心不懂zbuffer... 更多内容讲解在后面)
8282
8383您的窗体大小调整函数可能像这样:
8484
8585`procedure ReSizeGLScene(Width, Height: Integer); cdecl;`\
8686`begin`\
8787`  if Height = 0 then`\
8888`    Height := 1;`\
89-\
89+
9090`  glViewport(0, 0, Width, Height);`\
9191`  glMatrixMode(GL_PROJECTION);`\
9292`  glLoadIdentity;`\
9393`  gluPerspective(45, Width / Height, 0.1, 1000);`\
94-\
94+
9595`  glMatrixMode(GL_MODELVIEW);`\
9696`  glLoadIdentity;`\
9797`end;`
9898
9999这个代码告诉OpenGL窗体应该绘制在哪里,并且将矩阵设为期望值。(矩阵函数将会在后面介绍)
100100
101101键盘输入使用以下回调执行:
102102
103103`procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;`\
104104`begin`\
105105`  if Key = 27 then`\
106106`    Halt(0);`\
107107`end;`
108108
109-该函数将告诉您的程序,若是按下ESC键就推出。GLUT为事件驱动,终止您程序的唯一方式就是调用您的回调函数中的<b>Halt</b>。若您以其他方式关闭窗体,窗体是消失了,但程序仍然会在主过程内继续无限地循环。
109+该函数将告诉您的程序,若是按下ESC键就推出。GLUT为事件驱动,终止您程序的唯一方式就是调用您的回调函数中的Halt。若您以其他方式关闭窗体,窗体是消失了,但程序仍然会在主过程内继续无限地循环。
110110
111-要启动主循环,调用<b>glutMainLoop</b>.这将进入调用您回调函数的无限循环。
111+要启动主循环,调用glutMainLoop.这将进入调用您回调函数的无限循环。
112112
113113您程序的主部分可能看起来如下:
114114
115115`const `\
116116`  AppWidth = 640; `\
117117`  AppHeight = 480; `\
118-\
118+
119119`procedure InitializeGL; `\
120120`begin `\
121121`  glClearColor(0.18, 0.20, 0.66, 0); `\
122122`end; `\
123-\
123+
124124`var `\
125125`  ScreenWidth, ScreenHeight: Integer; `\
126126`begin `\
127127`  glutInitPascal(True); `\
128128`  glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH); `\
129129`  glutInitWindowSize(AppWidth, AppHeight); `\
130130`  ScreenWidth := glutGet(GLUT_SCREEN_WIDTH); `\
131131`  ScreenHeight := glutGet(GLUT_SCREEN_HEIGHT); `\
132132`  glutInitWindowPosition((ScreenWidth - AppWidth) div 2,`\
133133`    (ScreenHeight - AppHeight) div 2); `\
134134`  glutCreateWindow('OpenGL Tutorial 1'); `\
135-\
135+
136136`  InitializeGL; `\
137-\
137+
138138`  glutDisplayFunc(@DrawGLScene); `\
139139`  glutReshapeFunc(@ReSizeGLScene); `\
140140`  glutKeyboardFunc(@GLKeyboard); `\
141-\
141+
142142`  glutMainLoop; `\
143143`end.`
144144
145145下一个教程将添加一些绘制简单图形的代码.
146146
147147从这里[Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145)下载源代码或者Linux/Win可执行文件。
148148
149149# 绘制简单图形
150150
151151**Note:** The following parts are almost only OpenGL, so they run under GLUT and LCL. You can recognize GLUT specific functions with the prefix 'glu'.
152152
153153This time we shall add just a few lines of code and focus on explanation of some of the OpenGL functions.
154154
155155Let us explain code you allready have.
156156
157157`  .`\
158158`  .`\
159159`  .`\
160160`  glMatrixMode(GL_PROJECTION);`\
161161`  glLoadIdentity;`\
162162`  gluPerspective(45, Width / Height, 0.1, 1000);`\
163-\
163+
164164`  glMatrixMode(GL_MODELVIEW);`\
165165`  glLoadIdentity;`\
166166`end;`
167167
168-Using <b>glMatrixMode</b> function you chose which matrix you want to change. OpenGL works with 3 matrices: <b>GL_MODELVIEW</b>: this one is used to move vertex to model space. <b>GL_PROJECTION</b>: this one is used to convert 3d coordinate to 2d coordinate for finall pixel position. <b>GL_TEXTURE</b>: this one is used to alter texture coordinates.
168+Using glMatrixMode function you chose which matrix you want to change. OpenGL works with 3 matrices: GL_MODELVIEW: this one is used to move vertex to model space. GL_PROJECTION: this one is used to convert 3d coordinate to 2d coordinate for finall pixel position. GL_TEXTURE: this one is used to alter texture coordinates.
169169
170-Once you chose matrix you want to change, you can call functions that affect matrix values. <b>glLoadIdentity</b> will reset matrix so it doesn't affect vertex position. Since almost all matrix functions multiply current matrix with a generated one, you sometimes need to clear matrix with this function.
170+Once you chose matrix you want to change, you can call functions that affect matrix values. glLoadIdentity will reset matrix so it doesn't affect vertex position. Since almost all matrix functions multiply current matrix with a generated one, you sometimes need to clear matrix with this function.
171171
172-In order to set perspective matrix, you can use <b>gluPerspective</b> function. Four parameters present the field of view, aspect ratio, near and far plane. It's that simple.
172+In order to set perspective matrix, you can use gluPerspective function. Four parameters present the field of view, aspect ratio, near and far plane. It's that simple.
173173
174174Now, you'll change model matrix... for this time, you just set it to identity.
175175
176176OK... and now, the code for drawing the first shape:
177177
178178`procedure DrawGLScene; cdecl;`\
179179`begin`\
180180`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
181-\
181+
182182`  glLoadIdentity;`\
183183`  glTranslatef(0, 0, -5);`\
184-\
184+
185185`  glBegin(GL_TRIANGLES);`\
186186`    glColor3f(1, 0, 0);`\
187187`    glVertex3f(-1, -1, 0);`\
188-\
188+
189189`    glColor3f(0, 1, 0);`\
190190`    glVertex3f(1, -1, 0);`\
191-\
191+
192192`    glColor3f(0, 0, 1);`\
193193`    glVertex3f(0, 1, 0);`\
194194`  glEnd;`\
195-\
195+
196196`  glutSwapBuffers;`\
197197`end;`
198198
199199We have allready used glClear function. It will just reset buffers. We'll skip next two functions and head for drawing ones.
200200
201-<b>glBegin</b> marks beginning of drawing block. After this function you can start entering vertices. Parameter describes how are vertices used when drawing: GL_POINTS: Treats each vertex as a single point. Vertex n defines point n. N points are drawn.
201+glBegin marks beginning of drawing block. After this function you can start entering vertices. Parameter describes how are vertices used when drawing: GL_POINTS: Treats each vertex as a single point. Vertex n defines point n. N points are drawn.
202202
203203GL_LINES: Treats each pair of vertices as an independent line segment. Vertices 2n-1 and 2n define line n. n/2 lines are drawn.
204204
205205GL_LINE_STRIP: Draws a connected group of line segments from the first vertex to the last. n-1 lines are drawn.
206206
207207GL_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.
208208
209209GL_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.
210210
211211GL_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.
212212
213213GL_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.
214214
215215GL_QUADS: Treats each group of four vertices as an independent quadrilateral. Vertices 4n-3, 4n-2, 4n-1 and 4n define quadrilateral n. n/4 quadrilaterals are drawn.
216216
217217GL_QUAD_STRIP: Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2n-1, 2n, 2n+2 and 2n+1 define quadrilateral n. n/2-1 quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data.
218218
219219GL_POLYGON: Draws a single, convex polygon. Vertices 1 through n define this polygon.
220220
221-![](/wiki-images/f/f3/SimpleShapePic1.jpg) You'll draw single triangle and for that GL_TRIANGLES flag will do the trick. <b>glVertex3f</b> function defines the position of a vertex you want to draw. There are more glVertex* functions. Only difference is number and type of parameters they take. For instance... glVertex2i takes two parameters (x and y) of integer type. glVertex3f will almost always be just what you need.
221+![](/wiki-images/f/f3/SimpleShapePic1.jpg) You'll draw single triangle and for that GL_TRIANGLES flag will do the trick. glVertex3f function defines the position of a vertex you want to draw. There are more glVertex* functions. Only difference is number and type of parameters they take. For instance... glVertex2i takes two parameters (x and y) of integer type. glVertex3f will almost always be just what you need.
222222
223-Before glVertex you can set color, material, texture... For simplicity you'll just specify color for each vertex in this tutorial. Color is set using <b>glColor3f</b> function. glColor can also take different set of parameters like glVertex.
223+Before glVertex you can set color, material, texture... For simplicity you'll just specify color for each vertex in this tutorial. Color is set using glColor3f function. glColor can also take different set of parameters like glVertex.
224224
225-As we look through code we can see that Z is set to 0 for all vertices. Since you set near plane to 0.1, triangle will not be visible. That is where those two functions we skipped in the beginning jump in. We already know that glLoadIdentity reset matrix. <b>glTranslatef</b> moves triangles by X, Y and Z values you provide. Since you set Z to -5 (negative Z is farther from camera) all vertices will be drawn 5 units far from point of view and will be visible.
225+As we look through code we can see that Z is set to 0 for all vertices. Since you set near plane to 0.1, triangle will not be visible. That is where those two functions we skipped in the beginning jump in. We already know that glLoadIdentity reset matrix. glTranslatef moves triangles by X, Y and Z values you provide. Since you set Z to -5 (negative Z is farther from camera) all vertices will be drawn 5 units far from point of view and will be visible.
226226
227-In the end you call <b>glEnd</b> functions that finishes drawing. You could now start another drawing block with new glBegin function if you wish.
227+In the end you call glEnd functions that finishes drawing. You could now start another drawing block with new glBegin function if you wish.
228228
229229Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
230230
231231# Using display lists
232232
233-Sometimes you'll need to draw some object multiple times on scene. OpenGL has ability to build <b>display lists</b> which make drawing a bit faster. Creating display list is very easy... just draw vertices as you did in previous tutorial and enclose them with <b>glNewList</b> and <b>glEndList</b> calls.
233+Sometimes you'll need to draw some object multiple times on scene. OpenGL has ability to build display lists which make drawing a bit faster. Creating display list is very easy... just draw vertices as you did in previous tutorial and enclose them with glNewList and glEndList calls.
234234
235235`const`\
236236`  LIST_OBJECT = 1;`\
237-\
237+
238238`procedure CreateList;`\
239239`begin`\
240240`  glNewList(LIST_OBJECT, GL_COMPILE);`\
241241`    glBegin(GL_TRIANGLE_FAN);`\
242242`      glColor3f(1, 0, 0);`\
243243`      glVertex3f(0, 0.5, 0);`\
244-\
244+
245245`      glColor3f(1, 1, 0);`\
246246`      glVertex3f(-0.5, -0.5, 0.5);`\
247-\
247+
248248`      glColor3f(1, 1, 1);`\
249249`      glVertex3f(0.5, -0.5, 0.5);`\
250-\
250+
251251`      glColor3f(0, 1, 1);`\
252252`      glVertex3f(0.5, -0.5, -0.5);`\
253-\
253+
254254`      glColor3f(0, 0, 1);`\
255255`      glVertex3f(-0.5, -0.5, -0.5);`\
256-\
256+
257257`      glColor3f(0, 1, 0);`\
258258`      glVertex3f(-0.5, -0.5, 0.5);`\
259259`    glEnd;`\
260-\
260+
261261`    glBegin(GL_QUADS);`\
262262`      glColor3f(1, 1, 0);`\
263263`      glVertex3f(-0.5, -0.5, 0.5);`\
264-\
264+
265265`      glColor3f(1, 1, 1);`\
266266`      glVertex3f(0.5, -0.5, 0.5);`\
267-\
267+
268268`      glColor3f(0, 1, 1);`\
269269`      glVertex3f(0.5, -0.5, -0.5);`\
270-\
270+
271271`      glColor3f(0, 0, 1);`\
272272`      glVertex3f(-0.5, -0.5, -0.5);`\
273-\
273+
274274`      glColor3f(0, 1, 0);`\
275275`      glVertex3f(-0.5, -0.5, 0.5);`\
276276`    glEnd;`\
277277`  glEndList;`\
278278`end;`
279279
280-<b>glNewList</b> creates new display list and all drawing functions will be recorded until <b>glEndList</b> is called. The first parameter for glNewList function is list ID. Every list is defined by it's ID. If list with given ID is already created it fill be cleared before recording. If the second parameter is GL_COMPILE then all drawing functions are just recorded, but if it is GL_COMPILE_AND_EXECUTE then they are recorded and executed automaticly.
280+glNewList creates new display list and all drawing functions will be recorded until glEndList is called. The first parameter for glNewList function is list ID. Every list is defined by it's ID. If list with given ID is already created it fill be cleared before recording. If the second parameter is GL_COMPILE then all drawing functions are just recorded, but if it is GL_COMPILE_AND_EXECUTE then they are recorded and executed automaticly.
281281
282-<b>glIsList</b> function can help you with display lists. It can tell if some list ID is already filled with data. Another useful function is <b>glGenLists</b>. It will create multiple empty display lists. You pass number of display lists you need and you get ID of the first one. If you require n lists, and get r ID, generated display lists are: r, r+1, r+2,..., r+n-1
282+glIsList function can help you with display lists. It can tell if some list ID is already filled with data. Another useful function is glGenLists. It will create multiple empty display lists. You pass number of display lists you need and you get ID of the first one. If you require n lists, and get r ID, generated display lists are: r, r+1, r+2,..., r+n-1
283283
284284All created lists should be deleted. You will do that before program exits:
285285
286286`procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;`\
287287`begin`\
288288`  if Key = 27 then`\
289289`  begin`\
290290`    glDeleteLists(LIST_OBJECT, 1);`\
291291`    Halt(0);`\
292292`  end;`\
293293`end;`
294294
295-<b>glDeleteLists</b> takes 2 parameters, ID of display list and number of lists to delete. If ID is r, and number of lists to delete is n, deleted lists are: r, r+1, r+2,..., r+n-1
295+glDeleteLists takes 2 parameters, ID of display list and number of lists to delete. If ID is r, and number of lists to delete is n, deleted lists are: r, r+1, r+2,..., r+n-1
296296
297297Now you know how to create and delete display lists... let's see how to draw them:
298298
299299`procedure DrawGLScene; cdecl;`\
300300`begin`\
301301`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
302-\
302+
303303`  glLoadIdentity;`\
304304`  glTranslatef(-2, 0, -5);`\
305305`  glRotatef(40, 1, 0, 1);`\
306306`  glCallList(LIST_OBJECT);`\
307-\
307+
308308`  glLoadIdentity;`\
309309`  glTranslatef(1, -2, -10);`\
310310`  glRotatef(62, 0, 1, 0);`\
311311`  glCallList(LIST_OBJECT);`\
312-\
312+
313313`  glLoadIdentity;`\
314314`  glTranslatef(-4, 0.5, -15);`\
315315`  glRotatef(200, 1, 0, 0);`\
316316`  glCallList(LIST_OBJECT);`\
317-\
317+
318318`  glutSwapBuffers;`\
319319`end;`
320320
321-![](/wiki-images/a/a5/DisplayListsPic1.jpg) Using <b>glCallList</b> you can draw only one display list. In this tutorial, before drawing display list, you change model matrix and draw object in different places. Some times you would like to draw multiple lists at once. That is possible using <b>glCallLists</b> function. It takes number of lists you want to draw, type of array that contains display list IDs and array with display list IDs. Type of list can be one of the following:
321+![](/wiki-images/a/a5/DisplayListsPic1.jpg) Using glCallList you can draw only one display list. In this tutorial, before drawing display list, you change model matrix and draw object in different places. Some times you would like to draw multiple lists at once. That is possible using glCallLists function. It takes number of lists you want to draw, type of array that contains display list IDs and array with display list IDs. Type of list can be one of the following:
322322
323323GL_BYTE: list is treated as an array of signed bytes, each in the range -128 through 127.
324324
325325GL_UNSIGNED_BYTE: list is treated as an array of unsigned bytes, each in the range 0 through 255.
326326
327327GL_SHORT: list is treated as an array of signed two-byte integers, each in the range -32768 through 32767.
328328
329329GL_UNSIGNED_SHORT: list is treated as an array of unsigned two-byte integers, each in the range 0 through 65535.
330330
331331GL_INT: lists is treated as an array of signed four-byte integers.
332332
333333GL_UNSIGNED_INT: list is treated as an array of unsigned four-byte integers.
334334
335335GL_FLOAT: list is treated as an array of four-byte floating-point values.
336336
337337GL_2_BYTES: list is treated as an array of unsigned bytes. Each pair of bytes specifies a single display list ID. The value of the pair is computed as 256 times the unsigned value of the first byte plus the unsigned value of the second byte.
338338
339339GL_3_BYTES: list is treated as an array of unsigned bytes. Each triplet of bytes specifies a single display list ID. The value of the triplet is computed as 65536 times the unsigned value of the first byte, plus 256 times the unsigned value of the second byte, plus the unsigned value of the third byte.
340340
341341GL_4_BYTES: list is treated as an array of unsigned bytes. Each quadruplet of bytes specifies a single display list ID. The value of the quadruplet is computed as 16777216 times the unsigned value of the first byte, plus 65536 times the unsigned value of the second byte, plus 256 times the unsigned value of the third byte, plus the unsigned value of the fourth byte.
342342
343343That is for now. Next tutorial will show how to create little planetary system. We'll talk about matrices and how to make animated scene that doesn't depend of number of frames per second.
344344
345345Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
346346
347347# Full screen animation
348348
349349Entering full screen mode is easy with GLUT. Let's change main part of the program:
350350
351351`const`\
352352`  FSMode = '800x600:32@75';`\
353-\
353+
354354`begin`\
355355`  glutInitPascal(False);`\
356356`  glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH);`\
357357`  glutGameModeString(FSMode);`\
358358`  glutEnterGameMode;`\
359359`  glutSetCursor(GLUT_CURSOR_NONE);`\
360-\
360+
361361`  InitializeGL;`\
362-\
362+
363363`  glutDisplayFunc(@DrawGLScene);`\
364364`  glutReshapeFunc(@ReSizeGLScene);`\
365365`  glutKeyboardFunc(@GLKeyboard);`\
366366`  glutIdleFunc(@DrawGLScene);`\
367-\
367+
368368`  glutMainLoop;`\
369369`end.`
370370
371-Since we don't want GLUT to parse command line this time we call glutInitPascal with False parameter. As you can see, there is no code for window creation. GLUT have <b>glutEnterGameMode</b> that create full screen window. To specify what kind of full screen mode you want, you call <b>glutGameModeString</b> function which takes string that defines mode you like. Format of that string is:
371+Since we don't want GLUT to parse command line this time we call glutInitPascal with False parameter. As you can see, there is no code for window creation. GLUT have glutEnterGameMode that create full screen window. To specify what kind of full screen mode you want, you call glutGameModeString function which takes string that defines mode you like. Format of that string is:
372372
373373`[width "x" height][":" bpp]["@" hertz]`
374374
375375In FSMode string we declared that full screen mode should be 800x600, with 32bit pallete and 75Hz refresh. It is possible to skip one of the group. If you omit size, GLUT will try to use current one or first smaller that can work. That policy is used and for other parameters.
376376
377-Usually in full screen mode cursor is not visible. To hide cursor you use <b>glutSetCursor</b> function. It takes only one parameter which describes cursor you would like to see:
377+Usually in full screen mode cursor is not visible. To hide cursor you use glutSetCursor function. It takes only one parameter which describes cursor you would like to see:
378378
379379`GLUT_CURSOR_RIGHT_ARROW`\
380380`GLUT_CURSOR_LEFT_ARROW`\
381381`GLUT_CURSOR_INFO`\
382382`GLUT_CURSOR_DESTROY`\
383383`GLUT_CURSOR_HELP`\
384384`GLUT_CURSOR_CYCLE`\
385385`GLUT_CURSOR_SPRAY`\
386386`GLUT_CURSOR_WAIT`\
387387`GLUT_CURSOR_TEXT`\
388388`GLUT_CURSOR_CROSSHAIR`\
389389`GLUT_CURSOR_UP_DOWN`\
390390`GLUT_CURSOR_LEFT_RIGHT`\
391391`GLUT_CURSOR_TOP_SIDE`\
392392`GLUT_CURSOR_BOTTOM_SIDE`\
393393`GLUT_CURSOR_LEFT_SIDE`\
394394`GLUT_CURSOR_RIGHT_SIDE`\
395395`GLUT_CURSOR_TOP_LEFT_CORNER`\
396396`GLUT_CURSOR_TOP_RIGHT_CORNER`\
397397`GLUT_CURSOR_BOTTOM_RIGHT_CORNER`\
398398`GLUT_CURSOR_BOTTOM_LEFT_CORNER`\
399399`GLUT_CURSOR_FULL_CROSSHAIR`\
400400`GLUT_CURSOR_NONE`\
401401`GLUT_CURSOR_INHERIT`
402402
403-<b>glutIdleFunc</b> defines callback function that you want to be called every time you program has no messages to process. Since we just want to render new frame if there is nothing to do, just set idle function to DrawGLScene. Some other tutorials show that idle function should send refresh message insted of drawing, but that way I have 50-100 frames less than using method I described.
403+glutIdleFunc defines callback function that you want to be called every time you program has no messages to process. Since we just want to render new frame if there is nothing to do, just set idle function to DrawGLScene. Some other tutorials show that idle function should send refresh message insted of drawing, but that way I have 50-100 frames less than using method I described.
404404
405405Now, let's look at the program termination where you need to exit full screen mode:
406406
407407`procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;`\
408408`begin`\
409409`  if Key = 27 then`\
410410`  begin`\
411411`    glutLeaveGameMode;`\
412412`    Halt(0);`\
413413`  end;`\
414414`end;`
415415
416-As you can see, all you need to do is to call <b>glutLeaveGameMode</b>.
416+As you can see, all you need to do is to call glutLeaveGameMode.
417417
418418Now, we'll introduce some new matrix functions. First, let's change ReSizeGLScene function:
419419
420420`procedure ReSizeGLScene(Width, Height: Integer); cdecl;`\
421421`begin`\
422422`  .`\
423423`  .`\
424424`  .`
425425
426426`  glMatrixMode(GL_MODELVIEW);`\
427427`  glLoadIdentity;`\
428428`  gluLookAt(0, 20, 25, 0, 0, 0, 0, 1, 0);`\
429429`end;`
430430
431-<b>gluLookAt</b> create matrix that will define from where are you look to objects. First 3 parameters are X, Y and Z coordinate of position of camera. Next 3 parameters are X, Y and Z coordinate of point where camera look at, and last 3 parameters defines "up" vector (where is "up" for the camera). Usually, up is positive y axis.
431+gluLookAt create matrix that will define from where are you look to objects. First 3 parameters are X, Y and Z coordinate of position of camera. Next 3 parameters are X, Y and Z coordinate of point where camera look at, and last 3 parameters defines "up" vector (where is "up" for the camera). Usually, up is positive y axis.
432432
433433OK, let's draw now. Since you set matrix with gluLookAt that should be used with all objects, you can't just use glLoadIdentity to reset matrix for next object... you'll save previous matrix state and restore it after object is drawn:
434434
435435`procedure DrawGLScene; cdecl;`\
436436`var`\
437437`  T: Single;`\
438438`begin`\
439439`  T := glutGet(GLUT_ELAPSED_TIME) / 1000;`\
440440`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
441-\
441+
442442`  glPushMatrix;`\
443443`    glRotatef(5 * T, 0, 1, 0);`\
444444`    glColor3f(1, 1, 0);`\
445445`    glutWireSphere(2, 20, 20);`\
446446`  glPopMatrix;`\
447-\
447+
448448`  glPushMatrix;`\
449449`    glRotatef(90 * T, 0, 1, 0);`\
450450`    glTranslatef(5, 0, 0);`\
451451`    glRotatef(40 * T, 0, 1, 0);`\
452452`    glColor3f(1, 0, 0);`\
453453`    glutWireSphere(0.6, 10, 10);`\
454454`  glPopMatrix;`\
455-\
455+
456456`  glPushMatrix;`\
457457`    glRotatef(60 * T, 0, 1, 0);`\
458458`    glTranslatef(-3, 0, 9);`\
459459`    glRotatef(50 * T, 0, 1, 0);`\
460460`    glColor3f(0, 1, 0);`\
461461`    glutWireSphere(1, 16, 16);`\
462-\
462+
463463`    glPushMatrix;`\
464464`      glRotatef(360 * T, 0, 1, 0);`\
465465`      glTranslatef(-1.7, 0, 0);`\
466466`      glRotatef(50 * T, 0, 1, 0);`\
467467`      glColor3f(0, 0, 1);`\
468468`      glutWireSphere(0.4, 10, 10);`\
469469`    glPopMatrix;`\
470-\
470+
471471`  glPopMatrix;`\
472-\
472+
473473`  glutSwapBuffers;`\
474474`end;`
475475
476-![](/wiki-images/5/51/FullScreenAnimationPic1.jpg) <b>glPushMatrix</b> i <b>glPopMatrix</b> are used to save and restore matrix state. As you can see, we save matrix state, then change matrix in order to draw object in right place, and then restore old matrix state.
476+![](/wiki-images/5/51/FullScreenAnimationPic1.jpg) glPushMatrix i glPopMatrix are used to save and restore matrix state. As you can see, we save matrix state, then change matrix in order to draw object in right place, and then restore old matrix state.
477477
478-You may wonder what is <b>T</b> variable for. Well, it is used to determen animation speed. Every change that depends on time is multiplied with T. That way animation speed is constant on every frame rate. <b>glutGet</b> function with <b>GLUT_ELAPSED_TIME</b> parameter returns time in milliseconds from glutInit is called. By dividing that value with 1000, we get time in seconds.
478+You may wonder what is T variable for. Well, it is used to determen animation speed. Every change that depends on time is multiplied with T. That way animation speed is constant on every frame rate. glutGet function with GLUT_ELAPSED_TIME parameter returns time in milliseconds from glutInit is called. By dividing that value with 1000, we get time in seconds.
479479
480-<b>glRotatef</b> function create rotation matrix. First parameter is angle in degrees, and last 3 parameters defines axis around which rotation will be done. Since you multiplied angle with T, object will be rotated by that angle in exactly 1 second.
480+glRotatef function create rotation matrix. First parameter is angle in degrees, and last 3 parameters defines axis around which rotation will be done. Since you multiplied angle with T, object will be rotated by that angle in exactly 1 second.
481481
482482Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
483483
484484# Light
485485
486486This tutorial will introduce some light to the scene. You'll make rotating cube and one light which will add some realism to the scene, but first let's make some utility unit.
487487
488488For now it will have only basic functions to help us getting current and delta (time that elapsed from one render to other render call) times and for calculating frames per second.
489489
490490`unit utils;`\
491-\
491+
492492`{$mode objfpc}{$H+}`\
493-\
493+
494494`interface`\
495-\
495+
496496`uses`\
497497`  glut;`\
498-\
498+
499499`function GetTotalTime: Single;`\
500500`function GetDeltaTime: Single;`\
501501`procedure FrameRendered(Count: Integer = 1);`\
502502`function GetFPS: Single;`\
503-\
503+
504504`implementation`\
505-\
505+
506506`var`\
507507`  OldTime: Integer = 0;`\
508508`  FPSTime: Integer = 0;`\
509509`  FPSCount: Integer = 0;`\
510-\
510+
511511`function GetTotalTime: Single;`\
512512`begin`\
513513`  Result := glutGet(GLUT_ELAPSED_TIME) / 1000;`\
514514`end;`\
515-\
515+
516516`function GetDeltaTime: Single;`\
517517`var`\
518518`  NewTime: Integer;`\
519519`begin`\
520520`  NewTime := glutGet(GLUT_ELAPSED_TIME);`\
521521`  Result := (NewTime - OldTime) / 1000;`\
522522`  OldTime := NewTime;`\
523523`end;`\
524-\
524+
525525`procedure FrameRendered(Count: Integer);`\
526526`begin`\
527527`  Inc(FPSCount, Count);`\
528528`end;`\
529-\
529+
530530`function GetFPS: Single;`\
531531`var`\
532532`  NewTime: Integer;`\
533533`begin`\
534534`  NewTime := glutGet(GLUT_ELAPSED_TIME);`\
535-\
535+
536536`  Result := FPSCount / ((NewTime - FPSTime) / 1000);`\
537-\
537+
538538`  FPSTime := NewTime;`\
539539`  FPSCount := 0;`\
540540`end;`\
541-\
541+
542542`end.`
543543
544544As you can see, there is nothing complicated in this unit. Time is simply saved betwen calls and difference is returned. FrameRendered should be called every time you draw scene so function can calculate FPS.
545545
546546Now, let's have fun with lights.
547547
548548OpenGL have several types of light... ambient, diffuse, point, spot, specular and emissive light.
549549
550550Ambient light is something like Sun. When sun rays pass through the window of a room they hit the walls and are reflected and scattered into all different directions which averagely brightens up the whole room. All vertices are lit with ambient light.
551551
552552Diffuse light can be represented as parallel light rays comming from far away. They will lit only vertices that are oriented towards the light source.
553553
554554Point light lights all around it. It is like a fire ball, it send light rays all around it and lights vertices that are oriented towards light source and that are close enough.
555555
556556Spot light is like light from flashlight. It is simply a point light source with a small light cone radius. All vertices that falls inside of cone and are close enough are lit.
557557
558558Just like Diffuse light, Specular light is a directional type of light. It comes from one particular direction. The difference between the two is that specular light reflects off the surface in a sharp and uniform way. The rendering of specular light relies on the angle between the viewer and the light source. From the viewer’s standpoint specular light creates a highlighted area on the surface of the viewed object known as specular highlight or specular reflection.
559559
560560Emissive light is a little different than any other previously explained light components. This light comes out of object you draw but don't lit other objects in nearby.
561561
562562For simplicity we'll use only diffuse light in this tutorial. Later on, some other lights may appear in tutorials :)
563563
564564Let's see how to enable light in scene:
565565
566566`const`\
567567`  DiffuseLight: array[0..3] of GLfloat = (0.8, 0.8, 0.8, 1);`\
568-\
568+
569569`  glEnable(GL_LIGHTING);`\
570570`  glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight);`\
571571`  glEnable(GL_LIGHT0);`
572572
573-As you see, we enable lighting in OpenGL so lights affect scene you are rendering. Light parameters are set with <b>glLightfv</b> function. It takes 3 parameters... one for light number you want to change (OpenGL suports up to 8 lights), next tells OpenGL what light parameter to change, and the last one is new parameter for light. You'll set just diffuse color for light in this tutorial. After that, you can enable light and there will be light in the scene... but... that is not all.
573+As you see, we enable lighting in OpenGL so lights affect scene you are rendering. Light parameters are set with glLightfv function. It takes 3 parameters... one for light number you want to change (OpenGL suports up to 8 lights), next tells OpenGL what light parameter to change, and the last one is new parameter for light. You'll set just diffuse color for light in this tutorial. After that, you can enable light and there will be light in the scene... but... that is not all.
574574
575575More about glLightfv: <http://www.opengl.org//documentation/specs/man_pages/hardcopy/GL/html/gl/light.html>
576576
577577If you want to use lights you can't just set color for vertex... you must set material for vertices. Let's setup material for drawing:
578578
579579`glEnable(GL_COLOR_MATERIAL);`\
580580`glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);`
581581
582-![](/wiki-images/e/ed/LightPic1.jpg) You expected something more complicated, do you? :) Well, this code allows us to use glColor function to set material to vertices. By using glEnable function and GL_COLOR_MATERIAL flag, you can define what material properties will glColor change. <b>glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)</b> tells OpenGL that glColor changes ambient and diffuse material. We'll discus materials more in later tutorials.
582+![](/wiki-images/e/ed/LightPic1.jpg) You expected something more complicated, do you? :) Well, this code allows us to use glColor function to set material to vertices. By using glEnable function and GL_COLOR_MATERIAL flag, you can define what material properties will glColor change. glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) tells OpenGL that glColor changes ambient and diffuse material. We'll discus materials more in later tutorials.
583583
584584One more thing that is important when using lights... every vertex must have normal associated with it. Normal is used to find the direction of vertex so light can be calculated properly. You'll use GLUT function to draw cube and it provides normals for us, so this time we'll just walk by normals.
585585
586586After all those setting ups, light will shine up your cube :)
587587
588588Part of the text is copied from [The OpenGL Light Bible](http://www.falloutsoftware.com/tutorials/gl/gl8.htm)
589589
590590Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
591591
592592# Bitmap fonts
593593
594594Games and programs usually need to write some text on screen. GLUT provides several functions for drawing chars that are platform independent.
595595
596596First, we'll show how to use default bitmap fonts. Almost all code additions will be made to utils.pas unit.
597597
598598Since text will be drawn in 2D, we'll need to know width and height of viewport... so, we'll write two functions for that:
599599
600600`function glGetViewportWidth: Integer;`\
601601`var`\
602602`  Rect: array[0..3] of Integer;`\
603603`begin`\
604604`  glGetIntegerv(GL_VIEWPORT, @Rect);`\
605605`  Result := Rect[2] - Rect[0];`\
606606`end;`\
607-\
607+
608608`function glGetViewportHeight: Integer;`\
609609`var`\
610610`  Rect: array[0..3] of Integer;`\
611611`begin`\
612612`  glGetIntegerv(GL_VIEWPORT, @Rect);`\
613613`  Result := Rect[3] - Rect[1];`\
614614`end;`
615615
616616We just get left/right, top/bottom and calculate width/height by subtracting them.
617617
618618There must be functions for entering and leaving 2D mode:
619619
620620`procedure glEnter2D;`\
621621`begin`\
622622`  glMatrixMode(GL_PROJECTION);`\
623623`  glPushMatrix;`\
624624`  glLoadIdentity;`\
625625`  gluOrtho2D(0, glGetViewportWidth, 0, glGetViewportHeight);`\
626-\
626+
627627`  glMatrixMode(GL_MODELVIEW);`\
628628`  glPushMatrix;`\
629629`  glLoadIdentity;`\
630-\
630+
631631`  glDisable(GL_DEPTH_TEST);`\
632632`end;`\
633-\
633+
634634`procedure glLeave2D;`\
635635`begin`\
636636`  glMatrixMode(GL_PROJECTION);`\
637637`  glPopMatrix;`\
638638`  glMatrixMode(GL_MODELVIEW);`\
639639`  glPopMatrix;`\
640-\
640+
641641`  glEnable(GL_DEPTH_TEST);`\
642642`end;`
643643
644-When entering 2D mode, we save current matrices and set 2D matrix using <b>gluOrtho2D</b> function. This way if we draw some thing on 100, 100 it will be drawn on exactly 100 pixels from left edge of window, and 100 pixels form bottom edge (positive Y is up). Also, we disable ZBuffer. This way text won't alter ZBuffer.
644+When entering 2D mode, we save current matrices and set 2D matrix using gluOrtho2D function. This way if we draw some thing on 100, 100 it will be drawn on exactly 100 pixels from left edge of window, and 100 pixels form bottom edge (positive Y is up). Also, we disable ZBuffer. This way text won't alter ZBuffer.
645645
646646Leaving 2D mode just returns old matrices and enable ZBuffer.
647647
648648Now, we can create function for text drawing:
649649
650650`procedure glWrite(X, Y: GLfloat; Font: Pointer; Text: String);`\
651651`var`\
652652`  I: Integer;`\
653653`begin`\
654654`  glRasterPos2f(X, Y);`\
655655`  for I := 1 to Length(Text) do`\
656656`    glutBitmapCharacter(Font, Integer(Text[I]));`\
657657`end;`
658658
659-<b>glutBitmapCharacter</b> can draw only one character of selected font. First parameter is desired font (GLUT_BITMAP_9_BY_15, GLUT_BITMAP_8_BY_13, GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TIMES_ROMAN_24, GLUT_BITMAP_HELVETICA_10, GLUT_BITMAP_HELVETICA_12 or GLUT_BITMAP_HELVETICA_18) and other one is character.
659+glutBitmapCharacter can draw only one character of selected font. First parameter is desired font (GLUT_BITMAP_9_BY_15, GLUT_BITMAP_8_BY_13, GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TIMES_ROMAN_24, GLUT_BITMAP_HELVETICA_10, GLUT_BITMAP_HELVETICA_12 or GLUT_BITMAP_HELVETICA_18) and other one is character.
660660
661-Character will be drawn at current raster position. To set desired raster position we call <b>glRasterPos</b> function. glRasterPos can handle different number and types of parameters just like glVertex function. Coordinate specified is transformed by model and projection matrix to get 2D coordinate where new raster position will be. Since we entered 2D mode, X and Y coordinates are actual 2D coordinates where drawing will occur.
661+Character will be drawn at current raster position. To set desired raster position we call glRasterPos function. glRasterPos can handle different number and types of parameters just like glVertex function. Coordinate specified is transformed by model and projection matrix to get 2D coordinate where new raster position will be. Since we entered 2D mode, X and Y coordinates are actual 2D coordinates where drawing will occur.
662662
663663This new functions will make text drawing very easy:
664664
665665`procedure DrawGLScene; cdecl;`\
666666`begin`\
667667`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
668-\
668+
669669`  glLoadIdentity;`\
670670`  glTranslatef(0, 0, -5);`\
671671`  glRotatef(GetTotalTime * 10, 0, 0.5, 0.5);`\
672-\
672+
673673`  glColor3f(1, 0, 0);`\
674674`  glutSolidCube(2);`\
675-\
675+
676676`  glEnter2D;`\
677-\
677+
678678`  glColor3f(0.2, 0.8 + 0.2 * Sin(GetTotalTime * 5), 0);`\
679679`  glWrite(20, glGetViewportHeight - 20, GLUT_BITMAP_8_BY_13,`\
680680`    Format('OpenGL Tutorial :: Bitmap Fonts :: FPS - %.2f FPS', [FPS]));`\
681-\
681+
682682`  glColor3f(1, 1, 1);`\
683683`  glWrite(50, glGetViewportHeight - 60, GLUT_BITMAP_9_BY_15, 'GLUT_BITMAP_9_BY_15');`\
684684`  glWrite(50, glGetViewportHeight - 90, GLUT_BITMAP_8_BY_13, 'GLUT_BITMAP_8_BY_13');`\
685685`  glWrite(50, glGetViewportHeight - 120, GLUT_BITMAP_TIMES_ROMAN_10, 'GLUT_BITMAP_TIMES_ROMAN_10');`\
686686`  glWrite(50, glGetViewportHeight - 150, GLUT_BITMAP_TIMES_ROMAN_24, 'GLUT_BITMAP_TIMES_ROMAN_24');`\
687687`  glWrite(50, glGetViewportHeight - 180, GLUT_BITMAP_HELVETICA_10, 'GLUT_BITMAP_HELVETICA_10');`\
688688`  glWrite(50, glGetViewportHeight - 210, GLUT_BITMAP_HELVETICA_12, 'GLUT_BITMAP_HELVETICA_12');`\
689689`  glWrite(50, glGetViewportHeight - 240, GLUT_BITMAP_HELVETICA_18, 'GLUT_BITMAP_HELVETICA_18');`\
690-\
690+
691691`  glColor3f(0.5, 0.5, 1);`\
692692`  glWrite(`\
693693`    glGetViewportWidth - glutBitmapLength(GLUT_BITMAP_9_BY_15, LazText) - 5,`\
694694`    10, GLUT_BITMAP_9_BY_15, LazText);`\
695-\
695+
696696`  glLeave2D;`\
697-\
697+
698698`  glutSwapBuffers;`\
699-\
699+
700700`  FrameRendered;`\
701701`end;`
702702
703-![](/wiki-images/b/ba/BitmapFontsPic1.jpg) We draw red cube and rotate it, and some text to show how various bitmap fonts look like. <b>glutBitmapLength</b> function is used to find width of string so it could be aligned to right. Code can easily be altered to center text.
703+![](/wiki-images/b/ba/BitmapFontsPic1.jpg) We draw red cube and rotate it, and some text to show how various bitmap fonts look like. glutBitmapLength function is used to find width of string so it could be aligned to right. Code can easily be altered to center text.
704704
705705Note: See how cube looks without light.
706706
707707Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
708708
709709# Textures
710710
711711It's time to use textures :)
712712
713713This tutorial will show how to draw textured polygons and how to blend textures using multipass technic. Since OpenGL has no builtin mechanism for loading textures, we'll use external library: [Vampyre Imaging Library](http://imaginglib.sourceforge.net/). We'll use just OpenGL helper functions, but you may find this lib handy for some other things to.
714714
715715Let's get started... we'll create display list for drawing textured rectangle:
716716
717717`procedure CreateList;`\
718718`begin`\
719719`  glNewList(LIST_OBJECT, GL_COMPILE);`\
720720`    glBegin(GL_QUADS);`\
721721`      glTexCoord2f(1, 0);`\
722722`      glVertex3f( 2, 2, 0);`\
723723`      glTexCoord2f(0, 0);`\
724724`      glVertex3f(-2, 2, 0);`\
725725`      glTexCoord2f(0, 1);`\
726726`      glVertex3f(-2,-2, 0);`\
727727`      glTexCoord2f(1, 1);`\
728728`      glVertex3f( 2,-2, 0);`\
729729`    glEnd;`\
730730`  glEndList;`\
731731`end;`
732732
733-Notice <b>glTexCoord</b> functions. They are used to specify which part of texture is assigned to vertex. Coordinates defined in this functions are from 0 to 1 (values greater than 1 are allowed but can generate different results). 0 is first pixel and 1 is last pixel. So, 0.5 will be right in the middle of texture.
733+Notice glTexCoord functions. They are used to specify which part of texture is assigned to vertex. Coordinates defined in this functions are from 0 to 1 (values greater than 1 are allowed but can generate different results). 0 is first pixel and 1 is last pixel. So, 0.5 will be right in the middle of texture.
734734
735735Texture loading is extremely easy with Vampyre Imaging Library:
736736
737737`var`\
738738`  Tex1, Tex2: GLuint;`\
739-\
739+
740740`procedure InitializeGL;`\
741741`begin`\
742742`  glClearColor(0, 0, 0, 0);`\
743743`  Tex1 := LoadGLTextureFromFile('ashwood.bmp');`\
744744`  Tex2 := LoadGLTextureFromFile('Flare.bmp');`\
745745`  glEnable(GL_TEXTURE_2D);`\
746746`end;`
747747
748-<b>LoadGLTextureFromFile</b> loads texture from file and returns it's ID. When texture is loaded it is allready setup for rendering. Last line just enables 2D textures.
748+LoadGLTextureFromFile loads texture from file and returns it's ID. When texture is loaded it is allready setup for rendering. Last line just enables 2D textures.
749749
750750To draw textured polygon you have to bind texture and setup texture coordinations (texture coordinations are set in display list in this tutorial):
751751
752752`  ...`\
753753`  glLoadIdentity;`\
754754`  glTranslatef(-5, 0, -15);`\
755755`  glBindTexture(GL_TEXTURE_2D, Tex1);`\
756756`  glCallList(LIST_OBJECT);`\
757757`  ...`
758758
759-<b>glBindTexture</b> function is used to select texture. When you draw polygins they will have selected texture on them. It's that easy :)
759+glBindTexture function is used to select texture. When you draw polygins they will have selected texture on them. It's that easy :)
760760
761761So, using one texture is easy... but how to blend two textures. Basicly you draw polygon once with one texture, setup blending parameters, and draw polygon once more time with other texture. You can blend houndreds of textures this way. Let's see how code for this looks:
762762
763763`  ...`\
764764`  glLoadIdentity;`\
765765`  glTranslatef(5, 0, -15);`\
766766`  glBindTexture(GL_TEXTURE_2D, Tex1);`\
767767`  glCallList(LIST_OBJECT);`\
768-\
768+
769769`  glEnable(GL_BLEND);`\
770770`  glBlendFunc(GL_ZERO, GL_SRC_COLOR);`\
771771`  glLoadIdentity;`\
772772`  glTranslatef(5, 0, -15);`\
773773`  glBindTexture(GL_TEXTURE_2D, Tex2);`\
774774`  glCallList(LIST_OBJECT);`\
775775`  glDisable(GL_BLEND);`\
776776`...`
777777
778-As you can see, polygon is drawn first time like we allready know. Before second drawing we enable blending by calling <b>glEnable(GL_BLEND)</b>. Blending means that finall pixel color is calculated like this:
778+As you can see, polygon is drawn first time like we allready know. Before second drawing we enable blending by calling glEnable(GL_BLEND). Blending means that finall pixel color is calculated like this:
779779
780780`DrawingColor * SRCBLEND + BackgroundColor * DESTBLEND`
781781
782-SRCBLEND and DESTBLEND are defined using <b>glBlendFunc</b> function. In this tutorial we set SRCBLEND to GL_ZERO (zero) and DESTBLENT to GL_SRC_COLOR (DrawingColor) and finall color is then:
782+SRCBLEND and DESTBLEND are defined using glBlendFunc function. In this tutorial we set SRCBLEND to GL_ZERO (zero) and DESTBLENT to GL_SRC_COLOR (DrawingColor) and finall color is then:
783783
784784`DrawingColor * 0 + BackgroundColor * DrawingColor`
785785
786-<figure>
787-<img src="TexturesPic1.jpg" title="TexturesPic1.jpg" />
788-<figcaption>TexturesPic1.jpg</figcaption>
789-</figure>
786+![](/wiki-images/d/d4/TexturesPic1.jpg)
790787
791788This means that background will get darker when you draw with dark colors... when you draw with white color, background color will not be changed. The result will look like this
792789
793790Next time, we'll use extensions to show how to use singlepass multitexturing.
794791
795792Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
796793
797794# Multitexturing (extensions)
798795
799796When youknow multipass multi texturing, singlepass is very easy. Texturing is separated in stages. First stage setup and draw first texture, second stage draws another one and so on. All you have to do is to setup texture stages and to render object.
800797
801798Let's see how code looks like:
802799
803800`procedure InitializeGL;`\
804801`begin`\
805802`  Load_GL_ARB_multitexture;`\
806803`  glClearColor(0, 0, 0, 0);`\
807804`  Tex1 := LoadGLTextureFromFile('Lazarus.bmp');`\
808805`  Tex2 := LoadGLTextureFromFile('Mask.bmp');`\
809806`  glActiveTextureARB(GL_TEXTURE0_ARB);`\
810807`  glEnable(GL_TEXTURE_2D);`\
811808`  glBindTexture(GL_TEXTURE_2D, Tex1);`\
812809`  glActiveTextureARB(GL_TEXTURE1_ARB);`\
813810`  glEnable(GL_TEXTURE_2D);`\
814811`  glBindTexture(GL_TEXTURE_2D, Tex2);`\
815812`end;`
816813
817-First we need load OpenGL extension that will allow us to use multitexture functions. <b>Load_GL_ARB_multitexture</b> will try to load those extensions and will return TRUE if operation was successful.
814+First we need load OpenGL extension that will allow us to use multitexture functions. Load_GL_ARB_multitexture will try to load those extensions and will return TRUE if operation was successful.
818815
819-To select texture stage you want to work on, use <b>glActiveTextureARB</b> function. It takes only one parameter that define which stage you want. After that all texture functions (enabling, disabling, binding, creating...) will affect that stage.
816+To select texture stage you want to work on, use glActiveTextureARB function. It takes only one parameter that define which stage you want. After that all texture functions (enabling, disabling, binding, creating...) will affect that stage.
820817
821818Since we setup every thing in initialization function, all we have to do is to draw object:
822819
823820`procedure DrawGLScene; cdecl;`\
824821`begin`\
825822`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
826-\
823+
827824`  glLoadIdentity;`\
828825`  glTranslatef(0, 0, -5);`\
829-\
826+
830827`  glBegin(GL_QUADS);`\
831828`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);`\
832829`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 0);`\
833830`    glVertex3f(2.516, 2, 0);`\
834831`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);`\
835832`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 0);`\
836833`    glVertex3f(-2.516, 2, 0);`\
837834`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);`\
838835`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 1);`\
839836`    glVertex3f(-2.516,-2, 0);`\
840837`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);`\
841838`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 1);`\
842839`    glVertex3f(2.516,-2, 0);`\
843840`  glEnd;`\
844-\
841+
845842`  glutSwapBuffers;`\
846843`end;`
847844
848-![](/wiki-images/5/52/MultitexturePic1.jpg) As you can see, difference is only in defining texture coordinations. We now use <b>glMultiTexCoord2fARB</b> function that takes texture stage and texture coordinations. Every thing else is unchanged.
845+![](/wiki-images/5/52/MultitexturePic1.jpg) As you can see, difference is only in defining texture coordinations. We now use glMultiTexCoord2fARB function that takes texture stage and texture coordinations. Every thing else is unchanged.
849846
850847Today almost all graphic cards supports at least 2 texture stages. Using singlepass multitexturing is faster than multipass version since you draw objects only once. If hardware supports singlepass multitexturing (Load_GL_ARB_multitexture returns TRUE) use it.
851848
852849Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
853850
854851# Render to texture
855852
856853This one will be short. OpenGL can capture current scene to texture so you can use it for texturing other objects (TV screen, mirror or some thing else). Well just render scene to texture and apply it to rotating plane.
857854
858855First, we must create empty texture which we'll use to capture scene:
859856
860857`procedure SetupRenderTexture;`\
861858`var`\
862859`  Data: Pointer;`\
863860`begin`\
864861`  GetMem(Data, 256*256*3);`\
865862`  glGenTextures(1, @RenderTexture);`\
866863`  glBindTexture(GL_TEXTURE_2D, RenderTexture);`\
867864`  glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, Data);`\
868865`  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);`\
869866`  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);`\
870867`  FreeMem(Data);`\
871868`end;`
872869
873870Buffer for 256*256 RGB image is created and it is used to setup 2D texture.
874871
875872Main part is in drawing function:
876873
877874`procedure DrawGLScene; cdecl;`\
878875`var`\
879876`  TotalTime: Single;`\
880877`begin`\
881878`  glClearColor(0, 0, 0, 0);`\
882879`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
883880`  glEnable(GL_LIGHTING);`\
884881`  glDisable(GL_TEXTURE_2D);`\
885882`  glViewport(0, 0, 256, 256);`\
886-\
883+
887884`  TotalTime := GetTotalTime;`\
888-\
885+
889886`  glLoadIdentity;`\
890887`  glTranslatef(0, 0, -5);`\
891888`  glRotatef(50 * TotalTime, 1, 0, 0);`\
892889`  glRotatef(100 * TotalTime, 0, 1, 0);`\
893890`  glRotatef(50 * TotalTime, 0, 0, 1);`\
894-\
891+
895892`  glColor3f(1, 1, 1);`\
896893`  glutSolidCube(2);`\
897-\
894+
898895`  glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 256, 256, 0);`\
899-\
896+
900897`  glClearColor(0.18, 0.20, 0.66, 0);`\
901898`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
902899`  glDisable(GL_LIGHTING);`\
903900`  glEnable(GL_TEXTURE_2D);`\
904901`  glViewport(0, 0, AppWidth, AppHeight);`\
905-\
902+
906903`  glLoadIdentity;`\
907904`  glTranslatef(0, 0, -7);`\
908905`  glRotatef(20 * TotalTime, 1, 0, 0);`\
909906`  glRotatef(50 * TotalTime, 0, 1, 0);`\
910-\
907+
911908`  glBegin(GL_QUADS);`\
912909`    glTexCoord2f(1, 0);`\
913910`    glVertex3f(2, 2, 0);`\
914911`    glTexCoord2f(0, 0);`\
915912`    glVertex3f(-2, 2, 0);`\
916913`    glTexCoord2f(0, 1);`\
917914`    glVertex3f(-2,-2, 0);`\
918915`    glTexCoord2f(1, 1);`\
919916`    glVertex3f(2,-2, 0);`\
920917`  glEnd;`\
921-\
918+
922919`  glutSwapBuffers;`\
923920`end;`
924921
925-![](/wiki-images/6/6e/RenderToTexturePic1.jpg) First, everything is setup for scene that will be captured. Viewport is reduced to 256*256 so it will fit into texture and scene is drawn. <b>glCopyTexImage2D</b> is used to capture scene to currently selected texture.
922+![](/wiki-images/6/6e/RenderToTexturePic1.jpg) First, everything is setup for scene that will be captured. Viewport is reduced to 256*256 so it will fit into texture and scene is drawn. glCopyTexImage2D is used to capture scene to currently selected texture.
926923
927924When we have scene captured to texture, everything can be cleared again, viewport can be returned to original size and final scene is drawn using previous scene as texture.
928925
929-P.S. Captured texture can be saved using <b>SaveGLTextureToFile</b> function from [Vampyre Imaging Library](http://imaginglib.sourceforge.net/).
926+P.S. Captured texture can be saved using SaveGLTextureToFile function from [Vampyre Imaging Library](http://imaginglib.sourceforge.net/).
930927
931928Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
932929
933930# Vertex array
934931
935932OpenGL is capable of rendering primitives using data that is stored in buffers insted of calling glVertex. Buffers can be used to define vertex and texture coordinates, and colors (index and RGBA), normals and edge flags.
936933
937934In this tutorial well use only vertex and color buffers, and we'll show non-indexed and indexed drawing. Non-indexed mode draws buffers as streams. Indexed mode will draw buffer elements in order that is defined in index buffer. But enough talking... let's start coding.
938935
939936First, let's define some types and constants:
940937
941938`type`\
942939`  TVertex3f = record`\
943940`    X, Y, Z: Single;`\
944941`  end;`\
945-\
942+
946943`  TColor3f = record`\
947944`   R, G, B: Single;`\
948945`  end;`\
949-\
946+
950947`  VertexBuffer: array [0..5] of TVertex3f = (`\
951948`    (X : 1; Y : 1; Z : 0),`\
952949`    (X : -1; Y : 1; Z : 0),`\
953950`    (X : -1; Y : -1; Z : 0),`\
954951`    (X : 1; Y : 1; Z : 0),`\
955952`    (X : -1; Y : -1; Z : 0),`\
956953`    (X : 1; Y : -1; Z : 0)`\
957954`  );`\
958955`  ColorBuffer: array [0..5] of TColor3f = (`\
959956`    (R : 1; G : 0; B : 1),`\
960957`    (R : 0; G : 0; B : 1),`\
961958`    (R : 0; G : 1; B : 0),`\
962959`    (R : 1; G : 0; B : 1),`\
963960`    (R : 0; G : 1; B : 0),`\
964961`    (R : 1; G : 1; B : 0)`\
965962`  );`
966963
967964We have two buffers. One for vertex coordinates and one for vertex colors. This 6 vertices defines 2 triangles that forms rectangle.
968965
969966Drawing primitives using buffers is easy:
970967
971968`  glEnableClientState(GL_VERTEX_ARRAY);`\
972969`  glEnableClientState(GL_COLOR_ARRAY);`\
973970`  glVertexPointer(3, GL_FLOAT, 0, @VertexBuffer[0]);`\
974971`  glColorPointer(3, GL_FLOAT, 0, @ColorBuffer[0]);`\
975-\
972+
976973`  glDrawArrays(GL_TRIANGLES, 0, Length(VertexBuffer));`\
977-\
974+
978975`  glDisableClientState(GL_VERTEX_ARRAY);`\
979976`  glDisableClientState(GL_COLOR_ARRAY);`
980977
981-First we enable buffers we want to use using <b>glEnableClientState</b> function. Than we can select buffers we want to use. Every buffer type has own function for selecting (<b>glColorPointer</b>, <b>glEdgeFlagPointer</b>, <b>glIndexPointer</b>, <b>glNormalPointer</b>, <b>glTexCoordPointer</b>, <b>glVertexPointer</b>). First parameter in those functions defines how many numbers every element contains. For example, let's take vertex buffer. If this parameter is 2 than OpenGL expects that every element in buffer contains x and y coordinate. If this parameter is, for example, 4, than every element should contains x, y, z and w coordinate. Next parameter defines what type of data element contains (GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT or GL_DOUBLE). Next one defines how many bytes are between each element. This way you can have buffer that contains vertex coordinates and some custom data. For arbitrary data type, this parameter can be calculated like this:
978+First we enable buffers we want to use using glEnableClientState function. Than we can select buffers we want to use. Every buffer type has own function for selecting (glColorPointer, glEdgeFlagPointer, glIndexPointer, glNormalPointer, glTexCoordPointer, glVertexPointer). First parameter in those functions defines how many numbers every element contains. For example, let's take vertex buffer. If this parameter is 2 than OpenGL expects that every element in buffer contains x and y coordinate. If this parameter is, for example, 4, than every element should contains x, y, z and w coordinate. Next parameter defines what type of data element contains (GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT or GL_DOUBLE). Next one defines how many bytes are between each element. This way you can have buffer that contains vertex coordinates and some custom data. For arbitrary data type, this parameter can be calculated like this:
982979
983980`type`\
984981`  TBufferData = record`\
985982`    DataBefore: TDataBefore;`\
986983`    Vertex: TVertex;`\
987984`    DataAfter: TDataAfter;`\
988985`  end;`\
989-\
986+
990987`Bytes between elements = SizeOf(TDataBefore) + SizeOf(TDataAfter)`
991988
992989Last parameter if pointer to the begginig of buffer.
993990
994-When buffers are selected we can draw them using <b>glDrawArrays</b> functions. All enabled buffers are used to draw primitives. What kind of polygons are being generated is defined in first parameter (same as in glBegin function). Next two defines subset of buffer which is used for drawing (start and count).
991+When buffers are selected we can draw them using glDrawArrays functions. All enabled buffers are used to draw primitives. What kind of polygons are being generated is defined in first parameter (same as in glBegin function). Next two defines subset of buffer which is used for drawing (start and count).
995992
996993When buffers are not needed you can disable them.
997994
998995To demonstrate indexed mode, I made some simple mesh class that can load vertex, color and index data from external files:
999996
1000997`type`\
1001998`  TMesh = class`\
1002999`  private`\
10031000`    FVertices: array of TVertex3f;`\
10041001`    FColors: array of TColor3f;`\
10051002`    FIndices: array of Integer;`\
10061003`    procedure FreeBuffers;`\
10071004`  public`\
10081005`    constructor Create;`\
10091006`    destructor Destroy; override;`\
10101007`    procedure LoadMesh(FileName: String);`\
10111008`    procedure DrawMesh;`\
10121009`  end;`
10131010
10141011FVertices will contain data about vertices, FColors data about color and FIndices data about indices when external file is loaded.
10151012
10161013First we'll write some code that deals with creation and destruction of class:
10171014
10181015`procedure TMesh.FreeBuffers;`\
10191016`begin`\
10201017`  FVertices := nil;`\
10211018`  FColors := nil;`\
10221019`  FIndices := nil;`\
10231020`end;`\
1024-\
1021+
10251022`constructor TMesh.Create;`\
10261023`begin`\
10271024`  FreeBuffers;`\
10281025`end;`\
1029-\
1026+
10301027`destructor TMesh.Destroy;`\
10311028`begin`\
10321029`  FreeBuffers;`\
10331030`  inherited Destroy;`\
10341031`end;`
10351032
10361033File that will contain mesh data is simple text file. First row will contain number of vertices and indices separated by space character. After that row will come rows for every vertex and color. X, Y, Z, R, G and B all separated by space character. In the end, there will be rows for indices... every index number is written in its own row... so, for one triangle, data file will look like this:
10371034
10381035`3 3`\
10391036`-1 -1 0 1 1 1`\
10401037`1 -1 0 1 1 1`\
10411038`0 1 0 1 1 1`\
10421039`0`\
10431040`1`\
10441041`2`
10451042
10461043This means that there is 3 vertices and 3 indices defined in file. First vrtex is at -1, -1, 0 and has color 1, 1, 1 and so on. Indices defines that order in which vertices are drawn (in this case vertices are drawn in the same order as they are defined).
10471044
10481045Code for loading this data will loke like this:
10491046
10501047`procedure TMesh.LoadMesh(FileName: String);`\
10511048`var`\
10521049`  MeshFile: TextFile;`\
10531050`  VertexCount, IndexCount: Integer;`\
10541051`  iV, iI: Integer;`\
10551052`begin`\
10561053`  FreeBuffers;`\
1057-\
1054+
10581055`  AssignFile(MeshFile, FileName);`\
10591056`  Reset(MeshFile);`\
1060-\
1057+
10611058`  ReadLn(MeshFile, VertexCount, IndexCount);`\
1062-\
1059+
10631060`  SetLength(FVertices, VertexCount);`\
10641061`  SetLength(FColors, VertexCount);`\
10651062`  SetLength(FIndices, IndexCount);`\
1066-\
1063+
10671064`  for iV := 0 to VertexCount - 1 do`\
10681065`    ReadLn(MeshFile,`\
10691066`      FVertices[iV].X, FVertices[iV].Y, FVertices[iV].Z,`\
10701067`      FColors[iV].R, FColors[iV].G, FColors[iV].B);`\
1071-\
1068+
10721069`  for iI := 0 to IndexCount - 1 do`\
10731070`    ReadLn(MeshFile, FIndices[iI]);`\
1074-\
1071+
10751072`  CloseFile(MeshFile);`\
10761073`end;`
10771074
10781075After loading data, we have everything for drawing:
10791076
10801077`procedure TMesh.DrawMesh;`\
10811078`begin`\
10821079`  glEnableClientState(GL_VERTEX_ARRAY);`\
10831080`  glEnableClientState(GL_COLOR_ARRAY);`\
10841081`  glVertexPointer(3, GL_FLOAT, 0, @FVertices[0]);`\
10851082`  glColorPointer(3, GL_FLOAT, 0, @FColors[0]);`\
1086-\
1083+
10871084`  glDrawElements(GL_TRIANGLES, Length(FIndices), GL_UNSIGNED_INT, @FIndices[0]);`\
1088-\
1085+
10891086`  glDisableClientState(GL_VERTEX_ARRAY);`\
10901087`  glDisableClientState(GL_COLOR_ARRAY);`\
10911088`end;`
10921089
1093-As you can see, allmost everything is the same as for non-indexed drawing, except function that actually draw polygons. In this case we use <b>glDrawElements</b> function. For this one we specify what kind of polygons we want, how many indices are in index buffer, type of data in index buffer and pointer to the beginning of index buffer.
1090+As you can see, allmost everything is the same as for non-indexed drawing, except function that actually draw polygons. In this case we use glDrawElements function. For this one we specify what kind of polygons we want, how many indices are in index buffer, type of data in index buffer and pointer to the beginning of index buffer.
10941091
10951092![](/wiki-images/7/77/VertexArrayPic1.jpg)Full source code comes with mesh data file that this class can use to generates rectangle that is identical with one that is drawn using non-indexed mode. Mesh data file looks like this:
10961093
10971094`4 6`\
10981095`1 1 0 1 0 1`\
10991096`-1 1 0 0 0 1`\
11001097`-1 -1 0 0 1 0`\
11011098`1 -1 0 1 1 0`\
11021099`0`\
11031100`1`\
11041101`2`\
11051102`0`\
11061103`2`\
11071104`3`
11081105
11091106As you can see, there is data for only 4 vertices and 6 indices. So, first triangle is defined by vertices 0, 1 and 2, and the seccond one by vertices 0, 2 and 3. By using indexed mode we don't have to duplicate vertices.
11101107
11111108Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
11121109
11131110------------------------------------------------------------------------
11141111
11151112[[Creating bindings for C libraries]]
11161113
11171114---
11181115*From the Free Pascal wiki, page "OpenGL Tutorial/zh CN", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history