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
91 //#include <GLES/gl.h>
92 //#include <EGL/egl.h>
94 //#include <GLES2/gl.h>
95 //#include <EGL/egl.h>
97 //#include <GLES2/gl2.h>
99 #include <emscripten/emscripten.h>
107 #include <GL/glext.h>
115 #if defined(__unix__) || defined(__APPLE__)
117 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
123 static double nearPlane = 1;
125 public double glesGetNearPlane()
130 public void glesSetNearPlane(double value)
135 #define glLoadMatrix glLoadMatrixd
136 #define glMultMatrix glMultMatrixd
137 #define glGetMatrix glGetDoublev
138 #define glTranslate glTranslated
139 #define glScale glScaled
142 #define glVertex3v glVertex3dv
143 #define glNormal3v glNormal3dv
147 //#ifdef VERTEX_FORMAT_DOUBLE
149 #define glLoadMatrix glLoadMatrixd
150 #define glMultMatrix glMultMatrixd
151 #define glGetMatrix glGetDoublev
152 #define glVertex3v glVertex3dv
153 #define glNormal3v glNormal3dv
154 #define glTranslate glTranslated
155 #define glScale glScaled
156 //#define GL_VERTEX_FORMAT GL_DOUBLE
160 #define glLoadMatrix glLoadMatrixf
161 #define glMultMatrix glMultMatrixf
162 #define glGetMatrix glGetFloatv
163 #define glVertex3v glVertex3fv
164 #define glNormal3v glNormal3fv
165 #define glTranslate glTranslatef
166 #define glScale glScalef
167 //#define GL_VERTEX_FORMAT GL_FLOAT
172 #define GL_ARRAY_BUFFER_ARB 0x8892
173 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
174 #define GL_STATIC_DRAW_ARB 0x88E4
175 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
176 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
178 #define GL_MULTISAMPLE_ARB 0x809D
180 #if defined(__WIN32__)
182 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
183 #define WGL_SAMPLES_ARB 0x2042
185 #define WGL_WGLEXT_VERSION 1
186 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
187 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
188 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
189 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
190 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
191 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
192 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
193 #define WGL_ACCELERATION_ARB 0x2003
194 #define WGL_NEED_PALETTE_ARB 0x2004
195 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
196 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
197 #define WGL_SWAP_METHOD_ARB 0x2007
198 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
199 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
200 #define WGL_TRANSPARENT_ARB 0x200A
201 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
202 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
203 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
204 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
205 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
206 #define WGL_SHARE_DEPTH_ARB 0x200C
207 #define WGL_SHARE_STENCIL_ARB 0x200D
208 #define WGL_SHARE_ACCUM_ARB 0x200E
209 #define WGL_SUPPORT_GDI_ARB 0x200F
210 #define WGL_SUPPORT_OPENGL_ARB 0x2010
211 #define WGL_DOUBLE_BUFFER_ARB 0x2011
212 #define WGL_STEREO_ARB 0x2012
213 #define WGL_PIXEL_TYPE_ARB 0x2013
214 #define WGL_COLOR_BITS_ARB 0x2014
215 #define WGL_RED_BITS_ARB 0x2015
216 #define WGL_RED_SHIFT_ARB 0x2016
217 #define WGL_GREEN_BITS_ARB 0x2017
218 #define WGL_GREEN_SHIFT_ARB 0x2018
219 #define WGL_BLUE_BITS_ARB 0x2019
220 #define WGL_BLUE_SHIFT_ARB 0x201A
221 #define WGL_ALPHA_BITS_ARB 0x201B
222 #define WGL_ALPHA_SHIFT_ARB 0x201C
223 #define WGL_ACCUM_BITS_ARB 0x201D
224 #define WGL_ACCUM_RED_BITS_ARB 0x201E
225 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
226 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
227 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
228 #define WGL_DEPTH_BITS_ARB 0x2022
229 #define WGL_STENCIL_BITS_ARB 0x2023
230 #define WGL_AUX_BUFFERS_ARB 0x2024
231 #define WGL_NO_ACCELERATION_ARB 0x2025
232 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
233 #define WGL_FULL_ACCELERATION_ARB 0x2027
234 #define WGL_SWAP_EXCHANGE_ARB 0x2028
235 #define WGL_SWAP_COPY_ARB 0x2029
236 #define WGL_SWAP_UNDEFINED_ARB 0x202A
237 #define WGL_TYPE_RGBA_ARB 0x202B
238 #define WGL_TYPE_COLORINDEX_ARB 0x202C
239 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
240 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
241 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
242 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
243 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
244 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
245 #define WGL_PBUFFER_LARGEST_ARB 0x2033
246 #define WGL_PBUFFER_WIDTH_ARB 0x2034
247 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
248 #define WGL_PBUFFER_LOST_ARB 0x2036
249 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
250 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
251 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
252 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
253 #define WGL_ACCELERATION_EXT 0x2003
254 #define WGL_NEED_PALETTE_EXT 0x2004
255 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
256 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
257 #define WGL_SWAP_METHOD_EXT 0x2007
258 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
259 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
260 #define WGL_TRANSPARENT_EXT 0x200A
261 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
262 #define WGL_SHARE_DEPTH_EXT 0x200C
263 #define WGL_SHARE_STENCIL_EXT 0x200D
264 #define WGL_SHARE_ACCUM_EXT 0x200E
265 #define WGL_SUPPORT_GDI_EXT 0x200F
266 #define WGL_SUPPORT_OPENGL_EXT 0x2010
267 #define WGL_DOUBLE_BUFFER_EXT 0x2011
268 #define WGL_STEREO_EXT 0x2012
269 #define WGL_PIXEL_TYPE_EXT 0x2013
270 #define WGL_COLOR_BITS_EXT 0x2014
271 #define WGL_RED_BITS_EXT 0x2015
272 #define WGL_RED_SHIFT_EXT 0x2016
273 #define WGL_GREEN_BITS_EXT 0x2017
274 #define WGL_GREEN_SHIFT_EXT 0x2018
275 #define WGL_BLUE_BITS_EXT 0x2019
276 #define WGL_BLUE_SHIFT_EXT 0x201A
277 #define WGL_ALPHA_BITS_EXT 0x201B
278 #define WGL_ALPHA_SHIFT_EXT 0x201C
279 #define WGL_ACCUM_BITS_EXT 0x201D
280 #define WGL_ACCUM_RED_BITS_EXT 0x201E
281 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
282 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
283 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
284 #define WGL_DEPTH_BITS_EXT 0x2022
285 #define WGL_STENCIL_BITS_EXT 0x2023
286 #define WGL_AUX_BUFFERS_EXT 0x2024
287 #define WGL_NO_ACCELERATION_EXT 0x2025
288 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
289 #define WGL_FULL_ACCELERATION_EXT 0x2027
290 #define WGL_SWAP_EXCHANGE_EXT 0x2028
291 #define WGL_SWAP_COPY_EXT 0x2029
292 #define WGL_SWAP_UNDEFINED_EXT 0x202A
293 #define WGL_TYPE_RGBA_EXT 0x202B
294 #define WGL_TYPE_COLORINDEX_EXT 0x202C
295 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
296 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
297 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
298 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
299 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
300 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
301 #define WGL_PBUFFER_LARGEST_EXT 0x2033
302 #define WGL_PBUFFER_WIDTH_EXT 0x2034
303 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
304 #define WGL_DEPTH_FLOAT_EXT 0x2040
305 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
306 #define WGL_SAMPLES_3DFX 0x2061
307 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
308 #define WGL_SAMPLES_EXT 0x2042
309 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
310 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
311 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
312 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
313 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
314 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
315 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
316 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
317 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
318 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
319 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
320 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
321 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
322 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
323 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
324 #define WGL_ARB_buffer_region 1
325 #define WGL_ARB_extensions_string 1
326 #define WGL_ARB_pixel_format 1
327 #define WGL_ARB_make_current_read 1
328 #define WGL_ARB_pbuffer 1
329 #define WGL_EXT_display_color_table 1
330 #define WGL_EXT_extensions_string 1
331 #define WGL_EXT_make_current_read 1
332 #define WGL_EXT_pbuffer 1
333 #define WGL_EXT_pixel_format 1
334 #define WGL_EXT_swap_control 1
335 #define WGL_WGL_EXT_depth_float 1
336 #define WGL_WGL_3DFX_multisample 1
337 #define WGL_WGL_EXT_multisample 1
338 #define WGL_NV_allocate_memory 1
341 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
342 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
343 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
344 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
345 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
349 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
350 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
351 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
352 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
354 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
355 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
356 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
357 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
358 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
359 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
360 typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
361 typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
362 typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
364 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
365 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
366 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
367 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
368 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
369 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
370 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
371 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
373 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
374 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
375 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
376 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
377 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
378 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
379 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
380 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
381 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
382 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
383 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
384 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
385 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
387 #define glBufferData glBufferDataARB
389 #ifdef WGL_WGLEXT_PROTOTYPES
390 extern BOOL WINAPI wglSwapIntervalEXT (int);
391 extern int WINAPI wglGetSwapIntervalEXT (void);
392 #endif /* WGL_WGLEXT_PROTOTYPES */
393 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
394 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
396 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
398 #elif defined(__ANDROID__)
400 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
401 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
402 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
404 #define GL_POLYGON_STIPPLE 0xFFFF
405 #define GL_LINE_STIPPLE 0xFFFF
406 #define GL_LINE 0xFFFF
407 #define GL_FILL 0xFFFF
408 #define GL_ALL_ATTRIB_BITS 0xFFFF
409 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
415 #define GL_QUAD_STRIP 0
416 //#define GL_DOUBLE 0
417 //#define GL_UNSIGNED_INT 0
420 //#define GL_LINE_STIPPLE 0
421 #define GL_BGRA_EXT 0
422 #define GL_UNPACK_ROW_LENGTH 0
423 #define GL_UNPACK_SKIP_PIXELS 0
424 #define GL_UNPACK_SKIP_ROWS 0
426 #define GL_PACK_ROW_LENGTH 0
427 #define GL_PACK_SKIP_ROWS 0
428 #define GL_PACK_SKIP_PIXELS 0
432 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
440 FillBytesBy4(this, 0, sizeof(Matrix) >> 2);
441 m[0][0]=m[1][1]=m[2][2]=m[3][3]=1;
444 void Transpose(Matrix source)
449 m[j][i] = source.m[i][j];
452 void Multiply(Matrix a, Matrix b)
454 // We need a full matrix multiplication for the Projection matrix
455 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];
456 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];
457 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];
458 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];
460 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];
461 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];
462 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];
463 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];
465 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];
466 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];
467 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];
468 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];
470 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];
471 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];
472 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];
473 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];
478 // Our own matrix stack
479 static Matrix matrixStack[3][32];
480 static int matrixIndex[3];
481 static int curStack = 0;
485 // OpenGL ES Porting Kit
486 #if defined(__ANDROID__)
487 #define glBindFramebuffer glBindFramebufferOES
488 #define glBindRenderbuffer glBindRenderbufferOES
489 #define glFramebufferTexture2D glFramebufferTexture2DOES
490 #define glGenFramebuffers glGenFramebuffersOES
491 #define glGenRenderbuffers glGenRenderbuffersOES
492 #define glDeleteFramebuffers glDeleteFramebuffersOES
493 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
495 #define GL_INT 0x1404
496 #define GL_UNSIGNED_INT 0x1405
497 #define GL_DOUBLE 0x140A
501 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
503 #define glBufferDatai glesBufferDatai
504 #define glBufferDatad glesBufferDatad
505 #define glVertexPointeri glesVertexPointeri
506 #define glVertexPointerd glesVertexPointerd
508 #define glRecti glesRecti
509 #define glBegin glesBegin
510 #define glTexCoord2i glesTexCoord2i
511 #define glVertex2i glesVertex2i
512 #define glTexCoord2d glesTexCoord2d
513 #define glVertex2d glesVertex2d
514 #define glTexCoord2f glesTexCoord2f
515 #define glVertex2f glesVertex2f
516 #define glEnd glesEnd
517 #define glColor3f glesColor3f
518 #define glColor4ub glesColor4ub
519 #define glColor4fv glesColor4fv
520 #define glLineStipple glesLineStipple
521 #define glNormal3fv glesNormal3fv
522 #define glTexCoord2fv glesTexCoord2fv
523 #define glColorMaterial glesColorMaterial
525 #define glLoadMatrixd glesLoadMatrixd
526 #define glMultMatrixd glesMultMatrixd
527 #define glFrustum glesFrustum
528 #define glOrtho glesOrtho
529 #define glScaled glesScaled
530 #define glTranslated glesTranslated
531 #define glRotated glesRotated
532 #define glVertex3d glesVertex3d
533 #define glVertex3dv glesVertex3dv
534 #define glVertex3f glesVertex3f
535 #define glVertex3fv glesVertex3fv
536 #define glLightModeli glesLightModeli
540 #define glVertexPointerd(nc, s, p, nv) glVertexPointer(nc, GL_DOUBLE, s, p)
541 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_INT, start)
545 #if defined(__ANDROID__)
546 static EGLDisplay eglDisplay;
547 static EGLSurface eglSurface;
548 static EGLContext eglContext;
549 static int eglWidth, eglHeight;
551 static bool egl_init_display(ANativeWindow* window)
553 const EGLint attribs[] =
555 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
559 EGL_DEPTH_SIZE, 16, //24,
560 /*EGL_SAMPLE_BUFFERS, 1,
561 EGL_SAMPLES, 0, //2,*/
570 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
571 eglInitialize(display, 0, 0);
572 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
573 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
575 surface = eglCreateWindowSurface(display, config, window, null);
576 context = eglCreateContext(display, config, null, null);
578 if(!eglMakeCurrent(display, surface, surface, context))
581 eglQuerySurface(display, surface, EGL_WIDTH, &w);
582 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
584 eglDisplay = display;
585 eglContext = context;
586 eglSurface = surface;
590 glEnableClientState(GL_VERTEX_ARRAY);
592 // Initialize GL state.
593 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
594 glEnable(GL_CULL_FACE);
595 glShadeModel(GL_SMOOTH);
596 glDisable(GL_DEPTH_TEST);
598 glDisable(GL_CULL_FACE);
599 glDisable(GL_DEPTH_TEST);
601 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
604 matrixStack[0][0].Identity();
605 matrixStack[1][0].Identity();
606 matrixStack[2][0].Identity();
608 glesMatrixMode(GL_MODELVIEW);
609 glScaled(1.0, 1.0, -1.0);
610 glesMatrixMode(GL_PROJECTION);
611 glShadeModel(GL_FLAT);
613 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
614 glFogi(GL_FOG_MODE, GL_EXP);
615 glFogf(GL_FOG_DENSITY, 0);
616 glEnable(GL_NORMALIZE);
617 glDepthFunc(GL_LESS);
619 glDisable(GL_MULTISAMPLE_ARB);
623 glOrtho(0,w,h,0,0.0,1.0);
625 currentVertexBuffer = 0;
629 static void egl_term_display()
633 glDeleteTextures(1, &stippleTexture);
636 if(eglDisplay != EGL_NO_DISPLAY)
638 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
639 if(eglContext != EGL_NO_CONTEXT)
640 eglDestroyContext(eglDisplay, eglContext);
641 if(eglSurface != EGL_NO_SURFACE)
642 eglDestroySurface(eglDisplay, eglSurface);
643 eglTerminate(eglDisplay);
645 eglDisplay = EGL_NO_DISPLAY;
646 eglContext = EGL_NO_CONTEXT;
647 eglSurface = EGL_NO_SURFACE;
652 // OpenGL Immediate Mode Porting Kit
653 static int beginCount;
654 static int vertexCount;
655 static int normalCount;
656 static float *vertexPointer;
657 static float *normalPointer;
658 static GLenum beginMode;
659 static unsigned int beginBufferSize, normalBufferSize;
660 static int numVertexCoords = 2;
662 public void glesRecti(int a, int b, int c, int d)
672 public void glesBegin(GLenum mode)
679 normalBufferSize = beginBufferSize = 1024; // default number of vertices
680 vertexPointer = new float[beginBufferSize * 5];
681 normalPointer = new float[normalBufferSize * 3];
685 public void glesTexCoord2f(float x, float y)
687 int count = vertexCount;
689 if(vertexCount + numVertexCoords > beginBufferSize)
691 beginBufferSize = beginBufferSize + beginBufferSize/2;
692 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
695 vertexPointer[count*(2+numVertexCoords) ] = x;
696 vertexPointer[count*(2+numVertexCoords)+1] = y;
699 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
701 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
702 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
704 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
705 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
709 public void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
710 public void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
711 public void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
713 public void glesVertex2f(float x, float y)
716 if(vertexCount + 4 > beginBufferSize)
718 beginBufferSize = beginBufferSize + beginBufferSize/2;
719 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
722 vertexPointer[vertexCount*4+2] = x;
723 vertexPointer[vertexCount*4+3] = y;
726 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
728 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
729 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
731 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
732 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
737 public void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
738 public void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
740 public void glesEnd(void)
742 int mode = beginMode;
743 if(mode == GL_QUADS) mode = GL_TRIANGLES;
744 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
746 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
747 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
748 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
749 if(normalCount && normalCount == vertexCount)
751 glEnableClientState(GL_NORMAL_ARRAY);
752 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
755 glDrawArrays(mode, 0, vertexCount);
757 glDisableClientState(GL_NORMAL_ARRAY);
758 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
763 static float *floatVPBuffer = null;
764 static short *shortVPBuffer = null;
765 static unsigned int shortVPSize = 0, floatVPSize = 0;
768 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
769 static unsigned short *shortBDBuffer = null;
770 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
772 public void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
777 if(numVertices*numCoords > shortVPSize)
779 shortVPSize = numVertices*numCoords;
780 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
782 for(i = 0; i < numVertices*numCoords; i++)
783 shortVPBuffer[i] = (short)pointer[i];
784 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
787 glVertexPointer(numCoords, GL_SHORT, stride, 0);
790 public void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
795 if(numVertices*numCoords > floatVPSize)
797 floatVPSize = numVertices*numCoords;
798 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
800 for(i = 0; i < numVertices*numCoords; i++)
801 floatVPBuffer[i] = (float)pointer[i];
802 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
805 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
808 public void glesTexReuseIntVP(int numCoords)
810 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
813 public void glesTexReuseDoubleVP(int numCoords)
815 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
818 public void glesColor3f( float r, float g, float b )
820 glColor4f(r, g, b, 1.0f);
823 public void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
825 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
828 public void glesColor4fv(float * a)
830 glColor4f(a[0], a[1], a[2], a[3]);
833 public void glesBufferDatad(int target, int size, void * data, int usage)
835 int numElems = size/sizeof(double);
836 double * dblPtr = (double *)data;
838 if (numElems > floatVPSize)
840 floatVPSize = numElems;
841 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
843 for (i=0; i< numElems; i++)
844 floatVPBuffer[i] = (float)dblPtr[i];
846 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
849 public void glesBufferDatai(int target, int size, void * data, int usage)
851 int numElems = size/sizeof(unsigned int);
852 unsigned int * pointer = (unsigned int *)data;
854 if (numElems > shortBDSize)
856 shortBDSize = numElems;
857 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
859 for (i=0; i< numElems; i++)
860 shortBDBuffer[i] = (unsigned short)pointer[i];
862 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
865 // *** Our Custom Matrix Stack ***
867 static void LoadCurMatrix()
869 double * i = matrixStack[curStack][matrixIndex[curStack]].array;
872 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
873 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
874 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
875 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
880 public void glesLoadIdentity()
882 matrixStack[curStack][matrixIndex[curStack]].Identity();
886 public void glesPushMatrix()
888 if(matrixIndex[curStack] + 1 < sizeof(matrixStack[0]) / sizeof(Matrix))
890 matrixIndex[curStack]++;
891 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrixStack[curStack][matrixIndex[curStack]-1].array, sizeof(Matrix));
895 public void glesPopMatrix()
897 if(matrixIndex[curStack] > 0)
899 matrixIndex[curStack]--;
904 public void glesLoadMatrixd(double * i)
906 memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
910 public void glesOrtho( double l, double r, double b, double t, double n, double f )
914 (2 / (r - l)), 0, 0, 0,
915 0, (2 / (t - b)), 0, 0,
916 0, 0, (-2 / (f - n)), 0,
917 (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
920 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
921 matrixStack[curStack][matrixIndex[curStack]] = res;
925 public void glesFrustum( double l, double r, double b, double t, double n, double f )
935 double A = ((r + l) / (r - l));
936 double B = ((t + b) / (t - b));
937 double C = (-(f + n) / (f - n));
938 double D = (-2*f*n/(f-n));
941 (2.0*n / (r - l)), 0, 0, 0,
942 0, (2.0*n / (t - b)), 0, 0,
947 res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
948 matrixStack[curStack][matrixIndex[curStack]] = res;
953 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
954 public void glesRotated( double a, double b, double c, double d )
959 q.RotationAxis({(float)b,(float)c,(float)-d}, a );
960 m.RotationQuaternion(q);
961 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
962 matrixStack[curStack][matrixIndex[curStack]] = r;
965 public void glesScaled( double a, double b, double c )
971 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
972 matrixStack[curStack][matrixIndex[curStack]] = r;
976 public void glesTranslated( double a, double b, double c )
982 r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
983 matrixStack[curStack][matrixIndex[curStack]] = r;
987 public void glesMultMatrixd( double * i )
990 r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
991 matrixStack[curStack][matrixIndex[curStack]] = r;
996 public void glesMatrixMode(int mode)
998 curStack = (mode == GL_MODELVIEW) ? 0 : (mode == GL_PROJECTION) ? 1 : 2;
1004 #define glPushMatrix glesPushMatrix
1005 #define glPopMatrix glesPopMatrix
1006 #define glLoadIdentity glesLoadIdentity
1007 #define glMatrixMode glesMatrixMode
1011 /* Using the built-in matrix stack
1012 void glesLoadMatrixd( double * i )
1016 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
1017 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
1018 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
1019 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
1024 void glesOrtho( double l, double r, double b, double t, double n, double f )
1026 float matrix[4][4] =
1028 { (float)(2 / (r - l)), 0, 0, 0 },
1029 { 0, (float)(2 / (t - b)), 0, 0 },
1030 { 0, 0, (float)(-2 / (f - n)), 0 },
1031 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
1033 glMultMatrixf((float *)matrix);
1036 void glesFrustum( double l, double r, double b, double t, double n, double f )
1038 float A = (float)((r + l) / (r - l));
1039 float B = (float)((t + b) / (t - b));
1040 float C = (float)(-(f + n) / (f - n));
1041 float D = (float)(-2*f*n/(f-n));
1042 float matrix[4][4] =
1044 { (float)(2*n / (r - l)), 0, 0, 0 },
1045 { 0, (float)(2*n / (t - b)), 0, 0 },
1049 glMultMatrixf((float *)matrix);
1052 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
1053 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
1054 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
1056 void glesMultMatrixd( double * i )
1060 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
1061 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
1062 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
1063 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
1069 // Need to do these...
1070 public void glesVertex3f( float x, float y, float z )
1072 numVertexCoords = 3;
1073 if(vertexCount + 4 > beginBufferSize)
1075 beginBufferSize = beginBufferSize + beginBufferSize/2;
1076 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
1079 vertexPointer[vertexCount*5+2] = x;
1080 vertexPointer[vertexCount*5+3] = y;
1081 vertexPointer[vertexCount*5+4] = z;
1084 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1086 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
1087 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
1088 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
1090 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
1091 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
1092 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
1098 public void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
1099 public void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
1100 public void glesVertex3dv( double* coords ) { glesVertex3f((float)coords[0], (float)coords[1], (float)coords[2]); }
1102 public void glesNormal3f(float x, float y, float z)
1104 normalCount = vertexCount;
1105 if(vertexCount + 4 > normalBufferSize)
1107 normalBufferSize = normalBufferSize + normalBufferSize/2;
1108 normalPointer = renew normalPointer float[normalBufferSize * 2];
1111 normalPointer[normalCount*3+0] = x;
1112 normalPointer[normalCount*3+1] = y;
1113 normalPointer[normalCount*3+2] = z;
1116 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
1118 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
1119 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
1120 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
1122 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
1123 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
1124 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
1128 public void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
1129 public void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
1131 public void glesColorMaterial(int a, int b)
1133 PrintLn("glColorMaterial stub");
1136 public void glesTerminate()
1138 delete vertexPointer;
1139 delete normalPointer;
1140 beginBufferSize = 0;
1142 delete floatVPBuffer;
1145 delete shortVPBuffer;
1148 delete shortBDBuffer;
1152 static GLuint stippleTexture;
1154 static bool stippleEnabled;
1157 public void glesLineStipple( int i, unsigned short j )
1161 for(x = 0; x < 16; x++)
1163 bool v = (j & (1 << x)) != 0;
1164 texture[x] = v ? 0xFFFFFFFF : 0;
1167 glGenTextures(1, &stippleTexture);
1168 glBindTexture(GL_TEXTURE_2D, stippleTexture);
1169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
1170 glEnable(GL_TEXTURE_2D);
1171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1174 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1175 glMatrixMode(GL_TEXTURE);
1177 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
1178 glScaled(i/16.0, 1, 1.0f);
1179 glTranslated(0.5, 0.5, 0);
1180 glMatrixMode(GL_PROJECTION);
1183 public void glesLightModeli( unsigned int pname, int param )
1185 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
1186 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
1190 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
1191 void glFogi( unsigned int pname, int param ) { }
1192 void glPolygonMode( unsigned int i, unsigned int j ) { }
1195 // *** Picking won't be supported for now ***
1196 void glPushName( unsigned int i ) { }
1197 void glLoadName( unsigned int i ) { }
1198 void glPopName() { }
1200 // Probably replace by regular glBlendFunc ...
1201 void glBlendFuncSeparate(int a, int b, int c, int d)
1206 // For direct pixel blitting...
1207 void glRasterPos2d(double a, double b) { }
1208 void glPixelZoom(float a, float b) { }
1209 void glDrawPixels(int a, int b, int c, int d, void * e) { }
1213 #if !defined(__APPLE__) && !defined(__WIN32__)
1214 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
1215 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
1216 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
1217 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
1220 static int currentVertexBuffer;
1222 bool GLSelectVBO(uint vbo)
1224 if(currentVertexBuffer != vbo)
1226 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
1227 currentVertexBuffer = vbo;
1233 void GLGenBuffers(int count, uint * buffer)
1236 glGenBuffers(count, buffer);
1238 #if defined(__WIN32__)
1241 glGenBuffersARB(count, buffer);
1245 void GLDeleteBuffers(int count, GLuint * buffer)
1248 glDeleteBuffers(count, buffer);
1250 #if defined(__WIN32__)
1251 if(glDeleteBuffersARB)
1253 glDeleteBuffersARB(count, buffer);
1257 void GLBindBuffer(int target, uint buffer)
1260 glBindBuffer(target, buffer);
1262 #if defined(__WIN32__)
1265 glBindBufferARB(target, buffer);
1269 public void GLVertexPointer(int numCoords, int glType, int stride, void *ptr, int numVertices)
1272 if(glType == GL_DOUBLE)
1273 glesVertexPointerd(numCoords, stride, ptr, numVertices);
1274 else if(glType == GL_INT)
1275 glesVertexPointeri(numCoords, stride, ptr, numVertices);
1278 glVertexPointer(numCoords, glType, stride, ptr);
1281 public void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum usage)
1284 if(type == GL_DOUBLE)
1285 glesBufferDatad(target, size, (void *)data, usage);
1286 else if(type == GL_UNSIGNED_INT)
1287 glesBufferDatai(target, size, (void *)data, usage);
1292 glBufferData(target, size, data, usage);
1295 #if defined(__WIN32__)
1298 glBufferDataARB(target, size, data, usage);
1302 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1303 static int primitiveTypes[RenderPrimitiveType] =
1305 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1310 // Non OpenGL ES friendly stuff
1314 //#undef GL_UNSIGNED_INT
1319 #undef GL_QUAD_STRIP
1320 #undef GL_POLYGON_STIPPLE
1321 #undef GL_LINE_STIPPLE
1324 #undef GL_ALL_ATTRIB_BITS
1325 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
1329 static int displayWidth, displayHeight;
1331 #define GL_CLAMP_TO_EDGE 0x812F
1333 static bool vboAvailable;
1335 static bool useSingleGLContext = false;
1336 class OGLDisplay : struct
1338 #if defined(__WIN32__)
1348 int imageBuffers[2];
1349 byte * pboMemory1, * pboMemory2;
1351 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1352 GLXContext glContext;
1355 XShmSegmentInfo shminfo;
1357 XShmSegmentInfo shminfoShape;
1358 XImage * shapeImage;
1362 X11Picture windowPicture;
1363 X11Picture pixmapPicture;
1365 X11Picture shapePicture;
1368 ColorAlpha * flippingBuffer;
1369 int flipBufH, flipBufW;
1374 class OGLSystem : struct
1379 #if defined(__WIN32__)
1380 PIXELFORMATDESCRIPTOR pfd;
1385 #elif !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1386 XVisualInfo * visualInfo;
1387 GLXContext glContext;
1388 GLXDrawable glxDrawable;
1392 class OGLSurface : struct
1398 bool writingOutline;
1400 float foreground[4], background[4], bitmapMult[4];
1403 class OGLMesh : struct
1412 class OGLIndices : struct
1422 class OpenGLDisplayDriver : DisplayDriver
1424 class_property(name) = "OpenGL";
1426 bool LockSystem(DisplaySystem displaySystem)
1428 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1429 OGLSystem oglSystem = displaySystem.driverData;
1430 if(useSingleGLContext) return true;
1431 #if defined(__WIN32__)
1432 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1433 #elif defined(__unix__) || defined(__APPLE__)
1434 //if(previous) return true;
1435 // printf("Making SYSTEM current\n");
1436 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
1437 //previous = oglSystem.glContext;
1443 void UnlockSystem(DisplaySystem displaySystem)
1445 if(useSingleGLContext) return;
1446 #if defined(__WIN32__)
1447 wglMakeCurrent(null, null);
1448 #elif defined(__unix__) || defined(__APPLE__)
1449 // printf("Making NULL current\n");
1450 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1452 glXMakeCurrent(xGlobalDisplay, None, null);
1458 bool Lock(Display display)
1460 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1461 OGLDisplay oglDisplay = display.driverData;
1462 if(useSingleGLContext) return true;
1463 #if defined(__WIN32__)
1464 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1465 #elif defined(__unix__) || defined(__APPLE__)
1466 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1467 // printf(" Making DISPLAY current\n");
1468 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1474 void Unlock(Display display)
1476 if(useSingleGLContext) return;
1477 //printf(" Making NULL current\n");
1478 //glXMakeCurrent(xGlobalDisplay, None, null);
1480 LockSystem(display.displaySystem);
1483 void DestroyDisplay(Display display)
1485 OGLDisplay oglDisplay = display.driverData;
1489 #if defined(__WIN32__)
1490 wglMakeCurrent( null, null );
1493 wglDeleteContext(oglDisplay.glrc);
1495 if(oglDisplay.hdc && oglDisplay.pBuffer)
1496 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1498 if(oglDisplay.pBuffer)
1499 wglDestroyPbufferARB(oglDisplay.pBuffer);
1502 ReleaseDC(display.window, oglDisplay.hdc);
1504 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1505 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1507 #elif defined(__unix__) || defined(__APPLE__)
1508 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1510 if(oglDisplay.shapePixmap)
1511 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1512 if(oglDisplay.pixmap)
1513 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1514 if(oglDisplay.image)
1516 if(oglDisplay.shminfoShape.shmid != -1)
1518 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1519 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1520 shmdt(oglDisplay.shminfo.shmaddr);
1521 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1524 if(oglDisplay.shapeImage)
1526 if(oglDisplay.shminfoShape.shmid != -1)
1528 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1529 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1530 shmdt(oglDisplay.shminfoShape.shmaddr);
1531 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1533 XDestroyImage(oglDisplay.shapeImage);
1534 oglDisplay.shapeImage = None;
1537 glXMakeCurrent(xGlobalDisplay, None, null);
1539 if(oglDisplay.glContext)
1540 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1543 delete oglDisplay.flippingBuffer;
1545 display.driverData = null;
1549 void ::CheckExtensions(OGLSystem oglSystem)
1551 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
1553 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
1554 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
1557 bool CreateDisplaySystem(DisplaySystem displaySystem)
1559 bool result = false;
1560 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1563 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1565 oglSystem.hdc = GetDC(oglSystem.hwnd);
1569 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1570 oglSystem.pfd.nVersion = 1;
1571 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1572 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1573 oglSystem.pfd.cColorBits = 24;
1574 oglSystem.pfd.cAlphaBits = 8;
1575 oglSystem.pfd.cDepthBits = 24;
1576 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1578 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1579 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1581 if(oglSystem.pfd.cColorBits > 8)
1583 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1584 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1587 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1589 // Get Pointers To The GL Functions
1590 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1591 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1592 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1593 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1594 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1595 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1596 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1597 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1598 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1599 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1600 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1601 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1603 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1604 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1605 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1606 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1607 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1608 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1609 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1610 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1611 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1613 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1615 vboAvailable = glBindBufferARB != null;
1617 // eSystem_LoggingMode(LOG_MSGBOX, null);
1619 if(wglChoosePixelFormatARB)
1624 float fAttributes[] = {0,0};
1627 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1628 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1629 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1630 WGL_COLOR_BITS_ARB,24,
1631 WGL_ALPHA_BITS_ARB,8,
1632 WGL_DEPTH_BITS_ARB,16,
1633 WGL_STENCIL_BITS_ARB,0,
1634 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1635 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1636 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1640 //Log("Found wglChoosePixelFormatARB\n");
1642 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1643 if(!valid || !numFormats)
1645 //Log("Can't find 4x multi sampling\n");
1646 iAttributes[19] = 2;
1647 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1648 if(!valid || !numFormats)
1650 // Log("Can't find 2x multi sampling\n");
1651 iAttributes[16] = 0;
1652 iAttributes[17] = 0;
1653 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1656 if(valid && numFormats)
1658 oglSystem.format = pixelFormat;
1659 wglMakeCurrent(null, null);
1660 wglDeleteContext(oglSystem.glrc);
1662 // *** DescribePixelFormat does not support WGL pixel formats! ***
1663 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1664 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1665 //Log("Successfully set pixel format\n");
1667 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1668 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1672 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1676 CheckExtensions(oglSystem);
1678 wglMakeCurrent(null, null);
1680 //eSystem_DumpErrors(true);
1684 #elif defined(__unix__) || defined(__APPLE__)
1685 vboAvailable = true;
1686 #if defined(__ANDROID__)
1687 egl_init_display(guiApp.desktop.windowHandle);
1688 CheckExtensions(oglSystem);
1690 #elif defined(__EMSCRIPTEN__)
1691 if(glfwInit() == GL_TRUE)
1693 const int width = 640, height = 480;
1694 if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
1696 //glfwSwapBuffers();
1700 printf("glfwOpenWindow() failed\n"); //glfwTerminate();
1703 printf("glfwInit() failed\n"); //glfwTerminate();
1706 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
1707 XSetWindowAttributes attr;
1712 #ifndef ECERE_MINIGLX
1713 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1716 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1720 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
1721 attr.background_pixel = 0;
1722 attr.border_pixel = 0;
1723 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
1724 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
1725 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1727 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
1728 oglSystem.visualInfo->visual, mask, &attr );
1730 if(oglSystem.visualInfo)
1732 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1733 if(oglSystem.glContext)
1735 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
1737 CheckExtensions(oglSystem);
1738 glXMakeCurrent(xGlobalDisplay, None, null);
1745 displaySystem.flags.alpha = true;
1746 displaySystem.flags.flipping = true;
1747 displaySystem.pixelFormat = pixelFormat888;
1751 void DestroyDisplaySystem(DisplaySystem displaySystem)
1753 OGLSystem oglSystem = displaySystem.driverData;
1755 #if defined(__WIN32__)
1756 wglMakeCurrent( null, null );
1759 wglDeleteContext(oglSystem.glrc);
1762 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1763 DestroyWindow(oglSystem.hwnd);
1765 #elif defined(__unix__) || defined(__APPLE__)
1766 #if defined(__ANDROID__)
1768 #elif defined(__EMSCRIPTEN__)
1771 if(oglSystem.visualInfo)
1773 #ifdef ECERE_MINIGLX
1774 __miniglx_XFree(oglSystem.visualInfo);
1776 XFree(oglSystem.visualInfo);
1780 if(oglSystem.glxDrawable)
1782 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1783 oglSystem.glxDrawable = 0;
1790 bool CreateDisplay(Display display)
1792 bool result = false;
1793 OGLDisplay oglDisplay = display.driverData;
1794 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
1795 OGLSystem oglSystem = display.displaySystem.driverData;
1798 oglDisplay = display.driverData = OGLDisplay { };
1799 //printf("Inside CreateDisplay\n");
1801 #if defined(__WIN32__) || defined(USEPBUFFER)
1802 if(!display.alphaBlend)
1805 #if defined(__WIN32__)
1806 oglDisplay.hdc = GetDC(display.window);
1807 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1808 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1810 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1811 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1815 ReleaseDC(display.window, oglDisplay.hdc);
1816 #elif defined(__unix__) || defined(__APPLE__)
1817 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1819 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1821 #if defined(__APPLE__)
1822 XVisualInfo template = { 0 };
1823 XWindowAttributes winAttr;
1825 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1826 template.visualid = XVisualIDFromVisual(winAttr.visual);
1827 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1829 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1830 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1831 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1832 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1834 // visualInfo = oglSystem.visualInfo;
1839 //printf("visualInfo is not null\n");
1840 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1841 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1842 //XFree(visualInfo);
1845 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1846 if(oglDisplay.glContext)
1848 //printf("CreateDisplay Got a Context\n");
1849 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1855 #if defined(__WIN32__) || defined(USEPBUFFER)
1861 #if defined(__WIN32__)
1862 if(glBlendFuncSeparate)
1863 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1865 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1867 #if !defined(__OLDX__)
1868 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1870 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1875 glMatrixMode(GL_MODELVIEW);
1876 glScaled(1.0, 1.0, -1.0);
1877 // glTranslatef(0.375f, 0.375f, 0.0f);
1878 // glTranslatef(-0.625f, -0.625f, 0.0f);
1879 glMatrixMode(GL_PROJECTION);
1880 glShadeModel(GL_FLAT);
1882 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1883 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1884 glFogi(GL_FOG_MODE, GL_EXP);
1885 glFogf(GL_FOG_DENSITY, 0);
1886 glEnable(GL_NORMALIZE);
1887 glDepthFunc(GL_LESS);
1889 glDisable(GL_MULTISAMPLE_ARB);
1891 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1892 display.ambient = Color { 50,50,50 };
1895 if(!useSingleGLContext)
1897 #if defined(__WIN32__)
1898 wglMakeCurrent(null, null);
1899 #elif defined(__unix__) || defined(__APPLE__)
1900 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
1903 glXMakeCurrent(xGlobalDisplay, None, null);
1911 bool DisplaySize(Display display, int width, int height)
1913 OGLDisplay oglDisplay = display.driverData;
1915 bool result = false;
1917 //printf("Inside DisplaySize\n");
1918 #if defined(__WIN32__) || defined(USEPBUFFER)
1919 OGLSystem oglSystem = display.displaySystem.driverData;
1920 if(display.alphaBlend)
1922 #if defined(__WIN32__)
1923 const int attributes[]=
1925 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1926 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1928 int pixelFormat = 0;
1929 if(wglChoosePixelFormatARB)
1933 float fAttributes[] = {0,0};
1936 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1937 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1938 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1939 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1940 WGL_COLOR_BITS_ARB,24,
1941 WGL_ALPHA_BITS_ARB,8,
1942 WGL_DEPTH_BITS_ARB,16,
1943 WGL_STENCIL_BITS_ARB,0,
1944 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1945 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1946 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1950 //Log("Found wglChoosePixelFormatARB\n");
1952 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1953 if(!valid || !numFormats)
1955 //Log("Can't find 4x multi sampling\n");
1956 iAttributes[19] = 2;
1957 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1958 if(!valid || !numFormats)
1960 // Log("Can't find 2x multi sampling\n");
1961 iAttributes[16] = 0;
1962 iAttributes[17] = 0;
1963 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1964 if(!valid || !numFormats)
1968 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1969 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1970 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1971 WGL_COLOR_BITS_ARB,24,
1972 WGL_ALPHA_BITS_ARB,8,
1973 WGL_DEPTH_BITS_ARB,16,
1976 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1980 if(valid && numFormats)
1982 wglMakeCurrent(null, null);
1986 wglMakeCurrent( null, null );
1987 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1988 if(oglDisplay.hdc && oglDisplay.pBuffer)
1989 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1991 wglDestroyPbufferARB(oglDisplay.pBuffer);
1993 if(!useSingleGLContext)
1994 wglMakeCurrent( null, null );
1997 wglDeleteContext(oglDisplay.glrc);
1999 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
2000 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
2001 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
2004 HDC hdc = GetDC(display.window);
2006 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2007 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2009 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
2010 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
2012 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
2014 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
2018 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
2019 oglDisplay.memDC = CreateCompatibleDC(hdc);
2020 SetMapMode(oglDisplay.memDC, MM_TEXT);
2021 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2022 info->bmiHeader.biPlanes = 1;
2023 info->bmiHeader.biCompression = BI_RGB;
2024 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
2025 info->bmiHeader.biWidth = width;
2026 info->bmiHeader.biHeight = height;
2027 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
2030 SelectObject(oglDisplay.memDC, newBitmap);
2031 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
2034 PIXELFORMATDESCRIPTOR pfd = { 0 };
2035 pfd.nSize = (short)sizeof(pfd);
2037 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
2038 pfd.iPixelType = PFD_TYPE_RGBA;
2039 pfd.cColorBits = 32;
2040 //pfd.cAlphaBits = 8;
2041 pfd.cDepthBits = 24;
2042 pfd.iLayerType = PFD_MAIN_PLANE;
2044 oglDisplay.hdc = oglDisplay.memDC;
2046 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
2047 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
2048 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
2050 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
2051 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
2052 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2057 const int imageSize = width * height * 4;
2059 glGenBuffersARB(2, oglDisplay.imageBuffers);
2061 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2062 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
2063 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2064 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
2067 oglDisplay.memBitmap = newBitmap;
2068 oglDisplay.stride = width;
2074 ReleaseDC(display.window, hdc);
2076 #elif defined(__unix__) || defined(__APPLE__)
2077 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2082 GLX_DOUBLEBUFFER, True,
2088 GLX_STENCIL_SIZE, 1,
2089 //GLX_DEPTH_SIZE, 24,
2090 GLX_RENDER_TYPE, GLX_RGBA_BIT,
2091 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
2097 GLX_PBUFFER_WIDTH, width,
2098 GLX_PBUFFER_HEIGHT, height,
2099 GLX_LARGEST_PBUFFER, False,
2103 // choose a pixel format that meets our minimum requirements
2106 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
2109 if(oglDisplay.pixmap)
2111 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2112 oglDisplay.pixmap = None;
2114 if(oglDisplay.shapePixmap)
2116 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
2117 oglDisplay.shapePixmap = None;
2120 // Free Shared Memory Pixmap
2121 if(oglDisplay.image)
2123 if(oglDisplay.shminfoShape.shmid != -1)
2125 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
2126 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2127 shmdt(oglDisplay.shminfo.shmaddr);
2128 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
2130 XDestroyImage(oglDisplay.image);
2131 oglDisplay.image = None;
2133 if(oglDisplay.shapeImage)
2135 if(oglDisplay.shminfoShape.shmid != -1)
2137 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
2138 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2139 shmdt(oglDisplay.shminfoShape.shmaddr);
2140 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
2142 XDestroyImage(oglDisplay.shapeImage);
2143 oglDisplay.shapeImage = None;
2146 if(oglDisplay.windowPicture)
2147 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
2148 if(oglDisplay.pixmapPicture)
2149 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
2151 if(oglDisplay.pixmap)
2152 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
2154 if(oglDisplay.glContext)
2155 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
2156 if(oglDisplay.pBuffer)
2157 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
2159 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
2160 if(oglDisplay.pBuffer)
2162 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
2163 if(oglDisplay.glContext)
2165 glXMakeCurrent(xGlobalDisplay, None, null);
2166 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2168 // Initialize Shared Memory Pixmap
2169 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
2170 ZPixmap, null, &oglDisplay.shminfo, width, height);
2171 if(oglDisplay.image)
2173 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
2174 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
2175 if(oglDisplay.shminfo.shmid != -1)
2177 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
2178 if(oglDisplay.shminfo.shmaddr != (void *)-1)
2180 oglDisplay.shminfo.readOnly = False;
2181 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
2183 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
2184 &oglDisplay.shminfo, width, height, 32);
2186 // Initialize Shared Memory Shape Pixmap
2187 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
2188 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
2189 if(oglDisplay.shapeImage)
2191 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
2192 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
2193 if(oglDisplay.shminfoShape.shmid != -1)
2195 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
2196 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
2198 oglDisplay.shminfoShape.readOnly = False;
2199 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
2201 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
2202 &oglDisplay.shminfoShape, width, height, 1);
2203 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
2206 XRenderPictureAttributes attributes = { 0 };
2207 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
2208 #if !defined(__APPLE__) && !defined(__OLDX__)
2209 attributes.repeat = RepeatNormal;
2211 attributes.repeat = 1;
2213 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
2214 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
2215 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
2216 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
2219 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
2220 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
2237 CreateDisplay(display);
2238 #if defined(__WIN32__)
2239 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
2240 #elif defined(__unix__) || defined(__APPLE__)
2241 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2245 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
2252 if(!result && display.alphaBlend)
2254 printf("Alpha blending windows not supported on this display\n");
2261 glViewport(0,0,width,height);
2263 glOrtho(0,width,height,0,0.0,1.0);
2264 displayWidth = display.width = width;
2265 displayHeight = display.height = height;
2267 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
2269 oglDisplay.flipBufW = width;
2270 oglDisplay.flipBufH = height;
2274 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
2277 if(oglDisplay.flippingBuffer || !width || !height)
2283 void DisplayPosition(Display display, int x, int y)
2285 OGLDisplay oglDisplay = display.driverData;
2291 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
2295 void RestorePalette(Display display)
2299 void StartUpdate(Display display)
2303 void EndUpdate(Display display)
2307 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
2311 void Update(Display display, Box updateBox)
2313 #if defined(__WIN32__) || defined(USEPBUFFER)
2314 OGLDisplay oglDisplay = display.driverData;
2316 //Logf("DisplayScreen\n");
2320 #if defined(__WIN32__) || defined(USEPBUFFER)
2321 if(display.alphaBlend)
2323 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2324 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
2325 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2326 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2327 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
2330 #if defined(__WIN32__)
2332 POINT point = { oglDisplay.x, oglDisplay.y};
2333 POINT srcPoint = { 0, 0 };
2334 BLENDFUNCTION blend = { 0 };
2336 size.cx = display.width;
2337 size.cy = display.height;
2338 blend.BlendOp = AC_SRC_OVER;
2339 blend.BlendFlags = 0;
2340 blend.SourceConstantAlpha = 255;
2341 blend.AlphaFormat = AC_SRC_ALPHA;
2344 // Process partial images. Mapping the buffer waits for
2345 // outstanding DMA transfers into the buffer to finish.
2346 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2347 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
2349 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2350 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2353 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2354 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2357 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2360 // Unmap the image buffers
2361 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2362 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2364 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2365 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2367 // Bind two different buffer objects and start the glReadPixels
2368 // asynchronously. Each call will return directly after
2369 // starting the DMA transfer.
2370 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2371 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2373 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2374 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2378 #elif defined(__unix__) || defined(__APPLE__)
2379 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__)
2381 XTransform transform =
2384 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2385 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2386 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2389 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2390 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2391 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2392 #if !defined(__APPLE__) && !defined(__OLDX__)
2393 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2395 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2397 XFlush(xGlobalDisplay);
2405 #if defined(__WIN32__)
2406 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2407 SwapBuffers(oglDisplay.hdc);
2408 #elif defined(__unix__) || defined(__APPLE__)
2409 #if defined(__ANDROID__)
2410 eglSwapBuffers(eglDisplay, eglSurface);
2411 #elif defined(__EMSCRIPTEN__)
2414 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
2418 //Logf("Out of DisplayScreen\n");
2421 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2423 if(bitmap.driverData)
2425 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
2426 glDeleteTextures(1, &tex);
2427 bitmap.driverData = 0;
2429 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2432 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2434 OGLSystem oglSystem = displaySystem.driverData;
2435 bool result = false;
2437 GLuint glBitmap = 0;
2439 uint w = width, h = height;
2440 if(oglSystem.pow2textures)
2445 w = Min(w, oglSystem.maxTextureSize);
2446 h = Min(h, oglSystem.maxTextureSize);
2448 glGenTextures(1, &glBitmap);
2449 glBindTexture(GL_TEXTURE_2D, glBitmap);
2451 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2453 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2454 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2459 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2461 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2463 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2464 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2468 bitmap.driverData = (void *)(uintptr)glBitmap;
2469 bitmap.driver = displaySystem.driver;
2477 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2479 bool result = false;
2480 OGLSystem oglSystem = displaySystem.driverData;
2481 Bitmap convBitmap = bitmap;
2485 convBitmap.Copy(bitmap);
2488 // Pre process the bitmap... First make it 32 bit
2489 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
2492 uint w = bitmap.width, h = bitmap.height;
2493 GLuint glBitmap = 0;
2494 if(oglSystem.pow2textures)
2499 w = Min(w, oglSystem.maxTextureSize);
2500 h = Min(h, oglSystem.maxTextureSize);
2504 while(w * 2 < h) w *= 2;
2505 while(h * 2 < w) h *= 2;
2508 // Switch ARGB to RGBA
2509 //if(bitmap.format != pixelFormatRGBA)
2511 for(c=0; c<bitmap.size; c++)
2513 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2515 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
2516 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2519 // convBitmap.pixelFormat = pixelFormat888;
2522 glGenTextures(1, &glBitmap);
2525 //int error = glGetError();
2529 glBindTexture(GL_TEXTURE_2D, glBitmap);
2530 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2532 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2533 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2534 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2536 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2537 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2539 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2540 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2542 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2546 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2549 if(bitmap.width != w || bitmap.height != h)
2551 mipMap = Bitmap { };
2552 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
2554 Surface mipSurface = mipMap.GetSurface(0,0,null);
2555 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
2565 mipMap = convBitmap;
2572 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2573 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2574 //printf("Calling glTexImage2D\n");
2575 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2576 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2577 if((error = glGetError()))
2579 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2580 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2584 if(mipMap != convBitmap)
2589 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
2590 bitmap.driverData = (void *)(uintptr)glBitmap;
2591 bitmap.driver = displaySystem.driver;
2594 FreeBitmap(displaySystem, bitmap);
2595 else if(oglSystem.loadingFont)
2597 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2598 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2599 oglSystem.loadingFont = false;
2605 void ReleaseSurface(Display display, Surface surface)
2607 glDisable(GL_SCISSOR_TEST);
2608 delete surface.driverData;
2609 surface.driverData = null;
2612 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2617 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2619 bool result = false;
2620 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2622 //Logf("GetSurface\n");
2626 if(displayWidth != display.width || displayHeight != display.height)
2628 displayWidth = display.width;
2629 displayHeight = display.height;
2631 glViewport(0,0,display.width,display.height);
2633 glOrtho(0,display.width,display.height,0,0.0,1.0);
2636 surface.offset.x = x;
2637 surface.offset.y = y;
2638 surface.unclippedBox = surface.box = clip;
2639 oglSurface.bitmapMult[0] = 1;
2640 oglSurface.bitmapMult[1] = 1;
2641 oglSurface.bitmapMult[2] = 1;
2642 oglSurface.bitmapMult[3] = 1;
2644 glEnable(GL_SCISSOR_TEST);
2647 (display.height) -(y+clip.bottom)-1,
2648 clip.right-clip.left+1,
2649 clip.bottom-clip.top+1);
2655 void Clip(Display display, Surface surface, Box clip)
2664 box.Clip(surface.unclippedBox);
2668 box = surface.box = surface.unclippedBox;
2669 box.left += surface.offset.x;
2670 box.top += surface.offset.y;
2671 box.right+= surface.offset.x;
2672 box.bottom += surface.offset.y;
2675 box.left,display.height - box.bottom - 1,
2676 box.right-box.left+1, box.bottom-box.top+1);
2679 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2681 bool result = false;
2682 OGLDisplay oglDisplay = display.driverData;
2683 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2685 if(oglDisplay.flippingBuffer)
2687 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2690 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2696 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2697 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2698 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2699 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2700 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2703 for(row = 0; row<h; row++)
2704 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2711 void SetForeground(Display display, Surface surface, ColorAlpha color)
2713 OGLSurface oglSurface = surface.driverData;
2715 //Logf("SetForeground\n");
2717 oglSurface.foreground[0] = color.color.r/255.0f;
2718 oglSurface.foreground[1] = color.color.g/255.0f;
2719 oglSurface.foreground[2] = color.color.b/255.0f;
2720 //oglSurface.foreground[3] = 1.0f;
2721 oglSurface.foreground[3] = color.a/255.0f;
2723 //if(!oglSurface.foreground[3])printf("bug");
2726 void SetBackground(Display display, Surface surface, ColorAlpha color)
2728 OGLSurface oglSurface = surface.driverData;
2730 //Logf("SetBackground\n");
2732 oglSurface.background[0] = color.color.r/255.0f;
2733 oglSurface.background[1] = color.color.g/255.0f;
2734 oglSurface.background[2] = color.color.b/255.0f;
2735 //oglSurface.background[3] = 1.0;
2736 oglSurface.background[3] = color.a/255.0f;
2739 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2741 OGLSurface oglSurface = surface.driverData;
2743 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2744 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2745 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2746 oglSurface.bitmapMult[3] = color.a/255.0f;
2749 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2754 void PutPixel(Display display, Surface surface,int x,int y)
2756 OGLSurface oglSurface = surface.driverData;
2758 //Logf("PutPixel\n");
2760 glColor4fv(oglSurface.foreground);
2762 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2763 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2768 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2770 OGLSurface oglSurface = surface.driverData;
2771 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2786 x1 += surface.offset.x;
2787 y1 += surface.offset.y;
2788 x2 += surface.offset.x;
2789 y2 += surface.offset.y;
2793 glColor4fv(oglSurface.foreground);
2798 glTexCoord2f(0.5f, 0);
2799 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2800 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2801 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2810 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2811 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2817 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2819 OGLSurface oglSurface = surface.driverData;
2820 x1 += surface.offset.x;
2821 y1 += surface.offset.y;
2822 x2 += surface.offset.x;
2823 y2 += surface.offset.y;
2825 //Logf("Rectangle\n");
2827 glColor4fv(oglSurface.foreground);
2833 glTexCoord2f(0.5f, 0);
2834 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2835 glTexCoord2f(y2-y1 + 0.5f, 0);
2836 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2838 glTexCoord2f(0.5f, 0);
2839 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2840 glTexCoord2f(x2 - x1 + 0.5f, 0);
2841 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2843 glTexCoord2f(0.5f, 0);
2844 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2845 glTexCoord2f(y1 - y2 + 0.5f, 0);
2846 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2848 glTexCoord2f(0.5f, 0);
2849 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2850 glTexCoord2f(x1 - x2 + 0.5f, 0);
2851 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2856 glBegin(GL_LINE_LOOP);
2863 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2864 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2865 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2866 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2871 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2873 OGLSurface oglSurface = surface.driverData;
2876 glColor4fv(oglSurface.background);
2878 #ifdef __EMSCRIPTEN__
2880 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
2881 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
2882 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
2883 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
2886 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2887 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2890 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2891 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2895 void Clear(Display display, Surface surface, ClearType type)
2897 OGLDisplay oglDisplay = display.driverData;
2898 OGLSurface oglSurface = surface.driverData;
2901 if(type != depthBuffer)
2902 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2903 if(type != colorBuffer && !oglDisplay.depthWrite)
2905 glDepthMask((byte)bool::true);
2907 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2908 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2909 if(type != colorBuffer && !oglDisplay.depthWrite)
2911 glDepthMask((byte)bool::false);
2915 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2920 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2922 OGLSurface oglSurface = surface.driverData;
2924 #if !defined(__OLDX__)
2925 // WHY DO WE HAVE GL_ONE HERE ?
2926 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2927 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2930 if(!oglSurface.writingText)
2932 // glTranslatef(-0.375f, -0.375f, 0.0f);
2933 glEnable(GL_TEXTURE_2D);
2934 glColor4fv(oglSurface.bitmapMult);
2936 else if(oglSurface.xOffset)
2937 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2939 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2944 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2945 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2946 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2947 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2948 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2949 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2950 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2951 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2956 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2957 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2958 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2959 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2960 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2961 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2962 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2963 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2966 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2967 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2968 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2969 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2970 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2971 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2972 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2973 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2977 if(!oglSurface.writingText)
2979 glDisable(GL_TEXTURE_2D);
2981 //glTranslate(0.375, 0.375, 0.0);
2983 else if(oglSurface.xOffset)
2984 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2986 #if !defined(__OLDX__)
2987 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2988 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2992 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2994 OGLSurface oglSurface = surface.driverData;
2996 //glTranslate(-0.375, -0.375, 0.0);
2998 //Logf("Stretch\n");
3000 #if !defined(__OLDX__)
3001 /*if(glBlendFuncSeparate)
3002 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3005 glEnable(GL_TEXTURE_2D);
3006 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
3008 glColor4fv(oglSurface.bitmapMult);
3014 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3015 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3017 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3018 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3020 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3021 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
3023 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3024 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
3028 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
3029 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
3031 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
3032 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
3034 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
3035 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
3037 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
3038 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
3043 glDisable(GL_TEXTURE_2D);
3045 //glTranslate(0.375, 0.375, 0.0);
3046 #if !defined(__OLDX__)
3047 /*if(glBlendFuncSeparate)
3048 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
3053 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3055 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3058 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3060 float s2dw,s2dh,d2sw,d2sh;
3061 //bool flipX = false, flipY = false;
3063 //Logf("StretchDI\n");
3065 if(Sgn(w) != Sgn(sw))
3071 if(Sgn(h) != Sgn(sh))
3083 //Clip against the edges of the source
3086 dx+=(int)((0-sx) * s2dw);
3087 w-=(int)((0-sx) * s2dw);
3093 dy+=(int)((0-sy) * s2dh);
3094 h-=(int)((0-sy) * s2dh);
3099 if(sx+sw>bitmap.width-1)
3101 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
3102 sw-=sx+sw-(bitmap.width-1)-1;
3104 if(sy+sh>(bitmap.height-1))
3106 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
3107 sh-=sy+sh-(bitmap.height-1)-1;
3109 //Clip against the edges of the surfaceination
3110 if(dx<surface.box.left)
3113 sx+=(int)((surface.box.left-dx)*d2sw);
3114 sw-=(int)((surface.box.left-dx)*d2sw);
3115 w-=surface.box.left-dx;
3116 dx=surface.box.left;
3118 if(dy<surface.box.top)
3120 sy+=(int)((surface.box.top-dy)*d2sh);
3121 sh-=(int)((surface.box.top-dy)*d2sh);
3122 h-=surface.box.top-dy;
3125 if(dx+w>surface.box.right)
3127 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
3128 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
3129 w-=dx+w-surface.box.right-1;
3131 if(dy+h>surface.box.bottom)
3133 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
3134 h-=dy+h-surface.box.bottom-1;
3136 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
3138 dx += surface.offset.x;
3139 dy += surface.offset.y;
3141 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3143 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3144 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3145 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3146 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3147 glRasterPos2d(dx,dy);
3148 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
3149 glPixelZoom(s2dw, -s2dh);
3150 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3151 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3152 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3153 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3154 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3158 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
3162 //Clip against the edges of the source
3175 if(sx+w>bitmap.width-1)
3176 w-=sx+w-(bitmap.width-1)-1;
3177 if(sy+h>bitmap.height-1)
3178 h-=sy+h-(bitmap.height-1)-1;
3179 //Clip against the edges of the surfaceination
3180 if(dx<surface.box.left)
3183 sx+=surface.box.left-dx;
3184 w-=surface.box.left-dx;
3185 dx=surface.box.left;
3187 if(dy<surface.box.top)
3189 sy+=surface.box.top-dy;
3190 h-=surface.box.top-dy;
3193 if(dx+w>surface.box.right)
3195 //if(flip) sx+=dx+w-surface.box.right-1;
3196 w-=dx+w-surface.box.right-1;
3198 if(dy+h>surface.box.bottom)
3199 h-=dy+h-surface.box.bottom-1;
3203 dx += surface.offset.x;
3204 dy += surface.offset.y;
3206 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
3208 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
3209 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
3210 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
3211 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
3212 glRasterPos2d(dx,dy);
3214 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
3215 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
3216 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3217 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
3218 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
3222 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
3224 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
3227 void UnloadFont(DisplaySystem displaySystem, Font font)
3229 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
3232 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
3235 OGLSystem oglSystem = displaySystem.driverData;
3236 oglSystem.loadingFont = true;
3237 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
3241 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
3243 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
3246 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
3248 OGLSurface oglSurface = surface.driverData;
3249 OGLSystem oglSystem = display.displaySystem.driverData;
3250 oglSystem.loadingFont = true;
3252 //glTranslated(-0.375, -0.375, 0.0);
3256 if(surface.textOpacity)
3259 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
3260 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
3263 oglSurface.writingText = true;
3265 glEnable(GL_TEXTURE_2D);
3267 if(surface.outline.size)
3269 ColorAlpha outlineColor = surface.outline.color;
3270 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
3271 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3272 //glEnable(GL_BLEND);
3274 oglSurface.writingOutline = true;
3275 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3276 oglSurface.writingOutline = false;
3278 glColor4fv(oglSurface.foreground);
3280 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
3281 oglSurface.writingText = false;
3282 oglSystem.loadingFont = false;
3284 glDisable(GL_TEXTURE_2D);
3286 //glTranslated(0.375, 0.375, 0.0);
3289 void TextFont(Display display, Surface surface, Font font)
3291 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
3294 void TextOpacity(Display display, Surface surface, bool opaque)
3296 OGLSurface oglSurface = surface.driverData;
3297 oglSurface.opaqueText = opaque;
3300 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
3302 OGLSurface oglSurface = surface.driverData;
3303 OGLSystem oglSystem = display.displaySystem.driverData;
3304 oglSystem.loadingFont = true;
3305 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
3306 oglSystem.loadingFont = false;
3309 void DrawingChar(Display display, Surface surface, char character)
3314 void LineStipple(Display display, Surface surface, uint32 stipple)
3316 //Logf("Stipple\n");
3321 stippleEnabled = true;
3322 glesLineStipple(1, (uint16)stipple);
3324 glLineStipple(1, (uint16)stipple);
3325 glEnable(GL_LINE_STIPPLE);
3331 stippleEnabled = false;
3332 glMatrixMode(GL_TEXTURE);
3334 glMatrixMode(GL_PROJECTION);
3335 glDisable(GL_TEXTURE_2D);
3337 glDisable(GL_LINE_STIPPLE);
3341 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
3342 void SetRenderState(Display display, RenderState state, uint value)
3344 OGLDisplay oglDisplay = display.driverData;
3345 //Logf("RenderState\n");
3351 glEnable(GL_MULTISAMPLE_ARB);
3353 glDisable(GL_MULTISAMPLE_ARB);
3357 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
3361 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
3364 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
3365 oglDisplay.depthWrite = (bool)value;
3369 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3370 glFogfv(GL_FOG_COLOR, (float *)&color);
3374 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
3377 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
3381 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
3382 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
3387 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
3392 #if defined(__WIN32__)
3393 wglSwapIntervalEXT(value ? 1 : 0);
3400 void SetLight(Display display, int id, Light light)
3402 //Logf("SetLight\n");
3406 Object lightObject = light.lightObject;
3407 float position[4] = { 0, 0, 0, 0 };
3408 float color[4] = { 0, 0, 0, 1 };
3410 glEnable(GL_LIGHT0 + id);
3412 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
3413 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
3414 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
3417 if(!light.multiplier) light.multiplier = 1.0f;
3419 color[0] = light.diffuse.r * light.multiplier;
3420 color[1] = light.diffuse.g * light.multiplier;
3421 color[2] = light.diffuse.b * light.multiplier;
3422 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3424 color[0] = light.ambient.r * light.multiplier;
3425 color[1] = light.ambient.g * light.multiplier;
3426 color[2] = light.ambient.b * light.multiplier;
3427 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3428 color[0] = light.specular.r * light.multiplier;
3429 color[1] = light.specular.g * light.multiplier;
3430 color[2] = light.specular.b * light.multiplier;
3431 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3435 Vector3D positionVector;
3436 if(light.flags.spot)
3438 if(lightObject.flags.root || !lightObject.parent)
3440 positionVector = lightObject.transform.position;
3441 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3445 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3446 if(display.display3D.camera)
3447 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3453 if(!light.direction.x && !light.direction.y && !light.direction.z)
3455 Vector3Df vector { 0,0,-1 };
3457 mat.RotationQuaternion(light.orientation);
3458 positionVector.MultMatrixf(vector, mat);
3462 positionVector = light.direction;
3467 position[0] = (float)positionVector.x;
3468 position[1] = (float)positionVector.y;
3469 position[2] = (float)positionVector.z;
3471 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3474 // Display Light Position
3475 glDisable(GL_LIGHTING);
3476 glDisable(GL_DEPTH_TEST);
3480 glVertex3fv(position);
3482 glEnable(GL_DEPTH_TEST);
3483 glEnable(GL_LIGHTING);
3487 if(lightObject.flags.root || !lightObject.parent)
3489 positionVector = light.target.transform.position;
3490 positionVector.Subtract(positionVector, display.camera.cPosition);
3494 positionVector.MultMatrix(light.target.transform.position,
3495 lightObject.light.target.parent.matrix);
3496 positionVector.Subtract(positionVector, display.camera.cPosition);
3499 position[0] = positionVector.x;
3500 position[1] = positionVector.y;
3501 position[2] = positionVector.z;
3503 glDisable(GL_LIGHTING);
3504 glDisable(GL_DEPTH_TEST);
3508 glVertex3fv(position);
3510 glEnable(GL_DEPTH_TEST);
3511 glEnable(GL_LIGHTING);
3514 if(light.flags.attenuation)
3516 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3517 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3518 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3521 if(light.flags.spot)
3524 #define MAXLIGHT 0.9
3525 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3526 // Figure out exponent out of the hot spot
3527 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3529 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3530 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3531 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3537 Vector3Df vector { 0,0,-1 };
3538 Vector3Df direction;
3541 mat.RotationQuaternion(light.orientation);
3542 direction.MultMatrix(vector, mat);
3544 position[0] = direction.x;
3545 position[1] = direction.y;
3546 position[2] = direction.z;
3548 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3552 glDisable(GL_LIGHT0 + id);
3555 void SetCamera(Display display, Surface surface, Camera camera)
3557 OGLDisplay oglDisplay = display.driverData;
3558 //Logf("SetCamera\n");
3562 int left = surface.box.left + surface.offset.x;
3563 int top = surface.box.top + surface.offset.y;
3564 int right = surface.box.right + surface.offset.x;
3565 int bottom = surface.box.bottom + surface.offset.y;
3566 float origX = surface.offset.x + camera.origin.x;
3567 float origY = surface.offset.y + camera.origin.y;
3569 int y = display.height - bottom - 1;
3570 int w = right - left + 1;
3571 int h = bottom - top + 1;
3574 glViewport(x, y, w, h);
3576 // *** Projection Matrix ***
3577 if(!display.display3D.camera)
3580 glMatrixMode(GL_PROJECTION);
3581 if(display.display3D.collectingHits)
3583 float pickX = display.display3D.pickX + surface.offset.x;
3584 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3588 w / display.display3D.pickWidth, 0, 0, 0,
3589 0, h / display.display3D.pickHeight, 0, 0,
3591 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3592 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3595 glLoadMatrixd(pickMatrix.array);
3600 (left - origX) * camera.zMin / camera.focalX,
3601 (right - origX) * camera.zMin / camera.focalX,
3602 (bottom - origY) * camera.zMin / camera.focalY,
3603 (top - origY) * camera.zMin / camera.focalY,
3604 camera.zMin, camera.zMax);
3606 glDisable(GL_BLEND);
3608 // *** Z Inverted Identity Matrix ***
3609 glMatrixMode(GL_MODELVIEW);
3610 if(!display.display3D.camera)
3615 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3617 // *** View Matrix ***
3618 glMultMatrixd(camera.viewMatrix.array);
3623 glEnable(GL_DEPTH_TEST);
3624 glEnable(GL_LIGHTING);
3625 glShadeModel(GL_SMOOTH);
3626 glDepthMask((byte)bool::true);
3627 oglDisplay.depthWrite = true;
3629 glEnable(GL_MULTISAMPLE_ARB);
3631 else if(display.display3D.camera)
3633 oglDisplay.depthWrite = false;
3634 glViewport(0,0,display.width,display.height);
3636 glDisable(GL_CULL_FACE);
3637 glDisable(GL_DEPTH_TEST);
3638 glDisable(GL_LIGHTING);
3640 glDisable(GL_TEXTURE_2D);
3641 glShadeModel(GL_FLAT);
3643 glDisable(GL_MULTISAMPLE_ARB);
3645 // *** Restore 2D MODELVIEW Matrix ***
3648 // *** Restore 2D PROJECTION Matrix ***
3649 glMatrixMode(GL_PROJECTION);
3653 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
3656 void ApplyMaterial(Display display, Material material, Mesh mesh)
3658 //Logf("ApplyMaterial\n");
3661 if(material.flags.doubleSided)
3663 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3664 glDisable(GL_CULL_FACE);
3668 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3669 glEnable(GL_CULL_FACE);
3673 if(material.flags.noFog)
3679 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
3681 Bitmap map = material.baseMap;
3682 glEnable(GL_TEXTURE_2D);
3683 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
3685 glMatrixMode(GL_TEXTURE);
3687 if(material.uScale && material.vScale)
3688 glScalef(material.uScale, material.vScale, 1);
3689 glMatrixMode(GL_MODELVIEW);
3691 if(material.flags.tile)
3693 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3694 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3698 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3699 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3703 glDisable(GL_TEXTURE_2D);
3705 if(mesh.flags.colors)
3707 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3708 glEnable(GL_COLOR_MATERIAL);
3712 glDisable(GL_COLOR_MATERIAL);
3714 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3715 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3718 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3719 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3723 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3724 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3727 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3728 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3731 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3734 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3736 OGLMesh oglMesh = mesh.data;
3739 if(!mesh.flags.vertices)
3741 if(oglMesh.vertices)
3743 GLDeleteBuffers(1, &oglMesh.vertices);
3744 oglMesh.vertices = 0;
3746 delete mesh.vertices;
3748 if(!mesh.flags.normals)
3752 GLDeleteBuffers(1, &oglMesh.normals);
3753 oglMesh.normals = 0;
3755 delete mesh.normals;
3757 if(!mesh.flags.texCoords1)
3759 if(oglMesh.texCoords)
3761 GLDeleteBuffers(1, &oglMesh.texCoords);
3762 oglMesh.texCoords = 0;
3764 delete mesh.texCoords;
3766 if(!mesh.flags.texCoords2)
3768 if(oglMesh.texCoords2)
3770 GLDeleteBuffers(1, &oglMesh.texCoords2);
3771 oglMesh.texCoords2 = 0;
3774 delete mesh.texCoords2;
3777 if(!mesh.flags.colors)
3781 GLDeleteBuffers(1, &oglMesh.colors);
3793 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3795 bool result = false;
3798 mesh.data = OGLMesh { };
3801 OGLMesh oglMesh = mesh.data;
3802 if(mesh.nVertices == nVertices)
3804 // Same number of vertices, adding features (Leaves the other features pointers alone)
3805 if(mesh.flags != flags)
3807 if(!mesh.flags.vertices && flags.vertices)
3809 if(flags.doubleVertices)
3811 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3814 mesh.vertices = new Vector3Df[nVertices];
3815 if(!oglMesh.vertices)
3816 GLGenBuffers(1, &oglMesh.vertices);
3818 if(!mesh.flags.normals && flags.normals)
3820 if(flags.doubleNormals)
3822 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3825 mesh.normals = new Vector3Df[nVertices];
3826 if(!oglMesh.normals)
3827 GLGenBuffers( 1, &oglMesh.normals);
3829 if(!mesh.flags.texCoords1 && flags.texCoords1)
3831 mesh.texCoords = new Pointf[nVertices];
3832 if(!oglMesh.texCoords)
3833 GLGenBuffers( 1, &oglMesh.texCoords);
3835 if(!mesh.flags.colors && flags.colors)
3837 mesh.colors = new ColorRGBAf[nVertices];
3839 GLGenBuffers( 1, &oglMesh.colors);
3845 // New number of vertices, reallocate all current and new features
3846 flags |= mesh.flags;
3849 if(flags.doubleVertices)
3851 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3854 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3855 if(!oglMesh.vertices)
3856 GLGenBuffers(1, &oglMesh.vertices);
3860 if(flags.doubleNormals)
3862 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3865 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3866 if(!oglMesh.normals)
3867 GLGenBuffers( 1, &oglMesh.normals);
3869 if(flags.texCoords1)
3871 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3872 if(!oglMesh.texCoords)
3873 GLGenBuffers( 1, &oglMesh.texCoords);
3877 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3879 GLGenBuffers( 1, &oglMesh.colors);
3887 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3889 OGLMesh oglMesh = mesh.data;
3890 if(!flags) flags = mesh.flags;
3894 if(flags.vertices && oglMesh.vertices)
3896 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3897 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 );
3900 if(flags.normals && oglMesh.normals)
3902 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3903 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 );
3906 if(flags.texCoords1 && oglMesh.texCoords)
3908 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3909 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3912 if(flags.colors && oglMesh.colors)
3914 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3915 GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3918 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
3922 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3929 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3933 if(oglIndices.buffer)
3934 GLDeleteBuffers(1, &oglIndices.buffer);
3935 delete oglIndices.indices;
3940 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3942 OGLIndices oglIndices = OGLIndices { };
3945 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3946 GLGenBuffers( 1, &oglIndices.buffer);
3947 oglIndices.nIndices = nIndices;
3952 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3956 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3957 GLBufferData( indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3958 oglIndices.indices, GL_STATIC_DRAW_ARB);
3959 GLBindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3963 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3966 return oglIndices.indices;
3969 void SelectMesh(Display display, Mesh mesh)
3971 //Logf("SelectMesh\n");
3973 #if !defined( __ANDROID__) && !defined(__APPLE__)
3975 #if defined(__WIN32__)
3976 if(glUnlockArraysEXT)
3978 if(!vboAvailable && display.display3D.mesh)
3979 glUnlockArraysEXT();
3984 OGLMesh oglMesh = mesh.data;
3986 // *** Vertex Stream ***
3987 glEnableClientState(GL_VERTEX_ARRAY);
3988 if(!display.display3D.collectingHits && oglMesh)
3990 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3991 if(mesh.flags.doubleVertices)
3992 glVertexPointerd(3, 0, (double *)(vboAvailable ? null : mesh.vertices), mesh.nVertices);
3994 glVertexPointer(3, GL_FLOAT, 0, vboAvailable ? null : mesh.vertices);
3996 // *** Normals Stream ***
3997 if(mesh.normals || mesh.flags.normals)
3999 glEnableClientState(GL_NORMAL_ARRAY);
4000 GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
4001 glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, vboAvailable ? null : mesh.normals);
4004 glDisableClientState(GL_NORMAL_ARRAY);
4006 // *** Texture Coordinates Stream ***
4007 if(mesh.texCoords || mesh.flags.texCoords1)
4009 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4010 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
4011 glTexCoordPointer(2, GL_FLOAT, 0, vboAvailable ? null : mesh.texCoords);
4014 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4016 // *** Color Stream ***
4017 if(mesh.colors || mesh.flags.colors)
4019 glEnableClientState(GL_COLOR_ARRAY);
4020 GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
4021 glColorPointer(4, GL_FLOAT, 0, vboAvailable ? null : mesh.colors);
4024 glDisableClientState(GL_COLOR_ARRAY);
4029 GLBindBuffer( GL_ARRAY_BUFFER_ARB, 0);
4030 if(mesh.flags.doubleVertices)
4031 glVertexPointerd(3, 0, (double *)mesh.vertices, mesh.nVertices);
4033 glVertexPointer(3, GL_FLOAT, 0, mesh.vertices);
4034 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
4036 glEnableClientState(GL_NORMAL_ARRAY);
4037 glNormalPointer(/*mesh.flags.doubleNormals ? GL_DOUBLE : */GL_FLOAT, 0, mesh.normals);
4040 glDisableClientState(GL_NORMAL_ARRAY);
4041 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
4043 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4044 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
4047 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4048 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
4050 glEnableClientState(GL_COLOR_ARRAY);
4051 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
4054 glDisableClientState(GL_COLOR_ARRAY);
4057 #if !defined(__ANDROID__) && !defined(__APPLE__)
4059 #if defined(__WIN32__)
4063 glLockArraysEXT(0, mesh.nVertices);
4068 GLBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
4071 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
4073 //Logf("DrawPrimitives\n");
4075 if(primitive->type.vertexRange)
4076 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
4079 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
4080 // HACK TO SPEED THINGS UP...
4082 /*GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4083 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
4086 glBegin(primitiveTypes[primitive->type.primitiveType]);
4089 OGLIndices oglIndices = primitive->data;
4090 MeshFeatures flags = mesh.flags;
4091 for(c = 0; c<primitive->nIndices; c++)
4093 uint16 index = ((uint16 *) oglIndices.indices)[c];
4094 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
4095 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
4096 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
4097 glVertex3fv((float *)&mesh.vertices[index]);
4105 OGLIndices oglIndices = primitive->data;
4107 if(!display.display3D.collectingHits && vboAvailable && oglIndices)
4109 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
4110 if(primitive->type.indices32bit)
4111 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, 0);
4113 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices, GL_UNSIGNED_SHORT, 0);
4114 GLBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
4118 if(primitive->type.indices32bit)
4119 glDrawElementsi(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4120 oglIndices ? oglIndices.indices : primitive->indices);
4122 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
4123 GL_UNSIGNED_SHORT, oglIndices ? oglIndices.indices : primitive->indices);
4129 void PushMatrix(Display display)
4134 void PopMatrix(Display display, bool setMatrix)
4139 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
4141 Matrix matrix = transMatrix;
4142 Camera camera = useCamera ? display.display3D.camera : null;
4147 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
4152 matrix.m[3][0] - camera.cPosition.x,
4153 matrix.m[3][1] - camera.cPosition.y,
4154 matrix.m[3][2] - camera.cPosition.z);
4166 glMultMatrixd(matrix.array);
4171 public void UseSingleGLContext(bool useSingle)
4173 useSingleGLContext = useSingle;
4176 default dllexport void *
4177 #if defined(__WIN32__)
4178 __attribute__((stdcall))
4180 IS_GLGetContext(DisplaySystem displaySystem)
4184 #if defined(__WIN32__)
4185 OGLSystem system = displaySystem.driverData;
4187 #elif defined(__ANDROID__)
4189 #elif defined(__EMSCRIPTEN__)
4191 OGLSystem system = displaySystem.driverData;
4192 return system.glContext;