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__) && !defined(__ODROID__)
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__) || defined(__ODROID__)
81 #define property _property
84 #define Window X11Window
85 #define Cursor X11Cursor
87 #define Display X11Display
89 #define KeyCode X11KeyCode
90 #define Picture X11Picture
109 #elif defined(__EMSCRIPTEN__)
111 #define property _property
116 //#include <GLES/gl.h>
117 //#include <EGL/egl.h>
119 //#include <GLES2/gl.h>
120 //#include <EGL/egl.h>
122 //#include <GLES2/gl2.h>
124 #include <emscripten/emscripten.h>
132 #include <GL/glext.h>
136 #if defined(__ODROID__) && !defined(_GLES)
140 #if defined(__EMSCRIPTEN__)
151 #if defined(__unix__) || defined(__APPLE__)
153 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
159 static double nearPlane = 1;
161 public double glesGetNearPlane()
166 public void glesSetNearPlane(double value)
171 #define glLoadMatrix glLoadMatrixd
172 #define glMultMatrix glMultMatrixd
173 #define glGetMatrix glGetDoublev
174 #define glTranslate glTranslated
175 #define glScale glScaled
178 #define glVertex3v glVertex3dv
179 #define glNormal3v glNormal3dv
183 //#ifdef VERTEX_FORMAT_DOUBLE
185 #define glLoadMatrix glLoadMatrixd
186 #define glMultMatrix glMultMatrixd
187 #define glGetMatrix glGetDoublev
188 #define glVertex3v glVertex3dv
189 #define glNormal3v glNormal3dv
190 #define glTranslate glTranslated
191 #define glScale glScaled
192 //#define GL_VERTEX_FORMAT GL_DOUBLE
196 #define glLoadMatrix glLoadMatrixf
197 #define glMultMatrix glMultMatrixf
198 #define glGetMatrix glGetFloatv
199 #define glVertex3v glVertex3fv
200 #define glNormal3v glNormal3fv
201 #define glTranslate glTranslatef
202 #define glScale glScalef
203 //#define GL_VERTEX_FORMAT GL_FLOAT
208 #define GL_ARRAY_BUFFER_ARB 0x8892
209 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
210 #define GL_STATIC_DRAW_ARB 0x88E4
211 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
212 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
214 #define GL_MULTISAMPLE_ARB 0x809D
216 #if defined(__WIN32__)
218 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
219 #define WGL_SAMPLES_ARB 0x2042
221 #define WGL_WGLEXT_VERSION 1
222 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
223 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
224 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
225 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
226 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
227 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
228 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
229 #define WGL_ACCELERATION_ARB 0x2003
230 #define WGL_NEED_PALETTE_ARB 0x2004
231 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
232 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
233 #define WGL_SWAP_METHOD_ARB 0x2007
234 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
235 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
236 #define WGL_TRANSPARENT_ARB 0x200A
237 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
238 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
239 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
240 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
241 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
242 #define WGL_SHARE_DEPTH_ARB 0x200C
243 #define WGL_SHARE_STENCIL_ARB 0x200D
244 #define WGL_SHARE_ACCUM_ARB 0x200E
245 #define WGL_SUPPORT_GDI_ARB 0x200F
246 #define WGL_SUPPORT_OPENGL_ARB 0x2010
247 #define WGL_DOUBLE_BUFFER_ARB 0x2011
248 #define WGL_STEREO_ARB 0x2012
249 #define WGL_PIXEL_TYPE_ARB 0x2013
250 #define WGL_COLOR_BITS_ARB 0x2014
251 #define WGL_RED_BITS_ARB 0x2015
252 #define WGL_RED_SHIFT_ARB 0x2016
253 #define WGL_GREEN_BITS_ARB 0x2017
254 #define WGL_GREEN_SHIFT_ARB 0x2018
255 #define WGL_BLUE_BITS_ARB 0x2019
256 #define WGL_BLUE_SHIFT_ARB 0x201A
257 #define WGL_ALPHA_BITS_ARB 0x201B
258 #define WGL_ALPHA_SHIFT_ARB 0x201C
259 #define WGL_ACCUM_BITS_ARB 0x201D
260 #define WGL_ACCUM_RED_BITS_ARB 0x201E
261 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
262 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
263 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
264 #define WGL_DEPTH_BITS_ARB 0x2022
265 #define WGL_STENCIL_BITS_ARB 0x2023
266 #define WGL_AUX_BUFFERS_ARB 0x2024
267 #define WGL_NO_ACCELERATION_ARB 0x2025
268 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
269 #define WGL_FULL_ACCELERATION_ARB 0x2027
270 #define WGL_SWAP_EXCHANGE_ARB 0x2028
271 #define WGL_SWAP_COPY_ARB 0x2029
272 #define WGL_SWAP_UNDEFINED_ARB 0x202A
273 #define WGL_TYPE_RGBA_ARB 0x202B
274 #define WGL_TYPE_COLORINDEX_ARB 0x202C
275 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
276 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
277 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
278 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
279 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
280 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
281 #define WGL_PBUFFER_LARGEST_ARB 0x2033
282 #define WGL_PBUFFER_WIDTH_ARB 0x2034
283 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
284 #define WGL_PBUFFER_LOST_ARB 0x2036
285 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
286 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
287 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
288 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
289 #define WGL_ACCELERATION_EXT 0x2003
290 #define WGL_NEED_PALETTE_EXT 0x2004
291 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
292 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
293 #define WGL_SWAP_METHOD_EXT 0x2007
294 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
295 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
296 #define WGL_TRANSPARENT_EXT 0x200A
297 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
298 #define WGL_SHARE_DEPTH_EXT 0x200C
299 #define WGL_SHARE_STENCIL_EXT 0x200D
300 #define WGL_SHARE_ACCUM_EXT 0x200E
301 #define WGL_SUPPORT_GDI_EXT 0x200F
302 #define WGL_SUPPORT_OPENGL_EXT 0x2010
303 #define WGL_DOUBLE_BUFFER_EXT 0x2011
304 #define WGL_STEREO_EXT 0x2012
305 #define WGL_PIXEL_TYPE_EXT 0x2013
306 #define WGL_COLOR_BITS_EXT 0x2014
307 #define WGL_RED_BITS_EXT 0x2015
308 #define WGL_RED_SHIFT_EXT 0x2016
309 #define WGL_GREEN_BITS_EXT 0x2017
310 #define WGL_GREEN_SHIFT_EXT 0x2018
311 #define WGL_BLUE_BITS_EXT 0x2019
312 #define WGL_BLUE_SHIFT_EXT 0x201A
313 #define WGL_ALPHA_BITS_EXT 0x201B
314 #define WGL_ALPHA_SHIFT_EXT 0x201C
315 #define WGL_ACCUM_BITS_EXT 0x201D
316 #define WGL_ACCUM_RED_BITS_EXT 0x201E
317 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
318 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
319 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
320 #define WGL_DEPTH_BITS_EXT 0x2022
321 #define WGL_STENCIL_BITS_EXT 0x2023
322 #define WGL_AUX_BUFFERS_EXT 0x2024
323 #define WGL_NO_ACCELERATION_EXT 0x2025
324 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
325 #define WGL_FULL_ACCELERATION_EXT 0x2027
326 #define WGL_SWAP_EXCHANGE_EXT 0x2028
327 #define WGL_SWAP_COPY_EXT 0x2029
328 #define WGL_SWAP_UNDEFINED_EXT 0x202A
329 #define WGL_TYPE_RGBA_EXT 0x202B
330 #define WGL_TYPE_COLORINDEX_EXT 0x202C
331 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
332 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
333 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
334 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
335 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
336 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
337 #define WGL_PBUFFER_LARGEST_EXT 0x2033
338 #define WGL_PBUFFER_WIDTH_EXT 0x2034
339 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
340 #define WGL_DEPTH_FLOAT_EXT 0x2040
341 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
342 #define WGL_SAMPLES_3DFX 0x2061
343 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
344 #define WGL_SAMPLES_EXT 0x2042
345 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
346 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
347 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
348 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
349 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
350 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
351 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
352 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
353 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
354 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
355 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
356 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
357 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
358 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
359 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
360 #define WGL_ARB_buffer_region 1
361 #define WGL_ARB_extensions_string 1
362 #define WGL_ARB_pixel_format 1
363 #define WGL_ARB_make_current_read 1
364 #define WGL_ARB_pbuffer 1
365 #define WGL_EXT_display_color_table 1
366 #define WGL_EXT_extensions_string 1
367 #define WGL_EXT_make_current_read 1
368 #define WGL_EXT_pbuffer 1
369 #define WGL_EXT_pixel_format 1
370 #define WGL_EXT_swap_control 1
371 #define WGL_WGL_EXT_depth_float 1
372 #define WGL_WGL_3DFX_multisample 1
373 #define WGL_WGL_EXT_multisample 1
374 #define WGL_NV_allocate_memory 1
377 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
378 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
379 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
380 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
381 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
385 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
386 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
387 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
388 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
390 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
391 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
392 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
393 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
394 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
395 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
396 typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
397 typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
398 typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
400 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
401 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
402 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
403 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
404 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
405 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
406 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
407 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
409 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
410 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
411 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
412 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
413 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
414 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
415 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
416 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
417 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
418 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
419 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
420 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
421 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
423 #define glBufferData glBufferDataARB
425 #ifdef WGL_WGLEXT_PROTOTYPES
426 extern BOOL WINAPI wglSwapIntervalEXT (int);
427 extern int WINAPI wglGetSwapIntervalEXT (void);
428 #endif /* WGL_WGLEXT_PROTOTYPES */
429 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
430 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
432 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
434 #elif defined(__ANDROID__) || defined(__ODROID__)
436 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
437 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
438 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
440 #define GL_POLYGON_STIPPLE 0xFFFF
441 #define GL_LINE_STIPPLE 0xFFFF
442 #define GL_LINE 0xFFFF
443 #define GL_FILL 0xFFFF
444 #define GL_ALL_ATTRIB_BITS 0xFFFF
445 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
451 #define GL_QUAD_STRIP 0
452 //#define GL_DOUBLE 0
453 //#define GL_UNSIGNED_INT 0
456 //#define GL_LINE_STIPPLE 0
457 #define GL_BGRA_EXT 0
458 #define GL_UNPACK_ROW_LENGTH 0
459 #define GL_UNPACK_SKIP_PIXELS 0
460 #define GL_UNPACK_SKIP_ROWS 0
462 #define GL_PACK_ROW_LENGTH 0
463 #define GL_PACK_SKIP_ROWS 0
464 #define GL_PACK_SKIP_PIXELS 0
468 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
476 FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
477 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
480 void Transpose(Matrix source)
485 m[j][i] = source.m[i][j];
488 void Multiply(Matrix a, Matrix b)
490 // We need a full matrix multiplication for the Projection matrix
491 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];
492 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];
493 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];
494 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];
496 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];
497 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];
498 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];
499 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];
501 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];
502 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];
503 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];
504 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];
506 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];
507 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];
508 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];
509 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];
514 // Our own matrix stack
515 static Matrix matrixStack[3][32];
516 static int matrixIndex[3];
517 static int curStack = 0;
521 // OpenGL ES Porting Kit
522 #if defined(__ANDROID__) || defined(__ODROID__)
523 #define glBindFramebuffer glBindFramebufferOES
524 #define glBindRenderbuffer glBindRenderbufferOES
525 #define glFramebufferTexture2D glFramebufferTexture2DOES
526 #define glGenFramebuffers glGenFramebuffersOES
527 #define glGenRenderbuffers glGenRenderbuffersOES
528 #define glDeleteFramebuffers glDeleteFramebuffersOES
529 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
531 #define GL_INT 0x1404
532 #define GL_UNSIGNED_INT 0x1405
533 #define GL_DOUBLE 0x140A
537 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
539 #define glBufferDatai glesBufferDatai
540 #define glBufferDatad glesBufferDatad
541 #define glVertexPointeri glesVertexPointeri
542 #define glVertexPointerd glesVertexPointerd
544 #define glRecti glesRecti
545 #define glBegin glesBegin
546 #define glTexCoord2i glesTexCoord2i
547 #define glVertex2i glesVertex2i
548 #define glTexCoord2d glesTexCoord2d
549 #define glVertex2d glesVertex2d
550 #define glTexCoord2f glesTexCoord2f
551 #define glVertex2f glesVertex2f
552 #define glEnd glesEnd
553 #define glColor3f glesColor3f
554 #define glColor4ub glesColor4ub
555 #define glColor4fv glesColor4fv
556 #define glLineStipple glesLineStipple
557 #define glNormal3fv glesNormal3fv
558 #define glNormal3f glesNormal3f
559 #define glTexCoord2fv glesTexCoord2fv
560 #define glColorMaterial glesColorMaterial
562 #define glLoadMatrixd glesLoadMatrixd
563 #define glMultMatrixd glesMultMatrixd
564 #define glFrustum glesFrustum
565 #define glOrtho glesOrtho
566 #define glScaled glesScaled
567 #define glTranslated glesTranslated
568 #define glRotated glesRotated
569 #define glVertex3d glesVertex3d
570 #define glVertex3dv glesVertex3dv
571 #define glVertex3f glesVertex3f
572 #define glVertex3fv glesVertex3fv
573 #define glLightModeli glesLightModeli
577 #define glVertexPointerd(nc, s, p, nv) glVertexPointer(nc, GL_DOUBLE, s, p)
578 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_INT, start)
582 #if defined(__ANDROID__) || defined(__ODROID__)
583 static EGLDisplay eglDisplay;
584 static EGLSurface eglSurface;
585 static EGLContext eglContext;
586 static int eglWidth, eglHeight;
588 #if defined(__ANDROID__)
589 static bool egl_init_display(ANativeWindow* window)
591 static bool egl_init_display(uint window)
594 const EGLint attribs[] =
596 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
600 EGL_DEPTH_SIZE, 16, //24,
601 /*EGL_SAMPLE_BUFFERS, 1,
602 EGL_SAMPLES, 0, //2,*/
611 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
612 eglInitialize(display, 0, 0);
613 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
614 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
616 surface = eglCreateWindowSurface(display, config, window, null);
617 context = eglCreateContext(display, config, null, null);
619 if(!eglMakeCurrent(display, surface, surface, context))
622 eglQuerySurface(display, surface, EGL_WIDTH, &w);
623 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
625 eglDisplay = display;
626 eglContext = context;
627 eglSurface = surface;
631 glEnableClientState(GL_VERTEX_ARRAY);
633 // Initialize GL state.
634 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
635 glEnable(GL_CULL_FACE);
636 glShadeModel(GL_SMOOTH);
637 glDisable(GL_DEPTH_TEST);
639 glDisable(GL_CULL_FACE);
640 glDisable(GL_DEPTH_TEST);
642 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
645 matrixStack[0][0].Identity();
646 matrixStack[1][0].Identity();
647 matrixStack[2][0].Identity();
649 glesMatrixMode(GL_MODELVIEW);
650 glScaled(1.0, 1.0, -1.0);
651 glesMatrixMode(GL_PROJECTION);
652 glShadeModel(GL_FLAT);
654 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
655 glFogi(GL_FOG_MODE, GL_EXP);
656 glFogf(GL_FOG_DENSITY, 0);
657 glEnable(GL_NORMALIZE);
658 glDepthFunc(GL_LESS);
660 glDisable(GL_MULTISAMPLE_ARB);
664 glOrtho(0,w,h,0,0.0,1.0);
667 curElementBuffer = 0;
671 static void egl_term_display()
675 glDeleteTextures(1, &stippleTexture);
678 if(eglDisplay != EGL_NO_DISPLAY)
680 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
681 if(eglContext != EGL_NO_CONTEXT)
682 eglDestroyContext(eglDisplay, eglContext);
683 if(eglSurface != EGL_NO_SURFACE)
684 eglDestroySurface(eglDisplay, eglSurface);
685 eglTerminate(eglDisplay);
687 eglDisplay = EGL_NO_DISPLAY;
688 eglContext = EGL_NO_CONTEXT;
689 eglSurface = EGL_NO_SURFACE;
694 // OpenGL Immediate Mode Porting Kit
695 static int beginCount;
696 static int vertexCount;
697 static int normalCount;
698 static float *vertexPointer;
699 static float *normalPointer;
700 static GLenum beginMode = -1;
701 static uint beginBufferSize, normalBufferSize;
702 static int numVertexCoords = 2;
703 static bool vertexColorValues = false;
704 static int vertexStride = 4;
705 static int vertexOffset = 2;
707 public void glesRecti(int a, int b, int c, int d)
717 public void glesBegin(GLenum mode)
722 vertexColorValues = false;
729 normalBufferSize = beginBufferSize = 1024; // default number of vertices
730 vertexPointer = new float[beginBufferSize * vertexStride];
731 normalPointer = new float[normalBufferSize * 3];
735 public void glesTexCoord2f(float x, float y)
737 int count = vertexCount;
739 if(vertexCount + numVertexCoords > beginBufferSize)
741 beginBufferSize = beginBufferSize + beginBufferSize/2;
742 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
745 vertexPointer[count*vertexStride ] = x;
746 vertexPointer[count*vertexStride+1] = y;
749 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
751 vertexPointer[count*vertexStride ] = vertexPointer[(count-4)*vertexStride];
752 vertexPointer[count*vertexStride+1] = vertexPointer[(count-4)*vertexStride+1];
754 vertexPointer[count*vertexStride ] = vertexPointer[(count-3)*vertexStride];
755 vertexPointer[count*vertexStride+1] = vertexPointer[(count-3)*vertexStride+1];
759 public void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
760 public void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
761 public void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
763 public void glesVertex2f(float x, float y)
766 vertexStride = vertexOffset + numVertexCoords;
768 if(vertexCount + 4 > beginBufferSize)
770 beginBufferSize = beginBufferSize + beginBufferSize/2;
771 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
774 vertexPointer[vertexCount*vertexStride+vertexOffset] = x;
775 vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = y;
778 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
780 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset];
781 vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset + 1];
783 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset];
784 vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset + 1];
789 public void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
790 public void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
792 public void glesEnd(void)
794 int mode = beginMode;
795 if(mode == GL_QUADS) mode = GL_TRIANGLES;
796 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
798 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
799 noAB.use(texCoord, 2, GL_FLOAT, vertexStride * sizeof(float), vertexPointer);
800 if(vertexColorValues)
802 glEnableClientState(GL_COLOR_ARRAY);
803 noAB.use(color, 4, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + 2);
805 noAB.use(vertex, numVertexCoords, GL_FLOAT, (vertexStride)*sizeof(float),vertexPointer+vertexOffset);
806 if(normalCount && normalCount == vertexCount)
808 glEnableClientState(GL_NORMAL_ARRAY);
809 noAB.use(normal, 3, GL_FLOAT, 3*sizeof(float),normalPointer);
812 glDrawArrays(mode, 0, vertexCount);
814 glDisableClientState(GL_NORMAL_ARRAY);
815 if(vertexColorValues)
816 glDisableClientState(GL_COLOR_ARRAY);
817 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
819 vertexColorValues = false;
825 static float *floatVPBuffer = null;
826 static short *shortVPBuffer = null;
827 static unsigned int shortVPSize = 0, floatVPSize = 0;
830 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
831 static unsigned short *shortBDBuffer = null;
832 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
834 public void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
839 if(numVertices*numCoords > shortVPSize)
841 shortVPSize = numVertices*numCoords;
842 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
844 for(i = 0; i < numVertices*numCoords; i++)
845 shortVPBuffer[i] = (short)pointer[i];
846 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
849 glVertexPointer(numCoords, GL_SHORT, stride, 0);
852 public void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
857 if(numVertices*numCoords > floatVPSize)
859 floatVPSize = numVertices*numCoords;
860 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
862 for(i = 0; i < numVertices*numCoords; i++)
863 floatVPBuffer[i] = (float)pointer[i];
864 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
867 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
870 public void glesTexReuseIntVP(int numCoords)
872 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
875 public void glesTexReuseDoubleVP(int numCoords)
877 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
880 public void glesColor4f(float r, float g, float b, float a)
882 if(beginMode != (GLenum)-1)
884 int count = vertexCount;
886 vertexColorValues = true;
888 vertexStride = vertexOffset + numVertexCoords;
890 if(vertexCount + vertexStride > beginBufferSize)
892 beginBufferSize = beginBufferSize + beginBufferSize/2;
893 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
896 vertexPointer[count*vertexStride + 2] = r;
897 vertexPointer[count*vertexStride + 3] = g;
898 vertexPointer[count*vertexStride + 4] = b;
899 vertexPointer[count*vertexStride + 5] = a;
902 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
904 vertexPointer[count*vertexStride + 2] = vertexPointer[(count-4) * vertexStride + 2];
905 vertexPointer[count*vertexStride + 3] = vertexPointer[(count-4) * vertexStride + 3];
906 vertexPointer[count*vertexStride + 4] = vertexPointer[(count-4) * vertexStride + 4];
907 vertexPointer[count*vertexStride + 5] = vertexPointer[(count-4) * vertexStride + 5];
909 vertexPointer[count*vertexStride + 2] = vertexPointer[(count-3) * vertexStride + 2];
910 vertexPointer[count*vertexStride + 3] = vertexPointer[(count-3) * vertexStride + 3];
911 vertexPointer[count*vertexStride + 4] = vertexPointer[(count-3) * vertexStride + 4];
912 vertexPointer[count*vertexStride + 5] = vertexPointer[(count-3) * vertexStride + 5];
917 glColor4f(r, g, b, a);
920 public void glesColor3f( float r, float g, float b )
922 glesColor4f(r, g, b, 1.0f);
925 public void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
927 glesColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
930 public void glesColor4fv(float * a)
932 glesColor4f(a[0], a[1], a[2], a[3]);
935 public void glesBufferDatad(int target, int size, void * data, int usage)
937 int numElems = size/sizeof(double);
938 double * dblPtr = (double *)data;
940 if (numElems > floatVPSize)
942 floatVPSize = numElems;
943 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
945 for (i=0; i< numElems; i++)
946 floatVPBuffer[i] = (float)dblPtr[i];
948 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
951 public void glesBufferDatai(int target, int size, void * data, int usage)
953 int numElems = size/sizeof(unsigned int);
954 unsigned int * pointer = (unsigned int *)data;
956 if (numElems > shortBDSize)
958 shortBDSize = numElems;
959 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
961 for (i=0; i< numElems; i++)
962 shortBDBuffer[i] = (unsigned short)pointer[i];
964 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
967 // *** Our Custom Matrix Stack ***
969 static void LoadCurMatrix()
971 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
974 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
975 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
976 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
977 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
982 public void glesLoadIdentity()
984 matrixStack[curStack][matrixIndex[curStack]].Identity();
988 public void glesPushMatrix()
990 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
992 matrixIndex[curStack]++;
993 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
997 public void glesPopMatrix()
999 if(matrixIndex[curStack] > 0)
1001 matrixIndex[curStack]--;
1006 public void glesLoadMatrixd(double * i)
1008 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
1012 public void glesOrtho( double l, double r, double b, double t, double n, double f )
1016 (2 / (r - l)), 0, 0, 0,
1017 0, (2 / (t - b)), 0, 0,
1018 0, 0, (-2 / (f - n)), 0,
1019 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
1022 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1023 matrixStack[curStack][matrixIndex[curStack]] = res;
1027 public void glesFrustum( double l, double r, double b, double t, double n, double f )
1037 double A = ((r + l) / (r - l));
1038 double B = ((t + b) / (t - b));
1039 double C = (-(f + n) / (f - n));
1040 double D = (-2*f*n/(f-n));
1043 (2.0*n / (r - l)), 0, 0, 0,
1044 0, (2.0*n / (t - b)), 0, 0,
1049 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1050 matrixStack[curStack][matrixIndex[curStack]] = res;
1055 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1056 public void glesRotated( double a, double b, double c, double d )
1061 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
1062 m.RotationQuaternion(q);
1063 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1064 matrixStack[curStack][matrixIndex[curStack]] = r;
1067 public void glesScaled( double a, double b, double c )
1073 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1074 matrixStack[curStack][matrixIndex[curStack]] = r;
1078 public void glesTranslated( double a, double b, double c )
1084 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1085 matrixStack[curStack][matrixIndex[curStack]] = r;
1089 public void glesMultMatrixd( double * i )
1092 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
1093 matrixStack[curStack][matrixIndex[curStack]] = r;
1098 public void glesMatrixMode(int mode)
1100 curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
1106 #define glPushMatrix glesPushMatrix
1107 #define glPopMatrix glesPopMatrix
1108 #define glLoadIdentity glesLoadIdentity
1109 #define glMatrixMode glesMatrixMode
1113 /* Using the built-in matrix stack
1114 void glesLoadMatrixd( double * i )
1118 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
1119 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
1120 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
1121 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
1126 void glesOrtho( double l, double r, double b, double t, double n, double f )
1128 float matrix[4][4] =
1130 { (float)(2 / (r - l)), 0, 0, 0 },
1131 { 0, (float)(2 / (t - b)), 0, 0 },
1132 { 0, 0, (float)(-2 / (f - n)), 0 },
1133 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
1135 glMultMatrixf((float *)matrix);
1138 void glesFrustum( double l, double r, double b, double t, double n, double f )
1140 float A = (float)((r + l) / (r - l));
1141 float B = (float)((t + b) / (t - b));
1142 float C = (float)(-(f + n) / (f - n));
1143 float D = (float)(-2*f*n/(f-n));
1144 float matrix[4][4] =
1146 { (float)(2*n / (r - l)), 0, 0, 0 },
1147 { 0, (float)(2*n / (t - b)), 0, 0 },
1151 glMultMatrixf((float *)matrix);
1154 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
1155 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
1156 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
1158 void glesMultMatrixd( double * i )
1162 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
1163 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
1164 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
1165 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
1171 // Need to do these...
1172 public void glesVertex3f( float x, float y, float z )
1174 numVertexCoords = 3;
1175 vertexStride = vertexOffset + numVertexCoords;
1177 if(vertexCount + vertexStride > beginBufferSize)
1179 beginBufferSize = beginBufferSize + beginBufferSize/2;
1180 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
1183 vertexPointer[vertexCount*vertexStride+vertexOffset] = x;
1184 vertexPointer[vertexCount*vertexStride+vertexOffset+1] = y;
1185 vertexPointer[vertexCount*vertexStride+vertexOffset+2] = z;
1188 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1190 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset];
1191 vertexPointer[vertexCount*vertexStride+vertexOffset+1] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset+1];
1192 vertexPointer[vertexCount*vertexStride+vertexOffset+2] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset+2];
1194 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset];
1195 vertexPointer[vertexCount*vertexStride+vertexOffset+1] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset+1];
1196 vertexPointer[vertexCount*vertexStride+vertexOffset+2] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset+2];
1202 public void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
1203 public void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
1204 public void glesVertex3dv( double* coords ) { glesVertex3f((float)coords[0], (float)coords[1], (float)coords[2]); }
1206 public void glesNormal3f(float x, float y, float z)
1208 normalCount = vertexCount;
1209 if(vertexCount + 4 > normalBufferSize)
1211 normalBufferSize = normalBufferSize + normalBufferSize/2;
1212 normalPointer = renew normalPointer float[normalBufferSize * 2];
1215 normalPointer[normalCount*3+0] = x;
1216 normalPointer[normalCount*3+1] = y;
1217 normalPointer[normalCount*3+2] = z;
1220 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1222 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1223 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1224 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1226 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1227 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1228 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1232 public void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1233 public void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1235 public void glesColorMaterial(int a, int b)
1237 PrintLn("glColorMaterial stub");
1240 public void glesTerminate()
1242 delete vertexPointer;
1243 delete normalPointer;
1244 beginBufferSize = 0;
1246 delete floatVPBuffer;
1249 delete shortVPBuffer;
1252 delete shortBDBuffer;
1256 static GLuint stippleTexture;
1257 #if defined(_GLES) || defined(EM_MODE)
1258 static bool stippleEnabled;
1261 public void glesLineStipple( int i, unsigned short j )
1265 for(x = 0; x < 16; x++)
1267 bool v = (j & (1 << x)) != 0;
1268 texture[x] = v ? 0xFFFFFFFF : 0;
1271 glGenTextures(1, &stippleTexture);
1272 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1273 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1274 glEnable(GL_TEXTURE_2D);
1275 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1276 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1279 glMatrixMode(GL_TEXTURE);
1281 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1282 glScaled(i/16.0, 1, 1.0f);
1283 glTranslated(0.5, 0.5, 0);
1284 glMatrixMode(GL_PROJECTION);
1287 public void glesLightModeli( unsigned int pname, int param )
1289 #if !defined(EM_MODE)
1290 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1291 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1295 #if defined(__ANDROID__) || defined(__ODROID__)
1296 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1297 void glFogi( unsigned int pname, int param ) { }
1298 void glPolygonMode( unsigned int i, unsigned int j ) { }
1301 // *** Picking won't be supported for now ***
1302 void glPushName( unsigned int i ) { }
1303 void glLoadName( unsigned int i ) { }
1304 void glPopName() { }
1306 // Probably replace by regular glBlendFunc ...
1307 void glBlendFuncSeparate(int a, int b, int c, int d)
1312 // For direct pixel blitting...
1313 void glRasterPos2d(double a, double b) { }
1314 void glPixelZoom(float a, float b) { }
1315 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1319 #if !defined(__APPLE__) && !defined(__WIN32__) && !defined(__ODROID__)
1320 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1321 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1322 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1323 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1326 public void GLLoadMatrix(Matrix matrix)
1330 (float)matrix.m[0][0], (float)matrix.m[0][1], (float)matrix.m[0][2], (float)matrix.m[0][3],
1331 (float)matrix.m[1][0], (float)matrix.m[1][1], (float)matrix.m[1][2], (float)matrix.m[1][3],
1332 (float)matrix.m[2][0], (float)matrix.m[2][1], (float)matrix.m[2][2], (float)matrix.m[2][3],
1333 (float)matrix.m[3][0], (float)matrix.m[3][1], (float)matrix.m[3][2], (float)matrix.m[3][3]
1338 public enum GLBufferContents { vertex, normal, texCoord, color };
1340 public define noAB = GLAB { 0 };
1342 static uint curArrayBuffer;
1348 void upload(uint size, void * data)
1353 GLGenBuffers(1, this);
1354 if(curArrayBuffer != buffer)
1355 GLBindBuffer(GL_ARRAY_BUFFER, buffer);
1356 glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); //GL_DYNAMIC_DRAW);
1364 GLDeleteBuffers(1, this);
1369 void use(GLBufferContents contents, int n, int type, uint stride, void * pointer)
1371 if(curArrayBuffer != ((this != null) ? buffer : 0))
1372 GLBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
1375 case normal: glNormalPointer(type, stride, pointer); break;
1376 case vertex: glVertexPointer(n, type, stride, pointer); break;
1377 case texCoord: glTexCoordPointer(n, type, stride, pointer); break;
1378 case color: glColorPointer(n, type, stride, pointer); break;
1382 void useVertTrans(uint count, int n, int type, uint stride, void * pointer)
1385 if(curArrayBuffer != ((this != null) ? buffer : 0))
1386 GLBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
1388 glVertexPointeri(n, stride, pointer, count);
1389 else if(type == GL_DOUBLE)
1390 glVertexPointerd(n, stride, pointer, count);
1392 use(vertex, n, type, stride, pointer);
1397 static uint curElementBuffer;
1399 public define noEAB = GLEAB { 0 };
1405 void upload(uint size, void * data)
1410 GLGenBuffers(1, (GLAB *)this);
1412 if(curElementBuffer != buffer)
1413 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
1414 glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); //GL_DYNAMIC_DRAW);
1422 GLDeleteBuffers(1, (GLAB *)this);
1427 void draw(int primType, int count, int type, void * indices)
1429 if(curElementBuffer != ((this != null) ? buffer : 0))
1430 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
1431 glDrawElements(primType, count, type, indices);
1435 public void GLGenBuffers(int count, GLAB * buffers)
1437 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1438 glGenBuffers(count, (GLuint *)buffers);
1440 #if defined(__WIN32__)
1443 glGenBuffersARB(count, (GLuint *)buffers);
1447 public void GLDeleteBuffers(int count, GLAB * buffers)
1450 for(i = 0; i < count; i++)
1452 uint buffer = buffers[i].buffer;
1453 if(buffer == curArrayBuffer)
1454 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
1455 else if(buffer == curElementBuffer)
1456 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1458 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1459 glDeleteBuffers(count, (GLuint *)buffers);
1461 #if defined(__WIN32__)
1462 if(glDeleteBuffersARB)
1464 glDeleteBuffersARB(count, (GLuint *)buffers);
1468 void GLBindBuffer(int target, uint buffer)
1470 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1471 glBindBuffer(target, buffer);
1473 #if defined(__WIN32__)
1476 glBindBufferARB(target, buffer);
1478 if(target == GL_ARRAY_BUFFER_ARB)
1479 curArrayBuffer = buffer;
1480 else if(target == GL_ELEMENT_ARRAY_BUFFER_ARB)
1481 curElementBuffer = buffer;
1484 public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
1487 if(glType == GL_DOUBLE)
1488 glesVertexPointerd(numCoords, stride, ptr, numVertices);
1489 else if(glType == GL_INT)
1490 glesVertexPointeri(numCoords, stride, ptr, numVertices);
1493 glVertexPointer(numCoords, glType, stride, ptr);
1496 public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1499 if(type == GL_DOUBLE)
1500 glesBufferDatad(target, size, (void *)data, usage);
1501 else if(type == GL_UNSIGNED_INT)
1502 glesBufferDatai(target, size, (void *)data, usage);
1506 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1507 glBufferData(target, size, data, usage);
1510 #if defined(__WIN32__)
1513 glBufferDataARB(target, size, data, usage);
1517 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1518 static int primitiveTypes[RenderPrimitiveType] =
1520 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1525 // Non OpenGL ES friendly stuff
1529 //#undef GL_UNSIGNED_INT
1534 #undef GL_QUAD_STRIP
1535 #undef GL_POLYGON_STIPPLE
1536 #undef GL_LINE_STIPPLE
1539 #undef GL_ALL_ATTRIB_BITS
1540 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1544 static int displayWidth, displayHeight;
1546 #define GL_CLAMP_TO_EDGE 0x812F
1548 static bool vboAvailable;
1550 static bool useSingleGLContext = false;
1551 class OGLDisplay : struct
1553 #if defined(__WIN32__)
1563 int imageBuffers[2];
1564 byte * pboMemory1, * pboMemory2;
1566 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
1567 GLXContext glContext;
1570 XShmSegmentInfo shminfo;
1572 XShmSegmentInfo shminfoShape;
1573 XImage * shapeImage;
1577 X11Picture windowPicture;
1578 X11Picture pixmapPicture;
1580 X11Picture shapePicture;
1583 ColorAlpha * flippingBuffer;
1584 int flipBufH, flipBufW;
1589 class OGLSystem : struct
1594 #if defined(__WIN32__)
1595 PIXELFORMATDESCRIPTOR pfd;
1600 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
1601 XVisualInfo * visualInfo;
1602 GLXContext glContext;
1603 GLXDrawable glxDrawable;
1607 class OGLSurface : struct
1613 bool writingOutline;
1615 float foreground[4], background[4], bitmapMult[4];
1618 class OGLMesh : struct
1627 class OGLIndices : struct
1637 class OpenGLDisplayDriver : DisplayDriver
1639 class_property(name) = "OpenGL";
1641 bool LockSystem(DisplaySystem displaySystem)
1643 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
1644 OGLSystem oglSystem = displaySystem.driverData;
1645 if(useSingleGLContext) return true;
1646 #if defined(__WIN32__)
1647 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1648 #elif defined(__unix__) || defined(__APPLE__)
1649 //if(previous) return true;
1650 // printf("Making SYSTEM current\n");
1651 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1652 //previous = oglSystem.glContext;
1658 void UnlockSystem(DisplaySystem displaySystem)
1660 if(useSingleGLContext) return;
1661 #if defined(__WIN32__)
1662 wglMakeCurrent(null, null);
1663 #elif defined(__unix__) || defined(__APPLE__)
1664 // printf("Making NULL current\n");
1665 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1667 glXMakeCurrent(xGlobalDisplay, None, null);
1673 bool Lock(Display display)
1675 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
1676 OGLDisplay oglDisplay = display.driverData;
1677 if(useSingleGLContext) return true;
1678 #if defined(__WIN32__)
1679 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1680 #elif defined(__unix__) || defined(__APPLE__)
1681 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1682 // printf(" Making DISPLAY current\n");
1683 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1689 void Unlock(Display display)
1691 if(useSingleGLContext) return;
1692 //printf(" Making NULL current\n");
1693 //glXMakeCurrent(xGlobalDisplay, None, null);
1695 LockSystem(display.displaySystem);
1698 void DestroyDisplay(Display display)
1700 OGLDisplay oglDisplay = display.driverData;
1704 #if defined(__WIN32__)
1705 wglMakeCurrent( null, null );
1708 wglDeleteContext(oglDisplay.glrc);
1710 if(oglDisplay.hdc && oglDisplay.pBuffer)
1711 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1713 if(oglDisplay.pBuffer)
1714 wglDestroyPbufferARB(oglDisplay.pBuffer);
1717 ReleaseDC(display.window, oglDisplay.hdc);
1719 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1720 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1722 #elif defined(__unix__) || defined(__APPLE__)
1723 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1725 if(oglDisplay.shapePixmap)
1726 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1727 if(oglDisplay.pixmap)
1728 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1729 if(oglDisplay.image)
1731 if(oglDisplay.shminfoShape.shmid != -1)
1733 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1734 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1735 shmdt(oglDisplay.shminfo.shmaddr);
1736 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1739 if(oglDisplay.shapeImage)
1741 if(oglDisplay.shminfoShape.shmid != -1)
1743 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1744 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1745 shmdt(oglDisplay.shminfoShape.shmaddr);
1746 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1748 XDestroyImage(oglDisplay.shapeImage);
1749 oglDisplay.shapeImage = None;
1752 glXMakeCurrent(xGlobalDisplay, None, null);
1754 if(oglDisplay.glContext)
1755 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1758 delete oglDisplay.flippingBuffer;
1760 display.driverData = null;
1764 void ::CheckExtensions(OGLSystem oglSystem)
1766 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1768 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1769 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1772 bool CreateDisplaySystem(DisplaySystem displaySystem)
1774 bool result = false;
1775 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1778 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1780 oglSystem.hdc = GetDC(oglSystem.hwnd);
1784 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1785 oglSystem.pfd.nVersion = 1;
1786 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1787 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1788 oglSystem.pfd.cColorBits = 24;
1789 oglSystem.pfd.cAlphaBits = 8;
1790 oglSystem.pfd.cDepthBits = 24;
1791 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1793 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1794 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1796 if(oglSystem.pfd.cColorBits > 8)
1798 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1799 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1802 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1804 // Get Pointers To The GL Functions
1805 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1806 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1807 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1808 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1809 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1810 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1811 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1812 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1813 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1814 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1815 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1816 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1818 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1819 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1820 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1821 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1822 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1823 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1824 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1825 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1826 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1828 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1830 vboAvailable = glBindBufferARB != null;
1832 // eSystem_LoggingMode(LOG_MSGBOX, null);
1834 if(wglChoosePixelFormatARB)
1839 float fAttributes[] = {0,0};
1842 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1843 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1844 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1845 WGL_COLOR_BITS_ARB,24,
1846 WGL_ALPHA_BITS_ARB,8,
1847 WGL_DEPTH_BITS_ARB,16,
1848 WGL_STENCIL_BITS_ARB,0,
1849 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1850 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1851 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1855 //Log("Found wglChoosePixelFormatARB\n");
1857 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1858 if(!valid || !numFormats)
1860 //Log("Can't find 4x multi sampling\n");
1861 iAttributes[19] = 2;
1862 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1863 if(!valid || !numFormats)
1865 // Log("Can't find 2x multi sampling\n");
1866 iAttributes[16] = 0;
1867 iAttributes[17] = 0;
1868 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1871 if(valid && numFormats)
1873 oglSystem.format = pixelFormat;
1874 wglMakeCurrent(null, null);
1875 wglDeleteContext(oglSystem.glrc);
1877 // *** DescribePixelFormat does not support WGL pixel formats! ***
1878 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1879 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1880 //Log("Successfully set pixel format\n");
1882 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1883 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1887 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1891 CheckExtensions(oglSystem);
1893 wglMakeCurrent(null, null);
1895 //eSystem_DumpErrors(true);
1899 #elif defined(__unix__) || defined(__APPLE__)
1900 vboAvailable = true;
1901 #if defined(__ANDROID__)
1902 egl_init_display(guiApp.desktop.windowHandle);
1903 #elif defined(__ODROID__)
1904 egl_init_display((uint)displaySystem.window);
1905 CheckExtensions(oglSystem);
1907 #elif defined(__EMSCRIPTEN__)
1908 if(glfwInit() == GL_TRUE)
1910 const int width = 640, height = 480;
1911 if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
1913 //glfwSwapBuffers();
1917 printf("glfwOpenWindow() failed\n"); //glfwTerminate();
1920 printf("glfwInit() failed\n"); //glfwTerminate();
1923 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1924 XSetWindowAttributes attr;
1929 #ifndef ECERE_MINIGLX
1930 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1933 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1937 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1938 attr.background_pixel = 0;
1939 attr.border_pixel = 0;
1940 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1941 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1942 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1944 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1945 oglSystem.visualInfo->visual, mask, &attr );
1947 if(oglSystem.visualInfo)
1949 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1950 if(oglSystem.glContext)
1952 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1954 CheckExtensions(oglSystem);
1955 glXMakeCurrent(xGlobalDisplay, None, null);
1962 displaySystem.flags.alpha = true;
1963 displaySystem.flags.flipping = true;
1964 displaySystem.pixelFormat = pixelFormat888;
1968 void DestroyDisplaySystem(DisplaySystem displaySystem)
1970 OGLSystem oglSystem = displaySystem.driverData;
1972 #if defined(__WIN32__)
1973 wglMakeCurrent( null, null );
1976 wglDeleteContext(oglSystem.glrc);
1979 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1980 DestroyWindow(oglSystem.hwnd);
1982 #elif defined(__unix__) || defined(__APPLE__)
1983 #if defined(__ANDROID__) || defined(__ODROID__)
1985 #elif defined(__EMSCRIPTEN__)
1988 if(oglSystem.visualInfo)
1990 #ifdef ECERE_MINIGLX
1991 __miniglx_XFree(oglSystem.visualInfo);
1993 XFree(oglSystem.visualInfo);
1997 if(oglSystem.glxDrawable)
1999 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
2000 oglSystem.glxDrawable = 0;
2007 bool CreateDisplay(Display display)
2009 bool result = false;
2010 OGLDisplay oglDisplay = display.driverData;
2011 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
2012 OGLSystem oglSystem = display.displaySystem.driverData;
2015 oglDisplay = display.driverData = OGLDisplay { };
2016 //printf("Inside CreateDisplay\n");
2018 #if defined(__WIN32__) || defined(USEPBUFFER)
2019 if(!display.alphaBlend)
2022 #if defined(__WIN32__)
2023 oglDisplay.hdc = GetDC(display.window);
2024 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
2025 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
2027 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2028 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2032 ReleaseDC(display.window, oglDisplay.hdc);
2033 #elif defined(__unix__) || defined(__APPLE__)
2034 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
2036 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
2038 #if defined(__APPLE__)
2039 XVisualInfo template = { 0 };
2040 XWindowAttributes winAttr;
2042 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
2043 template.visualid = XVisualIDFromVisual(winAttr.visual);
2044 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
2046 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
2047 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
2048 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
2049 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
2051 // visualInfo = oglSystem.visualInfo;
2056 //printf("visualInfo is not null\n");
2057 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
2058 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
2059 //XFree(visualInfo);
2062 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
2063 if(oglDisplay.glContext)
2065 //printf("CreateDisplay Got a Context\n");
2066 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2072 #if defined(__WIN32__) || defined(USEPBUFFER)
2078 #if defined(__WIN32__)
2079 if(glBlendFuncSeparate)
2080 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2082 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2084 #if !defined(__OLDX__)
2085 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2087 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2092 glMatrixMode(GL_MODELVIEW);
2093 glScaled(1.0, 1.0, -1.0);
2094 // glTranslatef(0.375f, 0.375f, 0.0f);
2095 // glTranslatef(-0.625f, -0.625f, 0.0f);
2096 glMatrixMode(GL_PROJECTION);
2097 glShadeModel(GL_FLAT);
2099 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
2100 #if !defined(EM_MODE)
2101 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2103 glFogi(GL_FOG_MODE, GL_EXP);
2104 glFogf(GL_FOG_DENSITY, 0);
2105 glEnable(GL_NORMALIZE);
2106 glDepthFunc(GL_LESS);
2108 glDisable(GL_MULTISAMPLE_ARB);
2110 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
2111 display.ambient = Color { 50,50,50 };
2114 if(!useSingleGLContext)
2116 #if defined(__WIN32__)
2117 wglMakeCurrent(null, null);
2118 #elif defined(__unix__) || defined(__APPLE__)
2119 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
2122 glXMakeCurrent(xGlobalDisplay, None, null);
2128 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
2136 bool DisplaySize(Display display, int width, int height)
2138 OGLDisplay oglDisplay = display.driverData;
2140 bool result = false;
2142 //printf("Inside DisplaySize\n");
2143 #if defined(__WIN32__) || defined(USEPBUFFER)
2144 OGLSystem oglSystem = display.displaySystem.driverData;
2145 if(display.alphaBlend)
2147 #if defined(__WIN32__)
2148 const int attributes[]=
2150 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
2151 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
2153 int pixelFormat = 0;
2154 if(wglChoosePixelFormatARB)
2158 float fAttributes[] = {0,0};
2161 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
2162 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
2163 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
2164 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
2165 WGL_COLOR_BITS_ARB,24,
2166 WGL_ALPHA_BITS_ARB,8,
2167 WGL_DEPTH_BITS_ARB,16,
2168 WGL_STENCIL_BITS_ARB,0,
2169 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
2170 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
2171 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
2175 //Log("Found wglChoosePixelFormatARB\n");
2177 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2178 if(!valid || !numFormats)
2180 //Log("Can't find 4x multi sampling\n");
2181 iAttributes[19] = 2;
2182 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2183 if(!valid || !numFormats)
2185 // Log("Can't find 2x multi sampling\n");
2186 iAttributes[16] = 0;
2187 iAttributes[17] = 0;
2188 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2189 if(!valid || !numFormats)
2193 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
2194 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
2195 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
2196 WGL_COLOR_BITS_ARB,24,
2197 WGL_ALPHA_BITS_ARB,8,
2198 WGL_DEPTH_BITS_ARB,16,
2201 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2205 if(valid && numFormats)
2207 wglMakeCurrent(null, null);
2211 wglMakeCurrent( null, null );
2212 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
2213 if(oglDisplay.hdc && oglDisplay.pBuffer)
2214 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
2216 wglDestroyPbufferARB(oglDisplay.pBuffer);
2218 if(!useSingleGLContext)
2219 wglMakeCurrent( null, null );
2222 wglDeleteContext(oglDisplay.glrc);
2224 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
2225 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
2226 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
2229 HDC hdc = GetDC(display.window);
2231 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2232 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2234 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
2235 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
2237 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
2239 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
2243 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
2244 oglDisplay.memDC = CreateCompatibleDC(hdc);
2245 SetMapMode(oglDisplay.memDC, MM_TEXT);
2246 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2247 info->bmiHeader.biPlanes = 1;
2248 info->bmiHeader.biCompression = BI_RGB;
2249 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
2250 info->bmiHeader.biWidth = width;
2251 info->bmiHeader.biHeight = height;
2252 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
2255 SelectObject(oglDisplay.memDC, newBitmap);
2256 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
2259 PIXELFORMATDESCRIPTOR pfd = { 0 };
2260 pfd.nSize = (short)sizeof(pfd);
2262 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
2263 pfd.iPixelType = PFD_TYPE_RGBA;
2264 pfd.cColorBits = 32;
2265 //pfd.cAlphaBits = 8;
2266 pfd.cDepthBits = 24;
2267 pfd.iLayerType = PFD_MAIN_PLANE;
2269 oglDisplay.hdc = oglDisplay.memDC;
2271 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
2272 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
2273 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
2275 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
2276 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2277 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2282 const int imageSize = width * height * 4;
2284 glGenBuffersARB(2, oglDisplay.imageBuffers);
2286 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2287 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
2288 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2289 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
2292 oglDisplay.memBitmap = newBitmap;
2293 oglDisplay.stride = width;
2299 ReleaseDC(display.window, hdc);
2301 #elif defined(__unix__) || defined(__APPLE__)
2302 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
2307 GLX_DOUBLEBUFFER, True,
2313 GLX_STENCIL_SIZE, 1,
2314 //GLX_DEPTH_SIZE, 24,
2315 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2316 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
2322 GLX_PBUFFER_WIDTH, width,
2323 GLX_PBUFFER_HEIGHT, height,
2324 GLX_LARGEST_PBUFFER, False,
2328 // choose a pixel format that meets our minimum requirements
2331 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
2334 if(oglDisplay.pixmap)
2336 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2337 oglDisplay.pixmap = None;
2339 if(oglDisplay.shapePixmap)
2341 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
2342 oglDisplay.shapePixmap = None;
2345 // Free Shared Memory Pixmap
2346 if(oglDisplay.image)
2348 if(oglDisplay.shminfoShape.shmid != -1)
2350 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
2351 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2352 shmdt(oglDisplay.shminfo.shmaddr);
2353 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
2355 XDestroyImage(oglDisplay.image);
2356 oglDisplay.image = None;
2358 if(oglDisplay.shapeImage)
2360 if(oglDisplay.shminfoShape.shmid != -1)
2362 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
2363 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2364 shmdt(oglDisplay.shminfoShape.shmaddr);
2365 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
2367 XDestroyImage(oglDisplay.shapeImage);
2368 oglDisplay.shapeImage = None;
2371 if(oglDisplay.windowPicture)
2372 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
2373 if(oglDisplay.pixmapPicture)
2374 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
2376 if(oglDisplay.pixmap)
2377 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2379 if(oglDisplay.glContext)
2380 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
2381 if(oglDisplay.pBuffer)
2382 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
2384 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
2385 if(oglDisplay.pBuffer)
2387 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2388 if(oglDisplay.glContext)
2390 glXMakeCurrent(xGlobalDisplay, None, null);
2391 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2393 // Initialize Shared Memory Pixmap
2394 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2395 ZPixmap, null, &oglDisplay.shminfo, width, height);
2396 if(oglDisplay.image)
2398 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2399 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2400 if(oglDisplay.shminfo.shmid != -1)
2402 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2403 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2405 oglDisplay.shminfo.readOnly = False;
2406 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2408 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2409 &oglDisplay.shminfo, width, height, 32);
2411 // Initialize Shared Memory Shape Pixmap
2412 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2413 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2414 if(oglDisplay.shapeImage)
2416 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2417 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2418 if(oglDisplay.shminfoShape.shmid != -1)
2420 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2421 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2423 oglDisplay.shminfoShape.readOnly = False;
2424 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2426 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2427 &oglDisplay.shminfoShape, width, height, 1);
2428 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2431 XRenderPictureAttributes attributes = { 0 };
2432 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2433 #if !defined(__APPLE__) && !defined(__OLDX__)
2434 attributes.repeat = RepeatNormal;
2436 attributes.repeat = 1;
2438 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2439 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2440 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2441 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2444 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2445 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2462 CreateDisplay(display);
2463 #if defined(__WIN32__)
2464 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2465 #elif defined(__unix__) || defined(__APPLE__)
2466 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
2470 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2477 if(!result && display.alphaBlend)
2479 printf("Alpha blending windows not supported on this display\n");
2486 glViewport(0,0,width,height);
2488 glOrtho(0,width,height,0,0.0,1.0);
2489 displayWidth = display.width = width;
2490 displayHeight = display.height = height;
2492 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2494 oglDisplay.flipBufW = width;
2495 oglDisplay.flipBufH = height;
2499 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2502 if(oglDisplay.flippingBuffer || !width || !height)
2508 void DisplayPosition(Display display, int x, int y)
2510 OGLDisplay oglDisplay = display.driverData;
2516 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2520 void RestorePalette(Display display)
2524 void StartUpdate(Display display)
2528 void EndUpdate(Display display)
2532 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2536 void Update(Display display, Box updateBox)
2538 #if defined(__WIN32__) || defined(USEPBUFFER)
2539 OGLDisplay oglDisplay = display.driverData;
2541 //Logf("DisplayScreen\n");
2545 #if defined(__WIN32__) || defined(USEPBUFFER)
2546 if(display.alphaBlend)
2548 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2549 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2550 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2551 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2552 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2555 #if defined(__WIN32__)
2557 POINT point = { oglDisplay.x, oglDisplay.y};
2558 POINT srcPoint = { 0, 0 };
2559 BLENDFUNCTION blend = { 0 };
2561 size.cx = display.width;
2562 size.cy = display.height;
2563 blend.BlendOp = AC_SRC_OVER;
2564 blend.BlendFlags = 0;
2565 blend.SourceConstantAlpha = 255;
2566 blend.AlphaFormat = AC_SRC_ALPHA;
2569 // Process partial images. Mapping the buffer waits for
2570 // outstanding DMA transfers into the buffer to finish.
2571 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2572 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2574 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2575 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2578 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2579 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2582 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2585 // Unmap the image buffers
2586 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2587 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2589 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2590 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2592 // Bind two different buffer objects and start the glReadPixels
2593 // asynchronously. Each call will return directly after
2594 // starting the DMA transfer.
2595 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2596 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2598 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2599 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2603 #elif defined(__unix__) || defined(__APPLE__)
2604 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
2606 XTransform transform =
2609 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2610 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2611 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2614 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2615 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2616 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2617 #if !defined(__APPLE__) && !defined(__OLDX__)
2618 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2620 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2622 XFlush(xGlobalDisplay);
2630 #if defined(__WIN32__)
2631 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2632 SwapBuffers(oglDisplay.hdc);
2633 #elif defined(__unix__) || defined(__APPLE__)
2634 #if defined(__ANDROID__) || defined(__ODROID__)
2635 eglSwapBuffers(eglDisplay, eglSurface);
2636 #elif defined(__EMSCRIPTEN__)
2639 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2643 //Logf("Out of DisplayScreen\n");
2646 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2648 if(bitmap.driverData)
2650 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2651 glDeleteTextures(1, &tex);
2652 bitmap.driverData = 0;
2654 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2657 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2659 OGLSystem oglSystem = displaySystem.driverData;
2660 bool result = false;
2662 GLuint glBitmap = 0;
2664 uint w = width, h = height;
2665 if(oglSystem.pow2textures)
2670 w = Min(w, oglSystem.maxTextureSize);
2671 h = Min(h, oglSystem.maxTextureSize);
2673 glGenTextures(1, &glBitmap);
2674 glBindTexture(GL_TEXTURE_2D, glBitmap);
2676 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2678 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2679 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2681 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2682 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2684 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2686 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2688 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2689 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2693 bitmap.driverData = (void *)(uintptr)glBitmap;
2694 bitmap.driver = displaySystem.driver;
2702 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2704 bool result = false;
2705 OGLSystem oglSystem = displaySystem.driverData;
2706 Bitmap convBitmap = bitmap;
2710 convBitmap.Copy(bitmap);
2713 // Pre process the bitmap... First make it 32 bit
2714 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
2717 uint w = bitmap.width, h = bitmap.height;
2718 GLuint glBitmap = 0;
2719 if(oglSystem.pow2textures)
2724 w = Min(w, oglSystem.maxTextureSize);
2725 h = Min(h, oglSystem.maxTextureSize);
2729 while(w * 2 < h) w *= 2;
2730 while(h * 2 < w) h *= 2;
2733 // Switch ARGB to RGBA
2734 //if(bitmap.format != pixelFormatRGBA)
2736 for(c=0; c<bitmap.size; c++)
2738 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2740 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
2741 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2744 // convBitmap.pixelFormat = pixelFormat888;
2747 glGenTextures(1, &glBitmap);
2750 //int error = glGetError();
2754 glBindTexture(GL_TEXTURE_2D, glBitmap);
2755 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2757 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2758 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2759 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2761 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2762 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2764 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2765 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2767 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2771 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2774 if(bitmap.width != w || bitmap.height != h)
2776 mipMap = Bitmap { };
2777 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
2779 Surface mipSurface = mipMap.GetSurface(0,0,null);
2780 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
2790 mipMap = convBitmap;
2797 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2798 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2799 //printf("Calling glTexImage2D\n");
2800 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2801 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2802 if((error = glGetError()))
2804 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2805 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2809 if(mipMap != convBitmap)
2814 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
2815 bitmap.driverData = (void *)(uintptr)glBitmap;
2816 bitmap.driver = displaySystem.driver;
2821 FreeBitmap(displaySystem, bitmap);
2822 else if(oglSystem.loadingFont)
2824 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2825 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2826 oglSystem.loadingFont = false;
2832 void ReleaseSurface(Display display, Surface surface)
2834 glDisable(GL_SCISSOR_TEST);
2835 delete surface.driverData;
2836 surface.driverData = null;
2839 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2844 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2846 bool result = false;
2847 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2849 //Logf("GetSurface\n");
2853 if(displayWidth != display.width || displayHeight != display.height)
2855 displayWidth = display.width;
2856 displayHeight = display.height;
2858 glViewport(0,0,display.width,display.height);
2860 glOrtho(0,display.width,display.height,0,0.0,1.0);
2863 surface.offset.x = x;
2864 surface.offset.y = y;
2865 surface.unclippedBox = surface.box = clip;
2866 oglSurface.bitmapMult[0] = 1;
2867 oglSurface.bitmapMult[1] = 1;
2868 oglSurface.bitmapMult[2] = 1;
2869 oglSurface.bitmapMult[3] = 1;
2871 glEnable(GL_SCISSOR_TEST);
2874 (display.height) -(y+clip.bottom)-1,
2875 clip.right-clip.left+1,
2876 clip.bottom-clip.top+1);
2882 void Clip(Display display, Surface surface, Box clip)
2891 box.Clip(surface.unclippedBox);
2895 box = surface.box = surface.unclippedBox;
2896 box.left += surface.offset.x;
2897 box.top += surface.offset.y;
2898 box.right+= surface.offset.x;
2899 box.bottom += surface.offset.y;
2902 box.left,display.height - box.bottom - 1,
2903 box.right-box.left+1, box.bottom-box.top+1);
2906 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2908 bool result = false;
2909 OGLDisplay oglDisplay = display.driverData;
2910 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2912 if(oglDisplay.flippingBuffer)
2914 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2917 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2923 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2924 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2925 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2926 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2927 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2930 for(row = 0; row<h; row++)
2931 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2938 void SetForeground(Display display, Surface surface, ColorAlpha color)
2940 OGLSurface oglSurface = surface.driverData;
2942 //Logf("SetForeground\n");
2944 oglSurface.foreground[0] = color.color.r/255.0f;
2945 oglSurface.foreground[1] = color.color.g/255.0f;
2946 oglSurface.foreground[2] = color.color.b/255.0f;
2947 //oglSurface.foreground[3] = 1.0f;
2948 oglSurface.foreground[3] = color.a/255.0f;
2950 //if(!oglSurface.foreground[3])printf("bug");
2953 void SetBackground(Display display, Surface surface, ColorAlpha color)
2955 OGLSurface oglSurface = surface.driverData;
2957 //Logf("SetBackground\n");
2959 oglSurface.background[0] = color.color.r/255.0f;
2960 oglSurface.background[1] = color.color.g/255.0f;
2961 oglSurface.background[2] = color.color.b/255.0f;
2962 //oglSurface.background[3] = 1.0;
2963 oglSurface.background[3] = color.a/255.0f;
2966 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2968 OGLSurface oglSurface = surface.driverData;
2970 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2971 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2972 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2973 oglSurface.bitmapMult[3] = color.a/255.0f;
2976 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2981 void PutPixel(Display display, Surface surface,int x,int y)
2983 OGLSurface oglSurface = surface.driverData;
2985 //Logf("PutPixel\n");
2987 glColor4fv(oglSurface.foreground);
2989 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2990 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2995 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2997 OGLSurface oglSurface = surface.driverData;
2998 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
3013 x1 += surface.offset.x;
3014 y1 += surface.offset.y;
3015 x2 += surface.offset.x;
3016 y2 += surface.offset.y;
3020 glColor4fv(oglSurface.foreground);
3022 #if defined(_GLES) || defined(EM_MODE)
3025 glTexCoord2f(0.5f, 0);
3026 glVertex2f(x1 + 0.5f, y1 + 0.5f);
3027 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
3028 glVertex2f(x2 + 0.5f, y2 + 0.5f);
3037 glVertex2f(x1 + 0.5f, y1 + 0.5f);
3038 glVertex2f(x2 + 0.5f, y2 + 0.5f);
3044 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
3046 OGLSurface oglSurface = surface.driverData;
3047 x1 += surface.offset.x;
3048 y1 += surface.offset.y;
3049 x2 += surface.offset.x;
3050 y2 += surface.offset.y;
3052 //Logf("Rectangle\n");
3054 glColor4fv(oglSurface.foreground);
3055 #if defined(_GLES) || defined(EM_MODE)
3060 glTexCoord2f(0.5f, 0);
3061 glVertex2f(x1 + 0.5f, y1 + 0.5f);
3062 glTexCoord2f(y2-y1 + 0.5f, 0);
3063 glVertex2f(x1 + 0.5f, y2 + 0.5f);
3065 glTexCoord2f(0.5f, 0);
3066 glVertex2f(x1 + 0.5f, y2 + 0.5f);
3067 glTexCoord2f(x2 - x1 + 0.5f, 0);
3068 glVertex2f(x2 + 0.5f, y2 + 0.5f);
3070 glTexCoord2f(0.5f, 0);
3071 glVertex2f(x2 + 0.5f, y2 + 0.5f);
3072 glTexCoord2f(y1 - y2 + 0.5f, 0);
3073 glVertex2f(x2 + 0.5f, y1 + 0.5f);
3075 glTexCoord2f(0.5f, 0);
3076 glVertex2f(x2 + 0.5f, y1 + 0.5f);
3077 glTexCoord2f(x1 - x2 + 0.5f, 0);
3078 glVertex2f(x1 + 0.5f, y1 + 0.5f);
3083 glBegin(GL_LINE_LOOP);
3090 glVertex2f(x1 + 0.5f, y1 + 0.5f);
3091 glVertex2f(x1 + 0.5f, y2 + 0.5f);
3092 glVertex2f(x2 + 0.5f, y2 + 0.5f);
3093 glVertex2f(x2 + 0.5f, y1 + 0.5f);
3098 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
3100 OGLSurface oglSurface = surface.driverData;
3103 glColor4fv(oglSurface.background);
3107 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
3108 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
3109 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
3110 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
3113 glRecti(x1+surface.offset.x, y1+surface.offset.y,
3114 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
3117 glRectf(x1+surface.offset.x, y1+surface.offset.y,
3118 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
3122 void Clear(Display display, Surface surface, ClearType type)
3124 OGLDisplay oglDisplay = display.driverData;
3125 OGLSurface oglSurface = surface.driverData;
3128 if(type != depthBuffer)
3129 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
3130 if(type != colorBuffer && !oglDisplay.depthWrite)
3132 glDepthMask((byte)bool::true);
3134 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
3135 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
3136 if(type != colorBuffer && !oglDisplay.depthWrite)
3138 glDepthMask((byte)bool::false);
3142 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
3147 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3149 OGLSurface oglSurface = surface.driverData;
3151 #if !defined(__OLDX__)
3152 // WHY DO WE HAVE GL_ONE HERE ?
3153 /*if(glBlendFuncSeparate && !oglSurface.writingText)
3154 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3157 if(!oglSurface.writingText)
3159 // glTranslatef(-0.375f, -0.375f, 0.0f);
3160 glEnable(GL_TEXTURE_2D);
3161 glColor4fv(oglSurface.bitmapMult);
3163 else if(oglSurface.xOffset)
3164 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
3166 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
3171 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
3172 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3173 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
3174 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3175 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
3176 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
3177 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
3178 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
3183 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
3184 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3185 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
3186 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3187 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
3188 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
3189 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
3190 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
3193 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
3194 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
3195 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
3196 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
3197 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
3198 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
3199 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
3200 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
3204 if(!oglSurface.writingText)
3206 glDisable(GL_TEXTURE_2D);
3208 //glTranslate(0.375, 0.375, 0.0);
3210 else if(oglSurface.xOffset)
3211 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
3213 #if !defined(__OLDX__)
3214 /*if(glBlendFuncSeparate && !oglSurface.writingText)
3215 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3219 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3221 OGLSurface oglSurface = surface.driverData;
3223 //glTranslate(-0.375, -0.375, 0.0);
3225 //Logf("Stretch\n");
3227 #if !defined(__OLDX__)
3228 /*if(glBlendFuncSeparate)
3229 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3232 glEnable(GL_TEXTURE_2D);
3233 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
3235 glColor4fv(oglSurface.bitmapMult);
3241 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3242 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3244 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3245 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3247 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3248 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
3250 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3251 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
3255 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3256 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3258 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3259 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3261 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3262 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
3264 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3265 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
3270 glDisable(GL_TEXTURE_2D);
3272 //glTranslate(0.375, 0.375, 0.0);
3273 #if !defined(__OLDX__)
3274 /*if(glBlendFuncSeparate)
3275 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3280 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3282 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3285 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3287 #if !defined(EM_MODE)
3288 float s2dw,s2dh,d2sw,d2sh;
3289 //bool flipX = false, flipY = false;
3291 //Logf("StretchDI\n");
3293 if(Sgn(w) != Sgn(sw))
3299 if(Sgn(h) != Sgn(sh))
3311 //Clip against the edges of the source
3314 dx+=(int)((0-sx) * s2dw);
3315 w-=(int)((0-sx) * s2dw);
3321 dy+=(int)((0-sy) * s2dh);
3322 h-=(int)((0-sy) * s2dh);
3327 if(sx+sw>bitmap.width-1)
3329 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
3330 sw-=sx+sw-(bitmap.width-1)-1;
3332 if(sy+sh>(bitmap.height-1))
3334 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
3335 sh-=sy+sh-(bitmap.height-1)-1;
3337 //Clip against the edges of the surfaceination
3338 if(dx<surface.box.left)
3341 sx+=(int)((surface.box.left-dx)*d2sw);
3342 sw-=(int)((surface.box.left-dx)*d2sw);
3343 w-=surface.box.left-dx;
3344 dx=surface.box.left;
3346 if(dy<surface.box.top)
3348 sy+=(int)((surface.box.top-dy)*d2sh);
3349 sh-=(int)((surface.box.top-dy)*d2sh);
3350 h-=surface.box.top-dy;
3353 if(dx+w>surface.box.right)
3355 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
3356 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
3357 w-=dx+w-surface.box.right-1;
3359 if(dy+h>surface.box.bottom)
3361 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
3362 h-=dy+h-surface.box.bottom-1;
3364 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
3366 dx += surface.offset.x;
3367 dy += surface.offset.y;
3369 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3371 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3372 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3373 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3374 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3375 glRasterPos2d(dx,dy);
3376 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
3377 glPixelZoom(s2dw, -s2dh);
3378 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3379 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3380 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3381 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3382 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3387 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3389 #if !defined(EM_MODE)
3392 //Clip against the edges of the source
3405 if(sx+w>bitmap.width-1)
3406 w-=sx+w-(bitmap.width-1)-1;
3407 if(sy+h>bitmap.height-1)
3408 h-=sy+h-(bitmap.height-1)-1;
3409 //Clip against the edges of the surfaceination
3410 if(dx<surface.box.left)
3413 sx+=surface.box.left-dx;
3414 w-=surface.box.left-dx;
3415 dx=surface.box.left;
3417 if(dy<surface.box.top)
3419 sy+=surface.box.top-dy;
3420 h-=surface.box.top-dy;
3423 if(dx+w>surface.box.right)
3425 //if(flip) sx+=dx+w-surface.box.right-1;
3426 w-=dx+w-surface.box.right-1;
3428 if(dy+h>surface.box.bottom)
3429 h-=dy+h-surface.box.bottom-1;
3433 dx += surface.offset.x;
3434 dy += surface.offset.y;
3436 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3438 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3439 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3440 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3441 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3442 glRasterPos2d(dx,dy);
3444 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3445 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3446 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3447 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3448 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3453 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3455 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3458 void UnloadFont(DisplaySystem displaySystem, Font font)
3460 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3463 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
3466 OGLSystem oglSystem = displaySystem.driverData;
3467 oglSystem.loadingFont = true;
3468 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3472 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
3474 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3477 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
3479 OGLSurface oglSurface = surface.driverData;
3480 OGLSystem oglSystem = display.displaySystem.driverData;
3481 oglSystem.loadingFont = true;
3483 //glTranslated(-0.375, -0.375, 0.0);
3487 if(surface.textOpacity)
3490 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3491 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3494 oglSurface.writingText = true;
3496 glEnable(GL_TEXTURE_2D);
3498 if(surface.outline.size)
3500 ColorAlpha outlineColor = surface.outline.color;
3501 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
3502 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3503 //glEnable(GL_BLEND);
3505 oglSurface.writingOutline = true;
3506 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3507 oglSurface.writingOutline = false;
3509 glColor4fv(oglSurface.foreground);
3511 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3512 oglSurface.writingText = false;
3513 oglSystem.loadingFont = false;
3515 glDisable(GL_TEXTURE_2D);
3517 //glTranslated(0.375, 0.375, 0.0);
3520 void TextFont(Display display, Surface surface, Font font)
3522 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3525 void TextOpacity(Display display, Surface surface, bool opaque)
3527 OGLSurface oglSurface = surface.driverData;
3528 oglSurface.opaqueText = opaque;
3531 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
3533 OGLSurface oglSurface = surface.driverData;
3534 OGLSystem oglSystem = display.displaySystem.driverData;
3535 oglSystem.loadingFont = true;
3536 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3537 oglSystem.loadingFont = false;
3540 void DrawingChar(Display display, Surface surface, char character)
3545 void LineStipple(Display display, Surface surface, uint32 stipple)
3547 //Logf("Stipple\n");
3551 #if defined(_GLES) || defined(EM_MODE)
3552 stippleEnabled = true;
3553 glesLineStipple(1, (uint16)stipple);
3555 glLineStipple(1, (uint16)stipple);
3556 glEnable(GL_LINE_STIPPLE);
3561 #if defined(_GLES) || defined(EM_MODE)
3562 stippleEnabled = false;
3563 glMatrixMode(GL_TEXTURE);
3565 glMatrixMode(GL_PROJECTION);
3566 glDisable(GL_TEXTURE_2D);
3568 glDisable(GL_LINE_STIPPLE);
3572 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3573 void SetRenderState(Display display, RenderState state, uint value)
3575 OGLDisplay oglDisplay = display.driverData;
3576 //Logf("RenderState\n");
3582 glEnable(GL_MULTISAMPLE_ARB);
3584 glDisable(GL_MULTISAMPLE_ARB);
3588 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3592 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3595 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3596 oglDisplay.depthWrite = (bool)value;
3600 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3601 glFogfv(GL_FOG_COLOR, (float *)&color);
3605 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
3608 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3612 #if !defined(EM_MODE)
3613 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3614 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3620 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3625 #if defined(__WIN32__)
3626 wglSwapIntervalEXT(value ? 1 : 0);
3633 void SetLight(Display display, int id, Light light)
3635 #if !defined(EM_MODE)
3636 //Logf("SetLight\n");
3640 Object lightObject = light.lightObject;
3641 float position[4] = { 0, 0, 0, 0 };
3642 float color[4] = { 0, 0, 0, 1 };
3644 glEnable(GL_LIGHT0 + id);
3646 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3647 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3648 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3651 if(!light.multiplier) light.multiplier = 1.0f;
3653 color[0] = light.diffuse.r * light.multiplier;
3654 color[1] = light.diffuse.g * light.multiplier;
3655 color[2] = light.diffuse.b * light.multiplier;
3656 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3658 color[0] = light.ambient.r * light.multiplier;
3659 color[1] = light.ambient.g * light.multiplier;
3660 color[2] = light.ambient.b * light.multiplier;
3661 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3662 color[0] = light.specular.r * light.multiplier;
3663 color[1] = light.specular.g * light.multiplier;
3664 color[2] = light.specular.b * light.multiplier;
3665 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3669 Vector3D positionVector;
3670 if(light.flags.spot)
3672 if(lightObject.flags.root || !lightObject.parent)
3674 positionVector = lightObject.transform.position;
3675 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3679 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3680 if(display.display3D.camera)
3681 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3687 if(!light.direction.x && !light.direction.y && !light.direction.z)
3689 Vector3Df vector { 0,0,-1 };
3691 mat.RotationQuaternion(light.orientation);
3692 positionVector.MultMatrixf(vector, mat);
3696 positionVector = light.direction;
3701 position[0] = (float)positionVector.x;
3702 position[1] = (float)positionVector.y;
3703 position[2] = (float)positionVector.z;
3705 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3708 // Display Light Position
3709 glDisable(GL_LIGHTING);
3710 glDisable(GL_DEPTH_TEST);
3714 glVertex3fv(position);
3716 glEnable(GL_DEPTH_TEST);
3717 glEnable(GL_LIGHTING);
3721 if(lightObject.flags.root || !lightObject.parent)
3723 positionVector = light.target.transform.position;
3724 positionVector.Subtract(positionVector, display.camera.cPosition);
3728 positionVector.MultMatrix(light.target.transform.position,
3729 lightObject.light.target.parent.matrix);
3730 positionVector.Subtract(positionVector, display.camera.cPosition);
3733 position[0] = positionVector.x;
3734 position[1] = positionVector.y;
3735 position[2] = positionVector.z;
3737 glDisable(GL_LIGHTING);
3738 glDisable(GL_DEPTH_TEST);
3742 glVertex3fv(position);
3744 glEnable(GL_DEPTH_TEST);
3745 glEnable(GL_LIGHTING);
3748 if(light.flags.attenuation)
3750 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3751 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3752 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3755 if(light.flags.spot)
3758 #define MAXLIGHT 0.9
3759 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3760 // Figure out exponent out of the hot spot
3761 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3763 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3764 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3765 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3771 Vector3Df vector { 0,0,-1 };
3772 Vector3Df direction;
3775 mat.RotationQuaternion(light.orientation);
3776 direction.MultMatrix(vector, mat);
3778 position[0] = direction.x;
3779 position[1] = direction.y;
3780 position[2] = direction.z;
3782 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3786 glDisable(GL_LIGHT0 + id);
3790 void SetCamera(Display display, Surface surface, Camera camera)
3792 OGLDisplay oglDisplay = display.driverData;
3793 //Logf("SetCamera\n");
3797 int left = surface.box.left + surface.offset.x;
3798 int top = surface.box.top + surface.offset.y;
3799 int right = surface.box.right + surface.offset.x;
3800 int bottom = surface.box.bottom + surface.offset.y;
3801 float origX = surface.offset.x + camera.origin.x;
3802 float origY = surface.offset.y + camera.origin.y;
3804 int y = display.height - bottom - 1;
3805 int w = right - left + 1;
3806 int h = bottom - top + 1;
3809 glViewport(x, y, w, h);
3811 // *** Projection Matrix ***
3812 if(!display.display3D.camera)
3814 glMatrixMode(GL_PROJECTION);
3818 glMatrixMode(GL_PROJECTION);
3819 if(display.display3D.collectingHits)
3821 float pickX = display.display3D.pickX + surface.offset.x;
3822 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3826 w / display.display3D.pickWidth, 0, 0, 0,
3827 0, h / display.display3D.pickHeight, 0, 0,
3829 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3830 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3833 glLoadMatrixd(pickMatrix.array);
3838 (left - origX) * camera.zMin / camera.focalX,
3839 (right - origX) * camera.zMin / camera.focalX,
3840 (bottom - origY) * camera.zMin / camera.focalY,
3841 (top - origY) * camera.zMin / camera.focalY,
3842 camera.zMin, camera.zMax);
3844 glDisable(GL_BLEND);
3846 // *** Z Inverted Identity Matrix ***
3847 glMatrixMode(GL_MODELVIEW);
3848 if(!display.display3D.camera)
3853 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3855 // *** View Matrix ***
3856 glMultMatrixd(camera.viewMatrix.array);
3861 glEnable(GL_DEPTH_TEST);
3862 //#if !defined(EM_MODE)
3863 glEnable(GL_LIGHTING);
3864 glShadeModel(GL_SMOOTH);
3866 glDepthMask((byte)bool::true);
3867 oglDisplay.depthWrite = true;
3869 glEnable(GL_MULTISAMPLE_ARB);
3871 else if(display.display3D.camera)
3873 oglDisplay.depthWrite = false;
3874 glViewport(0,0,display.width,display.height);
3876 glDisable(GL_CULL_FACE);
3877 glDisable(GL_DEPTH_TEST);
3878 glDisable(GL_LIGHTING);
3880 glDisable(GL_TEXTURE_2D);
3881 //#if !defined(EM_MODE)
3882 glShadeModel(GL_FLAT);
3885 glDisable(GL_MULTISAMPLE_ARB);
3887 // *** Restore 2D MODELVIEW Matrix ***
3890 // *** Restore 2D PROJECTION Matrix ***
3891 glMatrixMode(GL_PROJECTION);
3897 void ApplyMaterial(Display display, Material material, Mesh mesh)
3899 //Logf("ApplyMaterial\n");
3902 if(material.flags.doubleSided)
3904 #if !defined(EM_MODE)
3905 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3907 glDisable(GL_CULL_FACE);
3911 #if !defined(EM_MODE)
3912 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3914 glEnable(GL_CULL_FACE);
3918 if(material.flags.noFog)
3924 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
3926 Bitmap map = material.baseMap;
3927 glEnable(GL_TEXTURE_2D);
3928 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3930 glMatrixMode(GL_TEXTURE);
3932 if(material.uScale && material.vScale)
3933 glScalef(material.uScale, material.vScale, 1);
3934 glMatrixMode(GL_MODELVIEW);
3936 if(material.flags.tile)
3938 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3939 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3943 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3944 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3948 glDisable(GL_TEXTURE_2D);
3951 glColor4f(material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity);
3953 if(mesh.flags.colors)
3955 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3956 glEnable(GL_COLOR_MATERIAL);
3960 glDisable(GL_COLOR_MATERIAL);
3962 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3963 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3966 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3967 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3971 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3972 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3975 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3976 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3979 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3983 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3985 OGLMesh oglMesh = mesh.data;
3988 if(!mesh.flags.vertices)
3990 oglMesh.vertices.free();
3991 delete mesh.vertices;
3993 if(!mesh.flags.normals)
3995 oglMesh.normals.free();
3996 delete mesh.normals;
3998 if(!mesh.flags.texCoords1)
4000 oglMesh.texCoords.free();
4001 delete mesh.texCoords;
4003 if(!mesh.flags.texCoords2)
4005 oglMesh.texCoords2.free();
4006 // delete mesh.texCoords2;
4008 if(!mesh.flags.colors)
4010 oglMesh.colors.free();
4021 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
4023 bool result = false;
4026 mesh.data = OGLMesh { };
4029 if(mesh.nVertices == nVertices)
4031 // Same number of vertices, adding features (Leaves the other features pointers alone)
4032 if(mesh.flags != flags)
4034 if(!mesh.flags.vertices && flags.vertices)
4036 if(flags.doubleVertices)
4038 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
4041 mesh.vertices = new Vector3Df[nVertices];
4043 if(!mesh.flags.normals && flags.normals)
4045 if(flags.doubleNormals)
4047 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
4050 mesh.normals = new Vector3Df[nVertices];
4052 if(!mesh.flags.texCoords1 && flags.texCoords1)
4054 mesh.texCoords = new Pointf[nVertices];
4056 if(!mesh.flags.colors && flags.colors)
4058 mesh.colors = new ColorRGBAf[nVertices];
4064 // New number of vertices, reallocate all current and new features
4065 flags |= mesh.flags;
4068 if(flags.doubleVertices)
4070 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
4073 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
4077 if(flags.doubleNormals)
4079 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
4082 mesh.normals = renew mesh.normals Vector3Df[nVertices];
4084 if(flags.texCoords1)
4086 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
4090 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
4098 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
4100 OGLMesh oglMesh = mesh.data;
4101 if(!flags) flags = mesh.flags;
4106 oglMesh.vertices.upload(
4107 mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices); //, GL_STATIC_DRAW_ARB );
4110 oglMesh.normals.upload(
4111 mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals); //, GL_STATIC_DRAW_ARB );
4113 if(flags.texCoords1)
4114 oglMesh.texCoords.upload(
4115 mesh.nVertices * sizeof(Pointf), mesh.texCoords); //, GL_STATIC_DRAW_ARB );
4118 oglMesh.colors.upload(
4119 mesh.nVertices * sizeof(ColorRGBAf), mesh.colors); //, GL_STATIC_DRAW_ARB );
4123 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
4130 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
4134 oglIndices.buffer.free();
4135 delete oglIndices.indices;
4140 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
4142 OGLIndices oglIndices = OGLIndices { };
4145 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
4146 oglIndices.nIndices = nIndices;
4151 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
4155 oglIndices.buffer.upload(
4156 nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
4157 oglIndices.indices); //GL_STATIC_DRAW_ARB);
4161 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
4164 return oglIndices.indices;
4167 void SelectMesh(Display display, Mesh mesh)
4169 //Logf("SelectMesh\n");
4171 #if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
4173 #if defined(__WIN32__)
4174 if(glUnlockArraysEXT)
4176 if(!vboAvailable && display.display3D.mesh)
4177 glUnlockArraysEXT();
4182 OGLMesh oglMesh = mesh.data;
4184 // *** Vertex Stream ***
4185 glEnableClientState(GL_VERTEX_ARRAY);
4186 if(!display.display3D.collectingHits && oglMesh)
4188 oglMesh.vertices.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, oglMesh.vertices.buffer ? null : (double *)mesh.vertices);
4190 // *** Normals Stream ***
4191 if(mesh.normals || mesh.flags.normals)
4193 glEnableClientState(GL_NORMAL_ARRAY);
4194 oglMesh.normals.use(normal, 3, GL_FLOAT, 0, oglMesh.normals.buffer ? null : mesh.normals);
4197 glDisableClientState(GL_NORMAL_ARRAY);
4199 // *** Texture Coordinates Stream ***
4200 if(mesh.texCoords || mesh.flags.texCoords1)
4202 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4203 oglMesh.texCoords.use(texCoord, 2, GL_FLOAT, 0, oglMesh.texCoords.buffer ? null : mesh.texCoords);
4206 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4208 // *** Color Stream ***
4209 if(mesh.colors || mesh.flags.colors)
4211 glEnableClientState(GL_COLOR_ARRAY);
4212 oglMesh.colors.use(color, 4, GL_FLOAT, 0, oglMesh.colors.buffer ? null : mesh.colors);
4215 glDisableClientState(GL_COLOR_ARRAY);
4219 noAB.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, (double *)mesh.vertices);
4220 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
4222 glEnableClientState(GL_NORMAL_ARRAY);
4223 noAB.use(normal, 3, GL_FLOAT, 0, mesh.normals);
4226 glDisableClientState(GL_NORMAL_ARRAY);
4227 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
4229 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4230 noAB.use(texCoord, 2, GL_FLOAT, 0, mesh.texCoords);
4233 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4234 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
4236 glEnableClientState(GL_COLOR_ARRAY);
4237 noAB.use(color, 4, GL_FLOAT, 0, mesh.colors);
4240 glDisableClientState(GL_COLOR_ARRAY);
4243 #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
4245 #if defined(__WIN32__)
4249 glLockArraysEXT(0, mesh.nVertices);
4255 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
4257 //Logf("DrawPrimitives\n");
4259 if(primitive->type.vertexRange)
4260 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
4263 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
4264 // HACK TO SPEED THINGS UP...
4266 /*GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4267 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
4270 glBegin(primitiveTypes[primitive->type.primitiveType]);
4273 OGLIndices oglIndices = primitive->data;
4274 MeshFeatures flags = mesh.flags;
4275 for(c = 0; c<primitive->nIndices; c++)
4277 uint16 index = ((uint16 *) oglIndices.indices)[c];
4278 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
4279 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
4280 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
4281 glVertex3fv((float *)&mesh.vertices[index]);
4289 OGLIndices oglIndices = primitive->data;
4291 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
4293 if(primitive->type.indices32bit)
4294 ; //oglIndices.buffer.draw(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, GL_UNSIGNED_SHORT, 0);
4296 oglIndices.buffer.draw(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, GL_UNSIGNED_SHORT, 0);
4300 if(primitive->type.indices32bit)
4301 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4302 oglIndices ? oglIndices.indices : primitive->indices);
4304 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4305 GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
4311 void PushMatrix(Display display)
4316 void PopMatrix(Display display, bool setMatrix)
4321 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
4323 Matrix matrix = transMatrix;
4324 Camera camera = useCamera ? display.display3D.camera : null;
4329 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
4334 matrix.m[3][0] - camera.cPosition.x,
4335 matrix.m[3][1] - camera.cPosition.y,
4336 matrix.m[3][2] - camera.cPosition.z);
4348 glMultMatrixd(matrix.array);
4353 public void UseSingleGLContext(bool useSingle)
4355 useSingleGLContext = useSingle;
4358 default dllexport void *
4359 #if defined(__WIN32__)
4360 __attribute__((stdcall))
4362 IS_GLGetContext(DisplaySystem displaySystem)
4366 #if defined(__WIN32__)
4367 OGLSystem system = displaySystem.driverData;
4369 #elif defined(__ANDROID__) || defined(__ODROID__)
4371 #elif defined(__EMSCRIPTEN__)
4373 OGLSystem system = displaySystem.driverData;
4374 return system.glContext;