8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
9 uglcContext, uglcShader, uglcArrayBuffer, uglcTypes, uglcBitmap;
12 TMainForm = class(TForm)
13 ApplicationProperties: TApplicationProperties;
16 procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
17 procedure FormCreate(Sender: TObject);
18 procedure FormDestroy(Sender: TObject);
19 procedure RenderPanelResize(Sender: TObject);
21 fContext: TglcContext;
22 fShader: TglcShaderProgram;
23 fVBO: TglcArrayBuffer;
24 fTexture: TglcBitmap2D;
25 procedure Log(aSender: TObject; const aMsg: String);
28 { public declarations }
39 dglOpenGL, ugluVector;
42 SHADER_FILE = 'shader.glsl';
43 TEXTURE_FILE = 'data\texture.png';
45 LAYOUT_LOCATION_POS = 0;
46 LAYOUT_LOCATION_TEX = 1;
48 UNIFORM_NAME_TEXTURE = 'uTexture';
51 TVertex = packed record
57 procedure TMainForm.FormCreate(Sender: TObject);
59 pf: TglcContextPixelFormatSettings;
61 texData: TglcBitmapData;
63 pf := TglcContext.MakePF();
64 fContext := TglcContext.GetPlatformClass.Create(RenderPanel, pf);
65 fContext.BuildContext;
67 fShader := TglcShaderProgram.Create(@Log);
68 fShader.LoadFromFile(ExtractFilePath(Application.ExeName) + SHADER_FILE);
70 fShader.Uniform1i(UNIFORM_NAME_TEXTURE, 0);
72 fVBO := TglcArrayBuffer.Create(TglcBufferTarget.btArrayBuffer);
73 fVBO.BufferData(4, sizeof(TVertex), TglcBufferUsage.buStaticDraw, nil);
74 p := fVBO.MapBuffer(TglcBufferAccess.baWriteOnly);
76 p^.pos := gluVector3f(-0.5, -0.5, 0);
77 p^.tex := gluVector2f( 0.0, 1.0);
80 p^.pos := gluVector3f( 0.5, -0.5, 0);
81 p^.tex := gluVector2f( 1.0, 1.0);
84 p^.pos := gluVector3f( 0.5, 0.5, 0);
85 p^.tex := gluVector2f( 1.0, 0.0);
88 p^.pos := gluVector3f(-0.5, 0.5, 0);
89 p^.tex := gluVector2f( 0.0, 0.0);
95 fTexture := TglcBitmap2D.Create;
96 texData := TglcBitmapData.Create;
98 texData.LoadFromFile(ExtractFilePath(Application.ExeName) + TEXTURE_FILE);
99 fTexture.UploadData(texData);
105 procedure TMainForm.ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
111 procedure TMainForm.FormDestroy(Sender: TObject);
115 FreeAndNil(fContext);
118 procedure TMainForm.RenderPanelResize(Sender: TObject);
120 if Assigned(fContext) then begin
121 glViewport(0, 0, RenderPanel.ClientWidth, RenderPanel.ClientHeight);
125 procedure TMainForm.Log(aSender: TObject; const aMsg: String);
127 LogLB.Items.Add(aMsg);
130 procedure TMainForm.Render;
132 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
137 glEnableVertexAttribArray(LAYOUT_LOCATION_POS);
138 glVertexAttribPointer(LAYOUT_LOCATION_POS, 3, GL_FLOAT, False, SizeOf(TVertex), @PVertex(nil)^.pos);
140 glEnableVertexAttribArray(LAYOUT_LOCATION_TEX);
141 glVertexAttribPointer(LAYOUT_LOCATION_TEX, 2, GL_FLOAT, False, SizeOf(TVertex), @PVertex(nil)^.tex);
143 glDrawArrays(GL_QUADS, 0, fVBO.DataCount);
145 glDisableVertexAttribArray(LAYOUT_LOCATION_POS);
146 glDisableVertexAttribArray(LAYOUT_LOCATION_TEX);
152 fContext.SwapBuffers;