1 implementation
3 uses SDL2;
5 const
6 GL_FLOAT = $1406;
7 GL_VERTEX_ARRAY = $8074;
8 GL_NORMAL_ARRAY = $8075;
9 GL_COLOR_ARRAY = $8076;
10 GL_TEXTURE_COORD_ARRAY = $8078;
11 GL_TRIANGLES = $0004;
12 GL_TEXTURE0 = $84C0;
13 GL_INVALID_ENUM = $0500;
15 const
16 ValPerVertex = 2;
17 ValPerColor = 4;
18 ValPerCoord = 2;
20 type
21 TArrayFloat = array of GLfloat;
23 TCmds = record
24 mode: GLenum;
25 v, c, t: TArrayFloat;
26 end;
28 var
29 cmds: TCmds;
31 var
32 es_glVertexPointer: procedure (size: GLint; _type: GLenum; stride: GLsizei; pointer: PGLvoid); cdecl;
33 es_glColorPointer: procedure (size: GLint; _type: GLenum; stride: GLsizei; pointer: PGLvoid); cdecl;
34 es_glTexCoordPointer: procedure (size: GLint; _type: GLenum; stride: GLsizei; pointer: PGLvoid); cdecl;
35 es_glEnableClientState: procedure (arr: GLenum); cdecl;
36 es_glDisableClientState: procedure (arr: GLenum); cdecl;
37 es_glDrawArrays: procedure (mode: GLenum; first: GLint; count: GLsizei); cdecl;
38 // es_glActiveTexture: procedure(texture: GLenum); cdecl;
39 // es_glClientActiveTexture: procedure(texture: GLenum); cdecl;
40 es_glColor4f: procedure(red, green, blue, alpha: GLfloat); cdecl;
42 es_glEnable: procedure (cap: GLenum); cdecl;
43 es_glDisable: procedure (cap: GLenum); cdecl;
44 es_glIsEnabled: function (cap: GLenum): GLBoolean; cdecl;
45 es_glGetString: function (name: GLenum): PChar; cdecl;
46 es_glClearColor: procedure (red, green, blue, alpha: GLclampf); cdecl;
47 es_glClear: procedure (mask: GLbitfield); cdecl;
48 es_glAlphaFunc: procedure (func: GLenum; ref: GLclampf); cdecl;
49 es_glBlendFunc: procedure (sfactor, dfactor: GLenum); cdecl;
50 es_glPointSize: procedure (size: GLfloat); cdecl;
51 es_glLineWidth: procedure (width: GLfloat); cdecl;
52 es_glGetIntegerv: procedure (pname: GLenum; params: PGLint); cdecl;
53 es_glFlush: procedure; cdecl;
54 es_glFinish: procedure; cdecl;
56 es_glLoadIdentity: procedure; cdecl;
57 es_glMatrixMode: procedure (mode: GLenum); cdecl;
58 es_glLoadMatrixf: procedure (m: PGLfloat); cdecl;
59 es_glPushMatrix: procedure; cdecl;
60 es_glPopMatrix: procedure; cdecl;
61 es_glTranslatef: procedure (x, y, z: GLfloat); cdecl;
62 es_glRotatef: procedure (angle, x, y, z: GLfloat); cdecl;
63 es_glScalef: procedure (x, y, z: GLfloat); cdecl;
65 es_glReadPixels: procedure (x, y: GLint; width, height: GLsizei; format,_type: GLenum; pixels: PGLvoid); cdecl;
67 es_glViewport: procedure (x, y: GLint; width, height: GLsizei); cdecl;
68 es_glScissor: procedure (x, y: GLint; width, height: GLsizei); cdecl;
69 es_glStencilMask: procedure (mask: GLuint); cdecl;
70 es_glStencilFunc: procedure (func: GLenum; ref: GLint; mask: GLuint); cdecl;
71 es_glStencilOp: procedure (fail, zfail, zpass: GLenum); cdecl;
72 es_glColorMask: procedure (red: GLboolean; green: GLboolean; blue: GLboolean; alpha: GLboolean); cdecl;
74 es_glBindTexture: procedure (target: GLenum; texture: GLuint); cdecl;
75 es_glGenTextures: procedure (n: GLsizei; textures: PGLuint); cdecl;
76 es_glTexEnvi: procedure (target: GLenum; pname: GLenum; param: GLint); cdecl;
77 es_glTexParameterf: procedure (target: GLenum; pname: GLenum; param: GLfloat); cdecl;
78 es_glTexParameteri: procedure (target: GLenum; pname: GLenum; param: GLint); cdecl;
79 es_glTexImage2D: procedure (target: GLenum; level, internalformat: GLint; width, height: GLsizei; border: GLint; format, _type:GLenum; pixels: PGLvoid); cdecl;
80 es_glTexSubImage2D: procedure (target: GLenum; level: GLint; xoffset, yoffset: GLint; width, height: GLsizei; format, _type: GLenum; pixels: PGLvoid); cdecl;
81 es_glDeleteTextures: procedure (n: GLsizei; textures: PGLuint); cdecl;
83 procedure glEnable(cap: GLenum);
84 begin
85 es_glEnable(cap);
86 end;
88 procedure glDisable(cap: GLenum);
89 begin
90 es_glDisable(cap);
91 end;
93 function glIsEnabled(cap: GLenum): GLboolean;
94 begin
95 result := es_glIsEnabled(cap);
96 end;
98 function glGetString(name: GLenum): PChar;
99 begin
100 result := es_glGetString(name);
101 end;
103 procedure glClearColor(red, green, blue, alpha: GLclampf);
104 begin
105 es_glClearColor(red, green, blue, alpha);
106 end;
108 procedure glClear(mask: GLbitfield);
109 begin
110 es_glClear(mask);
111 end;
113 procedure glAlphaFunc(func: GLenum; ref: GLclampf);
114 begin
115 es_glAlphaFunc(func, ref);
116 end;
118 procedure glBlendFunc(sfactor, dfactor: GLenum);
119 begin
120 es_glBlendFunc(sfactor, dfactor);
121 end;
123 procedure glPointSize(size: GLfloat);
124 begin
125 es_glPointSize(size);
126 end;
128 procedure glLineWidth(width: GLfloat);
129 begin
130 es_glLineWidth(width);
131 end;
133 procedure glGetIntegerv(pname: GLenum; params: PGLint);
134 begin
135 es_glGetIntegerv(pname, params);
136 end;
138 procedure glFlush;
139 begin
140 es_glFlush;
141 end;
143 procedure glFinish;
144 begin
145 es_glFinish;
146 end;
152 procedure glBegin(mode: GLenum);
153 begin
154 assert(cmds.mode = GL_INVALID_ENUM);
155 assert((mode = GL_POINTS) or (mode = GL_LINES) or (mode = GL_QUADS));
156 cmds.mode := mode;
157 SetLength(cmds.v, 0);
158 SetLength(cmds.c, 0);
159 SetLength(cmds.t, 0);
160 end;
162 procedure glEnd;
163 begin
164 assert(cmds.mode <> GL_INVALID_ENUM);
165 assert(Length(cmds.v) mod ValPerVertex = 0);
166 assert(Length(cmds.c) mod ValPerColor = 0);
167 assert(Length(cmds.t) mod ValPerCoord = 0);
168 if Length(cmds.c) <> 0 then
169 assert(Length(cmds.v) div ValPerVertex = Length(cmds.c) div ValPerColor);
170 if Length(cmds.t) <> 0 then
171 assert(Length(cmds.v) div ValPerVertex = Length(cmds.t) div ValPerCoord);
173 es_glVertexPointer(ValPerVertex, GL_FLOAT, 0, @cmds.v[0]);
174 es_glColorPointer(ValPerColor, GL_FLOAT, 0, @cmds.c[0]);
175 es_glTexCoordPointer(ValPerCoord, GL_FLOAT, 0, @cmds.t[0]);
176 es_glEnableClientState(GL_VERTEX_ARRAY);
177 es_glEnableClientState(GL_COLOR_ARRAY);
178 es_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
179 es_glDisableClientState(GL_NORMAL_ARRAY);
181 if Length(cmds.c) = 0 then
182 es_glDisableClientState(GL_COLOR_ARRAY);
183 if Length(cmds.t) = 0 then
184 es_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
186 if cmds.mode = GL_QUADS then
187 es_glDrawArrays(GL_TRIANGLES, 0, Length(cmds.v) div ValPerVertex)
188 else
189 es_glDrawArrays(cmds.mode, 0, Length(cmds.v) div ValPerVertex);
191 SetLength(cmds.v, 0);
192 SetLength(cmds.c, 0);
193 SetLength(cmds.t, 0);
194 cmds.mode := GL_INVALID_ENUM;
195 end;
197 (* ---------- begin internals ---------- *)
199 procedure AddFloatRaw (var x: TArrayFloat; f: GLfloat);
200 var i: Integer;
201 begin
202 i := Length(x);
203 SetLength(x, i + 1);
204 x[i] := f;
205 end;
207 procedure AddFloatX (var x: TArrayFloat; v: array of GLfloat); inline;
208 var i, j, vpx: Integer;
209 begin
210 i := Length(x);
211 vpx := Length(v);
212 if (cmds.mode = GL_QUADS) and (i div vpx mod 4 = 3) then
213 begin
214 for j := 0 to vpx - 1 do
215 AddFloatRaw(x, x[i - 3*vpx + j]);
216 for j := 0 to vpx - 1 do
217 AddFloatRaw(x, x[i - 1*vpx + j]);
218 end;
219 for j := 0 to vpx - 1 do
220 AddFloatRaw(x, v[j]);
221 end;
223 (* ---------- end internals ---------- *)
225 procedure glVertex2f(x, y: GLfloat);
226 begin
227 AddFloatX(cmds.v, [x, y]);
228 end;
230 procedure glVertex2i(x, y: GLint);
231 begin
232 AddFloatX(cmds.v, [x, y]);
233 end;
235 procedure glColor4f(red, green, blue, alpha: GLfloat);
236 begin
237 if cmds.mode = GL_INVALID_ENUM then
238 es_glColor4f(red, green, blue, alpha)
239 else
240 AddFloatX(cmds.c, [red, green, blue, alpha])
241 end;
243 procedure glColor4ub(red, green, blue, alpha: GLubyte);
244 begin
245 glColor4f(red / 255, green / 255, blue / 255, alpha / 255)
246 end;
248 procedure glColor3ub(red, green, blue: GLubyte);
249 begin
250 glColor4f(red / 255, green / 255, blue / 255, 255)
251 end;
253 procedure glTexCoord2f(s, t: GLfloat);
254 begin
255 AddFloatX(cmds.t, [s, t]);
256 end;
258 procedure glTexCoord2i(s, t: GLint);
259 begin
260 AddFloatX(cmds.t, [s, t]);
261 end;
269 procedure glReadPixels(x, y: GLint; width, height: GLsizei; format, atype: GLenum; pixels: Pointer);
270 begin
271 es_glReadPixels(x, y, width, height, format, atype, pixels);
272 end;
279 procedure glLoadIdentity;
280 begin
281 es_glLoadIdentity;
282 end;
284 procedure glMatrixMode(mode: GLenum);
285 begin
286 es_glMatrixMode(mode);
287 end;
289 procedure glLoadMatrixd(const m: PGLdouble);
290 var
291 i: Integer;
292 n: array [0..15] of GLfloat;
293 begin
294 for i := 0 to 15 do
295 n[i] := m[i];
296 es_glLoadMatrixf(@n[0]);
297 end;
299 procedure glPushMatrix;
300 begin
301 es_glPushMatrix;
302 end;
304 procedure glPopMatrix;
305 begin
306 es_glPopMatrix;
307 end;
309 procedure glTranslatef(x, y, z: GLfloat);
310 begin
311 es_glTranslatef(x, y, z);
312 end;
314 procedure glRotatef(angle, x, y, z: GLfloat);
315 begin
316 es_glRotatef(angle, x, y, z);
317 end;
319 procedure glScalef(x, y, z: GLfloat);
320 begin
321 es_glScalef(x, y, z);
322 end;
329 procedure glViewport(x, y: GLint; width, height: GLsizei);
330 begin
331 es_glViewport(x, y, width, height);
332 end;
334 procedure glScissor(x, y: GLint; width, height: GLsizei);
335 begin
336 es_glScissor(x, y, width, height);
337 end;
339 procedure glStencilMask(mask: GLuint);
340 begin
341 es_glStencilMask(mask);
342 end;
344 procedure glStencilFunc(func: GLenum; ref: GLint; mask: GLuint);
345 begin
346 es_glStencilFunc(func, ref, mask);
347 end;
349 procedure glStencilOp(fail, zfail, zpass: GLenum);
350 begin
351 es_glStencilOp(fail, zfail, zpass);
352 end;
354 procedure glColorMask(red, green, blue, alpha: GLboolean);
355 begin
356 es_glColorMask(red, green, blue, alpha);
357 end;
364 procedure glBindTexture(target: GLenum; texture: GLuint);
365 begin
366 es_glBindTexture(target, texture);
367 end;
369 procedure glGenTextures(n: GLsizei; textures: PGLuint);
370 begin
371 es_glGenTextures(n, textures);
372 end;
374 procedure glTexEnvi(target: GLenum; pname: GLenum; param: GLint);
375 begin
376 es_glTexEnvi(target, pname, param);
377 end;
379 procedure glTexParameterf(target: GLenum; pname: GLenum; param: GLfloat);
380 begin
381 es_glTexParameterf(target, pname, param);
382 end;
384 procedure glTexParameteri(target: GLenum; pname: GLenum; param: GLint);
385 begin
386 es_glTexParameteri(target, pname, param);
387 end;
389 procedure glTexImage2D(target: GLenum; level, internalformat: GLint; width, height: GLsizei; border: GLint; format, atype: GLenum; const pixels: Pointer);
390 begin
391 es_glTexImage2D(target, level, internalformat, width, height, border, format, atype, pixels);
392 end;
394 procedure glTexSubImage2D(target: GLenum; level, xoffset, yoffset: GLint; width, height: GLsizei; format, atype: GLenum; const pixels: Pointer);
395 begin
396 es_glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, atype, pixels);
397 end;
399 procedure glDeleteTextures(n: GLsizei; const textures: PGLuint);
400 begin
401 es_glDeleteTextures(n, textures);
402 end;
408 procedure nogl_Init;
409 begin
410 es_glVertexPointer := SDL_GL_GetProcAddress('glVertexPointer');
411 es_glColorPointer := SDL_GL_GetProcAddress('glColorPointer');
412 es_glTexCoordPointer := SDL_GL_GetProcAddress('glTexCoordPointer');
413 es_glEnableClientState := SDL_GL_GetProcAddress('glEnableClientState');
414 es_glDisableClientState := SDL_GL_GetProcAddress('glDisableClientState');
415 es_glDrawArrays := SDL_GL_GetProcAddress('glDrawArrays');
416 // es_glActiveTexture := SDL_GL_GetProcAddress('glActiveTexture');
417 // es_glClientActiveTexture := SDL_GL_GetProcAddress('glClientActiveTexture');
418 es_glColor4f := SDL_GL_GetProcAddress('glColor4f');
420 es_glEnable := SDL_GL_GetProcAddress('glEnable');
421 es_glDisable := SDL_GL_GetProcAddress('glDisable');
422 es_glIsEnabled := SDL_GL_GetProcAddress('glIsEnabled');
423 es_glGetString := SDL_GL_GetProcAddress('glGetString');
424 es_glClearColor := SDL_GL_GetProcAddress('glClearColor');
425 es_glClear := SDL_GL_GetProcAddress('glClear');
426 es_glAlphaFunc := SDL_GL_GetProcAddress('glAlphaFunc');
427 es_glBlendFunc := SDL_GL_GetProcAddress('glBlendFunc');
428 es_glPointSize := SDL_GL_GetProcAddress('glPointSize');
429 es_glLineWidth := SDL_GL_GetProcAddress('glLineWidth');
430 es_glGetIntegerv := SDL_GL_GetProcAddress('glGetIntegerv');
431 es_glFlush := SDL_GL_GetProcAddress('glFlush');
432 es_glFinish := SDL_GL_GetProcAddress('glFinish');
434 es_glLoadIdentity := SDL_GL_GetProcAddress('glLoadIdentity');
435 es_glMatrixMode := SDL_GL_GetProcAddress('glMatrixMode');
436 es_glLoadMatrixf := SDL_GL_GetProcAddress('glLoadMatrixf');
437 es_glPushMatrix := SDL_GL_GetProcAddress('glPushMatrix');
438 es_glPopMatrix := SDL_GL_GetProcAddress('glPopMatrix');
439 es_glTranslatef := SDL_GL_GetProcAddress('glTranslatef');
440 es_glRotatef := SDL_GL_GetProcAddress('glRotatef');
441 es_glScalef := SDL_GL_GetProcAddress('glScalef');
443 es_glReadPixels := SDL_GL_GetProcAddress('glReadPixels');
445 es_glViewport := SDL_GL_GetProcAddress('glViewport');
446 es_glScissor := SDL_GL_GetProcAddress('glScissor');
447 es_glStencilMask := SDL_GL_GetProcAddress('glStencilMask');
448 es_glStencilFunc := SDL_GL_GetProcAddress('glStencilFunc');
449 es_glStencilOp := SDL_GL_GetProcAddress('glStencilOp');
450 es_glColorMask := SDL_GL_GetProcAddress('glColorMask');
452 es_glBindTexture := SDL_GL_GetProcAddress('glBindTexture');
453 es_glGenTextures := SDL_GL_GetProcAddress('glGenTextures');
454 es_glTexEnvi := SDL_GL_GetProcAddress('glTexEnvi');
455 es_glTexParameterf := SDL_GL_GetProcAddress('glTexParameterf');
456 es_glTexParameteri := SDL_GL_GetProcAddress('glTexParameteri');
457 es_glTexImage2D := SDL_GL_GetProcAddress('glTexImage2D');
458 es_glTexSubImage2D := SDL_GL_GetProcAddress('glTexSubImage2D');
459 es_glDeleteTextures := SDL_GL_GetProcAddress('glDeleteTextures');
461 cmds.mode := GL_INVALID_ENUM
462 end;
464 procedure nogl_Quit;
465 begin
466 es_glVertexPointer := nil;
467 es_glColorPointer := nil;
468 es_glTexCoordPointer := nil;
469 es_glEnableClientState := nil;
470 es_glDisableClientState := nil;
471 es_glDrawArrays := nil;
472 // es_glActiveTexture := nil;
473 // es_glClientActiveTexture := nil;
474 es_glColor4f := nil;
476 es_glEnable := nil;
477 es_glDisable := nil;
478 es_glIsEnabled := nil;
479 es_glGetString := nil;
480 es_glClearColor := nil;
481 es_glClear := nil;
482 es_glAlphaFunc := nil;
483 es_glBlendFunc := nil;
484 es_glPointSize := nil;
485 es_glLineWidth := nil;
486 es_glGetIntegerv := nil;
487 es_glFlush := nil;
488 es_glFinish := nil;
490 es_glLoadIdentity := nil;
491 es_glMatrixMode := nil;
492 es_glLoadMatrixf := nil;
493 es_glPushMatrix := nil;
494 es_glPopMatrix := nil;
495 es_glTranslatef := nil;
496 es_glRotatef := nil;
497 es_glScalef := nil;
499 es_glReadPixels := nil;
501 es_glViewport := nil;
502 es_glScissor := nil;
503 es_glStencilMask := nil;
504 es_glStencilFunc := nil;
505 es_glStencilOp := nil;
506 es_glColorMask := nil;
508 es_glBindTexture := nil;
509 es_glGenTextures := nil;
510 es_glTexEnvi := nil;
511 es_glTexParameterf := nil;
512 es_glTexParameteri := nil;
513 es_glTexImage2D := nil;
514 es_glTexSubImage2D := nil;
515 es_glDeleteTextures := nil;
516 end;
518 initialization