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 0x0500
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);
328 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
329 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
330 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
331 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
332 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
333 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
334 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
335 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
337 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
338 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
339 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
340 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
341 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
342 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
343 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
344 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
345 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
346 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
347 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
348 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
349 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
351 #ifdef WGL_WGLEXT_PROTOTYPES
352 extern BOOL WINAPI wglSwapIntervalEXT (int);
353 extern int WINAPI wglGetSwapIntervalEXT (void);
354 #endif /* WGL_WGLEXT_PROTOTYPES */
355 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
356 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
358 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
362 #if defined(__ANDROID__)
364 // Our own matrix stack
365 static Matrix matrixStack[3][32];
366 static int matrixIndex[3];
367 static int curStack = 0;
369 // OpenGL ES Porting Kit
371 #define glBindFramebuffer glBindFramebufferOES
372 #define glBindRenderbuffer glBindRenderbufferOES
373 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
374 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
375 #define glFramebufferTexture2D glFramebufferTexture2DOES
376 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
377 #define glGenFramebuffers glGenFramebuffersOES
378 #define glGenRenderbuffers glGenRenderbuffersOES
379 #define glDeleteFramebuffers glDeleteFramebuffersOES
380 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
382 #define GL_POLYGON_STIPPLE 0xFFFF
383 #define GL_LINE_STIPPLE 0xFFFF
384 #define GL_LINE 0xFFFF
385 #define GL_FILL 0xFFFF
386 #define GL_ALL_ATTRIB_BITS 0xFFFF
387 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
388 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
390 #define GL_UNSIGNED_INT 0x1405
391 #define GL_DOUBLE 0x140A
396 #define glBufferDatai glesBufferDatai
397 #define glBufferDatad glesBufferDatad
398 #define glVertexPointeri glesVertexPointeri
399 #define glVertexPointerd glesVertexPointerd
401 #define glRecti glesRecti
402 #define glBegin glesBegin
403 #define glTexCoord2i glesTexCoord2i
404 #define glVertex2i glesVertex2i
405 #define glTexCoord2d glesTexCoord2d
406 #define glVertex2d glesVertex2d
407 #define glTexCoord2f glesTexCoord2f
408 #define glVertex2f glesVertex2f
409 #define glEnd glesEnd
410 #define glColor3f glesColor3f
411 #define glColor4ub glesColor4ub
412 #define glColor4fv glesColor4fv
413 #define glLineStipple glesLineStipple
414 #define glNormal3fv glesNormal3fv
415 #define glTexCoord2fv glesTexCoord2fv
416 #define glColorMaterial glesColorMaterial
418 #define glLoadMatrixd glesLoadMatrixd
419 #define glMultMatrixd glesMultMatrixd
420 #define glFrustum glesFrustum
421 #define glOrtho glesOrtho
422 #define glScaled glesScaled
423 #define glTranslated glesTranslated
424 #define glRotated glesRotated
425 #define glVertex3d glesVertex3d
426 #define glVertex3f glesVertex3f
427 #define glVertex3fv glesVertex3fv
428 #define glLightModeli glesLightModeli
432 #define GL_QUAD_STRIP 0
433 //#define GL_DOUBLE 0
434 //#define GL_UNSIGNED_INT 0
437 //#define GL_LINE_STIPPLE 0
438 #define GL_BGRA_EXT 0
439 #define GL_UNPACK_ROW_LENGTH 0
440 #define GL_UNPACK_SKIP_PIXELS 0
441 #define GL_UNPACK_SKIP_ROWS 0
443 #define GL_PACK_ROW_LENGTH 0
444 #define GL_PACK_SKIP_ROWS 0
445 #define GL_PACK_SKIP_PIXELS 0
447 static EGLDisplay eglDisplay;
448 static EGLSurface eglSurface;
449 static EGLContext eglContext;
450 static int eglWidth, eglHeight;
452 static bool egl_init_display(ANativeWindow* window)
454 const EGLint attribs[] =
456 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
461 /*EGL_SAMPLE_BUFFERS, 1,
462 EGL_SAMPLES, 0, //2,*/
465 EGLint w, h, dummy, format;
471 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
472 eglInitialize(display, 0, 0);
473 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
474 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
476 surface = eglCreateWindowSurface(display, config, window, null);
477 context = eglCreateContext(display, config, null, null);
479 if(!eglMakeCurrent(display, surface, surface, context))
482 eglQuerySurface(display, surface, EGL_WIDTH, &w);
483 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
485 eglDisplay = display;
486 eglContext = context;
487 eglSurface = surface;
491 glEnableClientState(GL_VERTEX_ARRAY);
493 // Initialize GL state.
494 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
495 glEnable(GL_CULL_FACE);
496 glShadeModel(GL_SMOOTH);
497 glDisable(GL_DEPTH_TEST);
499 glDisable(GL_CULL_FACE);
500 glDisable(GL_DEPTH_TEST);
502 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
505 matrixStack[0][0].Identity();
506 matrixStack[1][0].Identity();
507 matrixStack[2][0].Identity();
509 glesMatrixMode(GL_MODELVIEW);
510 glScaled(1.0, 1.0, -1.0);
511 glesMatrixMode(GL_PROJECTION);
512 glShadeModel(GL_FLAT);
514 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
515 glFogi(GL_FOG_MODE, GL_EXP);
516 glFogf(GL_FOG_DENSITY, 0);
517 glEnable(GL_NORMALIZE);
518 glDepthFunc(GL_LESS);
520 glDisable(GL_MULTISAMPLE_ARB);
524 glOrtho(0,w,h,0,0.0,1.0);
526 currentVertexBuffer = 0;
530 static void egl_term_display()
534 glDeleteTextures(1, &stippleTexture);
537 if(eglDisplay != EGL_NO_DISPLAY)
539 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
540 if(eglContext != EGL_NO_CONTEXT)
541 eglDestroyContext(eglDisplay, eglContext);
542 if(eglSurface != EGL_NO_SURFACE)
543 eglDestroySurface(eglDisplay, eglSurface);
544 eglTerminate(eglDisplay);
546 eglDisplay = EGL_NO_DISPLAY;
547 eglContext = EGL_NO_CONTEXT;
548 eglSurface = EGL_NO_SURFACE;
551 // OpenGL Immediate Mode Porting Kit
552 static int beginCount;
553 static int vertexCount;
554 static int normalCount;
555 static float *vertexPointer;
556 static float *normalPointer;
557 static GLenum beginMode;
558 static unsigned int beginBufferSize, normalBufferSize;
559 static int numVertexCoords = 2;
561 void glesRecti(int a, int b, int c, int d)
571 void glesBegin(GLenum mode)
578 normalBufferSize = beginBufferSize = 1024; // default number of vertices
579 vertexPointer = new float[beginBufferSize * 5];
580 normalPointer = new float[normalBufferSize * 3];
584 void glesTexCoord2f(float x, float y)
586 int count = vertexCount;
588 if(vertexCount + numVertexCoords > beginBufferSize)
590 beginBufferSize = beginBufferSize + beginBufferSize/2;
591 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
594 vertexPointer[count*(2+numVertexCoords) ] = x;
595 vertexPointer[count*(2+numVertexCoords)+1] = y;
598 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
600 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
601 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
603 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
604 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
608 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
609 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
610 void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
612 void glesVertex2f(float x, float y)
615 if(vertexCount + 4 > beginBufferSize)
617 beginBufferSize = beginBufferSize + beginBufferSize/2;
618 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
621 vertexPointer[vertexCount*4+2] = x;
622 vertexPointer[vertexCount*4+3] = y;
625 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
627 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
628 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
630 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
631 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
636 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
637 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
641 int mode = beginMode;
642 if(mode == GL_QUADS) mode = GL_TRIANGLES;
643 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
645 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
646 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
647 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
648 if(normalCount && normalCount == vertexCount)
650 glEnableClientState(GL_NORMAL_ARRAY);
651 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
654 glDrawArrays(mode, 0, vertexCount);
656 glDisableClientState(GL_NORMAL_ARRAY);
657 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
662 static float *floatVPBuffer = null;
663 static short *shortVPBuffer = null;
664 static unsigned int shortVPSize = 0, floatVPSize = 0;
667 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
668 static unsigned short *shortBDBuffer = null;
669 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
671 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
676 if(numVertices*numCoords > shortVPSize)
678 shortVPSize = numVertices*numCoords;
679 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
681 for(i = 0; i < numVertices*numCoords; i++)
682 shortVPBuffer[i] = (short)pointer[i];
683 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
686 glVertexPointer(numCoords, GL_SHORT, stride, 0);
689 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
694 if(numVertices*numCoords > floatVPSize)
696 floatVPSize = numVertices*numCoords;
697 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
699 for(i = 0; i < numVertices*numCoords; i++)
700 floatVPBuffer[i] = (float)pointer[i];
701 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
704 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
707 void glesTexReuseIntVP(int numCoords)
709 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
712 void glesTexReuseDoubleVP(int numCoords)
714 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
717 void glesColor3f( float r, float g, float b )
719 glColor4f(r, g, b, 1.0f);
722 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
724 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
727 void glesColor4fv(float * a)
729 glColor4f(a[0], a[1], a[2], a[3]);
732 void glesBufferDatad(int target, int size, void * data, int usage)
734 int numElems = size/sizeof(double);
735 double * dblPtr = (double *)data;
737 if (numElems > floatVPSize)
739 floatVPSize = numElems;
740 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
742 for (i=0; i< numElems; i++)
743 floatVPBuffer[i] = (float)dblPtr[i];
745 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
748 void glesBufferDatai(int target, int size, void * data, int usage)
750 int numElems = size/sizeof(unsigned int);
751 unsigned int * pointer = (unsigned int *)data;
753 if (numElems > shortBDSize)
755 shortBDSize = numElems;
756 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
758 for (i=0; i< numElems; i++)
759 shortBDBuffer[i] = (unsigned short)pointer[i];
761 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
764 // *** Our Custom Matrix Stack ***
766 static void LoadCurMatrix()
768 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
771 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
772 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
773 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
774 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
779 void glesLoadIdentity()
781 matrixStack[curStack][matrixIndex[curStack]].Identity();
785 void glesPushMatrix()
787 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
789 matrixIndex[curStack]++;
790 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
796 if(matrixIndex[curStack] > 0)
798 matrixIndex[curStack]--;
803 void glesLoadMatrixd(double * i)
805 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
809 void glesOrtho( double l, double r, double b, double t, double n, double f )
813 (2 / (r - l)), 0, 0, 0,
814 0, (2 / (t - b)), 0, 0,
815 0, 0, (-2 / (f - n)), 0,
816 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
818 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
819 matrixStack[curStack][matrixIndex[curStack]] = res;
823 void glesFrustum( double l, double r, double b, double t, double n, double f )
833 double A = ((r + l) / (r - l));
834 double B = ((t + b) / (t - b));
835 double C = (-(f + n) / (f - n));
836 double D = (-2*f*n/(f-n));
839 (2.0*n / (r - l)), 0, 0, 0,
840 0, (2.0*n / (t - b)), 0, 0,
844 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
845 matrixStack[curStack][matrixIndex[curStack]] = res;
850 void glesRotated( double a, double b, double c, double d )
854 q.RotationAxis({(float)b,(float)-c,(float)d}, a );
855 m.RotationQuaternion(q);
856 matrixStack[curStack][matrixIndex[curStack]].Rotate(q);
859 void glesScaled( double a, double b, double c )
865 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
866 matrixStack[curStack][matrixIndex[curStack]] = r;
870 void glesTranslated( double a, double b, double c )
876 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
877 matrixStack[curStack][matrixIndex[curStack]] = r;
881 void glesMultMatrixd( double * i )
884 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
885 matrixStack[curStack][matrixIndex[curStack]] = r;
889 void glesMatrixMode(int mode)
891 curStack = mode == GL_MODELVIEW ? 0 : mode == GL_PROJECTION ? 1 : 2;
895 #define glPushMatrix glesPushMatrix
896 #define glPopMatrix glesPopMatrix
897 #define glLoadIdentity glesLoadIdentity
898 #define glMatrixMode glesMatrixMode
900 /* Using the built-in matrix stack
901 void glesLoadMatrixd( double * i )
905 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
906 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
907 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
908 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
913 void glesOrtho( double l, double r, double b, double t, double n, double f )
917 { (float)(2 / (r - l)), 0, 0, 0 },
918 { 0, (float)(2 / (t - b)), 0, 0 },
919 { 0, 0, (float)(-2 / (f - n)), 0 },
920 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
922 glMultMatrixf((float *)matrix);
925 void glesFrustum( double l, double r, double b, double t, double n, double f )
927 float A = (float)((r + l) / (r - l));
928 float B = (float)((t + b) / (t - b));
929 float C = (float)(-(f + n) / (f - n));
930 float D = (float)(-2*f*n/(f-n));
933 { (float)(2*n / (r - l)), 0, 0, 0 },
934 { 0, (float)(2*n / (t - b)), 0, 0 },
938 glMultMatrixf((float *)matrix);
941 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
942 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
943 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
945 void glesMultMatrixd( double * i )
949 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
950 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
951 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
952 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
958 // Need to do these...
959 void glesVertex3f( float x, float y, float z )
962 if(vertexCount + 4 > beginBufferSize)
964 beginBufferSize = beginBufferSize + beginBufferSize/2;
965 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
968 vertexPointer[vertexCount*5+2] = x;
969 vertexPointer[vertexCount*5+3] = y;
970 vertexPointer[vertexCount*5+4] = z;
973 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
975 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
976 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
977 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
979 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
980 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
981 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
987 void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
988 void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
990 void glesNormal3f(float x, float y, float z)
992 normalCount = vertexCount;
993 if(vertexCount + 4 > normalBufferSize)
995 normalBufferSize = normalBufferSize + normalBufferSize/2;
996 normalPointer = renew normalPointer float[normalBufferSize * 2];
999 normalPointer[normalCount*3+0] = x;
1000 normalPointer[normalCount*3+1] = y;
1001 normalPointer[normalCount*3+2] = z;
1004 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1006 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1007 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1008 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1010 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1011 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1012 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1016 void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1017 void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1019 void glesColorMaterial(int a, int b)
1021 PrintLn("glColorMaterial stub");
1024 void glesTerminate()
1026 delete vertexPointer;
1027 beginBufferSize = 0;
1029 delete floatVPBuffer;
1032 delete shortVPBuffer;
1035 delete shortBDBuffer;
1039 static GLuint stippleTexture;
1040 static bool stippleEnabled;
1042 void glesLineStipple( int i, unsigned short j )
1047 for(x = 0; x < 16; x++)
1049 bool v = (j & (1 << x)) != 0;
1050 texture[x] = v ? 0xFFFFFFFF : 0;
1053 glGenTextures(1, &stippleTexture);
1054 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1055 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1056 glEnable(GL_TEXTURE_2D);
1057 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1058 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1059 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1060 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1061 glMatrixMode(GL_TEXTURE);
1063 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1064 glScaled(i/16.0, 1, 1.0f);
1065 glTranslated(0.5, 0.5, 0);
1066 glMatrixMode(GL_PROJECTION);
1069 void glesLightModeli( unsigned int pname, int param )
1071 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1072 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1075 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1076 void glFogi( unsigned int pname, int param ) { }
1077 void glPolygonMode( unsigned int i, unsigned int j ) { }
1080 // *** Picking won't be supported for now ***
1081 void glPushName( unsigned int i ) { }
1082 void glLoadName( unsigned int i ) { }
1083 void glPopName() { }
1085 // Probably replace by regular glBlendFunc ...
1086 void glBlendFuncSeparate(int a, int b, int c, int d)
1091 // For direct pixel blitting...
1092 void glRasterPos2d(double a, double b) { }
1093 void glPixelZoom(float a, float b) { }
1094 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1098 /* Non OpenGL ES friendly stuff
1099 #undef GL_UNSIGNED_INT
1104 #undef GL_QUAD_STRIP
1105 #undef GL_POLYGON_STIPPLE
1106 #undef GL_LINE_STIPPLE
1109 #undef GL_ALL_ATTRIB_BITS
1110 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1115 #if !defined(__APPLE__)
1116 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1117 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1118 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1119 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1124 static int currentVertexBuffer;
1126 bool GLSelectVBO(uint vbo)
1128 if(currentVertexBuffer != vbo)
1130 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
1131 currentVertexBuffer = vbo;
1137 void GLGenBuffers(int count, uint * buffer)
1140 glGenBuffers(count, buffer);
1143 glGenBuffersARB(count, buffer);
1147 void GLDeleteBuffers(int count, GLuint * buffer)
1150 glDeleteBuffers(count, buffer);
1152 if(glDeleteBuffersARB)
1153 glDeleteBuffersARB(count, buffer);
1157 void GLBindBuffer(int target, uint buffer)
1160 glBindBuffer(target, buffer);
1163 glBindBufferARB(target, buffer);
1167 void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1170 if(type == GL_DOUBLE)
1171 glesBufferDatad(target, size, data, usage);
1172 else if(type == GL_UNSIGNED_INT)
1173 glesBufferDatai(target, size, data, usage);
1175 glBufferData(target, size, data, usage);
1178 glBufferDataARB(target, size, data, usage);
1183 static int displayWidth, displayHeight;
1185 #define GL_CLAMP_TO_EDGE 0x812F
1187 static bool vboAvailable;
1189 static bool useSingleGLContext = false;
1190 class OGLDisplay : struct
1192 #if defined(__WIN32__)
1202 int imageBuffers[2];
1203 byte * pboMemory1, * pboMemory2;
1206 GLXContext glContext;
1209 XShmSegmentInfo shminfo;
1211 XShmSegmentInfo shminfoShape;
1212 XImage * shapeImage;
1216 X11Picture windowPicture;
1217 X11Picture pixmapPicture;
1219 X11Picture shapePicture;
1222 ColorAlpha * flippingBuffer;
1223 int flipBufH, flipBufW;
1228 class OGLSystem : struct
1233 #if defined(__WIN32__)
1234 PIXELFORMATDESCRIPTOR pfd;
1240 XVisualInfo * visualInfo;
1241 GLXContext glContext;
1242 GLXDrawable glxDrawable;
1246 class OGLSurface : struct
1253 float foreground[4], background[4], bitmapMult[4];
1256 class OGLMesh : struct
1265 class OGLIndices : struct
1272 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1273 static int primitiveTypes[RenderPrimitiveType] =
1275 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1282 class OpenGLDisplayDriver : DisplayDriver
1284 class_property(name) = "OpenGL";
1286 bool LockSystem(DisplaySystem displaySystem)
1288 OGLSystem oglSystem = displaySystem.driverData;
1289 if(useSingleGLContext) return true;
1290 #if defined(__WIN32__)
1291 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1292 #elif defined(__unix__) || defined(__APPLE__)
1293 //if(previous) return true;
1294 // printf("Making SYSTEM current\n");
1295 #if !defined(__ANDROID__)
1296 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1298 //previous = oglSystem.glContext;
1303 void UnlockSystem(DisplaySystem displaySystem)
1305 if(useSingleGLContext) return;
1306 #if defined(__WIN32__)
1307 wglMakeCurrent(null, null);
1308 #elif defined(__unix__) || defined(__APPLE__)
1309 // printf("Making NULL current\n");
1310 #if defined(__ANDROID__)
1312 glXMakeCurrent(xGlobalDisplay, None, null);
1318 bool Lock(Display display)
1320 OGLDisplay oglDisplay = display.driverData;
1322 if(useSingleGLContext) return true;
1323 #if defined(__WIN32__)
1324 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1325 #elif defined(__unix__) || defined(__APPLE__)
1326 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1327 // printf(" Making DISPLAY current\n");
1328 #if defined(__ANDROID__)
1330 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1336 void Unlock(Display display)
1338 if(useSingleGLContext) return;
1339 //printf(" Making NULL current\n");
1340 //glXMakeCurrent(xGlobalDisplay, None, null);
1342 LockSystem(display.displaySystem);
1345 void DestroyDisplay(Display display)
1347 OGLDisplay oglDisplay = display.driverData;
1351 #if defined(__WIN32__)
1352 wglMakeCurrent( null, null );
1355 wglDeleteContext(oglDisplay.glrc);
1357 if(oglDisplay.hdc && oglDisplay.pBuffer)
1358 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1360 if(oglDisplay.pBuffer)
1361 wglDestroyPbufferARB(oglDisplay.pBuffer);
1364 ReleaseDC(display.window, oglDisplay.hdc);
1366 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1367 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1369 #elif defined(__unix__) || defined(__APPLE__)
1370 #if defined(__ANDROID__)
1372 if(oglDisplay.shapePixmap)
1373 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1374 if(oglDisplay.pixmap)
1375 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1376 if(oglDisplay.image)
1378 if(oglDisplay.shminfoShape.shmid != -1)
1380 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1381 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1382 shmdt(oglDisplay.shminfo.shmaddr);
1383 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1386 if(oglDisplay.shapeImage)
1388 if(oglDisplay.shminfoShape.shmid != -1)
1390 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1391 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1392 shmdt(oglDisplay.shminfoShape.shmaddr);
1393 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1395 XDestroyImage(oglDisplay.shapeImage);
1396 oglDisplay.shapeImage = None;
1399 glXMakeCurrent(xGlobalDisplay, None, null);
1401 if(oglDisplay.glContext)
1402 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1405 delete oglDisplay.flippingBuffer;
1407 display.driverData = null;
1411 void ::CheckExtensions(OGLSystem oglSystem)
1413 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1415 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1416 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1419 bool CreateDisplaySystem(DisplaySystem displaySystem)
1421 bool result = false;
1422 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1425 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1427 oglSystem.hdc = GetDC(oglSystem.hwnd);
1431 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1432 oglSystem.pfd.nVersion = 1;
1433 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1434 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1435 oglSystem.pfd.cColorBits = 24;
1436 oglSystem.pfd.cAlphaBits = 8;
1437 oglSystem.pfd.cDepthBits = 24;
1438 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1440 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1441 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1443 if(oglSystem.pfd.cColorBits > 8)
1445 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1446 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1449 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1451 // Get Pointers To The GL Functions
1452 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1453 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1454 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1455 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1456 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1457 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1458 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1459 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1460 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1461 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1462 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1463 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1465 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1466 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1467 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1468 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1469 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1470 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1471 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1472 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1473 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1475 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1477 vboAvailable = glBindBufferARB != null;
1479 // eSystem_LoggingMode(LOG_MSGBOX, null);
1481 if(wglChoosePixelFormatARB)
1486 float fAttributes[] = {0,0};
1489 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1490 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1491 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1492 WGL_COLOR_BITS_ARB,24,
1493 WGL_ALPHA_BITS_ARB,8,
1494 WGL_DEPTH_BITS_ARB,16,
1495 WGL_STENCIL_BITS_ARB,0,
1496 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1497 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1498 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1502 //Log("Found wglChoosePixelFormatARB\n");
1504 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1505 if(!valid || !numFormats)
1507 //Log("Can't find 4x multi sampling\n");
1508 iAttributes[19] = 2;
1509 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1510 if(!valid || !numFormats)
1512 // Log("Can't find 2x multi sampling\n");
1513 iAttributes[16] = 0;
1514 iAttributes[17] = 0;
1515 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1518 if(valid && numFormats)
1520 oglSystem.format = pixelFormat;
1521 wglMakeCurrent(null, null);
1522 wglDeleteContext(oglSystem.glrc);
1524 // *** DescribePixelFormat does not support WGL pixel formats! ***
1525 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1526 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1527 //Log("Successfully set pixel format\n");
1529 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1530 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1534 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1538 CheckExtensions(oglSystem);
1540 wglMakeCurrent(null, null);
1542 //eSystem_DumpErrors(true);
1546 #elif defined(__unix__) || defined(__APPLE__)
1547 vboAvailable = true;
1548 #if defined(__ANDROID__)
1549 egl_init_display(guiApp.desktop.windowHandle);
1550 CheckExtensions(oglSystem);
1554 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1555 XSetWindowAttributes attr;
1560 #ifndef ECERE_MINIGLX
1561 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1564 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1568 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1569 attr.background_pixel = 0;
1570 attr.border_pixel = 0;
1571 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1572 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1573 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1575 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1576 oglSystem.visualInfo->visual, mask, &attr );
1578 if(oglSystem.visualInfo)
1580 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1581 if(oglSystem.glContext)
1583 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1585 CheckExtensions(oglSystem);
1586 glXMakeCurrent(xGlobalDisplay, None, null);
1593 displaySystem.flags.alpha = true;
1594 displaySystem.flags.flipping = true;
1595 displaySystem.pixelFormat = pixelFormat888;
1599 void DestroyDisplaySystem(DisplaySystem displaySystem)
1601 OGLSystem oglSystem = displaySystem.driverData;
1603 #if defined(__WIN32__)
1604 wglMakeCurrent( null, null );
1607 wglDeleteContext(oglSystem.glrc);
1610 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1611 DestroyWindow(oglSystem.hwnd);
1613 #elif defined(__unix__) || defined(__APPLE__)
1614 #if defined(__ANDROID__)
1617 if(oglSystem.visualInfo)
1619 #ifdef ECERE_MINIGLX
1620 __miniglx_XFree(oglSystem.visualInfo);
1622 XFree(oglSystem.visualInfo);
1626 if(oglSystem.glxDrawable)
1628 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1629 oglSystem.glxDrawable = 0;
1636 bool CreateDisplay(Display display)
1638 bool result = false;
1639 OGLDisplay oglDisplay = display.driverData;
1640 OGLSystem oglSystem = display.displaySystem.driverData;
1642 oglDisplay = display.driverData = OGLDisplay { };
1643 //printf("Inside CreateDisplay\n");
1645 #if defined(__WIN32__) || defined(USEPBUFFER)
1646 if(!display.alphaBlend)
1649 #if defined(__WIN32__)
1650 oglDisplay.hdc = GetDC(display.window);
1651 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1652 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1654 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1655 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1659 ReleaseDC(display.window, oglDisplay.hdc);
1660 #elif defined(__unix__) || defined(__APPLE__)
1661 #if defined(__ANDROID__)
1663 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1665 #if defined(__APPLE__)
1666 XVisualInfo template = { 0 };
1667 XWindowAttributes winAttr;
1669 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1670 template.visualid = XVisualIDFromVisual(winAttr.visual);
1671 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1673 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1674 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1675 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1676 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1678 // visualInfo = oglSystem.visualInfo;
1683 //printf("visualInfo is not null\n");
1684 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1685 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1686 //XFree(visualInfo);
1689 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1690 if(oglDisplay.glContext)
1692 //printf("CreateDisplay Got a Context\n");
1693 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1699 #if defined(__WIN32__) || defined(USEPBUFFER)
1705 #if !defined(__OLDX__)
1706 if(glBlendFuncSeparate)
1707 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1710 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1713 glMatrixMode(GL_MODELVIEW);
1714 glScaled(1.0, 1.0, -1.0);
1715 // glTranslatef(0.375f, 0.375f, 0.0f);
1716 // glTranslatef(-0.625f, -0.625f, 0.0f);
1717 glMatrixMode(GL_PROJECTION);
1718 glShadeModel(GL_FLAT);
1720 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1721 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1722 glFogi(GL_FOG_MODE, GL_EXP);
1723 glFogf(GL_FOG_DENSITY, 0);
1724 glEnable(GL_NORMALIZE);
1725 glDepthFunc(GL_LESS);
1727 glDisable(GL_MULTISAMPLE_ARB);
1729 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1730 display.ambient = Color { 50,50,50 };
1733 if(!useSingleGLContext)
1735 #if defined(__WIN32__)
1736 wglMakeCurrent(null, null);
1737 #elif defined(__unix__) || defined(__APPLE__)
1738 #if defined(__ANDROID__)
1741 glXMakeCurrent(xGlobalDisplay, None, null);
1749 bool DisplaySize(Display display, int width, int height)
1751 OGLDisplay oglDisplay = display.driverData;
1752 OGLSystem oglSystem = display.displaySystem.driverData;
1754 bool result = false;
1756 //printf("Inside DisplaySize\n");
1757 #if defined(__WIN32__) || defined(USEPBUFFER)
1758 if(display.alphaBlend)
1760 #if defined(__WIN32__)
1761 const int attributes[]=
1763 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1764 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1766 int pixelFormat = 0;
1767 if(wglChoosePixelFormatARB)
1771 float fAttributes[] = {0,0};
1774 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1775 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1776 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1777 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1778 WGL_COLOR_BITS_ARB,24,
1779 WGL_ALPHA_BITS_ARB,8,
1780 WGL_DEPTH_BITS_ARB,16,
1781 WGL_STENCIL_BITS_ARB,0,
1782 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1783 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1784 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1788 //Log("Found wglChoosePixelFormatARB\n");
1790 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1791 if(!valid || !numFormats)
1793 //Log("Can't find 4x multi sampling\n");
1794 iAttributes[19] = 2;
1795 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1796 if(!valid || !numFormats)
1798 // Log("Can't find 2x multi sampling\n");
1799 iAttributes[16] = 0;
1800 iAttributes[17] = 0;
1801 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1802 if(!valid || !numFormats)
1806 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1807 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1808 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1809 WGL_COLOR_BITS_ARB,24,
1810 WGL_ALPHA_BITS_ARB,8,
1811 WGL_DEPTH_BITS_ARB,16,
1814 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1818 if(valid && numFormats)
1820 wglMakeCurrent(null, null);
1824 wglMakeCurrent( null, null );
1825 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1826 if(oglDisplay.hdc && oglDisplay.pBuffer)
1827 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1829 wglDestroyPbufferARB(oglDisplay.pBuffer);
1831 if(!useSingleGLContext)
1832 wglMakeCurrent( null, null );
1835 wglDeleteContext(oglDisplay.glrc);
1837 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1838 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1839 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1842 HDC hdc = GetDC(display.window);
1844 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1845 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1847 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1848 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1850 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1852 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1856 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1857 oglDisplay.memDC = CreateCompatibleDC(hdc);
1858 SetMapMode(oglDisplay.memDC, MM_TEXT);
1859 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1860 info->bmiHeader.biPlanes = 1;
1861 info->bmiHeader.biCompression = BI_RGB;
1862 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1863 info->bmiHeader.biWidth = width;
1864 info->bmiHeader.biHeight = height;
1865 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1868 SelectObject(oglDisplay.memDC, newBitmap);
1869 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1872 PIXELFORMATDESCRIPTOR pfd = { 0 };
1873 pfd.nSize = (short)sizeof(pfd);
1875 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1876 pfd.iPixelType = PFD_TYPE_RGBA;
1877 pfd.cColorBits = 32;
1878 //pfd.cAlphaBits = 8;
1879 pfd.cDepthBits = 24;
1880 pfd.iLayerType = PFD_MAIN_PLANE;
1882 oglDisplay.hdc = oglDisplay.memDC;
1884 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1885 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1886 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1888 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1889 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1890 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1895 const int imageSize = width * height * 4;
1897 glGenBuffersARB(2, oglDisplay.imageBuffers);
1899 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1900 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1901 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1902 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1905 oglDisplay.memBitmap = newBitmap;
1906 oglDisplay.stride = width;
1912 ReleaseDC(display.window, hdc);
1914 #elif defined(__unix__) || defined(__APPLE__)
1915 #if defined(__ANDROID__)
1920 GLX_DOUBLEBUFFER, True,
1926 GLX_STENCIL_SIZE, 1,
1927 //GLX_DEPTH_SIZE, 24,
1928 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1929 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1935 GLX_PBUFFER_WIDTH, width,
1936 GLX_PBUFFER_HEIGHT, height,
1937 GLX_LARGEST_PBUFFER, False,
1941 // choose a pixel format that meets our minimum requirements
1944 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1947 if(oglDisplay.pixmap)
1949 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1950 oglDisplay.pixmap = None;
1952 if(oglDisplay.shapePixmap)
1954 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1955 oglDisplay.shapePixmap = None;
1958 // Free Shared Memory Pixmap
1959 if(oglDisplay.image)
1961 if(oglDisplay.shminfoShape.shmid != -1)
1963 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1964 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1965 shmdt(oglDisplay.shminfo.shmaddr);
1966 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1968 XDestroyImage(oglDisplay.image);
1969 oglDisplay.image = None;
1971 if(oglDisplay.shapeImage)
1973 if(oglDisplay.shminfoShape.shmid != -1)
1975 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1976 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1977 shmdt(oglDisplay.shminfoShape.shmaddr);
1978 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1980 XDestroyImage(oglDisplay.shapeImage);
1981 oglDisplay.shapeImage = None;
1984 if(oglDisplay.windowPicture)
1985 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1986 if(oglDisplay.pixmapPicture)
1987 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1989 if(oglDisplay.pixmap)
1990 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1992 if(oglDisplay.glContext)
1993 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1994 if(oglDisplay.pBuffer)
1995 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1997 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1998 if(oglDisplay.pBuffer)
2000 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2001 if(oglDisplay.glContext)
2003 glXMakeCurrent(xGlobalDisplay, None, null);
2004 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2006 // Initialize Shared Memory Pixmap
2007 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2008 ZPixmap, null, &oglDisplay.shminfo, width, height);
2009 if(oglDisplay.image)
2011 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2012 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2013 if(oglDisplay.shminfo.shmid != -1)
2015 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2016 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2018 oglDisplay.shminfo.readOnly = False;
2019 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2021 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2022 &oglDisplay.shminfo, width, height, 32);
2024 // Initialize Shared Memory Shape Pixmap
2025 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2026 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2027 if(oglDisplay.shapeImage)
2029 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2030 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2031 if(oglDisplay.shminfoShape.shmid != -1)
2033 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2034 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2036 oglDisplay.shminfoShape.readOnly = False;
2037 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2039 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2040 &oglDisplay.shminfoShape, width, height, 1);
2041 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2044 XRenderPictureAttributes attributes = { 0 };
2045 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2046 #if !defined(__APPLE__) && !defined(__OLDX__)
2047 attributes.repeat = RepeatNormal;
2049 attributes.repeat = 1;
2051 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2052 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2053 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2054 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2057 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2058 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2075 CreateDisplay(display);
2076 #if defined(__WIN32__)
2077 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2078 #elif defined(__unix__) || defined(__APPLE__)
2079 #if defined(__ANDROID__)
2083 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2090 if(!result && display.alphaBlend)
2092 printf("Alpha blending windows not supported on this display\n");
2099 glViewport(0,0,width,height);
2101 glOrtho(0,width,height,0,0.0,1.0);
2102 displayWidth = display.width = width;
2103 displayHeight = display.height = height;
2105 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2107 oglDisplay.flipBufW = width;
2108 oglDisplay.flipBufH = height;
2109 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2111 if(oglDisplay.flippingBuffer || !width || !height)
2117 void DisplayPosition(Display display, int x, int y)
2119 OGLDisplay oglDisplay = display.driverData;
2125 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2129 void RestorePalette(Display display)
2133 void StartUpdate(Display display)
2137 void EndUpdate(Display display)
2141 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2145 void Update(Display display, Box updateBox)
2147 OGLDisplay oglDisplay = display.driverData;
2148 //Logf("DisplayScreen\n");
2152 #if defined(__WIN32__) || defined(USEPBUFFER)
2153 if(display.alphaBlend)
2155 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2156 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2157 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2158 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2159 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2162 #if defined(__WIN32__)
2164 POINT point = { oglDisplay.x, oglDisplay.y};
2165 POINT srcPoint = { 0, 0 };
2166 BLENDFUNCTION blend = { 0 };
2168 size.cx = display.width;
2169 size.cy = display.height;
2170 blend.BlendOp = AC_SRC_OVER;
2171 blend.BlendFlags = 0;
2172 blend.SourceConstantAlpha = 255;
2173 blend.AlphaFormat = AC_SRC_ALPHA;
2176 // Process partial images. Mapping the buffer waits for
2177 // outstanding DMA transfers into the buffer to finish.
2178 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2179 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2181 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2182 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2185 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2186 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2189 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2192 // Unmap the image buffers
2193 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2194 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2196 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2197 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2199 // Bind two different buffer objects and start the glReadPixels
2200 // asynchronously. Each call will return directly after
2201 // starting the DMA transfer.
2202 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2203 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2205 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2206 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2210 #elif defined(__unix__) || defined(__APPLE__)
2211 #if defined(__ANDROID__)
2213 XTransform transform =
2216 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2217 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2218 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2221 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2222 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2223 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2224 #if !defined(__APPLE__) && !defined(__OLDX__)
2225 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2227 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2229 XFlush(xGlobalDisplay);
2237 #if defined(__WIN32__)
2238 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2239 SwapBuffers(oglDisplay.hdc);
2240 #elif defined(__unix__) || defined(__APPLE__)
2241 #if defined(__ANDROID__)
2242 eglSwapBuffers(eglDisplay, eglSurface);
2244 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2248 //Logf("Out of DisplayScreen\n");
2251 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2253 if(bitmap.driverData)
2255 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2256 glDeleteTextures(1, &tex);
2257 bitmap.driverData = 0;
2259 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2262 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2264 OGLSystem oglSystem = displaySystem.driverData;
2265 bool result = false;
2267 GLuint glBitmap = 0;
2269 uint w = width, h = height;
2270 if(oglSystem.pow2textures)
2275 w = Min(w, oglSystem.maxTextureSize);
2276 h = Min(h, oglSystem.maxTextureSize);
2278 glGenTextures(1, &glBitmap);
2279 glBindTexture(GL_TEXTURE_2D, glBitmap);
2281 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2283 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2284 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2286 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2289 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2291 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2293 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2294 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2298 bitmap.driverData = (void *)(uintptr)glBitmap;
2299 bitmap.driver = displaySystem.driver;
2307 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2309 bool result = false;
2310 OGLSystem oglSystem = displaySystem.driverData;
2312 // Pre process the bitmap... First make it 32 bit
2313 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
2316 uint w = bitmap.width, h = bitmap.height;
2317 GLuint glBitmap = 0;
2318 if(oglSystem.pow2textures)
2323 w = Min(w, oglSystem.maxTextureSize);
2324 h = Min(h, oglSystem.maxTextureSize);
2328 while(w * 2 < h) w *= 2;
2329 while(h * 2 < w) h *= 2;
2332 // Switch ARGB to RGBA
2333 //if(bitmap.format != pixelFormatRGBA)
2335 for(c=0; c<bitmap.size; c++)
2337 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2339 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
2340 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2343 bitmap.pixelFormat = pixelFormat888;
2346 glGenTextures(1, &glBitmap);
2349 //int error = glGetError();
2353 glBindTexture(GL_TEXTURE_2D, glBitmap);
2354 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2358 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2360 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2361 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2366 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2370 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2373 if(bitmap.width != w || bitmap.height != h)
2375 mipMap = Bitmap { };
2376 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
2378 Surface mipSurface = mipMap.GetSurface(0,0,null);
2379 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
2396 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2397 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2398 //printf("Calling glTexImage2D\n");
2399 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2400 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2401 if((error = glGetError()))
2403 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2404 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2408 if(mipMap != bitmap)
2413 if(!bitmap.keepData)
2414 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
2415 bitmap.driverData = (void *)(uintptr)glBitmap;
2416 bitmap.driver = displaySystem.driver;
2419 FreeBitmap(displaySystem, bitmap);
2420 else if(oglSystem.loadingFont)
2422 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2423 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2424 oglSystem.loadingFont = false;
2430 void ReleaseSurface(Display display, Surface surface)
2432 glDisable(GL_SCISSOR_TEST);
2433 delete surface.driverData;
2434 surface.driverData = null;
2437 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2442 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2444 bool result = false;
2445 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2447 //Logf("GetSurface\n");
2451 if(displayWidth != display.width || displayHeight != display.height)
2453 displayWidth = display.width;
2454 displayHeight = display.height;
2456 glViewport(0,0,display.width,display.height);
2458 glOrtho(0,display.width,display.height,0,0.0,1.0);
2461 surface.offset.x = x;
2462 surface.offset.y = y;
2463 surface.unclippedBox = surface.box = clip;
2464 oglSurface.bitmapMult[0] = 1;
2465 oglSurface.bitmapMult[1] = 1;
2466 oglSurface.bitmapMult[2] = 1;
2467 oglSurface.bitmapMult[3] = 1;
2469 glEnable(GL_SCISSOR_TEST);
2472 (display.height) -(y+clip.bottom)-1,
2473 clip.right-clip.left+1,
2474 clip.bottom-clip.top+1);
2480 void Clip(Display display, Surface surface, Box clip)
2489 box.Clip(surface.unclippedBox);
2493 box = surface.box = surface.unclippedBox;
2494 box.left += surface.offset.x;
2495 box.top += surface.offset.y;
2496 box.right+= surface.offset.x;
2497 box.bottom += surface.offset.y;
2500 box.left,display.height - box.bottom - 1,
2501 box.right-box.left+1, box.bottom-box.top+1);
2504 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2506 bool result = false;
2507 OGLDisplay oglDisplay = display.driverData;
2508 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2510 if(oglDisplay.flippingBuffer)
2512 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2515 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2521 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2522 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2523 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2524 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2525 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2528 for(row = 0; row<h; row++)
2529 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2536 void SetForeground(Display display, Surface surface, ColorAlpha color)
2538 OGLSurface oglSurface = surface.driverData;
2540 //Logf("SetForeground\n");
2542 oglSurface.foreground[0] = color.color.r/255.0f;
2543 oglSurface.foreground[1] = color.color.g/255.0f;
2544 oglSurface.foreground[2] = color.color.b/255.0f;
2545 //oglSurface.foreground[3] = 1.0f;
2546 oglSurface.foreground[3] = color.a/255.0f;
2548 //if(!oglSurface.foreground[3])printf("bug");
2551 void SetBackground(Display display, Surface surface, ColorAlpha color)
2553 OGLSurface oglSurface = surface.driverData;
2555 //Logf("SetBackground\n");
2557 oglSurface.background[0] = color.color.r/255.0f;
2558 oglSurface.background[1] = color.color.g/255.0f;
2559 oglSurface.background[2] = color.color.b/255.0f;
2560 //oglSurface.background[3] = 1.0;
2561 oglSurface.background[3] = color.a/255.0f;
2564 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2566 OGLSurface oglSurface = surface.driverData;
2568 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2569 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2570 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2571 oglSurface.bitmapMult[3] = color.a/255.0f;
2574 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2579 void PutPixel(Display display, Surface surface,int x,int y)
2581 OGLSurface oglSurface = surface.driverData;
2583 //Logf("PutPixel\n");
2585 glColor4fv(oglSurface.foreground);
2587 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2588 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2593 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2595 OGLSurface oglSurface = surface.driverData;
2596 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2611 x1 += surface.offset.x;
2612 y1 += surface.offset.y;
2613 x2 += surface.offset.x;
2614 y2 += surface.offset.y;
2618 glColor4fv(oglSurface.foreground);
2623 glTexCoord2f(0.5f, 0);
2624 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2625 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2626 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2635 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2636 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2642 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2644 OGLSurface oglSurface = surface.driverData;
2645 x1 += surface.offset.x;
2646 y1 += surface.offset.y;
2647 x2 += surface.offset.x;
2648 y2 += surface.offset.y;
2650 //Logf("Rectangle\n");
2652 glColor4fv(oglSurface.foreground);
2658 glTexCoord2f(0.5f, 0);
2659 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2660 glTexCoord2f(y2-y1 + 0.5f, 0);
2661 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2663 glTexCoord2f(0.5f, 0);
2664 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2665 glTexCoord2f(x2 - x1 + 0.5f, 0);
2666 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2668 glTexCoord2f(0.5f, 0);
2669 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2670 glTexCoord2f(y1 - y2 + 0.5f, 0);
2671 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2673 glTexCoord2f(0.5f, 0);
2674 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2675 glTexCoord2f(x1 - x2 + 0.5f, 0);
2676 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2681 glBegin(GL_LINE_LOOP);
2688 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2689 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2690 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2691 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2696 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2698 OGLSurface oglSurface = surface.driverData;
2701 glColor4fv(oglSurface.background);
2702 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2703 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2706 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2707 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2711 void Clear(Display display, Surface surface, ClearType type)
2713 OGLDisplay oglDisplay = display.driverData;
2714 OGLSurface oglSurface = surface.driverData;
2717 if(type != depthBuffer)
2718 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2719 if(type != colorBuffer && !oglDisplay.depthWrite)
2721 glDepthMask((byte)bool::true);
2723 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2724 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2725 if(type != colorBuffer && !oglDisplay.depthWrite)
2727 glDepthMask((byte)bool::false);
2731 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2736 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2738 OGLSurface oglSurface = surface.driverData;
2740 #if !defined(__OLDX__)
2741 // WHY DO WE HAVE GL_ONE HERE ?
2742 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2743 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2746 if(!oglSurface.writingText)
2748 // glTranslatef(-0.375f, -0.375f, 0.0f);
2749 glEnable(GL_TEXTURE_2D);
2750 glColor4fv(oglSurface.bitmapMult);
2752 else if(oglSurface.xOffset)
2753 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2755 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2760 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2761 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2762 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2763 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2764 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2765 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2766 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2767 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2772 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2773 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2774 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2775 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2776 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2777 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2778 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2779 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2782 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2783 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2784 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2785 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2786 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2787 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2788 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2789 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2793 if(!oglSurface.writingText)
2795 glDisable(GL_TEXTURE_2D);
2797 //glTranslate(0.375, 0.375, 0.0);
2799 else if(oglSurface.xOffset)
2800 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2802 #if !defined(__OLDX__)
2803 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2804 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2808 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2810 OGLSurface oglSurface = surface.driverData;
2812 //glTranslate(-0.375, -0.375, 0.0);
2814 //Logf("Stretch\n");
2816 #if !defined(__OLDX__)
2817 /*if(glBlendFuncSeparate)
2818 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2821 glEnable(GL_TEXTURE_2D);
2822 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2824 glColor4fv(oglSurface.bitmapMult);
2830 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2831 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2833 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2834 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2836 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2837 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2839 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2840 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2844 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2845 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2847 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2848 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2850 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2851 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2853 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2854 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2859 glDisable(GL_TEXTURE_2D);
2861 //glTranslate(0.375, 0.375, 0.0);
2862 #if !defined(__OLDX__)
2863 /*if(glBlendFuncSeparate)
2864 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2869 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2871 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2874 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2876 float s2dw,s2dh,d2sw,d2sh;
2877 //bool flipX = false, flipY = false;
2879 //Logf("StretchDI\n");
2881 if(Sgn(w) != Sgn(sw))
2887 if(Sgn(h) != Sgn(sh))
2899 //Clip against the edges of the source
2902 dx+=(int)((0-sx) * s2dw);
2903 w-=(int)((0-sx) * s2dw);
2909 dy+=(int)((0-sy) * s2dh);
2910 h-=(int)((0-sy) * s2dh);
2915 if(sx+sw>bitmap.width-1)
2917 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2918 sw-=sx+sw-(bitmap.width-1)-1;
2920 if(sy+sh>(bitmap.height-1))
2922 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2923 sh-=sy+sh-(bitmap.height-1)-1;
2925 //Clip against the edges of the surfaceination
2926 if(dx<surface.box.left)
2929 sx+=(int)((surface.box.left-dx)*d2sw);
2930 sw-=(int)((surface.box.left-dx)*d2sw);
2931 w-=surface.box.left-dx;
2932 dx=surface.box.left;
2934 if(dy<surface.box.top)
2936 sy+=(int)((surface.box.top-dy)*d2sh);
2937 sh-=(int)((surface.box.top-dy)*d2sh);
2938 h-=surface.box.top-dy;
2941 if(dx+w>surface.box.right)
2943 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2944 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2945 w-=dx+w-surface.box.right-1;
2947 if(dy+h>surface.box.bottom)
2949 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2950 h-=dy+h-surface.box.bottom-1;
2952 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2954 dx += surface.offset.x;
2955 dy += surface.offset.y;
2957 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2959 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2960 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2961 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2962 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2963 glRasterPos2d(dx,dy);
2964 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2965 glPixelZoom(s2dw, -s2dh);
2966 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2967 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2968 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2969 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2970 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2974 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2978 //Clip against the edges of the source
2991 if(sx+w>bitmap.width-1)
2992 w-=sx+w-(bitmap.width-1)-1;
2993 if(sy+h>bitmap.height-1)
2994 h-=sy+h-(bitmap.height-1)-1;
2995 //Clip against the edges of the surfaceination
2996 if(dx<surface.box.left)
2999 sx+=surface.box.left-dx;
3000 w-=surface.box.left-dx;
3001 dx=surface.box.left;
3003 if(dy<surface.box.top)
3005 sy+=surface.box.top-dy;
3006 h-=surface.box.top-dy;
3009 if(dx+w>surface.box.right)
3011 //if(flip) sx+=dx+w-surface.box.right-1;
3012 w-=dx+w-surface.box.right-1;
3014 if(dy+h>surface.box.bottom)
3015 h-=dy+h-surface.box.bottom-1;
3019 dx += surface.offset.x;
3020 dy += surface.offset.y;
3022 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3024 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3025 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3026 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3027 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3028 glRasterPos2d(dx,dy);
3030 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3031 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3032 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3033 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3034 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3038 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3040 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3043 void UnloadFont(DisplaySystem displaySystem, Font font)
3045 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3048 Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
3051 OGLSystem oglSystem = displaySystem.driverData;
3052 oglSystem.loadingFont = true;
3053 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3057 void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
3059 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3062 void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
3064 OGLSurface oglSurface = surface.driverData;
3065 OGLSystem oglSystem = display.displaySystem.driverData;
3066 oglSystem.loadingFont = true;
3068 //glTranslated(-0.375, -0.375, 0.0);
3072 if(surface.textOpacity)
3075 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3076 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3079 oglSurface.writingText = true;
3081 glEnable(GL_TEXTURE_2D);
3082 glColor4fv(oglSurface.foreground);
3084 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3085 oglSurface.writingText = false;
3086 oglSystem.loadingFont = false;
3088 glDisable(GL_TEXTURE_2D);
3090 //glTranslated(0.375, 0.375, 0.0);
3093 void TextFont(Display display, Surface surface, Font font)
3095 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3098 void TextOpacity(Display display, Surface surface, bool opaque)
3100 OGLSurface oglSurface = surface.driverData;
3101 oglSurface.opaqueText = opaque;
3104 void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
3106 OGLSurface oglSurface = surface.driverData;
3107 OGLSystem oglSystem = display.displaySystem.driverData;
3108 oglSystem.loadingFont = true;
3109 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3110 oglSystem.loadingFont = false;
3113 void DrawingChar(Display display, Surface surface, char character)
3118 void LineStipple(Display display, Surface surface, uint32 stipple)
3120 //Logf("Stipple\n");
3124 #if defined(__ANDROID__)
3125 stippleEnabled = true;
3126 glesLineStipple(1, (uint16)stipple);
3128 glLineStipple(1, (uint16)stipple);
3129 glEnable(GL_LINE_STIPPLE);
3134 #if defined(__ANDROID__)
3135 stippleEnabled = false;
3136 glMatrixMode(GL_TEXTURE);
3138 glMatrixMode(GL_PROJECTION);
3139 glDisable(GL_TEXTURE_2D);
3141 glDisable(GL_LINE_STIPPLE);
3145 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3146 void SetRenderState(Display display, RenderState state, uint value)
3148 OGLDisplay oglDisplay = display.driverData;
3149 //Logf("RenderState\n");
3155 glEnable(GL_MULTISAMPLE_ARB);
3157 glDisable(GL_MULTISAMPLE_ARB);
3160 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3163 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3166 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3167 oglDisplay.depthWrite = (bool)value;
3171 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3172 glFogfv(GL_FOG_COLOR, (float *)&color);
3177 glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
3180 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3184 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3185 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3190 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3195 #if defined(__WIN32__)
3196 wglSwapIntervalEXT(value ? 1 : 0);
3203 void SetLight(Display display, int id, Light light)
3205 //Logf("SetLight\n");
3209 Object lightObject = light.lightObject;
3210 float position[4] = { 0, 0, 0, 0 };
3211 float color[4] = { 0, 0, 0, 1 };
3213 glEnable(GL_LIGHT0 + id);
3215 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3216 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3217 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3220 if(!light.multiplier) light.multiplier = 1.0f;
3222 color[0] = light.diffuse.r * light.multiplier;
3223 color[1] = light.diffuse.g * light.multiplier;
3224 color[2] = light.diffuse.b * light.multiplier;
3225 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3227 color[0] = light.ambient.r * light.multiplier;
3228 color[1] = light.ambient.g * light.multiplier;
3229 color[2] = light.ambient.b * light.multiplier;
3230 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3231 color[0] = light.specular.r * light.multiplier;
3232 color[1] = light.specular.g * light.multiplier;
3233 color[2] = light.specular.b * light.multiplier;
3234 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3238 Vector3D positionVector;
3239 if(light.flags.spot)
3241 if(lightObject.flags.root || !lightObject.parent)
3243 positionVector = lightObject.transform.position;
3244 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3248 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3249 if(display.display3D.camera)
3250 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3256 if(!light.direction.x && !light.direction.y && !light.direction.z)
3258 Vector3Df vector { 0,0,-1 };
3260 mat.RotationQuaternion(light.orientation);
3261 positionVector.MultMatrixf(vector, mat);
3265 positionVector = light.direction;
3270 position[0] = (float)positionVector.x;
3271 position[1] = (float)positionVector.y;
3272 position[2] = (float)positionVector.z;
3274 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3277 // Display Light Position
3278 glDisable(GL_LIGHTING);
3279 glDisable(GL_DEPTH_TEST);
3283 glVertex3fv(position);
3285 glEnable(GL_DEPTH_TEST);
3286 glEnable(GL_LIGHTING);
3290 if(lightObject.flags.root || !lightObject.parent)
3292 positionVector = light.target.transform.position;
3293 positionVector.Subtract(positionVector, display.camera.cPosition);
3297 positionVector.MultMatrix(light.target.transform.position,
3298 lightObject.light.target.parent.matrix);
3299 positionVector.Subtract(positionVector, display.camera.cPosition);
3302 position[0] = positionVector.x;
3303 position[1] = positionVector.y;
3304 position[2] = positionVector.z;
3306 glDisable(GL_LIGHTING);
3307 glDisable(GL_DEPTH_TEST);
3311 glVertex3fv(position);
3313 glEnable(GL_DEPTH_TEST);
3314 glEnable(GL_LIGHTING);
3317 if(light.flags.attenuation)
3319 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3320 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3321 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3324 if(light.flags.spot)
3327 #define MAXLIGHT 0.9
3328 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3329 // Figure out exponent out of the hot spot
3330 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3332 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3333 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3334 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3340 Vector3Df vector { 0,0,-1 };
3341 Vector3Df direction;
3344 mat.RotationQuaternion(light.orientation);
3345 direction.MultMatrix(vector, mat);
3347 position[0] = direction.x;
3348 position[1] = direction.y;
3349 position[2] = direction.z;
3351 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3355 glDisable(GL_LIGHT0 + id);
3358 void SetCamera(Display display, Surface surface, Camera camera)
3360 OGLDisplay oglDisplay = display.driverData;
3361 //Logf("SetCamera\n");
3365 int left = surface.box.left + surface.offset.x;
3366 int top = surface.box.top + surface.offset.y;
3367 int right = surface.box.right + surface.offset.x;
3368 int bottom = surface.box.bottom + surface.offset.y;
3369 float origX = surface.offset.x + camera.origin.x;
3370 float origY = surface.offset.y + camera.origin.y;
3372 int y = display.height - bottom - 1;
3373 int w = right - left + 1;
3374 int h = bottom - top + 1;
3377 glViewport(x, y, w, h);
3379 // *** Projection Matrix ***
3380 if(!display.display3D.camera)
3383 glMatrixMode(GL_PROJECTION);
3384 if(display.display3D.collectingHits)
3386 float pickX = display.display3D.pickX + surface.offset.x;
3387 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3391 w / display.display3D.pickWidth, 0, 0, 0,
3392 0, h / display.display3D.pickHeight, 0, 0,
3394 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3395 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3398 glLoadMatrixd(pickMatrix.array);
3403 (left - origX) * camera.zMin / camera.focalX,
3404 (right - origX) * camera.zMin / camera.focalX,
3405 (bottom - origY) * camera.zMin / camera.focalY,
3406 (top - origY) * camera.zMin / camera.focalY,
3407 camera.zMin, camera.zMax);
3409 glDisable(GL_BLEND);
3411 // *** Z Inverted Identity Matrix ***
3412 glMatrixMode(GL_MODELVIEW);
3413 if(!display.display3D.camera)
3418 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3420 // *** View Matrix ***
3421 glMultMatrixd(camera.viewMatrix.array);
3426 glEnable(GL_DEPTH_TEST);
3427 glEnable(GL_LIGHTING);
3428 glShadeModel(GL_SMOOTH);
3429 glDepthMask((byte)bool::true);
3430 oglDisplay.depthWrite = true;
3432 glEnable(GL_MULTISAMPLE_ARB);
3434 else if(display.display3D.camera)
3436 oglDisplay.depthWrite = false;
3437 glViewport(0,0,display.width,display.height);
3439 glDisable(GL_CULL_FACE);
3440 glDisable(GL_DEPTH_TEST);
3441 glDisable(GL_LIGHTING);
3443 glDisable(GL_TEXTURE_2D);
3444 glShadeModel(GL_FLAT);
3446 glDisable(GL_MULTISAMPLE_ARB);
3448 // *** Restore 2D MODELVIEW Matrix ***
3451 // *** Restore 2D PROJECTION Matrix ***
3452 glMatrixMode(GL_PROJECTION);
3456 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3459 void ApplyMaterial(Display display, Material material, Mesh mesh)
3461 //Logf("ApplyMaterial\n");
3464 if(material.flags.doubleSided)
3466 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3467 glDisable(GL_CULL_FACE);
3471 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3472 glEnable(GL_CULL_FACE);
3476 if(material.flags.noFog)
3482 if(material.baseMap && mesh.texCoords)
3484 Bitmap map = material.baseMap;
3485 glEnable(GL_TEXTURE_2D);
3486 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3488 glMatrixMode(GL_TEXTURE);
3490 if(material.uScale && material.vScale)
3491 glScalef(material.uScale, material.vScale, 1);
3492 glMatrixMode(GL_MODELVIEW);
3494 if(material.flags.tile)
3496 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3497 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3501 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3502 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3506 glDisable(GL_TEXTURE_2D);
3508 if(mesh.flags.colors)
3510 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3511 glEnable(GL_COLOR_MATERIAL);
3515 glDisable(GL_COLOR_MATERIAL);
3517 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3518 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3521 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3522 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3526 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3527 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3530 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3531 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3534 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3537 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3539 OGLMesh oglMesh = mesh.data;
3542 if(!mesh.flags.vertices)
3544 if(oglMesh.vertices)
3546 GLDeleteBuffers(1, &oglMesh.vertices);
3547 oglMesh.vertices = 0;
3549 delete mesh.vertices;
3551 if(!mesh.flags.normals)
3555 GLDeleteBuffers(1, &oglMesh.normals);
3556 oglMesh.normals = 0;
3558 delete mesh.normals;
3560 if(!mesh.flags.texCoords1)
3562 if(oglMesh.texCoords)
3564 GLDeleteBuffers(1, &oglMesh.texCoords);
3565 oglMesh.texCoords = 0;
3567 delete mesh.texCoords;
3569 if(!mesh.flags.texCoords2)
3571 if(oglMesh.texCoords2)
3573 GLDeleteBuffers(1, &oglMesh.texCoords2);
3574 oglMesh.texCoords2 = 0;
3577 delete mesh.texCoords2;
3580 if(!mesh.flags.colors)
3584 GLDeleteBuffers(1, &oglMesh.colors);
3596 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
3598 bool result = false;
3601 mesh.data = OGLMesh { };
3604 OGLMesh oglMesh = mesh.data;
3606 if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
3608 mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3609 GLGenBuffers(1, &oglMesh.vertices);
3611 if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
3613 GLGenBuffers( 1, &oglMesh.normals);
3614 mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3616 if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
3618 GLGenBuffers( 1, &oglMesh.texCoords);
3619 mesh.texCoords = new Pointf[mesh.nVertices];
3621 if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
3623 GLGenBuffers( 1, &oglMesh.colors);
3624 mesh.colors = new ColorRGBAf[mesh.nVertices];
3631 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3633 OGLMesh oglMesh = mesh.data;
3634 if(!flags) flags = mesh.flags;
3638 if(!(flags.vertices) || oglMesh.vertices)
3640 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3641 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 );
3644 if(!(flags.normals) || oglMesh.normals)
3646 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3647 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 );
3650 if(!(flags.texCoords1) || oglMesh.texCoords)
3652 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3653 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3656 if(!(flags.colors) || oglMesh.colors)
3658 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3659 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3662 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3666 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3673 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3677 if(oglIndices.buffer)
3678 GLDeleteBuffers(1, &oglIndices.buffer);
3679 delete oglIndices.indices;
3684 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3686 OGLIndices oglIndices = OGLIndices { };
3689 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3690 GLGenBuffers( 1, &oglIndices.buffer);
3691 oglIndices.nIndices = nIndices;
3696 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3700 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3701 GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3702 oglIndices.indices, GL_STATIC_DRAW_ARB);
3703 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3707 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3710 return oglIndices.indices;
3713 void SelectMesh(Display display, Mesh mesh)
3715 //Logf("SelectMesh\n");
3717 #if !defined( __ANDROID__) && !defined(__APPLE__)
3718 if(display.display3D.mesh && glUnlockArraysEXT)
3719 glUnlockArraysEXT();
3723 OGLMesh oglMesh = mesh.data;
3725 // *** Vertex Stream ***
3726 glEnableClientState(GL_VERTEX_ARRAY);
3727 if(!display.display3D.collectingHits && oglMesh)
3729 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3730 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
3732 // *** Normals Stream ***
3735 glEnableClientState(GL_NORMAL_ARRAY);
3736 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3737 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
3740 glDisableClientState(GL_NORMAL_ARRAY);
3742 // *** Texture Coordinates Stream ***
3745 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3746 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3747 glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
3750 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3752 // *** Color Stream ***
3755 glEnableClientState(GL_COLOR_ARRAY);
3756 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3757 glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
3760 glDisableClientState(GL_COLOR_ARRAY);
3765 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3766 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3767 if(mesh.normals && !display.display3D.collectingHits)
3769 glEnableClientState(GL_NORMAL_ARRAY);
3770 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3773 glDisableClientState(GL_NORMAL_ARRAY);
3774 if(mesh.texCoords && !display.display3D.collectingHits)
3776 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3777 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3780 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3781 if(mesh.colors && !display.display3D.collectingHits)
3783 glEnableClientState(GL_COLOR_ARRAY);
3784 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3787 glDisableClientState(GL_COLOR_ARRAY);
3790 #if !defined(__ANDROID__) && !defined(__APPLE__)
3791 if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
3795 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3798 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3800 //Logf("DrawPrimitives\n");
3802 if(primitive->type.vertexRange)
3803 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3806 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3807 // HACK TO SPEED THINGS UP...
3809 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3810 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3813 glBegin(primitiveTypes[primitive->type.primitiveType]);
3816 OGLIndices oglIndices = primitive->data;
3817 MeshFeatures flags = mesh.flags;
3818 for(c = 0; c<primitive->nIndices; c++)
3820 uint16 index = ((uint16 *) oglIndices.indices)[c];
3821 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3822 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3823 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3824 glVertex3fv((float *)&mesh.vertices[index]);
3832 OGLIndices oglIndices = primitive->data;
3834 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
3836 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3837 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3838 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3839 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3842 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3843 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
3848 void PushMatrix(Display display)
3853 void PopMatrix(Display display, bool setMatrix)
3858 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3860 Matrix matrix = transMatrix;
3861 Camera camera = useCamera ? display.display3D.camera : null;
3866 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3871 matrix.m[3][0] - camera.cPosition.x,
3872 matrix.m[3][1] - camera.cPosition.y,
3873 matrix.m[3][2] - camera.cPosition.z);
3885 glMultMatrixd(matrix.array);
3890 public void UseSingleGLContext(bool useSingle)
3892 useSingleGLContext = useSingle;
3895 default dllexport void * __attribute__((stdcall)) IS_GLGetContext(DisplaySystem displaySystem)
3899 OGLSystem system = displaySystem.driverData;
3900 #if defined(__WIN32__)
3903 return system.glContext;