Sign in Join
Wiki copytemporary

OpenGL Tutorial/ja: what changed

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

OldNewLine
11# 概要
22
33OpenGLは2Dや3DのインタラクティブなグラフィックスアプリケーションをOSなどに依存せず作成できる、優れた開発環境です。OpenGLは1992年から開発がすすめられ、いまや2D、3DグラフィックスアプリケーションのAPIとしては、数多くのアプリケーションで使われ、いろんなコンピュータプラットホームで利用でき、業界標準になっています。
44
55OpenGLは、様々な種類のレンダリング、テクスチャマッピング、特殊効果、および他の強力なビジュアライゼーション機能を取り入れており、革新的な、アプリケーション開発を加速できます。
66
77開発者はすべてのポピュラーなデスクトップとワークステーションプラットホームに、OpenGLのパワーを利用することができますので、広いアプリケーション展開が可能になります。もっと詳しい情報は、 [OpenGLのサイト](http://www.opengl.org/about/overview/)で得ることができます。
88
99GLUTはOpenGL Utility Toolkitですの略です。OpenGLを使ったプログラムを書くために、どのようなウィンドウシステムで利用できるツールキットです。 これは、簡単なウインドウをもつOpenGLを使うプログラムで使える、アプリケーションプログラミングインターフェース(API)を実装します。(訳注:GLUTを使えば、ウインドウシステム、プラットフォームの違いを吸収してくれます。) GLUTを使えば、OpenGLプログラミングを学んだり、調査すしたりすることが簡単になります。 また、いろいろなPCとワークステーション、OSにわたって動作するひとつのOpenGLプログラムに書けるのと同様に、GLUTは携帯用のAPIも提供しています。もっと詳しい情報は、 [OpenGLのGLUTのサイト](http://www.opengl.org/resources/libraries/glut/)で得ることができます。
1010
1111多くのOSにGLUTがプレインストールされています。もし、自分の環境にあったものが分からない場合は、 [Google](http://www.google.com/)で調べてみてください。
1212
1313Windowsのバイナリは、[www.xmission.com](http://www.xmission.com/~nate/glut.html)でダウンロードできます。
1414
1515# 最初のGLUTプログラムを作ろう
1616
17-GLUTを使うために、初期化処理をおこなう必要があります。これは、<b>glutInit</b>関数を使って行えます。 この関数はコマンドラインを解析し、メインウインドウのパラメータをセットします。しかし、それは、C/C++形式の入力を前提としています。 あなたは、自分の関数内で、ParamCountとParamStrから、C/C++風のコマンドラインパラメータに変換する必要があります。
17+GLUTを使うために、初期化処理をおこなう必要があります。これは、glutInit関数を使って行えます。 この関数はコマンドラインを解析し、メインウインドウのパラメータをセットします。しかし、それは、C/C++形式の入力を前提としています。 あなたは、自分の関数内で、ParamCountとParamStrから、C/C++風のコマンドラインパラメータに変換する必要があります。
1818
1919`procedure glutInitPascal(ParseCmdLine: Boolean); `\
2020`var`\
2121`  Cmd: array of PChar;`\
2222`  CmdCount, I: Integer;`\
2323`begin`\
2424`  if ParseCmdLine then`\
2525`    CmdCount := ParamCount + 1`\
2626`  else`\
2727`    CmdCount := 1;`\
2828`  SetLength(Cmd, CmdCount);`\
2929`  for I := 0 to CmdCount - 1 do`\
3030`    Cmd[I] := PChar(ParamStr(I));`\
3131`  glutInit(@CmdCount, @Cmd);`\
3232`end;`
3333
3434要するに、配列を作って、ParamStrからの文字列をセットしています。この関数は、glutInitに渡して、制御するためのパラメータを取ることができます。コマンドライン、もしくは、実行ファイルそのものの名です。
3535
3636glutInitについての情報は: <http://www.opengl.org/resources/libraries/glut/spec3/node10.html>
3737
38-次に、メインウインドウを生成する必要があります。 メインウインドウに、<b>glutInitDisplayMode</b>を使って、ディスプレイモードを設定します。 これは、フラグの組み合わせで表現されるただ1つのパラメータを取ります。 通常は、<b>GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH</b> で充分でしょう。
38+次に、メインウインドウを生成する必要があります。 メインウインドウに、glutInitDisplayModeを使って、ディスプレイモードを設定します。 これは、フラグの組み合わせで表現されるただ1つのパラメータを取ります。 通常は、GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH で充分でしょう。
3939
4040glutInitDisplayModeについての情報は: <http://www.opengl.org/resources/libraries/glut/spec3/node12.html>
4141
42-The position and size of the window is controlled using ウインドウの位置とサイズは、 <b>glutInitWindowPosition</b> と、 <b>glutInitWindowSize</b>.によって制御されます。 これらは、2個のパラメータをとります。 最初のものはX and Y の組、後のものは、幅と高さです。ウインドウのサイズと中心を得るために、<b>glutGet</b>を使うこともできます。
42+The position and size of the window is controlled using ウインドウの位置とサイズは、 glutInitWindowPosition と、 glutInitWindowSize.によって制御されます。 これらは、2個のパラメータをとります。 最初のものはX and Y の組、後のものは、幅と高さです。ウインドウのサイズと中心を得るために、glutGetを使うこともできます。
4343
4444glutInitWindowPosition, glutInitWindowSize と glutGetについての情報は: <http://www.opengl.org/resources/libraries/glut/spec3/node11.html> <http://www.opengl.org/documentation/specs/glut/spec3/node70.html>
4545
46-最後に、<b>glutCreateWindow</b> 関数を使って、ウインドウを生成します。 これは、ウインドウを生成し、パラメータを通してキャプションを設定します。
46+最後に、glutCreateWindow 関数を使って、ウインドウを生成します。 これは、ウインドウを生成し、パラメータを通してキャプションを設定します。
4747
4848結果として、この関数はウインドウのハンドルを返します。これは、他の関数で必要とされて使われる事があります。
4949
5050glutCreateWindowについての情報は: <http://www.opengl.org/resources/libraries/glut/spec3/node16.html>
5151
52-プログラムがメインループにはいる前に、いくつかコールバックを設定しておきます。ウインドウを描画するとき、リサイズするとき、キーボード入力があったとき、などに、コールバックを必要とします。これらのコールバックは、<b>glutDisplayFunc</b>、 <b>glutReshapeFunc</b> 、 <b>glutKeyboardFunc</b>を使って設定します。
52+プログラムがメインループにはいる前に、いくつかコールバックを設定しておきます。ウインドウを描画するとき、リサイズするとき、キーボード入力があったとき、などに、コールバックを必要とします。これらのコールバックは、glutDisplayFunc、 glutReshapeFunc 、 glutKeyboardFuncを使って設定します。
5353
5454コールバックについての情報は: <http://www.opengl.org/resources/libraries/glut/spec3/node45.html#SECTION00080000000000000000>
5555
5656描画関数は、このようになるでしょう。
5757
5858`procedure DrawGLScene; cdecl;`\
5959`begin`\
6060`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
6161`  glutSwapBuffers;`\
6262`end;`
6363
6464これは、単にウインドウをバックグラウンド色でクリアして、Zバッファをリセットします。(Zバッファについては、後で説明するので分からなくても気にしないでください。)
6565
6666リサイズ関数は、次のようになるでしょう。
6767
6868`procedure ReSizeGLScene(Width, Height: Integer); cdecl;`\
6969`begin`\
7070`  if Height = 0 then`\
7171`    Height := 1;`\
72-\
72+
7373`  glViewport(0, 0, Width, Height);`\
7474`  glMatrixMode(GL_PROJECTION);`\
7575`  glLoadIdentity;`\
7676`  gluPerspective(45, Width / Height, 0.1, 1000);`\
77-\
77+
7878`  glMatrixMode(GL_MODELVIEW);`\
7979`  glLoadIdentity;`\
8080`end;`
8181
8282このコードで、OpenGLにウインドウのどの部分に描画し、必要な値を適切な行列に設定することができます。 (行列関数は、後で説明します。)
8383
8484キーボード入力は、次の関数で評価されます。
8585
8686`procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;`\
8787`begin`\
8888`  if Key = 27 then`\
8989`    Halt(0);`\
9090`end;`
9191
92-この関数はESCキーを押すとプログラムを終了するようにします。GLUTはイベントドリブンであり、プログラムを終了するためには、コールバック関数の中で<b>Halt</b>関数をコールします。
92+この関数はESCキーを押すとプログラムを終了するようにします。GLUTはイベントドリブンであり、プログラムを終了するためには、コールバック関数の中でHalt関数をコールします。
9393
9494もし、別の方法でウインドウをクローズすると、ウインドウは消えても、プログラムは見えないところでループを続けているでしょう。
9595
96-メインループを開始するには、<b>glutMainLoop</b>をコールします。 これは、終了しないループに突入し、コールバック関数をコールしていきます。 プログラムの主要部分は次のようになるでしょう。
96+メインループを開始するには、glutMainLoopをコールします。 これは、終了しないループに突入し、コールバック関数をコールしていきます。 プログラムの主要部分は次のようになるでしょう。
9797
9898`const `\
9999`  AppWidth = 640; `\
100100`  AppHeight = 480; `\
101-\
101+
102102`procedure InitializeGL; `\
103103`begin `\
104104`  glClearColor(0.18, 0.20, 0.66, 0); `\
105105`end; `\
106-\
106+
107107`var `\
108108`  ScreenWidth, ScreenHeight: Integer; `\
109109`begin `\
110110`  glutInitPascal(True); `\
111111`  glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH); `\
112112`  glutInitWindowSize(AppWidth, AppHeight); `\
113113`  ScreenWidth := glutGet(GLUT_SCREEN_WIDTH); `\
114114`  ScreenHeight := glutGet(GLUT_SCREEN_HEIGHT); `\
115115`  glutInitWindowPosition((ScreenWidth - AppWidth) div 2,`\
116116`    (ScreenHeight - AppHeight) div 2); `\
117117`  glutCreateWindow('OpenGL Tutorial 1'); `\
118-\
118+
119119`  InitializeGL; `\
120-\
120+
121121`  glutDisplayFunc(@DrawGLScene); `\
122122`  glutReshapeFunc(@ReSizeGLScene); `\
123123`  glutKeyboardFunc(@GLKeyboard); `\
124-\
124+
125125`  glutMainLoop; `\
126126`end.`
127127
128128次のチュートリアルでは、少しコードを追加して、簡単な図形を書いてみます。
129129
130130下記でソースコードやlinux/Windowsの実行ファイルをダウンロードできます。 [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
131131
132132# 単純な形状を描画する
133133
134134今回は、少しだけコードを追加して、OpenGLの関数の説明にフォーカスします。
135135
136136これまでのコードで説明します。
137137
138138`  .`\
139139`  .`\
140140`  .`\
141141`  glMatrixMode(GL_PROJECTION);`\
142142`  glLoadIdentity;`\
143143`  gluPerspective(45, Width / Height, 0.1, 1000);`\
144-\
144+
145145`  glMatrixMode(GL_MODELVIEW);`\
146146`  glLoadIdentity;`\
147147`end;`
148148
149-<b>glMatrixMode</b>関数で、変更したい行列を選択します。 OpenGL には、3つの行列があります。
149+glMatrixMode関数で、変更したい行列を選択します。 OpenGL には、3つの行列があります。
150150
151-<b>GL_MODELVIEW</b>: この行列は、頂点をモデル空間へ移動するためのものです。
151+GL_MODELVIEW: この行列は、頂点をモデル空間へ移動するためのものです。
152152
153-<b>GL_PROJECTION</b>: この行列は、3次元の位置を2次元の最終的に表示するピクセル位置に変換するためのものです。
153+GL_PROJECTION: この行列は、3次元の位置を2次元の最終的に表示するピクセル位置に変換するためのものです。
154154
155-<b>GL_TEXTURE</b>: この行列はテクスチャ座標を変換するためのものです。
155+GL_TEXTURE: この行列はテクスチャ座標を変換するためのものです。
156156
157157いったん、変更したい行列を選択したら、行列値に影響を与える関数をコールします。
158158
159-<b>glLoadIdentity</b> は、行列をリセットするので、頂点位置に影響をあたえなくなります。 ほとんどすべての行列関数は、現在の行列に、生成した一つの行列を掛けるので、時々この関数で行列をクリアする必要があるでしょう。 (訳注:glLoadIdentityは、変換行列を単位行列にする。reset(リセット)や、clear(クリアする)、というと、プログラミング的な表現では、ゼロ行列をおもいうかべそうだが、原文作者の数学的な観念の表現であると考えて、そのまま和訳した)
159+glLoadIdentity は、行列をリセットするので、頂点位置に影響をあたえなくなります。 ほとんどすべての行列関数は、現在の行列に、生成した一つの行列を掛けるので、時々この関数で行列をクリアする必要があるでしょう。 (訳注:glLoadIdentityは、変換行列を単位行列にする。reset(リセット)や、clear(クリアする)、というと、プログラミング的な表現では、ゼロ行列をおもいうかべそうだが、原文作者の数学的な観念の表現であると考えて、そのまま和訳した)
160160
161-視野行列を設定したいなら、 <b>gluPerspective</b> を使うことができます。4つのパラメータ(アスペクト比、視野の近い側の面、遠い側の面)、が視野をあらわします。これは、シンプルです。
161+視野行列を設定したいなら、 gluPerspective を使うことができます。4つのパラメータ(アスペクト比、視野の近い側の面、遠い側の面)、が視野をあらわします。これは、シンプルです。
162162
163163(訳注:OpenGLの観念としては、視野は、体積としてとらえるので、視野体積として空間を捉えるとよいだろう。) それで、モデル行列を変更できます。この時点で、固有のものをセットすればよいでしょう。 よろしいでしょうか。それでは、最初の形状を書いてみましょう。
164164
165165`procedure DrawGLScene; cdecl;`\
166166`begin`\
167167`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
168-\
168+
169169`  glLoadIdentity;`\
170170`  glTranslatef(0, 0, -5);`\
171-\
171+
172172`  glBegin(GL_TRIANGLES);`\
173173`    glColor3f(1, 0, 0);`\
174174`    glVertex3f(-1, -1, 0);`\
175-\
175+
176176`    glColor3f(0, 1, 0);`\
177177`    glVertex3f(1, -1, 0);`\
178-\
178+
179179`    glColor3f(0, 0, 1);`\
180180`    glVertex3f(0, 1, 0);`\
181181`  glEnd;`\
182-\
182+
183183`  glutSwapBuffers;`\
184184`end;`
185185
186186すでに、glClear関数は使いましたが、これは、バッファをリセットします。 次の2個の関数はスキップして、描画関数に注目します。
187187
188-<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.
188+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.
189189
190190GL_LINES: Treats each pair of vertices as an independent line segment. Vertices 2n-1 and 2n define line n. n/2 lines are drawn.
191191
192192GL_LINE_STRIP: Draws a connected group of line segments from the first vertex to the last. n-1 lines are drawn.
193193
194194GL_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.
195195
196196GL_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.
197197
198198GL_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.
199199
200200GL_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.
201201
202202GL_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.
203203
204204GL_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.
205205
206206GL_POLYGON: Draws a single, convex polygon. Vertices 1 through n define this polygon.
207207
208-![](/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.
208+![](/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.
209209
210-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.
210+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.
211211
212-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.
212+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.
213213
214-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.
214+In the end you call glEnd functions that finishes drawing. You could now start another drawing block with new glBegin function if you wish.
215215
216216Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
217217
218218# Using display lists
219219
220-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.
220+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.
221221
222222`const`\
223223`  LIST_OBJECT = 1;`\
224-\
224+
225225`procedure CreateList;`\
226226`begin`\
227227`  glNewList(LIST_OBJECT, GL_COMPILE);`\
228228`    glBegin(GL_TRIANGLE_FAN);`\
229229`      glColor3f(1, 0, 0);`\
230230`      glVertex3f(0, 0.5, 0);`\
231-\
231+
232232`      glColor3f(1, 1, 0);`\
233233`      glVertex3f(-0.5, -0.5, 0.5);`\
234-\
234+
235235`      glColor3f(1, 1, 1);`\
236236`      glVertex3f(0.5, -0.5, 0.5);`\
237-\
237+
238238`      glColor3f(0, 1, 1);`\
239239`      glVertex3f(0.5, -0.5, -0.5);`\
240-\
240+
241241`      glColor3f(0, 0, 1);`\
242242`      glVertex3f(-0.5, -0.5, -0.5);`\
243-\
243+
244244`      glColor3f(0, 1, 0);`\
245245`      glVertex3f(-0.5, -0.5, 0.5);`\
246246`    glEnd;`\
247-\
247+
248248`    glBegin(GL_QUADS);`\
249249`      glColor3f(1, 1, 0);`\
250250`      glVertex3f(-0.5, -0.5, 0.5);`\
251-\
251+
252252`      glColor3f(1, 1, 1);`\
253253`      glVertex3f(0.5, -0.5, 0.5);`\
254-\
254+
255255`      glColor3f(0, 1, 1);`\
256256`      glVertex3f(0.5, -0.5, -0.5);`\
257-\
257+
258258`      glColor3f(0, 0, 1);`\
259259`      glVertex3f(-0.5, -0.5, -0.5);`\
260-\
260+
261261`      glColor3f(0, 1, 0);`\
262262`      glVertex3f(-0.5, -0.5, 0.5);`\
263263`    glEnd;`\
264264`  glEndList;`\
265265`end;`
266266
267-<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.
267+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.
268268
269-<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
269+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
270270
271271All created lists should be deleted. You will do that before program exits:
272272
273273`procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;`\
274274`begin`\
275275`  if Key = 27 then`\
276276`  begin`\
277277`    glDeleteLists(LIST_OBJECT, 1);`\
278278`    Halt(0);`\
279279`  end;`\
280280`end;`
281281
282-<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
282+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
283283
284284Now you know how to create and delete display lists... let's see how to draw them:
285285
286286`procedure DrawGLScene; cdecl;`\
287287`begin`\
288288`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
289-\
289+
290290`  glLoadIdentity;`\
291291`  glTranslatef(-2, 0, -5);`\
292292`  glRotatef(40, 1, 0, 1);`\
293293`  glCallList(LIST_OBJECT);`\
294-\
294+
295295`  glLoadIdentity;`\
296296`  glTranslatef(1, -2, -10);`\
297297`  glRotatef(62, 0, 1, 0);`\
298298`  glCallList(LIST_OBJECT);`\
299-\
299+
300300`  glLoadIdentity;`\
301301`  glTranslatef(-4, 0.5, -15);`\
302302`  glRotatef(200, 1, 0, 0);`\
303303`  glCallList(LIST_OBJECT);`\
304-\
304+
305305`  glutSwapBuffers;`\
306306`end;`
307307
308-![](/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:
308+![](/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:
309309
310310GL_BYTE: list is treated as an array of signed bytes, each in the range -128 through 127.
311311
312312GL_UNSIGNED_BYTE: list is treated as an array of unsigned bytes, each in the range 0 through 255.
313313
314314GL_SHORT: list is treated as an array of signed two-byte integers, each in the range -32768 through 32767.
315315
316316GL_UNSIGNED_SHORT: list is treated as an array of unsigned two-byte integers, each in the range 0 through 65535.
317317
318318GL_INT: lists is treated as an array of signed four-byte integers.
319319
320320GL_UNSIGNED_INT: list is treated as an array of unsigned four-byte integers.
321321
322322GL_FLOAT: list is treated as an array of four-byte floating-point values.
323323
324324GL_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.
325325
326326GL_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.
327327
328328GL_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.
329329
330330That 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.
331331
332332Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
333333
334334# Full screen animation
335335
336336Entering full screen mode is easy with GLUT. Let's change main part of the program:
337337
338338`const`\
339339`  FSMode = '800x600:32@75';`\
340-\
340+
341341`begin`\
342342`  glutInitPascal(False);`\
343343`  glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH);`\
344344`  glutGameModeString(FSMode);`\
345345`  glutEnterGameMode;`\
346346`  glutSetCursor(GLUT_CURSOR_NONE);`\
347-\
347+
348348`  InitializeGL;`\
349-\
349+
350350`  glutDisplayFunc(@DrawGLScene);`\
351351`  glutReshapeFunc(@ReSizeGLScene);`\
352352`  glutKeyboardFunc(@GLKeyboard);`\
353353`  glutIdleFunc(@DrawGLScene);`\
354-\
354+
355355`  glutMainLoop;`\
356356`end.`
357357
358-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:
358+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:
359359
360360`[width "x" height][":" bpp]["@" hertz]`
361361
362362In 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.
363363
364-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:
364+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:
365365
366366`GLUT_CURSOR_RIGHT_ARROW`\
367367`GLUT_CURSOR_LEFT_ARROW`\
368368`GLUT_CURSOR_INFO`\
369369`GLUT_CURSOR_DESTROY`\
370370`GLUT_CURSOR_HELP`\
371371`GLUT_CURSOR_CYCLE`\
372372`GLUT_CURSOR_SPRAY`\
373373`GLUT_CURSOR_WAIT`\
374374`GLUT_CURSOR_TEXT`\
375375`GLUT_CURSOR_CROSSHAIR`\
376376`GLUT_CURSOR_UP_DOWN`\
377377`GLUT_CURSOR_LEFT_RIGHT`\
378378`GLUT_CURSOR_TOP_SIDE`\
379379`GLUT_CURSOR_BOTTOM_SIDE`\
380380`GLUT_CURSOR_LEFT_SIDE`\
381381`GLUT_CURSOR_RIGHT_SIDE`\
382382`GLUT_CURSOR_TOP_LEFT_CORNER`\
383383`GLUT_CURSOR_TOP_RIGHT_CORNER`\
384384`GLUT_CURSOR_BOTTOM_RIGHT_CORNER`\
385385`GLUT_CURSOR_BOTTOM_LEFT_CORNER`\
386386`GLUT_CURSOR_FULL_CROSSHAIR`\
387387`GLUT_CURSOR_NONE`\
388388`GLUT_CURSOR_INHERIT`
389389
390-<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.
390+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.
391391
392392Now, let's look at the program termination where you need to exit full screen mode:
393393
394394`procedure GLKeyboard(Key: Byte; X, Y: Longint); cdecl;`\
395395`begin`\
396396`  if Key = 27 then`\
397397`  begin`\
398398`    glutLeaveGameMode;`\
399399`    Halt(0);`\
400400`  end;`\
401401`end;`
402402
403-As you can see, all you need to do is to call <b>glutLeaveGameMode</b>.
403+As you can see, all you need to do is to call glutLeaveGameMode.
404404
405405Now, we'll introduce some new matrix functions. First, let's change ReSizeGLScene function:
406406
407407`procedure ReSizeGLScene(Width, Height: Integer); cdecl;`\
408408`begin`\
409409`  .`\
410410`  .`\
411411`  .`
412412
413413`  glMatrixMode(GL_MODELVIEW);`\
414414`  glLoadIdentity;`\
415415`  gluLookAt(0, 20, 25, 0, 0, 0, 0, 1, 0);`\
416416`end;`
417417
418-<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.
418+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.
419419
420420OK, 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:
421421
422422`procedure DrawGLScene; cdecl;`\
423423`var`\
424424`  T: Single;`\
425425`begin`\
426426`  T := glutGet(GLUT_ELAPSED_TIME) / 1000;`\
427427`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
428-\
428+
429429`  glPushMatrix;`\
430430`    glRotatef(5 * T, 0, 1, 0);`\
431431`    glColor3f(1, 1, 0);`\
432432`    glutWireSphere(2, 20, 20);`\
433433`  glPopMatrix;`\
434-\
434+
435435`  glPushMatrix;`\
436436`    glRotatef(90 * T, 0, 1, 0);`\
437437`    glTranslatef(5, 0, 0);`\
438438`    glRotatef(40 * T, 0, 1, 0);`\
439439`    glColor3f(1, 0, 0);`\
440440`    glutWireSphere(0.6, 10, 10);`\
441441`  glPopMatrix;`\
442-\
442+
443443`  glPushMatrix;`\
444444`    glRotatef(60 * T, 0, 1, 0);`\
445445`    glTranslatef(-3, 0, 9);`\
446446`    glRotatef(50 * T, 0, 1, 0);`\
447447`    glColor3f(0, 1, 0);`\
448448`    glutWireSphere(1, 16, 16);`\
449-\
449+
450450`    glPushMatrix;`\
451451`      glRotatef(360 * T, 0, 1, 0);`\
452452`      glTranslatef(-1.7, 0, 0);`\
453453`      glRotatef(50 * T, 0, 1, 0);`\
454454`      glColor3f(0, 0, 1);`\
455455`      glutWireSphere(0.4, 10, 10);`\
456456`    glPopMatrix;`\
457-\
457+
458458`  glPopMatrix;`\
459-\
459+
460460`  glutSwapBuffers;`\
461461`end;`
462462
463-![](/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.
463+![](/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.
464464
465-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.
465+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.
466466
467-<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.
467+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.
468468
469469Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
470470
471471# Light
472472
473473This 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.
474474
475475For 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.
476476
477477`unit utils;`\
478-\
478+
479479`{$mode objfpc}{$H+}`\
480-\
480+
481481`interface`\
482-\
482+
483483`uses`\
484484`  glut;`\
485-\
485+
486486`function GetTotalTime: Single;`\
487487`function GetDeltaTime: Single;`\
488488`procedure FrameRendered(Count: Integer = 1);`\
489489`function GetFPS: Single;`\
490-\
490+
491491`implementation`\
492-\
492+
493493`var`\
494494`  OldTime: Integer = 0;`\
495495`  FPSTime: Integer = 0;`\
496496`  FPSCount: Integer = 0;`\
497-\
497+
498498`function GetTotalTime: Single;`\
499499`begin`\
500500`  Result := glutGet(GLUT_ELAPSED_TIME) / 1000;`\
501501`end;`\
502-\
502+
503503`function GetDeltaTime: Single;`\
504504`var`\
505505`  NewTime: Integer;`\
506506`begin`\
507507`  NewTime := glutGet(GLUT_ELAPSED_TIME);`\
508508`  Result := (NewTime - OldTime) / 1000;`\
509509`  OldTime := NewTime;`\
510510`end;`\
511-\
511+
512512`procedure FrameRendered(Count: Integer);`\
513513`begin`\
514514`  Inc(FPSCount, Count);`\
515515`end;`\
516-\
516+
517517`function GetFPS: Single;`\
518518`var`\
519519`  NewTime: Integer;`\
520520`begin`\
521521`  NewTime := glutGet(GLUT_ELAPSED_TIME);`\
522-\
522+
523523`  Result := FPSCount / ((NewTime - FPSTime) / 1000);`\
524-\
524+
525525`  FPSTime := NewTime;`\
526526`  FPSCount := 0;`\
527527`end;`\
528-\
528+
529529`end.`
530530
531531As 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.
532532
533533Now, let's have fun with lights.
534534
535535OpenGL have several types of light... ambient, diffuse, point, spot, specular and emissive light.
536536
537537Ambient 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.
538538
539539Diffuse light can be represented as parallel light rays comming from far away. They will lit only vertices that are oriented towards the light source.
540540
541541Point 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.
542542
543543Spot 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.
544544
545545Just 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.
546546
547547Emissive 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.
548548
549549For simplicity we'll use only diffuse light in this tutorial. Later on, some other lights may appear in tutorials :)
550550
551551Let's see how to enable light in scene:
552552
553553`const`\
554554`  DiffuseLight: array[0..3] of GLfloat = (0.8, 0.8, 0.8, 1);`\
555-\
555+
556556`  glEnable(GL_LIGHTING);`\
557557`  glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight);`\
558558`  glEnable(GL_LIGHT0);`
559559
560-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.
560+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.
561561
562562More about glLightfv: <http://www.opengl.org//documentation/specs/man_pages/hardcopy/GL/html/gl/light.html>
563563
564564If 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:
565565
566566`glEnable(GL_COLOR_MATERIAL);`\
567567`glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);`
568568
569-![](/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.
569+![](/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.
570570
571571One 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.
572572
573573After all those setting ups, light will shine up your cube :)
574574
575575Part of the text is copied from [The OpenGL Light Bible](http://www.falloutsoftware.com/tutorials/gl/gl8.htm)
576576
577577Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
578578
579579# Bitmap fonts
580580
581581Games and programs usually need to write some text on screen. GLUT provides several functions for drawing chars that are platform independent.
582582
583583First, we'll show how to use default bitmap fonts. Almost all code additions will be made to utils.pas unit.
584584
585585Since text will be drawn in 2D, we'll need to know width and height of viewport... so, we'll write two functions for that:
586586
587587`function glGetViewportWidth: Integer;`\
588588`var`\
589589`  Rect: array[0..3] of Integer;`\
590590`begin`\
591591`  glGetIntegerv(GL_VIEWPORT, @Rect);`\
592592`  Result := Rect[2] - Rect[0];`\
593593`end;`\
594-\
594+
595595`function glGetViewportHeight: Integer;`\
596596`var`\
597597`  Rect: array[0..3] of Integer;`\
598598`begin`\
599599`  glGetIntegerv(GL_VIEWPORT, @Rect);`\
600600`  Result := Rect[3] - Rect[1];`\
601601`end;`
602602
603603We just get left/right, top/bottom and calculate width/height by subtracting them.
604604
605605There must be functions for entering and leaving 2D mode:
606606
607607`procedure glEnter2D;`\
608608`begin`\
609609`  glMatrixMode(GL_PROJECTION);`\
610610`  glPushMatrix;`\
611611`  glLoadIdentity;`\
612612`  gluOrtho2D(0, glGetViewportWidth, 0, glGetViewportHeight);`\
613-\
613+
614614`  glMatrixMode(GL_MODELVIEW);`\
615615`  glPushMatrix;`\
616616`  glLoadIdentity;`\
617-\
617+
618618`  glDisable(GL_DEPTH_TEST);`\
619619`end;`\
620-\
620+
621621`procedure glLeave2D;`\
622622`begin`\
623623`  glMatrixMode(GL_PROJECTION);`\
624624`  glPopMatrix;`\
625625`  glMatrixMode(GL_MODELVIEW);`\
626626`  glPopMatrix;`\
627-\
627+
628628`  glEnable(GL_DEPTH_TEST);`\
629629`end;`
630630
631-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.
631+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.
632632
633633Leaving 2D mode just returns old matrices and enable ZBuffer.
634634
635635Now, we can create function for text drawing:
636636
637637`procedure glWrite(X, Y: GLfloat; Font: Pointer; Text: String);`\
638638`var`\
639639`  I: Integer;`\
640640`begin`\
641641`  glRasterPos2f(X, Y);`\
642642`  for I := 1 to Length(Text) do`\
643643`    glutBitmapCharacter(Font, Integer(Text[I]));`\
644644`end;`
645645
646-<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.
646+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.
647647
648-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.
648+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.
649649
650650This new functions will make text drawing very easy:
651651
652652`procedure DrawGLScene; cdecl;`\
653653`begin`\
654654`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
655-\
655+
656656`  glLoadIdentity;`\
657657`  glTranslatef(0, 0, -5);`\
658658`  glRotatef(GetTotalTime * 10, 0, 0.5, 0.5);`\
659-\
659+
660660`  glColor3f(1, 0, 0);`\
661661`  glutSolidCube(2);`\
662-\
662+
663663`  glEnter2D;`\
664-\
664+
665665`  glColor3f(0.2, 0.8 + 0.2 * Sin(GetTotalTime * 5), 0);`\
666666`  glWrite(20, glGetViewportHeight - 20, GLUT_BITMAP_8_BY_13,`\
667667`    Format('OpenGL Tutorial :: Bitmap Fonts :: FPS - %.2f FPS', [FPS]));`\
668-\
668+
669669`  glColor3f(1, 1, 1);`\
670670`  glWrite(50, glGetViewportHeight - 60, GLUT_BITMAP_9_BY_15, 'GLUT_BITMAP_9_BY_15');`\
671671`  glWrite(50, glGetViewportHeight - 90, GLUT_BITMAP_8_BY_13, 'GLUT_BITMAP_8_BY_13');`\
672672`  glWrite(50, glGetViewportHeight - 120, GLUT_BITMAP_TIMES_ROMAN_10, 'GLUT_BITMAP_TIMES_ROMAN_10');`\
673673`  glWrite(50, glGetViewportHeight - 150, GLUT_BITMAP_TIMES_ROMAN_24, 'GLUT_BITMAP_TIMES_ROMAN_24');`\
674674`  glWrite(50, glGetViewportHeight - 180, GLUT_BITMAP_HELVETICA_10, 'GLUT_BITMAP_HELVETICA_10');`\
675675`  glWrite(50, glGetViewportHeight - 210, GLUT_BITMAP_HELVETICA_12, 'GLUT_BITMAP_HELVETICA_12');`\
676676`  glWrite(50, glGetViewportHeight - 240, GLUT_BITMAP_HELVETICA_18, 'GLUT_BITMAP_HELVETICA_18');`\
677-\
677+
678678`  glColor3f(0.5, 0.5, 1);`\
679679`  glWrite(`\
680680`    glGetViewportWidth - glutBitmapLength(GLUT_BITMAP_9_BY_15, LazText) - 5,`\
681681`    10, GLUT_BITMAP_9_BY_15, LazText);`\
682-\
682+
683683`  glLeave2D;`\
684-\
684+
685685`  glutSwapBuffers;`\
686-\
686+
687687`  FrameRendered;`\
688688`end;`
689689
690-![](/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.
690+![](/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.
691691
692692Note: See how cube looks without light.
693693
694694Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
695695
696696# Textures
697697
698698It's time to use textures :)
699699
700700This 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.
701701
702702Let's get started... we'll create display list for drawing textured rectangle:
703703
704704`procedure CreateList;`\
705705`begin`\
706706`  glNewList(LIST_OBJECT, GL_COMPILE);`\
707707`    glBegin(GL_QUADS);`\
708708`      glTexCoord2f(1, 0);`\
709709`      glVertex3f( 2, 2, 0);`\
710710`      glTexCoord2f(0, 0);`\
711711`      glVertex3f(-2, 2, 0);`\
712712`      glTexCoord2f(0, 1);`\
713713`      glVertex3f(-2,-2, 0);`\
714714`      glTexCoord2f(1, 1);`\
715715`      glVertex3f( 2,-2, 0);`\
716716`    glEnd;`\
717717`  glEndList;`\
718718`end;`
719719
720-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.
720+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.
721721
722722Texture loading is extremely easy with Vampyre Imaging Library:
723723
724724`var`\
725725`  Tex1, Tex2: GLuint;`\
726-\
726+
727727`procedure InitializeGL;`\
728728`begin`\
729729`  glClearColor(0, 0, 0, 0);`\
730730`  Tex1 := LoadGLTextureFromFile('ashwood.bmp');`\
731731`  Tex2 := LoadGLTextureFromFile('Flare.bmp');`\
732732`  glEnable(GL_TEXTURE_2D);`\
733733`end;`
734734
735-<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.
735+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.
736736
737737To draw textured polygon you have to bind texture and setup texture coordinations (texture coordinations are set in display list in this tutorial):
738738
739739`  ...`\
740740`  glLoadIdentity;`\
741741`  glTranslatef(-5, 0, -15);`\
742742`  glBindTexture(GL_TEXTURE_2D, Tex1);`\
743743`  glCallList(LIST_OBJECT);`\
744744`  ...`
745745
746-<b>glBindTexture</b> function is used to select texture. When you draw polygins they will have selected texture on them. It's that easy :)
746+glBindTexture function is used to select texture. When you draw polygins they will have selected texture on them. It's that easy :)
747747
748748So, 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:
749749
750750`  ...`\
751751`  glLoadIdentity;`\
752752`  glTranslatef(5, 0, -15);`\
753753`  glBindTexture(GL_TEXTURE_2D, Tex1);`\
754754`  glCallList(LIST_OBJECT);`\
755-\
755+
756756`  glEnable(GL_BLEND);`\
757757`  glBlendFunc(GL_ZERO, GL_SRC_COLOR);`\
758758`  glLoadIdentity;`\
759759`  glTranslatef(5, 0, -15);`\
760760`  glBindTexture(GL_TEXTURE_2D, Tex2);`\
761761`  glCallList(LIST_OBJECT);`\
762762`  glDisable(GL_BLEND);`\
763763`...`
764764
765-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:
765+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:
766766
767767`DrawingColor * SRCBLEND + BackgroundColor * DESTBLEND`
768768
769-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:
769+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:
770770
771771`DrawingColor * 0 + BackgroundColor * DrawingColor`
772772
773-<figure>
774-<img src="TexturesPic1.jpg" title="TexturesPic1.jpg" />
775-<figcaption>TexturesPic1.jpg</figcaption>
776-</figure>
773+![](/wiki-images/d/d4/TexturesPic1.jpg)
777774
778775This 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
779776
780777Next time, we'll use extensions to show how to use singlepass multitexturing.
781778
782779Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
783780
784781# Multitexturing (extensions)
785782
786783When 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.
787784
788785Let's see how code looks like:
789786
790787`procedure InitializeGL;`\
791788`begin`\
792789`  Load_GL_ARB_multitexture;`\
793790`  glClearColor(0, 0, 0, 0);`\
794791`  Tex1 := LoadGLTextureFromFile('Lazarus.bmp');`\
795792`  Tex2 := LoadGLTextureFromFile('Mask.bmp');`\
796793`  glActiveTextureARB(GL_TEXTURE0_ARB);`\
797794`  glEnable(GL_TEXTURE_2D);`\
798795`  glBindTexture(GL_TEXTURE_2D, Tex1);`\
799796`  glActiveTextureARB(GL_TEXTURE1_ARB);`\
800797`  glEnable(GL_TEXTURE_2D);`\
801798`  glBindTexture(GL_TEXTURE_2D, Tex2);`\
802799`end;`
803800
804-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.
801+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.
805802
806-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.
803+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.
807804
808805Since we setup every thing in initialization function, all we have to do is to draw object:
809806
810807`procedure DrawGLScene; cdecl;`\
811808`begin`\
812809`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
813-\
810+
814811`  glLoadIdentity;`\
815812`  glTranslatef(0, 0, -5);`\
816-\
813+
817814`  glBegin(GL_QUADS);`\
818815`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);`\
819816`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 0);`\
820817`    glVertex3f(2.516, 2, 0);`\
821818`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);`\
822819`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 0);`\
823820`    glVertex3f(-2.516, 2, 0);`\
824821`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);`\
825822`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 1);`\
826823`    glVertex3f(-2.516,-2, 0);`\
827824`    glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);`\
828825`    glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 1);`\
829826`    glVertex3f(2.516,-2, 0);`\
830827`  glEnd;`\
831-\
828+
832829`  glutSwapBuffers;`\
833830`end;`
834831
835-![](/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.
832+![](/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.
836833
837834Today 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.
838835
839836Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
840837
841838# Render to texture
842839
843840This 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.
844841
845842First, we must create empty texture which well use to capture scene:
846843
847844`procedure SetupRenderTexture;`\
848845`var`\
849846`  Data: Pointer;`\
850847`begin`\
851848`  GetMem(Data, 256*256*3);`\
852849`  glGenTextures(1, @RenderTexture);`\
853850`  glBindTexture(GL_TEXTURE_2D, RenderTexture);`\
854851`  glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, Data);`\
855852`  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);`\
856853`  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);`\
857854`  FreeMem(Data);`\
858855`end;`
859856
860857Buffer for 256*256 RGB image is created and it is used to setup 2D texture.
861858
862859Main part is in drawing function:
863860
864861`procedure DrawGLScene; cdecl;`\
865862`var`\
866863`  TotalTime: Single;`\
867864`begin`\
868865`  glClearColor(0, 0, 0, 0);`\
869866`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
870867`  glEnable(GL_LIGHTING);`\
871868`  glDisable(GL_TEXTURE_2D);`\
872869`  glViewport(0, 0, 256, 256);`\
873-\
870+
874871`  TotalTime := GetTotalTime;`\
875-\
872+
876873`  glLoadIdentity;`\
877874`  glTranslatef(0, 0, -5);`\
878875`  glRotatef(50 * TotalTime, 1, 0, 0);`\
879876`  glRotatef(100 * TotalTime, 0, 1, 0);`\
880877`  glRotatef(50 * TotalTime, 0, 0, 1);`\
881-\
878+
882879`  glColor3f(1, 1, 1);`\
883880`  glutSolidCube(2);`\
884-\
881+
885882`  glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 256, 256, 0);`\
886-\
883+
887884`  glClearColor(0.18, 0.20, 0.66, 0);`\
888885`  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);`\
889886`  glDisable(GL_LIGHTING);`\
890887`  glEnable(GL_TEXTURE_2D);`\
891888`  glViewport(0, 0, AppWidth, AppHeight);`\
892-\
889+
893890`  glLoadIdentity;`\
894891`  glTranslatef(0, 0, -7);`\
895892`  glRotatef(20 * TotalTime, 1, 0, 0);`\
896893`  glRotatef(50 * TotalTime, 0, 1, 0);`\
897-\
894+
898895`  glBegin(GL_QUADS);`\
899896`    glTexCoord2f(1, 0);`\
900897`    glVertex3f(2, 2, 0);`\
901898`    glTexCoord2f(0, 0);`\
902899`    glVertex3f(-2, 2, 0);`\
903900`    glTexCoord2f(0, 1);`\
904901`    glVertex3f(-2,-2, 0);`\
905902`    glTexCoord2f(1, 1);`\
906903`    glVertex3f(2,-2, 0);`\
907904`  glEnd;`\
908-\
905+
909906`  glutSwapBuffers;`\
910907`end;`
911908
912-![](/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.
909+![](/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.
913910
914911When 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.
915912
916-P.S. Captured texture can be saved using <b>SaveGLTextureToFile</b> function from [Vampyre Imaging Library](http://imaginglib.sourceforge.net/).
913+P.S. Captured texture can be saved using SaveGLTextureToFile function from [Vampyre Imaging Library](http://imaginglib.sourceforge.net/).
917914
918915Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
919916
920917# Vertex array
921918
922919OpenGL 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.
923920
924921In 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.
925922
926923First, let's define some types and constants:
927924
928925`type`\
929926`  TVertex3f = record`\
930927`    X, Y, Z: Single;`\
931928`  end;`\
932-\
929+
933930`  TColor3f = record`\
934931`   R, G, B: Single;`\
935932`  end;`\
936-\
933+
937934`  VertexBuffer: array [0..5] of TVertex3f = (`\
938935`    (X : 1; Y : 1; Z : 0),`\
939936`    (X : -1; Y : 1; Z : 0),`\
940937`    (X : -1; Y : -1; Z : 0),`\
941938`    (X : 1; Y : 1; Z : 0),`\
942939`    (X : -1; Y : -1; Z : 0),`\
943940`    (X : 1; Y : -1; Z : 0)`\
944941`  );`\
945942`  ColorBuffer: array [0..5] of TColor3f = (`\
946943`    (R : 1; G : 0; B : 1),`\
947944`    (R : 0; G : 0; B : 1),`\
948945`    (R : 0; G : 1; B : 0),`\
949946`    (R : 1; G : 0; B : 1),`\
950947`    (R : 0; G : 1; B : 0),`\
951948`    (R : 1; G : 1; B : 0)`\
952949`  );`
953950
954951We have two buffers. One for vertex coordinates and one for vertex colors. This 6 vertices defines 2 triangles that forms rectangle.
955952
956953Drawing primitives using buffers is easy:
957954
958955`  glEnableClientState(GL_VERTEX_ARRAY);`\
959956`  glEnableClientState(GL_COLOR_ARRAY);`\
960957`  glVertexPointer(3, GL_FLOAT, 0, @VertexBuffer[0]);`\
961958`  glColorPointer(3, GL_FLOAT, 0, @ColorBuffer[0]);`\
962-\
959+
963960`  glDrawArrays(GL_TRIANGLES, 0, Length(VertexBuffer));`\
964-\
961+
965962`  glDisableClientState(GL_VERTEX_ARRAY);`\
966963`  glDisableClientState(GL_COLOR_ARRAY);`
967964
968-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:
965+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:
969966
970967`type`\
971968`  TBufferData = record`\
972969`    DataBefore: TDataBefore;`\
973970`    Vertex: TVertex;`\
974971`    DataAfter: TDataAfter;`\
975972`  end;`\
976-\
973+
977974`Bytes between elements = SizeOf(TDataBefore) + SizeOf(TDataAfter)`
978975
979976Last parameter if pointer to the begginig of buffer.
980977
981-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).
978+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).
982979
983980When buffers are not needed you can disable them.
984981
985982To demonstrate indexed mode, I made some simple mesh class that can load vertex, color and index data from external files:
986983
987984`type`\
988985`  TMesh = class`\
989986`  private`\
990987`    FVertices: array of TVertex3f;`\
991988`    FColors: array of TColor3f;`\
992989`    FIndices: array of Integer;`\
993990`    procedure FreeBuffers;`\
994991`  public`\
995992`    constructor Create;`\
996993`    destructor Destroy; override;`\
997994`    procedure LoadMesh(FileName: String);`\
998995`    procedure DrawMesh;`\
999996`  end;`
1000997
1001998FVertices will contain data about vertices, FColors data about color and FIndices data about indices when external file is loaded.
1002999
10031000First we'll write some code that deals with creation and destruction of class:
10041001
10051002`procedure TMesh.FreeBuffers;`\
10061003`begin`\
10071004`  FVertices := nil;`\
10081005`  FColors := nil;`\
10091006`  FIndices := nil;`\
10101007`end;`\
1011-\
1008+
10121009`constructor TMesh.Create;`\
10131010`begin`\
10141011`  FreeBuffers;`\
10151012`end;`\
1016-\
1013+
10171014`destructor TMesh.Destroy;`\
10181015`begin`\
10191016`  FreeBuffers;`\
10201017`  inherited Destroy;`\
10211018`end;`
10221019
10231020File 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:
10241021
10251022`3 3`\
10261023`-1 -1 0 1 1 1`\
10271024`1 -1 0 1 1 1`\
10281025`0 1 0 1 1 1`\
10291026`0`\
10301027`1`\
10311028`2`
10321029
10331030This 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).
10341031
10351032Code for loading this data will loke like this:
10361033
10371034`procedure TMesh.LoadMesh(FileName: String);`\
10381035`var`\
10391036`  MeshFile: TextFile;`\
10401037`  VertexCount, IndexCount: Integer;`\
10411038`  iV, iI: Integer;`\
10421039`begin`\
10431040`  FreeBuffers;`\
1044-\
1041+
10451042`  AssignFile(MeshFile, FileName);`\
10461043`  Reset(MeshFile);`\
1047-\
1044+
10481045`  ReadLn(MeshFile, VertexCount, IndexCount);`\
1049-\
1046+
10501047`  SetLength(FVertices, VertexCount);`\
10511048`  SetLength(FColors, VertexCount);`\
10521049`  SetLength(FIndices, IndexCount);`\
1053-\
1050+
10541051`  for iV := 0 to VertexCount - 1 do`\
10551052`    ReadLn(MeshFile,`\
10561053`      FVertices[iV].X, FVertices[iV].Y, FVertices[iV].Z,`\
10571054`      FColors[iV].R, FColors[iV].G, FColors[iV].B);`\
1058-\
1055+
10591056`  for iI := 0 to IndexCount - 1 do`\
10601057`    ReadLn(MeshFile, FIndices[iI]);`\
1061-\
1058+
10621059`  CloseFile(MeshFile);`\
10631060`end;`
10641061
10651062After loading data, we have everything for drawing:
10661063
10671064`procedure TMesh.DrawMesh;`\
10681065`begin`\
10691066`  glEnableClientState(GL_VERTEX_ARRAY);`\
10701067`  glEnableClientState(GL_COLOR_ARRAY);`\
10711068`  glVertexPointer(3, GL_FLOAT, 0, @FVertices[0]);`\
10721069`  glColorPointer(3, GL_FLOAT, 0, @FColors[0]);`\
1073-\
1070+
10741071`  glDrawElements(GL_TRIANGLES, Length(FIndices), GL_UNSIGNED_INT, @FIndices[0]);`\
1075-\
1072+
10761073`  glDisableClientState(GL_VERTEX_ARRAY);`\
10771074`  glDisableClientState(GL_COLOR_ARRAY);`\
10781075`end;`
10791076
1080-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.
1077+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.
10811078
10821079![](/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:
10831080
10841081`4 6`\
10851082`1 1 0 1 0 1`\
10861083`-1 1 0 0 0 1`\
10871084`-1 -1 0 0 1 0`\
10881085`1 -1 0 1 1 0`\
10891086`0`\
10901087`1`\
10911088`2`\
10921089`0`\
10931090`2`\
10941091`3`
10951092
10961093As 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.
10971094
10981095Download source code, linux executable or windows executable from [Lazarus CCR SourceForge](http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=199145).
10991096
11001097---
11011098*From the Free Pascal wiki, page "OpenGL Tutorial/ja", as it stood on 2009-01-29 after a change by Saeka-jp, 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