1 // We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
4 namespace gfx::drivers;
7 #if defined(__unix__) || defined(__APPLE__)
9 #if !defined(__MINGW32__)
10 #define GL_GLEXT_PROTOTYPES
15 //#include <GL/miniglx.h>
19 #if defined(__ANDROID__)
23 #define property _property
27 #define Window X11Window
28 #define Cursor X11Cursor
30 #define Display X11Display
32 #define KeyCode X11KeyCode
33 #define Picture X11Picture
37 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
42 #include <X11/extensions/Xrender.h>
43 #include <X11/extensions/shape.h>
63 #if defined(__APPLE__)
64 #include <OpenGl/gl.h>
67 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
69 #if defined(__WIN32__)
70 #define WIN32_LEAN_AND_MEAN
72 #define _WIN32_WINNT 0x0500
78 #if defined(__ANDROID__)
92 #if defined(__unix__) || defined(__APPLE__)
100 #define glLoadMatrix glLoadMatrixd
101 #define glMultMatrix glMultMatrixd
102 #define glGetMatrix glGetDoublev
103 #define glTranslate glTranslated
104 #define glScale glScaled
107 #define glVertex3v glVertex3dv
108 #define glNormal3v glNormal3dv
112 //#ifdef VERTEX_FORMAT_DOUBLE
114 #define glLoadMatrix glLoadMatrixd
115 #define glMultMatrix glMultMatrixd
116 #define glGetMatrix glGetDoublev
117 #define glVertex3v glVertex3dv
118 #define glNormal3v glNormal3dv
119 #define glTranslate glTranslated
120 #define glScale glScaled
121 //#define GL_VERTEX_FORMAT GL_DOUBLE
125 #define glLoadMatrix glLoadMatrixf
126 #define glMultMatrix glMultMatrixf
127 #define glGetMatrix glGetFloatv
128 #define glVertex3v glVertex3fv
129 #define glNormal3v glNormal3fv
130 #define glTranslate glTranslatef
131 #define glScale glScalef
132 //#define GL_VERTEX_FORMAT GL_FLOAT
137 #define GL_ARRAY_BUFFER_ARB 0x8892
138 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
139 #define GL_STATIC_DRAW_ARB 0x88E4
140 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
141 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
143 #define GL_MULTISAMPLE_ARB 0x809D
145 #if defined(__WIN32__)
147 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
148 #define WGL_SAMPLES_ARB 0x2042
150 #define WGL_WGLEXT_VERSION 1
151 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
152 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
153 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
154 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
155 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
156 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
157 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
158 #define WGL_ACCELERATION_ARB 0x2003
159 #define WGL_NEED_PALETTE_ARB 0x2004
160 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
161 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
162 #define WGL_SWAP_METHOD_ARB 0x2007
163 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
164 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
165 #define WGL_TRANSPARENT_ARB 0x200A
166 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
167 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
168 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
169 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
170 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
171 #define WGL_SHARE_DEPTH_ARB 0x200C
172 #define WGL_SHARE_STENCIL_ARB 0x200D
173 #define WGL_SHARE_ACCUM_ARB 0x200E
174 #define WGL_SUPPORT_GDI_ARB 0x200F
175 #define WGL_SUPPORT_OPENGL_ARB 0x2010
176 #define WGL_DOUBLE_BUFFER_ARB 0x2011
177 #define WGL_STEREO_ARB 0x2012
178 #define WGL_PIXEL_TYPE_ARB 0x2013
179 #define WGL_COLOR_BITS_ARB 0x2014
180 #define WGL_RED_BITS_ARB 0x2015
181 #define WGL_RED_SHIFT_ARB 0x2016
182 #define WGL_GREEN_BITS_ARB 0x2017
183 #define WGL_GREEN_SHIFT_ARB 0x2018
184 #define WGL_BLUE_BITS_ARB 0x2019
185 #define WGL_BLUE_SHIFT_ARB 0x201A
186 #define WGL_ALPHA_BITS_ARB 0x201B
187 #define WGL_ALPHA_SHIFT_ARB 0x201C
188 #define WGL_ACCUM_BITS_ARB 0x201D
189 #define WGL_ACCUM_RED_BITS_ARB 0x201E
190 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
191 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
192 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
193 #define WGL_DEPTH_BITS_ARB 0x2022
194 #define WGL_STENCIL_BITS_ARB 0x2023
195 #define WGL_AUX_BUFFERS_ARB 0x2024
196 #define WGL_NO_ACCELERATION_ARB 0x2025
197 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
198 #define WGL_FULL_ACCELERATION_ARB 0x2027
199 #define WGL_SWAP_EXCHANGE_ARB 0x2028
200 #define WGL_SWAP_COPY_ARB 0x2029
201 #define WGL_SWAP_UNDEFINED_ARB 0x202A
202 #define WGL_TYPE_RGBA_ARB 0x202B
203 #define WGL_TYPE_COLORINDEX_ARB 0x202C
204 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
205 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
206 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
207 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
208 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
209 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
210 #define WGL_PBUFFER_LARGEST_ARB 0x2033
211 #define WGL_PBUFFER_WIDTH_ARB 0x2034
212 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
213 #define WGL_PBUFFER_LOST_ARB 0x2036
214 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
215 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
216 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
217 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
218 #define WGL_ACCELERATION_EXT 0x2003
219 #define WGL_NEED_PALETTE_EXT 0x2004
220 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
221 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
222 #define WGL_SWAP_METHOD_EXT 0x2007
223 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
224 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
225 #define WGL_TRANSPARENT_EXT 0x200A
226 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
227 #define WGL_SHARE_DEPTH_EXT 0x200C
228 #define WGL_SHARE_STENCIL_EXT 0x200D
229 #define WGL_SHARE_ACCUM_EXT 0x200E
230 #define WGL_SUPPORT_GDI_EXT 0x200F
231 #define WGL_SUPPORT_OPENGL_EXT 0x2010
232 #define WGL_DOUBLE_BUFFER_EXT 0x2011
233 #define WGL_STEREO_EXT 0x2012
234 #define WGL_PIXEL_TYPE_EXT 0x2013
235 #define WGL_COLOR_BITS_EXT 0x2014
236 #define WGL_RED_BITS_EXT 0x2015
237 #define WGL_RED_SHIFT_EXT 0x2016
238 #define WGL_GREEN_BITS_EXT 0x2017
239 #define WGL_GREEN_SHIFT_EXT 0x2018
240 #define WGL_BLUE_BITS_EXT 0x2019
241 #define WGL_BLUE_SHIFT_EXT 0x201A
242 #define WGL_ALPHA_BITS_EXT 0x201B
243 #define WGL_ALPHA_SHIFT_EXT 0x201C
244 #define WGL_ACCUM_BITS_EXT 0x201D
245 #define WGL_ACCUM_RED_BITS_EXT 0x201E
246 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
247 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
248 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
249 #define WGL_DEPTH_BITS_EXT 0x2022
250 #define WGL_STENCIL_BITS_EXT 0x2023
251 #define WGL_AUX_BUFFERS_EXT 0x2024
252 #define WGL_NO_ACCELERATION_EXT 0x2025
253 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
254 #define WGL_FULL_ACCELERATION_EXT 0x2027
255 #define WGL_SWAP_EXCHANGE_EXT 0x2028
256 #define WGL_SWAP_COPY_EXT 0x2029
257 #define WGL_SWAP_UNDEFINED_EXT 0x202A
258 #define WGL_TYPE_RGBA_EXT 0x202B
259 #define WGL_TYPE_COLORINDEX_EXT 0x202C
260 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
261 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
262 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
263 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
264 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
265 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
266 #define WGL_PBUFFER_LARGEST_EXT 0x2033
267 #define WGL_PBUFFER_WIDTH_EXT 0x2034
268 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
269 #define WGL_DEPTH_FLOAT_EXT 0x2040
270 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
271 #define WGL_SAMPLES_3DFX 0x2061
272 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
273 #define WGL_SAMPLES_EXT 0x2042
274 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
275 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
276 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
277 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
278 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
279 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
280 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
281 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
282 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
283 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
284 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
285 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
286 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
287 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
288 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
289 #define WGL_ARB_buffer_region 1
290 #define WGL_ARB_extensions_string 1
291 #define WGL_ARB_pixel_format 1
292 #define WGL_ARB_make_current_read 1
293 #define WGL_ARB_pbuffer 1
294 #define WGL_EXT_display_color_table 1
295 #define WGL_EXT_extensions_string 1
296 #define WGL_EXT_make_current_read 1
297 #define WGL_EXT_pbuffer 1
298 #define WGL_EXT_pixel_format 1
299 #define WGL_EXT_swap_control 1
300 #define WGL_WGL_EXT_depth_float 1
301 #define WGL_WGL_3DFX_multisample 1
302 #define WGL_WGL_EXT_multisample 1
303 #define WGL_NV_allocate_memory 1
306 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
307 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
308 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
309 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
310 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
314 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
315 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
316 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
317 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
319 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
320 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
321 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
322 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
323 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
324 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
326 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
327 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
328 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
329 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
330 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
331 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
332 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
333 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
335 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
336 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
337 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
338 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
339 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
340 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
341 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
342 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
343 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
344 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
345 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
346 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
347 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
349 #ifdef WGL_WGLEXT_PROTOTYPES
350 extern BOOL WINAPI wglSwapIntervalEXT (int);
351 extern int WINAPI wglGetSwapIntervalEXT (void);
352 #endif /* WGL_WGLEXT_PROTOTYPES */
353 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
354 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
356 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
360 #if defined(__ANDROID__)
362 // OpenGL ES Porting Kit
364 #define glBindFramebuffer glBindFramebufferOES
365 #define glBindRenderbuffer glBindRenderbufferOES
366 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
367 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
368 #define glFramebufferTexture2D glFramebufferTexture2DOES
369 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
370 #define glGenFramebuffers glGenFramebuffersOES
371 #define glGenRenderbuffers glGenRenderbuffersOES
372 #define glDeleteFramebuffers glDeleteFramebuffersOES
373 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
375 #define GL_POLYGON_STIPPLE 0xFFFF
376 #define GL_LINE_STIPPLE 0xFFFF
377 #define GL_LINE 0xFFFF
378 #define GL_FILL 0xFFFF
379 #define GL_ALL_ATTRIB_BITS 0xFFFF
380 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
381 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
386 #define glBufferDatai glesBufferDatai
387 #define glBufferDatad glesBufferDatad
388 #define glVertexPointeri glesVertexPointeri
389 #define glVertexPointerd glesVertexPointerd
391 #define glRecti glesRecti
392 #define glBegin glesBegin
393 #define glTexCoord2i glesTexCoord2i
394 #define glVertex2i glesVertex2i
395 #define glTexCoord2d glesTexCoord2d
396 #define glVertex2d glesVertex2d
397 #define glTexCoord2f glesTexCoord2f
398 #define glVertex2f glesVertex2f
399 #define glEnd glesEnd
400 #define glColor3f glesColor3f
401 #define glColor4ub glesColor4ub
402 #define glColor4fv glesColor4fv
403 #define glLineStipple glesLineStipple
404 #define glNormal3fv glesNormal3fv
405 #define glTexCoord2fv glesTexCoord2fv
406 #define glColorMaterial glesColorMaterial
408 #define glLoadMatrixd glesLoadMatrixd
409 #define glMultMatrixd glesMultMatrixd
410 #define glFrustum glesFrustum
411 #define glOrtho glesOrtho
412 #define glScaled glesScaled
413 #define glTranslated glesTranslated
414 #define glRotated glesRotated
415 #define glVertex3d glesVertex3d
416 #define glVertex3f glesVertex3f
417 #define glVertex3fv glesVertex3fv
418 #define glLightModeli glesLightModeli
422 #define GL_QUAD_STRIP 0
424 #define GL_UNSIGNED_INT 0
427 //#define GL_LINE_STIPPLE 0
428 #define GL_BGRA_EXT 0
429 #define GL_UNPACK_ROW_LENGTH 0
430 #define GL_UNPACK_SKIP_PIXELS 0
431 #define GL_UNPACK_SKIP_ROWS 0
433 #define GL_PACK_ROW_LENGTH 0
434 #define GL_PACK_SKIP_ROWS 0
435 #define GL_PACK_SKIP_PIXELS 0
437 static EGLDisplay eglDisplay;
438 static EGLSurface eglSurface;
439 static EGLContext eglContext;
440 static int eglWidth, eglHeight;
442 static bool egl_init_display(ANativeWindow* window)
444 const EGLint attribs[] =
446 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
451 /*EGL_SAMPLE_BUFFERS, 1,
452 EGL_SAMPLES, 0, //2,*/
455 EGLint w, h, dummy, format;
461 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
462 eglInitialize(display, 0, 0);
463 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
464 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
466 surface = eglCreateWindowSurface(display, config, window, null);
467 context = eglCreateContext(display, config, null, null);
469 if(!eglMakeCurrent(display, surface, surface, context))
472 eglQuerySurface(display, surface, EGL_WIDTH, &w);
473 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
475 eglDisplay = display;
476 eglContext = context;
477 eglSurface = surface;
481 glEnableClientState(GL_VERTEX_ARRAY);
483 // Initialize GL state.
484 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
485 glEnable(GL_CULL_FACE);
486 glShadeModel(GL_SMOOTH);
487 glDisable(GL_DEPTH_TEST);
489 glDisable(GL_CULL_FACE);
490 glDisable(GL_DEPTH_TEST);
492 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
495 glMatrixMode(GL_MODELVIEW);
496 glScalef(1.0f, 1.0f, -1.0f);
497 glMatrixMode(GL_PROJECTION);
498 glShadeModel(GL_FLAT);
500 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
501 glFogi(GL_FOG_MODE, GL_EXP);
502 glFogf(GL_FOG_DENSITY, 0);
503 glEnable(GL_NORMALIZE);
504 glDepthFunc(GL_LESS);
506 glDisable(GL_MULTISAMPLE_ARB);
510 glOrtho(0,w,h,0,0.0,1.0);
514 static void egl_term_display()
518 glDeleteTextures(1, (int *)&stippleTexture);
521 if(eglDisplay != EGL_NO_DISPLAY)
523 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
524 if(eglContext != EGL_NO_CONTEXT)
525 eglDestroyContext(eglDisplay, eglContext);
526 if(eglSurface != EGL_NO_SURFACE)
527 eglDestroySurface(eglDisplay, eglSurface);
528 eglTerminate(eglDisplay);
530 eglDisplay = EGL_NO_DISPLAY;
531 eglContext = EGL_NO_CONTEXT;
532 eglSurface = EGL_NO_SURFACE;
535 // OpenGL Immediate Mode Porting Kit
536 static int beginCount;
537 static int vertexCount;
538 static int normalCount;
539 static float *vertexPointer;
540 static float *normalPointer;
541 static GLenum beginMode;
542 static unsigned int beginBufferSize, normalBufferSize;
543 static int numVertexCoords = 2;
545 void glesRecti(int a, int b, int c, int d)
555 void glesBegin(GLenum mode)
562 normalBufferSize = beginBufferSize = 1024; // default number of vertices
563 vertexPointer = new float[beginBufferSize * 5];
564 normalPointer = new float[normalBufferSize * 3];
568 void glesTexCoord2f(float x, float y)
570 int count = vertexCount;
572 if(vertexCount + numVertexCoords > beginBufferSize)
574 beginBufferSize = beginBufferSize + beginBufferSize/2;
575 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
578 vertexPointer[count*(2+numVertexCoords) ] = x;
579 vertexPointer[count*(2+numVertexCoords)+1] = y;
582 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
584 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
585 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
587 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
588 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
592 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
593 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
594 void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
596 void glesVertex2f(float x, float y)
599 if(vertexCount + 4 > beginBufferSize)
601 beginBufferSize = beginBufferSize + beginBufferSize/2;
602 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
605 vertexPointer[vertexCount*4+2] = x;
606 vertexPointer[vertexCount*4+3] = y;
609 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
611 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
612 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
614 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
615 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
620 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
621 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
625 int mode = beginMode;
626 if(mode == GL_QUADS) mode = GL_TRIANGLES;
627 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
629 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
630 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
631 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
632 if(normalCount && normalCount == vertexCount)
634 glEnableClientState(GL_NORMAL_ARRAY);
635 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
638 glDrawArrays(mode, 0, vertexCount);
640 glDisableClientState(GL_NORMAL_ARRAY);
641 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
646 static float *floatVPBuffer = null;
647 static short *shortVPBuffer = null;
648 static unsigned int shortVPSize = 0, floatVPSize = 0;
651 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
652 static unsigned short *shortBDBuffer = null;
653 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
655 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
660 if(numVertices*numCoords > shortVPSize)
662 shortVPSize = numVertices*numCoords;
663 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
665 for(i = 0; i < numVertices*numCoords; i++)
666 shortVPBuffer[i] = (short)pointer[i];
667 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
670 glVertexPointer(numCoords, GL_SHORT, stride, 0);
673 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
678 if(numVertices*numCoords > floatVPSize)
680 floatVPSize = numVertices*numCoords;
681 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
683 for(i = 0; i < numVertices*numCoords; i++)
684 floatVPBuffer[i] = (float)pointer[i];
685 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
688 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
691 void glesTexReuseIntVP(int numCoords)
693 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
696 void glesTexReuseDoubleVP(int numCoords)
698 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
701 void glesColor3f( float r, float g, float b )
703 glColor4f(r, g, b, 1.0f);
706 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
708 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
711 void glesColor4fv(float * a)
713 glColor4f(a[0], a[1], a[2], a[3]);
716 void glesBufferDatad(int target, int size, void * data, int usage)
718 int numElems = size/sizeof(double);
719 double * dblPtr = (double *)data;
721 if (numElems > floatVPSize)
723 floatVPSize = numElems;
724 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
726 for (i=0; i< numElems; i++)
727 floatVPBuffer[i] = (float)dblPtr[i];
729 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
732 void glesBufferDatai(int target, int size, void * data, int usage)
734 int numElems = size/sizeof(unsigned int);
735 unsigned int * pointer = (unsigned int *)data;
737 if (numElems > shortBDSize)
739 shortBDSize = numElems;
740 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
742 for (i=0; i< numElems; i++)
743 shortBDBuffer[i] = (unsigned short)pointer[i];
745 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
748 /// *** These might require an implementation to get things working ***
749 void glesLoadMatrixd( double * i )
753 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
754 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
755 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
756 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
761 void glesOrtho( double l, double r, double b, double t, double n, double f )
765 { (float)(2 / (r - l)), 0, 0, 0 },
766 { 0, (float)(2 / (t - b)), 0, 0 },
767 { 0, 0, (float)(-2 / (f - n)), 0 },
768 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
770 glMultMatrixf((float *)matrix);
773 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
774 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
775 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
777 void glesMultMatrixd( double * i )
781 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
782 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
783 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
784 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
789 // Need to do these...
790 void glesVertex3f( float x, float y, float z )
793 if(vertexCount + 4 > beginBufferSize)
795 beginBufferSize = beginBufferSize + beginBufferSize/2;
796 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
799 vertexPointer[vertexCount*5+2] = x;
800 vertexPointer[vertexCount*5+3] = y;
801 vertexPointer[vertexCount*5+4] = z;
804 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
806 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
807 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
808 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
810 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
811 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
812 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
818 void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
819 void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
821 void glesNormal3f(float x, float y, float z)
823 normalCount = vertexCount;
824 if(vertexCount + 4 > normalBufferSize)
826 normalBufferSize = normalBufferSize + normalBufferSize/2;
827 normalPointer = renew normalPointer float[normalBufferSize * 2];
830 normalPointer[normalCount*3+0] = x;
831 normalPointer[normalCount*3+1] = y;
832 normalPointer[normalCount*3+2] = z;
835 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
837 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
838 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
839 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
841 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
842 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
843 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
847 void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
848 void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
850 void glesColorMaterial(int a, int b)
852 PrintLn("glColorMaterial stub");
857 delete vertexPointer;
860 delete floatVPBuffer;
863 delete shortVPBuffer;
866 delete shortBDBuffer;
870 static int stippleTexture;
871 static bool stippleEnabled;
873 void glesLineStipple( int i, unsigned short j )
878 for(x = 0; x < 16; x++)
880 bool v = (j & (1 << x)) != 0;
881 texture[x] = v ? 0xFFFFFFFF : 0;
884 glGenTextures(1, &stippleTexture);
885 glBindTexture(GL_TEXTURE_2D, stippleTexture);
886 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
887 glEnable(GL_TEXTURE_2D);
888 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
889 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
890 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
891 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
892 glMatrixMode(GL_TEXTURE);
894 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
895 glScaled(i/16.0, 1, 1.0f);
896 glTranslated(0.5, 0.5, 0);
897 glMatrixMode(GL_PROJECTION);
900 void glesFrustum( double l, double r, double b, double t, double n, double f )
902 float A = (float)((r + l) / (r - l));
903 float B = (float)((t + b) / (t - b));
904 float C = (float)(-(f + n) / (f - n));
905 float D = (float)(-2*f*n/(f-n));
908 { (float)(2*n / (r - l)), 0, 0, 0 },
909 { 0, (float)(2*n / (t - b)), 0, 0 },
913 glMultMatrixf((float *)matrix);
916 void glesLightModeli( unsigned int pname, int param )
918 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
919 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
922 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
923 void glFogi( unsigned int pname, int param ) { }
924 void glPolygonMode( unsigned int i, unsigned int j ) { }
927 // *** Picking won't be supported for now ***
928 void glPushName( unsigned int i ) { }
929 void glLoadName( unsigned int i ) { }
932 // Probably replace by regular glBlendFunc ...
933 void glBlendFuncSeparate(int a, int b, int c, int d)
938 // For direct pixel blitting...
939 void glRasterPos2d(double a, double b) { }
940 void glPixelZoom(float a, float b) { }
941 void glDrawPixels(int a, int b, int c, int d, void * e) { }
945 /* Non OpenGL ES friendly stuff
946 #undef GL_UNSIGNED_INT
952 #undef GL_POLYGON_STIPPLE
953 #undef GL_LINE_STIPPLE
956 #undef GL_ALL_ATTRIB_BITS
957 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
962 #if !defined(__APPLE__)
963 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
964 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
965 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
966 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
971 static int currentVertexBuffer;
973 bool GLSelectVBO(uint vbo)
975 if(currentVertexBuffer != vbo)
977 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
978 currentVertexBuffer = vbo;
984 void GLBindBuffer(int target, uint buffer)
987 glBindBuffer(target, buffer);
989 glBindBufferARB(target, buffer);
993 static int displayWidth, displayHeight;
995 #define GL_CLAMP_TO_EDGE 0x812F
997 static bool useSingleGLContext = false;
998 class OGLDisplay : struct
1000 #if defined(__WIN32__)
1010 int imageBuffers[2];
1011 byte * pboMemory1, * pboMemory2;
1014 GLXContext glContext;
1017 XShmSegmentInfo shminfo;
1019 XShmSegmentInfo shminfoShape;
1020 XImage * shapeImage;
1024 X11Picture windowPicture;
1025 X11Picture pixmapPicture;
1027 X11Picture shapePicture;
1030 ColorAlpha * flippingBuffer;
1031 int flipBufH, flipBufW;
1036 class OGLSystem : struct
1038 #if defined(__WIN32__)
1039 PIXELFORMATDESCRIPTOR pfd;
1045 XVisualInfo * visualInfo;
1046 GLXContext glContext;
1047 GLXDrawable glxDrawable;
1052 class OGLSurface : struct
1059 float foreground[4], background[4], bitmapMult[4];
1062 class OGLMesh : struct
1071 class OGLIndices : struct
1078 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1079 static int primitiveTypes[RenderPrimitiveType] =
1081 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1088 class OpenGLDisplayDriver : DisplayDriver
1090 class_property(name) = "OpenGL";
1092 bool LockSystem(DisplaySystem displaySystem)
1094 OGLSystem oglSystem = displaySystem.driverData;
1095 if(useSingleGLContext) return true;
1096 #if defined(__WIN32__)
1097 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1098 #elif defined(__unix__) || defined(__APPLE__)
1099 //if(previous) return true;
1100 // printf("Making SYSTEM current\n");
1101 #if !defined(__ANDROID__)
1102 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1104 //previous = oglSystem.glContext;
1109 void UnlockSystem(DisplaySystem displaySystem)
1111 if(useSingleGLContext) return;
1112 #if defined(__WIN32__)
1113 wglMakeCurrent(null, null);
1114 #elif defined(__unix__) || defined(__APPLE__)
1115 // printf("Making NULL current\n");
1116 #if defined(__ANDROID__)
1118 glXMakeCurrent(xGlobalDisplay, None, null);
1124 bool Lock(Display display)
1126 OGLDisplay oglDisplay = display.driverData;
1127 OGLSystem oglSystem = display.displaySystem.driverData;
1129 if(useSingleGLContext) return true;
1130 #if defined(__WIN32__)
1131 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1132 #elif defined(__unix__) || defined(__APPLE__)
1133 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1134 // printf(" Making DISPLAY current\n");
1135 #if defined(__ANDROID__)
1137 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1143 void Unlock(Display display)
1145 if(useSingleGLContext) return;
1146 //printf(" Making NULL current\n");
1147 //glXMakeCurrent(xGlobalDisplay, None, null);
1149 LockSystem(display.displaySystem);
1152 void DestroyDisplay(Display display)
1154 OGLDisplay oglDisplay = display.driverData;
1158 #if defined(__WIN32__)
1159 wglMakeCurrent( null, null );
1162 wglDeleteContext(oglDisplay.glrc);
1164 if(oglDisplay.hdc && oglDisplay.pBuffer)
1165 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1167 if(oglDisplay.pBuffer)
1168 wglDestroyPbufferARB(oglDisplay.pBuffer);
1171 ReleaseDC(display.window, oglDisplay.hdc);
1173 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1174 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1176 #elif defined(__unix__) || defined(__APPLE__)
1177 #if defined(__ANDROID__)
1179 if(oglDisplay.shapePixmap)
1180 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1181 if(oglDisplay.pixmap)
1182 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1183 if(oglDisplay.image)
1185 if(oglDisplay.shminfoShape.shmid != -1)
1187 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1188 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1189 shmdt(oglDisplay.shminfo.shmaddr);
1190 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1193 if(oglDisplay.shapeImage)
1195 if(oglDisplay.shminfoShape.shmid != -1)
1197 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1198 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1199 shmdt(oglDisplay.shminfoShape.shmaddr);
1200 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1202 XDestroyImage(oglDisplay.shapeImage);
1203 oglDisplay.shapeImage = None;
1206 glXMakeCurrent(xGlobalDisplay, None, null);
1208 if(oglDisplay.glContext)
1209 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1212 delete oglDisplay.flippingBuffer;
1214 display.driverData = null;
1218 bool CreateDisplaySystem(DisplaySystem displaySystem)
1220 bool result = false;
1221 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1224 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1226 oglSystem.hdc = GetDC(oglSystem.hwnd);
1230 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1231 oglSystem.pfd.nVersion = 1;
1232 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1233 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1234 oglSystem.pfd.cColorBits = 24;
1235 oglSystem.pfd.cAlphaBits = 8;
1236 oglSystem.pfd.cDepthBits = 24;
1237 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1239 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1240 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1242 if(oglSystem.pfd.cColorBits > 8)
1244 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1245 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1248 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1250 // Get Pointers To The GL Functions
1251 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1252 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1253 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1254 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1255 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1256 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1257 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1258 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1259 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1260 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1261 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1262 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1264 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1265 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1266 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1267 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1268 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1269 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1270 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1271 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1272 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1274 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1276 // eSystem_LoggingMode(LOG_MSGBOX, null);
1278 if(wglChoosePixelFormatARB)
1283 float fAttributes[] = {0,0};
1286 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1287 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1288 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1289 WGL_COLOR_BITS_ARB,24,
1290 WGL_ALPHA_BITS_ARB,8,
1291 WGL_DEPTH_BITS_ARB,16,
1292 WGL_STENCIL_BITS_ARB,0,
1293 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1294 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1295 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1299 //Log("Found wglChoosePixelFormatARB\n");
1301 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1302 if(!valid || !numFormats)
1304 //Log("Can't find 4x multi sampling\n");
1305 iAttributes[19] = 2;
1306 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1307 if(!valid || !numFormats)
1309 // Log("Can't find 2x multi sampling\n");
1310 iAttributes[16] = 0;
1311 iAttributes[17] = 0;
1312 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1315 if(valid && numFormats)
1317 oglSystem.format = pixelFormat;
1318 wglMakeCurrent(null, null);
1319 wglDeleteContext(oglSystem.glrc);
1321 // *** DescribePixelFormat does not support WGL pixel formats! ***
1322 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1323 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1324 //Log("Successfully set pixel format\n");
1326 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1327 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1331 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1335 wglMakeCurrent(null, null);
1337 //eSystem_DumpErrors(true);
1341 #elif defined(__unix__) || defined(__APPLE__)
1342 #if defined(__ANDROID__)
1343 egl_init_display(guiApp.desktop.windowHandle);
1347 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1348 XSetWindowAttributes attr;
1353 #ifndef ECERE_MINIGLX
1354 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1357 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1361 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1362 attr.background_pixel = 0;
1363 attr.border_pixel = 0;
1364 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1365 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1366 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1368 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1369 oglSystem.visualInfo->visual, mask, &attr );
1371 if(oglSystem.visualInfo)
1373 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1374 if(oglSystem.glContext)
1376 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1378 glXMakeCurrent(xGlobalDisplay, None, null);
1385 displaySystem.flags.alpha = true;
1386 displaySystem.flags.flipping = true;
1387 displaySystem.pixelFormat = pixelFormat888;
1391 void DestroyDisplaySystem(DisplaySystem displaySystem)
1393 OGLSystem oglSystem = displaySystem.driverData;
1395 #if defined(__WIN32__)
1396 wglMakeCurrent( null, null );
1399 wglDeleteContext(oglSystem.glrc);
1402 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1403 DestroyWindow(oglSystem.hwnd);
1405 #elif defined(__unix__) || defined(__APPLE__)
1406 #if defined(__ANDROID__)
1409 if(oglSystem.visualInfo)
1411 #ifdef ECERE_MINIGLX
1412 __miniglx_XFree(oglSystem.visualInfo);
1414 XFree(oglSystem.visualInfo);
1418 if(oglSystem.glxDrawable)
1420 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1421 oglSystem.glxDrawable = 0;
1428 bool CreateDisplay(Display display)
1430 bool result = false;
1431 OGLDisplay oglDisplay = display.driverData;
1432 OGLSystem oglSystem = display.displaySystem.driverData;
1434 oglDisplay = display.driverData = OGLDisplay { };
1435 //printf("Inside CreateDisplay\n");
1437 #if defined(__WIN32__) || defined(USEPBUFFER)
1438 if(!display.alphaBlend)
1441 #if defined(__WIN32__)
1442 oglDisplay.hdc = GetDC(display.window);
1443 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1444 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1446 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1447 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1451 ReleaseDC(display.window, oglDisplay.hdc);
1452 #elif defined(__unix__) || defined(__APPLE__)
1453 #if defined(__ANDROID__)
1455 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1457 #if defined(__APPLE__)
1458 XVisualInfo template = { 0 };
1459 XWindowAttributes winAttr;
1461 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1462 template.visualid = XVisualIDFromVisual(winAttr.visual);
1463 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1465 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1466 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1467 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1468 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1470 // visualInfo = oglSystem.visualInfo;
1475 //printf("visualInfo is not null\n");
1476 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1477 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1478 //XFree(visualInfo);
1481 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1482 if(oglDisplay.glContext)
1484 //printf("CreateDisplay Got a Context\n");
1485 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1491 #if defined(__WIN32__) || defined(USEPBUFFER)
1497 #if !defined(__OLDX__)
1498 if(glBlendFuncSeparate)
1499 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1502 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1505 glMatrixMode(GL_MODELVIEW);
1506 glScalef(1.0f, 1.0f, -1.0f);
1507 // glTranslatef(0.375f, 0.375f, 0.0f);
1508 // glTranslatef(-0.625f, -0.625f, 0.0f);
1509 glMatrixMode(GL_PROJECTION);
1510 glShadeModel(GL_FLAT);
1512 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1513 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1514 glFogi(GL_FOG_MODE, GL_EXP);
1515 glFogf(GL_FOG_DENSITY, 0);
1516 glEnable(GL_NORMALIZE);
1517 glDepthFunc(GL_LESS);
1519 glDisable(GL_MULTISAMPLE_ARB);
1521 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1522 display.ambient = Color { 50,50,50 };
1525 if(!useSingleGLContext)
1527 #if defined(__WIN32__)
1528 wglMakeCurrent(null, null);
1529 #elif defined(__unix__) || defined(__APPLE__)
1530 #if defined(__ANDROID__)
1533 glXMakeCurrent(xGlobalDisplay, None, null);
1541 bool DisplaySize(Display display, int width, int height)
1543 OGLDisplay oglDisplay = display.driverData;
1544 OGLSystem oglSystem = display.displaySystem.driverData;
1546 bool result = false;
1548 //printf("Inside DisplaySize\n");
1549 #if defined(__WIN32__) || defined(USEPBUFFER)
1550 if(display.alphaBlend)
1552 #if defined(__WIN32__)
1553 const int attributes[]=
1555 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1556 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1558 int pixelFormat = 0;
1559 if(wglChoosePixelFormatARB)
1563 float fAttributes[] = {0,0};
1566 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1567 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1568 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1569 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1570 WGL_COLOR_BITS_ARB,24,
1571 WGL_ALPHA_BITS_ARB,8,
1572 WGL_DEPTH_BITS_ARB,16,
1573 WGL_STENCIL_BITS_ARB,0,
1574 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1575 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1576 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1580 //Log("Found wglChoosePixelFormatARB\n");
1582 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1583 if(!valid || !numFormats)
1585 //Log("Can't find 4x multi sampling\n");
1586 iAttributes[19] = 2;
1587 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1588 if(!valid || !numFormats)
1590 // Log("Can't find 2x multi sampling\n");
1591 iAttributes[16] = 0;
1592 iAttributes[17] = 0;
1593 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1594 if(!valid || !numFormats)
1598 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1599 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1600 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1601 WGL_COLOR_BITS_ARB,24,
1602 WGL_ALPHA_BITS_ARB,8,
1603 WGL_DEPTH_BITS_ARB,16,
1606 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1610 if(valid && numFormats)
1612 wglMakeCurrent(null, null);
1616 wglMakeCurrent( null, null );
1617 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1618 if(oglDisplay.hdc && oglDisplay.pBuffer)
1619 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1621 wglDestroyPbufferARB(oglDisplay.pBuffer);
1623 if(!useSingleGLContext)
1624 wglMakeCurrent( null, null );
1627 wglDeleteContext(oglDisplay.glrc);
1629 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1630 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1631 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1634 HDC hdc = GetDC(display.window);
1636 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1637 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1639 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1640 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1642 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1644 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1648 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1649 oglDisplay.memDC = CreateCompatibleDC(hdc);
1650 SetMapMode(oglDisplay.memDC, MM_TEXT);
1651 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1652 info->bmiHeader.biPlanes = 1;
1653 info->bmiHeader.biCompression = BI_RGB;
1654 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1655 info->bmiHeader.biWidth = width;
1656 info->bmiHeader.biHeight = height;
1657 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1660 SelectObject(oglDisplay.memDC, newBitmap);
1661 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1664 PIXELFORMATDESCRIPTOR pfd = { 0 };
1665 pfd.nSize = (short)sizeof(pfd);
1667 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1668 pfd.iPixelType = PFD_TYPE_RGBA;
1669 pfd.cColorBits = 32;
1670 //pfd.cAlphaBits = 8;
1671 pfd.cDepthBits = 24;
1672 pfd.iLayerType = PFD_MAIN_PLANE;
1674 oglDisplay.hdc = oglDisplay.memDC;
1676 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1677 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1678 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1680 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1681 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1682 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1687 const int imageSize = width * height * 4;
1689 glGenBuffersARB(2, oglDisplay.imageBuffers);
1691 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1692 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1693 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1694 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1697 oglDisplay.memBitmap = newBitmap;
1698 oglDisplay.stride = width;
1704 ReleaseDC(display.window, hdc);
1706 #elif defined(__unix__) || defined(__APPLE__)
1707 #if defined(__ANDROID__)
1712 GLX_DOUBLEBUFFER, True,
1718 GLX_STENCIL_SIZE, 1,
1719 //GLX_DEPTH_SIZE, 24,
1720 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1721 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1727 GLX_PBUFFER_WIDTH, width,
1728 GLX_PBUFFER_HEIGHT, height,
1729 GLX_LARGEST_PBUFFER, False,
1733 // choose a pixel format that meets our minimum requirements
1736 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1739 if(oglDisplay.pixmap)
1741 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1742 oglDisplay.pixmap = None;
1744 if(oglDisplay.shapePixmap)
1746 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1747 oglDisplay.shapePixmap = None;
1750 // Free Shared Memory Pixmap
1751 if(oglDisplay.image)
1753 if(oglDisplay.shminfoShape.shmid != -1)
1755 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1756 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1757 shmdt(oglDisplay.shminfo.shmaddr);
1758 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1760 XDestroyImage(oglDisplay.image);
1761 oglDisplay.image = None;
1763 if(oglDisplay.shapeImage)
1765 if(oglDisplay.shminfoShape.shmid != -1)
1767 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1768 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1769 shmdt(oglDisplay.shminfoShape.shmaddr);
1770 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1772 XDestroyImage(oglDisplay.shapeImage);
1773 oglDisplay.shapeImage = None;
1776 if(oglDisplay.windowPicture)
1777 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1778 if(oglDisplay.pixmapPicture)
1779 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1781 if(oglDisplay.pixmap)
1782 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1784 if(oglDisplay.glContext)
1785 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1786 if(oglDisplay.pBuffer)
1787 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1789 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1790 if(oglDisplay.pBuffer)
1792 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1793 if(oglDisplay.glContext)
1795 glXMakeCurrent(xGlobalDisplay, None, null);
1796 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1798 // Initialize Shared Memory Pixmap
1799 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1800 ZPixmap, null, &oglDisplay.shminfo, width, height);
1801 if(oglDisplay.image)
1803 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1804 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1805 if(oglDisplay.shminfo.shmid != -1)
1807 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1808 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1810 oglDisplay.shminfo.readOnly = False;
1811 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1813 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1814 &oglDisplay.shminfo, width, height, 32);
1816 // Initialize Shared Memory Shape Pixmap
1817 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1818 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1819 if(oglDisplay.shapeImage)
1821 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1822 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1823 if(oglDisplay.shminfoShape.shmid != -1)
1825 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1826 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1828 oglDisplay.shminfoShape.readOnly = False;
1829 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1831 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1832 &oglDisplay.shminfoShape, width, height, 1);
1833 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1836 XRenderPictureAttributes attributes = { 0 };
1837 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1838 #if !defined(__APPLE__) && !defined(__OLDX__)
1839 attributes.repeat = RepeatNormal;
1841 attributes.repeat = 1;
1843 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1844 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1845 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1846 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1849 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1850 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1867 CreateDisplay(display);
1868 #if defined(__WIN32__)
1869 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1870 #elif defined(__unix__) || defined(__APPLE__)
1871 #if defined(__ANDROID__)
1875 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1882 if(!result && display.alphaBlend)
1884 printf("Alpha blending windows not supported on this display\n");
1891 glViewport(0,0,width,height);
1893 glOrtho(0,width,height,0,0.0,1.0);
1894 displayWidth = display.width = width;
1895 displayHeight = display.height = height;
1897 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1899 oglDisplay.flipBufW = width;
1900 oglDisplay.flipBufH = height;
1901 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1903 if(oglDisplay.flippingBuffer || !width || !height)
1909 void DisplayPosition(Display display, int x, int y)
1911 OGLDisplay oglDisplay = display.driverData;
1917 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1921 void RestorePalette(Display display)
1925 void StartUpdate(Display display)
1929 void EndUpdate(Display display)
1933 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1937 void Update(Display display, Box updateBox)
1939 OGLDisplay oglDisplay = display.driverData;
1940 //Logf("DisplayScreen\n");
1944 #if defined(__WIN32__) || defined(USEPBUFFER)
1945 if(display.alphaBlend)
1947 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1948 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1949 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1950 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1951 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1954 #if defined(__WIN32__)
1956 POINT point = { oglDisplay.x, oglDisplay.y};
1957 POINT srcPoint = { 0, 0 };
1958 BLENDFUNCTION blend = { 0 };
1960 size.cx = display.width;
1961 size.cy = display.height;
1962 blend.BlendOp = AC_SRC_OVER;
1963 blend.BlendFlags = 0;
1964 blend.SourceConstantAlpha = 255;
1965 blend.AlphaFormat = AC_SRC_ALPHA;
1968 // Process partial images. Mapping the buffer waits for
1969 // outstanding DMA transfers into the buffer to finish.
1970 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1971 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1973 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1974 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
1977 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
1978 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
1981 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
1984 // Unmap the image buffers
1985 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1986 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1988 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1989 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1991 // Bind two different buffer objects and start the glReadPixels
1992 // asynchronously. Each call will return directly after
1993 // starting the DMA transfer.
1994 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1995 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1997 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1998 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2002 #elif defined(__unix__) || defined(__APPLE__)
2003 #if defined(__ANDROID__)
2005 XTransform transform =
2008 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2009 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2010 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2013 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2014 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2015 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2016 #if !defined(__APPLE__) && !defined(__OLDX__)
2017 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2019 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2021 XFlush(xGlobalDisplay);
2029 #if defined(__WIN32__)
2030 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2031 SwapBuffers(oglDisplay.hdc);
2032 #elif defined(__unix__) || defined(__APPLE__)
2033 #if defined(__ANDROID__)
2034 eglSwapBuffers(eglDisplay, eglSurface);
2036 glXSwapBuffers(xGlobalDisplay, (int)display.window);
2040 //Logf("Out of DisplayScreen\n");
2043 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2045 glDeleteTextures(1, (int *)&bitmap.driverData);
2046 bitmap.driverData = 0;
2048 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2051 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2053 bool result = false;
2057 uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
2058 //uint w = pow2i(Min(width, 2048)), h = pow2i(Min(height, 2048));
2059 //uint w = pow2i(Min(width, 512)), h = pow2i(Min(height, 512));
2061 glGenTextures(1, &glBitmap);
2062 glBindTexture(GL_TEXTURE_2D, glBitmap);
2064 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2066 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2067 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2069 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2070 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2072 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2074 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2076 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2077 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2081 bitmap.driverData = (void *)glBitmap;
2082 bitmap.driver = displaySystem.driver;
2090 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2092 bool result = false;
2093 OGLSystem oglSystem = displaySystem.driverData;
2095 // Pre process the bitmap... First make it 32 bit
2096 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
2099 uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
2100 //uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
2103 // Switch ARGB to RGBA
2104 //if(bitmap.format != pixelFormatRGBA)
2106 for(c=0; c<bitmap.size; c++)
2108 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2110 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
2111 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2114 bitmap.pixelFormat = pixelFormat888;
2117 glGenTextures(1, &glBitmap);
2120 int error = glGetError();
2125 glBindTexture(GL_TEXTURE_2D, glBitmap);
2126 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2130 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2132 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2133 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2138 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2142 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2145 if(bitmap.width != w || bitmap.height != h)
2147 mipMap = Bitmap { };
2148 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
2150 Surface mipSurface = mipMap.GetSurface(0,0,null);
2151 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
2168 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2169 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2170 //printf("Calling glTexImage2D\n");
2171 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2172 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2173 if((error = glGetError()))
2175 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2176 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2180 if(mipMap != bitmap)
2185 if(!bitmap.keepData)
2186 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
2187 bitmap.driverData = (void *)glBitmap;
2188 bitmap.driver = displaySystem.driver;
2191 FreeBitmap(displaySystem, bitmap);
2192 else if(oglSystem.loadingFont)
2194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2196 oglSystem.loadingFont = false;
2202 void ReleaseSurface(Display display, Surface surface)
2204 glDisable(GL_SCISSOR_TEST);
2205 delete surface.driverData;
2206 surface.driverData = null;
2209 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2214 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2216 bool result = false;
2217 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2219 //Logf("GetSurface\n");
2223 if(displayWidth != display.width || displayHeight != display.height)
2225 displayWidth = display.width;
2226 displayHeight = display.height;
2228 glViewport(0,0,display.width,display.height);
2230 glOrtho(0,display.width,display.height,0,0.0,1.0);
2233 surface.offset.x = x;
2234 surface.offset.y = y;
2235 surface.unclippedBox = surface.box = clip;
2236 oglSurface.bitmapMult[0] = 1;
2237 oglSurface.bitmapMult[1] = 1;
2238 oglSurface.bitmapMult[2] = 1;
2239 oglSurface.bitmapMult[3] = 1;
2241 glEnable(GL_SCISSOR_TEST);
2244 (display.height) -(y+clip.bottom)-1,
2245 clip.right-clip.left+1,
2246 clip.bottom-clip.top+1);
2252 void Clip(Display display, Surface surface, Box clip)
2261 box.Clip(surface.unclippedBox);
2265 box = surface.box = surface.unclippedBox;
2266 box.left += surface.offset.x;
2267 box.top += surface.offset.y;
2268 box.right+= surface.offset.x;
2269 box.bottom += surface.offset.y;
2272 box.left,display.height - box.bottom - 1,
2273 box.right-box.left+1, box.bottom-box.top+1);
2276 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2278 bool result = false;
2279 OGLDisplay oglDisplay = display.driverData;
2280 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2282 if(oglDisplay.flippingBuffer)
2284 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2287 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2293 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2294 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2295 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2296 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2297 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2300 for(row = 0; row<h; row++)
2301 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2308 void SetForeground(Display display, Surface surface, ColorAlpha color)
2310 OGLSurface oglSurface = surface.driverData;
2312 //Logf("SetForeground\n");
2314 oglSurface.foreground[0] = color.color.r/255.0f;
2315 oglSurface.foreground[1] = color.color.g/255.0f;
2316 oglSurface.foreground[2] = color.color.b/255.0f;
2317 //oglSurface.foreground[3] = 1.0f;
2318 oglSurface.foreground[3] = color.a/255.0f;
2320 //if(!oglSurface.foreground[3])printf("bug");
2323 void SetBackground(Display display, Surface surface, ColorAlpha color)
2325 OGLSurface oglSurface = surface.driverData;
2327 //Logf("SetBackground\n");
2329 oglSurface.background[0] = color.color.r/255.0f;
2330 oglSurface.background[1] = color.color.g/255.0f;
2331 oglSurface.background[2] = color.color.b/255.0f;
2332 //oglSurface.background[3] = 1.0;
2333 oglSurface.background[3] = color.a/255.0f;
2336 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2338 OGLSurface oglSurface = surface.driverData;
2340 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2341 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2342 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2343 oglSurface.bitmapMult[3] = color.a/255.0f;
2346 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2351 void PutPixel(Display display, Surface surface,int x,int y)
2353 OGLSurface oglSurface = surface.driverData;
2355 //Logf("PutPixel\n");
2357 glColor4fv(oglSurface.foreground);
2359 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2360 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2365 void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
2367 OGLSurface oglSurface = surface.driverData;
2368 if(x1 == x2) { y2++; y1--; }
2369 else if(y1 == y2) { x2++; x1--; }
2370 x1 += surface.offset.x;
2371 y1 += surface.offset.y;
2372 x2 += surface.offset.x;
2373 y2 += surface.offset.y;
2377 glColor4fv(oglSurface.foreground);
2382 glTexCoord2f(0.5f, 0);
2383 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2384 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2385 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2394 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2395 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2401 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2403 OGLSurface oglSurface = surface.driverData;
2404 x1 += surface.offset.x;
2405 y1 += surface.offset.y;
2406 x2 += surface.offset.x;
2407 y2 += surface.offset.y;
2409 //Logf("Rectangle\n");
2411 glColor4fv(oglSurface.foreground);
2417 glTexCoord2f(0.5f, 0);
2418 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2419 glTexCoord2f(y2-y1 + 0.5f, 0);
2420 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2422 glTexCoord2f(0.5f, 0);
2423 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2424 glTexCoord2f(x2 - x1 + 0.5f, 0);
2425 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2427 glTexCoord2f(0.5f, 0);
2428 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2429 glTexCoord2f(y1 - y2 + 0.5f, 0);
2430 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2432 glTexCoord2f(0.5f, 0);
2433 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2434 glTexCoord2f(x1 - x2 + 0.5f, 0);
2435 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2440 glBegin(GL_LINE_LOOP);
2447 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2448 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2449 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2450 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2455 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2457 OGLSurface oglSurface = surface.driverData;
2460 glColor4fv(oglSurface.background);
2461 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2462 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2465 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2466 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2470 void Clear(Display display, Surface surface, ClearType type)
2472 OGLDisplay oglDisplay = display.driverData;
2473 OGLSurface oglSurface = surface.driverData;
2476 if(type != depthBuffer)
2477 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2478 if(type != colorBuffer && !oglDisplay.depthWrite)
2480 glDepthMask((byte)bool::true);
2482 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2483 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2484 if(type != colorBuffer && !oglDisplay.depthWrite)
2486 glDepthMask((byte)bool::false);
2490 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2495 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2497 OGLSurface oglSurface = surface.driverData;
2499 #if !defined(__OLDX__)
2500 // WHY DO WE HAVE GL_ONE HERE ?
2501 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2502 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2505 if(!oglSurface.writingText)
2507 // glTranslatef(-0.375f, -0.375f, 0.0f);
2508 glEnable(GL_TEXTURE_2D);
2509 glColor4fv(oglSurface.bitmapMult);
2511 else if(oglSurface.xOffset)
2512 glTranslatef(oglSurface.xOffset / 64.0f/*-0.375f*/, 0.0f, 0.0f);
2514 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2519 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2520 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2521 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2522 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2523 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2524 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2525 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2526 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2531 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2532 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2533 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2534 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2535 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2536 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2537 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2538 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2541 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2542 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2543 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2544 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2545 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2546 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2547 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2548 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2552 if(!oglSurface.writingText)
2554 glDisable(GL_TEXTURE_2D);
2556 //glTranslatef(0.375f, 0.375f, 0.0f);
2558 else if(oglSurface.xOffset)
2559 glTranslatef(-oglSurface.xOffset / 64.0f/*+0.375f*/, 0.0f, 0.0f);
2561 #if !defined(__OLDX__)
2562 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2563 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2567 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2569 OGLSurface oglSurface = surface.driverData;
2571 //glTranslatef(-0.375f, -0.375f, 0.0f);
2573 //Logf("Stretch\n");
2575 #if !defined(__OLDX__)
2576 /*if(glBlendFuncSeparate)
2577 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2580 glEnable(GL_TEXTURE_2D);
2581 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2583 glColor4fv(oglSurface.bitmapMult);
2589 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2590 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2592 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2593 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2595 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2596 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2598 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2599 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2603 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2604 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2606 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2607 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2609 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2610 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2612 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2613 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2618 glDisable(GL_TEXTURE_2D);
2620 //glTranslatef(0.375f, 0.375f, 0.0f);
2621 #if !defined(__OLDX__)
2622 /*if(glBlendFuncSeparate)
2623 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2628 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2630 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2633 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2635 float s2dw,s2dh,d2sw,d2sh;
2636 bool flipX = false, flipY = false;
2638 //Logf("StretchDI\n");
2640 if(Sgn(w) != Sgn(sw))
2646 if(Sgn(h) != Sgn(sh))
2658 //Clip against the edges of the source
2661 dx+=(int)((0-sx) * s2dw);
2662 w-=(int)((0-sx) * s2dw);
2668 dy+=(int)((0-sy) * s2dh);
2669 h-=(int)((0-sy) * s2dh);
2674 if(sx+sw>bitmap.width-1)
2676 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2677 sw-=sx+sw-(bitmap.width-1)-1;
2679 if(sy+sh>(bitmap.height-1))
2681 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2682 sh-=sy+sh-(bitmap.height-1)-1;
2684 //Clip against the edges of the surfaceination
2685 if(dx<surface.box.left)
2688 sx+=(int)((surface.box.left-dx)*d2sw);
2689 sw-=(int)((surface.box.left-dx)*d2sw);
2690 w-=surface.box.left-dx;
2691 dx=surface.box.left;
2693 if(dy<surface.box.top)
2695 sy+=(int)((surface.box.top-dy)*d2sh);
2696 sh-=(int)((surface.box.top-dy)*d2sh);
2697 h-=surface.box.top-dy;
2700 if(dx+w>surface.box.right)
2702 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2703 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2704 w-=dx+w-surface.box.right-1;
2706 if(dy+h>surface.box.bottom)
2708 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2709 h-=dy+h-surface.box.bottom-1;
2711 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2713 dx += surface.offset.x;
2714 dy += surface.offset.y;
2716 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2718 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2719 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2720 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2721 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2722 glRasterPos2d(dx,dy);
2723 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2724 glPixelZoom(s2dw, -s2dh);
2725 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2726 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2727 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2731 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2735 //Clip against the edges of the source
2748 if(sx+w>bitmap.width-1)
2749 w-=sx+w-(bitmap.width-1)-1;
2750 if(sy+h>bitmap.height-1)
2751 h-=sy+h-(bitmap.height-1)-1;
2752 //Clip against the edges of the surfaceination
2753 if(dx<surface.box.left)
2756 sx+=surface.box.left-dx;
2757 w-=surface.box.left-dx;
2758 dx=surface.box.left;
2760 if(dy<surface.box.top)
2762 sy+=surface.box.top-dy;
2763 h-=surface.box.top-dy;
2766 if(dx+w>surface.box.right)
2768 //if(flip) sx+=dx+w-surface.box.right-1;
2769 w-=dx+w-surface.box.right-1;
2771 if(dy+h>surface.box.bottom)
2772 h-=dy+h-surface.box.bottom-1;
2776 dx += surface.offset.x;
2777 dy += surface.offset.y;
2779 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2781 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2782 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2783 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2784 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2785 glRasterPos2d(dx,dy);
2787 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2788 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2789 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2793 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2795 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2798 void UnloadFont(DisplaySystem displaySystem, Font font)
2800 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2803 Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
2806 OGLSystem oglSystem = displaySystem.driverData;
2807 oglSystem.loadingFont = true;
2808 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
2812 void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
2814 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
2817 void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
2819 OGLSurface oglSurface = surface.driverData;
2820 OGLSystem oglSystem = display.displaySystem.driverData;
2821 oglSystem.loadingFont = true;
2823 //glTranslatef(-0.375f, -0.375f, 0.0f);
2827 if(surface.textOpacity)
2830 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
2831 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2834 oglSurface.writingText = true;
2836 glEnable(GL_TEXTURE_2D);
2837 glColor4fv(oglSurface.foreground);
2839 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2840 oglSurface.writingText = false;
2841 oglSystem.loadingFont = false;
2843 glDisable(GL_TEXTURE_2D);
2845 //glTranslatef(0.375f, 0.375f, 0.0f);
2848 void TextFont(Display display, Surface surface, Font font)
2850 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2853 void TextOpacity(Display display, Surface surface, bool opaque)
2855 OGLSurface oglSurface = surface.driverData;
2856 oglSurface.opaqueText = opaque;
2859 void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
2861 OGLSurface oglSurface = surface.driverData;
2862 OGLSystem oglSystem = display.displaySystem.driverData;
2863 oglSystem.loadingFont = true;
2864 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
2865 oglSystem.loadingFont = false;
2868 void DrawingChar(Display display, Surface surface, char character)
2873 void LineStipple(Display display, Surface surface, uint32 stipple)
2875 //Logf("Stipple\n");
2879 #if defined(__ANDROID__)
2880 stippleEnabled = true;
2881 glesLineStipple(1, (uint16)stipple);
2883 glLineStipple(1, (uint16)stipple);
2884 glEnable(GL_LINE_STIPPLE);
2889 #if defined(__ANDROID__)
2890 stippleEnabled = false;
2891 glMatrixMode(GL_TEXTURE);
2893 glMatrixMode(GL_PROJECTION);
2894 glDisable(GL_TEXTURE_2D);
2896 glDisable(GL_LINE_STIPPLE);
2900 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
2901 void SetRenderState(Display display, RenderState state, uint value)
2903 OGLDisplay oglDisplay = display.driverData;
2904 //Logf("RenderState\n");
2910 glEnable(GL_MULTISAMPLE_ARB);
2912 glDisable(GL_MULTISAMPLE_ARB);
2915 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2918 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2921 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2922 oglDisplay.depthWrite = value;
2926 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2927 glFogfv(GL_FOG_COLOR, (float *)&color);
2931 glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
2934 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2938 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2939 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2944 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2949 #if defined(__WIN32__)
2950 wglSwapIntervalEXT(value ? 1 : 0);
2957 void SetLight(Display display, int id, Light light)
2959 //Logf("SetLight\n");
2963 Object lightObject = light.lightObject;
2964 float position[4] = { 0, 0, 0, 0 };
2965 float color[4] = { 0, 0, 0, 1 };
2967 glEnable(GL_LIGHT0 + id);
2969 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2970 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2971 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2974 if(!light.multiplier) light.multiplier = 1.0f;
2976 color[0] = light.diffuse.r * light.multiplier;
2977 color[1] = light.diffuse.g * light.multiplier;
2978 color[2] = light.diffuse.b * light.multiplier;
2979 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2981 color[0] = light.ambient.r * light.multiplier;
2982 color[1] = light.ambient.g * light.multiplier;
2983 color[2] = light.ambient.b * light.multiplier;
2984 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2985 color[0] = light.specular.r * light.multiplier;
2986 color[1] = light.specular.g * light.multiplier;
2987 color[2] = light.specular.b * light.multiplier;
2988 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2992 Vector3D positionVector;
2993 if(light.flags.spot)
2995 if(lightObject.flags.root || !lightObject.parent)
2997 positionVector = lightObject.transform.position;
2998 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3002 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3003 if(display.display3D.camera)
3004 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3010 if(!light.direction.x && !light.direction.y && !light.direction.z)
3012 Vector3Df vector { 0,0,-1 };
3014 mat.RotationQuaternion(light.orientation);
3015 positionVector.MultMatrixf(vector, mat);
3019 positionVector = light.direction;
3024 position[0] = (float)positionVector.x;
3025 position[1] = (float)positionVector.y;
3026 position[2] = (float)positionVector.z;
3028 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3031 // Display Light Position
3032 glDisable(GL_LIGHTING);
3033 glDisable(GL_DEPTH_TEST);
3037 glVertex3fv(position);
3039 glEnable(GL_DEPTH_TEST);
3040 glEnable(GL_LIGHTING);
3044 if(lightObject.flags.root || !lightObject.parent)
3046 positionVector = light.target.transform.position;
3047 positionVector.Subtract(positionVector, display.camera.cPosition);
3051 positionVector.MultMatrix(light.target.transform.position,
3052 lightObject.light.target.parent.matrix);
3053 positionVector.Subtract(positionVector, display.camera.cPosition);
3056 position[0] = positionVector.x;
3057 position[1] = positionVector.y;
3058 position[2] = positionVector.z;
3060 glDisable(GL_LIGHTING);
3061 glDisable(GL_DEPTH_TEST);
3065 glVertex3fv(position);
3067 glEnable(GL_DEPTH_TEST);
3068 glEnable(GL_LIGHTING);
3071 if(light.flags.attenuation)
3073 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3074 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3075 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3078 if(light.flags.spot)
3081 #define MAXLIGHT 0.9
3082 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3083 // Figure out exponent out of the hot spot
3084 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3086 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3087 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3088 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3094 Vector3Df vector { 0,0,-1 };
3095 Vector3Df direction;
3098 mat.RotationQuaternion(light.orientation);
3099 direction.MultMatrix(vector, mat);
3101 position[0] = direction.x;
3102 position[1] = direction.y;
3103 position[2] = direction.z;
3105 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3109 glDisable(GL_LIGHT0 + id);
3112 void SetCamera(Display display, Surface surface, Camera camera)
3114 OGLDisplay oglDisplay = display.driverData;
3115 //Logf("SetCamera\n");
3119 int left = surface.box.left + surface.offset.x;
3120 int top = surface.box.top + surface.offset.y;
3121 int right = surface.box.right + surface.offset.x;
3122 int bottom = surface.box.bottom + surface.offset.y;
3123 float origX = surface.offset.x + camera.origin.x;
3124 float origY = surface.offset.y + camera.origin.y;
3126 int y = display.height - bottom - 1;
3127 int w = right - left + 1;
3128 int h = bottom - top + 1;
3131 glViewport(x, y, w, h);
3133 // *** Projection Matrix ***
3134 if(!display.display3D.camera)
3137 glMatrixMode(GL_PROJECTION);
3138 if(display.display3D.collectingHits)
3140 float pickX = display.display3D.pickX + surface.offset.x;
3141 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3145 w / display.display3D.pickWidth, 0, 0, 0,
3146 0, h / display.display3D.pickHeight, 0, 0,
3148 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3149 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3152 glLoadMatrixd(pickMatrix.array);
3157 (left - origX) * camera.zMin / camera.focalX,
3158 (right - origX) * camera.zMin / camera.focalX,
3159 (bottom - origY) * camera.zMin / camera.focalY,
3160 (top - origY) * camera.zMin / camera.focalY,
3161 camera.zMin, camera.zMax);
3163 glDisable(GL_BLEND);
3165 // *** Z Inverted Identity Matrix ***
3166 glMatrixMode(GL_MODELVIEW);
3167 if(!display.display3D.camera)
3171 glScalef(1.0f, 1.0f, -1.0f);
3173 // *** View Matrix ***
3174 glMultMatrixd(camera.viewMatrix.array);
3179 glEnable(GL_DEPTH_TEST);
3180 glEnable(GL_LIGHTING);
3181 glShadeModel(GL_SMOOTH);
3182 glDepthMask((byte)bool::true);
3183 oglDisplay.depthWrite = true;
3185 glEnable(GL_MULTISAMPLE_ARB);
3187 else if(display.display3D.camera)
3189 oglDisplay.depthWrite = false;
3190 glViewport(0,0,display.width,display.height);
3192 glDisable(GL_CULL_FACE);
3193 glDisable(GL_DEPTH_TEST);
3194 glDisable(GL_LIGHTING);
3196 glDisable(GL_TEXTURE_2D);
3197 glShadeModel(GL_FLAT);
3199 glDisable(GL_MULTISAMPLE_ARB);
3201 // *** Restore 2D MODELVIEW Matrix ***
3204 // *** Restore 2D PROJECTION Matrix ***
3205 glMatrixMode(GL_PROJECTION);
3210 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3213 void ApplyMaterial(Display display, Material material, Mesh mesh)
3215 //Logf("ApplyMaterial\n");
3218 if(material.flags.doubleSided)
3220 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3221 glDisable(GL_CULL_FACE);
3225 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3226 glEnable(GL_CULL_FACE);
3230 if(material.flags.noFog)
3236 if(material.baseMap && mesh.texCoords)
3238 Bitmap map = material.baseMap;
3239 glEnable(GL_TEXTURE_2D);
3240 glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
3242 if(material.flags.tile)
3244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3254 glDisable(GL_TEXTURE_2D);
3256 if(mesh.flags.colors)
3258 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3259 glEnable(GL_COLOR_MATERIAL);
3263 glDisable(GL_COLOR_MATERIAL);
3265 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3266 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3269 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3270 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3274 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3275 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3278 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3279 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3282 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3285 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3287 OGLMesh oglMesh = mesh.data;
3290 if(!mesh.flags.vertices)
3292 if(oglMesh.vertices)
3294 glDeleteBuffersARB(1, &oglMesh.vertices);
3295 oglMesh.vertices = 0;
3297 delete mesh.vertices;
3299 if(!mesh.flags.normals)
3303 glDeleteBuffersARB(1, &oglMesh.normals);
3304 oglMesh.normals = 0;
3306 delete mesh.normals;
3308 if(!mesh.flags.texCoords1)
3310 if(oglMesh.texCoords)
3312 glDeleteBuffersARB(1, &oglMesh.texCoords);
3313 oglMesh.texCoords = 0;
3315 delete mesh.texCoords;
3317 if(!mesh.flags.texCoords2)
3319 if(oglMesh.texCoords2)
3321 glDeleteBuffersARB(1, &oglMesh.texCoords2);
3322 oglMesh.texCoords2 = 0;
3325 delete mesh.texCoords2;
3328 if(!mesh.flags.colors)
3332 glDeleteBuffersARB(1, &oglMesh.colors);
3344 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
3346 bool result = false;
3349 mesh.data = OGLMesh { };
3352 OGLMesh oglMesh = mesh.data;
3354 if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
3356 mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3358 glGenBuffersARB(1, &oglMesh.vertices);
3360 if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
3363 glGenBuffersARB( 1, &oglMesh.normals);
3364 mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3366 if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
3369 glGenBuffersARB( 1, &oglMesh.texCoords);
3370 mesh.texCoords = new Pointf[mesh.nVertices];
3372 if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
3375 glGenBuffersARB( 1, &oglMesh.colors);
3376 mesh.colors = new ColorRGBAf[mesh.nVertices];
3383 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3385 OGLMesh oglMesh = mesh.data;
3386 if(!flags) flags = mesh.flags;
3390 if(!(flags.vertices) || oglMesh.vertices)
3392 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3393 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3396 if(!(flags.normals) || oglMesh.normals)
3398 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3399 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
3402 if(!(flags.texCoords1) || oglMesh.texCoords)
3404 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3405 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3408 if(!(flags.colors) || oglMesh.colors)
3410 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3411 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3414 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3418 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3425 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3429 if(oglIndices.buffer)
3430 glDeleteBuffersARB(1, &oglIndices.buffer);
3431 delete oglIndices.indices;
3436 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3438 OGLIndices oglIndices = OGLIndices { };
3441 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3443 glGenBuffersARB( 1, &oglIndices.buffer);
3444 oglIndices.nIndices = nIndices;
3449 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3453 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3454 glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3455 oglIndices.indices, GL_STATIC_DRAW_ARB);
3456 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3460 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3463 return oglIndices.indices;
3466 void SelectMesh(Display display, Mesh mesh)
3468 //Logf("SelectMesh\n");
3470 #if !defined( __ANDROID__) && !defined(__APPLE__)
3471 if(display.display3D.mesh && glUnlockArraysEXT)
3472 glUnlockArraysEXT();
3476 OGLDisplay oglDisplay = display.driverData;
3477 OGLMesh oglMesh = mesh.data;
3479 // *** Vertex Stream ***
3480 glEnableClientState(GL_VERTEX_ARRAY);
3481 if(!display.display3D.collectingHits && oglMesh)
3484 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3485 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
3487 // *** Normals Stream ***
3490 glEnableClientState(GL_NORMAL_ARRAY);
3492 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3493 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
3496 glDisableClientState(GL_NORMAL_ARRAY);
3498 // *** Texture Coordinates Stream ***
3501 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3503 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3504 glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
3507 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3509 // *** Color Stream ***
3512 glEnableClientState(GL_COLOR_ARRAY);
3514 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3515 glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
3518 glDisableClientState(GL_COLOR_ARRAY);
3524 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3525 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3526 if(mesh.normals && !display.display3D.collectingHits)
3528 glEnableClientState(GL_NORMAL_ARRAY);
3529 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3532 glDisableClientState(GL_NORMAL_ARRAY);
3533 if(mesh.texCoords && !display.display3D.collectingHits)
3535 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3536 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3539 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3540 if(mesh.colors && !display.display3D.collectingHits)
3542 glEnableClientState(GL_COLOR_ARRAY);
3543 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3546 glDisableClientState(GL_COLOR_ARRAY);
3549 #if !defined(__ANDROID__) && !defined(__APPLE__)
3550 if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
3553 else if(glBindBufferARB)
3554 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3557 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3559 OGLDisplay oglDisplay = display.driverData;
3560 //Logf("DrawPrimitives\n");
3562 if(primitive->type.vertexRange)
3563 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3566 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3567 // HACK TO SPEED THINGS UP...
3568 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3571 glBegin(primitiveTypes[primitive->type.primitiveType]);
3574 OGLIndices oglIndices = primitive->data;
3575 MeshFeatures flags = mesh.flags;
3576 for(c = 0; c<primitive->nIndices; c++)
3578 short index = ((short *) oglIndices.indices)[c];
3579 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3580 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3581 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3582 glVertex3fv((float *)&mesh.vertices[index]);
3589 OGLIndices oglIndices = primitive->data;
3591 if(!display.display3D.collectingHits && glBindBufferARB && oglIndices)
3593 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3594 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3595 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3596 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3599 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3600 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices.indices);
3602 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3603 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, primitive->indices);
3608 void PushMatrix(Display display)
3613 void PopMatrix(Display display, bool setMatrix)
3618 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3620 Matrix matrix = transMatrix;
3621 Camera camera = useCamera ? display.display3D.camera : null;
3626 glScalef(1.0f, 1.0f, -1.0f);
3631 matrix.m[3][0] - camera.cPosition.x,
3632 matrix.m[3][1] - camera.cPosition.y,
3633 matrix.m[3][2] - camera.cPosition.z);
3645 glMultMatrixd(matrix.array);
3650 public void UseSingleGLContext(bool useSingle)
3652 useSingleGLContext = useSingle;