1 // We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
4 namespace gfx::drivers;
7 #if defined(__unix__) || defined(__APPLE__)
9 #if !defined(__MINGW32__)
10 #define GL_GLEXT_PROTOTYPES
15 //#include <GL/miniglx.h>
19 #if defined(__ANDROID__)
23 #define property _property
27 #define Window X11Window
28 #define Cursor X11Cursor
30 #define Display X11Display
32 #define KeyCode X11KeyCode
33 #define Picture X11Picture
37 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
42 #include <X11/extensions/Xrender.h>
43 #include <X11/extensions/shape.h>
63 #if defined(__APPLE__)
64 #include <OpenGl/gl.h>
67 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
69 #if defined(__WIN32__)
70 #define WIN32_LEAN_AND_MEAN
72 #define _WIN32_WINNT 0x0502
78 #if defined(__ANDROID__)
92 #if defined(__unix__) || defined(__APPLE__)
100 static double nearPlane = 1;
102 #define glLoadMatrix glLoadMatrixd
103 #define glMultMatrix glMultMatrixd
104 #define glGetMatrix glGetDoublev
105 #define glTranslate glTranslated
106 #define glScale glScaled
109 #define glVertex3v glVertex3dv
110 #define glNormal3v glNormal3dv
114 //#ifdef VERTEX_FORMAT_DOUBLE
116 #define glLoadMatrix glLoadMatrixd
117 #define glMultMatrix glMultMatrixd
118 #define glGetMatrix glGetDoublev
119 #define glVertex3v glVertex3dv
120 #define glNormal3v glNormal3dv
121 #define glTranslate glTranslated
122 #define glScale glScaled
123 //#define GL_VERTEX_FORMAT GL_DOUBLE
127 #define glLoadMatrix glLoadMatrixf
128 #define glMultMatrix glMultMatrixf
129 #define glGetMatrix glGetFloatv
130 #define glVertex3v glVertex3fv
131 #define glNormal3v glNormal3fv
132 #define glTranslate glTranslatef
133 #define glScale glScalef
134 //#define GL_VERTEX_FORMAT GL_FLOAT
139 #define GL_ARRAY_BUFFER_ARB 0x8892
140 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
141 #define GL_STATIC_DRAW_ARB 0x88E4
142 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
143 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
145 #define GL_MULTISAMPLE_ARB 0x809D
147 #if defined(__WIN32__)
149 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
150 #define WGL_SAMPLES_ARB 0x2042
152 #define WGL_WGLEXT_VERSION 1
153 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
154 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
155 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
156 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
157 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
158 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
159 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
160 #define WGL_ACCELERATION_ARB 0x2003
161 #define WGL_NEED_PALETTE_ARB 0x2004
162 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
163 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
164 #define WGL_SWAP_METHOD_ARB 0x2007
165 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
166 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
167 #define WGL_TRANSPARENT_ARB 0x200A
168 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
169 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
170 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
171 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
172 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
173 #define WGL_SHARE_DEPTH_ARB 0x200C
174 #define WGL_SHARE_STENCIL_ARB 0x200D
175 #define WGL_SHARE_ACCUM_ARB 0x200E
176 #define WGL_SUPPORT_GDI_ARB 0x200F
177 #define WGL_SUPPORT_OPENGL_ARB 0x2010
178 #define WGL_DOUBLE_BUFFER_ARB 0x2011
179 #define WGL_STEREO_ARB 0x2012
180 #define WGL_PIXEL_TYPE_ARB 0x2013
181 #define WGL_COLOR_BITS_ARB 0x2014
182 #define WGL_RED_BITS_ARB 0x2015
183 #define WGL_RED_SHIFT_ARB 0x2016
184 #define WGL_GREEN_BITS_ARB 0x2017
185 #define WGL_GREEN_SHIFT_ARB 0x2018
186 #define WGL_BLUE_BITS_ARB 0x2019
187 #define WGL_BLUE_SHIFT_ARB 0x201A
188 #define WGL_ALPHA_BITS_ARB 0x201B
189 #define WGL_ALPHA_SHIFT_ARB 0x201C
190 #define WGL_ACCUM_BITS_ARB 0x201D
191 #define WGL_ACCUM_RED_BITS_ARB 0x201E
192 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
193 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
194 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
195 #define WGL_DEPTH_BITS_ARB 0x2022
196 #define WGL_STENCIL_BITS_ARB 0x2023
197 #define WGL_AUX_BUFFERS_ARB 0x2024
198 #define WGL_NO_ACCELERATION_ARB 0x2025
199 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
200 #define WGL_FULL_ACCELERATION_ARB 0x2027
201 #define WGL_SWAP_EXCHANGE_ARB 0x2028
202 #define WGL_SWAP_COPY_ARB 0x2029
203 #define WGL_SWAP_UNDEFINED_ARB 0x202A
204 #define WGL_TYPE_RGBA_ARB 0x202B
205 #define WGL_TYPE_COLORINDEX_ARB 0x202C
206 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
207 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
208 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
209 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
210 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
211 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
212 #define WGL_PBUFFER_LARGEST_ARB 0x2033
213 #define WGL_PBUFFER_WIDTH_ARB 0x2034
214 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
215 #define WGL_PBUFFER_LOST_ARB 0x2036
216 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
217 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
218 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
219 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
220 #define WGL_ACCELERATION_EXT 0x2003
221 #define WGL_NEED_PALETTE_EXT 0x2004
222 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
223 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
224 #define WGL_SWAP_METHOD_EXT 0x2007
225 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
226 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
227 #define WGL_TRANSPARENT_EXT 0x200A
228 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
229 #define WGL_SHARE_DEPTH_EXT 0x200C
230 #define WGL_SHARE_STENCIL_EXT 0x200D
231 #define WGL_SHARE_ACCUM_EXT 0x200E
232 #define WGL_SUPPORT_GDI_EXT 0x200F
233 #define WGL_SUPPORT_OPENGL_EXT 0x2010
234 #define WGL_DOUBLE_BUFFER_EXT 0x2011
235 #define WGL_STEREO_EXT 0x2012
236 #define WGL_PIXEL_TYPE_EXT 0x2013
237 #define WGL_COLOR_BITS_EXT 0x2014
238 #define WGL_RED_BITS_EXT 0x2015
239 #define WGL_RED_SHIFT_EXT 0x2016
240 #define WGL_GREEN_BITS_EXT 0x2017
241 #define WGL_GREEN_SHIFT_EXT 0x2018
242 #define WGL_BLUE_BITS_EXT 0x2019
243 #define WGL_BLUE_SHIFT_EXT 0x201A
244 #define WGL_ALPHA_BITS_EXT 0x201B
245 #define WGL_ALPHA_SHIFT_EXT 0x201C
246 #define WGL_ACCUM_BITS_EXT 0x201D
247 #define WGL_ACCUM_RED_BITS_EXT 0x201E
248 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
249 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
250 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
251 #define WGL_DEPTH_BITS_EXT 0x2022
252 #define WGL_STENCIL_BITS_EXT 0x2023
253 #define WGL_AUX_BUFFERS_EXT 0x2024
254 #define WGL_NO_ACCELERATION_EXT 0x2025
255 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
256 #define WGL_FULL_ACCELERATION_EXT 0x2027
257 #define WGL_SWAP_EXCHANGE_EXT 0x2028
258 #define WGL_SWAP_COPY_EXT 0x2029
259 #define WGL_SWAP_UNDEFINED_EXT 0x202A
260 #define WGL_TYPE_RGBA_EXT 0x202B
261 #define WGL_TYPE_COLORINDEX_EXT 0x202C
262 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
263 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
264 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
265 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
266 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
267 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
268 #define WGL_PBUFFER_LARGEST_EXT 0x2033
269 #define WGL_PBUFFER_WIDTH_EXT 0x2034
270 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
271 #define WGL_DEPTH_FLOAT_EXT 0x2040
272 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
273 #define WGL_SAMPLES_3DFX 0x2061
274 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
275 #define WGL_SAMPLES_EXT 0x2042
276 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
277 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
278 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
279 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
280 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
281 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
282 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
283 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
284 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
285 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
286 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
287 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
288 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
289 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
290 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
291 #define WGL_ARB_buffer_region 1
292 #define WGL_ARB_extensions_string 1
293 #define WGL_ARB_pixel_format 1
294 #define WGL_ARB_make_current_read 1
295 #define WGL_ARB_pbuffer 1
296 #define WGL_EXT_display_color_table 1
297 #define WGL_EXT_extensions_string 1
298 #define WGL_EXT_make_current_read 1
299 #define WGL_EXT_pbuffer 1
300 #define WGL_EXT_pixel_format 1
301 #define WGL_EXT_swap_control 1
302 #define WGL_WGL_EXT_depth_float 1
303 #define WGL_WGL_3DFX_multisample 1
304 #define WGL_WGL_EXT_multisample 1
305 #define WGL_NV_allocate_memory 1
308 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
309 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
310 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
311 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
312 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
316 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
317 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
318 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
319 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
321 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
322 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
323 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
324 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
325 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
326 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
327 typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
328 typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
329 typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
331 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
332 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
333 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
334 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
335 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
336 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
337 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
338 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
340 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
341 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
342 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
343 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
344 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
345 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
346 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
347 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
348 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
349 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
350 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
351 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
352 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
354 #ifdef WGL_WGLEXT_PROTOTYPES
355 extern BOOL WINAPI wglSwapIntervalEXT (int);
356 extern int WINAPI wglGetSwapIntervalEXT (void);
357 #endif /* WGL_WGLEXT_PROTOTYPES */
358 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
359 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
361 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
365 #if defined(__ANDROID__)
367 // Our own matrix stack
368 static Matrix matrixStack[3][32];
369 static int matrixIndex[3];
370 static int curStack = 0;
372 // OpenGL ES Porting Kit
374 #define glBindFramebuffer glBindFramebufferOES
375 #define glBindRenderbuffer glBindRenderbufferOES
376 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
377 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
378 #define glFramebufferTexture2D glFramebufferTexture2DOES
379 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
380 #define glGenFramebuffers glGenFramebuffersOES
381 #define glGenRenderbuffers glGenRenderbuffersOES
382 #define glDeleteFramebuffers glDeleteFramebuffersOES
383 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
385 #define GL_POLYGON_STIPPLE 0xFFFF
386 #define GL_LINE_STIPPLE 0xFFFF
387 #define GL_LINE 0xFFFF
388 #define GL_FILL 0xFFFF
389 #define GL_ALL_ATTRIB_BITS 0xFFFF
390 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
391 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
393 #define GL_UNSIGNED_INT 0x1405
394 #define GL_DOUBLE 0x140A
399 #define glBufferDatai glesBufferDatai
400 #define glBufferDatad glesBufferDatad
401 #define glVertexPointeri glesVertexPointeri
402 #define glVertexPointerd glesVertexPointerd
404 #define glRecti glesRecti
405 #define glBegin glesBegin
406 #define glTexCoord2i glesTexCoord2i
407 #define glVertex2i glesVertex2i
408 #define glTexCoord2d glesTexCoord2d
409 #define glVertex2d glesVertex2d
410 #define glTexCoord2f glesTexCoord2f
411 #define glVertex2f glesVertex2f
412 #define glEnd glesEnd
413 #define glColor3f glesColor3f
414 #define glColor4ub glesColor4ub
415 #define glColor4fv glesColor4fv
416 #define glLineStipple glesLineStipple
417 #define glNormal3fv glesNormal3fv
418 #define glTexCoord2fv glesTexCoord2fv
419 #define glColorMaterial glesColorMaterial
421 #define glLoadMatrixd glesLoadMatrixd
422 #define glMultMatrixd glesMultMatrixd
423 #define glFrustum glesFrustum
424 #define glOrtho glesOrtho
425 #define glScaled glesScaled
426 #define glTranslated glesTranslated
427 #define glRotated glesRotated
428 #define glVertex3d glesVertex3d
429 #define glVertex3f glesVertex3f
430 #define glVertex3fv glesVertex3fv
431 #define glLightModeli glesLightModeli
435 #define GL_QUAD_STRIP 0
436 //#define GL_DOUBLE 0
437 //#define GL_UNSIGNED_INT 0
440 //#define GL_LINE_STIPPLE 0
441 #define GL_BGRA_EXT 0
442 #define GL_UNPACK_ROW_LENGTH 0
443 #define GL_UNPACK_SKIP_PIXELS 0
444 #define GL_UNPACK_SKIP_ROWS 0
446 #define GL_PACK_ROW_LENGTH 0
447 #define GL_PACK_SKIP_ROWS 0
448 #define GL_PACK_SKIP_PIXELS 0
450 static EGLDisplay eglDisplay;
451 static EGLSurface eglSurface;
452 static EGLContext eglContext;
453 static int eglWidth, eglHeight;
455 static bool egl_init_display(ANativeWindow* window)
457 const EGLint attribs[] =
459 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
464 /*EGL_SAMPLE_BUFFERS, 1,
465 EGL_SAMPLES, 0, //2,*/
474 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
475 eglInitialize(display, 0, 0);
476 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
477 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
479 surface = eglCreateWindowSurface(display, config, window, null);
480 context = eglCreateContext(display, config, null, null);
482 if(!eglMakeCurrent(display, surface, surface, context))
485 eglQuerySurface(display, surface, EGL_WIDTH, &w);
486 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
488 eglDisplay = display;
489 eglContext = context;
490 eglSurface = surface;
494 glEnableClientState(GL_VERTEX_ARRAY);
496 // Initialize GL state.
497 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
498 glEnable(GL_CULL_FACE);
499 glShadeModel(GL_SMOOTH);
500 glDisable(GL_DEPTH_TEST);
502 glDisable(GL_CULL_FACE);
503 glDisable(GL_DEPTH_TEST);
505 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
508 matrixStack[0][0].Identity();
509 matrixStack[1][0].Identity();
510 matrixStack[2][0].Identity();
512 glesMatrixMode(GL_MODELVIEW);
513 glScaled(1.0, 1.0, -1.0);
514 glesMatrixMode(GL_PROJECTION);
515 glShadeModel(GL_FLAT);
517 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
518 glFogi(GL_FOG_MODE, GL_EXP);
519 glFogf(GL_FOG_DENSITY, 0);
520 glEnable(GL_NORMALIZE);
521 glDepthFunc(GL_LESS);
523 glDisable(GL_MULTISAMPLE_ARB);
527 glOrtho(0,w,h,0,0.0,1.0);
529 currentVertexBuffer = 0;
533 static void egl_term_display()
537 glDeleteTextures(1, &stippleTexture);
540 if(eglDisplay != EGL_NO_DISPLAY)
542 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
543 if(eglContext != EGL_NO_CONTEXT)
544 eglDestroyContext(eglDisplay, eglContext);
545 if(eglSurface != EGL_NO_SURFACE)
546 eglDestroySurface(eglDisplay, eglSurface);
547 eglTerminate(eglDisplay);
549 eglDisplay = EGL_NO_DISPLAY;
550 eglContext = EGL_NO_CONTEXT;
551 eglSurface = EGL_NO_SURFACE;
554 // OpenGL Immediate Mode Porting Kit
555 static int beginCount;
556 static int vertexCount;
557 static int normalCount;
558 static float *vertexPointer;
559 static float *normalPointer;
560 static GLenum beginMode;
561 static unsigned int beginBufferSize, normalBufferSize;
562 static int numVertexCoords = 2;
564 void glesRecti(int a, int b, int c, int d)
574 void glesBegin(GLenum mode)
581 normalBufferSize = beginBufferSize = 1024; // default number of vertices
582 vertexPointer = new float[beginBufferSize * 5];
583 normalPointer = new float[normalBufferSize * 3];
587 void glesTexCoord2f(float x, float y)
589 int count = vertexCount;
591 if(vertexCount + numVertexCoords > beginBufferSize)
593 beginBufferSize = beginBufferSize + beginBufferSize/2;
594 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
597 vertexPointer[count*(2+numVertexCoords) ] = x;
598 vertexPointer[count*(2+numVertexCoords)+1] = y;
601 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
603 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
604 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
606 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
607 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
611 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
612 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
613 void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
615 void glesVertex2f(float x, float y)
618 if(vertexCount + 4 > beginBufferSize)
620 beginBufferSize = beginBufferSize + beginBufferSize/2;
621 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
624 vertexPointer[vertexCount*4+2] = x;
625 vertexPointer[vertexCount*4+3] = y;
628 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
630 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
631 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
633 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
634 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
639 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
640 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
644 int mode = beginMode;
645 if(mode == GL_QUADS) mode = GL_TRIANGLES;
646 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
648 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
649 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
650 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
651 if(normalCount && normalCount == vertexCount)
653 glEnableClientState(GL_NORMAL_ARRAY);
654 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
657 glDrawArrays(mode, 0, vertexCount);
659 glDisableClientState(GL_NORMAL_ARRAY);
660 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
665 static float *floatVPBuffer = null;
666 static short *shortVPBuffer = null;
667 static unsigned int shortVPSize = 0, floatVPSize = 0;
670 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
671 static unsigned short *shortBDBuffer = null;
672 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
674 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
679 if(numVertices*numCoords > shortVPSize)
681 shortVPSize = numVertices*numCoords;
682 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
684 for(i = 0; i < numVertices*numCoords; i++)
685 shortVPBuffer[i] = (short)pointer[i];
686 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
689 glVertexPointer(numCoords, GL_SHORT, stride, 0);
692 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
697 if(numVertices*numCoords > floatVPSize)
699 floatVPSize = numVertices*numCoords;
700 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
702 for(i = 0; i < numVertices*numCoords; i++)
703 floatVPBuffer[i] = (float)pointer[i];
704 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
707 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
710 void glesTexReuseIntVP(int numCoords)
712 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
715 void glesTexReuseDoubleVP(int numCoords)
717 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
720 void glesColor3f( float r, float g, float b )
722 glColor4f(r, g, b, 1.0f);
725 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
727 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
730 void glesColor4fv(float * a)
732 glColor4f(a[0], a[1], a[2], a[3]);
735 void glesBufferDatad(int target, int size, void * data, int usage)
737 int numElems = size/sizeof(double);
738 double * dblPtr = (double *)data;
740 if (numElems > floatVPSize)
742 floatVPSize = numElems;
743 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
745 for (i=0; i< numElems; i++)
746 floatVPBuffer[i] = (float)dblPtr[i];
748 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
751 void glesBufferDatai(int target, int size, void * data, int usage)
753 int numElems = size/sizeof(unsigned int);
754 unsigned int * pointer = (unsigned int *)data;
756 if (numElems > shortBDSize)
758 shortBDSize = numElems;
759 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
761 for (i=0; i< numElems; i++)
762 shortBDBuffer[i] = (unsigned short)pointer[i];
764 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
767 // *** Our Custom Matrix Stack ***
769 static void LoadCurMatrix()
771 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
774 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
775 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
776 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
777 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
782 void glesLoadIdentity()
784 matrixStack[curStack][matrixIndex[curStack]].Identity();
788 void glesPushMatrix()
790 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
792 matrixIndex[curStack]++;
793 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
799 if(matrixIndex[curStack] > 0)
801 matrixIndex[curStack]--;
806 void glesLoadMatrixd(double * i)
808 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
812 void glesOrtho( double l, double r, double b, double t, double n, double f )
816 (2 / (r - l)), 0, 0, 0,
817 0, (2 / (t - b)), 0, 0,
818 0, 0, (-2 / (f - n)), 0,
819 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
822 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
823 matrixStack[curStack][matrixIndex[curStack]] = res;
827 void glesFrustum( double l, double r, double b, double t, double n, double f )
837 double A = ((r + l) / (r - l));
838 double B = ((t + b) / (t - b));
839 double C = (-(f + n) / (f - n));
840 double D = (-2*f*n/(f-n));
843 (2.0*n / (r - l)), 0, 0, 0,
844 0, (2.0*n / (t - b)), 0, 0,
849 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
850 matrixStack[curStack][matrixIndex[curStack]] = res;
855 void glesRotated( double a, double b, double c, double d )
860 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
861 m.RotationQuaternion(q);
862 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
863 matrixStack[curStack][matrixIndex[curStack]] = r;
866 void glesScaled( double a, double b, double c )
872 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
873 matrixStack[curStack][matrixIndex[curStack]] = r;
877 void glesTranslated( double a, double b, double c )
883 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
884 matrixStack[curStack][matrixIndex[curStack]] = r;
888 void glesMultMatrixd( double * i )
891 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
892 matrixStack[curStack][matrixIndex[curStack]] = r;
896 void glesMatrixMode(int mode)
898 curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
902 #define glPushMatrix glesPushMatrix
903 #define glPopMatrix glesPopMatrix
904 #define glLoadIdentity glesLoadIdentity
905 #define glMatrixMode glesMatrixMode
907 /* Using the built-in matrix stack
908 void glesLoadMatrixd( double * i )
912 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
913 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
914 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
915 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
920 void glesOrtho( double l, double r, double b, double t, double n, double f )
924 { (float)(2 / (r - l)), 0, 0, 0 },
925 { 0, (float)(2 / (t - b)), 0, 0 },
926 { 0, 0, (float)(-2 / (f - n)), 0 },
927 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
929 glMultMatrixf((float *)matrix);
932 void glesFrustum( double l, double r, double b, double t, double n, double f )
934 float A = (float)((r + l) / (r - l));
935 float B = (float)((t + b) / (t - b));
936 float C = (float)(-(f + n) / (f - n));
937 float D = (float)(-2*f*n/(f-n));
940 { (float)(2*n / (r - l)), 0, 0, 0 },
941 { 0, (float)(2*n / (t - b)), 0, 0 },
945 glMultMatrixf((float *)matrix);
948 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
949 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
950 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
952 void glesMultMatrixd( double * i )
956 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
957 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
958 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
959 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
965 // Need to do these...
966 void glesVertex3f( float x, float y, float z )
969 if(vertexCount + 4 > beginBufferSize)
971 beginBufferSize = beginBufferSize + beginBufferSize/2;
972 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
975 vertexPointer[vertexCount*5+2] = x;
976 vertexPointer[vertexCount*5+3] = y;
977 vertexPointer[vertexCount*5+4] = z;
980 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
982 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
983 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
984 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
986 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
987 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
988 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
994 void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
995 void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
997 void glesNormal3f(float x, float y, float z)
999 normalCount = vertexCount;
1000 if(vertexCount + 4 > normalBufferSize)
1002 normalBufferSize = normalBufferSize + normalBufferSize/2;
1003 normalPointer = renew normalPointer float[normalBufferSize * 2];
1006 normalPointer[normalCount*3+0] = x;
1007 normalPointer[normalCount*3+1] = y;
1008 normalPointer[normalCount*3+2] = z;
1011 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1013 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1014 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1015 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1017 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1018 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1019 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1023 void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1024 void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1026 void glesColorMaterial(int a, int b)
1028 PrintLn("glColorMaterial stub");
1031 void glesTerminate()
1033 delete vertexPointer;
1034 delete normalPointer;
1035 beginBufferSize = 0;
1037 delete floatVPBuffer;
1040 delete shortVPBuffer;
1043 delete shortBDBuffer;
1047 static GLuint stippleTexture;
1048 static bool stippleEnabled;
1050 void glesLineStipple( int i, unsigned short j )
1054 for(x = 0; x < 16; x++)
1056 bool v = (j & (1 << x)) != 0;
1057 texture[x] = v ? 0xFFFFFFFF : 0;
1060 glGenTextures(1, &stippleTexture);
1061 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1062 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1063 glEnable(GL_TEXTURE_2D);
1064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1065 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1066 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1067 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1068 glMatrixMode(GL_TEXTURE);
1070 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1071 glScaled(i/16.0, 1, 1.0f);
1072 glTranslated(0.5, 0.5, 0);
1073 glMatrixMode(GL_PROJECTION);
1076 void glesLightModeli( unsigned int pname, int param )
1078 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1079 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1082 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1083 void glFogi( unsigned int pname, int param ) { }
1084 void glPolygonMode( unsigned int i, unsigned int j ) { }
1087 // *** Picking won't be supported for now ***
1088 void glPushName( unsigned int i ) { }
1089 void glLoadName( unsigned int i ) { }
1090 void glPopName() { }
1092 // Probably replace by regular glBlendFunc ...
1093 void glBlendFuncSeparate(int a, int b, int c, int d)
1098 // For direct pixel blitting...
1099 void glRasterPos2d(double a, double b) { }
1100 void glPixelZoom(float a, float b) { }
1101 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1105 /* Non OpenGL ES friendly stuff
1106 #undef GL_UNSIGNED_INT
1111 #undef GL_QUAD_STRIP
1112 #undef GL_POLYGON_STIPPLE
1113 #undef GL_LINE_STIPPLE
1116 #undef GL_ALL_ATTRIB_BITS
1117 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1122 #if !defined(__APPLE__)
1123 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1124 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1125 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1126 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1131 static int currentVertexBuffer;
1133 bool GLSelectVBO(uint vbo)
1135 if(currentVertexBuffer != vbo)
1137 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
1138 currentVertexBuffer = vbo;
1144 void GLGenBuffers(int count, uint * buffer)
1147 glGenBuffers(count, buffer);
1149 #if defined(__WIN32__)
1152 glGenBuffersARB(count, buffer);
1156 void GLDeleteBuffers(int count, GLuint * buffer)
1159 glDeleteBuffers(count, buffer);
1161 #if defined(__WIN32__)
1162 if(glDeleteBuffersARB)
1164 glDeleteBuffersARB(count, buffer);
1168 void GLBindBuffer(int target, uint buffer)
1171 glBindBuffer(target, buffer);
1173 #if defined(__WIN32__)
1176 glBindBufferARB(target, buffer);
1180 public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
1183 if(type == GL_DOUBLE)
1184 glesVertexPointerd(numCoords, stride, pointer, numVertices);
1185 else if(type == GL_INT)
1186 glesVertexPointeri(numCoords, stride, pointer, numVertices);
1189 glVertexPointer(numCoords, glType, stride, ptr);
1192 public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1195 if(type == GL_DOUBLE)
1196 glesBufferDatad(target, size, (void *)data, usage);
1197 else if(type == GL_UNSIGNED_INT)
1198 glesBufferDatai(target, size, (void *)data, usage);
1200 glBufferData(target, size, data, usage);
1203 #if defined(__WIN32__)
1206 glBufferDataARB(target, size, data, usage);
1211 static int displayWidth, displayHeight;
1213 #define GL_CLAMP_TO_EDGE 0x812F
1215 static bool vboAvailable;
1217 static bool useSingleGLContext = false;
1218 class OGLDisplay : struct
1220 #if defined(__WIN32__)
1230 int imageBuffers[2];
1231 byte * pboMemory1, * pboMemory2;
1233 #elif !defined(__ANDROID__)
1234 GLXContext glContext;
1237 XShmSegmentInfo shminfo;
1239 XShmSegmentInfo shminfoShape;
1240 XImage * shapeImage;
1244 X11Picture windowPicture;
1245 X11Picture pixmapPicture;
1247 X11Picture shapePicture;
1250 ColorAlpha * flippingBuffer;
1251 int flipBufH, flipBufW;
1256 class OGLSystem : struct
1261 #if defined(__WIN32__)
1262 PIXELFORMATDESCRIPTOR pfd;
1267 #elif !defined(__ANDROID__)
1268 XVisualInfo * visualInfo;
1269 GLXContext glContext;
1270 GLXDrawable glxDrawable;
1274 class OGLSurface : struct
1281 float foreground[4], background[4], bitmapMult[4];
1284 class OGLMesh : struct
1293 class OGLIndices : struct
1300 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1301 static int primitiveTypes[RenderPrimitiveType] =
1303 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1310 class OpenGLDisplayDriver : DisplayDriver
1312 class_property(name) = "OpenGL";
1314 bool LockSystem(DisplaySystem displaySystem)
1316 #if !defined(__ANDROID__)
1317 OGLSystem oglSystem = displaySystem.driverData;
1318 if(useSingleGLContext) return true;
1319 #if defined(__WIN32__)
1320 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1321 #elif defined(__unix__) || defined(__APPLE__)
1322 //if(previous) return true;
1323 // printf("Making SYSTEM current\n");
1324 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1325 //previous = oglSystem.glContext;
1331 void UnlockSystem(DisplaySystem displaySystem)
1333 if(useSingleGLContext) return;
1334 #if defined(__WIN32__)
1335 wglMakeCurrent(null, null);
1336 #elif defined(__unix__) || defined(__APPLE__)
1337 // printf("Making NULL current\n");
1338 #if defined(__ANDROID__)
1340 glXMakeCurrent(xGlobalDisplay, None, null);
1346 bool Lock(Display display)
1348 #if !defined(__ANDROID__)
1349 OGLDisplay oglDisplay = display.driverData;
1350 if(useSingleGLContext) return true;
1351 #if defined(__WIN32__)
1352 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1353 #elif defined(__unix__) || defined(__APPLE__)
1354 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1355 // printf(" Making DISPLAY current\n");
1356 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1362 void Unlock(Display display)
1364 if(useSingleGLContext) return;
1365 //printf(" Making NULL current\n");
1366 //glXMakeCurrent(xGlobalDisplay, None, null);
1368 LockSystem(display.displaySystem);
1371 void DestroyDisplay(Display display)
1373 OGLDisplay oglDisplay = display.driverData;
1377 #if defined(__WIN32__)
1378 wglMakeCurrent( null, null );
1381 wglDeleteContext(oglDisplay.glrc);
1383 if(oglDisplay.hdc && oglDisplay.pBuffer)
1384 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1386 if(oglDisplay.pBuffer)
1387 wglDestroyPbufferARB(oglDisplay.pBuffer);
1390 ReleaseDC(display.window, oglDisplay.hdc);
1392 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1393 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1395 #elif defined(__unix__) || defined(__APPLE__)
1396 #if defined(__ANDROID__)
1398 if(oglDisplay.shapePixmap)
1399 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1400 if(oglDisplay.pixmap)
1401 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1402 if(oglDisplay.image)
1404 if(oglDisplay.shminfoShape.shmid != -1)
1406 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1407 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1408 shmdt(oglDisplay.shminfo.shmaddr);
1409 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1412 if(oglDisplay.shapeImage)
1414 if(oglDisplay.shminfoShape.shmid != -1)
1416 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1417 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1418 shmdt(oglDisplay.shminfoShape.shmaddr);
1419 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1421 XDestroyImage(oglDisplay.shapeImage);
1422 oglDisplay.shapeImage = None;
1425 glXMakeCurrent(xGlobalDisplay, None, null);
1427 if(oglDisplay.glContext)
1428 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1431 delete oglDisplay.flippingBuffer;
1433 display.driverData = null;
1437 void ::CheckExtensions(OGLSystem oglSystem)
1439 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1441 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1442 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1445 bool CreateDisplaySystem(DisplaySystem displaySystem)
1447 bool result = false;
1448 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1451 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1453 oglSystem.hdc = GetDC(oglSystem.hwnd);
1457 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1458 oglSystem.pfd.nVersion = 1;
1459 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1460 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1461 oglSystem.pfd.cColorBits = 24;
1462 oglSystem.pfd.cAlphaBits = 8;
1463 oglSystem.pfd.cDepthBits = 24;
1464 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1466 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1467 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1469 if(oglSystem.pfd.cColorBits > 8)
1471 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1472 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1475 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1477 // Get Pointers To The GL Functions
1478 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1479 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1480 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1481 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1482 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1483 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1484 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1485 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1486 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1487 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1488 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1489 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1491 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1492 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1493 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1494 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1495 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1496 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1497 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1498 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1499 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1501 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1503 vboAvailable = glBindBufferARB != null;
1505 // eSystem_LoggingMode(LOG_MSGBOX, null);
1507 if(wglChoosePixelFormatARB)
1512 float fAttributes[] = {0,0};
1515 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1516 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1517 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1518 WGL_COLOR_BITS_ARB,24,
1519 WGL_ALPHA_BITS_ARB,8,
1520 WGL_DEPTH_BITS_ARB,16,
1521 WGL_STENCIL_BITS_ARB,0,
1522 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1523 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1524 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1528 //Log("Found wglChoosePixelFormatARB\n");
1530 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1531 if(!valid || !numFormats)
1533 //Log("Can't find 4x multi sampling\n");
1534 iAttributes[19] = 2;
1535 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1536 if(!valid || !numFormats)
1538 // Log("Can't find 2x multi sampling\n");
1539 iAttributes[16] = 0;
1540 iAttributes[17] = 0;
1541 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1544 if(valid && numFormats)
1546 oglSystem.format = pixelFormat;
1547 wglMakeCurrent(null, null);
1548 wglDeleteContext(oglSystem.glrc);
1550 // *** DescribePixelFormat does not support WGL pixel formats! ***
1551 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1552 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1553 //Log("Successfully set pixel format\n");
1555 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1556 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1560 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1564 CheckExtensions(oglSystem);
1566 wglMakeCurrent(null, null);
1568 //eSystem_DumpErrors(true);
1572 #elif defined(__unix__) || defined(__APPLE__)
1573 vboAvailable = true;
1574 #if defined(__ANDROID__)
1575 egl_init_display(guiApp.desktop.windowHandle);
1576 CheckExtensions(oglSystem);
1580 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1581 XSetWindowAttributes attr;
1586 #ifndef ECERE_MINIGLX
1587 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1590 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1594 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1595 attr.background_pixel = 0;
1596 attr.border_pixel = 0;
1597 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1598 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1599 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1601 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1602 oglSystem.visualInfo->visual, mask, &attr );
1604 if(oglSystem.visualInfo)
1606 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1607 if(oglSystem.glContext)
1609 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1611 CheckExtensions(oglSystem);
1612 glXMakeCurrent(xGlobalDisplay, None, null);
1619 displaySystem.flags.alpha = true;
1620 displaySystem.flags.flipping = true;
1621 displaySystem.pixelFormat = pixelFormat888;
1625 void DestroyDisplaySystem(DisplaySystem displaySystem)
1627 OGLSystem oglSystem = displaySystem.driverData;
1629 #if defined(__WIN32__)
1630 wglMakeCurrent( null, null );
1633 wglDeleteContext(oglSystem.glrc);
1636 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1637 DestroyWindow(oglSystem.hwnd);
1639 #elif defined(__unix__) || defined(__APPLE__)
1640 #if defined(__ANDROID__)
1643 if(oglSystem.visualInfo)
1645 #ifdef ECERE_MINIGLX
1646 __miniglx_XFree(oglSystem.visualInfo);
1648 XFree(oglSystem.visualInfo);
1652 if(oglSystem.glxDrawable)
1654 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1655 oglSystem.glxDrawable = 0;
1662 bool CreateDisplay(Display display)
1664 bool result = false;
1665 OGLDisplay oglDisplay = display.driverData;
1666 #if !defined(__ANDROID__)
1667 OGLSystem oglSystem = display.displaySystem.driverData;
1670 oglDisplay = display.driverData = OGLDisplay { };
1671 //printf("Inside CreateDisplay\n");
1673 #if defined(__WIN32__) || defined(USEPBUFFER)
1674 if(!display.alphaBlend)
1677 #if defined(__WIN32__)
1678 oglDisplay.hdc = GetDC(display.window);
1679 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1680 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1682 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1683 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1687 ReleaseDC(display.window, oglDisplay.hdc);
1688 #elif defined(__unix__) || defined(__APPLE__)
1689 #if defined(__ANDROID__)
1691 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1693 #if defined(__APPLE__)
1694 XVisualInfo template = { 0 };
1695 XWindowAttributes winAttr;
1697 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1698 template.visualid = XVisualIDFromVisual(winAttr.visual);
1699 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1701 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1702 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1703 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1704 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1706 // visualInfo = oglSystem.visualInfo;
1711 //printf("visualInfo is not null\n");
1712 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1713 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1714 //XFree(visualInfo);
1717 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1718 if(oglDisplay.glContext)
1720 //printf("CreateDisplay Got a Context\n");
1721 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1727 #if defined(__WIN32__) || defined(USEPBUFFER)
1733 #if defined(__WIN32__)
1734 if(glBlendFuncSeparate)
1735 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1737 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1739 #if !defined(__OLDX__)
1740 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1742 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1747 glMatrixMode(GL_MODELVIEW);
1748 glScaled(1.0, 1.0, -1.0);
1749 // glTranslatef(0.375f, 0.375f, 0.0f);
1750 // glTranslatef(-0.625f, -0.625f, 0.0f);
1751 glMatrixMode(GL_PROJECTION);
1752 glShadeModel(GL_FLAT);
1754 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1755 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1756 glFogi(GL_FOG_MODE, GL_EXP);
1757 glFogf(GL_FOG_DENSITY, 0);
1758 glEnable(GL_NORMALIZE);
1759 glDepthFunc(GL_LESS);
1761 glDisable(GL_MULTISAMPLE_ARB);
1763 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1764 display.ambient = Color { 50,50,50 };
1767 if(!useSingleGLContext)
1769 #if defined(__WIN32__)
1770 wglMakeCurrent(null, null);
1771 #elif defined(__unix__) || defined(__APPLE__)
1772 #if defined(__ANDROID__)
1775 glXMakeCurrent(xGlobalDisplay, None, null);
1783 bool DisplaySize(Display display, int width, int height)
1785 OGLDisplay oglDisplay = display.driverData;
1787 bool result = false;
1789 //printf("Inside DisplaySize\n");
1790 #if defined(__WIN32__) || defined(USEPBUFFER)
1791 OGLSystem oglSystem = display.displaySystem.driverData;
1792 if(display.alphaBlend)
1794 #if defined(__WIN32__)
1795 const int attributes[]=
1797 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1798 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1800 int pixelFormat = 0;
1801 if(wglChoosePixelFormatARB)
1805 float fAttributes[] = {0,0};
1808 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1809 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1810 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1811 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1812 WGL_COLOR_BITS_ARB,24,
1813 WGL_ALPHA_BITS_ARB,8,
1814 WGL_DEPTH_BITS_ARB,16,
1815 WGL_STENCIL_BITS_ARB,0,
1816 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1817 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1818 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1822 //Log("Found wglChoosePixelFormatARB\n");
1824 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1825 if(!valid || !numFormats)
1827 //Log("Can't find 4x multi sampling\n");
1828 iAttributes[19] = 2;
1829 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1830 if(!valid || !numFormats)
1832 // Log("Can't find 2x multi sampling\n");
1833 iAttributes[16] = 0;
1834 iAttributes[17] = 0;
1835 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1836 if(!valid || !numFormats)
1840 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1841 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1842 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1843 WGL_COLOR_BITS_ARB,24,
1844 WGL_ALPHA_BITS_ARB,8,
1845 WGL_DEPTH_BITS_ARB,16,
1848 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1852 if(valid && numFormats)
1854 wglMakeCurrent(null, null);
1858 wglMakeCurrent( null, null );
1859 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1860 if(oglDisplay.hdc && oglDisplay.pBuffer)
1861 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1863 wglDestroyPbufferARB(oglDisplay.pBuffer);
1865 if(!useSingleGLContext)
1866 wglMakeCurrent( null, null );
1869 wglDeleteContext(oglDisplay.glrc);
1871 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1872 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1873 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1876 HDC hdc = GetDC(display.window);
1878 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1879 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1881 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1882 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1884 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1886 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1890 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1891 oglDisplay.memDC = CreateCompatibleDC(hdc);
1892 SetMapMode(oglDisplay.memDC, MM_TEXT);
1893 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1894 info->bmiHeader.biPlanes = 1;
1895 info->bmiHeader.biCompression = BI_RGB;
1896 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1897 info->bmiHeader.biWidth = width;
1898 info->bmiHeader.biHeight = height;
1899 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1902 SelectObject(oglDisplay.memDC, newBitmap);
1903 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1906 PIXELFORMATDESCRIPTOR pfd = { 0 };
1907 pfd.nSize = (short)sizeof(pfd);
1909 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1910 pfd.iPixelType = PFD_TYPE_RGBA;
1911 pfd.cColorBits = 32;
1912 //pfd.cAlphaBits = 8;
1913 pfd.cDepthBits = 24;
1914 pfd.iLayerType = PFD_MAIN_PLANE;
1916 oglDisplay.hdc = oglDisplay.memDC;
1918 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1919 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1920 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1922 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1923 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1924 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1929 const int imageSize = width * height * 4;
1931 glGenBuffersARB(2, oglDisplay.imageBuffers);
1933 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1934 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1935 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1936 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1939 oglDisplay.memBitmap = newBitmap;
1940 oglDisplay.stride = width;
1946 ReleaseDC(display.window, hdc);
1948 #elif defined(__unix__) || defined(__APPLE__)
1949 #if defined(__ANDROID__)
1954 GLX_DOUBLEBUFFER, True,
1960 GLX_STENCIL_SIZE, 1,
1961 //GLX_DEPTH_SIZE, 24,
1962 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1963 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1969 GLX_PBUFFER_WIDTH, width,
1970 GLX_PBUFFER_HEIGHT, height,
1971 GLX_LARGEST_PBUFFER, False,
1975 // choose a pixel format that meets our minimum requirements
1978 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1981 if(oglDisplay.pixmap)
1983 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1984 oglDisplay.pixmap = None;
1986 if(oglDisplay.shapePixmap)
1988 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1989 oglDisplay.shapePixmap = None;
1992 // Free Shared Memory Pixmap
1993 if(oglDisplay.image)
1995 if(oglDisplay.shminfoShape.shmid != -1)
1997 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1998 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1999 shmdt(oglDisplay.shminfo.shmaddr);
2000 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
2002 XDestroyImage(oglDisplay.image);
2003 oglDisplay.image = None;
2005 if(oglDisplay.shapeImage)
2007 if(oglDisplay.shminfoShape.shmid != -1)
2009 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
2010 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2011 shmdt(oglDisplay.shminfoShape.shmaddr);
2012 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
2014 XDestroyImage(oglDisplay.shapeImage);
2015 oglDisplay.shapeImage = None;
2018 if(oglDisplay.windowPicture)
2019 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
2020 if(oglDisplay.pixmapPicture)
2021 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
2023 if(oglDisplay.pixmap)
2024 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2026 if(oglDisplay.glContext)
2027 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
2028 if(oglDisplay.pBuffer)
2029 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
2031 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
2032 if(oglDisplay.pBuffer)
2034 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2035 if(oglDisplay.glContext)
2037 glXMakeCurrent(xGlobalDisplay, None, null);
2038 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2040 // Initialize Shared Memory Pixmap
2041 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2042 ZPixmap, null, &oglDisplay.shminfo, width, height);
2043 if(oglDisplay.image)
2045 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2046 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2047 if(oglDisplay.shminfo.shmid != -1)
2049 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2050 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2052 oglDisplay.shminfo.readOnly = False;
2053 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2055 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2056 &oglDisplay.shminfo, width, height, 32);
2058 // Initialize Shared Memory Shape Pixmap
2059 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2060 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2061 if(oglDisplay.shapeImage)
2063 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2064 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2065 if(oglDisplay.shminfoShape.shmid != -1)
2067 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2068 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2070 oglDisplay.shminfoShape.readOnly = False;
2071 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2073 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2074 &oglDisplay.shminfoShape, width, height, 1);
2075 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2078 XRenderPictureAttributes attributes = { 0 };
2079 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2080 #if !defined(__APPLE__) && !defined(__OLDX__)
2081 attributes.repeat = RepeatNormal;
2083 attributes.repeat = 1;
2085 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2086 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2087 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2088 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2091 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2092 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2109 CreateDisplay(display);
2110 #if defined(__WIN32__)
2111 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2112 #elif defined(__unix__) || defined(__APPLE__)
2113 #if defined(__ANDROID__)
2117 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2124 if(!result && display.alphaBlend)
2126 printf("Alpha blending windows not supported on this display\n");
2133 glViewport(0,0,width,height);
2135 glOrtho(0,width,height,0,0.0,1.0);
2136 displayWidth = display.width = width;
2137 displayHeight = display.height = height;
2139 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2141 oglDisplay.flipBufW = width;
2142 oglDisplay.flipBufH = height;
2146 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2149 if(oglDisplay.flippingBuffer || !width || !height)
2155 void DisplayPosition(Display display, int x, int y)
2157 OGLDisplay oglDisplay = display.driverData;
2163 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2167 void RestorePalette(Display display)
2171 void StartUpdate(Display display)
2175 void EndUpdate(Display display)
2179 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2183 void Update(Display display, Box updateBox)
2185 #if defined(__WIN32__) || defined(USEPBUFFER)
2186 OGLDisplay oglDisplay = display.driverData;
2188 //Logf("DisplayScreen\n");
2192 #if defined(__WIN32__) || defined(USEPBUFFER)
2193 if(display.alphaBlend)
2195 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2196 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2197 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2198 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2199 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2202 #if defined(__WIN32__)
2204 POINT point = { oglDisplay.x, oglDisplay.y};
2205 POINT srcPoint = { 0, 0 };
2206 BLENDFUNCTION blend = { 0 };
2208 size.cx = display.width;
2209 size.cy = display.height;
2210 blend.BlendOp = AC_SRC_OVER;
2211 blend.BlendFlags = 0;
2212 blend.SourceConstantAlpha = 255;
2213 blend.AlphaFormat = AC_SRC_ALPHA;
2216 // Process partial images. Mapping the buffer waits for
2217 // outstanding DMA transfers into the buffer to finish.
2218 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2219 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2221 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2222 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2225 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2226 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2229 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2232 // Unmap the image buffers
2233 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2234 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2236 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2237 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2239 // Bind two different buffer objects and start the glReadPixels
2240 // asynchronously. Each call will return directly after
2241 // starting the DMA transfer.
2242 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2243 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2245 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2246 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2250 #elif defined(__unix__) || defined(__APPLE__)
2251 #if defined(__ANDROID__)
2253 XTransform transform =
2256 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2257 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2258 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2261 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2262 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2263 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2264 #if !defined(__APPLE__) && !defined(__OLDX__)
2265 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2267 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2269 XFlush(xGlobalDisplay);
2277 #if defined(__WIN32__)
2278 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2279 SwapBuffers(oglDisplay.hdc);
2280 #elif defined(__unix__) || defined(__APPLE__)
2281 #if defined(__ANDROID__)
2282 eglSwapBuffers(eglDisplay, eglSurface);
2284 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2288 //Logf("Out of DisplayScreen\n");
2291 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2293 if(bitmap.driverData)
2295 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2296 glDeleteTextures(1, &tex);
2297 bitmap.driverData = 0;
2299 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2302 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2304 OGLSystem oglSystem = displaySystem.driverData;
2305 bool result = false;
2307 GLuint glBitmap = 0;
2309 uint w = width, h = height;
2310 if(oglSystem.pow2textures)
2315 w = Min(w, oglSystem.maxTextureSize);
2316 h = Min(h, oglSystem.maxTextureSize);
2318 glGenTextures(1, &glBitmap);
2319 glBindTexture(GL_TEXTURE_2D, glBitmap);
2321 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2323 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2329 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2331 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2333 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2334 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2338 bitmap.driverData = (void *)(uintptr)glBitmap;
2339 bitmap.driver = displaySystem.driver;
2347 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2349 bool result = false;
2350 OGLSystem oglSystem = displaySystem.driverData;
2352 // Pre process the bitmap... First make it 32 bit
2353 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
2356 uint w = bitmap.width, h = bitmap.height;
2357 GLuint glBitmap = 0;
2358 if(oglSystem.pow2textures)
2363 w = Min(w, oglSystem.maxTextureSize);
2364 h = Min(h, oglSystem.maxTextureSize);
2368 while(w * 2 < h) w *= 2;
2369 while(h * 2 < w) h *= 2;
2372 // Switch ARGB to RGBA
2373 //if(bitmap.format != pixelFormatRGBA)
2375 for(c=0; c<bitmap.size; c++)
2377 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2379 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
2380 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2383 bitmap.pixelFormat = pixelFormat888;
2386 glGenTextures(1, &glBitmap);
2389 //int error = glGetError();
2393 glBindTexture(GL_TEXTURE_2D, glBitmap);
2394 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2396 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2397 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2398 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2400 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2401 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2406 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2410 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2413 if(bitmap.width != w || bitmap.height != h)
2415 mipMap = Bitmap { };
2416 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
2418 Surface mipSurface = mipMap.GetSurface(0,0,null);
2419 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
2436 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2437 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2438 //printf("Calling glTexImage2D\n");
2439 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2440 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2441 if((error = glGetError()))
2443 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2444 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2448 if(mipMap != bitmap)
2453 if(!bitmap.keepData)
2454 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
2455 bitmap.driverData = (void *)(uintptr)glBitmap;
2456 bitmap.driver = displaySystem.driver;
2459 FreeBitmap(displaySystem, bitmap);
2460 else if(oglSystem.loadingFont)
2462 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2463 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2464 oglSystem.loadingFont = false;
2470 void ReleaseSurface(Display display, Surface surface)
2472 glDisable(GL_SCISSOR_TEST);
2473 delete surface.driverData;
2474 surface.driverData = null;
2477 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2482 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2484 bool result = false;
2485 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2487 //Logf("GetSurface\n");
2491 if(displayWidth != display.width || displayHeight != display.height)
2493 displayWidth = display.width;
2494 displayHeight = display.height;
2496 glViewport(0,0,display.width,display.height);
2498 glOrtho(0,display.width,display.height,0,0.0,1.0);
2501 surface.offset.x = x;
2502 surface.offset.y = y;
2503 surface.unclippedBox = surface.box = clip;
2504 oglSurface.bitmapMult[0] = 1;
2505 oglSurface.bitmapMult[1] = 1;
2506 oglSurface.bitmapMult[2] = 1;
2507 oglSurface.bitmapMult[3] = 1;
2509 glEnable(GL_SCISSOR_TEST);
2512 (display.height) -(y+clip.bottom)-1,
2513 clip.right-clip.left+1,
2514 clip.bottom-clip.top+1);
2520 void Clip(Display display, Surface surface, Box clip)
2529 box.Clip(surface.unclippedBox);
2533 box = surface.box = surface.unclippedBox;
2534 box.left += surface.offset.x;
2535 box.top += surface.offset.y;
2536 box.right+= surface.offset.x;
2537 box.bottom += surface.offset.y;
2540 box.left,display.height - box.bottom - 1,
2541 box.right-box.left+1, box.bottom-box.top+1);
2544 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2546 bool result = false;
2547 OGLDisplay oglDisplay = display.driverData;
2548 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2550 if(oglDisplay.flippingBuffer)
2552 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2555 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2561 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2562 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2563 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2564 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2565 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2568 for(row = 0; row<h; row++)
2569 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2576 void SetForeground(Display display, Surface surface, ColorAlpha color)
2578 OGLSurface oglSurface = surface.driverData;
2580 //Logf("SetForeground\n");
2582 oglSurface.foreground[0] = color.color.r/255.0f;
2583 oglSurface.foreground[1] = color.color.g/255.0f;
2584 oglSurface.foreground[2] = color.color.b/255.0f;
2585 //oglSurface.foreground[3] = 1.0f;
2586 oglSurface.foreground[3] = color.a/255.0f;
2588 //if(!oglSurface.foreground[3])printf("bug");
2591 void SetBackground(Display display, Surface surface, ColorAlpha color)
2593 OGLSurface oglSurface = surface.driverData;
2595 //Logf("SetBackground\n");
2597 oglSurface.background[0] = color.color.r/255.0f;
2598 oglSurface.background[1] = color.color.g/255.0f;
2599 oglSurface.background[2] = color.color.b/255.0f;
2600 //oglSurface.background[3] = 1.0;
2601 oglSurface.background[3] = color.a/255.0f;
2604 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2606 OGLSurface oglSurface = surface.driverData;
2608 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2609 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2610 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2611 oglSurface.bitmapMult[3] = color.a/255.0f;
2614 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2619 void PutPixel(Display display, Surface surface,int x,int y)
2621 OGLSurface oglSurface = surface.driverData;
2623 //Logf("PutPixel\n");
2625 glColor4fv(oglSurface.foreground);
2627 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2628 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2633 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2635 OGLSurface oglSurface = surface.driverData;
2636 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2651 x1 += surface.offset.x;
2652 y1 += surface.offset.y;
2653 x2 += surface.offset.x;
2654 y2 += surface.offset.y;
2658 glColor4fv(oglSurface.foreground);
2663 glTexCoord2f(0.5f, 0);
2664 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2665 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2666 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2675 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2676 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2682 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2684 OGLSurface oglSurface = surface.driverData;
2685 x1 += surface.offset.x;
2686 y1 += surface.offset.y;
2687 x2 += surface.offset.x;
2688 y2 += surface.offset.y;
2690 //Logf("Rectangle\n");
2692 glColor4fv(oglSurface.foreground);
2698 glTexCoord2f(0.5f, 0);
2699 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2700 glTexCoord2f(y2-y1 + 0.5f, 0);
2701 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2703 glTexCoord2f(0.5f, 0);
2704 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2705 glTexCoord2f(x2 - x1 + 0.5f, 0);
2706 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2708 glTexCoord2f(0.5f, 0);
2709 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2710 glTexCoord2f(y1 - y2 + 0.5f, 0);
2711 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2713 glTexCoord2f(0.5f, 0);
2714 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2715 glTexCoord2f(x1 - x2 + 0.5f, 0);
2716 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2721 glBegin(GL_LINE_LOOP);
2728 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2729 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2730 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2731 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2736 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2738 OGLSurface oglSurface = surface.driverData;
2741 glColor4fv(oglSurface.background);
2742 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2743 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2746 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2747 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2751 void Clear(Display display, Surface surface, ClearType type)
2753 OGLDisplay oglDisplay = display.driverData;
2754 OGLSurface oglSurface = surface.driverData;
2757 if(type != depthBuffer)
2758 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2759 if(type != colorBuffer && !oglDisplay.depthWrite)
2761 glDepthMask((byte)bool::true);
2763 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2764 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2765 if(type != colorBuffer && !oglDisplay.depthWrite)
2767 glDepthMask((byte)bool::false);
2771 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2776 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2778 OGLSurface oglSurface = surface.driverData;
2780 #if !defined(__OLDX__)
2781 // WHY DO WE HAVE GL_ONE HERE ?
2782 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2783 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2786 if(!oglSurface.writingText)
2788 // glTranslatef(-0.375f, -0.375f, 0.0f);
2789 glEnable(GL_TEXTURE_2D);
2790 glColor4fv(oglSurface.bitmapMult);
2792 else if(oglSurface.xOffset)
2793 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2795 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2800 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2801 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2802 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2803 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2804 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2805 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2806 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2807 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2812 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2813 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2814 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2815 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2816 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2817 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2818 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2819 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2822 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2823 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2824 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2825 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2826 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2827 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2828 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2829 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2833 if(!oglSurface.writingText)
2835 glDisable(GL_TEXTURE_2D);
2837 //glTranslate(0.375, 0.375, 0.0);
2839 else if(oglSurface.xOffset)
2840 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2842 #if !defined(__OLDX__)
2843 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2844 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2848 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2850 OGLSurface oglSurface = surface.driverData;
2852 //glTranslate(-0.375, -0.375, 0.0);
2854 //Logf("Stretch\n");
2856 #if !defined(__OLDX__)
2857 /*if(glBlendFuncSeparate)
2858 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2861 glEnable(GL_TEXTURE_2D);
2862 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2864 glColor4fv(oglSurface.bitmapMult);
2870 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2871 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2873 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2874 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2876 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2877 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2879 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2880 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2884 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2885 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2887 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2888 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2890 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2891 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2893 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2894 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2899 glDisable(GL_TEXTURE_2D);
2901 //glTranslate(0.375, 0.375, 0.0);
2902 #if !defined(__OLDX__)
2903 /*if(glBlendFuncSeparate)
2904 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2909 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2911 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2914 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2916 float s2dw,s2dh,d2sw,d2sh;
2917 //bool flipX = false, flipY = false;
2919 //Logf("StretchDI\n");
2921 if(Sgn(w) != Sgn(sw))
2927 if(Sgn(h) != Sgn(sh))
2939 //Clip against the edges of the source
2942 dx+=(int)((0-sx) * s2dw);
2943 w-=(int)((0-sx) * s2dw);
2949 dy+=(int)((0-sy) * s2dh);
2950 h-=(int)((0-sy) * s2dh);
2955 if(sx+sw>bitmap.width-1)
2957 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2958 sw-=sx+sw-(bitmap.width-1)-1;
2960 if(sy+sh>(bitmap.height-1))
2962 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2963 sh-=sy+sh-(bitmap.height-1)-1;
2965 //Clip against the edges of the surfaceination
2966 if(dx<surface.box.left)
2969 sx+=(int)((surface.box.left-dx)*d2sw);
2970 sw-=(int)((surface.box.left-dx)*d2sw);
2971 w-=surface.box.left-dx;
2972 dx=surface.box.left;
2974 if(dy<surface.box.top)
2976 sy+=(int)((surface.box.top-dy)*d2sh);
2977 sh-=(int)((surface.box.top-dy)*d2sh);
2978 h-=surface.box.top-dy;
2981 if(dx+w>surface.box.right)
2983 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2984 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2985 w-=dx+w-surface.box.right-1;
2987 if(dy+h>surface.box.bottom)
2989 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2990 h-=dy+h-surface.box.bottom-1;
2992 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2994 dx += surface.offset.x;
2995 dy += surface.offset.y;
2997 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2999 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3000 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3001 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3002 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3003 glRasterPos2d(dx,dy);
3004 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
3005 glPixelZoom(s2dw, -s2dh);
3006 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3007 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3008 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3009 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3010 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3014 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3018 //Clip against the edges of the source
3031 if(sx+w>bitmap.width-1)
3032 w-=sx+w-(bitmap.width-1)-1;
3033 if(sy+h>bitmap.height-1)
3034 h-=sy+h-(bitmap.height-1)-1;
3035 //Clip against the edges of the surfaceination
3036 if(dx<surface.box.left)
3039 sx+=surface.box.left-dx;
3040 w-=surface.box.left-dx;
3041 dx=surface.box.left;
3043 if(dy<surface.box.top)
3045 sy+=surface.box.top-dy;
3046 h-=surface.box.top-dy;
3049 if(dx+w>surface.box.right)
3051 //if(flip) sx+=dx+w-surface.box.right-1;
3052 w-=dx+w-surface.box.right-1;
3054 if(dy+h>surface.box.bottom)
3055 h-=dy+h-surface.box.bottom-1;
3059 dx += surface.offset.x;
3060 dy += surface.offset.y;
3062 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3064 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3065 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3066 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3067 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3068 glRasterPos2d(dx,dy);
3070 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3071 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3072 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3073 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3074 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3078 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3080 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3083 void UnloadFont(DisplaySystem displaySystem, Font font)
3085 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3088 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
3091 OGLSystem oglSystem = displaySystem.driverData;
3092 oglSystem.loadingFont = true;
3093 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3097 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
3099 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3102 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
3104 OGLSurface oglSurface = surface.driverData;
3105 OGLSystem oglSystem = display.displaySystem.driverData;
3106 oglSystem.loadingFont = true;
3108 //glTranslated(-0.375, -0.375, 0.0);
3112 if(surface.textOpacity)
3115 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3116 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3119 oglSurface.writingText = true;
3121 glEnable(GL_TEXTURE_2D);
3122 glColor4fv(oglSurface.foreground);
3124 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3125 oglSurface.writingText = false;
3126 oglSystem.loadingFont = false;
3128 glDisable(GL_TEXTURE_2D);
3130 //glTranslated(0.375, 0.375, 0.0);
3133 void TextFont(Display display, Surface surface, Font font)
3135 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3138 void TextOpacity(Display display, Surface surface, bool opaque)
3140 OGLSurface oglSurface = surface.driverData;
3141 oglSurface.opaqueText = opaque;
3144 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
3146 OGLSurface oglSurface = surface.driverData;
3147 OGLSystem oglSystem = display.displaySystem.driverData;
3148 oglSystem.loadingFont = true;
3149 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3150 oglSystem.loadingFont = false;
3153 void DrawingChar(Display display, Surface surface, char character)
3158 void LineStipple(Display display, Surface surface, uint32 stipple)
3160 //Logf("Stipple\n");
3164 #if defined(__ANDROID__)
3165 stippleEnabled = true;
3166 glesLineStipple(1, (uint16)stipple);
3168 glLineStipple(1, (uint16)stipple);
3169 glEnable(GL_LINE_STIPPLE);
3174 #if defined(__ANDROID__)
3175 stippleEnabled = false;
3176 glMatrixMode(GL_TEXTURE);
3178 glMatrixMode(GL_PROJECTION);
3179 glDisable(GL_TEXTURE_2D);
3181 glDisable(GL_LINE_STIPPLE);
3185 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3186 void SetRenderState(Display display, RenderState state, uint value)
3188 OGLDisplay oglDisplay = display.driverData;
3189 //Logf("RenderState\n");
3195 glEnable(GL_MULTISAMPLE_ARB);
3197 glDisable(GL_MULTISAMPLE_ARB);
3200 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3203 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3206 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3207 oglDisplay.depthWrite = (bool)value;
3211 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3212 glFogfv(GL_FOG_COLOR, (float *)&color);
3216 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
3219 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3223 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3224 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3229 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3234 #if defined(__WIN32__)
3235 wglSwapIntervalEXT(value ? 1 : 0);
3242 void SetLight(Display display, int id, Light light)
3244 //Logf("SetLight\n");
3248 Object lightObject = light.lightObject;
3249 float position[4] = { 0, 0, 0, 0 };
3250 float color[4] = { 0, 0, 0, 1 };
3252 glEnable(GL_LIGHT0 + id);
3254 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3255 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3256 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3259 if(!light.multiplier) light.multiplier = 1.0f;
3261 color[0] = light.diffuse.r * light.multiplier;
3262 color[1] = light.diffuse.g * light.multiplier;
3263 color[2] = light.diffuse.b * light.multiplier;
3264 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3266 color[0] = light.ambient.r * light.multiplier;
3267 color[1] = light.ambient.g * light.multiplier;
3268 color[2] = light.ambient.b * light.multiplier;
3269 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3270 color[0] = light.specular.r * light.multiplier;
3271 color[1] = light.specular.g * light.multiplier;
3272 color[2] = light.specular.b * light.multiplier;
3273 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3277 Vector3D positionVector;
3278 if(light.flags.spot)
3280 if(lightObject.flags.root || !lightObject.parent)
3282 positionVector = lightObject.transform.position;
3283 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3287 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3288 if(display.display3D.camera)
3289 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3295 if(!light.direction.x && !light.direction.y && !light.direction.z)
3297 Vector3Df vector { 0,0,-1 };
3299 mat.RotationQuaternion(light.orientation);
3300 positionVector.MultMatrixf(vector, mat);
3304 positionVector = light.direction;
3309 position[0] = (float)positionVector.x;
3310 position[1] = (float)positionVector.y;
3311 position[2] = (float)positionVector.z;
3313 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3316 // Display Light Position
3317 glDisable(GL_LIGHTING);
3318 glDisable(GL_DEPTH_TEST);
3322 glVertex3fv(position);
3324 glEnable(GL_DEPTH_TEST);
3325 glEnable(GL_LIGHTING);
3329 if(lightObject.flags.root || !lightObject.parent)
3331 positionVector = light.target.transform.position;
3332 positionVector.Subtract(positionVector, display.camera.cPosition);
3336 positionVector.MultMatrix(light.target.transform.position,
3337 lightObject.light.target.parent.matrix);
3338 positionVector.Subtract(positionVector, display.camera.cPosition);
3341 position[0] = positionVector.x;
3342 position[1] = positionVector.y;
3343 position[2] = positionVector.z;
3345 glDisable(GL_LIGHTING);
3346 glDisable(GL_DEPTH_TEST);
3350 glVertex3fv(position);
3352 glEnable(GL_DEPTH_TEST);
3353 glEnable(GL_LIGHTING);
3356 if(light.flags.attenuation)
3358 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3359 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3360 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3363 if(light.flags.spot)
3366 #define MAXLIGHT 0.9
3367 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3368 // Figure out exponent out of the hot spot
3369 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3371 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3372 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3373 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3379 Vector3Df vector { 0,0,-1 };
3380 Vector3Df direction;
3383 mat.RotationQuaternion(light.orientation);
3384 direction.MultMatrix(vector, mat);
3386 position[0] = direction.x;
3387 position[1] = direction.y;
3388 position[2] = direction.z;
3390 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3394 glDisable(GL_LIGHT0 + id);
3397 void SetCamera(Display display, Surface surface, Camera camera)
3399 OGLDisplay oglDisplay = display.driverData;
3400 //Logf("SetCamera\n");
3404 int left = surface.box.left + surface.offset.x;
3405 int top = surface.box.top + surface.offset.y;
3406 int right = surface.box.right + surface.offset.x;
3407 int bottom = surface.box.bottom + surface.offset.y;
3408 float origX = surface.offset.x + camera.origin.x;
3409 float origY = surface.offset.y + camera.origin.y;
3411 int y = display.height - bottom - 1;
3412 int w = right - left + 1;
3413 int h = bottom - top + 1;
3416 glViewport(x, y, w, h);
3418 // *** Projection Matrix ***
3419 if(!display.display3D.camera)
3422 glMatrixMode(GL_PROJECTION);
3423 if(display.display3D.collectingHits)
3425 float pickX = display.display3D.pickX + surface.offset.x;
3426 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3430 w / display.display3D.pickWidth, 0, 0, 0,
3431 0, h / display.display3D.pickHeight, 0, 0,
3433 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3434 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3437 glLoadMatrixd(pickMatrix.array);
3442 (left - origX) * camera.zMin / camera.focalX,
3443 (right - origX) * camera.zMin / camera.focalX,
3444 (bottom - origY) * camera.zMin / camera.focalY,
3445 (top - origY) * camera.zMin / camera.focalY,
3446 camera.zMin, camera.zMax);
3448 glDisable(GL_BLEND);
3450 // *** Z Inverted Identity Matrix ***
3451 glMatrixMode(GL_MODELVIEW);
3452 if(!display.display3D.camera)
3457 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3459 // *** View Matrix ***
3460 glMultMatrixd(camera.viewMatrix.array);
3465 glEnable(GL_DEPTH_TEST);
3466 glEnable(GL_LIGHTING);
3467 glShadeModel(GL_SMOOTH);
3468 glDepthMask((byte)bool::true);
3469 oglDisplay.depthWrite = true;
3471 glEnable(GL_MULTISAMPLE_ARB);
3473 else if(display.display3D.camera)
3475 oglDisplay.depthWrite = false;
3476 glViewport(0,0,display.width,display.height);
3478 glDisable(GL_CULL_FACE);
3479 glDisable(GL_DEPTH_TEST);
3480 glDisable(GL_LIGHTING);
3482 glDisable(GL_TEXTURE_2D);
3483 glShadeModel(GL_FLAT);
3485 glDisable(GL_MULTISAMPLE_ARB);
3487 // *** Restore 2D MODELVIEW Matrix ***
3490 // *** Restore 2D PROJECTION Matrix ***
3491 glMatrixMode(GL_PROJECTION);
3495 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3498 void ApplyMaterial(Display display, Material material, Mesh mesh)
3500 //Logf("ApplyMaterial\n");
3503 if(material.flags.doubleSided)
3505 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3506 glDisable(GL_CULL_FACE);
3510 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3511 glEnable(GL_CULL_FACE);
3515 if(material.flags.noFog)
3521 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
3523 Bitmap map = material.baseMap;
3524 glEnable(GL_TEXTURE_2D);
3525 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3527 glMatrixMode(GL_TEXTURE);
3529 if(material.uScale && material.vScale)
3530 glScalef(material.uScale, material.vScale, 1);
3531 glMatrixMode(GL_MODELVIEW);
3533 if(material.flags.tile)
3535 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3536 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3540 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3541 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3545 glDisable(GL_TEXTURE_2D);
3547 if(mesh.flags.colors)
3549 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3550 glEnable(GL_COLOR_MATERIAL);
3554 glDisable(GL_COLOR_MATERIAL);
3556 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3557 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3560 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3561 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3565 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3566 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3569 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3570 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3573 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3576 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3578 OGLMesh oglMesh = mesh.data;
3581 if(!mesh.flags.vertices)
3583 if(oglMesh.vertices)
3585 GLDeleteBuffers(1, &oglMesh.vertices);
3586 oglMesh.vertices = 0;
3588 delete mesh.vertices;
3590 if(!mesh.flags.normals)
3594 GLDeleteBuffers(1, &oglMesh.normals);
3595 oglMesh.normals = 0;
3597 delete mesh.normals;
3599 if(!mesh.flags.texCoords1)
3601 if(oglMesh.texCoords)
3603 GLDeleteBuffers(1, &oglMesh.texCoords);
3604 oglMesh.texCoords = 0;
3606 delete mesh.texCoords;
3608 if(!mesh.flags.texCoords2)
3610 if(oglMesh.texCoords2)
3612 GLDeleteBuffers(1, &oglMesh.texCoords2);
3613 oglMesh.texCoords2 = 0;
3616 delete mesh.texCoords2;
3619 if(!mesh.flags.colors)
3623 GLDeleteBuffers(1, &oglMesh.colors);
3635 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3637 bool result = false;
3640 mesh.data = OGLMesh { };
3643 OGLMesh oglMesh = mesh.data;
3644 if(mesh.nVertices == nVertices)
3646 // Same number of vertices, adding features (Leaves the other features pointers alone)
3647 if(mesh.flags != flags)
3649 if(!mesh.flags.vertices && flags.vertices)
3651 if(flags.doubleVertices)
3653 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3656 mesh.vertices = new Vector3Df[nVertices];
3657 if(!oglMesh.vertices)
3658 GLGenBuffers(1, &oglMesh.vertices);
3660 if(!mesh.flags.normals && flags.normals)
3662 if(flags.doubleNormals)
3664 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3667 mesh.normals = new Vector3Df[nVertices];
3668 if(!oglMesh.normals)
3669 GLGenBuffers( 1, &oglMesh.normals);
3671 if(!mesh.flags.texCoords1 && flags.texCoords1)
3673 mesh.texCoords = new Pointf[nVertices];
3674 if(!oglMesh.texCoords)
3675 GLGenBuffers( 1, &oglMesh.texCoords);
3677 if(!mesh.flags.colors && flags.colors)
3679 mesh.colors = new ColorRGBAf[nVertices];
3681 GLGenBuffers( 1, &oglMesh.colors);
3687 // New number of vertices, reallocate all current and new features
3688 flags |= mesh.flags;
3691 if(flags.doubleVertices)
3693 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3696 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3697 if(!oglMesh.vertices)
3698 GLGenBuffers(1, &oglMesh.vertices);
3702 if(flags.doubleNormals)
3704 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3707 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3708 if(!oglMesh.normals)
3709 GLGenBuffers( 1, &oglMesh.normals);
3711 if(flags.texCoords1)
3713 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3714 if(!oglMesh.texCoords)
3715 GLGenBuffers( 1, &oglMesh.texCoords);
3719 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3721 GLGenBuffers( 1, &oglMesh.colors);
3729 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3731 OGLMesh oglMesh = mesh.data;
3732 if(!flags) flags = mesh.flags;
3736 if(flags.vertices && oglMesh.vertices)
3738 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3739 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 );
3742 if(flags.normals && oglMesh.normals)
3744 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3745 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 );
3748 if(flags.texCoords1 && oglMesh.texCoords)
3750 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3751 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3754 if(flags.colors && oglMesh.colors)
3756 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3757 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3760 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3764 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3771 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3775 if(oglIndices.buffer)
3776 GLDeleteBuffers(1, &oglIndices.buffer);
3777 delete oglIndices.indices;
3782 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3784 OGLIndices oglIndices = OGLIndices { };
3787 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3788 GLGenBuffers( 1, &oglIndices.buffer);
3789 oglIndices.nIndices = nIndices;
3794 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3798 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3799 GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3800 oglIndices.indices, GL_STATIC_DRAW_ARB);
3801 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3805 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3808 return oglIndices.indices;
3811 void SelectMesh(Display display, Mesh mesh)
3813 //Logf("SelectMesh\n");
3815 #if !defined( __ANDROID__) && !defined(__APPLE__)
3817 #if defined(__WIN32__)
3818 if(glUnlockArraysEXT)
3820 if(!vboAvailable && display.display3D.mesh)
3821 glUnlockArraysEXT();
3826 OGLMesh oglMesh = mesh.data;
3828 // *** Vertex Stream ***
3829 glEnableClientState(GL_VERTEX_ARRAY);
3830 if(!display.display3D.collectingHits && oglMesh)
3832 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3833 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
3835 // *** Normals Stream ***
3836 if(mesh.normals || mesh.flags.normals)
3838 glEnableClientState(GL_NORMAL_ARRAY);
3839 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3840 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
3843 glDisableClientState(GL_NORMAL_ARRAY);
3845 // *** Texture Coordinates Stream ***
3846 if(mesh.texCoords || mesh.flags.texCoords1)
3848 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3849 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3850 glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
3853 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3855 // *** Color Stream ***
3856 if(mesh.colors || mesh.flags.colors)
3858 glEnableClientState(GL_COLOR_ARRAY);
3859 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3860 glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
3863 glDisableClientState(GL_COLOR_ARRAY);
3868 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3869 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3870 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
3872 glEnableClientState(GL_NORMAL_ARRAY);
3873 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3876 glDisableClientState(GL_NORMAL_ARRAY);
3877 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
3879 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3880 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3883 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3884 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
3886 glEnableClientState(GL_COLOR_ARRAY);
3887 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3890 glDisableClientState(GL_COLOR_ARRAY);
3893 #if !defined(__ANDROID__) && !defined(__APPLE__)
3895 #if defined(__WIN32__)
3899 glLockArraysEXT(0, mesh.nVertices);
3904 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3907 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3909 //Logf("DrawPrimitives\n");
3911 if(primitive->type.vertexRange)
3912 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3915 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3916 // HACK TO SPEED THINGS UP...
3918 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3919 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3922 glBegin(primitiveTypes[primitive->type.primitiveType]);
3925 OGLIndices oglIndices = primitive->data;
3926 MeshFeatures flags = mesh.flags;
3927 for(c = 0; c<primitive->nIndices; c++)
3929 uint16 index = ((uint16 *) oglIndices.indices)[c];
3930 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3931 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3932 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3933 glVertex3fv((float *)&mesh.vertices[index]);
3941 OGLIndices oglIndices = primitive->data;
3943 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
3945 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3946 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3947 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3948 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3951 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3952 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
3957 void PushMatrix(Display display)
3962 void PopMatrix(Display display, bool setMatrix)
3967 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3969 Matrix matrix = transMatrix;
3970 Camera camera = useCamera ? display.display3D.camera : null;
3975 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3980 matrix.m[3][0] - camera.cPosition.x,
3981 matrix.m[3][1] - camera.cPosition.y,
3982 matrix.m[3][2] - camera.cPosition.z);
3994 glMultMatrixd(matrix.array);
3999 public void UseSingleGLContext(bool useSingle)
4001 useSingleGLContext = useSingle;
4004 default dllexport void *
4005 #if defined(__WIN32__)
4006 __attribute__((stdcall))
4008 IS_GLGetContext(DisplaySystem displaySystem)
4012 #if defined(__WIN32__)
4013 OGLSystem system = displaySystem.driverData;
4015 #elif !defined(__ANDROID__)
4016 OGLSystem system = displaySystem.driverData;
4017 return system.glContext;