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
15 //#include <GL/miniglx.h>
19 #if defined(__ANDROID__)
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__)
92 #if defined(__unix__) || defined(__APPLE__)
100 static double nearPlane = 1;
102 #define glLoadMatrix glLoadMatrixd
103 #define glMultMatrix glMultMatrixd
104 #define glGetMatrix glGetDoublev
105 #define glTranslate glTranslated
106 #define glScale glScaled
109 #define glVertex3v glVertex3dv
110 #define glNormal3v glNormal3dv
114 //#ifdef VERTEX_FORMAT_DOUBLE
116 #define glLoadMatrix glLoadMatrixd
117 #define glMultMatrix glMultMatrixd
118 #define glGetMatrix glGetDoublev
119 #define glVertex3v glVertex3dv
120 #define glNormal3v glNormal3dv
121 #define glTranslate glTranslated
122 #define glScale glScaled
123 //#define GL_VERTEX_FORMAT GL_DOUBLE
127 #define glLoadMatrix glLoadMatrixf
128 #define glMultMatrix glMultMatrixf
129 #define glGetMatrix glGetFloatv
130 #define glVertex3v glVertex3fv
131 #define glNormal3v glNormal3fv
132 #define glTranslate glTranslatef
133 #define glScale glScalef
134 //#define GL_VERTEX_FORMAT GL_FLOAT
139 #define GL_ARRAY_BUFFER_ARB 0x8892
140 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
141 #define GL_STATIC_DRAW_ARB 0x88E4
142 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
143 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
145 #define GL_MULTISAMPLE_ARB 0x809D
147 #if defined(__WIN32__)
149 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
150 #define WGL_SAMPLES_ARB 0x2042
152 #define WGL_WGLEXT_VERSION 1
153 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
154 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
155 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
156 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
157 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
158 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
159 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
160 #define WGL_ACCELERATION_ARB 0x2003
161 #define WGL_NEED_PALETTE_ARB 0x2004
162 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
163 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
164 #define WGL_SWAP_METHOD_ARB 0x2007
165 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
166 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
167 #define WGL_TRANSPARENT_ARB 0x200A
168 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
169 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
170 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
171 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
172 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
173 #define WGL_SHARE_DEPTH_ARB 0x200C
174 #define WGL_SHARE_STENCIL_ARB 0x200D
175 #define WGL_SHARE_ACCUM_ARB 0x200E
176 #define WGL_SUPPORT_GDI_ARB 0x200F
177 #define WGL_SUPPORT_OPENGL_ARB 0x2010
178 #define WGL_DOUBLE_BUFFER_ARB 0x2011
179 #define WGL_STEREO_ARB 0x2012
180 #define WGL_PIXEL_TYPE_ARB 0x2013
181 #define WGL_COLOR_BITS_ARB 0x2014
182 #define WGL_RED_BITS_ARB 0x2015
183 #define WGL_RED_SHIFT_ARB 0x2016
184 #define WGL_GREEN_BITS_ARB 0x2017
185 #define WGL_GREEN_SHIFT_ARB 0x2018
186 #define WGL_BLUE_BITS_ARB 0x2019
187 #define WGL_BLUE_SHIFT_ARB 0x201A
188 #define WGL_ALPHA_BITS_ARB 0x201B
189 #define WGL_ALPHA_SHIFT_ARB 0x201C
190 #define WGL_ACCUM_BITS_ARB 0x201D
191 #define WGL_ACCUM_RED_BITS_ARB 0x201E
192 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
193 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
194 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
195 #define WGL_DEPTH_BITS_ARB 0x2022
196 #define WGL_STENCIL_BITS_ARB 0x2023
197 #define WGL_AUX_BUFFERS_ARB 0x2024
198 #define WGL_NO_ACCELERATION_ARB 0x2025
199 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
200 #define WGL_FULL_ACCELERATION_ARB 0x2027
201 #define WGL_SWAP_EXCHANGE_ARB 0x2028
202 #define WGL_SWAP_COPY_ARB 0x2029
203 #define WGL_SWAP_UNDEFINED_ARB 0x202A
204 #define WGL_TYPE_RGBA_ARB 0x202B
205 #define WGL_TYPE_COLORINDEX_ARB 0x202C
206 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
207 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
208 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
209 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
210 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
211 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
212 #define WGL_PBUFFER_LARGEST_ARB 0x2033
213 #define WGL_PBUFFER_WIDTH_ARB 0x2034
214 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
215 #define WGL_PBUFFER_LOST_ARB 0x2036
216 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
217 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
218 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
219 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
220 #define WGL_ACCELERATION_EXT 0x2003
221 #define WGL_NEED_PALETTE_EXT 0x2004
222 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
223 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
224 #define WGL_SWAP_METHOD_EXT 0x2007
225 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
226 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
227 #define WGL_TRANSPARENT_EXT 0x200A
228 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
229 #define WGL_SHARE_DEPTH_EXT 0x200C
230 #define WGL_SHARE_STENCIL_EXT 0x200D
231 #define WGL_SHARE_ACCUM_EXT 0x200E
232 #define WGL_SUPPORT_GDI_EXT 0x200F
233 #define WGL_SUPPORT_OPENGL_EXT 0x2010
234 #define WGL_DOUBLE_BUFFER_EXT 0x2011
235 #define WGL_STEREO_EXT 0x2012
236 #define WGL_PIXEL_TYPE_EXT 0x2013
237 #define WGL_COLOR_BITS_EXT 0x2014
238 #define WGL_RED_BITS_EXT 0x2015
239 #define WGL_RED_SHIFT_EXT 0x2016
240 #define WGL_GREEN_BITS_EXT 0x2017
241 #define WGL_GREEN_SHIFT_EXT 0x2018
242 #define WGL_BLUE_BITS_EXT 0x2019
243 #define WGL_BLUE_SHIFT_EXT 0x201A
244 #define WGL_ALPHA_BITS_EXT 0x201B
245 #define WGL_ALPHA_SHIFT_EXT 0x201C
246 #define WGL_ACCUM_BITS_EXT 0x201D
247 #define WGL_ACCUM_RED_BITS_EXT 0x201E
248 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
249 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
250 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
251 #define WGL_DEPTH_BITS_EXT 0x2022
252 #define WGL_STENCIL_BITS_EXT 0x2023
253 #define WGL_AUX_BUFFERS_EXT 0x2024
254 #define WGL_NO_ACCELERATION_EXT 0x2025
255 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
256 #define WGL_FULL_ACCELERATION_EXT 0x2027
257 #define WGL_SWAP_EXCHANGE_EXT 0x2028
258 #define WGL_SWAP_COPY_EXT 0x2029
259 #define WGL_SWAP_UNDEFINED_EXT 0x202A
260 #define WGL_TYPE_RGBA_EXT 0x202B
261 #define WGL_TYPE_COLORINDEX_EXT 0x202C
262 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
263 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
264 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
265 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
266 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
267 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
268 #define WGL_PBUFFER_LARGEST_EXT 0x2033
269 #define WGL_PBUFFER_WIDTH_EXT 0x2034
270 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
271 #define WGL_DEPTH_FLOAT_EXT 0x2040
272 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
273 #define WGL_SAMPLES_3DFX 0x2061
274 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
275 #define WGL_SAMPLES_EXT 0x2042
276 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
277 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
278 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
279 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
280 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
281 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
282 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
283 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
284 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
285 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
286 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
287 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
288 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
289 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
290 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
291 #define WGL_ARB_buffer_region 1
292 #define WGL_ARB_extensions_string 1
293 #define WGL_ARB_pixel_format 1
294 #define WGL_ARB_make_current_read 1
295 #define WGL_ARB_pbuffer 1
296 #define WGL_EXT_display_color_table 1
297 #define WGL_EXT_extensions_string 1
298 #define WGL_EXT_make_current_read 1
299 #define WGL_EXT_pbuffer 1
300 #define WGL_EXT_pixel_format 1
301 #define WGL_EXT_swap_control 1
302 #define WGL_WGL_EXT_depth_float 1
303 #define WGL_WGL_3DFX_multisample 1
304 #define WGL_WGL_EXT_multisample 1
305 #define WGL_NV_allocate_memory 1
308 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
309 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
310 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
311 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
312 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
316 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
317 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
318 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
319 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
321 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
322 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
323 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
324 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
325 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
326 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
327 typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
328 typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
329 typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
331 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
332 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
333 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
334 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
335 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
336 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
337 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
338 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
340 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
341 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
342 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
343 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
344 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
345 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
346 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
347 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
348 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
349 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
350 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
351 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
352 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
354 #ifdef WGL_WGLEXT_PROTOTYPES
355 extern BOOL WINAPI wglSwapIntervalEXT (int);
356 extern int WINAPI wglGetSwapIntervalEXT (void);
357 #endif /* WGL_WGLEXT_PROTOTYPES */
358 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
359 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
361 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
365 #if defined(__ANDROID__)
367 // Our own matrix stack
368 static Matrix matrixStack[3][32];
369 static int matrixIndex[3];
370 static int curStack = 0;
372 // OpenGL ES Porting Kit
374 #define glBindFramebuffer glBindFramebufferOES
375 #define glBindRenderbuffer glBindRenderbufferOES
376 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
377 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
378 #define glFramebufferTexture2D glFramebufferTexture2DOES
379 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
380 #define glGenFramebuffers glGenFramebuffersOES
381 #define glGenRenderbuffers glGenRenderbuffersOES
382 #define glDeleteFramebuffers glDeleteFramebuffersOES
383 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
385 #define GL_POLYGON_STIPPLE 0xFFFF
386 #define GL_LINE_STIPPLE 0xFFFF
387 #define GL_LINE 0xFFFF
388 #define GL_FILL 0xFFFF
389 #define GL_ALL_ATTRIB_BITS 0xFFFF
390 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
391 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
393 #define GL_UNSIGNED_INT 0x1405
394 #define GL_DOUBLE 0x140A
399 #define glBufferDatai glesBufferDatai
400 #define glBufferDatad glesBufferDatad
401 #define glVertexPointeri glesVertexPointeri
402 #define glVertexPointerd glesVertexPointerd
404 #define glRecti glesRecti
405 #define glBegin glesBegin
406 #define glTexCoord2i glesTexCoord2i
407 #define glVertex2i glesVertex2i
408 #define glTexCoord2d glesTexCoord2d
409 #define glVertex2d glesVertex2d
410 #define glTexCoord2f glesTexCoord2f
411 #define glVertex2f glesVertex2f
412 #define glEnd glesEnd
413 #define glColor3f glesColor3f
414 #define glColor4ub glesColor4ub
415 #define glColor4fv glesColor4fv
416 #define glLineStipple glesLineStipple
417 #define glNormal3fv glesNormal3fv
418 #define glTexCoord2fv glesTexCoord2fv
419 #define glColorMaterial glesColorMaterial
421 #define glLoadMatrixd glesLoadMatrixd
422 #define glMultMatrixd glesMultMatrixd
423 #define glFrustum glesFrustum
424 #define glOrtho glesOrtho
425 #define glScaled glesScaled
426 #define glTranslated glesTranslated
427 #define glRotated glesRotated
428 #define glVertex3d glesVertex3d
429 #define glVertex3f glesVertex3f
430 #define glVertex3fv glesVertex3fv
431 #define glLightModeli glesLightModeli
435 #define GL_QUAD_STRIP 0
436 //#define GL_DOUBLE 0
437 //#define GL_UNSIGNED_INT 0
440 //#define GL_LINE_STIPPLE 0
441 #define GL_BGRA_EXT 0
442 #define GL_UNPACK_ROW_LENGTH 0
443 #define GL_UNPACK_SKIP_PIXELS 0
444 #define GL_UNPACK_SKIP_ROWS 0
446 #define GL_PACK_ROW_LENGTH 0
447 #define GL_PACK_SKIP_ROWS 0
448 #define GL_PACK_SKIP_PIXELS 0
450 static EGLDisplay eglDisplay;
451 static EGLSurface eglSurface;
452 static EGLContext eglContext;
453 static int eglWidth, eglHeight;
455 static bool egl_init_display(ANativeWindow* window)
457 const EGLint attribs[] =
459 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
464 /*EGL_SAMPLE_BUFFERS, 1,
465 EGL_SAMPLES, 0, //2,*/
474 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
475 eglInitialize(display, 0, 0);
476 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
477 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
479 surface = eglCreateWindowSurface(display, config, window, null);
480 context = eglCreateContext(display, config, null, null);
482 if(!eglMakeCurrent(display, surface, surface, context))
485 eglQuerySurface(display, surface, EGL_WIDTH, &w);
486 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
488 eglDisplay = display;
489 eglContext = context;
490 eglSurface = surface;
494 glEnableClientState(GL_VERTEX_ARRAY);
496 // Initialize GL state.
497 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
498 glEnable(GL_CULL_FACE);
499 glShadeModel(GL_SMOOTH);
500 glDisable(GL_DEPTH_TEST);
502 glDisable(GL_CULL_FACE);
503 glDisable(GL_DEPTH_TEST);
505 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
508 matrixStack[0][0].Identity();
509 matrixStack[1][0].Identity();
510 matrixStack[2][0].Identity();
512 glesMatrixMode(GL_MODELVIEW);
513 glScaled(1.0, 1.0, -1.0);
514 glesMatrixMode(GL_PROJECTION);
515 glShadeModel(GL_FLAT);
517 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
518 glFogi(GL_FOG_MODE, GL_EXP);
519 glFogf(GL_FOG_DENSITY, 0);
520 glEnable(GL_NORMALIZE);
521 glDepthFunc(GL_LESS);
523 glDisable(GL_MULTISAMPLE_ARB);
527 glOrtho(0,w,h,0,0.0,1.0);
529 currentVertexBuffer = 0;
533 static void egl_term_display()
537 glDeleteTextures(1, &stippleTexture);
540 if(eglDisplay != EGL_NO_DISPLAY)
542 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
543 if(eglContext != EGL_NO_CONTEXT)
544 eglDestroyContext(eglDisplay, eglContext);
545 if(eglSurface != EGL_NO_SURFACE)
546 eglDestroySurface(eglDisplay, eglSurface);
547 eglTerminate(eglDisplay);
549 eglDisplay = EGL_NO_DISPLAY;
550 eglContext = EGL_NO_CONTEXT;
551 eglSurface = EGL_NO_SURFACE;
554 // OpenGL Immediate Mode Porting Kit
555 static int beginCount;
556 static int vertexCount;
557 static int normalCount;
558 static float *vertexPointer;
559 static float *normalPointer;
560 static GLenum beginMode;
561 static unsigned int beginBufferSize, normalBufferSize;
562 static int numVertexCoords = 2;
564 void glesRecti(int a, int b, int c, int d)
574 void glesBegin(GLenum mode)
581 normalBufferSize = beginBufferSize = 1024; // default number of vertices
582 vertexPointer = new float[beginBufferSize * 5];
583 normalPointer = new float[normalBufferSize * 3];
587 void glesTexCoord2f(float x, float y)
589 int count = vertexCount;
591 if(vertexCount + numVertexCoords > beginBufferSize)
593 beginBufferSize = beginBufferSize + beginBufferSize/2;
594 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
597 vertexPointer[count*(2+numVertexCoords) ] = x;
598 vertexPointer[count*(2+numVertexCoords)+1] = y;
601 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
603 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
604 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
606 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
607 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
611 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
612 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
613 void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
615 void glesVertex2f(float x, float y)
618 if(vertexCount + 4 > beginBufferSize)
620 beginBufferSize = beginBufferSize + beginBufferSize/2;
621 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
624 vertexPointer[vertexCount*4+2] = x;
625 vertexPointer[vertexCount*4+3] = y;
628 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
630 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
631 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
633 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
634 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
639 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
640 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
644 int mode = beginMode;
645 if(mode == GL_QUADS) mode = GL_TRIANGLES;
646 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
648 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
649 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
650 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
651 if(normalCount && normalCount == vertexCount)
653 glEnableClientState(GL_NORMAL_ARRAY);
654 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
657 glDrawArrays(mode, 0, vertexCount);
659 glDisableClientState(GL_NORMAL_ARRAY);
660 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
665 static float *floatVPBuffer = null;
666 static short *shortVPBuffer = null;
667 static unsigned int shortVPSize = 0, floatVPSize = 0;
670 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
671 static unsigned short *shortBDBuffer = null;
672 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
674 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
679 if(numVertices*numCoords > shortVPSize)
681 shortVPSize = numVertices*numCoords;
682 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
684 for(i = 0; i < numVertices*numCoords; i++)
685 shortVPBuffer[i] = (short)pointer[i];
686 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
689 glVertexPointer(numCoords, GL_SHORT, stride, 0);
692 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
697 if(numVertices*numCoords > floatVPSize)
699 floatVPSize = numVertices*numCoords;
700 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
702 for(i = 0; i < numVertices*numCoords; i++)
703 floatVPBuffer[i] = (float)pointer[i];
704 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
707 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
710 void glesTexReuseIntVP(int numCoords)
712 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
715 void glesTexReuseDoubleVP(int numCoords)
717 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
720 void glesColor3f( float r, float g, float b )
722 glColor4f(r, g, b, 1.0f);
725 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
727 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
730 void glesColor4fv(float * a)
732 glColor4f(a[0], a[1], a[2], a[3]);
735 void glesBufferDatad(int target, int size, void * data, int usage)
737 int numElems = size/sizeof(double);
738 double * dblPtr = (double *)data;
740 if (numElems > floatVPSize)
742 floatVPSize = numElems;
743 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
745 for (i=0; i< numElems; i++)
746 floatVPBuffer[i] = (float)dblPtr[i];
748 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
751 void glesBufferDatai(int target, int size, void * data, int usage)
753 int numElems = size/sizeof(unsigned int);
754 unsigned int * pointer = (unsigned int *)data;
756 if (numElems > shortBDSize)
758 shortBDSize = numElems;
759 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
761 for (i=0; i< numElems; i++)
762 shortBDBuffer[i] = (unsigned short)pointer[i];
764 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
767 // *** Our Custom Matrix Stack ***
769 static void LoadCurMatrix()
771 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
774 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
775 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
776 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
777 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
782 void glesLoadIdentity()
784 matrixStack[curStack][matrixIndex[curStack]].Identity();
788 void glesPushMatrix()
790 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
792 matrixIndex[curStack]++;
793 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
799 if(matrixIndex[curStack] > 0)
801 matrixIndex[curStack]--;
806 void glesLoadMatrixd(double * i)
808 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
812 void glesOrtho( double l, double r, double b, double t, double n, double f )
816 (2 / (r - l)), 0, 0, 0,
817 0, (2 / (t - b)), 0, 0,
818 0, 0, (-2 / (f - n)), 0,
819 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
822 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
823 matrixStack[curStack][matrixIndex[curStack]] = res;
827 void glesFrustum( double l, double r, double b, double t, double n, double f )
837 double A = ((r + l) / (r - l));
838 double B = ((t + b) / (t - b));
839 double C = (-(f + n) / (f - n));
840 double D = (-2*f*n/(f-n));
843 (2.0*n / (r - l)), 0, 0, 0,
844 0, (2.0*n / (t - b)), 0, 0,
849 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
850 matrixStack[curStack][matrixIndex[curStack]] = res;
855 void glesRotated( double a, double b, double c, double d )
860 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
861 m.RotationQuaternion(q);
862 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
863 matrixStack[curStack][matrixIndex[curStack]] = r;
866 void glesScaled( double a, double b, double c )
872 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
873 matrixStack[curStack][matrixIndex[curStack]] = r;
877 void glesTranslated( double a, double b, double c )
883 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
884 matrixStack[curStack][matrixIndex[curStack]] = r;
888 void glesMultMatrixd( double * i )
891 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
892 matrixStack[curStack][matrixIndex[curStack]] = r;
896 void glesMatrixMode(int mode)
898 curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
902 #define glPushMatrix glesPushMatrix
903 #define glPopMatrix glesPopMatrix
904 #define glLoadIdentity glesLoadIdentity
905 #define glMatrixMode glesMatrixMode
907 /* Using the built-in matrix stack
908 void glesLoadMatrixd( double * i )
912 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
913 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
914 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
915 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
920 void glesOrtho( double l, double r, double b, double t, double n, double f )
924 { (float)(2 / (r - l)), 0, 0, 0 },
925 { 0, (float)(2 / (t - b)), 0, 0 },
926 { 0, 0, (float)(-2 / (f - n)), 0 },
927 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
929 glMultMatrixf((float *)matrix);
932 void glesFrustum( double l, double r, double b, double t, double n, double f )
934 float A = (float)((r + l) / (r - l));
935 float B = (float)((t + b) / (t - b));
936 float C = (float)(-(f + n) / (f - n));
937 float D = (float)(-2*f*n/(f-n));
940 { (float)(2*n / (r - l)), 0, 0, 0 },
941 { 0, (float)(2*n / (t - b)), 0, 0 },
945 glMultMatrixf((float *)matrix);
948 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
949 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
950 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
952 void glesMultMatrixd( double * i )
956 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
957 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
958 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
959 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
965 // Need to do these...
966 void glesVertex3f( float x, float y, float z )
969 if(vertexCount + 4 > beginBufferSize)
971 beginBufferSize = beginBufferSize + beginBufferSize/2;
972 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
975 vertexPointer[vertexCount*5+2] = x;
976 vertexPointer[vertexCount*5+3] = y;
977 vertexPointer[vertexCount*5+4] = z;
980 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
982 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
983 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
984 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
986 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
987 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
988 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
994 void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
995 void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
997 void glesNormal3f(float x, float y, float z)
999 normalCount = vertexCount;
1000 if(vertexCount + 4 > normalBufferSize)
1002 normalBufferSize = normalBufferSize + normalBufferSize/2;
1003 normalPointer = renew normalPointer float[normalBufferSize * 2];
1006 normalPointer[normalCount*3+0] = x;
1007 normalPointer[normalCount*3+1] = y;
1008 normalPointer[normalCount*3+2] = z;
1011 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1013 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1014 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1015 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1017 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1018 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1019 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1023 void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1024 void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1026 void glesColorMaterial(int a, int b)
1028 PrintLn("glColorMaterial stub");
1031 void glesTerminate()
1033 delete vertexPointer;
1034 delete normalPointer;
1035 beginBufferSize = 0;
1037 delete floatVPBuffer;
1040 delete shortVPBuffer;
1043 delete shortBDBuffer;
1047 static GLuint stippleTexture;
1048 static bool stippleEnabled;
1050 void glesLineStipple( int i, unsigned short j )
1054 for(x = 0; x < 16; x++)
1056 bool v = (j & (1 << x)) != 0;
1057 texture[x] = v ? 0xFFFFFFFF : 0;
1060 glGenTextures(1, &stippleTexture);
1061 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1062 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1063 glEnable(GL_TEXTURE_2D);
1064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1065 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1066 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1067 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1068 glMatrixMode(GL_TEXTURE);
1070 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1071 glScaled(i/16.0, 1, 1.0f);
1072 glTranslated(0.5, 0.5, 0);
1073 glMatrixMode(GL_PROJECTION);
1076 void glesLightModeli( unsigned int pname, int param )
1078 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1079 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1082 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1083 void glFogi( unsigned int pname, int param ) { }
1084 void glPolygonMode( unsigned int i, unsigned int j ) { }
1087 // *** Picking won't be supported for now ***
1088 void glPushName( unsigned int i ) { }
1089 void glLoadName( unsigned int i ) { }
1090 void glPopName() { }
1092 // Probably replace by regular glBlendFunc ...
1093 void glBlendFuncSeparate(int a, int b, int c, int d)
1098 // For direct pixel blitting...
1099 void glRasterPos2d(double a, double b) { }
1100 void glPixelZoom(float a, float b) { }
1101 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1105 /* Non OpenGL ES friendly stuff
1106 #undef GL_UNSIGNED_INT
1111 #undef GL_QUAD_STRIP
1112 #undef GL_POLYGON_STIPPLE
1113 #undef GL_LINE_STIPPLE
1116 #undef GL_ALL_ATTRIB_BITS
1117 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1122 #if !defined(__APPLE__)
1123 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1124 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1125 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1126 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1131 static int currentVertexBuffer;
1133 bool GLSelectVBO(uint vbo)
1135 if(currentVertexBuffer != vbo)
1137 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
1138 currentVertexBuffer = vbo;
1144 void GLGenBuffers(int count, uint * buffer)
1147 glGenBuffers(count, buffer);
1149 #if defined(__WIN32__)
1152 glGenBuffersARB(count, buffer);
1156 void GLDeleteBuffers(int count, GLuint * buffer)
1159 glDeleteBuffers(count, buffer);
1161 #if defined(__WIN32__)
1162 if(glDeleteBuffersARB)
1164 glDeleteBuffersARB(count, buffer);
1168 void GLBindBuffer(int target, uint buffer)
1171 glBindBuffer(target, buffer);
1173 #if defined(__WIN32__)
1176 glBindBufferARB(target, buffer);
1180 public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
1183 if(type == GL_DOUBLE)
1184 glesVertexPointerd(numCoords, stride, pointer, numVertices);
1185 else if(type == GL_INT)
1186 glesVertexPointeri(numCoords, stride, pointer, numVertices);
1189 glVertexPointer(numCoords, glType, stride, ptr);
1192 public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1195 if(type == GL_DOUBLE)
1196 glesBufferDatad(target, size, (void *)data, usage);
1197 else if(type == GL_UNSIGNED_INT)
1198 glesBufferDatai(target, size, (void *)data, usage);
1200 glBufferData(target, size, data, usage);
1203 #if defined(__WIN32__)
1206 glBufferDataARB(target, size, data, usage);
1211 static int displayWidth, displayHeight;
1213 #define GL_CLAMP_TO_EDGE 0x812F
1215 static bool vboAvailable;
1217 static bool useSingleGLContext = false;
1218 class OGLDisplay : struct
1220 #if defined(__WIN32__)
1230 int imageBuffers[2];
1231 byte * pboMemory1, * pboMemory2;
1233 #elif !defined(__ANDROID__)
1234 GLXContext glContext;
1237 XShmSegmentInfo shminfo;
1239 XShmSegmentInfo shminfoShape;
1240 XImage * shapeImage;
1244 X11Picture windowPicture;
1245 X11Picture pixmapPicture;
1247 X11Picture shapePicture;
1250 ColorAlpha * flippingBuffer;
1251 int flipBufH, flipBufW;
1256 class OGLSystem : struct
1261 #if defined(__WIN32__)
1262 PIXELFORMATDESCRIPTOR pfd;
1267 #elif !defined(__ANDROID__)
1268 XVisualInfo * visualInfo;
1269 GLXContext glContext;
1270 GLXDrawable glxDrawable;
1274 class OGLSurface : struct
1280 bool writingOutline;
1282 float foreground[4], background[4], bitmapMult[4];
1285 class OGLMesh : struct
1294 class OGLIndices : struct
1301 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1302 static int primitiveTypes[RenderPrimitiveType] =
1304 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1311 class OpenGLDisplayDriver : DisplayDriver
1313 class_property(name) = "OpenGL";
1315 bool LockSystem(DisplaySystem displaySystem)
1317 #if !defined(__ANDROID__)
1318 OGLSystem oglSystem = displaySystem.driverData;
1319 if(useSingleGLContext) return true;
1320 #if defined(__WIN32__)
1321 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1322 #elif defined(__unix__) || defined(__APPLE__)
1323 //if(previous) return true;
1324 // printf("Making SYSTEM current\n");
1325 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1326 //previous = oglSystem.glContext;
1332 void UnlockSystem(DisplaySystem displaySystem)
1334 if(useSingleGLContext) return;
1335 #if defined(__WIN32__)
1336 wglMakeCurrent(null, null);
1337 #elif defined(__unix__) || defined(__APPLE__)
1338 // printf("Making NULL current\n");
1339 #if defined(__ANDROID__)
1341 glXMakeCurrent(xGlobalDisplay, None, null);
1347 bool Lock(Display display)
1349 #if !defined(__ANDROID__)
1350 OGLDisplay oglDisplay = display.driverData;
1351 if(useSingleGLContext) return true;
1352 #if defined(__WIN32__)
1353 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1354 #elif defined(__unix__) || defined(__APPLE__)
1355 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1356 // printf(" Making DISPLAY current\n");
1357 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1363 void Unlock(Display display)
1365 if(useSingleGLContext) return;
1366 //printf(" Making NULL current\n");
1367 //glXMakeCurrent(xGlobalDisplay, None, null);
1369 LockSystem(display.displaySystem);
1372 void DestroyDisplay(Display display)
1374 OGLDisplay oglDisplay = display.driverData;
1378 #if defined(__WIN32__)
1379 wglMakeCurrent( null, null );
1382 wglDeleteContext(oglDisplay.glrc);
1384 if(oglDisplay.hdc && oglDisplay.pBuffer)
1385 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1387 if(oglDisplay.pBuffer)
1388 wglDestroyPbufferARB(oglDisplay.pBuffer);
1391 ReleaseDC(display.window, oglDisplay.hdc);
1393 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1394 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1396 #elif defined(__unix__) || defined(__APPLE__)
1397 #if defined(__ANDROID__)
1399 if(oglDisplay.shapePixmap)
1400 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1401 if(oglDisplay.pixmap)
1402 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1403 if(oglDisplay.image)
1405 if(oglDisplay.shminfoShape.shmid != -1)
1407 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1408 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1409 shmdt(oglDisplay.shminfo.shmaddr);
1410 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1413 if(oglDisplay.shapeImage)
1415 if(oglDisplay.shminfoShape.shmid != -1)
1417 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1418 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1419 shmdt(oglDisplay.shminfoShape.shmaddr);
1420 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1422 XDestroyImage(oglDisplay.shapeImage);
1423 oglDisplay.shapeImage = None;
1426 glXMakeCurrent(xGlobalDisplay, None, null);
1428 if(oglDisplay.glContext)
1429 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1432 delete oglDisplay.flippingBuffer;
1434 display.driverData = null;
1438 void ::CheckExtensions(OGLSystem oglSystem)
1440 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1442 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1443 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1446 bool CreateDisplaySystem(DisplaySystem displaySystem)
1448 bool result = false;
1449 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1452 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1454 oglSystem.hdc = GetDC(oglSystem.hwnd);
1458 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1459 oglSystem.pfd.nVersion = 1;
1460 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1461 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1462 oglSystem.pfd.cColorBits = 24;
1463 oglSystem.pfd.cAlphaBits = 8;
1464 oglSystem.pfd.cDepthBits = 24;
1465 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1467 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1468 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1470 if(oglSystem.pfd.cColorBits > 8)
1472 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1473 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1476 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1478 // Get Pointers To The GL Functions
1479 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1480 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1481 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1482 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1483 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1484 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1485 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1486 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1487 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1488 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1489 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1490 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1492 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1493 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1494 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1495 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1496 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1497 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1498 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1499 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1500 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1502 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1504 vboAvailable = glBindBufferARB != null;
1506 // eSystem_LoggingMode(LOG_MSGBOX, null);
1508 if(wglChoosePixelFormatARB)
1513 float fAttributes[] = {0,0};
1516 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1517 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1518 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1519 WGL_COLOR_BITS_ARB,24,
1520 WGL_ALPHA_BITS_ARB,8,
1521 WGL_DEPTH_BITS_ARB,16,
1522 WGL_STENCIL_BITS_ARB,0,
1523 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1524 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1525 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1529 //Log("Found wglChoosePixelFormatARB\n");
1531 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1532 if(!valid || !numFormats)
1534 //Log("Can't find 4x multi sampling\n");
1535 iAttributes[19] = 2;
1536 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1537 if(!valid || !numFormats)
1539 // Log("Can't find 2x multi sampling\n");
1540 iAttributes[16] = 0;
1541 iAttributes[17] = 0;
1542 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1545 if(valid && numFormats)
1547 oglSystem.format = pixelFormat;
1548 wglMakeCurrent(null, null);
1549 wglDeleteContext(oglSystem.glrc);
1551 // *** DescribePixelFormat does not support WGL pixel formats! ***
1552 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1553 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1554 //Log("Successfully set pixel format\n");
1556 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1557 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1561 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1565 CheckExtensions(oglSystem);
1567 wglMakeCurrent(null, null);
1569 //eSystem_DumpErrors(true);
1573 #elif defined(__unix__) || defined(__APPLE__)
1574 vboAvailable = true;
1575 #if defined(__ANDROID__)
1576 egl_init_display(guiApp.desktop.windowHandle);
1577 CheckExtensions(oglSystem);
1581 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1582 XSetWindowAttributes attr;
1587 #ifndef ECERE_MINIGLX
1588 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1591 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1595 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1596 attr.background_pixel = 0;
1597 attr.border_pixel = 0;
1598 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1599 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1600 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1602 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1603 oglSystem.visualInfo->visual, mask, &attr );
1605 if(oglSystem.visualInfo)
1607 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1608 if(oglSystem.glContext)
1610 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1612 CheckExtensions(oglSystem);
1613 glXMakeCurrent(xGlobalDisplay, None, null);
1620 displaySystem.flags.alpha = true;
1621 displaySystem.flags.flipping = true;
1622 displaySystem.pixelFormat = pixelFormat888;
1626 void DestroyDisplaySystem(DisplaySystem displaySystem)
1628 OGLSystem oglSystem = displaySystem.driverData;
1630 #if defined(__WIN32__)
1631 wglMakeCurrent( null, null );
1634 wglDeleteContext(oglSystem.glrc);
1637 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1638 DestroyWindow(oglSystem.hwnd);
1640 #elif defined(__unix__) || defined(__APPLE__)
1641 #if defined(__ANDROID__)
1644 if(oglSystem.visualInfo)
1646 #ifdef ECERE_MINIGLX
1647 __miniglx_XFree(oglSystem.visualInfo);
1649 XFree(oglSystem.visualInfo);
1653 if(oglSystem.glxDrawable)
1655 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1656 oglSystem.glxDrawable = 0;
1663 bool CreateDisplay(Display display)
1665 bool result = false;
1666 OGLDisplay oglDisplay = display.driverData;
1667 #if !defined(__ANDROID__)
1668 OGLSystem oglSystem = display.displaySystem.driverData;
1671 oglDisplay = display.driverData = OGLDisplay { };
1672 //printf("Inside CreateDisplay\n");
1674 #if defined(__WIN32__) || defined(USEPBUFFER)
1675 if(!display.alphaBlend)
1678 #if defined(__WIN32__)
1679 oglDisplay.hdc = GetDC(display.window);
1680 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1681 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1683 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1684 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1688 ReleaseDC(display.window, oglDisplay.hdc);
1689 #elif defined(__unix__) || defined(__APPLE__)
1690 #if defined(__ANDROID__)
1692 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1694 #if defined(__APPLE__)
1695 XVisualInfo template = { 0 };
1696 XWindowAttributes winAttr;
1698 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1699 template.visualid = XVisualIDFromVisual(winAttr.visual);
1700 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1702 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1703 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1704 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1705 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1707 // visualInfo = oglSystem.visualInfo;
1712 //printf("visualInfo is not null\n");
1713 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1714 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1715 //XFree(visualInfo);
1718 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1719 if(oglDisplay.glContext)
1721 //printf("CreateDisplay Got a Context\n");
1722 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1728 #if defined(__WIN32__) || defined(USEPBUFFER)
1734 #if defined(__WIN32__)
1735 if(glBlendFuncSeparate)
1736 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1738 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1740 #if !defined(__OLDX__)
1741 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1743 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1748 glMatrixMode(GL_MODELVIEW);
1749 glScaled(1.0, 1.0, -1.0);
1750 // glTranslatef(0.375f, 0.375f, 0.0f);
1751 // glTranslatef(-0.625f, -0.625f, 0.0f);
1752 glMatrixMode(GL_PROJECTION);
1753 glShadeModel(GL_FLAT);
1755 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1756 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1757 glFogi(GL_FOG_MODE, GL_EXP);
1758 glFogf(GL_FOG_DENSITY, 0);
1759 glEnable(GL_NORMALIZE);
1760 glDepthFunc(GL_LESS);
1762 glDisable(GL_MULTISAMPLE_ARB);
1764 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1765 display.ambient = Color { 50,50,50 };
1768 if(!useSingleGLContext)
1770 #if defined(__WIN32__)
1771 wglMakeCurrent(null, null);
1772 #elif defined(__unix__) || defined(__APPLE__)
1773 #if defined(__ANDROID__)
1776 glXMakeCurrent(xGlobalDisplay, None, null);
1784 bool DisplaySize(Display display, int width, int height)
1786 OGLDisplay oglDisplay = display.driverData;
1788 bool result = false;
1790 //printf("Inside DisplaySize\n");
1791 #if defined(__WIN32__) || defined(USEPBUFFER)
1792 OGLSystem oglSystem = display.displaySystem.driverData;
1793 if(display.alphaBlend)
1795 #if defined(__WIN32__)
1796 const int attributes[]=
1798 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1799 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1801 int pixelFormat = 0;
1802 if(wglChoosePixelFormatARB)
1806 float fAttributes[] = {0,0};
1809 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1810 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1811 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1812 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1813 WGL_COLOR_BITS_ARB,24,
1814 WGL_ALPHA_BITS_ARB,8,
1815 WGL_DEPTH_BITS_ARB,16,
1816 WGL_STENCIL_BITS_ARB,0,
1817 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1818 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1819 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1823 //Log("Found wglChoosePixelFormatARB\n");
1825 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1826 if(!valid || !numFormats)
1828 //Log("Can't find 4x multi sampling\n");
1829 iAttributes[19] = 2;
1830 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1831 if(!valid || !numFormats)
1833 // Log("Can't find 2x multi sampling\n");
1834 iAttributes[16] = 0;
1835 iAttributes[17] = 0;
1836 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1837 if(!valid || !numFormats)
1841 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1842 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1843 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1844 WGL_COLOR_BITS_ARB,24,
1845 WGL_ALPHA_BITS_ARB,8,
1846 WGL_DEPTH_BITS_ARB,16,
1849 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1853 if(valid && numFormats)
1855 wglMakeCurrent(null, null);
1859 wglMakeCurrent( null, null );
1860 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1861 if(oglDisplay.hdc && oglDisplay.pBuffer)
1862 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1864 wglDestroyPbufferARB(oglDisplay.pBuffer);
1866 if(!useSingleGLContext)
1867 wglMakeCurrent( null, null );
1870 wglDeleteContext(oglDisplay.glrc);
1872 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1873 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1874 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1877 HDC hdc = GetDC(display.window);
1879 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1880 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1882 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1883 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1885 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1887 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1891 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1892 oglDisplay.memDC = CreateCompatibleDC(hdc);
1893 SetMapMode(oglDisplay.memDC, MM_TEXT);
1894 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1895 info->bmiHeader.biPlanes = 1;
1896 info->bmiHeader.biCompression = BI_RGB;
1897 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1898 info->bmiHeader.biWidth = width;
1899 info->bmiHeader.biHeight = height;
1900 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1903 SelectObject(oglDisplay.memDC, newBitmap);
1904 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1907 PIXELFORMATDESCRIPTOR pfd = { 0 };
1908 pfd.nSize = (short)sizeof(pfd);
1910 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1911 pfd.iPixelType = PFD_TYPE_RGBA;
1912 pfd.cColorBits = 32;
1913 //pfd.cAlphaBits = 8;
1914 pfd.cDepthBits = 24;
1915 pfd.iLayerType = PFD_MAIN_PLANE;
1917 oglDisplay.hdc = oglDisplay.memDC;
1919 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1920 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1921 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1923 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1924 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1925 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1930 const int imageSize = width * height * 4;
1932 glGenBuffersARB(2, oglDisplay.imageBuffers);
1934 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1935 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1936 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1937 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1940 oglDisplay.memBitmap = newBitmap;
1941 oglDisplay.stride = width;
1947 ReleaseDC(display.window, hdc);
1949 #elif defined(__unix__) || defined(__APPLE__)
1950 #if defined(__ANDROID__)
1955 GLX_DOUBLEBUFFER, True,
1961 GLX_STENCIL_SIZE, 1,
1962 //GLX_DEPTH_SIZE, 24,
1963 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1964 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1970 GLX_PBUFFER_WIDTH, width,
1971 GLX_PBUFFER_HEIGHT, height,
1972 GLX_LARGEST_PBUFFER, False,
1976 // choose a pixel format that meets our minimum requirements
1979 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1982 if(oglDisplay.pixmap)
1984 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1985 oglDisplay.pixmap = None;
1987 if(oglDisplay.shapePixmap)
1989 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1990 oglDisplay.shapePixmap = None;
1993 // Free Shared Memory Pixmap
1994 if(oglDisplay.image)
1996 if(oglDisplay.shminfoShape.shmid != -1)
1998 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1999 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2000 shmdt(oglDisplay.shminfo.shmaddr);
2001 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
2003 XDestroyImage(oglDisplay.image);
2004 oglDisplay.image = None;
2006 if(oglDisplay.shapeImage)
2008 if(oglDisplay.shminfoShape.shmid != -1)
2010 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
2011 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2012 shmdt(oglDisplay.shminfoShape.shmaddr);
2013 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
2015 XDestroyImage(oglDisplay.shapeImage);
2016 oglDisplay.shapeImage = None;
2019 if(oglDisplay.windowPicture)
2020 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
2021 if(oglDisplay.pixmapPicture)
2022 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
2024 if(oglDisplay.pixmap)
2025 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2027 if(oglDisplay.glContext)
2028 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
2029 if(oglDisplay.pBuffer)
2030 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
2032 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
2033 if(oglDisplay.pBuffer)
2035 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2036 if(oglDisplay.glContext)
2038 glXMakeCurrent(xGlobalDisplay, None, null);
2039 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2041 // Initialize Shared Memory Pixmap
2042 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2043 ZPixmap, null, &oglDisplay.shminfo, width, height);
2044 if(oglDisplay.image)
2046 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2047 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2048 if(oglDisplay.shminfo.shmid != -1)
2050 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2051 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2053 oglDisplay.shminfo.readOnly = False;
2054 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2056 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2057 &oglDisplay.shminfo, width, height, 32);
2059 // Initialize Shared Memory Shape Pixmap
2060 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2061 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2062 if(oglDisplay.shapeImage)
2064 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2065 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2066 if(oglDisplay.shminfoShape.shmid != -1)
2068 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2069 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2071 oglDisplay.shminfoShape.readOnly = False;
2072 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2074 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2075 &oglDisplay.shminfoShape, width, height, 1);
2076 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2079 XRenderPictureAttributes attributes = { 0 };
2080 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2081 #if !defined(__APPLE__) && !defined(__OLDX__)
2082 attributes.repeat = RepeatNormal;
2084 attributes.repeat = 1;
2086 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2087 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2088 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2089 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2092 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2093 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2110 CreateDisplay(display);
2111 #if defined(__WIN32__)
2112 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2113 #elif defined(__unix__) || defined(__APPLE__)
2114 #if defined(__ANDROID__)
2118 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2125 if(!result && display.alphaBlend)
2127 printf("Alpha blending windows not supported on this display\n");
2134 glViewport(0,0,width,height);
2136 glOrtho(0,width,height,0,0.0,1.0);
2137 displayWidth = display.width = width;
2138 displayHeight = display.height = height;
2140 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2142 oglDisplay.flipBufW = width;
2143 oglDisplay.flipBufH = height;
2147 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2150 if(oglDisplay.flippingBuffer || !width || !height)
2156 void DisplayPosition(Display display, int x, int y)
2158 OGLDisplay oglDisplay = display.driverData;
2164 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2168 void RestorePalette(Display display)
2172 void StartUpdate(Display display)
2176 void EndUpdate(Display display)
2180 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2184 void Update(Display display, Box updateBox)
2186 #if defined(__WIN32__) || defined(USEPBUFFER)
2187 OGLDisplay oglDisplay = display.driverData;
2189 //Logf("DisplayScreen\n");
2193 #if defined(__WIN32__) || defined(USEPBUFFER)
2194 if(display.alphaBlend)
2196 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2197 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2198 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2199 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2200 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2203 #if defined(__WIN32__)
2205 POINT point = { oglDisplay.x, oglDisplay.y};
2206 POINT srcPoint = { 0, 0 };
2207 BLENDFUNCTION blend = { 0 };
2209 size.cx = display.width;
2210 size.cy = display.height;
2211 blend.BlendOp = AC_SRC_OVER;
2212 blend.BlendFlags = 0;
2213 blend.SourceConstantAlpha = 255;
2214 blend.AlphaFormat = AC_SRC_ALPHA;
2217 // Process partial images. Mapping the buffer waits for
2218 // outstanding DMA transfers into the buffer to finish.
2219 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2220 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2222 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2223 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2226 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2227 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2230 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2233 // Unmap the image buffers
2234 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2235 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2237 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2238 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2240 // Bind two different buffer objects and start the glReadPixels
2241 // asynchronously. Each call will return directly after
2242 // starting the DMA transfer.
2243 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2244 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2246 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2247 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2251 #elif defined(__unix__) || defined(__APPLE__)
2252 #if defined(__ANDROID__)
2254 XTransform transform =
2257 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2258 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2259 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2262 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2263 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2264 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2265 #if !defined(__APPLE__) && !defined(__OLDX__)
2266 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2268 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2270 XFlush(xGlobalDisplay);
2278 #if defined(__WIN32__)
2279 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2280 SwapBuffers(oglDisplay.hdc);
2281 #elif defined(__unix__) || defined(__APPLE__)
2282 #if defined(__ANDROID__)
2283 eglSwapBuffers(eglDisplay, eglSurface);
2285 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2289 //Logf("Out of DisplayScreen\n");
2292 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2294 if(bitmap.driverData)
2296 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2297 glDeleteTextures(1, &tex);
2298 bitmap.driverData = 0;
2300 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2303 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2305 OGLSystem oglSystem = displaySystem.driverData;
2306 bool result = false;
2308 GLuint glBitmap = 0;
2310 uint w = width, h = height;
2311 if(oglSystem.pow2textures)
2316 w = Min(w, oglSystem.maxTextureSize);
2317 h = Min(h, oglSystem.maxTextureSize);
2319 glGenTextures(1, &glBitmap);
2320 glBindTexture(GL_TEXTURE_2D, glBitmap);
2322 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2330 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2332 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2334 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2335 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2339 bitmap.driverData = (void *)(uintptr)glBitmap;
2340 bitmap.driver = displaySystem.driver;
2348 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2350 bool result = false;
2351 OGLSystem oglSystem = displaySystem.driverData;
2352 Bitmap convBitmap = bitmap;
2356 convBitmap.Copy(bitmap);
2359 // Pre process the bitmap... First make it 32 bit
2360 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
2363 uint w = bitmap.width, h = bitmap.height;
2364 GLuint glBitmap = 0;
2365 if(oglSystem.pow2textures)
2370 w = Min(w, oglSystem.maxTextureSize);
2371 h = Min(h, oglSystem.maxTextureSize);
2375 while(w * 2 < h) w *= 2;
2376 while(h * 2 < w) h *= 2;
2379 // Switch ARGB to RGBA
2380 //if(bitmap.format != pixelFormatRGBA)
2382 for(c=0; c<bitmap.size; c++)
2384 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2386 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
2387 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2390 // convBitmap.pixelFormat = pixelFormat888;
2393 glGenTextures(1, &glBitmap);
2396 //int error = glGetError();
2400 glBindTexture(GL_TEXTURE_2D, glBitmap);
2401 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2405 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2407 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2408 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2411 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2413 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2417 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2420 if(bitmap.width != w || bitmap.height != h)
2422 mipMap = Bitmap { };
2423 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
2425 Surface mipSurface = mipMap.GetSurface(0,0,null);
2426 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
2436 mipMap = convBitmap;
2443 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2444 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2445 //printf("Calling glTexImage2D\n");
2446 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2447 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2448 if((error = glGetError()))
2450 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2451 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2455 if(mipMap != convBitmap)
2460 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
2461 bitmap.driverData = (void *)(uintptr)glBitmap;
2462 bitmap.driver = displaySystem.driver;
2465 FreeBitmap(displaySystem, bitmap);
2466 else if(oglSystem.loadingFont)
2468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2469 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2470 oglSystem.loadingFont = false;
2476 void ReleaseSurface(Display display, Surface surface)
2478 glDisable(GL_SCISSOR_TEST);
2479 delete surface.driverData;
2480 surface.driverData = null;
2483 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2488 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2490 bool result = false;
2491 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2493 //Logf("GetSurface\n");
2497 if(displayWidth != display.width || displayHeight != display.height)
2499 displayWidth = display.width;
2500 displayHeight = display.height;
2502 glViewport(0,0,display.width,display.height);
2504 glOrtho(0,display.width,display.height,0,0.0,1.0);
2507 surface.offset.x = x;
2508 surface.offset.y = y;
2509 surface.unclippedBox = surface.box = clip;
2510 oglSurface.bitmapMult[0] = 1;
2511 oglSurface.bitmapMult[1] = 1;
2512 oglSurface.bitmapMult[2] = 1;
2513 oglSurface.bitmapMult[3] = 1;
2515 glEnable(GL_SCISSOR_TEST);
2518 (display.height) -(y+clip.bottom)-1,
2519 clip.right-clip.left+1,
2520 clip.bottom-clip.top+1);
2526 void Clip(Display display, Surface surface, Box clip)
2535 box.Clip(surface.unclippedBox);
2539 box = surface.box = surface.unclippedBox;
2540 box.left += surface.offset.x;
2541 box.top += surface.offset.y;
2542 box.right+= surface.offset.x;
2543 box.bottom += surface.offset.y;
2546 box.left,display.height - box.bottom - 1,
2547 box.right-box.left+1, box.bottom-box.top+1);
2550 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2552 bool result = false;
2553 OGLDisplay oglDisplay = display.driverData;
2554 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2556 if(oglDisplay.flippingBuffer)
2558 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2561 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2567 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2568 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2569 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2570 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2571 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2574 for(row = 0; row<h; row++)
2575 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2582 void SetForeground(Display display, Surface surface, ColorAlpha color)
2584 OGLSurface oglSurface = surface.driverData;
2586 //Logf("SetForeground\n");
2588 oglSurface.foreground[0] = color.color.r/255.0f;
2589 oglSurface.foreground[1] = color.color.g/255.0f;
2590 oglSurface.foreground[2] = color.color.b/255.0f;
2591 //oglSurface.foreground[3] = 1.0f;
2592 oglSurface.foreground[3] = color.a/255.0f;
2594 //if(!oglSurface.foreground[3])printf("bug");
2597 void SetBackground(Display display, Surface surface, ColorAlpha color)
2599 OGLSurface oglSurface = surface.driverData;
2601 //Logf("SetBackground\n");
2603 oglSurface.background[0] = color.color.r/255.0f;
2604 oglSurface.background[1] = color.color.g/255.0f;
2605 oglSurface.background[2] = color.color.b/255.0f;
2606 //oglSurface.background[3] = 1.0;
2607 oglSurface.background[3] = color.a/255.0f;
2610 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2612 OGLSurface oglSurface = surface.driverData;
2614 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2615 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2616 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2617 oglSurface.bitmapMult[3] = color.a/255.0f;
2620 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2625 void PutPixel(Display display, Surface surface,int x,int y)
2627 OGLSurface oglSurface = surface.driverData;
2629 //Logf("PutPixel\n");
2631 glColor4fv(oglSurface.foreground);
2633 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2634 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2639 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2641 OGLSurface oglSurface = surface.driverData;
2642 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2657 x1 += surface.offset.x;
2658 y1 += surface.offset.y;
2659 x2 += surface.offset.x;
2660 y2 += surface.offset.y;
2664 glColor4fv(oglSurface.foreground);
2669 glTexCoord2f(0.5f, 0);
2670 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2671 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2672 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2681 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2682 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2688 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2690 OGLSurface oglSurface = surface.driverData;
2691 x1 += surface.offset.x;
2692 y1 += surface.offset.y;
2693 x2 += surface.offset.x;
2694 y2 += surface.offset.y;
2696 //Logf("Rectangle\n");
2698 glColor4fv(oglSurface.foreground);
2704 glTexCoord2f(0.5f, 0);
2705 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2706 glTexCoord2f(y2-y1 + 0.5f, 0);
2707 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2709 glTexCoord2f(0.5f, 0);
2710 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2711 glTexCoord2f(x2 - x1 + 0.5f, 0);
2712 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2714 glTexCoord2f(0.5f, 0);
2715 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2716 glTexCoord2f(y1 - y2 + 0.5f, 0);
2717 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2719 glTexCoord2f(0.5f, 0);
2720 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2721 glTexCoord2f(x1 - x2 + 0.5f, 0);
2722 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2727 glBegin(GL_LINE_LOOP);
2734 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2735 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2736 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2737 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2742 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2744 OGLSurface oglSurface = surface.driverData;
2747 glColor4fv(oglSurface.background);
2748 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2749 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2752 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2753 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2757 void Clear(Display display, Surface surface, ClearType type)
2759 OGLDisplay oglDisplay = display.driverData;
2760 OGLSurface oglSurface = surface.driverData;
2763 if(type != depthBuffer)
2764 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2765 if(type != colorBuffer && !oglDisplay.depthWrite)
2767 glDepthMask((byte)bool::true);
2769 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2770 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2771 if(type != colorBuffer && !oglDisplay.depthWrite)
2773 glDepthMask((byte)bool::false);
2777 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2782 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2784 OGLSurface oglSurface = surface.driverData;
2786 #if !defined(__OLDX__)
2787 // WHY DO WE HAVE GL_ONE HERE ?
2788 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2789 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2792 if(!oglSurface.writingText)
2794 // glTranslatef(-0.375f, -0.375f, 0.0f);
2795 glEnable(GL_TEXTURE_2D);
2796 glColor4fv(oglSurface.bitmapMult);
2798 else if(oglSurface.xOffset)
2799 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2801 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2806 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2807 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2808 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2809 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2810 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2811 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2812 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2813 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2818 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2819 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2820 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2821 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2822 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2823 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2824 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2825 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2828 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2829 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2830 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2831 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2832 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2833 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2834 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2835 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2839 if(!oglSurface.writingText)
2841 glDisable(GL_TEXTURE_2D);
2843 //glTranslate(0.375, 0.375, 0.0);
2845 else if(oglSurface.xOffset)
2846 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2848 #if !defined(__OLDX__)
2849 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2850 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2854 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2856 OGLSurface oglSurface = surface.driverData;
2858 //glTranslate(-0.375, -0.375, 0.0);
2860 //Logf("Stretch\n");
2862 #if !defined(__OLDX__)
2863 /*if(glBlendFuncSeparate)
2864 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2867 glEnable(GL_TEXTURE_2D);
2868 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2870 glColor4fv(oglSurface.bitmapMult);
2876 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2877 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2879 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2880 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2882 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2883 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2885 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2886 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2890 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2891 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2893 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2894 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2896 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2897 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2899 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2900 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2905 glDisable(GL_TEXTURE_2D);
2907 //glTranslate(0.375, 0.375, 0.0);
2908 #if !defined(__OLDX__)
2909 /*if(glBlendFuncSeparate)
2910 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2915 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2917 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2920 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2922 float s2dw,s2dh,d2sw,d2sh;
2923 //bool flipX = false, flipY = false;
2925 //Logf("StretchDI\n");
2927 if(Sgn(w) != Sgn(sw))
2933 if(Sgn(h) != Sgn(sh))
2945 //Clip against the edges of the source
2948 dx+=(int)((0-sx) * s2dw);
2949 w-=(int)((0-sx) * s2dw);
2955 dy+=(int)((0-sy) * s2dh);
2956 h-=(int)((0-sy) * s2dh);
2961 if(sx+sw>bitmap.width-1)
2963 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2964 sw-=sx+sw-(bitmap.width-1)-1;
2966 if(sy+sh>(bitmap.height-1))
2968 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2969 sh-=sy+sh-(bitmap.height-1)-1;
2971 //Clip against the edges of the surfaceination
2972 if(dx<surface.box.left)
2975 sx+=(int)((surface.box.left-dx)*d2sw);
2976 sw-=(int)((surface.box.left-dx)*d2sw);
2977 w-=surface.box.left-dx;
2978 dx=surface.box.left;
2980 if(dy<surface.box.top)
2982 sy+=(int)((surface.box.top-dy)*d2sh);
2983 sh-=(int)((surface.box.top-dy)*d2sh);
2984 h-=surface.box.top-dy;
2987 if(dx+w>surface.box.right)
2989 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2990 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2991 w-=dx+w-surface.box.right-1;
2993 if(dy+h>surface.box.bottom)
2995 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2996 h-=dy+h-surface.box.bottom-1;
2998 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
3000 dx += surface.offset.x;
3001 dy += surface.offset.y;
3003 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3005 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3006 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3007 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3008 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3009 glRasterPos2d(dx,dy);
3010 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
3011 glPixelZoom(s2dw, -s2dh);
3012 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3013 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3014 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3015 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3016 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3020 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3024 //Clip against the edges of the source
3037 if(sx+w>bitmap.width-1)
3038 w-=sx+w-(bitmap.width-1)-1;
3039 if(sy+h>bitmap.height-1)
3040 h-=sy+h-(bitmap.height-1)-1;
3041 //Clip against the edges of the surfaceination
3042 if(dx<surface.box.left)
3045 sx+=surface.box.left-dx;
3046 w-=surface.box.left-dx;
3047 dx=surface.box.left;
3049 if(dy<surface.box.top)
3051 sy+=surface.box.top-dy;
3052 h-=surface.box.top-dy;
3055 if(dx+w>surface.box.right)
3057 //if(flip) sx+=dx+w-surface.box.right-1;
3058 w-=dx+w-surface.box.right-1;
3060 if(dy+h>surface.box.bottom)
3061 h-=dy+h-surface.box.bottom-1;
3065 dx += surface.offset.x;
3066 dy += surface.offset.y;
3068 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3070 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3071 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3072 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3073 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3074 glRasterPos2d(dx,dy);
3076 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3077 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3078 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3079 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3080 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3084 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3086 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3089 void UnloadFont(DisplaySystem displaySystem, Font font)
3091 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3094 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
3097 OGLSystem oglSystem = displaySystem.driverData;
3098 oglSystem.loadingFont = true;
3099 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3103 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
3105 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3108 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
3110 OGLSurface oglSurface = surface.driverData;
3111 OGLSystem oglSystem = display.displaySystem.driverData;
3112 oglSystem.loadingFont = true;
3114 //glTranslated(-0.375, -0.375, 0.0);
3118 if(surface.textOpacity)
3121 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3122 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3125 oglSurface.writingText = true;
3127 glEnable(GL_TEXTURE_2D);
3129 if(surface.outline.size)
3131 ColorAlpha outlineColor = surface.outline.color;
3132 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
3133 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3134 //glEnable(GL_BLEND);
3136 oglSurface.writingOutline = true;
3137 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3138 oglSurface.writingOutline = false;
3140 glColor4fv(oglSurface.foreground);
3142 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3143 oglSurface.writingText = false;
3144 oglSystem.loadingFont = false;
3146 glDisable(GL_TEXTURE_2D);
3148 //glTranslated(0.375, 0.375, 0.0);
3151 void TextFont(Display display, Surface surface, Font font)
3153 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3156 void TextOpacity(Display display, Surface surface, bool opaque)
3158 OGLSurface oglSurface = surface.driverData;
3159 oglSurface.opaqueText = opaque;
3162 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
3164 OGLSurface oglSurface = surface.driverData;
3165 OGLSystem oglSystem = display.displaySystem.driverData;
3166 oglSystem.loadingFont = true;
3167 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3168 oglSystem.loadingFont = false;
3171 void DrawingChar(Display display, Surface surface, char character)
3176 void LineStipple(Display display, Surface surface, uint32 stipple)
3178 //Logf("Stipple\n");
3182 #if defined(__ANDROID__)
3183 stippleEnabled = true;
3184 glesLineStipple(1, (uint16)stipple);
3186 glLineStipple(1, (uint16)stipple);
3187 glEnable(GL_LINE_STIPPLE);
3192 #if defined(__ANDROID__)
3193 stippleEnabled = false;
3194 glMatrixMode(GL_TEXTURE);
3196 glMatrixMode(GL_PROJECTION);
3197 glDisable(GL_TEXTURE_2D);
3199 glDisable(GL_LINE_STIPPLE);
3203 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3204 void SetRenderState(Display display, RenderState state, uint value)
3206 OGLDisplay oglDisplay = display.driverData;
3207 //Logf("RenderState\n");
3213 glEnable(GL_MULTISAMPLE_ARB);
3215 glDisable(GL_MULTISAMPLE_ARB);
3218 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3221 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3224 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3225 oglDisplay.depthWrite = (bool)value;
3229 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3230 glFogfv(GL_FOG_COLOR, (float *)&color);
3234 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
3237 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3241 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3242 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3247 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3252 #if defined(__WIN32__)
3253 wglSwapIntervalEXT(value ? 1 : 0);
3260 void SetLight(Display display, int id, Light light)
3262 //Logf("SetLight\n");
3266 Object lightObject = light.lightObject;
3267 float position[4] = { 0, 0, 0, 0 };
3268 float color[4] = { 0, 0, 0, 1 };
3270 glEnable(GL_LIGHT0 + id);
3272 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3273 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3274 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3277 if(!light.multiplier) light.multiplier = 1.0f;
3279 color[0] = light.diffuse.r * light.multiplier;
3280 color[1] = light.diffuse.g * light.multiplier;
3281 color[2] = light.diffuse.b * light.multiplier;
3282 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3284 color[0] = light.ambient.r * light.multiplier;
3285 color[1] = light.ambient.g * light.multiplier;
3286 color[2] = light.ambient.b * light.multiplier;
3287 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3288 color[0] = light.specular.r * light.multiplier;
3289 color[1] = light.specular.g * light.multiplier;
3290 color[2] = light.specular.b * light.multiplier;
3291 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3295 Vector3D positionVector;
3296 if(light.flags.spot)
3298 if(lightObject.flags.root || !lightObject.parent)
3300 positionVector = lightObject.transform.position;
3301 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3305 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3306 if(display.display3D.camera)
3307 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3313 if(!light.direction.x && !light.direction.y && !light.direction.z)
3315 Vector3Df vector { 0,0,-1 };
3317 mat.RotationQuaternion(light.orientation);
3318 positionVector.MultMatrixf(vector, mat);
3322 positionVector = light.direction;
3327 position[0] = (float)positionVector.x;
3328 position[1] = (float)positionVector.y;
3329 position[2] = (float)positionVector.z;
3331 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3334 // Display Light Position
3335 glDisable(GL_LIGHTING);
3336 glDisable(GL_DEPTH_TEST);
3340 glVertex3fv(position);
3342 glEnable(GL_DEPTH_TEST);
3343 glEnable(GL_LIGHTING);
3347 if(lightObject.flags.root || !lightObject.parent)
3349 positionVector = light.target.transform.position;
3350 positionVector.Subtract(positionVector, display.camera.cPosition);
3354 positionVector.MultMatrix(light.target.transform.position,
3355 lightObject.light.target.parent.matrix);
3356 positionVector.Subtract(positionVector, display.camera.cPosition);
3359 position[0] = positionVector.x;
3360 position[1] = positionVector.y;
3361 position[2] = positionVector.z;
3363 glDisable(GL_LIGHTING);
3364 glDisable(GL_DEPTH_TEST);
3368 glVertex3fv(position);
3370 glEnable(GL_DEPTH_TEST);
3371 glEnable(GL_LIGHTING);
3374 if(light.flags.attenuation)
3376 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3377 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3378 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3381 if(light.flags.spot)
3384 #define MAXLIGHT 0.9
3385 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3386 // Figure out exponent out of the hot spot
3387 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3389 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3390 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3391 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3397 Vector3Df vector { 0,0,-1 };
3398 Vector3Df direction;
3401 mat.RotationQuaternion(light.orientation);
3402 direction.MultMatrix(vector, mat);
3404 position[0] = direction.x;
3405 position[1] = direction.y;
3406 position[2] = direction.z;
3408 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3412 glDisable(GL_LIGHT0 + id);
3415 void SetCamera(Display display, Surface surface, Camera camera)
3417 OGLDisplay oglDisplay = display.driverData;
3418 //Logf("SetCamera\n");
3422 int left = surface.box.left + surface.offset.x;
3423 int top = surface.box.top + surface.offset.y;
3424 int right = surface.box.right + surface.offset.x;
3425 int bottom = surface.box.bottom + surface.offset.y;
3426 float origX = surface.offset.x + camera.origin.x;
3427 float origY = surface.offset.y + camera.origin.y;
3429 int y = display.height - bottom - 1;
3430 int w = right - left + 1;
3431 int h = bottom - top + 1;
3434 glViewport(x, y, w, h);
3436 // *** Projection Matrix ***
3437 if(!display.display3D.camera)
3440 glMatrixMode(GL_PROJECTION);
3441 if(display.display3D.collectingHits)
3443 float pickX = display.display3D.pickX + surface.offset.x;
3444 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3448 w / display.display3D.pickWidth, 0, 0, 0,
3449 0, h / display.display3D.pickHeight, 0, 0,
3451 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3452 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3455 glLoadMatrixd(pickMatrix.array);
3460 (left - origX) * camera.zMin / camera.focalX,
3461 (right - origX) * camera.zMin / camera.focalX,
3462 (bottom - origY) * camera.zMin / camera.focalY,
3463 (top - origY) * camera.zMin / camera.focalY,
3464 camera.zMin, camera.zMax);
3466 glDisable(GL_BLEND);
3468 // *** Z Inverted Identity Matrix ***
3469 glMatrixMode(GL_MODELVIEW);
3470 if(!display.display3D.camera)
3475 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3477 // *** View Matrix ***
3478 glMultMatrixd(camera.viewMatrix.array);
3483 glEnable(GL_DEPTH_TEST);
3484 glEnable(GL_LIGHTING);
3485 glShadeModel(GL_SMOOTH);
3486 glDepthMask((byte)bool::true);
3487 oglDisplay.depthWrite = true;
3489 glEnable(GL_MULTISAMPLE_ARB);
3491 else if(display.display3D.camera)
3493 oglDisplay.depthWrite = false;
3494 glViewport(0,0,display.width,display.height);
3496 glDisable(GL_CULL_FACE);
3497 glDisable(GL_DEPTH_TEST);
3498 glDisable(GL_LIGHTING);
3500 glDisable(GL_TEXTURE_2D);
3501 glShadeModel(GL_FLAT);
3503 glDisable(GL_MULTISAMPLE_ARB);
3505 // *** Restore 2D MODELVIEW Matrix ***
3508 // *** Restore 2D PROJECTION Matrix ***
3509 glMatrixMode(GL_PROJECTION);
3513 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3516 void ApplyMaterial(Display display, Material material, Mesh mesh)
3518 //Logf("ApplyMaterial\n");
3521 if(material.flags.doubleSided)
3523 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3524 glDisable(GL_CULL_FACE);
3528 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3529 glEnable(GL_CULL_FACE);
3533 if(material.flags.noFog)
3539 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
3541 Bitmap map = material.baseMap;
3542 glEnable(GL_TEXTURE_2D);
3543 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3545 glMatrixMode(GL_TEXTURE);
3547 if(material.uScale && material.vScale)
3548 glScalef(material.uScale, material.vScale, 1);
3549 glMatrixMode(GL_MODELVIEW);
3551 if(material.flags.tile)
3553 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3558 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3559 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3563 glDisable(GL_TEXTURE_2D);
3565 if(mesh.flags.colors)
3567 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3568 glEnable(GL_COLOR_MATERIAL);
3572 glDisable(GL_COLOR_MATERIAL);
3574 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3575 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3578 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3579 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3583 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3584 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3587 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3588 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3591 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3594 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3596 OGLMesh oglMesh = mesh.data;
3599 if(!mesh.flags.vertices)
3601 if(oglMesh.vertices)
3603 GLDeleteBuffers(1, &oglMesh.vertices);
3604 oglMesh.vertices = 0;
3606 delete mesh.vertices;
3608 if(!mesh.flags.normals)
3612 GLDeleteBuffers(1, &oglMesh.normals);
3613 oglMesh.normals = 0;
3615 delete mesh.normals;
3617 if(!mesh.flags.texCoords1)
3619 if(oglMesh.texCoords)
3621 GLDeleteBuffers(1, &oglMesh.texCoords);
3622 oglMesh.texCoords = 0;
3624 delete mesh.texCoords;
3626 if(!mesh.flags.texCoords2)
3628 if(oglMesh.texCoords2)
3630 GLDeleteBuffers(1, &oglMesh.texCoords2);
3631 oglMesh.texCoords2 = 0;
3634 delete mesh.texCoords2;
3637 if(!mesh.flags.colors)
3641 GLDeleteBuffers(1, &oglMesh.colors);
3653 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3655 bool result = false;
3658 mesh.data = OGLMesh { };
3661 OGLMesh oglMesh = mesh.data;
3662 if(mesh.nVertices == nVertices)
3664 // Same number of vertices, adding features (Leaves the other features pointers alone)
3665 if(mesh.flags != flags)
3667 if(!mesh.flags.vertices && flags.vertices)
3669 if(flags.doubleVertices)
3671 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3674 mesh.vertices = new Vector3Df[nVertices];
3675 if(!oglMesh.vertices)
3676 GLGenBuffers(1, &oglMesh.vertices);
3678 if(!mesh.flags.normals && flags.normals)
3680 if(flags.doubleNormals)
3682 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3685 mesh.normals = new Vector3Df[nVertices];
3686 if(!oglMesh.normals)
3687 GLGenBuffers( 1, &oglMesh.normals);
3689 if(!mesh.flags.texCoords1 && flags.texCoords1)
3691 mesh.texCoords = new Pointf[nVertices];
3692 if(!oglMesh.texCoords)
3693 GLGenBuffers( 1, &oglMesh.texCoords);
3695 if(!mesh.flags.colors && flags.colors)
3697 mesh.colors = new ColorRGBAf[nVertices];
3699 GLGenBuffers( 1, &oglMesh.colors);
3705 // New number of vertices, reallocate all current and new features
3706 flags |= mesh.flags;
3709 if(flags.doubleVertices)
3711 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3714 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3715 if(!oglMesh.vertices)
3716 GLGenBuffers(1, &oglMesh.vertices);
3720 if(flags.doubleNormals)
3722 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3725 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3726 if(!oglMesh.normals)
3727 GLGenBuffers( 1, &oglMesh.normals);
3729 if(flags.texCoords1)
3731 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3732 if(!oglMesh.texCoords)
3733 GLGenBuffers( 1, &oglMesh.texCoords);
3737 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3739 GLGenBuffers( 1, &oglMesh.colors);
3747 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3749 OGLMesh oglMesh = mesh.data;
3750 if(!flags) flags = mesh.flags;
3754 if(flags.vertices && oglMesh.vertices)
3756 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3757 GLBufferData( mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3760 if(flags.normals && oglMesh.normals)
3762 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3763 GLBufferData( mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
3766 if(flags.texCoords1 && oglMesh.texCoords)
3768 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3769 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3772 if(flags.colors && oglMesh.colors)
3774 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3775 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3778 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3782 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3789 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3793 if(oglIndices.buffer)
3794 GLDeleteBuffers(1, &oglIndices.buffer);
3795 delete oglIndices.indices;
3800 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3802 OGLIndices oglIndices = OGLIndices { };
3805 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3806 GLGenBuffers( 1, &oglIndices.buffer);
3807 oglIndices.nIndices = nIndices;
3812 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3816 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3817 GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3818 oglIndices.indices, GL_STATIC_DRAW_ARB);
3819 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3823 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3826 return oglIndices.indices;
3829 void SelectMesh(Display display, Mesh mesh)
3831 //Logf("SelectMesh\n");
3833 #if !defined( __ANDROID__) && !defined(__APPLE__)
3835 #if defined(__WIN32__)
3836 if(glUnlockArraysEXT)
3838 if(!vboAvailable && display.display3D.mesh)
3839 glUnlockArraysEXT();
3844 OGLMesh oglMesh = mesh.data;
3846 // *** Vertex Stream ***
3847 glEnableClientState(GL_VERTEX_ARRAY);
3848 if(!display.display3D.collectingHits && oglMesh)
3850 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3851 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
3853 // *** Normals Stream ***
3854 if(mesh.normals || mesh.flags.normals)
3856 glEnableClientState(GL_NORMAL_ARRAY);
3857 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3858 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
3861 glDisableClientState(GL_NORMAL_ARRAY);
3863 // *** Texture Coordinates Stream ***
3864 if(mesh.texCoords || mesh.flags.texCoords1)
3866 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3867 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3868 glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
3871 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3873 // *** Color Stream ***
3874 if(mesh.colors || mesh.flags.colors)
3876 glEnableClientState(GL_COLOR_ARRAY);
3877 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3878 glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
3881 glDisableClientState(GL_COLOR_ARRAY);
3886 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3887 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3888 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
3890 glEnableClientState(GL_NORMAL_ARRAY);
3891 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3894 glDisableClientState(GL_NORMAL_ARRAY);
3895 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
3897 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3898 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3901 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3902 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
3904 glEnableClientState(GL_COLOR_ARRAY);
3905 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3908 glDisableClientState(GL_COLOR_ARRAY);
3911 #if !defined(__ANDROID__) && !defined(__APPLE__)
3913 #if defined(__WIN32__)
3917 glLockArraysEXT(0, mesh.nVertices);
3922 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3925 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3927 //Logf("DrawPrimitives\n");
3929 if(primitive->type.vertexRange)
3930 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3933 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3934 // HACK TO SPEED THINGS UP...
3936 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3937 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3940 glBegin(primitiveTypes[primitive->type.primitiveType]);
3943 OGLIndices oglIndices = primitive->data;
3944 MeshFeatures flags = mesh.flags;
3945 for(c = 0; c<primitive->nIndices; c++)
3947 uint16 index = ((uint16 *) oglIndices.indices)[c];
3948 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3949 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3950 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3951 glVertex3fv((float *)&mesh.vertices[index]);
3959 OGLIndices oglIndices = primitive->data;
3961 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
3963 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3964 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3965 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3966 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3969 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3970 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
3975 void PushMatrix(Display display)
3980 void PopMatrix(Display display, bool setMatrix)
3985 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3987 Matrix matrix = transMatrix;
3988 Camera camera = useCamera ? display.display3D.camera : null;
3993 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3998 matrix.m[3][0] - camera.cPosition.x,
3999 matrix.m[3][1] - camera.cPosition.y,
4000 matrix.m[3][2] - camera.cPosition.z);
4012 glMultMatrixd(matrix.array);
4017 public void UseSingleGLContext(bool useSingle)
4019 useSingleGLContext = useSingle;
4022 default dllexport void *
4023 #if defined(__WIN32__)
4024 __attribute__((stdcall))
4026 IS_GLGetContext(DisplaySystem displaySystem)
4030 #if defined(__WIN32__)
4031 OGLSystem system = displaySystem.driverData;
4033 #elif !defined(__ANDROID__)
4034 OGLSystem system = displaySystem.driverData;
4035 return system.glContext;