1 // We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
4 namespace gfx::drivers;
7 #if defined(__unix__) || defined(__APPLE__)
9 #if !defined(__MINGW32__)
10 #define GL_GLEXT_PROTOTYPES
13 #define pointer _pointer
17 //#include <GL/miniglx.h>
21 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
23 #define property _property
27 #define Window X11Window
28 #define Cursor X11Cursor
30 #define Display X11Display
32 #define KeyCode X11KeyCode
33 #define Picture X11Picture
37 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
42 #include <X11/extensions/Xrender.h>
43 #include <X11/extensions/shape.h>
63 #if defined(__APPLE__)
64 #include <OpenGl/gl.h>
67 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
69 #if defined(__WIN32__)
70 #define WIN32_LEAN_AND_MEAN
72 #define _WIN32_WINNT 0x0502
78 #if defined(__ANDROID__)
83 #elif defined(__EMSCRIPTEN__)
85 #define property _property
90 //#include <GLES/gl.h>
91 //#include <EGL/egl.h>
93 //#include <GLES2/gl.h>
94 //#include <EGL/egl.h>
96 //#include <GLES2/gl2.h>
98 #include <emscripten/emscripten.h>
106 #include <GL/glext.h>
110 #if defined(__EMSCRIPTEN__)
121 #if defined(__unix__) || defined(__APPLE__)
123 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
129 static double nearPlane = 1;
131 public double glesGetNearPlane()
136 public void glesSetNearPlane(double value)
141 #define glLoadMatrix glLoadMatrixd
142 #define glMultMatrix glMultMatrixd
143 #define glGetMatrix glGetDoublev
144 #define glTranslate glTranslated
145 #define glScale glScaled
148 #define glVertex3v glVertex3dv
149 #define glNormal3v glNormal3dv
153 //#ifdef VERTEX_FORMAT_DOUBLE
155 #define glLoadMatrix glLoadMatrixd
156 #define glMultMatrix glMultMatrixd
157 #define glGetMatrix glGetDoublev
158 #define glVertex3v glVertex3dv
159 #define glNormal3v glNormal3dv
160 #define glTranslate glTranslated
161 #define glScale glScaled
162 //#define GL_VERTEX_FORMAT GL_DOUBLE
166 #define glLoadMatrix glLoadMatrixf
167 #define glMultMatrix glMultMatrixf
168 #define glGetMatrix glGetFloatv
169 #define glVertex3v glVertex3fv
170 #define glNormal3v glNormal3fv
171 #define glTranslate glTranslatef
172 #define glScale glScalef
173 //#define GL_VERTEX_FORMAT GL_FLOAT
178 #define GL_ARRAY_BUFFER_ARB 0x8892
179 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
180 #define GL_STATIC_DRAW_ARB 0x88E4
181 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
182 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
184 #define GL_MULTISAMPLE_ARB 0x809D
186 #if defined(__WIN32__)
188 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
189 #define WGL_SAMPLES_ARB 0x2042
191 #define WGL_WGLEXT_VERSION 1
192 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
193 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
194 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
195 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
196 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
197 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
198 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
199 #define WGL_ACCELERATION_ARB 0x2003
200 #define WGL_NEED_PALETTE_ARB 0x2004
201 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
202 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
203 #define WGL_SWAP_METHOD_ARB 0x2007
204 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
205 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
206 #define WGL_TRANSPARENT_ARB 0x200A
207 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
208 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
209 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
210 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
211 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
212 #define WGL_SHARE_DEPTH_ARB 0x200C
213 #define WGL_SHARE_STENCIL_ARB 0x200D
214 #define WGL_SHARE_ACCUM_ARB 0x200E
215 #define WGL_SUPPORT_GDI_ARB 0x200F
216 #define WGL_SUPPORT_OPENGL_ARB 0x2010
217 #define WGL_DOUBLE_BUFFER_ARB 0x2011
218 #define WGL_STEREO_ARB 0x2012
219 #define WGL_PIXEL_TYPE_ARB 0x2013
220 #define WGL_COLOR_BITS_ARB 0x2014
221 #define WGL_RED_BITS_ARB 0x2015
222 #define WGL_RED_SHIFT_ARB 0x2016
223 #define WGL_GREEN_BITS_ARB 0x2017
224 #define WGL_GREEN_SHIFT_ARB 0x2018
225 #define WGL_BLUE_BITS_ARB 0x2019
226 #define WGL_BLUE_SHIFT_ARB 0x201A
227 #define WGL_ALPHA_BITS_ARB 0x201B
228 #define WGL_ALPHA_SHIFT_ARB 0x201C
229 #define WGL_ACCUM_BITS_ARB 0x201D
230 #define WGL_ACCUM_RED_BITS_ARB 0x201E
231 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
232 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
233 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
234 #define WGL_DEPTH_BITS_ARB 0x2022
235 #define WGL_STENCIL_BITS_ARB 0x2023
236 #define WGL_AUX_BUFFERS_ARB 0x2024
237 #define WGL_NO_ACCELERATION_ARB 0x2025
238 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
239 #define WGL_FULL_ACCELERATION_ARB 0x2027
240 #define WGL_SWAP_EXCHANGE_ARB 0x2028
241 #define WGL_SWAP_COPY_ARB 0x2029
242 #define WGL_SWAP_UNDEFINED_ARB 0x202A
243 #define WGL_TYPE_RGBA_ARB 0x202B
244 #define WGL_TYPE_COLORINDEX_ARB 0x202C
245 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
246 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
247 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
248 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
249 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
250 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
251 #define WGL_PBUFFER_LARGEST_ARB 0x2033
252 #define WGL_PBUFFER_WIDTH_ARB 0x2034
253 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
254 #define WGL_PBUFFER_LOST_ARB 0x2036
255 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
256 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
257 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
258 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
259 #define WGL_ACCELERATION_EXT 0x2003
260 #define WGL_NEED_PALETTE_EXT 0x2004
261 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
262 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
263 #define WGL_SWAP_METHOD_EXT 0x2007
264 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
265 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
266 #define WGL_TRANSPARENT_EXT 0x200A
267 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
268 #define WGL_SHARE_DEPTH_EXT 0x200C
269 #define WGL_SHARE_STENCIL_EXT 0x200D
270 #define WGL_SHARE_ACCUM_EXT 0x200E
271 #define WGL_SUPPORT_GDI_EXT 0x200F
272 #define WGL_SUPPORT_OPENGL_EXT 0x2010
273 #define WGL_DOUBLE_BUFFER_EXT 0x2011
274 #define WGL_STEREO_EXT 0x2012
275 #define WGL_PIXEL_TYPE_EXT 0x2013
276 #define WGL_COLOR_BITS_EXT 0x2014
277 #define WGL_RED_BITS_EXT 0x2015
278 #define WGL_RED_SHIFT_EXT 0x2016
279 #define WGL_GREEN_BITS_EXT 0x2017
280 #define WGL_GREEN_SHIFT_EXT 0x2018
281 #define WGL_BLUE_BITS_EXT 0x2019
282 #define WGL_BLUE_SHIFT_EXT 0x201A
283 #define WGL_ALPHA_BITS_EXT 0x201B
284 #define WGL_ALPHA_SHIFT_EXT 0x201C
285 #define WGL_ACCUM_BITS_EXT 0x201D
286 #define WGL_ACCUM_RED_BITS_EXT 0x201E
287 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
288 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
289 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
290 #define WGL_DEPTH_BITS_EXT 0x2022
291 #define WGL_STENCIL_BITS_EXT 0x2023
292 #define WGL_AUX_BUFFERS_EXT 0x2024
293 #define WGL_NO_ACCELERATION_EXT 0x2025
294 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
295 #define WGL_FULL_ACCELERATION_EXT 0x2027
296 #define WGL_SWAP_EXCHANGE_EXT 0x2028
297 #define WGL_SWAP_COPY_EXT 0x2029
298 #define WGL_SWAP_UNDEFINED_EXT 0x202A
299 #define WGL_TYPE_RGBA_EXT 0x202B
300 #define WGL_TYPE_COLORINDEX_EXT 0x202C
301 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
302 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
303 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
304 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
305 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
306 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
307 #define WGL_PBUFFER_LARGEST_EXT 0x2033
308 #define WGL_PBUFFER_WIDTH_EXT 0x2034
309 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
310 #define WGL_DEPTH_FLOAT_EXT 0x2040
311 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
312 #define WGL_SAMPLES_3DFX 0x2061
313 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
314 #define WGL_SAMPLES_EXT 0x2042
315 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
316 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
317 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
318 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
319 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
320 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
321 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
322 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
323 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
324 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
325 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
326 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
327 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
328 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
329 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
330 #define WGL_ARB_buffer_region 1
331 #define WGL_ARB_extensions_string 1
332 #define WGL_ARB_pixel_format 1
333 #define WGL_ARB_make_current_read 1
334 #define WGL_ARB_pbuffer 1
335 #define WGL_EXT_display_color_table 1
336 #define WGL_EXT_extensions_string 1
337 #define WGL_EXT_make_current_read 1
338 #define WGL_EXT_pbuffer 1
339 #define WGL_EXT_pixel_format 1
340 #define WGL_EXT_swap_control 1
341 #define WGL_WGL_EXT_depth_float 1
342 #define WGL_WGL_3DFX_multisample 1
343 #define WGL_WGL_EXT_multisample 1
344 #define WGL_NV_allocate_memory 1
347 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
348 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
349 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
350 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
351 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
355 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
356 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
357 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
358 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
360 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
361 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
362 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
363 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
364 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
365 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
366 typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
367 typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
368 typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
370 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
371 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
372 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
373 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
374 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
375 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
376 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
377 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
379 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
380 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
381 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
382 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
383 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
384 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
385 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
386 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
387 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
388 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
389 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
390 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
391 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
393 #define glBufferData glBufferDataARB
395 #ifdef WGL_WGLEXT_PROTOTYPES
396 extern BOOL WINAPI wglSwapIntervalEXT (int);
397 extern int WINAPI wglGetSwapIntervalEXT (void);
398 #endif /* WGL_WGLEXT_PROTOTYPES */
399 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
400 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
402 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
404 #elif defined(__ANDROID__)
406 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
407 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
408 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
410 #define GL_POLYGON_STIPPLE 0xFFFF
411 #define GL_LINE_STIPPLE 0xFFFF
412 #define GL_LINE 0xFFFF
413 #define GL_FILL 0xFFFF
414 #define GL_ALL_ATTRIB_BITS 0xFFFF
415 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
421 #define GL_QUAD_STRIP 0
422 //#define GL_DOUBLE 0
423 //#define GL_UNSIGNED_INT 0
426 //#define GL_LINE_STIPPLE 0
427 #define GL_BGRA_EXT 0
428 #define GL_UNPACK_ROW_LENGTH 0
429 #define GL_UNPACK_SKIP_PIXELS 0
430 #define GL_UNPACK_SKIP_ROWS 0
432 #define GL_PACK_ROW_LENGTH 0
433 #define GL_PACK_SKIP_ROWS 0
434 #define GL_PACK_SKIP_PIXELS 0
438 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
446 FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
447 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
450 void Transpose(Matrix source)
455 m[j][i] = source.m[i][j];
458 void Multiply(Matrix a, Matrix b)
460 // We need a full matrix multiplication for the Projection matrix
461 m[0][0]=a.m[0][0]*b.m[0][0] + a.m[0][1]*b.m[1][0] + a.m[0][2]*b.m[2][0] + a.m[0][3]*b.m[3][0];
462 m[0][1]=a.m[0][0]*b.m[0][1] + a.m[0][1]*b.m[1][1] + a.m[0][2]*b.m[2][1] + a.m[0][3]*b.m[3][1];
463 m[0][2]=a.m[0][0]*b.m[0][2] + a.m[0][1]*b.m[1][2] + a.m[0][2]*b.m[2][2] + a.m[0][3]*b.m[3][2];
464 m[0][3]=a.m[0][0]*b.m[0][3] + a.m[0][1]*b.m[1][3] + a.m[0][2]*b.m[2][3] + a.m[0][3]*b.m[3][3];
466 m[1][0]=a.m[1][0]*b.m[0][0] + a.m[1][1]*b.m[1][0] + a.m[1][2]*b.m[2][0] + a.m[1][3]*b.m[3][0];
467 m[1][1]=a.m[1][0]*b.m[0][1] + a.m[1][1]*b.m[1][1] + a.m[1][2]*b.m[2][1] + a.m[1][3]*b.m[3][1];
468 m[1][2]=a.m[1][0]*b.m[0][2] + a.m[1][1]*b.m[1][2] + a.m[1][2]*b.m[2][2] + a.m[1][3]*b.m[3][2];
469 m[1][3]=a.m[1][0]*b.m[0][3] + a.m[1][1]*b.m[1][3] + a.m[1][2]*b.m[2][3] + a.m[1][3]*b.m[3][3];
471 m[2][0]=a.m[2][0]*b.m[0][0] + a.m[2][1]*b.m[1][0] + a.m[2][2]*b.m[2][0] + a.m[2][3]*b.m[3][0];
472 m[2][1]=a.m[2][0]*b.m[0][1] + a.m[2][1]*b.m[1][1] + a.m[2][2]*b.m[2][1] + a.m[2][3]*b.m[3][1];
473 m[2][2]=a.m[2][0]*b.m[0][2] + a.m[2][1]*b.m[1][2] + a.m[2][2]*b.m[2][2] + a.m[2][3]*b.m[3][2];
474 m[2][3]=a.m[2][0]*b.m[0][3] + a.m[2][1]*b.m[1][3] + a.m[2][2]*b.m[2][3] + a.m[2][3]*b.m[3][3];
476 m[3][0]=a.m[3][0]*b.m[0][0] + a.m[3][1]*b.m[1][0] + a.m[3][2]*b.m[2][0] + a.m[3][3]*b.m[3][0];
477 m[3][1]=a.m[3][0]*b.m[0][1] + a.m[3][1]*b.m[1][1] + a.m[3][2]*b.m[2][1] + a.m[3][3]*b.m[3][1];
478 m[3][2]=a.m[3][0]*b.m[0][2] + a.m[3][1]*b.m[1][2] + a.m[3][2]*b.m[2][2] + a.m[3][3]*b.m[3][2];
479 m[3][3]=a.m[3][0]*b.m[0][3] + a.m[3][1]*b.m[1][3] + a.m[3][2]*b.m[2][3] + a.m[3][3]*b.m[3][3];
484 // Our own matrix stack
485 static Matrix matrixStack[3][32];
486 static int matrixIndex[3];
487 static int curStack = 0;
491 // OpenGL ES Porting Kit
492 #if defined(__ANDROID__)
493 #define glBindFramebuffer glBindFramebufferOES
494 #define glBindRenderbuffer glBindRenderbufferOES
495 #define glFramebufferTexture2D glFramebufferTexture2DOES
496 #define glGenFramebuffers glGenFramebuffersOES
497 #define glGenRenderbuffers glGenRenderbuffersOES
498 #define glDeleteFramebuffers glDeleteFramebuffersOES
499 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
501 #define GL_INT 0x1404
502 #define GL_UNSIGNED_INT 0x1405
503 #define GL_DOUBLE 0x140A
507 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
509 #define glBufferDatai glesBufferDatai
510 #define glBufferDatad glesBufferDatad
511 #define glVertexPointeri glesVertexPointeri
512 #define glVertexPointerd glesVertexPointerd
514 #define glRecti glesRecti
515 #define glBegin glesBegin
516 #define glTexCoord2i glesTexCoord2i
517 #define glVertex2i glesVertex2i
518 #define glTexCoord2d glesTexCoord2d
519 #define glVertex2d glesVertex2d
520 #define glTexCoord2f glesTexCoord2f
521 #define glVertex2f glesVertex2f
522 #define glEnd glesEnd
523 #define glColor3f glesColor3f
524 #define glColor4ub glesColor4ub
525 #define glColor4fv glesColor4fv
526 #define glLineStipple glesLineStipple
527 #define glNormal3fv glesNormal3fv
528 #define glTexCoord2fv glesTexCoord2fv
529 #define glColorMaterial glesColorMaterial
531 #define glLoadMatrixd glesLoadMatrixd
532 #define glMultMatrixd glesMultMatrixd
533 #define glFrustum glesFrustum
534 #define glOrtho glesOrtho
535 #define glScaled glesScaled
536 #define glTranslated glesTranslated
537 #define glRotated glesRotated
538 #define glVertex3d glesVertex3d
539 #define glVertex3dv glesVertex3dv
540 #define glVertex3f glesVertex3f
541 #define glVertex3fv glesVertex3fv
542 #define glLightModeli glesLightModeli
546 #define glVertexPointerd(nc, s, p, nv) glVertexPointer(nc, GL_DOUBLE, s, p)
547 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_INT, start)
551 #if defined(__ANDROID__)
552 static EGLDisplay eglDisplay;
553 static EGLSurface eglSurface;
554 static EGLContext eglContext;
555 static int eglWidth, eglHeight;
557 static bool egl_init_display(ANativeWindow* window)
559 const EGLint attribs[] =
561 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
565 EGL_DEPTH_SIZE, 16, //24,
566 /*EGL_SAMPLE_BUFFERS, 1,
567 EGL_SAMPLES, 0, //2,*/
576 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
577 eglInitialize(display, 0, 0);
578 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
579 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
581 surface = eglCreateWindowSurface(display, config, window, null);
582 context = eglCreateContext(display, config, null, null);
584 if(!eglMakeCurrent(display, surface, surface, context))
587 eglQuerySurface(display, surface, EGL_WIDTH, &w);
588 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
590 eglDisplay = display;
591 eglContext = context;
592 eglSurface = surface;
596 glEnableClientState(GL_VERTEX_ARRAY);
598 // Initialize GL state.
599 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
600 glEnable(GL_CULL_FACE);
601 glShadeModel(GL_SMOOTH);
602 glDisable(GL_DEPTH_TEST);
604 glDisable(GL_CULL_FACE);
605 glDisable(GL_DEPTH_TEST);
607 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
610 matrixStack[0][0].Identity();
611 matrixStack[1][0].Identity();
612 matrixStack[2][0].Identity();
614 glesMatrixMode(GL_MODELVIEW);
615 glScaled(1.0, 1.0, -1.0);
616 glesMatrixMode(GL_PROJECTION);
617 glShadeModel(GL_FLAT);
619 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
620 glFogi(GL_FOG_MODE, GL_EXP);
621 glFogf(GL_FOG_DENSITY, 0);
622 glEnable(GL_NORMALIZE);
623 glDepthFunc(GL_LESS);
625 glDisable(GL_MULTISAMPLE_ARB);
629 glOrtho(0,w,h,0,0.0,1.0);
631 currentVertexBuffer = 0;
635 static void egl_term_display()
639 glDeleteTextures(1, &stippleTexture);
642 if(eglDisplay != EGL_NO_DISPLAY)
644 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
645 if(eglContext != EGL_NO_CONTEXT)
646 eglDestroyContext(eglDisplay, eglContext);
647 if(eglSurface != EGL_NO_SURFACE)
648 eglDestroySurface(eglDisplay, eglSurface);
649 eglTerminate(eglDisplay);
651 eglDisplay = EGL_NO_DISPLAY;
652 eglContext = EGL_NO_CONTEXT;
653 eglSurface = EGL_NO_SURFACE;
658 // OpenGL Immediate Mode Porting Kit
659 static int beginCount;
660 static int vertexCount;
661 static int normalCount;
662 static float *vertexPointer;
663 static float *normalPointer;
664 static GLenum beginMode = -1;
665 static uint beginBufferSize, normalBufferSize;
666 static int numVertexCoords = 2;
667 static bool vertexColorValues = false;
668 static int vertexStride = 4;
669 static int vertexOffset = 2;
671 public void glesRecti(int a, int b, int c, int d)
681 public void glesBegin(GLenum mode)
686 vertexColorValues = false;
693 normalBufferSize = beginBufferSize = 1024; // default number of vertices
694 vertexPointer = new float[beginBufferSize * vertexStride];
695 normalPointer = new float[normalBufferSize * 3];
699 public void glesTexCoord2f(float x, float y)
701 int count = vertexCount;
703 if(vertexCount + numVertexCoords > beginBufferSize)
705 beginBufferSize = beginBufferSize + beginBufferSize/2;
706 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
709 vertexPointer[count*vertexStride ] = x;
710 vertexPointer[count*vertexStride+1] = y;
713 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
715 vertexPointer[count*vertexStride ] = vertexPointer[(count-4)*vertexStride];
716 vertexPointer[count*vertexStride+1] = vertexPointer[(count-4)*vertexStride+1];
718 vertexPointer[count*vertexStride ] = vertexPointer[(count-3)*vertexStride];
719 vertexPointer[count*vertexStride+1] = vertexPointer[(count-3)*vertexStride+1];
723 public void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
724 public void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
725 public void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
727 public void glesVertex2f(float x, float y)
730 vertexStride = vertexOffset + numVertexCoords;
732 if(vertexCount + 4 > beginBufferSize)
734 beginBufferSize = beginBufferSize + beginBufferSize/2;
735 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
738 vertexPointer[vertexCount*vertexStride+vertexOffset] = x;
739 vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = y;
742 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
744 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset];
745 vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset + 1];
747 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset];
748 vertexPointer[vertexCount*vertexStride+vertexOffset + 1] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset + 1];
753 public void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
754 public void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
756 public void glesEnd(void)
758 int mode = beginMode;
759 if(mode == GL_QUADS) mode = GL_TRIANGLES;
760 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
763 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
764 glTexCoordPointer(2, GL_FLOAT, vertexStride * sizeof(float), vertexPointer);
765 if(vertexColorValues)
767 glEnableClientState(GL_COLOR_ARRAY);
768 glColorPointer(4, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + 2);
770 glVertexPointer (numVertexCoords, GL_FLOAT, (vertexStride)*sizeof(float),vertexPointer+vertexOffset);
771 if(normalCount && normalCount == vertexCount)
773 glEnableClientState(GL_NORMAL_ARRAY);
774 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
777 glDrawArrays(mode, 0, vertexCount);
779 glDisableClientState(GL_NORMAL_ARRAY);
780 if(vertexColorValues)
781 glDisableClientState(GL_COLOR_ARRAY);
782 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
784 vertexColorValues = false;
790 static float *floatVPBuffer = null;
791 static short *shortVPBuffer = null;
792 static unsigned int shortVPSize = 0, floatVPSize = 0;
795 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
796 static unsigned short *shortBDBuffer = null;
797 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
799 public void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
804 if(numVertices*numCoords > shortVPSize)
806 shortVPSize = numVertices*numCoords;
807 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
809 for(i = 0; i < numVertices*numCoords; i++)
810 shortVPBuffer[i] = (short)pointer[i];
811 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
814 glVertexPointer(numCoords, GL_SHORT, stride, 0);
817 public void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
822 if(numVertices*numCoords > floatVPSize)
824 floatVPSize = numVertices*numCoords;
825 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
827 for(i = 0; i < numVertices*numCoords; i++)
828 floatVPBuffer[i] = (float)pointer[i];
829 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
832 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
835 public void glesTexReuseIntVP(int numCoords)
837 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
840 public void glesTexReuseDoubleVP(int numCoords)
842 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
845 public void glesColor4f(float r, float g, float b, float a)
847 if(beginMode != (GLenum)-1)
849 int count = vertexCount;
851 vertexColorValues = true;
853 vertexStride = vertexOffset + numVertexCoords;
855 if(vertexCount + vertexStride > beginBufferSize)
857 beginBufferSize = beginBufferSize + beginBufferSize/2;
858 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
861 vertexPointer[count*vertexStride + 2] = r;
862 vertexPointer[count*vertexStride + 3] = g;
863 vertexPointer[count*vertexStride + 4] = b;
864 vertexPointer[count*vertexStride + 5] = a;
867 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
869 vertexPointer[count*vertexStride + 2] = vertexPointer[(count-4) * vertexStride + 2];
870 vertexPointer[count*vertexStride + 3] = vertexPointer[(count-4) * vertexStride + 3];
871 vertexPointer[count*vertexStride + 4] = vertexPointer[(count-4) * vertexStride + 4];
872 vertexPointer[count*vertexStride + 5] = vertexPointer[(count-4) * vertexStride + 5];
874 vertexPointer[count*vertexStride + 2] = vertexPointer[(count-3) * vertexStride + 2];
875 vertexPointer[count*vertexStride + 3] = vertexPointer[(count-3) * vertexStride + 3];
876 vertexPointer[count*vertexStride + 4] = vertexPointer[(count-3) * vertexStride + 4];
877 vertexPointer[count*vertexStride + 5] = vertexPointer[(count-3) * vertexStride + 5];
882 glColor4f(r, g, b, a);
885 public void glesColor3f( float r, float g, float b )
887 glesColor4f(r, g, b, 1.0f);
890 public void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
892 glesColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
895 public void glesColor4fv(float * a)
897 glesColor4f(a[0], a[1], a[2], a[3]);
900 public void glesBufferDatad(int target, int size, void * data, int usage)
902 int numElems = size/sizeof(double);
903 double * dblPtr = (double *)data;
905 if (numElems > floatVPSize)
907 floatVPSize = numElems;
908 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
910 for (i=0; i< numElems; i++)
911 floatVPBuffer[i] = (float)dblPtr[i];
913 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
916 public void glesBufferDatai(int target, int size, void * data, int usage)
918 int numElems = size/sizeof(unsigned int);
919 unsigned int * pointer = (unsigned int *)data;
921 if (numElems > shortBDSize)
923 shortBDSize = numElems;
924 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
926 for (i=0; i< numElems; i++)
927 shortBDBuffer[i] = (unsigned short)pointer[i];
929 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
932 // *** Our Custom Matrix Stack ***
934 static void LoadCurMatrix()
936 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
939 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
940 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
941 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
942 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
947 public void glesLoadIdentity()
949 matrixStack[curStack][matrixIndex[curStack]].Identity();
953 public void glesPushMatrix()
955 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
957 matrixIndex[curStack]++;
958 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
962 public void glesPopMatrix()
964 if(matrixIndex[curStack] > 0)
966 matrixIndex[curStack]--;
971 public void glesLoadMatrixd(double * i)
973 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
977 public void glesOrtho( double l, double r, double b, double t, double n, double f )
981 (2 / (r - l)), 0, 0, 0,
982 0, (2 / (t - b)), 0, 0,
983 0, 0, (-2 / (f - n)), 0,
984 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
987 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
988 matrixStack[curStack][matrixIndex[curStack]] = res;
992 public void glesFrustum( double l, double r, double b, double t, double n, double f )
1002 double A = ((r + l) / (r - l));
1003 double B = ((t + b) / (t - b));
1004 double C = (-(f + n) / (f - n));
1005 double D = (-2*f*n/(f-n));
1008 (2.0*n / (r - l)), 0, 0, 0,
1009 0, (2.0*n / (t - b)), 0, 0,
1014 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1015 matrixStack[curStack][matrixIndex[curStack]] = res;
1020 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1021 public void glesRotated( double a, double b, double c, double d )
1026 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
1027 m.RotationQuaternion(q);
1028 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1029 matrixStack[curStack][matrixIndex[curStack]] = r;
1032 public void glesScaled( double a, double b, double c )
1038 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1039 matrixStack[curStack][matrixIndex[curStack]] = r;
1043 public void glesTranslated( double a, double b, double c )
1049 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
1050 matrixStack[curStack][matrixIndex[curStack]] = r;
1054 public void glesMultMatrixd( double * i )
1057 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
1058 matrixStack[curStack][matrixIndex[curStack]] = r;
1063 public void glesMatrixMode(int mode)
1065 curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
1071 #define glPushMatrix glesPushMatrix
1072 #define glPopMatrix glesPopMatrix
1073 #define glLoadIdentity glesLoadIdentity
1074 #define glMatrixMode glesMatrixMode
1078 /* Using the built-in matrix stack
1079 void glesLoadMatrixd( double * i )
1083 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
1084 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
1085 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
1086 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
1091 void glesOrtho( double l, double r, double b, double t, double n, double f )
1093 float matrix[4][4] =
1095 { (float)(2 / (r - l)), 0, 0, 0 },
1096 { 0, (float)(2 / (t - b)), 0, 0 },
1097 { 0, 0, (float)(-2 / (f - n)), 0 },
1098 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
1100 glMultMatrixf((float *)matrix);
1103 void glesFrustum( double l, double r, double b, double t, double n, double f )
1105 float A = (float)((r + l) / (r - l));
1106 float B = (float)((t + b) / (t - b));
1107 float C = (float)(-(f + n) / (f - n));
1108 float D = (float)(-2*f*n/(f-n));
1109 float matrix[4][4] =
1111 { (float)(2*n / (r - l)), 0, 0, 0 },
1112 { 0, (float)(2*n / (t - b)), 0, 0 },
1116 glMultMatrixf((float *)matrix);
1119 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
1120 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
1121 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
1123 void glesMultMatrixd( double * i )
1127 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
1128 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
1129 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
1130 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
1136 // Need to do these...
1137 public void glesVertex3f( float x, float y, float z )
1139 numVertexCoords = 3;
1140 vertexStride = vertexOffset + numVertexCoords;
1142 if(vertexCount + vertexStride > beginBufferSize)
1144 beginBufferSize = beginBufferSize + beginBufferSize/2;
1145 vertexPointer = renew vertexPointer float[beginBufferSize * vertexStride];
1148 vertexPointer[vertexCount*vertexStride+vertexOffset] = x;
1149 vertexPointer[vertexCount*vertexStride+vertexOffset+1] = y;
1150 vertexPointer[vertexCount*vertexStride+vertexOffset+2] = z;
1153 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1155 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset];
1156 vertexPointer[vertexCount*vertexStride+vertexOffset+1] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset+1];
1157 vertexPointer[vertexCount*vertexStride+vertexOffset+2] = vertexPointer[(vertexCount-4)*vertexStride+vertexOffset+2];
1159 vertexPointer[vertexCount*vertexStride+vertexOffset] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset];
1160 vertexPointer[vertexCount*vertexStride+vertexOffset+1] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset+1];
1161 vertexPointer[vertexCount*vertexStride+vertexOffset+2] = vertexPointer[(vertexCount-3)*vertexStride+vertexOffset+2];
1167 public void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
1168 public void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
1169 public void glesVertex3dv( double* coords ) { glesVertex3f((float)coords[0], (float)coords[1], (float)coords[2]); }
1171 public void glesNormal3f(float x, float y, float z)
1173 normalCount = vertexCount;
1174 if(vertexCount + 4 > normalBufferSize)
1176 normalBufferSize = normalBufferSize + normalBufferSize/2;
1177 normalPointer = renew normalPointer float[normalBufferSize * 2];
1180 normalPointer[normalCount*3+0] = x;
1181 normalPointer[normalCount*3+1] = y;
1182 normalPointer[normalCount*3+2] = z;
1185 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1187 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1188 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1189 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1191 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1192 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1193 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1197 public void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1198 public void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1200 public void glesColorMaterial(int a, int b)
1202 PrintLn("glColorMaterial stub");
1205 public void glesTerminate()
1207 delete vertexPointer;
1208 delete normalPointer;
1209 beginBufferSize = 0;
1211 delete floatVPBuffer;
1214 delete shortVPBuffer;
1217 delete shortBDBuffer;
1221 static GLuint stippleTexture;
1222 #if defined(_GLES) || defined(EM_MODE)
1223 static bool stippleEnabled;
1226 public void glesLineStipple( int i, unsigned short j )
1230 for(x = 0; x < 16; x++)
1232 bool v = (j & (1 << x)) != 0;
1233 texture[x] = v ? 0xFFFFFFFF : 0;
1236 glGenTextures(1, &stippleTexture);
1237 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1238 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1239 glEnable(GL_TEXTURE_2D);
1240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1244 glMatrixMode(GL_TEXTURE);
1246 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1247 glScaled(i/16.0, 1, 1.0f);
1248 glTranslated(0.5, 0.5, 0);
1249 glMatrixMode(GL_PROJECTION);
1252 public void glesLightModeli( unsigned int pname, int param )
1254 #if !defined(EM_MODE)
1255 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1256 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1261 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1262 void glFogi( unsigned int pname, int param ) { }
1263 void glPolygonMode( unsigned int i, unsigned int j ) { }
1266 // *** Picking won't be supported for now ***
1267 void glPushName( unsigned int i ) { }
1268 void glLoadName( unsigned int i ) { }
1269 void glPopName() { }
1271 // Probably replace by regular glBlendFunc ...
1272 void glBlendFuncSeparate(int a, int b, int c, int d)
1277 // For direct pixel blitting...
1278 void glRasterPos2d(double a, double b) { }
1279 void glPixelZoom(float a, float b) { }
1280 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1284 #if !defined(__APPLE__) && !defined(__WIN32__)
1285 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1286 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1287 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1288 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1291 static int currentVertexBuffer;
1293 bool GLSelectVBO(uint vbo)
1295 if(currentVertexBuffer != vbo)
1297 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
1298 currentVertexBuffer = vbo;
1304 void GLGenBuffers(int count, uint * buffer)
1306 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1307 glGenBuffers(count, buffer);
1309 #if defined(__WIN32__)
1312 glGenBuffersARB(count, buffer);
1316 void GLDeleteBuffers(int count, GLuint * buffer)
1318 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1319 glDeleteBuffers(count, buffer);
1321 #if defined(__WIN32__)
1322 if(glDeleteBuffersARB)
1324 glDeleteBuffersARB(count, buffer);
1328 void GLBindBuffer(int target, uint buffer)
1330 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1331 glBindBuffer(target, buffer);
1333 #if defined(__WIN32__)
1336 glBindBufferARB(target, buffer);
1338 currentVertexBuffer = buffer;
1341 public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
1344 if(glType == GL_DOUBLE)
1345 glesVertexPointerd(numCoords, stride, ptr, numVertices);
1346 else if(glType == GL_INT)
1347 glesVertexPointeri(numCoords, stride, ptr, numVertices);
1350 glVertexPointer(numCoords, glType, stride, ptr);
1353 public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1356 if(type == GL_DOUBLE)
1357 glesBufferDatad(target, size, (void *)data, usage);
1358 else if(type == GL_UNSIGNED_INT)
1359 glesBufferDatai(target, size, (void *)data, usage);
1363 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1364 glBufferData(target, size, data, usage);
1367 #if defined(__WIN32__)
1370 glBufferDataARB(target, size, data, usage);
1374 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1375 static int primitiveTypes[RenderPrimitiveType] =
1377 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1382 // Non OpenGL ES friendly stuff
1386 //#undef GL_UNSIGNED_INT
1391 #undef GL_QUAD_STRIP
1392 #undef GL_POLYGON_STIPPLE
1393 #undef GL_LINE_STIPPLE
1396 #undef GL_ALL_ATTRIB_BITS
1397 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1401 static int displayWidth, displayHeight;
1403 #define GL_CLAMP_TO_EDGE 0x812F
1405 static bool vboAvailable;
1407 static bool useSingleGLContext = false;
1408 class OGLDisplay : struct
1410 #if defined(__WIN32__)
1420 int imageBuffers[2];
1421 byte * pboMemory1, * pboMemory2;
1423 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1424 GLXContext glContext;
1427 XShmSegmentInfo shminfo;
1429 XShmSegmentInfo shminfoShape;
1430 XImage * shapeImage;
1434 X11Picture windowPicture;
1435 X11Picture pixmapPicture;
1437 X11Picture shapePicture;
1440 ColorAlpha * flippingBuffer;
1441 int flipBufH, flipBufW;
1446 class OGLSystem : struct
1451 #if defined(__WIN32__)
1452 PIXELFORMATDESCRIPTOR pfd;
1457 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1458 XVisualInfo * visualInfo;
1459 GLXContext glContext;
1460 GLXDrawable glxDrawable;
1464 class OGLSurface : struct
1470 bool writingOutline;
1472 float foreground[4], background[4], bitmapMult[4];
1475 class OGLMesh : struct
1484 class OGLIndices : struct
1494 class OpenGLDisplayDriver : DisplayDriver
1496 class_property(name) = "OpenGL";
1498 bool LockSystem(DisplaySystem displaySystem)
1500 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1501 OGLSystem oglSystem = displaySystem.driverData;
1502 if(useSingleGLContext) return true;
1503 #if defined(__WIN32__)
1504 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1505 #elif defined(__unix__) || defined(__APPLE__)
1506 //if(previous) return true;
1507 // printf("Making SYSTEM current\n");
1508 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1509 //previous = oglSystem.glContext;
1515 void UnlockSystem(DisplaySystem displaySystem)
1517 if(useSingleGLContext) return;
1518 #if defined(__WIN32__)
1519 wglMakeCurrent(null, null);
1520 #elif defined(__unix__) || defined(__APPLE__)
1521 // printf("Making NULL current\n");
1522 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1524 glXMakeCurrent(xGlobalDisplay, None, null);
1530 bool Lock(Display display)
1532 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1533 OGLDisplay oglDisplay = display.driverData;
1534 if(useSingleGLContext) return true;
1535 #if defined(__WIN32__)
1536 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1537 #elif defined(__unix__) || defined(__APPLE__)
1538 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1539 // printf(" Making DISPLAY current\n");
1540 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1546 void Unlock(Display display)
1548 if(useSingleGLContext) return;
1549 //printf(" Making NULL current\n");
1550 //glXMakeCurrent(xGlobalDisplay, None, null);
1552 LockSystem(display.displaySystem);
1555 void DestroyDisplay(Display display)
1557 OGLDisplay oglDisplay = display.driverData;
1561 #if defined(__WIN32__)
1562 wglMakeCurrent( null, null );
1565 wglDeleteContext(oglDisplay.glrc);
1567 if(oglDisplay.hdc && oglDisplay.pBuffer)
1568 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1570 if(oglDisplay.pBuffer)
1571 wglDestroyPbufferARB(oglDisplay.pBuffer);
1574 ReleaseDC(display.window, oglDisplay.hdc);
1576 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1577 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1579 #elif defined(__unix__) || defined(__APPLE__)
1580 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1582 if(oglDisplay.shapePixmap)
1583 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1584 if(oglDisplay.pixmap)
1585 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1586 if(oglDisplay.image)
1588 if(oglDisplay.shminfoShape.shmid != -1)
1590 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1591 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1592 shmdt(oglDisplay.shminfo.shmaddr);
1593 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1596 if(oglDisplay.shapeImage)
1598 if(oglDisplay.shminfoShape.shmid != -1)
1600 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1601 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1602 shmdt(oglDisplay.shminfoShape.shmaddr);
1603 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1605 XDestroyImage(oglDisplay.shapeImage);
1606 oglDisplay.shapeImage = None;
1609 glXMakeCurrent(xGlobalDisplay, None, null);
1611 if(oglDisplay.glContext)
1612 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1615 delete oglDisplay.flippingBuffer;
1617 display.driverData = null;
1621 void ::CheckExtensions(OGLSystem oglSystem)
1623 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1625 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1626 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1629 bool CreateDisplaySystem(DisplaySystem displaySystem)
1631 bool result = false;
1632 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1635 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1637 oglSystem.hdc = GetDC(oglSystem.hwnd);
1641 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1642 oglSystem.pfd.nVersion = 1;
1643 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1644 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1645 oglSystem.pfd.cColorBits = 24;
1646 oglSystem.pfd.cAlphaBits = 8;
1647 oglSystem.pfd.cDepthBits = 24;
1648 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1650 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1651 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1653 if(oglSystem.pfd.cColorBits > 8)
1655 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1656 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1659 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1661 // Get Pointers To The GL Functions
1662 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1663 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1664 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1665 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1666 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1667 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1668 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1669 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1670 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1671 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1672 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1673 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1675 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1676 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1677 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1678 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1679 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1680 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1681 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1682 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1683 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1685 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1687 vboAvailable = glBindBufferARB != null;
1689 // eSystem_LoggingMode(LOG_MSGBOX, null);
1691 if(wglChoosePixelFormatARB)
1696 float fAttributes[] = {0,0};
1699 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1700 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1701 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1702 WGL_COLOR_BITS_ARB,24,
1703 WGL_ALPHA_BITS_ARB,8,
1704 WGL_DEPTH_BITS_ARB,16,
1705 WGL_STENCIL_BITS_ARB,0,
1706 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1707 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1708 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1712 //Log("Found wglChoosePixelFormatARB\n");
1714 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1715 if(!valid || !numFormats)
1717 //Log("Can't find 4x multi sampling\n");
1718 iAttributes[19] = 2;
1719 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1720 if(!valid || !numFormats)
1722 // Log("Can't find 2x multi sampling\n");
1723 iAttributes[16] = 0;
1724 iAttributes[17] = 0;
1725 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1728 if(valid && numFormats)
1730 oglSystem.format = pixelFormat;
1731 wglMakeCurrent(null, null);
1732 wglDeleteContext(oglSystem.glrc);
1734 // *** DescribePixelFormat does not support WGL pixel formats! ***
1735 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1736 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1737 //Log("Successfully set pixel format\n");
1739 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1740 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1744 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1748 CheckExtensions(oglSystem);
1750 wglMakeCurrent(null, null);
1752 //eSystem_DumpErrors(true);
1756 #elif defined(__unix__) || defined(__APPLE__)
1757 vboAvailable = true;
1758 #if defined(__ANDROID__)
1759 egl_init_display(guiApp.desktop.windowHandle);
1760 CheckExtensions(oglSystem);
1762 #elif defined(__EMSCRIPTEN__)
1763 if(glfwInit() == GL_TRUE)
1765 const int width = 640, height = 480;
1766 if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
1768 //glfwSwapBuffers();
1772 printf("glfwOpenWindow() failed\n"); //glfwTerminate();
1775 printf("glfwInit() failed\n"); //glfwTerminate();
1778 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1779 XSetWindowAttributes attr;
1784 #ifndef ECERE_MINIGLX
1785 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1788 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1792 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1793 attr.background_pixel = 0;
1794 attr.border_pixel = 0;
1795 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1796 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1797 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1799 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1800 oglSystem.visualInfo->visual, mask, &attr );
1802 if(oglSystem.visualInfo)
1804 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1805 if(oglSystem.glContext)
1807 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1809 CheckExtensions(oglSystem);
1810 glXMakeCurrent(xGlobalDisplay, None, null);
1817 displaySystem.flags.alpha = true;
1818 displaySystem.flags.flipping = true;
1819 displaySystem.pixelFormat = pixelFormat888;
1823 void DestroyDisplaySystem(DisplaySystem displaySystem)
1825 OGLSystem oglSystem = displaySystem.driverData;
1827 #if defined(__WIN32__)
1828 wglMakeCurrent( null, null );
1831 wglDeleteContext(oglSystem.glrc);
1834 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1835 DestroyWindow(oglSystem.hwnd);
1837 #elif defined(__unix__) || defined(__APPLE__)
1838 #if defined(__ANDROID__)
1840 #elif defined(__EMSCRIPTEN__)
1843 if(oglSystem.visualInfo)
1845 #ifdef ECERE_MINIGLX
1846 __miniglx_XFree(oglSystem.visualInfo);
1848 XFree(oglSystem.visualInfo);
1852 if(oglSystem.glxDrawable)
1854 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1855 oglSystem.glxDrawable = 0;
1862 bool CreateDisplay(Display display)
1864 bool result = false;
1865 OGLDisplay oglDisplay = display.driverData;
1866 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1867 OGLSystem oglSystem = display.displaySystem.driverData;
1870 oglDisplay = display.driverData = OGLDisplay { };
1871 //printf("Inside CreateDisplay\n");
1873 #if defined(__WIN32__) || defined(USEPBUFFER)
1874 if(!display.alphaBlend)
1877 #if defined(__WIN32__)
1878 oglDisplay.hdc = GetDC(display.window);
1879 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1880 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1882 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1883 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1887 ReleaseDC(display.window, oglDisplay.hdc);
1888 #elif defined(__unix__) || defined(__APPLE__)
1889 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1891 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1893 #if defined(__APPLE__)
1894 XVisualInfo template = { 0 };
1895 XWindowAttributes winAttr;
1897 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1898 template.visualid = XVisualIDFromVisual(winAttr.visual);
1899 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1901 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1902 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1903 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1904 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1906 // visualInfo = oglSystem.visualInfo;
1911 //printf("visualInfo is not null\n");
1912 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1913 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1914 //XFree(visualInfo);
1917 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1918 if(oglDisplay.glContext)
1920 //printf("CreateDisplay Got a Context\n");
1921 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1927 #if defined(__WIN32__) || defined(USEPBUFFER)
1933 #if defined(__WIN32__)
1934 if(glBlendFuncSeparate)
1935 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1937 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1939 #if !defined(__OLDX__)
1940 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1942 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1947 glMatrixMode(GL_MODELVIEW);
1948 glScaled(1.0, 1.0, -1.0);
1949 // glTranslatef(0.375f, 0.375f, 0.0f);
1950 // glTranslatef(-0.625f, -0.625f, 0.0f);
1951 glMatrixMode(GL_PROJECTION);
1952 glShadeModel(GL_FLAT);
1954 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1955 #if !defined(EM_MODE)
1956 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1958 glFogi(GL_FOG_MODE, GL_EXP);
1959 glFogf(GL_FOG_DENSITY, 0);
1960 glEnable(GL_NORMALIZE);
1961 glDepthFunc(GL_LESS);
1963 glDisable(GL_MULTISAMPLE_ARB);
1965 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1966 display.ambient = Color { 50,50,50 };
1969 if(!useSingleGLContext)
1971 #if defined(__WIN32__)
1972 wglMakeCurrent(null, null);
1973 #elif defined(__unix__) || defined(__APPLE__)
1974 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1977 glXMakeCurrent(xGlobalDisplay, None, null);
1985 bool DisplaySize(Display display, int width, int height)
1987 OGLDisplay oglDisplay = display.driverData;
1989 bool result = false;
1991 //printf("Inside DisplaySize\n");
1992 #if defined(__WIN32__) || defined(USEPBUFFER)
1993 OGLSystem oglSystem = display.displaySystem.driverData;
1994 if(display.alphaBlend)
1996 #if defined(__WIN32__)
1997 const int attributes[]=
1999 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
2000 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
2002 int pixelFormat = 0;
2003 if(wglChoosePixelFormatARB)
2007 float fAttributes[] = {0,0};
2010 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
2011 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
2012 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
2013 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
2014 WGL_COLOR_BITS_ARB,24,
2015 WGL_ALPHA_BITS_ARB,8,
2016 WGL_DEPTH_BITS_ARB,16,
2017 WGL_STENCIL_BITS_ARB,0,
2018 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
2019 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
2020 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
2024 //Log("Found wglChoosePixelFormatARB\n");
2026 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2027 if(!valid || !numFormats)
2029 //Log("Can't find 4x multi sampling\n");
2030 iAttributes[19] = 2;
2031 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2032 if(!valid || !numFormats)
2034 // Log("Can't find 2x multi sampling\n");
2035 iAttributes[16] = 0;
2036 iAttributes[17] = 0;
2037 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2038 if(!valid || !numFormats)
2042 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
2043 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
2044 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
2045 WGL_COLOR_BITS_ARB,24,
2046 WGL_ALPHA_BITS_ARB,8,
2047 WGL_DEPTH_BITS_ARB,16,
2050 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
2054 if(valid && numFormats)
2056 wglMakeCurrent(null, null);
2060 wglMakeCurrent( null, null );
2061 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
2062 if(oglDisplay.hdc && oglDisplay.pBuffer)
2063 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
2065 wglDestroyPbufferARB(oglDisplay.pBuffer);
2067 if(!useSingleGLContext)
2068 wglMakeCurrent( null, null );
2071 wglDeleteContext(oglDisplay.glrc);
2073 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
2074 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
2075 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
2078 HDC hdc = GetDC(display.window);
2080 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2081 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2083 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
2084 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
2086 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
2088 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
2092 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
2093 oglDisplay.memDC = CreateCompatibleDC(hdc);
2094 SetMapMode(oglDisplay.memDC, MM_TEXT);
2095 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2096 info->bmiHeader.biPlanes = 1;
2097 info->bmiHeader.biCompression = BI_RGB;
2098 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
2099 info->bmiHeader.biWidth = width;
2100 info->bmiHeader.biHeight = height;
2101 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
2104 SelectObject(oglDisplay.memDC, newBitmap);
2105 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
2108 PIXELFORMATDESCRIPTOR pfd = { 0 };
2109 pfd.nSize = (short)sizeof(pfd);
2111 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
2112 pfd.iPixelType = PFD_TYPE_RGBA;
2113 pfd.cColorBits = 32;
2114 //pfd.cAlphaBits = 8;
2115 pfd.cDepthBits = 24;
2116 pfd.iLayerType = PFD_MAIN_PLANE;
2118 oglDisplay.hdc = oglDisplay.memDC;
2120 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
2121 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
2122 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
2124 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
2125 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2126 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2131 const int imageSize = width * height * 4;
2133 glGenBuffersARB(2, oglDisplay.imageBuffers);
2135 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2136 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
2137 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2138 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
2141 oglDisplay.memBitmap = newBitmap;
2142 oglDisplay.stride = width;
2148 ReleaseDC(display.window, hdc);
2150 #elif defined(__unix__) || defined(__APPLE__)
2151 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2156 GLX_DOUBLEBUFFER, True,
2162 GLX_STENCIL_SIZE, 1,
2163 //GLX_DEPTH_SIZE, 24,
2164 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2165 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
2171 GLX_PBUFFER_WIDTH, width,
2172 GLX_PBUFFER_HEIGHT, height,
2173 GLX_LARGEST_PBUFFER, False,
2177 // choose a pixel format that meets our minimum requirements
2180 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
2183 if(oglDisplay.pixmap)
2185 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2186 oglDisplay.pixmap = None;
2188 if(oglDisplay.shapePixmap)
2190 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
2191 oglDisplay.shapePixmap = None;
2194 // Free Shared Memory Pixmap
2195 if(oglDisplay.image)
2197 if(oglDisplay.shminfoShape.shmid != -1)
2199 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
2200 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2201 shmdt(oglDisplay.shminfo.shmaddr);
2202 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
2204 XDestroyImage(oglDisplay.image);
2205 oglDisplay.image = None;
2207 if(oglDisplay.shapeImage)
2209 if(oglDisplay.shminfoShape.shmid != -1)
2211 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
2212 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2213 shmdt(oglDisplay.shminfoShape.shmaddr);
2214 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
2216 XDestroyImage(oglDisplay.shapeImage);
2217 oglDisplay.shapeImage = None;
2220 if(oglDisplay.windowPicture)
2221 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
2222 if(oglDisplay.pixmapPicture)
2223 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
2225 if(oglDisplay.pixmap)
2226 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2228 if(oglDisplay.glContext)
2229 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
2230 if(oglDisplay.pBuffer)
2231 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
2233 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
2234 if(oglDisplay.pBuffer)
2236 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2237 if(oglDisplay.glContext)
2239 glXMakeCurrent(xGlobalDisplay, None, null);
2240 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2242 // Initialize Shared Memory Pixmap
2243 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2244 ZPixmap, null, &oglDisplay.shminfo, width, height);
2245 if(oglDisplay.image)
2247 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2248 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2249 if(oglDisplay.shminfo.shmid != -1)
2251 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2252 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2254 oglDisplay.shminfo.readOnly = False;
2255 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2257 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2258 &oglDisplay.shminfo, width, height, 32);
2260 // Initialize Shared Memory Shape Pixmap
2261 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2262 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2263 if(oglDisplay.shapeImage)
2265 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2266 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2267 if(oglDisplay.shminfoShape.shmid != -1)
2269 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2270 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2272 oglDisplay.shminfoShape.readOnly = False;
2273 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2275 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2276 &oglDisplay.shminfoShape, width, height, 1);
2277 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2280 XRenderPictureAttributes attributes = { 0 };
2281 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2282 #if !defined(__APPLE__) && !defined(__OLDX__)
2283 attributes.repeat = RepeatNormal;
2285 attributes.repeat = 1;
2287 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2288 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2289 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2290 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2293 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2294 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2311 CreateDisplay(display);
2312 #if defined(__WIN32__)
2313 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2314 #elif defined(__unix__) || defined(__APPLE__)
2315 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2319 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2326 if(!result && display.alphaBlend)
2328 printf("Alpha blending windows not supported on this display\n");
2335 glViewport(0,0,width,height);
2337 glOrtho(0,width,height,0,0.0,1.0);
2338 displayWidth = display.width = width;
2339 displayHeight = display.height = height;
2341 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2343 oglDisplay.flipBufW = width;
2344 oglDisplay.flipBufH = height;
2348 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2351 if(oglDisplay.flippingBuffer || !width || !height)
2357 void DisplayPosition(Display display, int x, int y)
2359 OGLDisplay oglDisplay = display.driverData;
2365 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2369 void RestorePalette(Display display)
2373 void StartUpdate(Display display)
2377 void EndUpdate(Display display)
2381 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2385 void Update(Display display, Box updateBox)
2387 #if defined(__WIN32__) || defined(USEPBUFFER)
2388 OGLDisplay oglDisplay = display.driverData;
2390 //Logf("DisplayScreen\n");
2394 #if defined(__WIN32__) || defined(USEPBUFFER)
2395 if(display.alphaBlend)
2397 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2398 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2399 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2400 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2401 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2404 #if defined(__WIN32__)
2406 POINT point = { oglDisplay.x, oglDisplay.y};
2407 POINT srcPoint = { 0, 0 };
2408 BLENDFUNCTION blend = { 0 };
2410 size.cx = display.width;
2411 size.cy = display.height;
2412 blend.BlendOp = AC_SRC_OVER;
2413 blend.BlendFlags = 0;
2414 blend.SourceConstantAlpha = 255;
2415 blend.AlphaFormat = AC_SRC_ALPHA;
2418 // Process partial images. Mapping the buffer waits for
2419 // outstanding DMA transfers into the buffer to finish.
2420 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2421 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2423 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2424 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2427 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2428 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2431 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2434 // Unmap the image buffers
2435 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2436 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2438 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2439 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2441 // Bind two different buffer objects and start the glReadPixels
2442 // asynchronously. Each call will return directly after
2443 // starting the DMA transfer.
2444 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2445 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2447 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2448 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2452 #elif defined(__unix__) || defined(__APPLE__)
2453 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2455 XTransform transform =
2458 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2459 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2460 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2463 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2464 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2465 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2466 #if !defined(__APPLE__) && !defined(__OLDX__)
2467 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2469 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2471 XFlush(xGlobalDisplay);
2479 #if defined(__WIN32__)
2480 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2481 SwapBuffers(oglDisplay.hdc);
2482 #elif defined(__unix__) || defined(__APPLE__)
2483 #if defined(__ANDROID__)
2484 eglSwapBuffers(eglDisplay, eglSurface);
2485 #elif defined(__EMSCRIPTEN__)
2488 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2492 //Logf("Out of DisplayScreen\n");
2495 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2497 if(bitmap.driverData)
2499 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2500 glDeleteTextures(1, &tex);
2501 bitmap.driverData = 0;
2503 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2506 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2508 OGLSystem oglSystem = displaySystem.driverData;
2509 bool result = false;
2511 GLuint glBitmap = 0;
2513 uint w = width, h = height;
2514 if(oglSystem.pow2textures)
2519 w = Min(w, oglSystem.maxTextureSize);
2520 h = Min(h, oglSystem.maxTextureSize);
2522 glGenTextures(1, &glBitmap);
2523 glBindTexture(GL_TEXTURE_2D, glBitmap);
2525 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2527 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2528 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2530 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2531 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2533 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2535 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2537 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2538 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2542 bitmap.driverData = (void *)(uintptr)glBitmap;
2543 bitmap.driver = displaySystem.driver;
2551 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2553 bool result = false;
2554 OGLSystem oglSystem = displaySystem.driverData;
2555 Bitmap convBitmap = bitmap;
2559 convBitmap.Copy(bitmap);
2562 // Pre process the bitmap... First make it 32 bit
2563 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
2566 uint w = bitmap.width, h = bitmap.height;
2567 GLuint glBitmap = 0;
2568 if(oglSystem.pow2textures)
2573 w = Min(w, oglSystem.maxTextureSize);
2574 h = Min(h, oglSystem.maxTextureSize);
2578 while(w * 2 < h) w *= 2;
2579 while(h * 2 < w) h *= 2;
2582 // Switch ARGB to RGBA
2583 //if(bitmap.format != pixelFormatRGBA)
2585 for(c=0; c<bitmap.size; c++)
2587 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2589 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
2590 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2593 // convBitmap.pixelFormat = pixelFormat888;
2596 glGenTextures(1, &glBitmap);
2599 //int error = glGetError();
2603 glBindTexture(GL_TEXTURE_2D, glBitmap);
2604 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2606 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2607 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2608 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2610 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2611 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2613 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2614 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2616 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2620 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2623 if(bitmap.width != w || bitmap.height != h)
2625 mipMap = Bitmap { };
2626 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
2628 Surface mipSurface = mipMap.GetSurface(0,0,null);
2629 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
2639 mipMap = convBitmap;
2646 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2647 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2648 //printf("Calling glTexImage2D\n");
2649 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2650 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2651 if((error = glGetError()))
2653 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2654 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2658 if(mipMap != convBitmap)
2663 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
2664 bitmap.driverData = (void *)(uintptr)glBitmap;
2665 bitmap.driver = displaySystem.driver;
2668 FreeBitmap(displaySystem, bitmap);
2669 else if(oglSystem.loadingFont)
2671 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2672 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2673 oglSystem.loadingFont = false;
2679 void ReleaseSurface(Display display, Surface surface)
2681 glDisable(GL_SCISSOR_TEST);
2682 delete surface.driverData;
2683 surface.driverData = null;
2686 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2691 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2693 bool result = false;
2694 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2696 //Logf("GetSurface\n");
2700 if(displayWidth != display.width || displayHeight != display.height)
2702 displayWidth = display.width;
2703 displayHeight = display.height;
2705 glViewport(0,0,display.width,display.height);
2707 glOrtho(0,display.width,display.height,0,0.0,1.0);
2710 surface.offset.x = x;
2711 surface.offset.y = y;
2712 surface.unclippedBox = surface.box = clip;
2713 oglSurface.bitmapMult[0] = 1;
2714 oglSurface.bitmapMult[1] = 1;
2715 oglSurface.bitmapMult[2] = 1;
2716 oglSurface.bitmapMult[3] = 1;
2718 glEnable(GL_SCISSOR_TEST);
2721 (display.height) -(y+clip.bottom)-1,
2722 clip.right-clip.left+1,
2723 clip.bottom-clip.top+1);
2729 void Clip(Display display, Surface surface, Box clip)
2738 box.Clip(surface.unclippedBox);
2742 box = surface.box = surface.unclippedBox;
2743 box.left += surface.offset.x;
2744 box.top += surface.offset.y;
2745 box.right+= surface.offset.x;
2746 box.bottom += surface.offset.y;
2749 box.left,display.height - box.bottom - 1,
2750 box.right-box.left+1, box.bottom-box.top+1);
2753 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2755 bool result = false;
2756 OGLDisplay oglDisplay = display.driverData;
2757 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2759 if(oglDisplay.flippingBuffer)
2761 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2764 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2770 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2771 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2772 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2773 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2774 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2777 for(row = 0; row<h; row++)
2778 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2785 void SetForeground(Display display, Surface surface, ColorAlpha color)
2787 OGLSurface oglSurface = surface.driverData;
2789 //Logf("SetForeground\n");
2791 oglSurface.foreground[0] = color.color.r/255.0f;
2792 oglSurface.foreground[1] = color.color.g/255.0f;
2793 oglSurface.foreground[2] = color.color.b/255.0f;
2794 //oglSurface.foreground[3] = 1.0f;
2795 oglSurface.foreground[3] = color.a/255.0f;
2797 //if(!oglSurface.foreground[3])printf("bug");
2800 void SetBackground(Display display, Surface surface, ColorAlpha color)
2802 OGLSurface oglSurface = surface.driverData;
2804 //Logf("SetBackground\n");
2806 oglSurface.background[0] = color.color.r/255.0f;
2807 oglSurface.background[1] = color.color.g/255.0f;
2808 oglSurface.background[2] = color.color.b/255.0f;
2809 //oglSurface.background[3] = 1.0;
2810 oglSurface.background[3] = color.a/255.0f;
2813 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2815 OGLSurface oglSurface = surface.driverData;
2817 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2818 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2819 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2820 oglSurface.bitmapMult[3] = color.a/255.0f;
2823 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2828 void PutPixel(Display display, Surface surface,int x,int y)
2830 OGLSurface oglSurface = surface.driverData;
2832 //Logf("PutPixel\n");
2834 glColor4fv(oglSurface.foreground);
2836 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2837 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2842 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2844 OGLSurface oglSurface = surface.driverData;
2845 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2860 x1 += surface.offset.x;
2861 y1 += surface.offset.y;
2862 x2 += surface.offset.x;
2863 y2 += surface.offset.y;
2867 glColor4fv(oglSurface.foreground);
2869 #if defined(_GLES) || defined(EM_MODE)
2872 glTexCoord2f(0.5f, 0);
2873 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2874 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2875 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2884 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2885 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2891 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2893 OGLSurface oglSurface = surface.driverData;
2894 x1 += surface.offset.x;
2895 y1 += surface.offset.y;
2896 x2 += surface.offset.x;
2897 y2 += surface.offset.y;
2899 //Logf("Rectangle\n");
2901 glColor4fv(oglSurface.foreground);
2902 #if defined(_GLES) || defined(EM_MODE)
2907 glTexCoord2f(0.5f, 0);
2908 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2909 glTexCoord2f(y2-y1 + 0.5f, 0);
2910 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2912 glTexCoord2f(0.5f, 0);
2913 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2914 glTexCoord2f(x2 - x1 + 0.5f, 0);
2915 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2917 glTexCoord2f(0.5f, 0);
2918 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2919 glTexCoord2f(y1 - y2 + 0.5f, 0);
2920 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2922 glTexCoord2f(0.5f, 0);
2923 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2924 glTexCoord2f(x1 - x2 + 0.5f, 0);
2925 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2930 glBegin(GL_LINE_LOOP);
2937 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2938 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2939 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2940 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2945 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2947 OGLSurface oglSurface = surface.driverData;
2950 glColor4fv(oglSurface.background);
2954 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
2955 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
2956 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
2957 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
2960 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2961 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2964 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2965 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2969 void Clear(Display display, Surface surface, ClearType type)
2971 OGLDisplay oglDisplay = display.driverData;
2972 OGLSurface oglSurface = surface.driverData;
2975 if(type != depthBuffer)
2976 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2977 if(type != colorBuffer && !oglDisplay.depthWrite)
2979 glDepthMask((byte)bool::true);
2981 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2982 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2983 if(type != colorBuffer && !oglDisplay.depthWrite)
2985 glDepthMask((byte)bool::false);
2989 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2994 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2996 OGLSurface oglSurface = surface.driverData;
2998 #if !defined(__OLDX__)
2999 // WHY DO WE HAVE GL_ONE HERE ?
3000 /*if(glBlendFuncSeparate && !oglSurface.writingText)
3001 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3004 if(!oglSurface.writingText)
3006 // glTranslatef(-0.375f, -0.375f, 0.0f);
3007 glEnable(GL_TEXTURE_2D);
3008 glColor4fv(oglSurface.bitmapMult);
3010 else if(oglSurface.xOffset)
3011 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
3013 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
3018 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
3019 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3020 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
3021 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3022 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
3023 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
3024 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
3025 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
3030 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
3031 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3032 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
3033 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3034 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
3035 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
3036 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
3037 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
3040 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
3041 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
3042 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
3043 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
3044 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
3045 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
3046 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
3047 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
3051 if(!oglSurface.writingText)
3053 glDisable(GL_TEXTURE_2D);
3055 //glTranslate(0.375, 0.375, 0.0);
3057 else if(oglSurface.xOffset)
3058 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
3060 #if !defined(__OLDX__)
3061 /*if(glBlendFuncSeparate && !oglSurface.writingText)
3062 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3066 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3068 OGLSurface oglSurface = surface.driverData;
3070 //glTranslate(-0.375, -0.375, 0.0);
3072 //Logf("Stretch\n");
3074 #if !defined(__OLDX__)
3075 /*if(glBlendFuncSeparate)
3076 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3079 glEnable(GL_TEXTURE_2D);
3080 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
3082 glColor4fv(oglSurface.bitmapMult);
3088 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3089 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3091 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3092 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3094 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3095 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
3097 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3098 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
3102 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3103 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3105 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3106 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3108 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3109 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
3111 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3112 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
3117 glDisable(GL_TEXTURE_2D);
3119 //glTranslate(0.375, 0.375, 0.0);
3120 #if !defined(__OLDX__)
3121 /*if(glBlendFuncSeparate)
3122 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3127 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3129 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3132 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3134 #if !defined(EM_MODE)
3135 float s2dw,s2dh,d2sw,d2sh;
3136 //bool flipX = false, flipY = false;
3138 //Logf("StretchDI\n");
3140 if(Sgn(w) != Sgn(sw))
3146 if(Sgn(h) != Sgn(sh))
3158 //Clip against the edges of the source
3161 dx+=(int)((0-sx) * s2dw);
3162 w-=(int)((0-sx) * s2dw);
3168 dy+=(int)((0-sy) * s2dh);
3169 h-=(int)((0-sy) * s2dh);
3174 if(sx+sw>bitmap.width-1)
3176 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
3177 sw-=sx+sw-(bitmap.width-1)-1;
3179 if(sy+sh>(bitmap.height-1))
3181 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
3182 sh-=sy+sh-(bitmap.height-1)-1;
3184 //Clip against the edges of the surfaceination
3185 if(dx<surface.box.left)
3188 sx+=(int)((surface.box.left-dx)*d2sw);
3189 sw-=(int)((surface.box.left-dx)*d2sw);
3190 w-=surface.box.left-dx;
3191 dx=surface.box.left;
3193 if(dy<surface.box.top)
3195 sy+=(int)((surface.box.top-dy)*d2sh);
3196 sh-=(int)((surface.box.top-dy)*d2sh);
3197 h-=surface.box.top-dy;
3200 if(dx+w>surface.box.right)
3202 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
3203 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
3204 w-=dx+w-surface.box.right-1;
3206 if(dy+h>surface.box.bottom)
3208 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
3209 h-=dy+h-surface.box.bottom-1;
3211 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
3213 dx += surface.offset.x;
3214 dy += surface.offset.y;
3216 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3218 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3219 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3220 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3221 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3222 glRasterPos2d(dx,dy);
3223 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
3224 glPixelZoom(s2dw, -s2dh);
3225 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3226 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3227 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3228 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3229 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3234 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3236 #if !defined(EM_MODE)
3239 //Clip against the edges of the source
3252 if(sx+w>bitmap.width-1)
3253 w-=sx+w-(bitmap.width-1)-1;
3254 if(sy+h>bitmap.height-1)
3255 h-=sy+h-(bitmap.height-1)-1;
3256 //Clip against the edges of the surfaceination
3257 if(dx<surface.box.left)
3260 sx+=surface.box.left-dx;
3261 w-=surface.box.left-dx;
3262 dx=surface.box.left;
3264 if(dy<surface.box.top)
3266 sy+=surface.box.top-dy;
3267 h-=surface.box.top-dy;
3270 if(dx+w>surface.box.right)
3272 //if(flip) sx+=dx+w-surface.box.right-1;
3273 w-=dx+w-surface.box.right-1;
3275 if(dy+h>surface.box.bottom)
3276 h-=dy+h-surface.box.bottom-1;
3280 dx += surface.offset.x;
3281 dy += surface.offset.y;
3283 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3285 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3286 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3287 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3288 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3289 glRasterPos2d(dx,dy);
3291 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3292 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3293 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3294 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3295 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3300 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3302 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3305 void UnloadFont(DisplaySystem displaySystem, Font font)
3307 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3310 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
3313 OGLSystem oglSystem = displaySystem.driverData;
3314 oglSystem.loadingFont = true;
3315 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3319 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
3321 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3324 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
3326 OGLSurface oglSurface = surface.driverData;
3327 OGLSystem oglSystem = display.displaySystem.driverData;
3328 oglSystem.loadingFont = true;
3330 //glTranslated(-0.375, -0.375, 0.0);
3334 if(surface.textOpacity)
3337 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3338 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3341 oglSurface.writingText = true;
3343 glEnable(GL_TEXTURE_2D);
3345 if(surface.outline.size)
3347 ColorAlpha outlineColor = surface.outline.color;
3348 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
3349 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3350 //glEnable(GL_BLEND);
3352 oglSurface.writingOutline = true;
3353 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3354 oglSurface.writingOutline = false;
3356 glColor4fv(oglSurface.foreground);
3358 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3359 oglSurface.writingText = false;
3360 oglSystem.loadingFont = false;
3362 glDisable(GL_TEXTURE_2D);
3364 //glTranslated(0.375, 0.375, 0.0);
3367 void TextFont(Display display, Surface surface, Font font)
3369 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3372 void TextOpacity(Display display, Surface surface, bool opaque)
3374 OGLSurface oglSurface = surface.driverData;
3375 oglSurface.opaqueText = opaque;
3378 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
3380 OGLSurface oglSurface = surface.driverData;
3381 OGLSystem oglSystem = display.displaySystem.driverData;
3382 oglSystem.loadingFont = true;
3383 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3384 oglSystem.loadingFont = false;
3387 void DrawingChar(Display display, Surface surface, char character)
3392 void LineStipple(Display display, Surface surface, uint32 stipple)
3394 //Logf("Stipple\n");
3398 #if defined(_GLES) || defined(EM_MODE)
3399 stippleEnabled = true;
3400 glesLineStipple(1, (uint16)stipple);
3402 glLineStipple(1, (uint16)stipple);
3403 glEnable(GL_LINE_STIPPLE);
3408 #if defined(_GLES) || defined(EM_MODE)
3409 stippleEnabled = false;
3410 glMatrixMode(GL_TEXTURE);
3412 glMatrixMode(GL_PROJECTION);
3413 glDisable(GL_TEXTURE_2D);
3415 glDisable(GL_LINE_STIPPLE);
3419 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3420 void SetRenderState(Display display, RenderState state, uint value)
3422 OGLDisplay oglDisplay = display.driverData;
3423 //Logf("RenderState\n");
3429 glEnable(GL_MULTISAMPLE_ARB);
3431 glDisable(GL_MULTISAMPLE_ARB);
3435 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3439 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3442 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3443 oglDisplay.depthWrite = (bool)value;
3447 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3448 glFogfv(GL_FOG_COLOR, (float *)&color);
3452 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
3455 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3459 #if !defined(EM_MODE)
3460 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3461 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3467 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3472 #if defined(__WIN32__)
3473 wglSwapIntervalEXT(value ? 1 : 0);
3480 void SetLight(Display display, int id, Light light)
3482 #if !defined(EM_MODE)
3483 //Logf("SetLight\n");
3487 Object lightObject = light.lightObject;
3488 float position[4] = { 0, 0, 0, 0 };
3489 float color[4] = { 0, 0, 0, 1 };
3491 glEnable(GL_LIGHT0 + id);
3493 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3494 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3495 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3498 if(!light.multiplier) light.multiplier = 1.0f;
3500 color[0] = light.diffuse.r * light.multiplier;
3501 color[1] = light.diffuse.g * light.multiplier;
3502 color[2] = light.diffuse.b * light.multiplier;
3503 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3505 color[0] = light.ambient.r * light.multiplier;
3506 color[1] = light.ambient.g * light.multiplier;
3507 color[2] = light.ambient.b * light.multiplier;
3508 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3509 color[0] = light.specular.r * light.multiplier;
3510 color[1] = light.specular.g * light.multiplier;
3511 color[2] = light.specular.b * light.multiplier;
3512 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3516 Vector3D positionVector;
3517 if(light.flags.spot)
3519 if(lightObject.flags.root || !lightObject.parent)
3521 positionVector = lightObject.transform.position;
3522 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3526 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3527 if(display.display3D.camera)
3528 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3534 if(!light.direction.x && !light.direction.y && !light.direction.z)
3536 Vector3Df vector { 0,0,-1 };
3538 mat.RotationQuaternion(light.orientation);
3539 positionVector.MultMatrixf(vector, mat);
3543 positionVector = light.direction;
3548 position[0] = (float)positionVector.x;
3549 position[1] = (float)positionVector.y;
3550 position[2] = (float)positionVector.z;
3552 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3555 // Display Light Position
3556 glDisable(GL_LIGHTING);
3557 glDisable(GL_DEPTH_TEST);
3561 glVertex3fv(position);
3563 glEnable(GL_DEPTH_TEST);
3564 glEnable(GL_LIGHTING);
3568 if(lightObject.flags.root || !lightObject.parent)
3570 positionVector = light.target.transform.position;
3571 positionVector.Subtract(positionVector, display.camera.cPosition);
3575 positionVector.MultMatrix(light.target.transform.position,
3576 lightObject.light.target.parent.matrix);
3577 positionVector.Subtract(positionVector, display.camera.cPosition);
3580 position[0] = positionVector.x;
3581 position[1] = positionVector.y;
3582 position[2] = positionVector.z;
3584 glDisable(GL_LIGHTING);
3585 glDisable(GL_DEPTH_TEST);
3589 glVertex3fv(position);
3591 glEnable(GL_DEPTH_TEST);
3592 glEnable(GL_LIGHTING);
3595 if(light.flags.attenuation)
3597 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3598 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3599 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3602 if(light.flags.spot)
3605 #define MAXLIGHT 0.9
3606 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3607 // Figure out exponent out of the hot spot
3608 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3610 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3611 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3612 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3618 Vector3Df vector { 0,0,-1 };
3619 Vector3Df direction;
3622 mat.RotationQuaternion(light.orientation);
3623 direction.MultMatrix(vector, mat);
3625 position[0] = direction.x;
3626 position[1] = direction.y;
3627 position[2] = direction.z;
3629 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3633 glDisable(GL_LIGHT0 + id);
3637 void SetCamera(Display display, Surface surface, Camera camera)
3639 OGLDisplay oglDisplay = display.driverData;
3640 //Logf("SetCamera\n");
3644 int left = surface.box.left + surface.offset.x;
3645 int top = surface.box.top + surface.offset.y;
3646 int right = surface.box.right + surface.offset.x;
3647 int bottom = surface.box.bottom + surface.offset.y;
3648 float origX = surface.offset.x + camera.origin.x;
3649 float origY = surface.offset.y + camera.origin.y;
3651 int y = display.height - bottom - 1;
3652 int w = right - left + 1;
3653 int h = bottom - top + 1;
3656 glViewport(x, y, w, h);
3658 // *** Projection Matrix ***
3659 if(!display.display3D.camera)
3661 glMatrixMode(GL_PROJECTION);
3665 glMatrixMode(GL_PROJECTION);
3666 if(display.display3D.collectingHits)
3668 float pickX = display.display3D.pickX + surface.offset.x;
3669 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3673 w / display.display3D.pickWidth, 0, 0, 0,
3674 0, h / display.display3D.pickHeight, 0, 0,
3676 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3677 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3680 glLoadMatrixd(pickMatrix.array);
3685 (left - origX) * camera.zMin / camera.focalX,
3686 (right - origX) * camera.zMin / camera.focalX,
3687 (bottom - origY) * camera.zMin / camera.focalY,
3688 (top - origY) * camera.zMin / camera.focalY,
3689 camera.zMin, camera.zMax);
3691 glDisable(GL_BLEND);
3693 // *** Z Inverted Identity Matrix ***
3694 glMatrixMode(GL_MODELVIEW);
3695 if(!display.display3D.camera)
3700 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3702 // *** View Matrix ***
3703 glMultMatrixd(camera.viewMatrix.array);
3708 glEnable(GL_DEPTH_TEST);
3709 //#if !defined(EM_MODE)
3710 glEnable(GL_LIGHTING);
3711 glShadeModel(GL_SMOOTH);
3713 glDepthMask((byte)bool::true);
3714 oglDisplay.depthWrite = true;
3716 glEnable(GL_MULTISAMPLE_ARB);
3718 else if(display.display3D.camera)
3720 oglDisplay.depthWrite = false;
3721 glViewport(0,0,display.width,display.height);
3723 glDisable(GL_CULL_FACE);
3724 glDisable(GL_DEPTH_TEST);
3725 glDisable(GL_LIGHTING);
3727 glDisable(GL_TEXTURE_2D);
3728 //#if !defined(EM_MODE)
3729 glShadeModel(GL_FLAT);
3732 glDisable(GL_MULTISAMPLE_ARB);
3734 // *** Restore 2D MODELVIEW Matrix ***
3737 // *** Restore 2D PROJECTION Matrix ***
3738 glMatrixMode(GL_PROJECTION);
3742 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3745 void ApplyMaterial(Display display, Material material, Mesh mesh)
3747 //Logf("ApplyMaterial\n");
3750 if(material.flags.doubleSided)
3752 #if !defined(EM_MODE)
3753 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3755 glDisable(GL_CULL_FACE);
3759 #if !defined(EM_MODE)
3760 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3762 glEnable(GL_CULL_FACE);
3766 if(material.flags.noFog)
3772 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
3774 Bitmap map = material.baseMap;
3775 glEnable(GL_TEXTURE_2D);
3776 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3778 glMatrixMode(GL_TEXTURE);
3780 if(material.uScale && material.vScale)
3781 glScalef(material.uScale, material.vScale, 1);
3782 glMatrixMode(GL_MODELVIEW);
3784 if(material.flags.tile)
3786 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3787 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3791 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3792 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3796 glDisable(GL_TEXTURE_2D);
3799 glColor4f(material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity);
3801 if(mesh.flags.colors)
3803 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3804 glEnable(GL_COLOR_MATERIAL);
3808 glDisable(GL_COLOR_MATERIAL);
3810 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3811 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3814 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3815 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3819 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3820 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3823 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3824 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3827 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3831 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3833 OGLMesh oglMesh = mesh.data;
3836 if(!mesh.flags.vertices)
3838 if(oglMesh.vertices)
3840 GLDeleteBuffers(1, &oglMesh.vertices);
3841 oglMesh.vertices = 0;
3843 delete mesh.vertices;
3845 if(!mesh.flags.normals)
3849 GLDeleteBuffers(1, &oglMesh.normals);
3850 oglMesh.normals = 0;
3852 delete mesh.normals;
3854 if(!mesh.flags.texCoords1)
3856 if(oglMesh.texCoords)
3858 GLDeleteBuffers(1, &oglMesh.texCoords);
3859 oglMesh.texCoords = 0;
3861 delete mesh.texCoords;
3863 if(!mesh.flags.texCoords2)
3865 if(oglMesh.texCoords2)
3867 GLDeleteBuffers(1, &oglMesh.texCoords2);
3868 oglMesh.texCoords2 = 0;
3871 delete mesh.texCoords2;
3874 if(!mesh.flags.colors)
3878 GLDeleteBuffers(1, &oglMesh.colors);
3890 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3892 bool result = false;
3895 mesh.data = OGLMesh { };
3898 OGLMesh oglMesh = mesh.data;
3899 if(mesh.nVertices == nVertices)
3901 // Same number of vertices, adding features (Leaves the other features pointers alone)
3902 if(mesh.flags != flags)
3904 if(!mesh.flags.vertices && flags.vertices)
3906 if(flags.doubleVertices)
3908 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3911 mesh.vertices = new Vector3Df[nVertices];
3912 if(!oglMesh.vertices)
3913 GLGenBuffers(1, &oglMesh.vertices);
3915 if(!mesh.flags.normals && flags.normals)
3917 if(flags.doubleNormals)
3919 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3922 mesh.normals = new Vector3Df[nVertices];
3923 if(!oglMesh.normals)
3924 GLGenBuffers( 1, &oglMesh.normals);
3926 if(!mesh.flags.texCoords1 && flags.texCoords1)
3928 mesh.texCoords = new Pointf[nVertices];
3929 if(!oglMesh.texCoords)
3930 GLGenBuffers( 1, &oglMesh.texCoords);
3932 if(!mesh.flags.colors && flags.colors)
3934 mesh.colors = new ColorRGBAf[nVertices];
3936 GLGenBuffers( 1, &oglMesh.colors);
3942 // New number of vertices, reallocate all current and new features
3943 flags |= mesh.flags;
3946 if(flags.doubleVertices)
3948 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3951 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3952 if(!oglMesh.vertices)
3953 GLGenBuffers(1, &oglMesh.vertices);
3957 if(flags.doubleNormals)
3959 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3962 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3963 if(!oglMesh.normals)
3964 GLGenBuffers( 1, &oglMesh.normals);
3966 if(flags.texCoords1)
3968 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3969 if(!oglMesh.texCoords)
3970 GLGenBuffers( 1, &oglMesh.texCoords);
3974 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3976 GLGenBuffers( 1, &oglMesh.colors);
3984 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3986 OGLMesh oglMesh = mesh.data;
3987 if(!flags) flags = mesh.flags;
3991 if(flags.vertices && oglMesh.vertices)
3993 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3994 GLBufferData( mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3997 if(flags.normals && oglMesh.normals)
3999 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
4000 GLBufferData( mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
4003 if(flags.texCoords1 && oglMesh.texCoords)
4005 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
4006 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
4009 if(flags.colors && oglMesh.colors)
4011 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
4012 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
4015 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
4019 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
4026 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
4030 if(oglIndices.buffer)
4031 GLDeleteBuffers(1, &oglIndices.buffer);
4032 delete oglIndices.indices;
4037 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
4039 OGLIndices oglIndices = OGLIndices { };
4042 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
4043 GLGenBuffers( 1, &oglIndices.buffer);
4044 oglIndices.nIndices = nIndices;
4049 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
4053 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
4054 GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
4055 oglIndices.indices, GL_STATIC_DRAW_ARB);
4056 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4060 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
4063 return oglIndices.indices;
4066 void SelectMesh(Display display, Mesh mesh)
4068 //Logf("SelectMesh\n");
4070 #if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
4072 #if defined(__WIN32__)
4073 if(glUnlockArraysEXT)
4075 if(!vboAvailable && display.display3D.mesh)
4076 glUnlockArraysEXT();
4081 OGLMesh oglMesh = mesh.data;
4083 // *** Vertex Stream ***
4084 glEnableClientState(GL_VERTEX_ARRAY);
4085 if(!display.display3D.collectingHits && oglMesh)
4087 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
4088 if(mesh.flags.doubleVertices)
4089 glVertexPointerd(3, 0, (double *)(vboAvailable ? null : mesh.vertices), mesh.nVertices);
4091 glVertexPointer(3, GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
4093 // *** Normals Stream ***
4094 if(mesh.normals || mesh.flags.normals)
4096 glEnableClientState(GL_NORMAL_ARRAY);
4097 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
4098 glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
4101 glDisableClientState(GL_NORMAL_ARRAY);
4103 // *** Texture Coordinates Stream ***
4104 if(mesh.texCoords || mesh.flags.texCoords1)
4106 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4107 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
4108 glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
4111 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4113 // *** Color Stream ***
4114 if(mesh.colors || mesh.flags.colors)
4116 glEnableClientState(GL_COLOR_ARRAY);
4117 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
4118 glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
4121 glDisableClientState(GL_COLOR_ARRAY);
4126 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
4127 if(mesh.flags.doubleVertices)
4128 glVertexPointerd(3, 0, (double *)mesh.vertices, mesh.nVertices);
4130 glVertexPointer(3, GL_FLOAT, 0, mesh.vertices);
4131 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
4133 glEnableClientState(GL_NORMAL_ARRAY);
4134 glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, mesh.normals);
4137 glDisableClientState(GL_NORMAL_ARRAY);
4138 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
4140 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4141 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
4144 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4145 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
4147 glEnableClientState(GL_COLOR_ARRAY);
4148 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
4151 glDisableClientState(GL_COLOR_ARRAY);
4154 #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
4156 #if defined(__WIN32__)
4160 glLockArraysEXT(0, mesh.nVertices);
4165 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
4168 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
4170 //Logf("DrawPrimitives\n");
4172 if(primitive->type.vertexRange)
4173 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
4176 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
4177 // HACK TO SPEED THINGS UP...
4179 /*GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4180 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
4183 glBegin(primitiveTypes[primitive->type.primitiveType]);
4186 OGLIndices oglIndices = primitive->data;
4187 MeshFeatures flags = mesh.flags;
4188 for(c = 0; c<primitive->nIndices; c++)
4190 uint16 index = ((uint16 *) oglIndices.indices)[c];
4191 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
4192 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
4193 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
4194 glVertex3fv((float *)&mesh.vertices[index]);
4202 OGLIndices oglIndices = primitive->data;
4204 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
4206 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
4207 if(primitive->type.indices32bit)
4208 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 0);
4210 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, GL_UNSIGNED_SHORT, 0);
4211 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4215 if(primitive->type.indices32bit)
4216 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4217 oglIndices ? oglIndices.indices : primitive->indices);
4219 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4220 GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
4226 void PushMatrix(Display display)
4231 void PopMatrix(Display display, bool setMatrix)
4236 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
4238 Matrix matrix = transMatrix;
4239 Camera camera = useCamera ? display.display3D.camera : null;
4244 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
4249 matrix.m[3][0] - camera.cPosition.x,
4250 matrix.m[3][1] - camera.cPosition.y,
4251 matrix.m[3][2] - camera.cPosition.z);
4263 glMultMatrixd(matrix.array);
4268 public void UseSingleGLContext(bool useSingle)
4270 useSingleGLContext = useSingle;
4273 default dllexport void *
4274 #if defined(__WIN32__)
4275 __attribute__((stdcall))
4277 IS_GLGetContext(DisplaySystem displaySystem)
4281 #if defined(__WIN32__)
4282 OGLSystem system = displaySystem.driverData;
4284 #elif defined(__ANDROID__)
4286 #elif defined(__EMSCRIPTEN__)
4288 OGLSystem system = displaySystem.driverData;
4289 return system.glContext;