-
Notifications
You must be signed in to change notification settings - Fork 24
/
glcube.pas
executable file
·438 lines (401 loc) · 10.7 KB
/
glcube.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
unit glcube;
//openGL cube
{$IFDEF FPC}
{$Include opts.inc}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
{$IFDEF FPC}
{$IFDEF COREGL}raycast_common, glcorearb, gl_core_matrix, {$ELSE}gl, glext, {$ENDIF}
OpenGLContext,
{$ELSE}
dglOpenGL, glpanel, windows,
{$ENDIF}
shaderu, Classes, SysUtils, Graphics, math, dialogs;
type
TGLCube = class
private
{$IFDEF COREGL}
uniform_mtx: GLint;
vbo_face2d, vao_point2d, shaderProgram: GLuint;
{$ELSE}displayLst: GLuint;{$ENDIF}
fAzimuth, fElevation,SizeFrac : Single;
scrnW, scrnH: integer;
isRedraw, isTopLeft: boolean;
procedure SetIsTopLeft(f: boolean);
procedure SetSize(f: single);
procedure SetAzimuth(f: single);
procedure SetElevation(f: single);
procedure ScreenSize(Width,Height: integer);
{$IFDEF COREGL}procedure CreateStrips;{$ENDIF}
procedure CreateCube(sz: single);
public
property TopLeft : boolean read isTopLeft write SetIsTopLeft;
property Azimuth : single read fAzimuth write SetAzimuth;
property Elevation : single read fElevation write fElevation;
property Size : single read SizeFrac write SetSize;
procedure Draw(Width,Height, zoom, zoomOffsetX, zoomOffsetY: integer); //must be called while TOpenGLControl is current context
{$IFDEF FPC}
constructor Create(Ctx: TOpenGLControl);
{$ELSE}
constructor Create(Ctx: TGLPanel);
{$ENDIF}
Destructor Destroy; override;
end;
{$IFNDEF COREGL}var GLErrorStr : string = '';{$ENDIF}
implementation
{$IFDEF COREGL}
type
TRGBA = packed record //Next: analyze Format Header structure
R,G,B,A : byte;
end;
TPoint3f = Packed Record
x,y,z: single;
end;
TVtxClr = Packed Record
vtx : TPoint3f; //vertex coordinates
clr : TRGBA;
end;
var
g2Dvnc: array of TVtxClr;
g2Drgba : TRGBA;
g2DNew: boolean;
gnface: integer;
const
kBlockSz = 8192;
kVert2D ='#version 330'
+#10'layout(location = 0) in vec3 Vert;'
+#10'layout(location = 3) in vec4 Clr;'
+#10'out vec4 vClr;'
+#10'uniform mat4 ModelViewProjectionMatrix;'
+#10'void main() {'
+#10' gl_Position = ModelViewProjectionMatrix * vec4(Vert, 1.0);'
+#10' vClr = Clr;'
+#10'}';
kFrag2D = '#version 330'
+#10'in vec4 vClr;'
+#10'out vec4 color;'
+#10'void main() {'
+#10' color = vClr;'
+#10'}';
procedure TGLCube.CreateStrips;
const
kATTRIB_VERT = 0; //vertex XYZ are positions 0,1,2
kATTRIB_CLR = 3; //color RGBA are positions 3,4,5,6
type
TInts = array of integer;
var
i: integer;
faces: TInts;
vbo_point : GLuint;
//mvp : TnMat44;
//mvpMat: GLint;
begin
//if not isRedraw then exit;
//nface := Length(g2Dvnc); //each face has 3 vertices
if gnface < 1 then exit;
if vao_point2d <> 0 then
glDeleteVertexArrays(1,@vao_point2d);
glGenVertexArrays(1, @vao_point2d);
if (vbo_face2d <> 0) then
glDeleteBuffers(1, @vbo_face2d);
glGenBuffers(1, @vbo_face2d);
vbo_point := 0;
glGenBuffers(1, @vbo_point);
glBindBuffer(GL_ARRAY_BUFFER, vbo_point);
glBufferData(GL_ARRAY_BUFFER, Length(g2Dvnc)*SizeOf(TVtxClr), @g2Dvnc[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Prepare vertrex array object (VAO)
glBindVertexArray(vao_point2d);
glBindBuffer(GL_ARRAY_BUFFER, vbo_point);
//Vertices
glVertexAttribPointer(kATTRIB_VERT, 3, GL_FLOAT, GL_FALSE, sizeof(TVtxClr), PChar(0));
glEnableVertexAttribArray(kATTRIB_VERT);
//Color
glVertexAttribPointer(kATTRIB_CLR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(TVtxClr), PChar( sizeof(TPoint3f)));
glEnableVertexAttribArray(kATTRIB_CLR);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteBuffers(1, @vbo_point);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_face2d);
setlength(faces,gnface);
for i := 0 to (gnface-1) do
faces[i] := i;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, gnface*sizeof(uint32), @faces[0], GL_STATIC_DRAW);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, Length(faces)*sizeof(TPoint3i), @faces[0], GL_STATIC_DRAW);
setlength(faces, 0 );
setlength(g2Dvnc,0);
end;
procedure nglBegin(mode: integer);
begin
g2DNew := true;
end;
procedure nglColor4ub (r,g,b,a: byte);
begin
g2Drgba.r := round(r );
g2Drgba.g := round(g );
g2Drgba.b := round(b );
g2Drgba.a := round(a );
end;
procedure nglVertex3f(x,y,z: single);
var
i: integer;
begin
i := gnface; //array indexed from 0 not 1
gnface := gnface + 1;
if (gnface+1) > length(g2Dvnc) then
setlength(g2Dvnc, length(g2Dvnc)+kBlockSz);
g2Dvnc[i].vtx.X := x;
g2Dvnc[i].vtx.Y := y;
g2Dvnc[i].vtx.Z := z;
g2Dvnc[i].clr := g2Drgba;
if not g2DNew then exit;
g2DNew := false;
g2Dvnc[gnface] := g2Dvnc[i];
gnface := gnface + 1;
end;
procedure nglEnd;
var
i: integer;
begin
//add tail
if gnface < 1 then exit;
i := gnface; //array indexed from 0 not 1
gnface := gnface + 1;
if gnface > length(g2Dvnc) then
setlength(g2Dvnc, length(g2Dvnc)+kBlockSz);
g2Dvnc[i] := g2Dvnc[i-1];
end;
(*procedure DrawTextCore (lScrnWid, lScrnHt: integer);
begin
nglMatrixMode(nGL_MODELVIEW);
nglLoadIdentity;
nglMatrixMode (nGL_PROJECTION);
nglLoadIdentity ();
nglOrtho (0, lScrnWid,0, lScrnHt,-10,10);
end; *)
{$ELSE} //for legacy OpenGL
procedure nglColor4ub (r,g,b,a: byte);
begin
glColor4ub (r,g,b,a);
end;
procedure nglVertex3f(x,y,z: single);
begin
glVertex3f(x,y,z);
end;
procedure nglVertex2f(x,y: single);
begin
glVertex2f(x,y);
end;
procedure nglBegin(mode: integer);
begin
glBegin(mode);
end;
procedure nglVertex2fr(x,y: single);
begin
nglVertex3f(round(x),round(y), -1);
end;
procedure nglEnd;
begin
glEnd();
end;
{$ENDIF}
procedure TGLCube.SetAzimuth(f: single);
begin
if (f <> fAzimuth) then isRedraw := true;
fAzimuth := f;
end;
procedure TGLCube.SetElevation(f: single);
begin
if (f <> fElevation) then isRedraw := true;
fElevation := f;
end;
procedure TGLCube.SetIsTopLeft(f: boolean);
begin
if (f <> isTopLeft) then isRedraw := true;
isTopLeft := f;
end;
procedure TGLCube.SetSize(f: single);
begin
if (f <> sizeFrac) then isRedraw := true;
sizeFrac := f;
if sizeFrac < 0.005 then sizeFrac := 0.005;
if sizeFrac > 0.25 then sizeFrac := 0.25;
end;
procedure TGLCube.ScreenSize(Width,Height: integer);
begin
if (Width = scrnW) and (Height = scrnH) then exit;
scrnW := Width;
scrnH := Height;
isRedraw := true;
end;
{$IFDEF FPC}
constructor TGLCube.Create(Ctx: TOpenGLControl);
{$ELSE}
constructor TGLCube.Create(Ctx: TGLPanel);
{$ENDIF}
begin
scrnH := 0;
SizeFrac := 0.03;
isRedraw := true;
fAzimuth := 30;
fElevation := -15;
isTopLeft := false;
{$IFDEF COREGL}
vao_point2d := 0;
vbo_face2d := 0;
Ctx.MakeCurrent();
shaderProgram := initVertFrag(kVert2D, kFrag2D);
uniform_mtx := glGetUniformLocation(shaderProgram, pAnsiChar('ModelViewProjectionMatrix'));
glFinish;
Ctx.ReleaseContext;
{$ELSE}
displayLst := 0;
{$ENDIF}
end;
procedure MakeCube(sz: single);
//draw a cube of size sz
var
sz2 : single;
begin
sz2 := sz;
{$IFDEF COREGL}
nglColor4ub(204,204,204,255);
{$ELSE}
nglColor4ub(25,25,25,255);
{$ENDIF}
nglBegin(GL_TRIANGLE_STRIP); //* Bottom side
nglVertex3f(-sz, -sz, -sz2);
nglVertex3f(-sz, sz, -sz2);
nglVertex3f(sz, -sz, -sz2);
nglVertex3f(sz, sz, -sz2);
nglEnd;
{$IFDEF COREGL}
nglColor4ub(25,25,25,255);
{$ELSE}
nglColor4ub(204,204,204,255);
{$ENDIF}
nglBegin(GL_TRIANGLE_STRIP); //* Top side
nglVertex3f(-sz, -sz, sz2);
nglVertex3f(sz, -sz, sz2);
nglVertex3f(-sz, sz, sz2);
nglVertex3f(sz, sz, sz2);
nglEnd;
nglColor4ub(0,0,128,255);
nglBegin(GL_TRIANGLE_STRIP); //* Front side
nglVertex3f(-sz, sz, -sz2);
nglVertex3f(-sz, sz, sz2);
nglVertex3f(sz, sz, -sz2);
nglVertex3f(sz, sz, sz2);
nglEnd;
nglColor4ub(77,0,77,255);
nglBegin(GL_TRIANGLE_STRIP);//* Back side
nglVertex3f(-sz, -sz, -sz2);
nglVertex3f(sz, -sz, -sz2);
nglVertex3f(-sz, -sz, sz2);
nglVertex3f(sz, -sz, sz2);
nglEnd;
nglColor4ub(153,0,0,255);
nglBegin(GL_TRIANGLE_STRIP); //* Left side
nglVertex3f(-sz, -sz, -sz2);
nglVertex3f(-sz, -sz, sz2);
nglVertex3f(-sz, sz, -sz2);
nglVertex3f(-sz, sz, sz2);
nglEnd;
nglColor4ub(0,153,0,255);
nglBegin(GL_TRIANGLE_STRIP); //* Right side
nglVertex3f(sz, -sz, -sz2);
nglVertex3f(sz, sz, -sz2);
nglVertex3f(sz, -sz, sz2);
nglVertex3f(sz, sz, sz2);
nglEnd();
end; //MakeCube()
procedure TGLCube.CreateCube(sz: single);
begin
{$IFDEF COREGL}
gnface := 0;
setlength(g2Dvnc, 0);
{$ELSE}
if displayLst <> 0 then
glDeleteLists(displayLst, 1);
displayLst := glGenLists(1);
glNewList(displayLst, GL_COMPILE);
{$ENDIF}
MakeCube(sz);
{$IFDEF COREGL}
CreateStrips;
{$ELSE}
glEndList();
{$ENDIF}
isRedraw := false;
end;
procedure TGLCube.Draw(Width,Height, zoom,zoomOffsetX, zoomOffsetY: integer);
var
sz: single;
{$IFDEF COREGL}
mvp : TnMat44;
{$ENDIF}
begin
ScreenSize(Width,Height);
sz := ScrnW;
if sz > ScrnH then sz := ScrnH;
if sz < 10 then exit;
sz := sz * SizeFrac ;
{$IFDEF COREGL}
nglMatrixMode(nGL_MODELVIEW);
nglLoadIdentity;
nglMatrixMode (nGL_PROJECTION);
nglLoadIdentity ();
nglOrtho (0, ScrnW,0, ScrnH,-10*sz,10*sz);
nglTranslatef(0,0,sz*8);
nglTranslatef(1.8*sz,1.8*sz,0);
nglRotatef(fElevation-90,-1,0,0);
nglRotatef(-fAzimuth,0,0,1);
{$ELSE}
glUseProgram(0);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable (GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, ScrnW div zoom, 0, ScrnH div zoom,0.01,sz*4);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
//glEnable(GL_DEPTH_TEST);
glTranslatef(0,0,-sz*2);
if isTopLeft then
glTranslatef((ScrnW) - (1.8*sz), (ScrnH)-(1.8*sz),0)
else
glTranslatef(1.8*sz,1.8*sz,0);
glTranslatef(zoomOffsetX, zoomOffsetY, 0);
glRotatef(90-fElevation,-1,0,0);
glRotatef(-fAzimuth,0,0,1);
{$ENDIF}
//glEnable( GL_MULTISAMPLE );
if isRedraw then
CreateCube(sz);
glEnable(GL_CULL_FACE);
{$IFDEF COREGL}
glUseProgram(shaderProgram);
mvp := ngl_ModelViewProjectionMatrix;
glUniformMatrix4fv(uniform_mtx, 1, GL_FALSE, @mvp[0,0]); // note model not MVP!
glBindVertexArray(vao_point2d);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_face2d);
glDrawElements(GL_TRIANGLE_STRIP, gnface, GL_UNSIGNED_INT, nil);
glBindVertexArray(0);
glUseProgram(0);
{$ELSE}
glCallList(displayLst);
glUseProgram(0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
{$ENDIF}
glDisable(GL_CULL_FACE);
end;
destructor TGLCube.Destroy;
begin
//call the parent destructor:
inherited;
end;
end.