1 // We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
4 namespace gfx::drivers;
7 #if defined(__unix__) || defined(__APPLE__)
9 #if !defined(__MINGW32__)
10 #define GL_GLEXT_PROTOTYPES
13 #define pointer _pointer
17 //#include <GL/miniglx.h>
21 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
23 #define property _property
27 #define Window X11Window
28 #define Cursor X11Cursor
30 #define Display X11Display
32 #define KeyCode X11KeyCode
33 #define Picture X11Picture
37 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
42 #include <X11/extensions/Xrender.h>
43 #include <X11/extensions/shape.h>
63 #if defined(__APPLE__)
64 #include <OpenGl/gl.h>
67 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
69 #if defined(__WIN32__)
70 #define WIN32_LEAN_AND_MEAN
72 #define _WIN32_WINNT 0x0502
78 #if defined(__ANDROID__)
83 #elif defined(__EMSCRIPTEN__)
85 #define property _property
91 //#include <GLES/gl.h>
92 //#include <EGL/egl.h>
94 //#include <GLES2/gl.h>
95 //#include <EGL/egl.h>
97 //#include <GLES2/gl2.h>
99 #include <emscripten/emscripten.h>
107 #include <GL/glext.h>
115 #if defined(__unix__) || defined(__APPLE__)
117 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
123 static double nearPlane = 1;
125 #define glLoadMatrix glLoadMatrixd
126 #define glMultMatrix glMultMatrixd
127 #define glGetMatrix glGetDoublev
128 #define glTranslate glTranslated
129 #define glScale glScaled
132 #define glVertex3v glVertex3dv
133 #define glNormal3v glNormal3dv
137 //#ifdef VERTEX_FORMAT_DOUBLE
139 #define glLoadMatrix glLoadMatrixd
140 #define glMultMatrix glMultMatrixd
141 #define glGetMatrix glGetDoublev
142 #define glVertex3v glVertex3dv
143 #define glNormal3v glNormal3dv
144 #define glTranslate glTranslated
145 #define glScale glScaled
146 //#define GL_VERTEX_FORMAT GL_DOUBLE
150 #define glLoadMatrix glLoadMatrixf
151 #define glMultMatrix glMultMatrixf
152 #define glGetMatrix glGetFloatv
153 #define glVertex3v glVertex3fv
154 #define glNormal3v glNormal3fv
155 #define glTranslate glTranslatef
156 #define glScale glScalef
157 //#define GL_VERTEX_FORMAT GL_FLOAT
162 #define GL_ARRAY_BUFFER_ARB 0x8892
163 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
164 #define GL_STATIC_DRAW_ARB 0x88E4
165 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
166 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
168 #define GL_MULTISAMPLE_ARB 0x809D
170 #if defined(__WIN32__)
172 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
173 #define WGL_SAMPLES_ARB 0x2042
175 #define WGL_WGLEXT_VERSION 1
176 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
177 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
178 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
179 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
180 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
181 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
182 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
183 #define WGL_ACCELERATION_ARB 0x2003
184 #define WGL_NEED_PALETTE_ARB 0x2004
185 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
186 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
187 #define WGL_SWAP_METHOD_ARB 0x2007
188 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
189 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
190 #define WGL_TRANSPARENT_ARB 0x200A
191 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
192 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
193 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
194 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
195 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
196 #define WGL_SHARE_DEPTH_ARB 0x200C
197 #define WGL_SHARE_STENCIL_ARB 0x200D
198 #define WGL_SHARE_ACCUM_ARB 0x200E
199 #define WGL_SUPPORT_GDI_ARB 0x200F
200 #define WGL_SUPPORT_OPENGL_ARB 0x2010
201 #define WGL_DOUBLE_BUFFER_ARB 0x2011
202 #define WGL_STEREO_ARB 0x2012
203 #define WGL_PIXEL_TYPE_ARB 0x2013
204 #define WGL_COLOR_BITS_ARB 0x2014
205 #define WGL_RED_BITS_ARB 0x2015
206 #define WGL_RED_SHIFT_ARB 0x2016
207 #define WGL_GREEN_BITS_ARB 0x2017
208 #define WGL_GREEN_SHIFT_ARB 0x2018
209 #define WGL_BLUE_BITS_ARB 0x2019
210 #define WGL_BLUE_SHIFT_ARB 0x201A
211 #define WGL_ALPHA_BITS_ARB 0x201B
212 #define WGL_ALPHA_SHIFT_ARB 0x201C
213 #define WGL_ACCUM_BITS_ARB 0x201D
214 #define WGL_ACCUM_RED_BITS_ARB 0x201E
215 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
216 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
217 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
218 #define WGL_DEPTH_BITS_ARB 0x2022
219 #define WGL_STENCIL_BITS_ARB 0x2023
220 #define WGL_AUX_BUFFERS_ARB 0x2024
221 #define WGL_NO_ACCELERATION_ARB 0x2025
222 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
223 #define WGL_FULL_ACCELERATION_ARB 0x2027
224 #define WGL_SWAP_EXCHANGE_ARB 0x2028
225 #define WGL_SWAP_COPY_ARB 0x2029
226 #define WGL_SWAP_UNDEFINED_ARB 0x202A
227 #define WGL_TYPE_RGBA_ARB 0x202B
228 #define WGL_TYPE_COLORINDEX_ARB 0x202C
229 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
230 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
231 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
232 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
233 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
234 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
235 #define WGL_PBUFFER_LARGEST_ARB 0x2033
236 #define WGL_PBUFFER_WIDTH_ARB 0x2034
237 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
238 #define WGL_PBUFFER_LOST_ARB 0x2036
239 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
240 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
241 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
242 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
243 #define WGL_ACCELERATION_EXT 0x2003
244 #define WGL_NEED_PALETTE_EXT 0x2004
245 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
246 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
247 #define WGL_SWAP_METHOD_EXT 0x2007
248 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
249 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
250 #define WGL_TRANSPARENT_EXT 0x200A
251 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
252 #define WGL_SHARE_DEPTH_EXT 0x200C
253 #define WGL_SHARE_STENCIL_EXT 0x200D
254 #define WGL_SHARE_ACCUM_EXT 0x200E
255 #define WGL_SUPPORT_GDI_EXT 0x200F
256 #define WGL_SUPPORT_OPENGL_EXT 0x2010
257 #define WGL_DOUBLE_BUFFER_EXT 0x2011
258 #define WGL_STEREO_EXT 0x2012
259 #define WGL_PIXEL_TYPE_EXT 0x2013
260 #define WGL_COLOR_BITS_EXT 0x2014
261 #define WGL_RED_BITS_EXT 0x2015
262 #define WGL_RED_SHIFT_EXT 0x2016
263 #define WGL_GREEN_BITS_EXT 0x2017
264 #define WGL_GREEN_SHIFT_EXT 0x2018
265 #define WGL_BLUE_BITS_EXT 0x2019
266 #define WGL_BLUE_SHIFT_EXT 0x201A
267 #define WGL_ALPHA_BITS_EXT 0x201B
268 #define WGL_ALPHA_SHIFT_EXT 0x201C
269 #define WGL_ACCUM_BITS_EXT 0x201D
270 #define WGL_ACCUM_RED_BITS_EXT 0x201E
271 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
272 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
273 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
274 #define WGL_DEPTH_BITS_EXT 0x2022
275 #define WGL_STENCIL_BITS_EXT 0x2023
276 #define WGL_AUX_BUFFERS_EXT 0x2024
277 #define WGL_NO_ACCELERATION_EXT 0x2025
278 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
279 #define WGL_FULL_ACCELERATION_EXT 0x2027
280 #define WGL_SWAP_EXCHANGE_EXT 0x2028
281 #define WGL_SWAP_COPY_EXT 0x2029
282 #define WGL_SWAP_UNDEFINED_EXT 0x202A
283 #define WGL_TYPE_RGBA_EXT 0x202B
284 #define WGL_TYPE_COLORINDEX_EXT 0x202C
285 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
286 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
287 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
288 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
289 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
290 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
291 #define WGL_PBUFFER_LARGEST_EXT 0x2033
292 #define WGL_PBUFFER_WIDTH_EXT 0x2034
293 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
294 #define WGL_DEPTH_FLOAT_EXT 0x2040
295 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
296 #define WGL_SAMPLES_3DFX 0x2061
297 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
298 #define WGL_SAMPLES_EXT 0x2042
299 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
300 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
301 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
302 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
303 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
304 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
305 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
306 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
307 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
308 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
309 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
310 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
311 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
312 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
313 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
314 #define WGL_ARB_buffer_region 1
315 #define WGL_ARB_extensions_string 1
316 #define WGL_ARB_pixel_format 1
317 #define WGL_ARB_make_current_read 1
318 #define WGL_ARB_pbuffer 1
319 #define WGL_EXT_display_color_table 1
320 #define WGL_EXT_extensions_string 1
321 #define WGL_EXT_make_current_read 1
322 #define WGL_EXT_pbuffer 1
323 #define WGL_EXT_pixel_format 1
324 #define WGL_EXT_swap_control 1
325 #define WGL_WGL_EXT_depth_float 1
326 #define WGL_WGL_3DFX_multisample 1
327 #define WGL_WGL_EXT_multisample 1
328 #define WGL_NV_allocate_memory 1
331 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
332 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
333 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
334 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
335 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
339 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
340 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
341 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
342 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
344 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
345 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
346 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
347 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
348 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
349 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
350 typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
351 typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
352 typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
354 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
355 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
356 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
357 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
358 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
359 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
360 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
361 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
363 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
364 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
365 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
366 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
367 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
368 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
369 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
370 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
371 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
372 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
373 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
374 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
375 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
377 #define glBufferData glBufferDataARB
379 #ifdef WGL_WGLEXT_PROTOTYPES
380 extern BOOL WINAPI wglSwapIntervalEXT (int);
381 extern int WINAPI wglGetSwapIntervalEXT (void);
382 #endif /* WGL_WGLEXT_PROTOTYPES */
383 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
384 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
386 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
388 #elif defined(__ANDROID__)
390 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
391 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
392 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
394 #define GL_POLYGON_STIPPLE 0xFFFF
395 #define GL_LINE_STIPPLE 0xFFFF
396 #define GL_LINE 0xFFFF
397 #define GL_FILL 0xFFFF
398 #define GL_ALL_ATTRIB_BITS 0xFFFF
399 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
405 #define GL_QUAD_STRIP 0
406 //#define GL_DOUBLE 0
407 //#define GL_UNSIGNED_INT 0
410 //#define GL_LINE_STIPPLE 0
411 #define GL_BGRA_EXT 0
412 #define GL_UNPACK_ROW_LENGTH 0
413 #define GL_UNPACK_SKIP_PIXELS 0
414 #define GL_UNPACK_SKIP_ROWS 0
416 #define GL_PACK_ROW_LENGTH 0
417 #define GL_PACK_SKIP_ROWS 0
418 #define GL_PACK_SKIP_PIXELS 0
422 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
430 FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
431 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
434 void Transpose(Matrix source)
439 m[j][i] = source.m[i][j];
442 void Multiply(Matrix a, Matrix b)
444 // We need a full matrix multiplication for the Projection matrix
445 m[0][0]=a.m[0][0]*b.m[0][0] + a.m[0][1]*b.m[1][0] + a.m[0][2]*b.m[2][0] + a.m[0][3]*b.m[3][0];
446 m[0][1]=a.m[0][0]*b.m[0][1] + a.m[0][1]*b.m[1][1] + a.m[0][2]*b.m[2][1] + a.m[0][3]*b.m[3][1];
447 m[0][2]=a.m[0][0]*b.m[0][2] + a.m[0][1]*b.m[1][2] + a.m[0][2]*b.m[2][2] + a.m[0][3]*b.m[3][2];
448 m[0][3]=a.m[0][0]*b.m[0][3] + a.m[0][1]*b.m[1][3] + a.m[0][2]*b.m[2][3] + a.m[0][3]*b.m[3][3];
450 m[1][0]=a.m[1][0]*b.m[0][0] + a.m[1][1]*b.m[1][0] + a.m[1][2]*b.m[2][0] + a.m[1][3]*b.m[3][0];
451 m[1][1]=a.m[1][0]*b.m[0][1] + a.m[1][1]*b.m[1][1] + a.m[1][2]*b.m[2][1] + a.m[1][3]*b.m[3][1];
452 m[1][2]=a.m[1][0]*b.m[0][2] + a.m[1][1]*b.m[1][2] + a.m[1][2]*b.m[2][2] + a.m[1][3]*b.m[3][2];
453 m[1][3]=a.m[1][0]*b.m[0][3] + a.m[1][1]*b.m[1][3] + a.m[1][2]*b.m[2][3] + a.m[1][3]*b.m[3][3];
455 m[2][0]=a.m[2][0]*b.m[0][0] + a.m[2][1]*b.m[1][0] + a.m[2][2]*b.m[2][0] + a.m[2][3]*b.m[3][0];
456 m[2][1]=a.m[2][0]*b.m[0][1] + a.m[2][1]*b.m[1][1] + a.m[2][2]*b.m[2][1] + a.m[2][3]*b.m[3][1];
457 m[2][2]=a.m[2][0]*b.m[0][2] + a.m[2][1]*b.m[1][2] + a.m[2][2]*b.m[2][2] + a.m[2][3]*b.m[3][2];
458 m[2][3]=a.m[2][0]*b.m[0][3] + a.m[2][1]*b.m[1][3] + a.m[2][2]*b.m[2][3] + a.m[2][3]*b.m[3][3];
460 m[3][0]=a.m[3][0]*b.m[0][0] + a.m[3][1]*b.m[1][0] + a.m[3][2]*b.m[2][0] + a.m[3][3]*b.m[3][0];
461 m[3][1]=a.m[3][0]*b.m[0][1] + a.m[3][1]*b.m[1][1] + a.m[3][2]*b.m[2][1] + a.m[3][3]*b.m[3][1];
462 m[3][2]=a.m[3][0]*b.m[0][2] + a.m[3][1]*b.m[1][2] + a.m[3][2]*b.m[2][2] + a.m[3][3]*b.m[3][2];
463 m[3][3]=a.m[3][0]*b.m[0][3] + a.m[3][1]*b.m[1][3] + a.m[3][2]*b.m[2][3] + a.m[3][3]*b.m[3][3];
468 // Our own matrix stack
469 static Matrix matrixStack[3][32];
470 static int matrixIndex[3];
471 static int curStack = 0;
475 // OpenGL ES Porting Kit
476 #if defined(__ANDROID__)
477 #define glBindFramebuffer glBindFramebufferOES
478 #define glBindRenderbuffer glBindRenderbufferOES
479 #define glFramebufferTexture2D glFramebufferTexture2DOES
480 #define glGenFramebuffers glGenFramebuffersOES
481 #define glGenRenderbuffers glGenRenderbuffersOES
482 #define glDeleteFramebuffers glDeleteFramebuffersOES
483 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
485 #define GL_INT 0x1404
486 #define GL_UNSIGNED_INT 0x1405
487 #define GL_DOUBLE 0x140A
491 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
493 #define glBufferDatai glesBufferDatai
494 #define glBufferDatad glesBufferDatad
495 #define glVertexPointeri glesVertexPointeri
496 #define glVertexPointerd glesVertexPointerd
498 #define glRecti glesRecti
499 #define glBegin glesBegin
500 #define glTexCoord2i glesTexCoord2i
501 #define glVertex2i glesVertex2i
502 #define glTexCoord2d glesTexCoord2d
503 #define glVertex2d glesVertex2d
504 #define glTexCoord2f glesTexCoord2f
505 #define glVertex2f glesVertex2f
506 #define glEnd glesEnd
507 #define glColor3f glesColor3f
508 #define glColor4ub glesColor4ub
509 #define glColor4fv glesColor4fv
510 #define glLineStipple glesLineStipple
511 #define glNormal3fv glesNormal3fv
512 #define glTexCoord2fv glesTexCoord2fv
513 #define glColorMaterial glesColorMaterial
515 #define glLoadMatrixd glesLoadMatrixd
516 #define glMultMatrixd glesMultMatrixd
517 #define glFrustum glesFrustum
518 #define glOrtho glesOrtho
519 #define glScaled glesScaled
520 #define glTranslated glesTranslated
521 #define glRotated glesRotated
522 #define glVertex3d glesVertex3d
523 #define glVertex3dv glesVertex3dv
524 #define glVertex3f glesVertex3f
525 #define glVertex3fv glesVertex3fv
526 #define glLightModeli glesLightModeli
530 #define glVertexPointerd(nc, s, p, nv) glVertexPointer(nc, GL_DOUBLE, s, p)
531 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_INT, start)
535 #if defined(__ANDROID__)
536 static EGLDisplay eglDisplay;
537 static EGLSurface eglSurface;
538 static EGLContext eglContext;
539 static int eglWidth, eglHeight;
541 static bool egl_init_display(ANativeWindow* window)
543 const EGLint attribs[] =
545 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
549 EGL_DEPTH_SIZE, 16, //24,
550 /*EGL_SAMPLE_BUFFERS, 1,
551 EGL_SAMPLES, 0, //2,*/
560 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
561 eglInitialize(display, 0, 0);
562 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
563 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
565 surface = eglCreateWindowSurface(display, config, window, null);
566 context = eglCreateContext(display, config, null, null);
568 if(!eglMakeCurrent(display, surface, surface, context))
571 eglQuerySurface(display, surface, EGL_WIDTH, &w);
572 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
574 eglDisplay = display;
575 eglContext = context;
576 eglSurface = surface;
580 glEnableClientState(GL_VERTEX_ARRAY);
582 // Initialize GL state.
583 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
584 glEnable(GL_CULL_FACE);
585 glShadeModel(GL_SMOOTH);
586 glDisable(GL_DEPTH_TEST);
588 glDisable(GL_CULL_FACE);
589 glDisable(GL_DEPTH_TEST);
591 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
594 matrixStack[0][0].Identity();
595 matrixStack[1][0].Identity();
596 matrixStack[2][0].Identity();
598 glesMatrixMode(GL_MODELVIEW);
599 glScaled(1.0, 1.0, -1.0);
600 glesMatrixMode(GL_PROJECTION);
601 glShadeModel(GL_FLAT);
603 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
604 glFogi(GL_FOG_MODE, GL_EXP);
605 glFogf(GL_FOG_DENSITY, 0);
606 glEnable(GL_NORMALIZE);
607 glDepthFunc(GL_LESS);
609 glDisable(GL_MULTISAMPLE_ARB);
613 glOrtho(0,w,h,0,0.0,1.0);
615 currentVertexBuffer = 0;
619 static void egl_term_display()
623 glDeleteTextures(1, &stippleTexture);
626 if(eglDisplay != EGL_NO_DISPLAY)
628 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
629 if(eglContext != EGL_NO_CONTEXT)
630 eglDestroyContext(eglDisplay, eglContext);
631 if(eglSurface != EGL_NO_SURFACE)
632 eglDestroySurface(eglDisplay, eglSurface);
633 eglTerminate(eglDisplay);
635 eglDisplay = EGL_NO_DISPLAY;
636 eglContext = EGL_NO_CONTEXT;
637 eglSurface = EGL_NO_SURFACE;
642 // OpenGL Immediate Mode Porting Kit
643 static int beginCount;
644 static int vertexCount;
645 static int normalCount;
646 static float *vertexPointer;
647 static float *normalPointer;
648 static GLenum beginMode;
649 static unsigned int beginBufferSize, normalBufferSize;
650 static int numVertexCoords = 2;
652 public void glesRecti(int a, int b, int c, int d)
662 public void glesBegin(GLenum mode)
669 normalBufferSize = beginBufferSize = 1024; // default number of vertices
670 vertexPointer = new float[beginBufferSize * 5];
671 normalPointer = new float[normalBufferSize * 3];
675 public void glesTexCoord2f(float x, float y)
677 int count = vertexCount;
679 if(vertexCount + numVertexCoords > beginBufferSize)
681 beginBufferSize = beginBufferSize + beginBufferSize/2;
682 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
685 vertexPointer[count*(2+numVertexCoords) ] = x;
686 vertexPointer[count*(2+numVertexCoords)+1] = y;
689 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
691 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
692 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
694 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
695 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
699 public void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
700 public void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
701 public void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
703 public void glesVertex2f(float x, float y)
706 if(vertexCount + 4 > beginBufferSize)
708 beginBufferSize = beginBufferSize + beginBufferSize/2;
709 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
712 vertexPointer[vertexCount*4+2] = x;
713 vertexPointer[vertexCount*4+3] = y;
716 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
718 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
719 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
721 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
722 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
727 public void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
728 public void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
730 public void glesEnd(void)
732 int mode = beginMode;
733 if(mode == GL_QUADS) mode = GL_TRIANGLES;
734 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
736 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
737 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
738 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
739 if(normalCount && normalCount == vertexCount)
741 glEnableClientState(GL_NORMAL_ARRAY);
742 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
745 glDrawArrays(mode, 0, vertexCount);
747 glDisableClientState(GL_NORMAL_ARRAY);
748 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
753 static float *floatVPBuffer = null;
754 static short *shortVPBuffer = null;
755 static unsigned int shortVPSize = 0, floatVPSize = 0;
758 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
759 static unsigned short *shortBDBuffer = null;
760 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
762 public void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
767 if(numVertices*numCoords > shortVPSize)
769 shortVPSize = numVertices*numCoords;
770 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
772 for(i = 0; i < numVertices*numCoords; i++)
773 shortVPBuffer[i] = (short)pointer[i];
774 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
777 glVertexPointer(numCoords, GL_SHORT, stride, 0);
780 public void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
785 if(numVertices*numCoords > floatVPSize)
787 floatVPSize = numVertices*numCoords;
788 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
790 for(i = 0; i < numVertices*numCoords; i++)
791 floatVPBuffer[i] = (float)pointer[i];
792 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
795 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
798 public void glesTexReuseIntVP(int numCoords)
800 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
803 public void glesTexReuseDoubleVP(int numCoords)
805 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
808 public void glesColor3f( float r, float g, float b )
810 glColor4f(r, g, b, 1.0f);
813 public void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
815 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
818 public void glesColor4fv(float * a)
820 glColor4f(a[0], a[1], a[2], a[3]);
823 public void glesBufferDatad(int target, int size, void * data, int usage)
825 int numElems = size/sizeof(double);
826 double * dblPtr = (double *)data;
828 if (numElems > floatVPSize)
830 floatVPSize = numElems;
831 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
833 for (i=0; i< numElems; i++)
834 floatVPBuffer[i] = (float)dblPtr[i];
836 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
839 public void glesBufferDatai(int target, int size, void * data, int usage)
841 int numElems = size/sizeof(unsigned int);
842 unsigned int * pointer = (unsigned int *)data;
844 if (numElems > shortBDSize)
846 shortBDSize = numElems;
847 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
849 for (i=0; i< numElems; i++)
850 shortBDBuffer[i] = (unsigned short)pointer[i];
852 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
855 // *** Our Custom Matrix Stack ***
857 static void LoadCurMatrix()
859 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
862 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
863 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
864 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
865 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
870 public void glesLoadIdentity()
872 matrixStack[curStack][matrixIndex[curStack]].Identity();
876 public void glesPushMatrix()
878 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
880 matrixIndex[curStack]++;
881 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
885 public void glesPopMatrix()
887 if(matrixIndex[curStack] > 0)
889 matrixIndex[curStack]--;
894 public void glesLoadMatrixd(double * i)
896 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
900 public void glesOrtho( double l, double r, double b, double t, double n, double f )
904 (2 / (r - l)), 0, 0, 0,
905 0, (2 / (t - b)), 0, 0,
906 0, 0, (-2 / (f - n)), 0,
907 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
910 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
911 matrixStack[curStack][matrixIndex[curStack]] = res;
915 public void glesFrustum( double l, double r, double b, double t, double n, double f )
925 double A = ((r + l) / (r - l));
926 double B = ((t + b) / (t - b));
927 double C = (-(f + n) / (f - n));
928 double D = (-2*f*n/(f-n));
931 (2.0*n / (r - l)), 0, 0, 0,
932 0, (2.0*n / (t - b)), 0, 0,
937 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
938 matrixStack[curStack][matrixIndex[curStack]] = res;
943 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
944 public void glesRotated( double a, double b, double c, double d )
949 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
950 m.RotationQuaternion(q);
951 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
952 matrixStack[curStack][matrixIndex[curStack]] = r;
955 public void glesScaled( double a, double b, double c )
961 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
962 matrixStack[curStack][matrixIndex[curStack]] = r;
966 public void glesTranslated( double a, double b, double c )
972 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
973 matrixStack[curStack][matrixIndex[curStack]] = r;
977 public void glesMultMatrixd( double * i )
980 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
981 matrixStack[curStack][matrixIndex[curStack]] = r;
986 public void glesMatrixMode(int mode)
988 curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
994 #define glPushMatrix glesPushMatrix
995 #define glPopMatrix glesPopMatrix
996 #define glLoadIdentity glesLoadIdentity
997 #define glMatrixMode glesMatrixMode
1001 /* Using the built-in matrix stack
1002 void glesLoadMatrixd( double * i )
1006 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
1007 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
1008 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
1009 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
1014 void glesOrtho( double l, double r, double b, double t, double n, double f )
1016 float matrix[4][4] =
1018 { (float)(2 / (r - l)), 0, 0, 0 },
1019 { 0, (float)(2 / (t - b)), 0, 0 },
1020 { 0, 0, (float)(-2 / (f - n)), 0 },
1021 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
1023 glMultMatrixf((float *)matrix);
1026 void glesFrustum( double l, double r, double b, double t, double n, double f )
1028 float A = (float)((r + l) / (r - l));
1029 float B = (float)((t + b) / (t - b));
1030 float C = (float)(-(f + n) / (f - n));
1031 float D = (float)(-2*f*n/(f-n));
1032 float matrix[4][4] =
1034 { (float)(2*n / (r - l)), 0, 0, 0 },
1035 { 0, (float)(2*n / (t - b)), 0, 0 },
1039 glMultMatrixf((float *)matrix);
1042 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
1043 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
1044 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
1046 void glesMultMatrixd( double * i )
1050 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
1051 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
1052 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
1053 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
1059 // Need to do these...
1060 public void glesVertex3f( float x, float y, float z )
1062 numVertexCoords = 3;
1063 if(vertexCount + 4 > beginBufferSize)
1065 beginBufferSize = beginBufferSize + beginBufferSize/2;
1066 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
1069 vertexPointer[vertexCount*5+2] = x;
1070 vertexPointer[vertexCount*5+3] = y;
1071 vertexPointer[vertexCount*5+4] = z;
1074 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1076 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
1077 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
1078 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
1080 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
1081 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
1082 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
1088 public void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
1089 public void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
1090 public void glesVertex3dv( double* coords ) { glesVertex3f((float)coords[0], (float)coords[1], (float)coords[2]); }
1092 public void glesNormal3f(float x, float y, float z)
1094 normalCount = vertexCount;
1095 if(vertexCount + 4 > normalBufferSize)
1097 normalBufferSize = normalBufferSize + normalBufferSize/2;
1098 normalPointer = renew normalPointer float[normalBufferSize * 2];
1101 normalPointer[normalCount*3+0] = x;
1102 normalPointer[normalCount*3+1] = y;
1103 normalPointer[normalCount*3+2] = z;
1106 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1108 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1109 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1110 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1112 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1113 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1114 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1118 public void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1119 public void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1121 public void glesColorMaterial(int a, int b)
1123 PrintLn("glColorMaterial stub");
1126 public void glesTerminate()
1128 delete vertexPointer;
1129 delete normalPointer;
1130 beginBufferSize = 0;
1132 delete floatVPBuffer;
1135 delete shortVPBuffer;
1138 delete shortBDBuffer;
1142 static GLuint stippleTexture;
1144 static bool stippleEnabled;
1147 public void glesLineStipple( int i, unsigned short j )
1151 for(x = 0; x < 16; x++)
1153 bool v = (j & (1 << x)) != 0;
1154 texture[x] = v ? 0xFFFFFFFF : 0;
1157 glGenTextures(1, &stippleTexture);
1158 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1159 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1160 glEnable(GL_TEXTURE_2D);
1161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1165 glMatrixMode(GL_TEXTURE);
1167 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1168 glScaled(i/16.0, 1, 1.0f);
1169 glTranslated(0.5, 0.5, 0);
1170 glMatrixMode(GL_PROJECTION);
1173 public void glesLightModeli( unsigned int pname, int param )
1175 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1176 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1180 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1181 void glFogi( unsigned int pname, int param ) { }
1182 void glPolygonMode( unsigned int i, unsigned int j ) { }
1185 // *** Picking won't be supported for now ***
1186 void glPushName( unsigned int i ) { }
1187 void glLoadName( unsigned int i ) { }
1188 void glPopName() { }
1190 // Probably replace by regular glBlendFunc ...
1191 void glBlendFuncSeparate(int a, int b, int c, int d)
1196 // For direct pixel blitting...
1197 void glRasterPos2d(double a, double b) { }
1198 void glPixelZoom(float a, float b) { }
1199 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1203 #if !defined(__APPLE__) && !defined(__WIN32__)
1204 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1205 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1206 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1207 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1210 static int currentVertexBuffer;
1212 bool GLSelectVBO(uint vbo)
1214 if(currentVertexBuffer != vbo)
1216 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
1217 currentVertexBuffer = vbo;
1223 void GLGenBuffers(int count, uint * buffer)
1226 glGenBuffers(count, buffer);
1228 #if defined(__WIN32__)
1231 glGenBuffersARB(count, buffer);
1235 void GLDeleteBuffers(int count, GLuint * buffer)
1238 glDeleteBuffers(count, buffer);
1240 #if defined(__WIN32__)
1241 if(glDeleteBuffersARB)
1243 glDeleteBuffersARB(count, buffer);
1247 void GLBindBuffer(int target, uint buffer)
1250 glBindBuffer(target, buffer);
1252 #if defined(__WIN32__)
1255 glBindBufferARB(target, buffer);
1259 public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
1262 if(glType == GL_DOUBLE)
1263 glesVertexPointerd(numCoords, stride, ptr, numVertices);
1264 else if(glType == GL_INT)
1265 glesVertexPointeri(numCoords, stride, ptr, numVertices);
1268 glVertexPointer(numCoords, glType, stride, ptr);
1271 public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1274 if(type == GL_DOUBLE)
1275 glesBufferDatad(target, size, (void *)data, usage);
1276 else if(type == GL_UNSIGNED_INT)
1277 glesBufferDatai(target, size, (void *)data, usage);
1282 glBufferData(target, size, data, usage);
1285 #if defined(__WIN32__)
1288 glBufferDataARB(target, size, data, usage);
1292 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1293 static int primitiveTypes[RenderPrimitiveType] =
1295 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1300 // Non OpenGL ES friendly stuff
1304 //#undef GL_UNSIGNED_INT
1309 #undef GL_QUAD_STRIP
1310 #undef GL_POLYGON_STIPPLE
1311 #undef GL_LINE_STIPPLE
1314 #undef GL_ALL_ATTRIB_BITS
1315 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1319 static int displayWidth, displayHeight;
1321 #define GL_CLAMP_TO_EDGE 0x812F
1323 static bool vboAvailable;
1325 static bool useSingleGLContext = false;
1326 class OGLDisplay : struct
1328 #if defined(__WIN32__)
1338 int imageBuffers[2];
1339 byte * pboMemory1, * pboMemory2;
1341 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1342 GLXContext glContext;
1345 XShmSegmentInfo shminfo;
1347 XShmSegmentInfo shminfoShape;
1348 XImage * shapeImage;
1352 X11Picture windowPicture;
1353 X11Picture pixmapPicture;
1355 X11Picture shapePicture;
1358 ColorAlpha * flippingBuffer;
1359 int flipBufH, flipBufW;
1364 class OGLSystem : struct
1369 #if defined(__WIN32__)
1370 PIXELFORMATDESCRIPTOR pfd;
1375 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1376 XVisualInfo * visualInfo;
1377 GLXContext glContext;
1378 GLXDrawable glxDrawable;
1382 class OGLSurface : struct
1388 bool writingOutline;
1390 float foreground[4], background[4], bitmapMult[4];
1393 class OGLMesh : struct
1402 class OGLIndices : struct
1412 class OpenGLDisplayDriver : DisplayDriver
1414 class_property(name) = "OpenGL";
1416 bool LockSystem(DisplaySystem displaySystem)
1418 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1419 OGLSystem oglSystem = displaySystem.driverData;
1420 if(useSingleGLContext) return true;
1421 #if defined(__WIN32__)
1422 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1423 #elif defined(__unix__) || defined(__APPLE__)
1424 //if(previous) return true;
1425 // printf("Making SYSTEM current\n");
1426 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1427 //previous = oglSystem.glContext;
1433 void UnlockSystem(DisplaySystem displaySystem)
1435 if(useSingleGLContext) return;
1436 #if defined(__WIN32__)
1437 wglMakeCurrent(null, null);
1438 #elif defined(__unix__) || defined(__APPLE__)
1439 // printf("Making NULL current\n");
1440 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1442 glXMakeCurrent(xGlobalDisplay, None, null);
1448 bool Lock(Display display)
1450 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1451 OGLDisplay oglDisplay = display.driverData;
1452 if(useSingleGLContext) return true;
1453 #if defined(__WIN32__)
1454 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1455 #elif defined(__unix__) || defined(__APPLE__)
1456 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1457 // printf(" Making DISPLAY current\n");
1458 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1464 void Unlock(Display display)
1466 if(useSingleGLContext) return;
1467 //printf(" Making NULL current\n");
1468 //glXMakeCurrent(xGlobalDisplay, None, null);
1470 LockSystem(display.displaySystem);
1473 void DestroyDisplay(Display display)
1475 OGLDisplay oglDisplay = display.driverData;
1479 #if defined(__WIN32__)
1480 wglMakeCurrent( null, null );
1483 wglDeleteContext(oglDisplay.glrc);
1485 if(oglDisplay.hdc && oglDisplay.pBuffer)
1486 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1488 if(oglDisplay.pBuffer)
1489 wglDestroyPbufferARB(oglDisplay.pBuffer);
1492 ReleaseDC(display.window, oglDisplay.hdc);
1494 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1495 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1497 #elif defined(__unix__) || defined(__APPLE__)
1498 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1500 if(oglDisplay.shapePixmap)
1501 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1502 if(oglDisplay.pixmap)
1503 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1504 if(oglDisplay.image)
1506 if(oglDisplay.shminfoShape.shmid != -1)
1508 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1509 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1510 shmdt(oglDisplay.shminfo.shmaddr);
1511 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1514 if(oglDisplay.shapeImage)
1516 if(oglDisplay.shminfoShape.shmid != -1)
1518 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1519 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1520 shmdt(oglDisplay.shminfoShape.shmaddr);
1521 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1523 XDestroyImage(oglDisplay.shapeImage);
1524 oglDisplay.shapeImage = None;
1527 glXMakeCurrent(xGlobalDisplay, None, null);
1529 if(oglDisplay.glContext)
1530 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1533 delete oglDisplay.flippingBuffer;
1535 display.driverData = null;
1539 void ::CheckExtensions(OGLSystem oglSystem)
1541 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1543 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1544 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1547 bool CreateDisplaySystem(DisplaySystem displaySystem)
1549 bool result = false;
1550 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1553 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1555 oglSystem.hdc = GetDC(oglSystem.hwnd);
1559 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1560 oglSystem.pfd.nVersion = 1;
1561 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1562 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1563 oglSystem.pfd.cColorBits = 24;
1564 oglSystem.pfd.cAlphaBits = 8;
1565 oglSystem.pfd.cDepthBits = 24;
1566 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1568 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1569 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1571 if(oglSystem.pfd.cColorBits > 8)
1573 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1574 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1577 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1579 // Get Pointers To The GL Functions
1580 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1581 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1582 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1583 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1584 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1585 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1586 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1587 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1588 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1589 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1590 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1591 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1593 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1594 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1595 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1596 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1597 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1598 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1599 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1600 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1601 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1603 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1605 vboAvailable = glBindBufferARB != null;
1607 // eSystem_LoggingMode(LOG_MSGBOX, null);
1609 if(wglChoosePixelFormatARB)
1614 float fAttributes[] = {0,0};
1617 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1618 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1619 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1620 WGL_COLOR_BITS_ARB,24,
1621 WGL_ALPHA_BITS_ARB,8,
1622 WGL_DEPTH_BITS_ARB,16,
1623 WGL_STENCIL_BITS_ARB,0,
1624 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1625 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1626 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1630 //Log("Found wglChoosePixelFormatARB\n");
1632 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1633 if(!valid || !numFormats)
1635 //Log("Can't find 4x multi sampling\n");
1636 iAttributes[19] = 2;
1637 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1638 if(!valid || !numFormats)
1640 // Log("Can't find 2x multi sampling\n");
1641 iAttributes[16] = 0;
1642 iAttributes[17] = 0;
1643 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1646 if(valid && numFormats)
1648 oglSystem.format = pixelFormat;
1649 wglMakeCurrent(null, null);
1650 wglDeleteContext(oglSystem.glrc);
1652 // *** DescribePixelFormat does not support WGL pixel formats! ***
1653 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1654 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1655 //Log("Successfully set pixel format\n");
1657 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1658 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1662 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1666 CheckExtensions(oglSystem);
1668 wglMakeCurrent(null, null);
1670 //eSystem_DumpErrors(true);
1674 #elif defined(__unix__) || defined(__APPLE__)
1675 vboAvailable = true;
1676 #if defined(__ANDROID__)
1677 egl_init_display(guiApp.desktop.windowHandle);
1678 CheckExtensions(oglSystem);
1680 #elif defined(__EMSCRIPTEN__)
1681 if(glfwInit() == GL_TRUE)
1683 const int width = 640, height = 480;
1684 if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
1686 //glfwSwapBuffers();
1690 printf("glfwOpenWindow() failed\n"); //glfwTerminate();
1693 printf("glfwInit() failed\n"); //glfwTerminate();
1696 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1697 XSetWindowAttributes attr;
1702 #ifndef ECERE_MINIGLX
1703 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1706 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1710 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1711 attr.background_pixel = 0;
1712 attr.border_pixel = 0;
1713 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1714 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1715 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1717 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1718 oglSystem.visualInfo->visual, mask, &attr );
1720 if(oglSystem.visualInfo)
1722 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1723 if(oglSystem.glContext)
1725 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1727 CheckExtensions(oglSystem);
1728 glXMakeCurrent(xGlobalDisplay, None, null);
1735 displaySystem.flags.alpha = true;
1736 displaySystem.flags.flipping = true;
1737 displaySystem.pixelFormat = pixelFormat888;
1741 void DestroyDisplaySystem(DisplaySystem displaySystem)
1743 OGLSystem oglSystem = displaySystem.driverData;
1745 #if defined(__WIN32__)
1746 wglMakeCurrent( null, null );
1749 wglDeleteContext(oglSystem.glrc);
1752 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1753 DestroyWindow(oglSystem.hwnd);
1755 #elif defined(__unix__) || defined(__APPLE__)
1756 #if defined(__ANDROID__)
1758 #elif defined(__EMSCRIPTEN__)
1761 if(oglSystem.visualInfo)
1763 #ifdef ECERE_MINIGLX
1764 __miniglx_XFree(oglSystem.visualInfo);
1766 XFree(oglSystem.visualInfo);
1770 if(oglSystem.glxDrawable)
1772 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1773 oglSystem.glxDrawable = 0;
1780 bool CreateDisplay(Display display)
1782 bool result = false;
1783 OGLDisplay oglDisplay = display.driverData;
1784 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1785 OGLSystem oglSystem = display.displaySystem.driverData;
1788 oglDisplay = display.driverData = OGLDisplay { };
1789 //printf("Inside CreateDisplay\n");
1791 #if defined(__WIN32__) || defined(USEPBUFFER)
1792 if(!display.alphaBlend)
1795 #if defined(__WIN32__)
1796 oglDisplay.hdc = GetDC(display.window);
1797 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1798 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1800 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1801 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1805 ReleaseDC(display.window, oglDisplay.hdc);
1806 #elif defined(__unix__) || defined(__APPLE__)
1807 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1809 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1811 #if defined(__APPLE__)
1812 XVisualInfo template = { 0 };
1813 XWindowAttributes winAttr;
1815 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1816 template.visualid = XVisualIDFromVisual(winAttr.visual);
1817 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1819 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1820 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1821 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1822 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1824 // visualInfo = oglSystem.visualInfo;
1829 //printf("visualInfo is not null\n");
1830 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1831 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1832 //XFree(visualInfo);
1835 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1836 if(oglDisplay.glContext)
1838 //printf("CreateDisplay Got a Context\n");
1839 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1845 #if defined(__WIN32__) || defined(USEPBUFFER)
1851 #if defined(__WIN32__)
1852 if(glBlendFuncSeparate)
1853 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1855 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1857 #if !defined(__OLDX__)
1858 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1860 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1865 glMatrixMode(GL_MODELVIEW);
1866 glScaled(1.0, 1.0, -1.0);
1867 // glTranslatef(0.375f, 0.375f, 0.0f);
1868 // glTranslatef(-0.625f, -0.625f, 0.0f);
1869 glMatrixMode(GL_PROJECTION);
1870 glShadeModel(GL_FLAT);
1872 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1873 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1874 glFogi(GL_FOG_MODE, GL_EXP);
1875 glFogf(GL_FOG_DENSITY, 0);
1876 glEnable(GL_NORMALIZE);
1877 glDepthFunc(GL_LESS);
1879 glDisable(GL_MULTISAMPLE_ARB);
1881 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1882 display.ambient = Color { 50,50,50 };
1885 if(!useSingleGLContext)
1887 #if defined(__WIN32__)
1888 wglMakeCurrent(null, null);
1889 #elif defined(__unix__) || defined(__APPLE__)
1890 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1893 glXMakeCurrent(xGlobalDisplay, None, null);
1901 bool DisplaySize(Display display, int width, int height)
1903 OGLDisplay oglDisplay = display.driverData;
1905 bool result = false;
1907 //printf("Inside DisplaySize\n");
1908 #if defined(__WIN32__) || defined(USEPBUFFER)
1909 OGLSystem oglSystem = display.displaySystem.driverData;
1910 if(display.alphaBlend)
1912 #if defined(__WIN32__)
1913 const int attributes[]=
1915 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1916 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1918 int pixelFormat = 0;
1919 if(wglChoosePixelFormatARB)
1923 float fAttributes[] = {0,0};
1926 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1927 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1928 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1929 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1930 WGL_COLOR_BITS_ARB,24,
1931 WGL_ALPHA_BITS_ARB,8,
1932 WGL_DEPTH_BITS_ARB,16,
1933 WGL_STENCIL_BITS_ARB,0,
1934 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1935 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1936 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1940 //Log("Found wglChoosePixelFormatARB\n");
1942 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1943 if(!valid || !numFormats)
1945 //Log("Can't find 4x multi sampling\n");
1946 iAttributes[19] = 2;
1947 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1948 if(!valid || !numFormats)
1950 // Log("Can't find 2x multi sampling\n");
1951 iAttributes[16] = 0;
1952 iAttributes[17] = 0;
1953 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1954 if(!valid || !numFormats)
1958 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1959 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1960 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1961 WGL_COLOR_BITS_ARB,24,
1962 WGL_ALPHA_BITS_ARB,8,
1963 WGL_DEPTH_BITS_ARB,16,
1966 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1970 if(valid && numFormats)
1972 wglMakeCurrent(null, null);
1976 wglMakeCurrent( null, null );
1977 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1978 if(oglDisplay.hdc && oglDisplay.pBuffer)
1979 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1981 wglDestroyPbufferARB(oglDisplay.pBuffer);
1983 if(!useSingleGLContext)
1984 wglMakeCurrent( null, null );
1987 wglDeleteContext(oglDisplay.glrc);
1989 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1990 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1991 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1994 HDC hdc = GetDC(display.window);
1996 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1997 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1999 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
2000 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
2002 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
2004 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
2008 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
2009 oglDisplay.memDC = CreateCompatibleDC(hdc);
2010 SetMapMode(oglDisplay.memDC, MM_TEXT);
2011 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2012 info->bmiHeader.biPlanes = 1;
2013 info->bmiHeader.biCompression = BI_RGB;
2014 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
2015 info->bmiHeader.biWidth = width;
2016 info->bmiHeader.biHeight = height;
2017 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
2020 SelectObject(oglDisplay.memDC, newBitmap);
2021 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
2024 PIXELFORMATDESCRIPTOR pfd = { 0 };
2025 pfd.nSize = (short)sizeof(pfd);
2027 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
2028 pfd.iPixelType = PFD_TYPE_RGBA;
2029 pfd.cColorBits = 32;
2030 //pfd.cAlphaBits = 8;
2031 pfd.cDepthBits = 24;
2032 pfd.iLayerType = PFD_MAIN_PLANE;
2034 oglDisplay.hdc = oglDisplay.memDC;
2036 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
2037 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
2038 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
2040 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
2041 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2042 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2047 const int imageSize = width * height * 4;
2049 glGenBuffersARB(2, oglDisplay.imageBuffers);
2051 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2052 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
2053 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2054 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
2057 oglDisplay.memBitmap = newBitmap;
2058 oglDisplay.stride = width;
2064 ReleaseDC(display.window, hdc);
2066 #elif defined(__unix__) || defined(__APPLE__)
2067 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2072 GLX_DOUBLEBUFFER, True,
2078 GLX_STENCIL_SIZE, 1,
2079 //GLX_DEPTH_SIZE, 24,
2080 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2081 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
2087 GLX_PBUFFER_WIDTH, width,
2088 GLX_PBUFFER_HEIGHT, height,
2089 GLX_LARGEST_PBUFFER, False,
2093 // choose a pixel format that meets our minimum requirements
2096 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
2099 if(oglDisplay.pixmap)
2101 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2102 oglDisplay.pixmap = None;
2104 if(oglDisplay.shapePixmap)
2106 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
2107 oglDisplay.shapePixmap = None;
2110 // Free Shared Memory Pixmap
2111 if(oglDisplay.image)
2113 if(oglDisplay.shminfoShape.shmid != -1)
2115 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
2116 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2117 shmdt(oglDisplay.shminfo.shmaddr);
2118 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
2120 XDestroyImage(oglDisplay.image);
2121 oglDisplay.image = None;
2123 if(oglDisplay.shapeImage)
2125 if(oglDisplay.shminfoShape.shmid != -1)
2127 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
2128 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2129 shmdt(oglDisplay.shminfoShape.shmaddr);
2130 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
2132 XDestroyImage(oglDisplay.shapeImage);
2133 oglDisplay.shapeImage = None;
2136 if(oglDisplay.windowPicture)
2137 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
2138 if(oglDisplay.pixmapPicture)
2139 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
2141 if(oglDisplay.pixmap)
2142 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2144 if(oglDisplay.glContext)
2145 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
2146 if(oglDisplay.pBuffer)
2147 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
2149 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
2150 if(oglDisplay.pBuffer)
2152 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2153 if(oglDisplay.glContext)
2155 glXMakeCurrent(xGlobalDisplay, None, null);
2156 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2158 // Initialize Shared Memory Pixmap
2159 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2160 ZPixmap, null, &oglDisplay.shminfo, width, height);
2161 if(oglDisplay.image)
2163 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2164 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2165 if(oglDisplay.shminfo.shmid != -1)
2167 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2168 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2170 oglDisplay.shminfo.readOnly = False;
2171 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2173 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2174 &oglDisplay.shminfo, width, height, 32);
2176 // Initialize Shared Memory Shape Pixmap
2177 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2178 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2179 if(oglDisplay.shapeImage)
2181 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2182 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2183 if(oglDisplay.shminfoShape.shmid != -1)
2185 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2186 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2188 oglDisplay.shminfoShape.readOnly = False;
2189 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2191 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2192 &oglDisplay.shminfoShape, width, height, 1);
2193 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2196 XRenderPictureAttributes attributes = { 0 };
2197 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2198 #if !defined(__APPLE__) && !defined(__OLDX__)
2199 attributes.repeat = RepeatNormal;
2201 attributes.repeat = 1;
2203 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2204 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2205 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2206 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2209 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2210 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2227 CreateDisplay(display);
2228 #if defined(__WIN32__)
2229 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2230 #elif defined(__unix__) || defined(__APPLE__)
2231 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2235 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2242 if(!result && display.alphaBlend)
2244 printf("Alpha blending windows not supported on this display\n");
2251 glViewport(0,0,width,height);
2253 glOrtho(0,width,height,0,0.0,1.0);
2254 displayWidth = display.width = width;
2255 displayHeight = display.height = height;
2257 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2259 oglDisplay.flipBufW = width;
2260 oglDisplay.flipBufH = height;
2264 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2267 if(oglDisplay.flippingBuffer || !width || !height)
2273 void DisplayPosition(Display display, int x, int y)
2275 OGLDisplay oglDisplay = display.driverData;
2281 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2285 void RestorePalette(Display display)
2289 void StartUpdate(Display display)
2293 void EndUpdate(Display display)
2297 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2301 void Update(Display display, Box updateBox)
2303 #if defined(__WIN32__) || defined(USEPBUFFER)
2304 OGLDisplay oglDisplay = display.driverData;
2306 //Logf("DisplayScreen\n");
2310 #if defined(__WIN32__) || defined(USEPBUFFER)
2311 if(display.alphaBlend)
2313 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2314 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2315 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2316 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2317 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2320 #if defined(__WIN32__)
2322 POINT point = { oglDisplay.x, oglDisplay.y};
2323 POINT srcPoint = { 0, 0 };
2324 BLENDFUNCTION blend = { 0 };
2326 size.cx = display.width;
2327 size.cy = display.height;
2328 blend.BlendOp = AC_SRC_OVER;
2329 blend.BlendFlags = 0;
2330 blend.SourceConstantAlpha = 255;
2331 blend.AlphaFormat = AC_SRC_ALPHA;
2334 // Process partial images. Mapping the buffer waits for
2335 // outstanding DMA transfers into the buffer to finish.
2336 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2337 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2339 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2340 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2343 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2344 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2347 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2350 // Unmap the image buffers
2351 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2352 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2354 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2355 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2357 // Bind two different buffer objects and start the glReadPixels
2358 // asynchronously. Each call will return directly after
2359 // starting the DMA transfer.
2360 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2361 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2363 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2364 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2368 #elif defined(__unix__) || defined(__APPLE__)
2369 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2371 XTransform transform =
2374 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2375 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2376 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2379 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2380 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2381 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2382 #if !defined(__APPLE__) && !defined(__OLDX__)
2383 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2385 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2387 XFlush(xGlobalDisplay);
2395 #if defined(__WIN32__)
2396 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2397 SwapBuffers(oglDisplay.hdc);
2398 #elif defined(__unix__) || defined(__APPLE__)
2399 #if defined(__ANDROID__)
2400 eglSwapBuffers(eglDisplay, eglSurface);
2401 #elif defined(__EMSCRIPTEN__)
2404 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2408 //Logf("Out of DisplayScreen\n");
2411 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2413 if(bitmap.driverData)
2415 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2416 glDeleteTextures(1, &tex);
2417 bitmap.driverData = 0;
2419 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2422 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2424 OGLSystem oglSystem = displaySystem.driverData;
2425 bool result = false;
2427 GLuint glBitmap = 0;
2429 uint w = width, h = height;
2430 if(oglSystem.pow2textures)
2435 w = Min(w, oglSystem.maxTextureSize);
2436 h = Min(h, oglSystem.maxTextureSize);
2438 glGenTextures(1, &glBitmap);
2439 glBindTexture(GL_TEXTURE_2D, glBitmap);
2441 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2443 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2444 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2446 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2449 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2451 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2453 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2454 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2458 bitmap.driverData = (void *)(uintptr)glBitmap;
2459 bitmap.driver = displaySystem.driver;
2467 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2469 bool result = false;
2470 OGLSystem oglSystem = displaySystem.driverData;
2471 Bitmap convBitmap = bitmap;
2475 convBitmap.Copy(bitmap);
2478 // Pre process the bitmap... First make it 32 bit
2479 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
2482 uint w = bitmap.width, h = bitmap.height;
2483 GLuint glBitmap = 0;
2484 if(oglSystem.pow2textures)
2489 w = Min(w, oglSystem.maxTextureSize);
2490 h = Min(h, oglSystem.maxTextureSize);
2494 while(w * 2 < h) w *= 2;
2495 while(h * 2 < w) h *= 2;
2498 // Switch ARGB to RGBA
2499 //if(bitmap.format != pixelFormatRGBA)
2501 for(c=0; c<bitmap.size; c++)
2503 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2505 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
2506 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2509 // convBitmap.pixelFormat = pixelFormat888;
2512 glGenTextures(1, &glBitmap);
2515 //int error = glGetError();
2519 glBindTexture(GL_TEXTURE_2D, glBitmap);
2520 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2522 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2523 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2524 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2526 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2527 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2529 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2530 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2532 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2536 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2539 if(bitmap.width != w || bitmap.height != h)
2541 mipMap = Bitmap { };
2542 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
2544 Surface mipSurface = mipMap.GetSurface(0,0,null);
2545 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
2555 mipMap = convBitmap;
2562 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2563 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2564 //printf("Calling glTexImage2D\n");
2565 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2566 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2567 if((error = glGetError()))
2569 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2570 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2574 if(mipMap != convBitmap)
2579 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
2580 bitmap.driverData = (void *)(uintptr)glBitmap;
2581 bitmap.driver = displaySystem.driver;
2584 FreeBitmap(displaySystem, bitmap);
2585 else if(oglSystem.loadingFont)
2587 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2588 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2589 oglSystem.loadingFont = false;
2595 void ReleaseSurface(Display display, Surface surface)
2597 glDisable(GL_SCISSOR_TEST);
2598 delete surface.driverData;
2599 surface.driverData = null;
2602 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2607 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2609 bool result = false;
2610 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2612 //Logf("GetSurface\n");
2616 if(displayWidth != display.width || displayHeight != display.height)
2618 displayWidth = display.width;
2619 displayHeight = display.height;
2621 glViewport(0,0,display.width,display.height);
2623 glOrtho(0,display.width,display.height,0,0.0,1.0);
2626 surface.offset.x = x;
2627 surface.offset.y = y;
2628 surface.unclippedBox = surface.box = clip;
2629 oglSurface.bitmapMult[0] = 1;
2630 oglSurface.bitmapMult[1] = 1;
2631 oglSurface.bitmapMult[2] = 1;
2632 oglSurface.bitmapMult[3] = 1;
2634 glEnable(GL_SCISSOR_TEST);
2637 (display.height) -(y+clip.bottom)-1,
2638 clip.right-clip.left+1,
2639 clip.bottom-clip.top+1);
2645 void Clip(Display display, Surface surface, Box clip)
2654 box.Clip(surface.unclippedBox);
2658 box = surface.box = surface.unclippedBox;
2659 box.left += surface.offset.x;
2660 box.top += surface.offset.y;
2661 box.right+= surface.offset.x;
2662 box.bottom += surface.offset.y;
2665 box.left,display.height - box.bottom - 1,
2666 box.right-box.left+1, box.bottom-box.top+1);
2669 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2671 bool result = false;
2672 OGLDisplay oglDisplay = display.driverData;
2673 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2675 if(oglDisplay.flippingBuffer)
2677 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2680 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2686 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2687 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2688 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2689 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2690 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2693 for(row = 0; row<h; row++)
2694 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2701 void SetForeground(Display display, Surface surface, ColorAlpha color)
2703 OGLSurface oglSurface = surface.driverData;
2705 //Logf("SetForeground\n");
2707 oglSurface.foreground[0] = color.color.r/255.0f;
2708 oglSurface.foreground[1] = color.color.g/255.0f;
2709 oglSurface.foreground[2] = color.color.b/255.0f;
2710 //oglSurface.foreground[3] = 1.0f;
2711 oglSurface.foreground[3] = color.a/255.0f;
2713 //if(!oglSurface.foreground[3])printf("bug");
2716 void SetBackground(Display display, Surface surface, ColorAlpha color)
2718 OGLSurface oglSurface = surface.driverData;
2720 //Logf("SetBackground\n");
2722 oglSurface.background[0] = color.color.r/255.0f;
2723 oglSurface.background[1] = color.color.g/255.0f;
2724 oglSurface.background[2] = color.color.b/255.0f;
2725 //oglSurface.background[3] = 1.0;
2726 oglSurface.background[3] = color.a/255.0f;
2729 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2731 OGLSurface oglSurface = surface.driverData;
2733 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2734 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2735 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2736 oglSurface.bitmapMult[3] = color.a/255.0f;
2739 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2744 void PutPixel(Display display, Surface surface,int x,int y)
2746 OGLSurface oglSurface = surface.driverData;
2748 //Logf("PutPixel\n");
2750 glColor4fv(oglSurface.foreground);
2752 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2753 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2758 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2760 OGLSurface oglSurface = surface.driverData;
2761 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2776 x1 += surface.offset.x;
2777 y1 += surface.offset.y;
2778 x2 += surface.offset.x;
2779 y2 += surface.offset.y;
2783 glColor4fv(oglSurface.foreground);
2788 glTexCoord2f(0.5f, 0);
2789 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2790 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2791 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2800 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2801 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2807 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2809 OGLSurface oglSurface = surface.driverData;
2810 x1 += surface.offset.x;
2811 y1 += surface.offset.y;
2812 x2 += surface.offset.x;
2813 y2 += surface.offset.y;
2815 //Logf("Rectangle\n");
2817 glColor4fv(oglSurface.foreground);
2823 glTexCoord2f(0.5f, 0);
2824 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2825 glTexCoord2f(y2-y1 + 0.5f, 0);
2826 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2828 glTexCoord2f(0.5f, 0);
2829 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2830 glTexCoord2f(x2 - x1 + 0.5f, 0);
2831 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2833 glTexCoord2f(0.5f, 0);
2834 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2835 glTexCoord2f(y1 - y2 + 0.5f, 0);
2836 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2838 glTexCoord2f(0.5f, 0);
2839 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2840 glTexCoord2f(x1 - x2 + 0.5f, 0);
2841 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2846 glBegin(GL_LINE_LOOP);
2853 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2854 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2855 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2856 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2861 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2863 OGLSurface oglSurface = surface.driverData;
2866 glColor4fv(oglSurface.background);
2868 #ifdef __EMSCRIPTEN__
2870 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
2871 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
2872 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
2873 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
2876 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2877 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2880 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2881 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2885 void Clear(Display display, Surface surface, ClearType type)
2887 OGLDisplay oglDisplay = display.driverData;
2888 OGLSurface oglSurface = surface.driverData;
2891 if(type != depthBuffer)
2892 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2893 if(type != colorBuffer && !oglDisplay.depthWrite)
2895 glDepthMask((byte)bool::true);
2897 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2898 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2899 if(type != colorBuffer && !oglDisplay.depthWrite)
2901 glDepthMask((byte)bool::false);
2905 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2910 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2912 OGLSurface oglSurface = surface.driverData;
2914 #if !defined(__OLDX__)
2915 // WHY DO WE HAVE GL_ONE HERE ?
2916 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2917 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2920 if(!oglSurface.writingText)
2922 // glTranslatef(-0.375f, -0.375f, 0.0f);
2923 glEnable(GL_TEXTURE_2D);
2924 glColor4fv(oglSurface.bitmapMult);
2926 else if(oglSurface.xOffset)
2927 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2929 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2934 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2935 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2936 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2937 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2938 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2939 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2940 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2941 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2946 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2947 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2948 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2949 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2950 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2951 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2952 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2953 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2956 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2957 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2958 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2959 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2960 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2961 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2962 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2963 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2967 if(!oglSurface.writingText)
2969 glDisable(GL_TEXTURE_2D);
2971 //glTranslate(0.375, 0.375, 0.0);
2973 else if(oglSurface.xOffset)
2974 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2976 #if !defined(__OLDX__)
2977 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2978 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2982 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2984 OGLSurface oglSurface = surface.driverData;
2986 //glTranslate(-0.375, -0.375, 0.0);
2988 //Logf("Stretch\n");
2990 #if !defined(__OLDX__)
2991 /*if(glBlendFuncSeparate)
2992 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2995 glEnable(GL_TEXTURE_2D);
2996 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2998 glColor4fv(oglSurface.bitmapMult);
3004 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3005 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3007 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3008 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3010 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3011 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
3013 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3014 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
3018 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3019 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3021 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3022 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3024 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3025 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
3027 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3028 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
3033 glDisable(GL_TEXTURE_2D);
3035 //glTranslate(0.375, 0.375, 0.0);
3036 #if !defined(__OLDX__)
3037 /*if(glBlendFuncSeparate)
3038 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3043 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3045 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3048 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3050 float s2dw,s2dh,d2sw,d2sh;
3051 //bool flipX = false, flipY = false;
3053 //Logf("StretchDI\n");
3055 if(Sgn(w) != Sgn(sw))
3061 if(Sgn(h) != Sgn(sh))
3073 //Clip against the edges of the source
3076 dx+=(int)((0-sx) * s2dw);
3077 w-=(int)((0-sx) * s2dw);
3083 dy+=(int)((0-sy) * s2dh);
3084 h-=(int)((0-sy) * s2dh);
3089 if(sx+sw>bitmap.width-1)
3091 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
3092 sw-=sx+sw-(bitmap.width-1)-1;
3094 if(sy+sh>(bitmap.height-1))
3096 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
3097 sh-=sy+sh-(bitmap.height-1)-1;
3099 //Clip against the edges of the surfaceination
3100 if(dx<surface.box.left)
3103 sx+=(int)((surface.box.left-dx)*d2sw);
3104 sw-=(int)((surface.box.left-dx)*d2sw);
3105 w-=surface.box.left-dx;
3106 dx=surface.box.left;
3108 if(dy<surface.box.top)
3110 sy+=(int)((surface.box.top-dy)*d2sh);
3111 sh-=(int)((surface.box.top-dy)*d2sh);
3112 h-=surface.box.top-dy;
3115 if(dx+w>surface.box.right)
3117 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
3118 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
3119 w-=dx+w-surface.box.right-1;
3121 if(dy+h>surface.box.bottom)
3123 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
3124 h-=dy+h-surface.box.bottom-1;
3126 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
3128 dx += surface.offset.x;
3129 dy += surface.offset.y;
3131 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3133 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3134 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3135 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3136 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3137 glRasterPos2d(dx,dy);
3138 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
3139 glPixelZoom(s2dw, -s2dh);
3140 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3141 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3142 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3143 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3144 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3148 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3152 //Clip against the edges of the source
3165 if(sx+w>bitmap.width-1)
3166 w-=sx+w-(bitmap.width-1)-1;
3167 if(sy+h>bitmap.height-1)
3168 h-=sy+h-(bitmap.height-1)-1;
3169 //Clip against the edges of the surfaceination
3170 if(dx<surface.box.left)
3173 sx+=surface.box.left-dx;
3174 w-=surface.box.left-dx;
3175 dx=surface.box.left;
3177 if(dy<surface.box.top)
3179 sy+=surface.box.top-dy;
3180 h-=surface.box.top-dy;
3183 if(dx+w>surface.box.right)
3185 //if(flip) sx+=dx+w-surface.box.right-1;
3186 w-=dx+w-surface.box.right-1;
3188 if(dy+h>surface.box.bottom)
3189 h-=dy+h-surface.box.bottom-1;
3193 dx += surface.offset.x;
3194 dy += surface.offset.y;
3196 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3198 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3199 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3200 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3201 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3202 glRasterPos2d(dx,dy);
3204 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3205 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3206 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3207 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3208 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3212 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3214 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3217 void UnloadFont(DisplaySystem displaySystem, Font font)
3219 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3222 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
3225 OGLSystem oglSystem = displaySystem.driverData;
3226 oglSystem.loadingFont = true;
3227 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3231 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
3233 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3236 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
3238 OGLSurface oglSurface = surface.driverData;
3239 OGLSystem oglSystem = display.displaySystem.driverData;
3240 oglSystem.loadingFont = true;
3242 //glTranslated(-0.375, -0.375, 0.0);
3246 if(surface.textOpacity)
3249 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3250 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3253 oglSurface.writingText = true;
3255 glEnable(GL_TEXTURE_2D);
3257 if(surface.outline.size)
3259 ColorAlpha outlineColor = surface.outline.color;
3260 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
3261 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3262 //glEnable(GL_BLEND);
3264 oglSurface.writingOutline = true;
3265 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3266 oglSurface.writingOutline = false;
3268 glColor4fv(oglSurface.foreground);
3270 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3271 oglSurface.writingText = false;
3272 oglSystem.loadingFont = false;
3274 glDisable(GL_TEXTURE_2D);
3276 //glTranslated(0.375, 0.375, 0.0);
3279 void TextFont(Display display, Surface surface, Font font)
3281 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3284 void TextOpacity(Display display, Surface surface, bool opaque)
3286 OGLSurface oglSurface = surface.driverData;
3287 oglSurface.opaqueText = opaque;
3290 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
3292 OGLSurface oglSurface = surface.driverData;
3293 OGLSystem oglSystem = display.displaySystem.driverData;
3294 oglSystem.loadingFont = true;
3295 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3296 oglSystem.loadingFont = false;
3299 void DrawingChar(Display display, Surface surface, char character)
3304 void LineStipple(Display display, Surface surface, uint32 stipple)
3306 //Logf("Stipple\n");
3311 stippleEnabled = true;
3312 glesLineStipple(1, (uint16)stipple);
3314 glLineStipple(1, (uint16)stipple);
3315 glEnable(GL_LINE_STIPPLE);
3321 stippleEnabled = false;
3322 glMatrixMode(GL_TEXTURE);
3324 glMatrixMode(GL_PROJECTION);
3325 glDisable(GL_TEXTURE_2D);
3327 glDisable(GL_LINE_STIPPLE);
3331 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3332 void SetRenderState(Display display, RenderState state, uint value)
3334 OGLDisplay oglDisplay = display.driverData;
3335 //Logf("RenderState\n");
3341 glEnable(GL_MULTISAMPLE_ARB);
3343 glDisable(GL_MULTISAMPLE_ARB);
3347 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3351 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3354 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3355 oglDisplay.depthWrite = (bool)value;
3359 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3360 glFogfv(GL_FOG_COLOR, (float *)&color);
3364 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
3367 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3371 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3372 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3377 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3382 #if defined(__WIN32__)
3383 wglSwapIntervalEXT(value ? 1 : 0);
3390 void SetLight(Display display, int id, Light light)
3392 //Logf("SetLight\n");
3396 Object lightObject = light.lightObject;
3397 float position[4] = { 0, 0, 0, 0 };
3398 float color[4] = { 0, 0, 0, 1 };
3400 glEnable(GL_LIGHT0 + id);
3402 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3403 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3404 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3407 if(!light.multiplier) light.multiplier = 1.0f;
3409 color[0] = light.diffuse.r * light.multiplier;
3410 color[1] = light.diffuse.g * light.multiplier;
3411 color[2] = light.diffuse.b * light.multiplier;
3412 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3414 color[0] = light.ambient.r * light.multiplier;
3415 color[1] = light.ambient.g * light.multiplier;
3416 color[2] = light.ambient.b * light.multiplier;
3417 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3418 color[0] = light.specular.r * light.multiplier;
3419 color[1] = light.specular.g * light.multiplier;
3420 color[2] = light.specular.b * light.multiplier;
3421 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3425 Vector3D positionVector;
3426 if(light.flags.spot)
3428 if(lightObject.flags.root || !lightObject.parent)
3430 positionVector = lightObject.transform.position;
3431 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3435 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3436 if(display.display3D.camera)
3437 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3443 if(!light.direction.x && !light.direction.y && !light.direction.z)
3445 Vector3Df vector { 0,0,-1 };
3447 mat.RotationQuaternion(light.orientation);
3448 positionVector.MultMatrixf(vector, mat);
3452 positionVector = light.direction;
3457 position[0] = (float)positionVector.x;
3458 position[1] = (float)positionVector.y;
3459 position[2] = (float)positionVector.z;
3461 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3464 // Display Light Position
3465 glDisable(GL_LIGHTING);
3466 glDisable(GL_DEPTH_TEST);
3470 glVertex3fv(position);
3472 glEnable(GL_DEPTH_TEST);
3473 glEnable(GL_LIGHTING);
3477 if(lightObject.flags.root || !lightObject.parent)
3479 positionVector = light.target.transform.position;
3480 positionVector.Subtract(positionVector, display.camera.cPosition);
3484 positionVector.MultMatrix(light.target.transform.position,
3485 lightObject.light.target.parent.matrix);
3486 positionVector.Subtract(positionVector, display.camera.cPosition);
3489 position[0] = positionVector.x;
3490 position[1] = positionVector.y;
3491 position[2] = positionVector.z;
3493 glDisable(GL_LIGHTING);
3494 glDisable(GL_DEPTH_TEST);
3498 glVertex3fv(position);
3500 glEnable(GL_DEPTH_TEST);
3501 glEnable(GL_LIGHTING);
3504 if(light.flags.attenuation)
3506 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3507 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3508 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3511 if(light.flags.spot)
3514 #define MAXLIGHT 0.9
3515 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3516 // Figure out exponent out of the hot spot
3517 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3519 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3520 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3521 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3527 Vector3Df vector { 0,0,-1 };
3528 Vector3Df direction;
3531 mat.RotationQuaternion(light.orientation);
3532 direction.MultMatrix(vector, mat);
3534 position[0] = direction.x;
3535 position[1] = direction.y;
3536 position[2] = direction.z;
3538 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3542 glDisable(GL_LIGHT0 + id);
3545 void SetCamera(Display display, Surface surface, Camera camera)
3547 OGLDisplay oglDisplay = display.driverData;
3548 //Logf("SetCamera\n");
3552 int left = surface.box.left + surface.offset.x;
3553 int top = surface.box.top + surface.offset.y;
3554 int right = surface.box.right + surface.offset.x;
3555 int bottom = surface.box.bottom + surface.offset.y;
3556 float origX = surface.offset.x + camera.origin.x;
3557 float origY = surface.offset.y + camera.origin.y;
3559 int y = display.height - bottom - 1;
3560 int w = right - left + 1;
3561 int h = bottom - top + 1;
3564 glViewport(x, y, w, h);
3566 // *** Projection Matrix ***
3567 if(!display.display3D.camera)
3570 glMatrixMode(GL_PROJECTION);
3571 if(display.display3D.collectingHits)
3573 float pickX = display.display3D.pickX + surface.offset.x;
3574 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3578 w / display.display3D.pickWidth, 0, 0, 0,
3579 0, h / display.display3D.pickHeight, 0, 0,
3581 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3582 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3585 glLoadMatrixd(pickMatrix.array);
3590 (left - origX) * camera.zMin / camera.focalX,
3591 (right - origX) * camera.zMin / camera.focalX,
3592 (bottom - origY) * camera.zMin / camera.focalY,
3593 (top - origY) * camera.zMin / camera.focalY,
3594 camera.zMin, camera.zMax);
3596 glDisable(GL_BLEND);
3598 // *** Z Inverted Identity Matrix ***
3599 glMatrixMode(GL_MODELVIEW);
3600 if(!display.display3D.camera)
3605 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3607 // *** View Matrix ***
3608 glMultMatrixd(camera.viewMatrix.array);
3613 glEnable(GL_DEPTH_TEST);
3614 glEnable(GL_LIGHTING);
3615 glShadeModel(GL_SMOOTH);
3616 glDepthMask((byte)bool::true);
3617 oglDisplay.depthWrite = true;
3619 glEnable(GL_MULTISAMPLE_ARB);
3621 else if(display.display3D.camera)
3623 oglDisplay.depthWrite = false;
3624 glViewport(0,0,display.width,display.height);
3626 glDisable(GL_CULL_FACE);
3627 glDisable(GL_DEPTH_TEST);
3628 glDisable(GL_LIGHTING);
3630 glDisable(GL_TEXTURE_2D);
3631 glShadeModel(GL_FLAT);
3633 glDisable(GL_MULTISAMPLE_ARB);
3635 // *** Restore 2D MODELVIEW Matrix ***
3638 // *** Restore 2D PROJECTION Matrix ***
3639 glMatrixMode(GL_PROJECTION);
3643 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3646 void ApplyMaterial(Display display, Material material, Mesh mesh)
3648 //Logf("ApplyMaterial\n");
3651 if(material.flags.doubleSided)
3653 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3654 glDisable(GL_CULL_FACE);
3658 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3659 glEnable(GL_CULL_FACE);
3663 if(material.flags.noFog)
3669 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
3671 Bitmap map = material.baseMap;
3672 glEnable(GL_TEXTURE_2D);
3673 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3675 glMatrixMode(GL_TEXTURE);
3677 if(material.uScale && material.vScale)
3678 glScalef(material.uScale, material.vScale, 1);
3679 glMatrixMode(GL_MODELVIEW);
3681 if(material.flags.tile)
3683 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3684 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3688 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3689 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3693 glDisable(GL_TEXTURE_2D);
3695 if(mesh.flags.colors)
3697 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3698 glEnable(GL_COLOR_MATERIAL);
3702 glDisable(GL_COLOR_MATERIAL);
3704 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3705 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3708 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3709 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3713 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3714 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3717 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3718 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3721 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3724 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3726 OGLMesh oglMesh = mesh.data;
3729 if(!mesh.flags.vertices)
3731 if(oglMesh.vertices)
3733 GLDeleteBuffers(1, &oglMesh.vertices);
3734 oglMesh.vertices = 0;
3736 delete mesh.vertices;
3738 if(!mesh.flags.normals)
3742 GLDeleteBuffers(1, &oglMesh.normals);
3743 oglMesh.normals = 0;
3745 delete mesh.normals;
3747 if(!mesh.flags.texCoords1)
3749 if(oglMesh.texCoords)
3751 GLDeleteBuffers(1, &oglMesh.texCoords);
3752 oglMesh.texCoords = 0;
3754 delete mesh.texCoords;
3756 if(!mesh.flags.texCoords2)
3758 if(oglMesh.texCoords2)
3760 GLDeleteBuffers(1, &oglMesh.texCoords2);
3761 oglMesh.texCoords2 = 0;
3764 delete mesh.texCoords2;
3767 if(!mesh.flags.colors)
3771 GLDeleteBuffers(1, &oglMesh.colors);
3783 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3785 bool result = false;
3788 mesh.data = OGLMesh { };
3791 OGLMesh oglMesh = mesh.data;
3792 if(mesh.nVertices == nVertices)
3794 // Same number of vertices, adding features (Leaves the other features pointers alone)
3795 if(mesh.flags != flags)
3797 if(!mesh.flags.vertices && flags.vertices)
3799 if(flags.doubleVertices)
3801 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3804 mesh.vertices = new Vector3Df[nVertices];
3805 if(!oglMesh.vertices)
3806 GLGenBuffers(1, &oglMesh.vertices);
3808 if(!mesh.flags.normals && flags.normals)
3810 if(flags.doubleNormals)
3812 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3815 mesh.normals = new Vector3Df[nVertices];
3816 if(!oglMesh.normals)
3817 GLGenBuffers( 1, &oglMesh.normals);
3819 if(!mesh.flags.texCoords1 && flags.texCoords1)
3821 mesh.texCoords = new Pointf[nVertices];
3822 if(!oglMesh.texCoords)
3823 GLGenBuffers( 1, &oglMesh.texCoords);
3825 if(!mesh.flags.colors && flags.colors)
3827 mesh.colors = new ColorRGBAf[nVertices];
3829 GLGenBuffers( 1, &oglMesh.colors);
3835 // New number of vertices, reallocate all current and new features
3836 flags |= mesh.flags;
3839 if(flags.doubleVertices)
3841 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3844 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3845 if(!oglMesh.vertices)
3846 GLGenBuffers(1, &oglMesh.vertices);
3850 if(flags.doubleNormals)
3852 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3855 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3856 if(!oglMesh.normals)
3857 GLGenBuffers( 1, &oglMesh.normals);
3859 if(flags.texCoords1)
3861 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3862 if(!oglMesh.texCoords)
3863 GLGenBuffers( 1, &oglMesh.texCoords);
3867 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3869 GLGenBuffers( 1, &oglMesh.colors);
3877 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3879 OGLMesh oglMesh = mesh.data;
3880 if(!flags) flags = mesh.flags;
3884 if(flags.vertices && oglMesh.vertices)
3886 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3887 GLBufferData( mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3890 if(flags.normals && oglMesh.normals)
3892 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3893 GLBufferData( mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
3896 if(flags.texCoords1 && oglMesh.texCoords)
3898 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3899 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3902 if(flags.colors && oglMesh.colors)
3904 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3905 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3908 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3912 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3919 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3923 if(oglIndices.buffer)
3924 GLDeleteBuffers(1, &oglIndices.buffer);
3925 delete oglIndices.indices;
3930 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3932 OGLIndices oglIndices = OGLIndices { };
3935 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3936 GLGenBuffers( 1, &oglIndices.buffer);
3937 oglIndices.nIndices = nIndices;
3942 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3946 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3947 GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3948 oglIndices.indices, GL_STATIC_DRAW_ARB);
3949 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3953 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3956 return oglIndices.indices;
3959 void SelectMesh(Display display, Mesh mesh)
3961 //Logf("SelectMesh\n");
3963 #if !defined( __ANDROID__) && !defined(__APPLE__)
3965 #if defined(__WIN32__)
3966 if(glUnlockArraysEXT)
3968 if(!vboAvailable && display.display3D.mesh)
3969 glUnlockArraysEXT();
3974 OGLMesh oglMesh = mesh.data;
3976 // *** Vertex Stream ***
3977 glEnableClientState(GL_VERTEX_ARRAY);
3978 if(!display.display3D.collectingHits && oglMesh)
3980 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3981 if(mesh.flags.doubleVertices)
3982 glVertexPointerd(3, 0, (double *)(vboAvailable ? null : mesh.vertices), mesh.nVertices);
3984 glVertexPointer(3, GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
3986 // *** Normals Stream ***
3987 if(mesh.normals || mesh.flags.normals)
3989 glEnableClientState(GL_NORMAL_ARRAY);
3990 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3991 glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
3994 glDisableClientState(GL_NORMAL_ARRAY);
3996 // *** Texture Coordinates Stream ***
3997 if(mesh.texCoords || mesh.flags.texCoords1)
3999 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4000 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
4001 glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
4004 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4006 // *** Color Stream ***
4007 if(mesh.colors || mesh.flags.colors)
4009 glEnableClientState(GL_COLOR_ARRAY);
4010 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
4011 glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
4014 glDisableClientState(GL_COLOR_ARRAY);
4019 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
4020 if(mesh.flags.doubleVertices)
4021 glVertexPointerd(3, 0, (double *)mesh.vertices, mesh.nVertices);
4023 glVertexPointer(3, GL_FLOAT, 0, mesh.vertices);
4024 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
4026 glEnableClientState(GL_NORMAL_ARRAY);
4027 glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, mesh.normals);
4030 glDisableClientState(GL_NORMAL_ARRAY);
4031 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
4033 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4034 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
4037 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4038 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
4040 glEnableClientState(GL_COLOR_ARRAY);
4041 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
4044 glDisableClientState(GL_COLOR_ARRAY);
4047 #if !defined(__ANDROID__) && !defined(__APPLE__)
4049 #if defined(__WIN32__)
4053 glLockArraysEXT(0, mesh.nVertices);
4058 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
4061 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
4063 //Logf("DrawPrimitives\n");
4065 if(primitive->type.vertexRange)
4066 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
4069 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
4070 // HACK TO SPEED THINGS UP...
4072 /*GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4073 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
4076 glBegin(primitiveTypes[primitive->type.primitiveType]);
4079 OGLIndices oglIndices = primitive->data;
4080 MeshFeatures flags = mesh.flags;
4081 for(c = 0; c<primitive->nIndices; c++)
4083 uint16 index = ((uint16 *) oglIndices.indices)[c];
4084 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
4085 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
4086 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
4087 glVertex3fv((float *)&mesh.vertices[index]);
4095 OGLIndices oglIndices = primitive->data;
4097 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
4099 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
4100 if(primitive->type.indices32bit)
4101 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 0);
4103 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, GL_UNSIGNED_SHORT, 0);
4104 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4108 if(primitive->type.indices32bit)
4109 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4110 oglIndices ? oglIndices.indices : primitive->indices);
4112 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4113 GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
4119 void PushMatrix(Display display)
4124 void PopMatrix(Display display, bool setMatrix)
4129 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
4131 Matrix matrix = transMatrix;
4132 Camera camera = useCamera ? display.display3D.camera : null;
4137 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
4142 matrix.m[3][0] - camera.cPosition.x,
4143 matrix.m[3][1] - camera.cPosition.y,
4144 matrix.m[3][2] - camera.cPosition.z);
4156 glMultMatrixd(matrix.array);
4161 public void UseSingleGLContext(bool useSingle)
4163 useSingleGLContext = useSingle;
4166 default dllexport void *
4167 #if defined(__WIN32__)
4168 __attribute__((stdcall))
4170 IS_GLGetContext(DisplaySystem displaySystem)
4174 #if defined(__WIN32__)
4175 OGLSystem system = displaySystem.driverData;
4177 #elif defined(__ANDROID__)
4179 #elif defined(__EMSCRIPTEN__)
4181 OGLSystem system = displaySystem.driverData;
4182 return system.glContext;