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;
9 #if !defined(__MINGW32__)
10 #define GL_GLEXT_PROTOTYPES
15 //#include <GL/miniglx.h>
19 #if defined(__ANDROID__)
23 #define property _property
27 #define Window X11Window
28 #define Cursor X11Cursor
30 #define Display X11Display
32 #define KeyCode X11KeyCode
33 #define Picture X11Picture
37 #include <X11/Xutil.h>
39 #include <X11/extensions/XShm.h>
42 #include <X11/extensions/Xrender.h>
43 #include <X11/extensions/shape.h>
63 #if defined(__APPLE__)
64 #include <OpenGl/gl.h>
67 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
69 #if defined(__WIN32__)
70 #define WIN32_LEAN_AND_MEAN
71 #define _WIN32_WINNT 0x0500
75 #if defined(__ANDROID__)
97 #define glLoadMatrix glLoadMatrixd
98 #define glMultMatrix glMultMatrixd
99 #define glGetMatrix glGetDoublev
100 #define glTranslate glTranslated
101 #define glScale glScaled
104 #define glVertex3v glVertex3dv
105 #define glNormal3v glNormal3dv
109 //#ifdef VERTEX_FORMAT_DOUBLE
111 #define glLoadMatrix glLoadMatrixd
112 #define glMultMatrix glMultMatrixd
113 #define glGetMatrix glGetDoublev
114 #define glVertex3v glVertex3dv
115 #define glNormal3v glNormal3dv
116 #define glTranslate glTranslated
117 #define glScale glScaled
118 //#define GL_VERTEX_FORMAT GL_DOUBLE
122 #define glLoadMatrix glLoadMatrixf
123 #define glMultMatrix glMultMatrixf
124 #define glGetMatrix glGetFloatv
125 #define glVertex3v glVertex3fv
126 #define glNormal3v glNormal3fv
127 #define glTranslate glTranslatef
128 #define glScale glScalef
129 //#define GL_VERTEX_FORMAT GL_FLOAT
134 #define GL_ARRAY_BUFFER_ARB 0x8892
135 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
136 #define GL_STATIC_DRAW_ARB 0x88E4
137 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
138 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
140 #define GL_MULTISAMPLE_ARB 0x809D
142 #if defined(__WIN32__)
144 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
145 #define WGL_SAMPLES_ARB 0x2042
147 #define WGL_WGLEXT_VERSION 1
148 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
149 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
150 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
151 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
152 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
153 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
154 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
155 #define WGL_ACCELERATION_ARB 0x2003
156 #define WGL_NEED_PALETTE_ARB 0x2004
157 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
158 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
159 #define WGL_SWAP_METHOD_ARB 0x2007
160 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
161 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
162 #define WGL_TRANSPARENT_ARB 0x200A
163 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
164 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
165 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
166 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
167 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
168 #define WGL_SHARE_DEPTH_ARB 0x200C
169 #define WGL_SHARE_STENCIL_ARB 0x200D
170 #define WGL_SHARE_ACCUM_ARB 0x200E
171 #define WGL_SUPPORT_GDI_ARB 0x200F
172 #define WGL_SUPPORT_OPENGL_ARB 0x2010
173 #define WGL_DOUBLE_BUFFER_ARB 0x2011
174 #define WGL_STEREO_ARB 0x2012
175 #define WGL_PIXEL_TYPE_ARB 0x2013
176 #define WGL_COLOR_BITS_ARB 0x2014
177 #define WGL_RED_BITS_ARB 0x2015
178 #define WGL_RED_SHIFT_ARB 0x2016
179 #define WGL_GREEN_BITS_ARB 0x2017
180 #define WGL_GREEN_SHIFT_ARB 0x2018
181 #define WGL_BLUE_BITS_ARB 0x2019
182 #define WGL_BLUE_SHIFT_ARB 0x201A
183 #define WGL_ALPHA_BITS_ARB 0x201B
184 #define WGL_ALPHA_SHIFT_ARB 0x201C
185 #define WGL_ACCUM_BITS_ARB 0x201D
186 #define WGL_ACCUM_RED_BITS_ARB 0x201E
187 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
188 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
189 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
190 #define WGL_DEPTH_BITS_ARB 0x2022
191 #define WGL_STENCIL_BITS_ARB 0x2023
192 #define WGL_AUX_BUFFERS_ARB 0x2024
193 #define WGL_NO_ACCELERATION_ARB 0x2025
194 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
195 #define WGL_FULL_ACCELERATION_ARB 0x2027
196 #define WGL_SWAP_EXCHANGE_ARB 0x2028
197 #define WGL_SWAP_COPY_ARB 0x2029
198 #define WGL_SWAP_UNDEFINED_ARB 0x202A
199 #define WGL_TYPE_RGBA_ARB 0x202B
200 #define WGL_TYPE_COLORINDEX_ARB 0x202C
201 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
202 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
203 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
204 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
205 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
206 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
207 #define WGL_PBUFFER_LARGEST_ARB 0x2033
208 #define WGL_PBUFFER_WIDTH_ARB 0x2034
209 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
210 #define WGL_PBUFFER_LOST_ARB 0x2036
211 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
212 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
213 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
214 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
215 #define WGL_ACCELERATION_EXT 0x2003
216 #define WGL_NEED_PALETTE_EXT 0x2004
217 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
218 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
219 #define WGL_SWAP_METHOD_EXT 0x2007
220 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
221 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
222 #define WGL_TRANSPARENT_EXT 0x200A
223 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
224 #define WGL_SHARE_DEPTH_EXT 0x200C
225 #define WGL_SHARE_STENCIL_EXT 0x200D
226 #define WGL_SHARE_ACCUM_EXT 0x200E
227 #define WGL_SUPPORT_GDI_EXT 0x200F
228 #define WGL_SUPPORT_OPENGL_EXT 0x2010
229 #define WGL_DOUBLE_BUFFER_EXT 0x2011
230 #define WGL_STEREO_EXT 0x2012
231 #define WGL_PIXEL_TYPE_EXT 0x2013
232 #define WGL_COLOR_BITS_EXT 0x2014
233 #define WGL_RED_BITS_EXT 0x2015
234 #define WGL_RED_SHIFT_EXT 0x2016
235 #define WGL_GREEN_BITS_EXT 0x2017
236 #define WGL_GREEN_SHIFT_EXT 0x2018
237 #define WGL_BLUE_BITS_EXT 0x2019
238 #define WGL_BLUE_SHIFT_EXT 0x201A
239 #define WGL_ALPHA_BITS_EXT 0x201B
240 #define WGL_ALPHA_SHIFT_EXT 0x201C
241 #define WGL_ACCUM_BITS_EXT 0x201D
242 #define WGL_ACCUM_RED_BITS_EXT 0x201E
243 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
244 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
245 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
246 #define WGL_DEPTH_BITS_EXT 0x2022
247 #define WGL_STENCIL_BITS_EXT 0x2023
248 #define WGL_AUX_BUFFERS_EXT 0x2024
249 #define WGL_NO_ACCELERATION_EXT 0x2025
250 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
251 #define WGL_FULL_ACCELERATION_EXT 0x2027
252 #define WGL_SWAP_EXCHANGE_EXT 0x2028
253 #define WGL_SWAP_COPY_EXT 0x2029
254 #define WGL_SWAP_UNDEFINED_EXT 0x202A
255 #define WGL_TYPE_RGBA_EXT 0x202B
256 #define WGL_TYPE_COLORINDEX_EXT 0x202C
257 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
258 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
259 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
260 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
261 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
262 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
263 #define WGL_PBUFFER_LARGEST_EXT 0x2033
264 #define WGL_PBUFFER_WIDTH_EXT 0x2034
265 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
266 #define WGL_DEPTH_FLOAT_EXT 0x2040
267 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
268 #define WGL_SAMPLES_3DFX 0x2061
269 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
270 #define WGL_SAMPLES_EXT 0x2042
271 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
272 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
273 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
274 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
275 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
276 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
277 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
278 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
279 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
280 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
281 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
282 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
283 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
284 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
285 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
286 #define WGL_ARB_buffer_region 1
287 #define WGL_ARB_extensions_string 1
288 #define WGL_ARB_pixel_format 1
289 #define WGL_ARB_make_current_read 1
290 #define WGL_ARB_pbuffer 1
291 #define WGL_EXT_display_color_table 1
292 #define WGL_EXT_extensions_string 1
293 #define WGL_EXT_make_current_read 1
294 #define WGL_EXT_pbuffer 1
295 #define WGL_EXT_pixel_format 1
296 #define WGL_EXT_swap_control 1
297 #define WGL_WGL_EXT_depth_float 1
298 #define WGL_WGL_3DFX_multisample 1
299 #define WGL_WGL_EXT_multisample 1
300 #define WGL_NV_allocate_memory 1
303 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
304 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
305 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
306 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
307 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
311 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
312 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
313 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
314 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
316 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
317 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
318 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
319 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
320 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
321 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
323 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
324 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
325 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
326 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
327 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
328 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
329 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
330 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
332 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
333 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
334 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
335 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
336 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
337 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
338 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
339 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
340 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
341 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
342 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
343 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
344 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
346 #ifdef WGL_WGLEXT_PROTOTYPES
347 extern BOOL WINAPI wglSwapIntervalEXT (int);
348 extern int WINAPI wglGetSwapIntervalEXT (void);
349 #endif /* WGL_WGLEXT_PROTOTYPES */
350 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
351 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
353 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
357 #if defined(__ANDROID__)
359 // OpenGL ES Porting Kit
361 #define glBindFramebuffer glBindFramebufferOES
362 #define glBindRenderbuffer glBindRenderbufferOES
363 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
364 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
365 #define glFramebufferTexture2D glFramebufferTexture2DOES
366 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
367 #define glGenFramebuffers glGenFramebuffersOES
368 #define glGenRenderbuffers glGenRenderbuffersOES
369 #define glDeleteFramebuffers glDeleteFramebuffersOES
370 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
372 #define GL_POLYGON_STIPPLE 0xFFFF
373 #define GL_LINE_STIPPLE 0xFFFF
374 #define GL_LINE 0xFFFF
375 #define GL_FILL 0xFFFF
376 #define GL_ALL_ATTRIB_BITS 0xFFFF
377 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
378 #define glDrawElementsi(type, count, start) glDrawElements(type, count, GL_UNSIGNED_SHORT, start)
383 #define glBufferDatai glesBufferDatai
384 #define glBufferDatad glesBufferDatad
385 #define glVertexPointeri glesVertexPointeri
386 #define glVertexPointerd glesVertexPointerd
388 #define glRecti glesRecti
389 #define glBegin glesBegin
390 #define glTexCoord2i glesTexCoord2i
391 #define glVertex2i glesVertex2i
392 #define glTexCoord2d glesTexCoord2d
393 #define glVertex2d glesVertex2d
394 #define glTexCoord2f glesTexCoord2f
395 #define glVertex2f glesVertex2f
396 #define glEnd glesEnd
397 #define glColor3f glesColor3f
398 #define glColor4ub glesColor4ub
399 #define glColor4fv glesColor4fv
400 #define glLineStipple glesLineStipple
401 #define glNormal3fv glesNormal3fv
402 #define glTexCoord2fv glesTexCoord2fv
403 #define glColorMaterial glesColorMaterial
405 #define glLoadMatrixd glesLoadMatrixd
406 #define glMultMatrixd glesMultMatrixd
407 #define glFrustum glesFrustum
408 #define glOrtho glesOrtho
409 #define glScaled glesScaled
410 #define glTranslated glesTranslated
411 #define glRotated glesRotated
412 #define glVertex3d glesVertex3d
413 #define glVertex3f glesVertex3f
414 #define glVertex3fv glesVertex3fv
415 #define glLightModeli glesLightModeli
419 #define GL_QUAD_STRIP 0
421 #define GL_UNSIGNED_INT 0
424 //#define GL_LINE_STIPPLE 0
425 #define GL_BGRA_EXT 0
426 #define GL_UNPACK_ROW_LENGTH 0
427 #define GL_UNPACK_SKIP_PIXELS 0
428 #define GL_UNPACK_SKIP_ROWS 0
430 #define GL_PACK_ROW_LENGTH 0
431 #define GL_PACK_SKIP_ROWS 0
432 #define GL_PACK_SKIP_PIXELS 0
434 static EGLDisplay eglDisplay;
435 static EGLSurface eglSurface;
436 static EGLContext eglContext;
437 static int eglWidth, eglHeight;
439 static bool egl_init_display(ANativeWindow* window)
441 const EGLint attribs[] =
443 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
448 /*EGL_SAMPLE_BUFFERS, 1,
449 EGL_SAMPLES, 0, //2,*/
452 EGLint w, h, dummy, format;
458 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
459 eglInitialize(display, 0, 0);
460 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
461 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
463 surface = eglCreateWindowSurface(display, config, window, null);
464 context = eglCreateContext(display, config, null, null);
466 if(!eglMakeCurrent(display, surface, surface, context))
469 eglQuerySurface(display, surface, EGL_WIDTH, &w);
470 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
472 eglDisplay = display;
473 eglContext = context;
474 eglSurface = surface;
478 glEnableClientState(GL_VERTEX_ARRAY);
480 // Initialize GL state.
481 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
482 glEnable(GL_CULL_FACE);
483 glShadeModel(GL_SMOOTH);
484 glDisable(GL_DEPTH_TEST);
486 glDisable(GL_CULL_FACE);
487 glDisable(GL_DEPTH_TEST);
489 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
492 glMatrixMode(GL_MODELVIEW);
493 glScalef(1.0f, 1.0f, -1.0f);
494 glMatrixMode(GL_PROJECTION);
495 glShadeModel(GL_FLAT);
497 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
498 glFogi(GL_FOG_MODE, GL_EXP);
499 glFogf(GL_FOG_DENSITY, 0);
500 glEnable(GL_NORMALIZE);
501 glDepthFunc(GL_LESS);
503 glDisable(GL_MULTISAMPLE_ARB);
507 glOrtho(0,w,h,0,0.0,1.0);
511 static void egl_term_display()
515 glDeleteTextures(1, (int *)&stippleTexture);
518 if(eglDisplay != EGL_NO_DISPLAY)
520 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
521 if(eglContext != EGL_NO_CONTEXT)
522 eglDestroyContext(eglDisplay, eglContext);
523 if(eglSurface != EGL_NO_SURFACE)
524 eglDestroySurface(eglDisplay, eglSurface);
525 eglTerminate(eglDisplay);
527 eglDisplay = EGL_NO_DISPLAY;
528 eglContext = EGL_NO_CONTEXT;
529 eglSurface = EGL_NO_SURFACE;
532 // OpenGL Immediate Mode Porting Kit
533 static int beginCount;
534 static int vertexCount;
535 static int normalCount;
536 static float *vertexPointer;
537 static float *normalPointer;
538 static GLenum beginMode;
539 static unsigned int beginBufferSize, normalBufferSize;
540 static int numVertexCoords = 2;
542 void glesRecti(int a, int b, int c, int d)
552 void glesBegin(GLenum mode)
559 normalBufferSize = beginBufferSize = 1024; // default number of vertices
560 vertexPointer = new float[beginBufferSize * 5];
561 normalPointer = new float[normalBufferSize * 3];
565 void glesTexCoord2f(float x, float y)
567 int count = vertexCount;
569 if(vertexCount + numVertexCoords > beginBufferSize)
571 beginBufferSize = beginBufferSize + beginBufferSize/2;
572 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
575 vertexPointer[count*(2+numVertexCoords) ] = x;
576 vertexPointer[count*(2+numVertexCoords)+1] = y;
579 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
581 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
582 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
584 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
585 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
589 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
590 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
591 void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
593 void glesVertex2f(float x, float y)
596 if(vertexCount + 4 > beginBufferSize)
598 beginBufferSize = beginBufferSize + beginBufferSize/2;
599 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
602 vertexPointer[vertexCount*4+2] = x;
603 vertexPointer[vertexCount*4+3] = y;
606 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
608 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
609 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
611 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
612 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
617 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
618 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
622 int mode = beginMode;
623 if(mode == GL_QUADS) mode = GL_TRIANGLES;
624 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
626 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
627 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
628 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
629 if(normalCount && normalCount == vertexCount)
631 glEnableClientState(GL_NORMAL_ARRAY);
632 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
635 glDrawArrays(mode, 0, vertexCount);
637 glDisableClientState(GL_NORMAL_ARRAY);
638 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
643 static float *floatVPBuffer = null;
644 static short *shortVPBuffer = null;
645 static unsigned int shortVPSize = 0, floatVPSize = 0;
648 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
649 static unsigned short *shortBDBuffer = null;
650 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
652 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
657 if(numVertices*numCoords > shortVPSize)
659 shortVPSize = numVertices*numCoords;
660 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
662 for(i = 0; i < numVertices*numCoords; i++)
663 shortVPBuffer[i] = (short)pointer[i];
664 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
667 glVertexPointer(numCoords, GL_SHORT, stride, 0);
670 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
675 if(numVertices*numCoords > floatVPSize)
677 floatVPSize = numVertices*numCoords;
678 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
680 for(i = 0; i < numVertices*numCoords; i++)
681 floatVPBuffer[i] = (float)pointer[i];
682 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
685 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
688 void glesTexReuseIntVP(int numCoords)
690 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
693 void glesTexReuseDoubleVP(int numCoords)
695 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
698 void glesColor3f( float r, float g, float b )
700 glColor4f(r, g, b, 1.0f);
703 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
705 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
708 void glesColor4fv(float * a)
710 glColor4f(a[0], a[1], a[2], a[3]);
713 void glesBufferDatad(int target, int size, void * data, int usage)
715 int numElems = size/sizeof(double);
716 double * dblPtr = (double *)data;
718 if (numElems > floatVPSize)
720 floatVPSize = numElems;
721 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
723 for (i=0; i< numElems; i++)
724 floatVPBuffer[i] = (float)dblPtr[i];
726 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
729 void glesBufferDatai(int target, int size, void * data, int usage)
731 int numElems = size/sizeof(unsigned int);
732 unsigned int * pointer = (unsigned int *)data;
734 if (numElems > shortBDSize)
736 shortBDSize = numElems;
737 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
739 for (i=0; i< numElems; i++)
740 shortBDBuffer[i] = (unsigned short)pointer[i];
742 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
745 /// *** These might require an implementation to get things working ***
746 void glesLoadMatrixd( double * i )
750 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
751 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
752 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
753 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
758 void glesOrtho( double l, double r, double b, double t, double n, double f )
762 { (float)(2 / (r - l)), 0, 0, 0 },
763 { 0, (float)(2 / (t - b)), 0, 0 },
764 { 0, 0, (float)(-2 / (f - n)), 0 },
765 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
767 glMultMatrixf((float *)matrix);
770 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
771 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
772 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
774 void glesMultMatrixd( double * i )
778 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
779 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
780 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
781 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
786 // Need to do these...
787 void glesVertex3f( float x, float y, float z )
790 if(vertexCount + 4 > beginBufferSize)
792 beginBufferSize = beginBufferSize + beginBufferSize/2;
793 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
796 vertexPointer[vertexCount*5+2] = x;
797 vertexPointer[vertexCount*5+3] = y;
798 vertexPointer[vertexCount*5+4] = z;
801 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
803 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
804 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
805 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
807 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
808 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
809 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
815 void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
816 void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
818 void glesNormal3f(float x, float y, float z)
820 normalCount = vertexCount;
821 if(vertexCount + 4 > normalBufferSize)
823 normalBufferSize = normalBufferSize + normalBufferSize/2;
824 normalPointer = renew normalPointer float[normalBufferSize * 2];
827 normalPointer[normalCount*3+0] = x;
828 normalPointer[normalCount*3+1] = y;
829 normalPointer[normalCount*3+2] = z;
832 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
834 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
835 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
836 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
838 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
839 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
840 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
844 void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
845 void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
847 void glesColorMaterial(int a, int b)
849 PrintLn("glColorMaterial stub");
854 delete vertexPointer;
857 delete floatVPBuffer;
860 delete shortVPBuffer;
863 delete shortBDBuffer;
867 static int stippleTexture;
868 static bool stippleEnabled;
870 void glesLineStipple( int i, unsigned short j )
875 for(x = 0; x < 16; x++)
877 bool v = (j & (1 << x)) != 0;
878 texture[x] = v ? 0xFFFFFFFF : 0;
881 glGenTextures(1, &stippleTexture);
882 glBindTexture(GL_TEXTURE_2D, stippleTexture);
883 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
884 glEnable(GL_TEXTURE_2D);
885 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
886 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
887 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
888 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
889 glMatrixMode(GL_TEXTURE);
891 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
892 glScaled(i/16.0, 1, 1.0f);
893 glTranslated(0.5, 0.5, 0);
894 glMatrixMode(GL_PROJECTION);
897 void glesFrustum( double l, double r, double b, double t, double n, double f )
899 float A = (float)((r + l) / (r - l));
900 float B = (float)((t + b) / (t - b));
901 float C = (float)(-(f + n) / (f - n));
902 float D = (float)(-2*f*n/(f-n));
905 { (float)(2*n / (r - l)), 0, 0, 0 },
906 { 0, (float)(2*n / (t - b)), 0, 0 },
910 glMultMatrixf((float *)matrix);
913 void glesLightModeli( unsigned int pname, int param )
915 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
916 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
919 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
920 void glFogi( unsigned int pname, int param ) { }
921 void glPolygonMode( unsigned int i, unsigned int j ) { }
924 // *** Picking won't be supported for now ***
925 void glPushName( unsigned int i ) { }
926 void glLoadName( unsigned int i ) { }
929 // Probably replace by regular glBlendFunc ...
930 void glBlendFuncSeparate(int a, int b, int c, int d)
935 // For direct pixel blitting...
936 void glRasterPos2d(double a, double b) { }
937 void glPixelZoom(float a, float b) { }
938 void glDrawPixels(int a, int b, int c, int d, void * e) { }
942 /* Non OpenGL ES friendly stuff
943 #undef GL_UNSIGNED_INT
949 #undef GL_POLYGON_STIPPLE
950 #undef GL_LINE_STIPPLE
953 #undef GL_ALL_ATTRIB_BITS
954 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
959 #if !defined(__APPLE__)
960 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
961 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
962 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
963 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
968 static int currentVertexBuffer;
970 bool GLSelectVBO(uint vbo)
972 if(currentVertexBuffer != vbo)
974 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
975 currentVertexBuffer = vbo;
981 void GLBindBuffer(int target, uint buffer)
984 glBindBuffer(target, buffer);
986 glBindBufferARB(target, buffer);
990 static int displayWidth, displayHeight;
992 #define GL_CLAMP_TO_EDGE 0x812F
994 static bool useSingleGLContext = false;
995 class OGLDisplay : struct
997 #if defined(__WIN32__)
1007 int imageBuffers[2];
1008 byte * pboMemory1, * pboMemory2;
1011 GLXContext glContext;
1014 XShmSegmentInfo shminfo;
1016 XShmSegmentInfo shminfoShape;
1017 XImage * shapeImage;
1021 X11Picture windowPicture;
1022 X11Picture pixmapPicture;
1024 X11Picture shapePicture;
1027 ColorAlpha * flippingBuffer;
1028 int flipBufH, flipBufW;
1033 class OGLSystem : struct
1035 #if defined(__WIN32__)
1036 PIXELFORMATDESCRIPTOR pfd;
1042 XVisualInfo * visualInfo;
1043 GLXContext glContext;
1045 GLXPixmap dummyGLXPixmap;
1050 class OGLSurface : struct
1057 float foreground[4], background[4], bitmapMult[4];
1060 class OGLMesh : struct
1069 class OGLIndices : struct
1076 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1077 static int primitiveTypes[RenderPrimitiveType] =
1079 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1086 class OpenGLDisplayDriver : DisplayDriver
1088 class_property(name) = "OpenGL";
1090 bool LockSystem(DisplaySystem displaySystem)
1092 OGLSystem oglSystem = displaySystem.driverData;
1093 if(useSingleGLContext) return true;
1094 #if defined(__WIN32__)
1095 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1096 #elif defined(__unix__)
1097 //if(previous) return true;
1098 // printf("Making SYSTEM current\n");
1099 /*#if defined(__APPLE__)
1100 //glXMakeCurrent(xGlobalDisplay, displaySystem.window, oglSystem.glContext);
1102 #if defined(__ANDROID__)
1104 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /-*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
1107 //previous = oglSystem.glContext;
1112 void UnlockSystem(DisplaySystem displaySystem)
1114 if(useSingleGLContext) return;
1115 #if defined(__WIN32__)
1116 wglMakeCurrent(null, null);
1117 #elif defined(__unix__)
1118 // printf("Making NULL current\n");
1119 #if defined(__ANDROID__)
1121 glXMakeCurrent(xGlobalDisplay, None, null);
1127 bool Lock(Display display)
1129 OGLDisplay oglDisplay = display.driverData;
1130 OGLSystem oglSystem = display.displaySystem.driverData;
1132 if(useSingleGLContext) return true;
1133 #if defined(__WIN32__)
1134 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1135 #elif defined(__unix__)
1136 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1137 // printf(" Making DISPLAY current\n");
1138 #if defined(__ANDROID__)
1140 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1146 void Unlock(Display display)
1148 if(useSingleGLContext) return;
1149 //printf(" Making NULL current\n");
1150 //glXMakeCurrent(xGlobalDisplay, None, null);
1152 LockSystem(display.displaySystem);
1155 void DestroyDisplay(Display display)
1157 OGLDisplay oglDisplay = display.driverData;
1161 #if defined(__WIN32__)
1162 wglMakeCurrent( null, null );
1165 wglDeleteContext(oglDisplay.glrc);
1167 if(oglDisplay.hdc && oglDisplay.pBuffer)
1168 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1170 if(oglDisplay.pBuffer)
1171 wglDestroyPbufferARB(oglDisplay.pBuffer);
1174 ReleaseDC(display.window, oglDisplay.hdc);
1176 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1177 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1179 #elif defined(__unix__)
1180 #if defined(__ANDROID__)
1182 if(oglDisplay.shapePixmap)
1183 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1184 if(oglDisplay.pixmap)
1185 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1186 if(oglDisplay.image)
1188 if(oglDisplay.shminfoShape.shmid != -1)
1190 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1191 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1192 shmdt(oglDisplay.shminfo.shmaddr);
1193 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1196 if(oglDisplay.shapeImage)
1198 if(oglDisplay.shminfoShape.shmid != -1)
1200 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1201 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1202 shmdt(oglDisplay.shminfoShape.shmaddr);
1203 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1205 XDestroyImage(oglDisplay.shapeImage);
1206 oglDisplay.shapeImage = None;
1209 glXMakeCurrent(xGlobalDisplay, None, null);
1211 if(oglDisplay.glContext)
1212 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1215 delete oglDisplay.flippingBuffer;
1217 display.driverData = null;
1221 bool CreateDisplaySystem(DisplaySystem displaySystem)
1223 bool result = false;
1224 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1227 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1229 oglSystem.hdc = GetDC(oglSystem.hwnd);
1233 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1234 oglSystem.pfd.nVersion = 1;
1235 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1236 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1237 oglSystem.pfd.cColorBits = 24;
1238 oglSystem.pfd.cAlphaBits = 8;
1239 oglSystem.pfd.cDepthBits = 24;
1240 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1242 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1243 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1245 if(oglSystem.pfd.cColorBits > 8)
1247 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1248 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1251 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1253 // Get Pointers To The GL Functions
1254 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1255 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1256 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1257 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1258 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1259 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1260 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1261 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1262 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1263 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1264 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1265 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1267 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1268 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1269 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1270 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1271 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1272 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1273 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1274 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1275 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1277 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1279 // eSystem_LoggingMode(LOG_MSGBOX, null);
1281 if(wglChoosePixelFormatARB)
1286 float fAttributes[] = {0,0};
1289 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1290 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1291 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1292 WGL_COLOR_BITS_ARB,24,
1293 WGL_ALPHA_BITS_ARB,8,
1294 WGL_DEPTH_BITS_ARB,16,
1295 WGL_STENCIL_BITS_ARB,0,
1296 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1297 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1298 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1302 //Log("Found wglChoosePixelFormatARB\n");
1304 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1305 if(!valid || !numFormats)
1307 //Log("Can't find 4x multi sampling\n");
1308 iAttributes[19] = 2;
1309 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1310 if(!valid || !numFormats)
1312 // Log("Can't find 2x multi sampling\n");
1313 iAttributes[16] = 0;
1314 iAttributes[17] = 0;
1315 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1318 if(valid && numFormats)
1320 oglSystem.format = pixelFormat;
1321 wglMakeCurrent(null, null);
1322 wglDeleteContext(oglSystem.glrc);
1324 // *** DescribePixelFormat does not support WGL pixel formats! ***
1325 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1326 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1327 //Log("Successfully set pixel format\n");
1329 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1330 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1334 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1338 wglMakeCurrent(null, null);
1340 //eSystem_DumpErrors(true);
1344 #elif defined(__unix__)
1345 #if defined(__ANDROID__)
1346 egl_init_display(guiApp.desktop.windowHandle);
1351 #ifndef ECERE_MINIGLX
1352 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1355 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1359 oglSystem.visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
1360 if(oglSystem.visualInfo)
1362 //printf("glXChooseVisual returnded a visual info\n");
1363 oglSystem.dummyPixmap = XCreatePixmap(xGlobalDisplay, (uint)displaySystem.window, 1, 1, oglSystem.visualInfo->depth);
1364 oglSystem.dummyGLXPixmap = glXCreateGLXPixmap(xGlobalDisplay, oglSystem.visualInfo, oglSystem.dummyPixmap);
1366 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1367 // printf("Creating system Context (%x)!\n", oglSystem.glContext);
1368 if(oglSystem.glContext)
1370 //printf("Got a Context\n");
1371 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /-*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
1375 glXMakeCurrent(xGlobalDisplay, None, null);
1383 displaySystem.flags.alpha = true;
1384 displaySystem.flags.flipping = true;
1385 displaySystem.pixelFormat = pixelFormat888;
1389 void DestroyDisplaySystem(DisplaySystem displaySystem)
1391 OGLSystem oglSystem = displaySystem.driverData;
1393 #if defined(__WIN32__)
1394 wglMakeCurrent( null, null );
1397 wglDeleteContext(oglSystem.glrc);
1400 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1401 DestroyWindow(oglSystem.hwnd);
1403 #elif defined(__unix__)
1404 #if defined(__ANDROID__)
1407 if(oglSystem.visualInfo)
1409 #ifdef ECERE_MINIGLX
1410 __miniglx_XFree(oglSystem.visualInfo);
1412 XFree(oglSystem.visualInfo);
1416 if(oglSystem.dummyGLXPixmap)
1417 glXDestroyGLXPixmap(xGlobalDisplay, oglSystem.dummyGLXPixmap);
1418 if(oglSystem.dummyPixmap);
1419 XFreePixmap(xGlobalDisplay, oglSystem.dummyPixmap);
1425 bool CreateDisplay(Display display)
1427 bool result = false;
1428 OGLDisplay oglDisplay = display.driverData;
1429 OGLSystem oglSystem = display.displaySystem.driverData;
1431 oglDisplay = display.driverData = OGLDisplay { };
1432 //printf("Inside CreateDisplay\n");
1434 #if defined(__WIN32__) || defined(USEPBUFFER)
1435 if(!display.alphaBlend)
1438 #if defined(__WIN32__)
1439 oglDisplay.hdc = GetDC(display.window);
1440 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1441 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1443 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1444 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1448 ReleaseDC(display.window, oglDisplay.hdc);
1449 #elif defined(__unix__)
1450 #if defined(__ANDROID__)
1452 XVisualInfo * visualInfo = null;
1456 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1457 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
1461 GLX_DOUBLEBUFFER, True,
1466 //visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
1467 visualInfo = ((XWindowData)display.windowDriverData).visual;
1470 GLXFBConfig *fbconfigs, fbconfig;
1472 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
1476 for (i = 0; i < numfbconfigs; i++)
1478 XRenderPictFormat * format;
1479 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
1480 if (!visualInfo) continue;
1481 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
1482 if (!format) continue;
1484 if(format->direct.alphaMask > 0)
1486 fbconfig = fbconfigs[i];
1489 //XFree(visualInfo);
1492 if (i == numfbconfigs)
1494 fbconfig = fbconfigs[0];
1495 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
1502 //printf("visualInfo is not null\n");
1503 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1504 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1505 //XFree(visualInfo);
1508 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1509 if(oglDisplay.glContext)
1511 //printf("CreateDisplay Got a Context\n");
1512 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1518 #if defined(__WIN32__) || defined(USEPBUFFER)
1524 #if !defined(__OLDX__)
1525 if(glBlendFuncSeparate)
1526 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1529 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1532 glMatrixMode(GL_MODELVIEW);
1533 glScalef(1.0f, 1.0f, -1.0f);
1534 // glTranslatef(0.375f, 0.375f, 0.0f);
1535 // glTranslatef(-0.625f, -0.625f, 0.0f);
1536 glMatrixMode(GL_PROJECTION);
1537 glShadeModel(GL_FLAT);
1539 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1540 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1541 glFogi(GL_FOG_MODE, GL_EXP);
1542 glFogf(GL_FOG_DENSITY, 0);
1543 glEnable(GL_NORMALIZE);
1544 glDepthFunc(GL_LESS);
1546 glDisable(GL_MULTISAMPLE_ARB);
1548 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1549 display.ambient = Color { 50,50,50 };
1552 if(!useSingleGLContext)
1553 #if defined(__WIN32__)
1554 wglMakeCurrent(null, null);
1555 #elif defined(__unix__)
1556 #if defined(__ANDROID__)
1559 glXMakeCurrent(xGlobalDisplay, None, null);
1566 bool DisplaySize(Display display, int width, int height)
1568 OGLDisplay oglDisplay = display.driverData;
1569 OGLSystem oglSystem = display.displaySystem.driverData;
1571 bool result = false;
1573 //printf("Inside DisplaySize\n");
1574 #if defined(__WIN32__) || defined(USEPBUFFER)
1575 if(display.alphaBlend)
1577 #if defined(__WIN32__)
1578 const int attributes[]=
1580 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1581 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1583 int pixelFormat = 0;
1584 if(wglChoosePixelFormatARB)
1588 float fAttributes[] = {0,0};
1591 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1592 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1593 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1594 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1595 WGL_COLOR_BITS_ARB,24,
1596 WGL_ALPHA_BITS_ARB,8,
1597 WGL_DEPTH_BITS_ARB,16,
1598 WGL_STENCIL_BITS_ARB,0,
1599 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1600 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1601 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1605 //Log("Found wglChoosePixelFormatARB\n");
1607 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1608 if(!valid || !numFormats)
1610 //Log("Can't find 4x multi sampling\n");
1611 iAttributes[19] = 2;
1612 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1613 if(!valid || !numFormats)
1615 // Log("Can't find 2x multi sampling\n");
1616 iAttributes[16] = 0;
1617 iAttributes[17] = 0;
1618 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1619 if(!valid || !numFormats)
1623 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1624 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1625 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1626 WGL_COLOR_BITS_ARB,24,
1627 WGL_ALPHA_BITS_ARB,8,
1628 WGL_DEPTH_BITS_ARB,16,
1631 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1635 if(valid && numFormats)
1637 wglMakeCurrent(null, null);
1641 wglMakeCurrent( null, null );
1642 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1643 if(oglDisplay.hdc && oglDisplay.pBuffer)
1644 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1646 wglDestroyPbufferARB(oglDisplay.pBuffer);
1648 if(!useSingleGLContext)
1649 wglMakeCurrent( null, null );
1652 wglDeleteContext(oglDisplay.glrc);
1654 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1655 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1656 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1659 HDC hdc = GetDC(display.window);
1661 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1662 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1664 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1665 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1667 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1669 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1673 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1674 oglDisplay.memDC = CreateCompatibleDC(hdc);
1675 SetMapMode(oglDisplay.memDC, MM_TEXT);
1676 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1677 info->bmiHeader.biPlanes = 1;
1678 info->bmiHeader.biCompression = BI_RGB;
1679 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1680 info->bmiHeader.biWidth = width;
1681 info->bmiHeader.biHeight = height;
1682 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1685 SelectObject(oglDisplay.memDC, newBitmap);
1686 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1689 PIXELFORMATDESCRIPTOR pfd = { 0 };
1690 pfd.nSize = (short)sizeof(pfd);
1692 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1693 pfd.iPixelType = PFD_TYPE_RGBA;
1694 pfd.cColorBits = 32;
1695 //pfd.cAlphaBits = 8;
1696 pfd.cDepthBits = 24;
1697 pfd.iLayerType = PFD_MAIN_PLANE;
1699 oglDisplay.hdc = oglDisplay.memDC;
1701 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1702 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1703 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1705 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1706 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1707 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1712 const int imageSize = width * height * 4;
1714 glGenBuffersARB(2, oglDisplay.imageBuffers);
1716 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1717 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1718 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1719 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1722 oglDisplay.memBitmap = newBitmap;
1723 oglDisplay.stride = width;
1729 ReleaseDC(display.window, hdc);
1731 #elif defined(__unix__)
1732 #if defined(__ANDROID__)
1737 GLX_DOUBLEBUFFER, True,
1743 GLX_STENCIL_SIZE, 1,
1744 //GLX_DEPTH_SIZE, 24,
1745 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1746 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1752 GLX_PBUFFER_WIDTH, width,
1753 GLX_PBUFFER_HEIGHT, height,
1754 GLX_LARGEST_PBUFFER, False,
1758 // choose a pixel format that meets our minimum requirements
1761 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1764 if(oglDisplay.pixmap)
1766 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1767 oglDisplay.pixmap = None;
1769 if(oglDisplay.shapePixmap)
1771 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1772 oglDisplay.shapePixmap = None;
1775 // Free Shared Memory Pixmap
1776 if(oglDisplay.image)
1778 if(oglDisplay.shminfoShape.shmid != -1)
1780 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1781 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1782 shmdt(oglDisplay.shminfo.shmaddr);
1783 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1785 XDestroyImage(oglDisplay.image);
1786 oglDisplay.image = None;
1788 if(oglDisplay.shapeImage)
1790 if(oglDisplay.shminfoShape.shmid != -1)
1792 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1793 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1794 shmdt(oglDisplay.shminfoShape.shmaddr);
1795 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1797 XDestroyImage(oglDisplay.shapeImage);
1798 oglDisplay.shapeImage = None;
1801 if(oglDisplay.windowPicture)
1802 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1803 if(oglDisplay.pixmapPicture)
1804 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1806 if(oglDisplay.pixmap)
1807 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1809 if(oglDisplay.glContext)
1810 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1811 if(oglDisplay.pBuffer)
1812 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1814 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1815 if(oglDisplay.pBuffer)
1817 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1818 if(oglDisplay.glContext)
1820 glXMakeCurrent(xGlobalDisplay, None, null);
1821 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1823 // Initialize Shared Memory Pixmap
1824 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1825 ZPixmap, null, &oglDisplay.shminfo, width, height);
1826 if(oglDisplay.image)
1828 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1829 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1830 if(oglDisplay.shminfo.shmid != -1)
1832 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1833 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1835 oglDisplay.shminfo.readOnly = False;
1836 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1838 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1839 &oglDisplay.shminfo, width, height, 32);
1841 // Initialize Shared Memory Shape Pixmap
1842 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1843 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1844 if(oglDisplay.shapeImage)
1846 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1847 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1848 if(oglDisplay.shminfoShape.shmid != -1)
1850 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1851 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1853 oglDisplay.shminfoShape.readOnly = False;
1854 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1856 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1857 &oglDisplay.shminfoShape, width, height, 1);
1858 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1861 XRenderPictureAttributes attributes = { 0 };
1862 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1863 #if !defined(__APPLE__) && !defined(__OLDX__)
1864 attributes.repeat = RepeatNormal;
1866 attributes.repeat = 1;
1868 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1869 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1870 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1871 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1874 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1875 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1892 CreateDisplay(display);
1893 #if defined(__WIN32__)
1894 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1895 #elif defined(__unix__)
1896 #if defined(__ANDROID__)
1900 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1907 if(!result && display.alphaBlend)
1909 printf("Alpha blending windows not supported on this display\n");
1916 glViewport(0,0,width,height);
1918 glOrtho(0,width,height,0,0.0,1.0);
1919 displayWidth = display.width = width;
1920 displayHeight = display.height = height;
1922 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1924 oglDisplay.flipBufW = width;
1925 oglDisplay.flipBufH = height;
1926 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1928 if(oglDisplay.flippingBuffer || !width || !height)
1934 void DisplayPosition(Display display, int x, int y)
1936 OGLDisplay oglDisplay = display.driverData;
1942 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1946 void RestorePalette(Display display)
1950 void StartUpdate(Display display)
1954 void EndUpdate(Display display)
1958 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1962 void Update(Display display, Box updateBox)
1964 OGLDisplay oglDisplay = display.driverData;
1965 //Logf("DisplayScreen\n");
1969 #if defined(__WIN32__) || defined(USEPBUFFER)
1970 if(display.alphaBlend)
1972 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1973 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1974 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1975 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1976 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1979 #if defined(__WIN32__)
1981 POINT point = { oglDisplay.x, oglDisplay.y};
1982 POINT srcPoint = { 0, 0 };
1983 BLENDFUNCTION blend = { 0 };
1985 size.cx = display.width;
1986 size.cy = display.height;
1987 blend.BlendOp = AC_SRC_OVER;
1988 blend.BlendFlags = 0;
1989 blend.SourceConstantAlpha = 255;
1990 blend.AlphaFormat = AC_SRC_ALPHA;
1993 // Process partial images. Mapping the buffer waits for
1994 // outstanding DMA transfers into the buffer to finish.
1995 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1996 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1998 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1999 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
2002 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
2003 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
2006 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
2009 // Unmap the image buffers
2010 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2011 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2013 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2014 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
2016 // Bind two different buffer objects and start the glReadPixels
2017 // asynchronously. Each call will return directly after
2018 // starting the DMA transfer.
2019 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
2020 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2022 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
2023 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
2027 #elif defined(__unix__)
2028 #if defined(__ANDROID__)
2030 XTransform transform =
2033 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
2034 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
2035 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
2038 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
2039 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2040 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2041 #if !defined(__APPLE__) && !defined(__OLDX__)
2042 XShapeCombineMask(xGlobalDisplay, (uint)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2044 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2046 XFlush(xGlobalDisplay);
2054 #if defined(__WIN32__)
2055 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2056 SwapBuffers(oglDisplay.hdc);
2057 #elif defined(__unix__)
2058 #if defined(__ANDROID__)
2059 eglSwapBuffers(eglDisplay, eglSurface);
2061 glXSwapBuffers(xGlobalDisplay, (int)display.window);
2065 //Logf("Out of DisplayScreen\n");
2068 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2070 glDeleteTextures(1, (int *)&bitmap.driverData);
2071 bitmap.driverData = 0;
2073 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2076 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2078 bool result = false;
2082 uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
2083 //uint w = pow2i(Min(width, 2048)), h = pow2i(Min(height, 2048));
2084 //uint w = pow2i(Min(width, 512)), h = pow2i(Min(height, 512));
2086 glGenTextures(1, &glBitmap);
2087 glBindTexture(GL_TEXTURE_2D, glBitmap);
2089 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2091 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2092 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2094 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2095 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2097 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2099 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2101 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2102 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2106 bitmap.driverData = (void *)glBitmap;
2107 bitmap.driver = displaySystem.driver;
2115 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2117 bool result = false;
2118 OGLSystem oglSystem = displaySystem.driverData;
2120 // Pre process the bitmap... First make it 32 bit
2121 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
2124 uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
2125 //uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
2128 // Switch ARGB to RGBA
2129 //if(bitmap.format != pixelFormatRGBA)
2131 for(c=0; c<bitmap.size; c++)
2133 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2135 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
2136 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2139 bitmap.pixelFormat = pixelFormat888;
2142 glGenTextures(1, &glBitmap);
2145 int error = glGetError();
2150 glBindTexture(GL_TEXTURE_2D, glBitmap);
2151 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2154 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2155 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2157 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2158 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2163 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2167 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2170 if(bitmap.width != w || bitmap.height != h)
2172 mipMap = Bitmap { };
2173 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
2175 Surface mipSurface = mipMap.GetSurface(0,0,null);
2176 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
2193 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2194 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2195 //printf("Calling glTexImage2D\n");
2196 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2197 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2198 if((error = glGetError()))
2200 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2201 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2205 if(mipMap != bitmap)
2210 if(!bitmap.keepData)
2211 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
2212 bitmap.driverData = (void *)glBitmap;
2213 bitmap.driver = displaySystem.driver;
2216 FreeBitmap(displaySystem, bitmap);
2217 else if(oglSystem.loadingFont)
2219 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2221 oglSystem.loadingFont = false;
2227 void ReleaseSurface(Display display, Surface surface)
2229 glDisable(GL_SCISSOR_TEST);
2230 delete surface.driverData;
2231 surface.driverData = null;
2234 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2239 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2241 bool result = false;
2242 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2244 //Logf("GetSurface\n");
2248 if(displayWidth != display.width || displayHeight != display.height)
2250 displayWidth = display.width;
2251 displayHeight = display.height;
2253 glViewport(0,0,display.width,display.height);
2255 glOrtho(0,display.width,display.height,0,0.0,1.0);
2258 surface.offset.x = x;
2259 surface.offset.y = y;
2260 surface.unclippedBox = surface.box = clip;
2261 oglSurface.bitmapMult[0] = 1;
2262 oglSurface.bitmapMult[1] = 1;
2263 oglSurface.bitmapMult[2] = 1;
2264 oglSurface.bitmapMult[3] = 1;
2266 glEnable(GL_SCISSOR_TEST);
2269 (display.height) -(y+clip.bottom)-1,
2270 clip.right-clip.left+1,
2271 clip.bottom-clip.top+1);
2277 void Clip(Display display, Surface surface, Box clip)
2286 box.Clip(surface.unclippedBox);
2290 box = surface.box = surface.unclippedBox;
2291 box.left += surface.offset.x;
2292 box.top += surface.offset.y;
2293 box.right+= surface.offset.x;
2294 box.bottom += surface.offset.y;
2297 box.left,display.height - box.bottom - 1,
2298 box.right-box.left+1, box.bottom-box.top+1);
2301 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2303 bool result = false;
2304 OGLDisplay oglDisplay = display.driverData;
2305 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2307 if(oglDisplay.flippingBuffer)
2309 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2312 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2318 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2319 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2320 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2321 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2322 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2325 for(row = 0; row<h; row++)
2326 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2333 void SetForeground(Display display, Surface surface, ColorAlpha color)
2335 OGLSurface oglSurface = surface.driverData;
2337 //Logf("SetForeground\n");
2339 oglSurface.foreground[0] = color.color.r/255.0f;
2340 oglSurface.foreground[1] = color.color.g/255.0f;
2341 oglSurface.foreground[2] = color.color.b/255.0f;
2342 //oglSurface.foreground[3] = 1.0f;
2343 oglSurface.foreground[3] = color.a/255.0f;
2345 //if(!oglSurface.foreground[3])printf("bug");
2348 void SetBackground(Display display, Surface surface, ColorAlpha color)
2350 OGLSurface oglSurface = surface.driverData;
2352 //Logf("SetBackground\n");
2354 oglSurface.background[0] = color.color.r/255.0f;
2355 oglSurface.background[1] = color.color.g/255.0f;
2356 oglSurface.background[2] = color.color.b/255.0f;
2357 //oglSurface.background[3] = 1.0;
2358 oglSurface.background[3] = color.a/255.0f;
2361 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2363 OGLSurface oglSurface = surface.driverData;
2365 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2366 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2367 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2368 oglSurface.bitmapMult[3] = color.a/255.0f;
2371 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2376 void PutPixel(Display display, Surface surface,int x,int y)
2378 OGLSurface oglSurface = surface.driverData;
2380 //Logf("PutPixel\n");
2382 glColor4fv(oglSurface.foreground);
2384 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2385 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2390 void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
2392 OGLSurface oglSurface = surface.driverData;
2393 if(x1 == x2) { y2++; y1--; }
2394 else if(y1 == y2) { x2++; x1--; }
2395 x1 += surface.offset.x;
2396 y1 += surface.offset.y;
2397 x2 += surface.offset.x;
2398 y2 += surface.offset.y;
2402 glColor4fv(oglSurface.foreground);
2407 glTexCoord2f(0.5f, 0);
2408 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2409 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2410 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2419 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2420 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2426 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2428 OGLSurface oglSurface = surface.driverData;
2429 x1 += surface.offset.x;
2430 y1 += surface.offset.y;
2431 x2 += surface.offset.x;
2432 y2 += surface.offset.y;
2434 //Logf("Rectangle\n");
2436 glColor4fv(oglSurface.foreground);
2442 glTexCoord2f(0.5f, 0);
2443 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2444 glTexCoord2f(y2-y1 + 0.5f, 0);
2445 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2447 glTexCoord2f(0.5f, 0);
2448 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2449 glTexCoord2f(x2 - x1 + 0.5f, 0);
2450 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2452 glTexCoord2f(0.5f, 0);
2453 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2454 glTexCoord2f(y1 - y2 + 0.5f, 0);
2455 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2457 glTexCoord2f(0.5f, 0);
2458 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2459 glTexCoord2f(x1 - x2 + 0.5f, 0);
2460 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2465 glBegin(GL_LINE_LOOP);
2472 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2473 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2474 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2475 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2480 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2482 OGLSurface oglSurface = surface.driverData;
2485 glColor4fv(oglSurface.background);
2486 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2487 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2490 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2491 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2495 void Clear(Display display, Surface surface, ClearType type)
2497 OGLDisplay oglDisplay = display.driverData;
2498 OGLSurface oglSurface = surface.driverData;
2501 if(type != depthBuffer)
2502 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2503 if(type != colorBuffer && !oglDisplay.depthWrite)
2505 glDepthMask((byte)bool::true);
2507 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2508 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2509 if(type != colorBuffer && !oglDisplay.depthWrite)
2511 glDepthMask((byte)bool::false);
2515 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2520 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2522 OGLSurface oglSurface = surface.driverData;
2524 #if !defined(__OLDX__)
2525 // WHY DO WE HAVE GL_ONE HERE ?
2526 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2527 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2530 if(!oglSurface.writingText)
2532 // glTranslatef(-0.375f, -0.375f, 0.0f);
2533 glEnable(GL_TEXTURE_2D);
2534 glColor4fv(oglSurface.bitmapMult);
2536 else if(oglSurface.xOffset)
2537 glTranslatef(oglSurface.xOffset / 64.0f/*-0.375f*/, 0.0f, 0.0f);
2539 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2544 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2545 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2546 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2547 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2548 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2549 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2550 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2551 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2556 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2557 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2558 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2559 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2560 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2561 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2562 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2563 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2566 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2567 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2568 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2569 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2570 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2571 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2572 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2573 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2577 if(!oglSurface.writingText)
2579 glDisable(GL_TEXTURE_2D);
2581 //glTranslatef(0.375f, 0.375f, 0.0f);
2583 else if(oglSurface.xOffset)
2584 glTranslatef(-oglSurface.xOffset / 64.0f/*+0.375f*/, 0.0f, 0.0f);
2586 #if !defined(__OLDX__)
2587 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2588 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2592 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2594 OGLSurface oglSurface = surface.driverData;
2596 //glTranslatef(-0.375f, -0.375f, 0.0f);
2598 //Logf("Stretch\n");
2600 #if !defined(__OLDX__)
2601 /*if(glBlendFuncSeparate)
2602 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2605 glEnable(GL_TEXTURE_2D);
2606 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2608 glColor4fv(oglSurface.bitmapMult);
2614 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2615 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2617 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2618 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2620 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2621 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2623 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2624 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2628 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2629 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2631 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2632 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2634 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2635 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2637 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2638 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2643 glDisable(GL_TEXTURE_2D);
2645 //glTranslatef(0.375f, 0.375f, 0.0f);
2646 #if !defined(__OLDX__)
2647 /*if(glBlendFuncSeparate)
2648 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2653 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2655 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2658 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2660 float s2dw,s2dh,d2sw,d2sh;
2661 bool flipX = false, flipY = false;
2663 //Logf("StretchDI\n");
2665 if(Sgn(w) != Sgn(sw))
2671 if(Sgn(h) != Sgn(sh))
2683 //Clip against the edges of the source
2686 dx+=(int)((0-sx) * s2dw);
2687 w-=(int)((0-sx) * s2dw);
2693 dy+=(int)((0-sy) * s2dh);
2694 h-=(int)((0-sy) * s2dh);
2699 if(sx+sw>bitmap.width-1)
2701 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2702 sw-=sx+sw-(bitmap.width-1)-1;
2704 if(sy+sh>(bitmap.height-1))
2706 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2707 sh-=sy+sh-(bitmap.height-1)-1;
2709 //Clip against the edges of the surfaceination
2710 if(dx<surface.box.left)
2713 sx+=(int)((surface.box.left-dx)*d2sw);
2714 sw-=(int)((surface.box.left-dx)*d2sw);
2715 w-=surface.box.left-dx;
2716 dx=surface.box.left;
2718 if(dy<surface.box.top)
2720 sy+=(int)((surface.box.top-dy)*d2sh);
2721 sh-=(int)((surface.box.top-dy)*d2sh);
2722 h-=surface.box.top-dy;
2725 if(dx+w>surface.box.right)
2727 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2728 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2729 w-=dx+w-surface.box.right-1;
2731 if(dy+h>surface.box.bottom)
2733 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2734 h-=dy+h-surface.box.bottom-1;
2736 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2738 dx += surface.offset.x;
2739 dy += surface.offset.y;
2741 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2743 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2744 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2745 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2746 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2747 glRasterPos2d(dx,dy);
2748 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2749 glPixelZoom(s2dw, -s2dh);
2750 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2751 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2752 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2756 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2760 //Clip against the edges of the source
2773 if(sx+w>bitmap.width-1)
2774 w-=sx+w-(bitmap.width-1)-1;
2775 if(sy+h>bitmap.height-1)
2776 h-=sy+h-(bitmap.height-1)-1;
2777 //Clip against the edges of the surfaceination
2778 if(dx<surface.box.left)
2781 sx+=surface.box.left-dx;
2782 w-=surface.box.left-dx;
2783 dx=surface.box.left;
2785 if(dy<surface.box.top)
2787 sy+=surface.box.top-dy;
2788 h-=surface.box.top-dy;
2791 if(dx+w>surface.box.right)
2793 //if(flip) sx+=dx+w-surface.box.right-1;
2794 w-=dx+w-surface.box.right-1;
2796 if(dy+h>surface.box.bottom)
2797 h-=dy+h-surface.box.bottom-1;
2801 dx += surface.offset.x;
2802 dy += surface.offset.y;
2804 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2806 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2807 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2808 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2809 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2810 glRasterPos2d(dx,dy);
2812 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2813 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2814 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2818 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2820 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2823 void UnloadFont(DisplaySystem displaySystem, Font font)
2825 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2828 Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
2831 OGLSystem oglSystem = displaySystem.driverData;
2832 oglSystem.loadingFont = true;
2833 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
2837 void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
2839 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
2842 void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
2844 OGLSurface oglSurface = surface.driverData;
2845 OGLSystem oglSystem = display.displaySystem.driverData;
2846 oglSystem.loadingFont = true;
2848 //glTranslatef(-0.375f, -0.375f, 0.0f);
2852 if(surface.textOpacity)
2855 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
2856 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2859 oglSurface.writingText = true;
2861 glEnable(GL_TEXTURE_2D);
2862 glColor4fv(oglSurface.foreground);
2864 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2865 oglSurface.writingText = false;
2866 oglSystem.loadingFont = false;
2868 glDisable(GL_TEXTURE_2D);
2870 //glTranslatef(0.375f, 0.375f, 0.0f);
2873 void TextFont(Display display, Surface surface, Font font)
2875 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2878 void TextOpacity(Display display, Surface surface, bool opaque)
2880 OGLSurface oglSurface = surface.driverData;
2881 oglSurface.opaqueText = opaque;
2884 void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
2886 OGLSurface oglSurface = surface.driverData;
2887 OGLSystem oglSystem = display.displaySystem.driverData;
2888 oglSystem.loadingFont = true;
2889 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
2890 oglSystem.loadingFont = false;
2893 void DrawingChar(Display display, Surface surface, char character)
2898 void LineStipple(Display display, Surface surface, uint32 stipple)
2900 //Logf("Stipple\n");
2904 #if defined(__ANDROID__)
2905 stippleEnabled = true;
2906 glesLineStipple(1, (uint16)stipple);
2908 glLineStipple(1, (uint16)stipple);
2909 glEnable(GL_LINE_STIPPLE);
2914 #if defined(__ANDROID__)
2915 stippleEnabled = false;
2916 glMatrixMode(GL_TEXTURE);
2918 glMatrixMode(GL_PROJECTION);
2919 glDisable(GL_TEXTURE_2D);
2921 glDisable(GL_LINE_STIPPLE);
2925 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
2926 void SetRenderState(Display display, RenderState state, uint value)
2928 OGLDisplay oglDisplay = display.driverData;
2929 //Logf("RenderState\n");
2935 glEnable(GL_MULTISAMPLE_ARB);
2937 glDisable(GL_MULTISAMPLE_ARB);
2940 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2943 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2946 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2947 oglDisplay.depthWrite = value;
2951 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2952 glFogfv(GL_FOG_COLOR, (float *)&color);
2956 glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
2959 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2963 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2964 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2969 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2974 #if defined(__WIN32__)
2975 wglSwapIntervalEXT(value ? 1 : 0);
2982 void SetLight(Display display, int id, Light light)
2984 //Logf("SetLight\n");
2988 Object lightObject = light.lightObject;
2989 float position[4] = { 0, 0, 0, 0 };
2990 float color[4] = { 0, 0, 0, 1 };
2992 glEnable(GL_LIGHT0 + id);
2994 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2995 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2996 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2999 if(!light.multiplier) light.multiplier = 1.0f;
3001 color[0] = light.diffuse.r * light.multiplier;
3002 color[1] = light.diffuse.g * light.multiplier;
3003 color[2] = light.diffuse.b * light.multiplier;
3004 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
3006 color[0] = light.ambient.r * light.multiplier;
3007 color[1] = light.ambient.g * light.multiplier;
3008 color[2] = light.ambient.b * light.multiplier;
3009 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
3010 color[0] = light.specular.r * light.multiplier;
3011 color[1] = light.specular.g * light.multiplier;
3012 color[2] = light.specular.b * light.multiplier;
3013 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
3017 Vector3D positionVector;
3018 if(light.flags.spot)
3020 if(lightObject.flags.root || !lightObject.parent)
3022 positionVector = lightObject.transform.position;
3023 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3027 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
3028 if(display.display3D.camera)
3029 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
3035 if(!light.direction.x && !light.direction.y && !light.direction.z)
3037 Vector3Df vector { 0,0,-1 };
3039 mat.RotationQuaternion(light.orientation);
3040 positionVector.MultMatrixf(vector, mat);
3044 positionVector = light.direction;
3049 position[0] = (float)positionVector.x;
3050 position[1] = (float)positionVector.y;
3051 position[2] = (float)positionVector.z;
3053 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3056 // Display Light Position
3057 glDisable(GL_LIGHTING);
3058 glDisable(GL_DEPTH_TEST);
3062 glVertex3fv(position);
3064 glEnable(GL_DEPTH_TEST);
3065 glEnable(GL_LIGHTING);
3069 if(lightObject.flags.root || !lightObject.parent)
3071 positionVector = light.target.transform.position;
3072 positionVector.Subtract(positionVector, display.camera.cPosition);
3076 positionVector.MultMatrix(light.target.transform.position,
3077 lightObject.light.target.parent.matrix);
3078 positionVector.Subtract(positionVector, display.camera.cPosition);
3081 position[0] = positionVector.x;
3082 position[1] = positionVector.y;
3083 position[2] = positionVector.z;
3085 glDisable(GL_LIGHTING);
3086 glDisable(GL_DEPTH_TEST);
3090 glVertex3fv(position);
3092 glEnable(GL_DEPTH_TEST);
3093 glEnable(GL_LIGHTING);
3096 if(light.flags.attenuation)
3098 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
3099 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
3100 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3103 if(light.flags.spot)
3106 #define MAXLIGHT 0.9
3107 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3108 // Figure out exponent out of the hot spot
3109 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3111 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3112 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3113 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3119 Vector3Df vector { 0,0,-1 };
3120 Vector3Df direction;
3123 mat.RotationQuaternion(light.orientation);
3124 direction.MultMatrix(vector, mat);
3126 position[0] = direction.x;
3127 position[1] = direction.y;
3128 position[2] = direction.z;
3130 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3134 glDisable(GL_LIGHT0 + id);
3137 void SetCamera(Display display, Surface surface, Camera camera)
3139 OGLDisplay oglDisplay = display.driverData;
3140 //Logf("SetCamera\n");
3144 int left = surface.box.left + surface.offset.x;
3145 int top = surface.box.top + surface.offset.y;
3146 int right = surface.box.right + surface.offset.x;
3147 int bottom = surface.box.bottom + surface.offset.y;
3148 float origX = surface.offset.x + camera.origin.x;
3149 float origY = surface.offset.y + camera.origin.y;
3151 int y = display.height - bottom - 1;
3152 int w = right - left + 1;
3153 int h = bottom - top + 1;
3156 glViewport(x, y, w, h);
3158 // *** Projection Matrix ***
3159 if(!display.display3D.camera)
3162 glMatrixMode(GL_PROJECTION);
3163 if(display.display3D.collectingHits)
3165 float pickX = display.display3D.pickX + surface.offset.x;
3166 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3170 w / display.display3D.pickWidth, 0, 0, 0,
3171 0, h / display.display3D.pickHeight, 0, 0,
3173 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3174 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3177 glLoadMatrixd(pickMatrix.array);
3182 (left - origX) * camera.zMin / camera.focalX,
3183 (right - origX) * camera.zMin / camera.focalX,
3184 (bottom - origY) * camera.zMin / camera.focalY,
3185 (top - origY) * camera.zMin / camera.focalY,
3186 camera.zMin, camera.zMax);
3188 glDisable(GL_BLEND);
3190 // *** Z Inverted Identity Matrix ***
3191 glMatrixMode(GL_MODELVIEW);
3192 if(!display.display3D.camera)
3196 glScalef(1.0f, 1.0f, -1.0f);
3198 // *** View Matrix ***
3199 glMultMatrixd(camera.viewMatrix.array);
3204 glEnable(GL_DEPTH_TEST);
3205 glEnable(GL_LIGHTING);
3206 glShadeModel(GL_SMOOTH);
3207 glDepthMask((byte)bool::true);
3208 oglDisplay.depthWrite = true;
3210 glEnable(GL_MULTISAMPLE_ARB);
3212 else if(display.display3D.camera)
3214 oglDisplay.depthWrite = false;
3215 glViewport(0,0,display.width,display.height);
3217 glDisable(GL_CULL_FACE);
3218 glDisable(GL_DEPTH_TEST);
3219 glDisable(GL_LIGHTING);
3221 glDisable(GL_TEXTURE_2D);
3222 glShadeModel(GL_FLAT);
3224 glDisable(GL_MULTISAMPLE_ARB);
3226 // *** Restore 2D MODELVIEW Matrix ***
3229 // *** Restore 2D PROJECTION Matrix ***
3230 glMatrixMode(GL_PROJECTION);
3235 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3238 void ApplyMaterial(Display display, Material material, Mesh mesh)
3240 //Logf("ApplyMaterial\n");
3243 if(material.flags.doubleSided)
3245 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3246 glDisable(GL_CULL_FACE);
3250 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3251 glEnable(GL_CULL_FACE);
3255 if(material.flags.noFog)
3261 if(material.baseMap && mesh.texCoords)
3263 Bitmap map = material.baseMap;
3264 glEnable(GL_TEXTURE_2D);
3265 glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
3267 if(material.flags.tile)
3269 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3270 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3275 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3279 glDisable(GL_TEXTURE_2D);
3281 if(mesh.flags.colors)
3283 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3284 glEnable(GL_COLOR_MATERIAL);
3288 glDisable(GL_COLOR_MATERIAL);
3290 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3291 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3294 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3295 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3299 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3300 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3303 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3304 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3307 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3310 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3312 OGLMesh oglMesh = mesh.data;
3315 if(!mesh.flags.vertices)
3317 if(oglMesh.vertices)
3319 glDeleteBuffersARB(1, &oglMesh.vertices);
3320 oglMesh.vertices = 0;
3322 delete mesh.vertices;
3324 if(!mesh.flags.normals)
3328 glDeleteBuffersARB(1, &oglMesh.normals);
3329 oglMesh.normals = 0;
3331 delete mesh.normals;
3333 if(!mesh.flags.texCoords1)
3335 if(oglMesh.texCoords)
3337 glDeleteBuffersARB(1, &oglMesh.texCoords);
3338 oglMesh.texCoords = 0;
3340 delete mesh.texCoords;
3342 if(!mesh.flags.texCoords2)
3344 if(oglMesh.texCoords2)
3346 glDeleteBuffersARB(1, &oglMesh.texCoords2);
3347 oglMesh.texCoords2 = 0;
3350 delete mesh.texCoords2;
3353 if(!mesh.flags.colors)
3357 glDeleteBuffersARB(1, &oglMesh.colors);
3369 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
3371 bool result = false;
3374 mesh.data = OGLMesh { };
3377 OGLMesh oglMesh = mesh.data;
3379 if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
3381 mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3383 glGenBuffersARB(1, &oglMesh.vertices);
3385 if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
3388 glGenBuffersARB( 1, &oglMesh.normals);
3389 mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3391 if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
3394 glGenBuffersARB( 1, &oglMesh.texCoords);
3395 mesh.texCoords = new Pointf[mesh.nVertices];
3397 if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
3400 glGenBuffersARB( 1, &oglMesh.colors);
3401 mesh.colors = new ColorRGBAf[mesh.nVertices];
3408 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3410 OGLMesh oglMesh = mesh.data;
3411 if(!flags) flags = mesh.flags;
3415 if(!(flags.vertices) || oglMesh.vertices)
3417 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3418 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3421 if(!(flags.normals) || oglMesh.normals)
3423 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3424 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
3427 if(!(flags.texCoords1) || oglMesh.texCoords)
3429 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3430 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3433 if(!(flags.colors) || oglMesh.colors)
3435 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3436 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3439 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3443 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3450 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3454 if(oglIndices.buffer)
3455 glDeleteBuffersARB(1, &oglIndices.buffer);
3456 delete oglIndices.indices;
3461 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3463 OGLIndices oglIndices = OGLIndices { };
3466 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3468 glGenBuffersARB( 1, &oglIndices.buffer);
3469 oglIndices.nIndices = nIndices;
3474 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3478 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3479 glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3480 oglIndices.indices, GL_STATIC_DRAW_ARB);
3481 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3485 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3488 return oglIndices.indices;
3491 void SelectMesh(Display display, Mesh mesh)
3493 //Logf("SelectMesh\n");
3495 #if !defined( __ANDROID__) && !defined(__APPLE__)
3496 if(display.display3D.mesh && glUnlockArraysEXT)
3497 glUnlockArraysEXT();
3501 OGLDisplay oglDisplay = display.driverData;
3502 OGLMesh oglMesh = mesh.data;
3504 // *** Vertex Stream ***
3505 glEnableClientState(GL_VERTEX_ARRAY);
3506 if(!display.display3D.collectingHits && oglMesh)
3509 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3510 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
3512 // *** Normals Stream ***
3515 glEnableClientState(GL_NORMAL_ARRAY);
3517 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3518 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
3521 glDisableClientState(GL_NORMAL_ARRAY);
3523 // *** Texture Coordinates Stream ***
3526 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3528 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3529 glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
3532 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3534 // *** Color Stream ***
3537 glEnableClientState(GL_COLOR_ARRAY);
3539 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3540 glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
3543 glDisableClientState(GL_COLOR_ARRAY);
3549 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3550 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3551 if(mesh.normals && !display.display3D.collectingHits)
3553 glEnableClientState(GL_NORMAL_ARRAY);
3554 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3557 glDisableClientState(GL_NORMAL_ARRAY);
3558 if(mesh.texCoords && !display.display3D.collectingHits)
3560 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3561 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3564 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3565 if(mesh.colors && !display.display3D.collectingHits)
3567 glEnableClientState(GL_COLOR_ARRAY);
3568 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3571 glDisableClientState(GL_COLOR_ARRAY);
3574 #if !defined(__ANDROID__) && !defined(__APPLE__)
3575 if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
3578 else if(glBindBufferARB)
3579 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3582 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3584 OGLDisplay oglDisplay = display.driverData;
3585 //Logf("DrawPrimitives\n");
3587 if(primitive->type.vertexRange)
3588 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3591 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3592 // HACK TO SPEED THINGS UP...
3593 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3596 glBegin(primitiveTypes[primitive->type.primitiveType]);
3599 OGLIndices oglIndices = primitive->data;
3600 MeshFeatures flags = mesh.flags;
3601 for(c = 0; c<primitive->nIndices; c++)
3603 short index = ((short *) oglIndices.indices)[c];
3604 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3605 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3606 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3607 glVertex3fv((float *)&mesh.vertices[index]);
3614 OGLIndices oglIndices = primitive->data;
3616 if(!display.display3D.collectingHits && glBindBufferARB && oglIndices)
3618 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3619 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3620 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3621 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3624 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3625 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices.indices);
3627 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3628 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, primitive->indices);
3633 void PushMatrix(Display display)
3638 void PopMatrix(Display display, bool setMatrix)
3643 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3645 Matrix matrix = transMatrix;
3646 Camera camera = useCamera ? display.display3D.camera : null;
3651 glScalef(1.0f, 1.0f, -1.0f);
3656 matrix.m[3][0] - camera.cPosition.x,
3657 matrix.m[3][1] - camera.cPosition.y,
3658 matrix.m[3][2] - camera.cPosition.z);
3670 glMultMatrixd(matrix.array);
3675 public void UseSingleGLContext(bool useSingle)
3677 useSingleGLContext = useSingle;