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,
450 EGLint w, h, dummy, format;
456 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
457 eglInitialize(display, 0, 0);
458 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
459 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
461 surface = eglCreateWindowSurface(display, config, window, null);
462 context = eglCreateContext(display, config, null, null);
464 if(!eglMakeCurrent(display, surface, surface, context))
467 eglQuerySurface(display, surface, EGL_WIDTH, &w);
468 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
470 eglDisplay = display;
471 eglContext = context;
472 eglSurface = surface;
476 glEnableClientState(GL_VERTEX_ARRAY);
478 // Initialize GL state.
479 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
480 glEnable(GL_CULL_FACE);
481 glShadeModel(GL_SMOOTH);
482 glDisable(GL_DEPTH_TEST);
484 glDisable(GL_CULL_FACE);
485 glDisable(GL_DEPTH_TEST);
487 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
490 glMatrixMode(GL_MODELVIEW);
491 glScalef(1.0f, 1.0f, -1.0f);
492 glMatrixMode(GL_PROJECTION);
493 glShadeModel(GL_FLAT);
495 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
496 glFogi(GL_FOG_MODE, GL_EXP);
497 glFogf(GL_FOG_DENSITY, 0);
498 glEnable(GL_NORMALIZE);
499 glDepthFunc(GL_LESS);
501 glDisable(GL_MULTISAMPLE_ARB);
505 glOrtho(0,w,h,0,0.0,1.0);
509 static void egl_term_display()
511 if(eglDisplay != EGL_NO_DISPLAY)
513 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
514 if(eglContext != EGL_NO_CONTEXT)
515 eglDestroyContext(eglDisplay, eglContext);
516 if(eglSurface != EGL_NO_SURFACE)
517 eglDestroySurface(eglDisplay, eglSurface);
518 eglTerminate(eglDisplay);
520 eglDisplay = EGL_NO_DISPLAY;
521 eglContext = EGL_NO_CONTEXT;
522 eglSurface = EGL_NO_SURFACE;
525 // OpenGL Immediate Mode Porting Kit
526 static int beginCount;
527 static int vertexCount;
528 static int normalCount;
529 static float *vertexPointer;
530 static float *normalPointer;
531 static GLenum beginMode;
532 static unsigned int beginBufferSize, normalBufferSize;
533 static int numVertexCoords = 2;
535 void glesRecti(int a, int b, int c, int d)
545 void glesBegin(GLenum mode)
552 normalBufferSize = beginBufferSize = 1024; // default number of vertices
553 vertexPointer = new float[beginBufferSize * 5];
554 normalPointer = new float[normalBufferSize * 3];
558 void glesTexCoord2f(float x, float y)
560 int count = vertexCount;
562 if(vertexCount + numVertexCoords > beginBufferSize)
564 beginBufferSize = beginBufferSize + beginBufferSize/2;
565 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
568 vertexPointer[count*(2+numVertexCoords) ] = x;
569 vertexPointer[count*(2+numVertexCoords)+1] = y;
572 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
574 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-4)*(2+numVertexCoords)];
575 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-4)*(2+numVertexCoords)+1];
577 vertexPointer[count*(2+numVertexCoords) ] = vertexPointer[(count-3)*(2+numVertexCoords)];
578 vertexPointer[count*(2+numVertexCoords)+1] = vertexPointer[(count-3)*(2+numVertexCoords)+1];
582 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
583 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
584 void glesTexCoord2fv(float * a) { glesTexCoord2f(a[0], a[1]); }
586 void glesVertex2f(float x, float y)
589 if(vertexCount + 4 > beginBufferSize)
591 beginBufferSize = beginBufferSize + beginBufferSize/2;
592 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
595 vertexPointer[vertexCount*4+2] = x;
596 vertexPointer[vertexCount*4+3] = y;
599 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
601 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
602 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
604 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
605 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
610 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
611 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
615 int mode = beginMode;
616 if(mode == GL_QUADS) mode = GL_TRIANGLES;
617 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
619 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
620 glTexCoordPointer(numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer);
621 glVertexPointer (numVertexCoords, GL_FLOAT, (numVertexCoords+2)*sizeof(float),vertexPointer+2);
622 if(normalCount && normalCount == vertexCount)
624 glEnableClientState(GL_NORMAL_ARRAY);
625 glNormalPointer (GL_FLOAT, 3*sizeof(float),normalPointer);
628 glDrawArrays(mode, 0, vertexCount);
630 glDisableClientState(GL_NORMAL_ARRAY);
631 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
636 static float *floatVPBuffer = null;
637 static short *shortVPBuffer = null;
638 static unsigned int shortVPSize = 0, floatVPSize = 0;
641 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
642 static unsigned short *shortBDBuffer = null;
643 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
645 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
650 if(numVertices*numCoords > shortVPSize)
652 shortVPSize = numVertices*numCoords;
653 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
655 for(i = 0; i < numVertices*numCoords; i++)
656 shortVPBuffer[i] = (short)pointer[i];
657 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
660 glVertexPointer(numCoords, GL_SHORT, stride, 0);
663 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
668 if(numVertices*numCoords > floatVPSize)
670 floatVPSize = numVertices*numCoords;
671 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
673 for(i = 0; i < numVertices*numCoords; i++)
674 floatVPBuffer[i] = (float)pointer[i];
675 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
678 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
681 void glesTexReuseIntVP(int numCoords)
683 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
686 void glesTexReuseDoubleVP(int numCoords)
688 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
691 void glesColor3f( float r, float g, float b )
693 glColor4f(r, g, b, 1.0f);
696 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
698 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
701 void glesColor4fv(float * a)
703 glColor4f(a[0], a[1], a[2], a[3]);
706 void glesBufferDatad(int target, int size, void * data, int usage)
708 int numElems = size/sizeof(double);
709 double * dblPtr = (double *)data;
711 if (numElems > floatVPSize)
713 floatVPSize = numElems;
714 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
716 for (i=0; i< numElems; i++)
717 floatVPBuffer[i] = (float)dblPtr[i];
719 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
722 void glesBufferDatai(int target, int size, void * data, int usage)
724 int numElems = size/sizeof(unsigned int);
725 unsigned int * pointer = (unsigned int *)data;
727 if (numElems > shortBDSize)
729 shortBDSize = numElems;
730 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
732 for (i=0; i< numElems; i++)
733 shortBDBuffer[i] = (unsigned short)pointer[i];
735 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
738 /// *** These might require an implementation to get things working ***
739 void glesLoadMatrixd( double * i )
743 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
744 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
745 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
746 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
751 void glesOrtho( double l, double r, double b, double t, double n, double f )
755 { (float)(2 / (r - l)), 0, 0, 0 },
756 { 0, (float)(2 / (t - b)), 0, 0 },
757 { 0, 0, (float)(-2 / (f - n)), 0 },
758 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
760 glMultMatrixf((float *)matrix);
763 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
764 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
765 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
767 void glesMultMatrixd( double * i )
771 (float)i[0], (float)i[1], (float)i[2], (float)i[3],
772 (float)i[4], (float)i[5], (float)i[6], (float)i[7],
773 (float)i[8], (float)i[9], (float)i[10], (float)i[11],
774 (float)i[12], (float)i[13], (float)i[14], (float)i[15]
779 // Need to do these...
780 void glesVertex3f( float x, float y, float z )
783 if(vertexCount + 4 > beginBufferSize)
785 beginBufferSize = beginBufferSize + beginBufferSize/2;
786 vertexPointer = renew vertexPointer float[beginBufferSize * 5];
789 vertexPointer[vertexCount*5+2] = x;
790 vertexPointer[vertexCount*5+3] = y;
791 vertexPointer[vertexCount*5+4] = z;
794 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
796 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-4)*5+2];
797 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-4)*5+3];
798 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-4)*5+4];
800 vertexPointer[vertexCount*5+2] = vertexPointer[(vertexCount-3)*5+2];
801 vertexPointer[vertexCount*5+3] = vertexPointer[(vertexCount-3)*5+3];
802 vertexPointer[vertexCount*5+4] = vertexPointer[(vertexCount-3)*5+4];
808 void glesVertex3d( double x, double y, double z ) { glesVertex3f((float)x, (float)y, (float)z); }
809 void glesVertex3fv( float* coords ) { glesVertex3f(coords[0], coords[1], coords[2]); }
811 void glesNormal3f(float x, float y, float z)
813 normalCount = vertexCount;
814 if(vertexCount + 4 > normalBufferSize)
816 normalBufferSize = normalBufferSize + normalBufferSize/2;
817 normalPointer = renew normalPointer float[normalBufferSize * 2];
820 normalPointer[normalCount*3+0] = x;
821 normalPointer[normalCount*3+1] = y;
822 normalPointer[normalCount*3+2] = z;
825 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
827 normalPointer[normalCount*3+0] = normalPointer[(normalCount-4)*3+0];
828 normalPointer[normalCount*3+1] = normalPointer[(normalCount-4)*3+1];
829 normalPointer[normalCount*3+2] = normalPointer[(normalCount-4)*3+2];
831 normalPointer[normalCount*3+0] = normalPointer[(normalCount-3)*3+0];
832 normalPointer[normalCount*3+1] = normalPointer[(normalCount-3)*3+1];
833 normalPointer[normalCount*3+2] = normalPointer[(normalCount-3)*3+2];
837 void glesNormal3fd(double x, double y, double z) { glesNormal3f((float)x, (float)y, (float)z); }
838 void glesNormal3fv(float * coords) { glesNormal3f(coords[0], coords[1], coords[2]); }
840 void glesColorMaterial(int a, int b)
842 PrintLn("glColorMaterial stub");
847 delete vertexPointer;
850 delete floatVPBuffer;
853 delete shortVPBuffer;
856 delete shortBDBuffer;
860 // *** We don't support stipple yet... ***
861 void glesLineStipple( int i, unsigned short j )
866 void glesFrustum( double l, double r, double b, double t, double n, double f )
868 float A = (float)((r + l) / (r - l));
869 float B = (float)((t + b) / (t - b));
870 float C = (float)(-(f + n) / (f - n));
871 float D = (float)(-2*f*n/(f-n));
874 { (float)(2*n / (r - l)), 0, 0, 0 },
875 { 0, (float)(2*n / (t - b)), 0, 0 },
879 glMultMatrixf((float *)matrix);
882 void glesLightModeli( unsigned int pname, int param )
884 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
885 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
888 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
889 void glFogi( unsigned int pname, int param ) { }
890 void glPolygonMode( unsigned int i, unsigned int j ) { }
893 // *** Picking won't be supported for now ***
894 void glPushName( unsigned int i ) { }
895 void glLoadName( unsigned int i ) { }
898 // Probably replace by regular glBlendFunc ...
899 void glBlendFuncSeparate(int a, int b, int c, int d) { }
901 // For direct pixel blitting...
902 void glRasterPos2d(double a, double b) { }
903 void glPixelZoom(float a, float b) { }
904 void glDrawPixels(int a, int b, int c, int d, void * e) { }
908 /* Non OpenGL ES friendly stuff
909 #undef GL_UNSIGNED_INT
915 #undef GL_POLYGON_STIPPLE
916 #undef GL_LINE_STIPPLE
919 #undef GL_ALL_ATTRIB_BITS
920 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
925 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
926 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
927 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
928 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
932 static int currentVertexBuffer;
934 bool GLSelectVBO(uint vbo)
936 if(currentVertexBuffer != vbo)
938 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
939 currentVertexBuffer = vbo;
945 void GLBindBuffer(int target, uint buffer)
948 glBindBuffer(target, buffer);
950 glBindBufferARB(target, buffer);
954 static int displayWidth, displayHeight;
956 #define GL_CLAMP_TO_EDGE 0x812F
958 static bool useSingleGLContext = false;
959 class OGLDisplay : struct
961 #if defined(__WIN32__)
972 byte * pboMemory1, * pboMemory2;
975 GLXContext glContext;
978 XShmSegmentInfo shminfo;
980 XShmSegmentInfo shminfoShape;
985 X11Picture windowPicture;
986 X11Picture pixmapPicture;
988 X11Picture shapePicture;
991 ColorAlpha * flippingBuffer;
992 int flipBufH, flipBufW;
997 class OGLSystem : struct
999 #if defined(__WIN32__)
1000 PIXELFORMATDESCRIPTOR pfd;
1006 XVisualInfo * visualInfo;
1007 GLXContext glContext;
1009 GLXPixmap dummyGLXPixmap;
1014 class OGLSurface : struct
1021 float foreground[4], background[4], bitmapMult[4];
1024 class OGLMesh : struct
1033 class OGLIndices : struct
1040 static int primitiveTypes[RenderPrimitiveType] =
1042 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
1048 class OpenGLDisplayDriver : DisplayDriver
1050 class_property(name) = "OpenGL";
1052 bool LockSystem(DisplaySystem displaySystem)
1054 OGLSystem oglSystem = displaySystem.driverData;
1055 if(useSingleGLContext) return true;
1056 #if defined(__WIN32__)
1057 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1058 #elif defined(__unix__)
1059 //if(previous) return true;
1060 // printf("Making SYSTEM current\n");
1061 /*#if defined(__APPLE__)
1062 //glXMakeCurrent(xGlobalDisplay, displaySystem.window, oglSystem.glContext);
1064 #if defined(__ANDROID__)
1066 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /-*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
1069 //previous = oglSystem.glContext;
1074 void UnlockSystem(DisplaySystem displaySystem)
1076 if(useSingleGLContext) return;
1077 #if defined(__WIN32__)
1078 wglMakeCurrent(null, null);
1079 #elif defined(__unix__)
1080 // printf("Making NULL current\n");
1081 #if defined(__ANDROID__)
1083 glXMakeCurrent(xGlobalDisplay, None, null);
1089 bool Lock(Display display)
1091 OGLDisplay oglDisplay = display.driverData;
1092 OGLSystem oglSystem = display.displaySystem.driverData;
1094 if(useSingleGLContext) return true;
1095 #if defined(__WIN32__)
1096 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1097 #elif defined(__unix__)
1098 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
1099 // printf(" Making DISPLAY current\n");
1100 #if defined(__ANDROID__)
1102 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1108 void Unlock(Display display)
1110 if(useSingleGLContext) return;
1111 //printf(" Making NULL current\n");
1112 //glXMakeCurrent(xGlobalDisplay, None, null);
1114 LockSystem(display.displaySystem);
1117 void DestroyDisplay(Display display)
1119 OGLDisplay oglDisplay = display.driverData;
1123 #if defined(__WIN32__)
1124 wglMakeCurrent( null, null );
1127 wglDeleteContext(oglDisplay.glrc);
1129 if(oglDisplay.hdc && oglDisplay.pBuffer)
1130 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1132 if(oglDisplay.pBuffer)
1133 wglDestroyPbufferARB(oglDisplay.pBuffer);
1136 ReleaseDC(display.window, oglDisplay.hdc);
1138 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1139 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1141 #elif defined(__unix__)
1142 #if defined(__ANDROID__)
1144 if(oglDisplay.shapePixmap)
1145 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1146 if(oglDisplay.pixmap)
1147 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1148 if(oglDisplay.image)
1150 if(oglDisplay.shminfoShape.shmid != -1)
1152 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1153 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1154 shmdt(oglDisplay.shminfo.shmaddr);
1155 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1158 if(oglDisplay.shapeImage)
1160 if(oglDisplay.shminfoShape.shmid != -1)
1162 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1163 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1164 shmdt(oglDisplay.shminfoShape.shmaddr);
1165 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1167 XDestroyImage(oglDisplay.shapeImage);
1168 oglDisplay.shapeImage = None;
1171 glXMakeCurrent(xGlobalDisplay, None, null);
1173 if(oglDisplay.glContext)
1174 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1177 delete oglDisplay.flippingBuffer;
1179 display.driverData = null;
1183 bool CreateDisplaySystem(DisplaySystem displaySystem)
1185 bool result = false;
1186 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1189 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1191 oglSystem.hdc = GetDC(oglSystem.hwnd);
1195 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1196 oglSystem.pfd.nVersion = 1;
1197 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1198 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1199 oglSystem.pfd.cColorBits = 24;
1200 oglSystem.pfd.cAlphaBits = 8;
1201 oglSystem.pfd.cDepthBits = 24;
1202 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1204 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1205 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1207 if(oglSystem.pfd.cColorBits > 8)
1209 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1210 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1213 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1215 // Get Pointers To The GL Functions
1216 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1217 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1218 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1219 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1220 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1221 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1222 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1223 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1224 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1225 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1226 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1227 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1229 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1230 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1231 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1232 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1233 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1234 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1235 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1236 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1237 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1239 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1241 // eSystem_LoggingMode(LOG_MSGBOX, null);
1243 if(wglChoosePixelFormatARB)
1248 float fAttributes[] = {0,0};
1251 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1252 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1253 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1254 WGL_COLOR_BITS_ARB,24,
1255 WGL_ALPHA_BITS_ARB,8,
1256 WGL_DEPTH_BITS_ARB,16,
1257 WGL_STENCIL_BITS_ARB,0,
1258 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1259 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1260 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1264 //Log("Found wglChoosePixelFormatARB\n");
1266 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1267 if(!valid || !numFormats)
1269 //Log("Can't find 4x multi sampling\n");
1270 iAttributes[19] = 2;
1271 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1272 if(!valid || !numFormats)
1274 // Log("Can't find 2x multi sampling\n");
1275 iAttributes[16] = 0;
1276 iAttributes[17] = 0;
1277 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1280 if(valid && numFormats)
1282 oglSystem.format = pixelFormat;
1283 wglMakeCurrent(null, null);
1284 wglDeleteContext(oglSystem.glrc);
1286 // *** DescribePixelFormat does not support WGL pixel formats! ***
1287 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1288 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1289 //Log("Successfully set pixel format\n");
1291 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1292 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1296 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1300 wglMakeCurrent(null, null);
1302 //eSystem_DumpErrors(true);
1306 #elif defined(__unix__)
1307 #if defined(__ANDROID__)
1308 egl_init_display(guiApp.desktop.windowHandle);
1313 #ifndef ECERE_MINIGLX
1314 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1317 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1321 oglSystem.visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
1322 if(oglSystem.visualInfo)
1324 //printf("glXChooseVisual returnded a visual info\n");
1325 oglSystem.dummyPixmap = XCreatePixmap(xGlobalDisplay, (uint)displaySystem.window, 1, 1, oglSystem.visualInfo->depth);
1326 oglSystem.dummyGLXPixmap = glXCreateGLXPixmap(xGlobalDisplay, oglSystem.visualInfo, oglSystem.dummyPixmap);
1328 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1329 // printf("Creating system Context (%x)!\n", oglSystem.glContext);
1330 if(oglSystem.glContext)
1332 //printf("Got a Context\n");
1333 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /-*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
1337 glXMakeCurrent(xGlobalDisplay, None, null);
1345 displaySystem.flags.alpha = true;
1346 displaySystem.flags.flipping = true;
1347 displaySystem.pixelFormat = pixelFormat888;
1351 void DestroyDisplaySystem(DisplaySystem displaySystem)
1353 OGLSystem oglSystem = displaySystem.driverData;
1355 #if defined(__WIN32__)
1356 wglMakeCurrent( null, null );
1359 wglDeleteContext(oglSystem.glrc);
1362 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1363 DestroyWindow(oglSystem.hwnd);
1365 #elif defined(__unix__)
1366 #if defined(__ANDROID__)
1369 if(oglSystem.visualInfo)
1371 #ifdef ECERE_MINIGLX
1372 __miniglx_XFree(oglSystem.visualInfo);
1374 XFree(oglSystem.visualInfo);
1378 if(oglSystem.dummyGLXPixmap)
1379 glXDestroyGLXPixmap(xGlobalDisplay, oglSystem.dummyGLXPixmap);
1380 if(oglSystem.dummyPixmap);
1381 XFreePixmap(xGlobalDisplay, oglSystem.dummyPixmap);
1387 bool CreateDisplay(Display display)
1389 bool result = false;
1390 OGLDisplay oglDisplay = display.driverData;
1391 OGLSystem oglSystem = display.displaySystem.driverData;
1393 oglDisplay = display.driverData = OGLDisplay { };
1394 //printf("Inside CreateDisplay\n");
1396 #if defined(__WIN32__) || defined(USEPBUFFER)
1397 if(!display.alphaBlend)
1400 #if defined(__WIN32__)
1401 oglDisplay.hdc = GetDC(display.window);
1402 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1403 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1405 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1406 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1410 ReleaseDC(display.window, oglDisplay.hdc);
1411 #elif defined(__unix__)
1412 #if defined(__ANDROID__)
1414 XVisualInfo * visualInfo = null;
1418 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1419 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
1423 GLX_DOUBLEBUFFER, True,
1428 //visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
1429 visualInfo = ((XWindowData)display.windowDriverData).visual;
1432 GLXFBConfig *fbconfigs, fbconfig;
1434 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
1438 for (i = 0; i < numfbconfigs; i++)
1440 XRenderPictFormat * format;
1441 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
1442 if (!visualInfo) continue;
1443 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
1444 if (!format) continue;
1446 if(format->direct.alphaMask > 0)
1448 fbconfig = fbconfigs[i];
1451 //XFree(visualInfo);
1454 if (i == numfbconfigs)
1456 fbconfig = fbconfigs[0];
1457 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
1464 //printf("visualInfo is not null\n");
1465 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1466 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1467 //XFree(visualInfo);
1470 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1471 if(oglDisplay.glContext)
1473 //printf("CreateDisplay Got a Context\n");
1474 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1480 #if defined(__WIN32__) || defined(USEPBUFFER)
1486 #if !defined(__OLDX__)
1487 if(glBlendFuncSeparate)
1488 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1491 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1494 glMatrixMode(GL_MODELVIEW);
1495 glScalef(1.0f, 1.0f, -1.0f);
1496 // glTranslatef(0.375f, 0.375f, 0.0f);
1497 // glTranslatef(-0.625f, -0.625f, 0.0f);
1498 glMatrixMode(GL_PROJECTION);
1499 glShadeModel(GL_FLAT);
1501 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1502 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1503 glFogi(GL_FOG_MODE, GL_EXP);
1504 glFogf(GL_FOG_DENSITY, 0);
1505 glEnable(GL_NORMALIZE);
1506 glDepthFunc(GL_LESS);
1508 glDisable(GL_MULTISAMPLE_ARB);
1510 display.ambient = Color { 50,50,50 };
1512 if(!useSingleGLContext)
1513 #if defined(__WIN32__)
1514 wglMakeCurrent(null, null);
1515 #elif defined(__unix__)
1516 #if defined(__ANDROID__)
1519 glXMakeCurrent(xGlobalDisplay, None, null);
1526 bool DisplaySize(Display display, int width, int height)
1528 OGLDisplay oglDisplay = display.driverData;
1529 OGLSystem oglSystem = display.displaySystem.driverData;
1531 bool result = false;
1533 //printf("Inside DisplaySize\n");
1534 #if defined(__WIN32__) || defined(USEPBUFFER)
1535 if(display.alphaBlend)
1537 #if defined(__WIN32__)
1538 const int attributes[]=
1540 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1541 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1543 int pixelFormat = 0;
1544 if(wglChoosePixelFormatARB)
1548 float fAttributes[] = {0,0};
1551 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1552 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1553 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1554 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1555 WGL_COLOR_BITS_ARB,24,
1556 WGL_ALPHA_BITS_ARB,8,
1557 WGL_DEPTH_BITS_ARB,16,
1558 WGL_STENCIL_BITS_ARB,0,
1559 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1560 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1561 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1565 //Log("Found wglChoosePixelFormatARB\n");
1567 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1568 if(!valid || !numFormats)
1570 //Log("Can't find 4x multi sampling\n");
1571 iAttributes[19] = 2;
1572 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1573 if(!valid || !numFormats)
1575 // Log("Can't find 2x multi sampling\n");
1576 iAttributes[16] = 0;
1577 iAttributes[17] = 0;
1578 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1579 if(!valid || !numFormats)
1583 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1584 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1585 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1586 WGL_COLOR_BITS_ARB,24,
1587 WGL_ALPHA_BITS_ARB,8,
1588 WGL_DEPTH_BITS_ARB,16,
1591 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1595 if(valid && numFormats)
1597 wglMakeCurrent(null, null);
1601 wglMakeCurrent( null, null );
1602 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1603 if(oglDisplay.hdc && oglDisplay.pBuffer)
1604 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1606 wglDestroyPbufferARB(oglDisplay.pBuffer);
1608 if(!useSingleGLContext)
1609 wglMakeCurrent( null, null );
1612 wglDeleteContext(oglDisplay.glrc);
1614 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1615 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1616 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1619 HDC hdc = GetDC(display.window);
1621 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1622 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1624 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1625 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1627 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1629 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1633 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1634 oglDisplay.memDC = CreateCompatibleDC(hdc);
1635 SetMapMode(oglDisplay.memDC, MM_TEXT);
1636 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1637 info->bmiHeader.biPlanes = 1;
1638 info->bmiHeader.biCompression = BI_RGB;
1639 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1640 info->bmiHeader.biWidth = width;
1641 info->bmiHeader.biHeight = height;
1642 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1645 SelectObject(oglDisplay.memDC, newBitmap);
1646 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1649 PIXELFORMATDESCRIPTOR pfd = { 0 };
1650 pfd.nSize = (short)sizeof(pfd);
1652 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1653 pfd.iPixelType = PFD_TYPE_RGBA;
1654 pfd.cColorBits = 32;
1655 //pfd.cAlphaBits = 8;
1656 pfd.cDepthBits = 24;
1657 pfd.iLayerType = PFD_MAIN_PLANE;
1659 oglDisplay.hdc = oglDisplay.memDC;
1661 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1662 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1663 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1665 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1666 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1667 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1672 const int imageSize = width * height * 4;
1674 glGenBuffersARB(2, oglDisplay.imageBuffers);
1676 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1677 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1678 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1679 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1682 oglDisplay.memBitmap = newBitmap;
1683 oglDisplay.stride = width;
1689 ReleaseDC(display.window, hdc);
1691 #elif defined(__unix__)
1692 #if defined(__ANDROID__)
1697 GLX_DOUBLEBUFFER, True,
1703 GLX_STENCIL_SIZE, 1,
1704 //GLX_DEPTH_SIZE, 24,
1705 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1706 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1712 GLX_PBUFFER_WIDTH, width,
1713 GLX_PBUFFER_HEIGHT, height,
1714 GLX_LARGEST_PBUFFER, False,
1718 // choose a pixel format that meets our minimum requirements
1721 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1724 if(oglDisplay.pixmap)
1726 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1727 oglDisplay.pixmap = None;
1729 if(oglDisplay.shapePixmap)
1731 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1732 oglDisplay.shapePixmap = None;
1735 // Free Shared Memory Pixmap
1736 if(oglDisplay.image)
1738 if(oglDisplay.shminfoShape.shmid != -1)
1740 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1741 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1742 shmdt(oglDisplay.shminfo.shmaddr);
1743 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1745 XDestroyImage(oglDisplay.image);
1746 oglDisplay.image = None;
1748 if(oglDisplay.shapeImage)
1750 if(oglDisplay.shminfoShape.shmid != -1)
1752 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1753 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1754 shmdt(oglDisplay.shminfoShape.shmaddr);
1755 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1757 XDestroyImage(oglDisplay.shapeImage);
1758 oglDisplay.shapeImage = None;
1761 if(oglDisplay.windowPicture)
1762 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1763 if(oglDisplay.pixmapPicture)
1764 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1766 if(oglDisplay.pixmap)
1767 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1769 if(oglDisplay.glContext)
1770 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1771 if(oglDisplay.pBuffer)
1772 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1774 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1775 if(oglDisplay.pBuffer)
1777 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1778 if(oglDisplay.glContext)
1780 glXMakeCurrent(xGlobalDisplay, None, null);
1781 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1783 // Initialize Shared Memory Pixmap
1784 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1785 ZPixmap, null, &oglDisplay.shminfo, width, height);
1786 if(oglDisplay.image)
1788 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1789 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1790 if(oglDisplay.shminfo.shmid != -1)
1792 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1793 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1795 oglDisplay.shminfo.readOnly = False;
1796 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1798 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1799 &oglDisplay.shminfo, width, height, 32);
1801 // Initialize Shared Memory Shape Pixmap
1802 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1803 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1804 if(oglDisplay.shapeImage)
1806 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1807 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1808 if(oglDisplay.shminfoShape.shmid != -1)
1810 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1811 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1813 oglDisplay.shminfoShape.readOnly = False;
1814 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1816 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1817 &oglDisplay.shminfoShape, width, height, 1);
1818 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1821 XRenderPictureAttributes attributes = { 0 };
1822 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1823 #if !defined(__APPLE__) && !defined(__OLDX__)
1824 attributes.repeat = RepeatNormal;
1826 attributes.repeat = 1;
1828 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1829 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1830 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1831 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1834 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1835 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1852 CreateDisplay(display);
1853 #if defined(__WIN32__)
1854 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1855 #elif defined(__unix__)
1856 #if defined(__ANDROID__)
1860 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1867 if(!result && display.alphaBlend)
1869 printf("Alpha blending windows not supported on this display\n");
1876 glViewport(0,0,width,height);
1878 glOrtho(0,width,height,0,0.0,1.0);
1879 displayWidth = display.width = width;
1880 displayHeight = display.height = height;
1882 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1884 oglDisplay.flipBufW = width;
1885 oglDisplay.flipBufH = height;
1886 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1888 if(oglDisplay.flippingBuffer || !width || !height)
1894 void DisplayPosition(Display display, int x, int y)
1896 OGLDisplay oglDisplay = display.driverData;
1902 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1906 void RestorePalette(Display display)
1910 void StartUpdate(Display display)
1914 void EndUpdate(Display display)
1918 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1922 void Update(Display display, Box updateBox)
1924 OGLDisplay oglDisplay = display.driverData;
1925 //Logf("DisplayScreen\n");
1929 #if defined(__WIN32__) || defined(USEPBUFFER)
1930 if(display.alphaBlend)
1932 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1933 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1934 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1935 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1936 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1939 #if defined(__WIN32__)
1941 POINT point = { oglDisplay.x, oglDisplay.y};
1942 POINT srcPoint = { 0, 0 };
1943 BLENDFUNCTION blend = { 0 };
1945 size.cx = display.width;
1946 size.cy = display.height;
1947 blend.BlendOp = AC_SRC_OVER;
1948 blend.BlendFlags = 0;
1949 blend.SourceConstantAlpha = 255;
1950 blend.AlphaFormat = AC_SRC_ALPHA;
1953 // Process partial images. Mapping the buffer waits for
1954 // outstanding DMA transfers into the buffer to finish.
1955 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1956 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1958 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1959 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
1962 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
1963 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
1966 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
1969 // Unmap the image buffers
1970 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1971 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1973 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1974 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1976 // Bind two different buffer objects and start the glReadPixels
1977 // asynchronously. Each call will return directly after
1978 // starting the DMA transfer.
1979 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1980 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1982 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1983 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1987 #elif defined(__unix__)
1988 #if defined(__ANDROID__)
1990 XTransform transform =
1993 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
1994 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
1995 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
1998 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
1999 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2000 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
2001 #if !defined(__APPLE__) && !defined(__OLDX__)
2002 XShapeCombineMask(xGlobalDisplay, (uint)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2004 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
2006 XFlush(xGlobalDisplay);
2014 #if defined(__WIN32__)
2015 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
2016 SwapBuffers(oglDisplay.hdc);
2017 #elif defined(__unix__)
2018 #if defined(__ANDROID__)
2019 eglSwapBuffers(eglDisplay, eglSurface);
2021 glXSwapBuffers(xGlobalDisplay, (int)display.window);
2025 //Logf("Out of DisplayScreen\n");
2028 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
2030 glDeleteTextures(1, (int *)&bitmap.driverData);
2031 bitmap.driverData = 0;
2033 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
2036 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
2038 bool result = false;
2042 uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
2043 //uint w = pow2i(Min(width, 2048)), h = pow2i(Min(height, 2048));
2044 //uint w = pow2i(Min(width, 512)), h = pow2i(Min(height, 512));
2046 glGenTextures(1, &glBitmap);
2047 glBindTexture(GL_TEXTURE_2D, glBitmap);
2049 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2051 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2052 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2054 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2055 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2057 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2059 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
2061 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2062 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2066 bitmap.driverData = (void *)glBitmap;
2067 bitmap.driver = displaySystem.driver;
2075 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
2077 bool result = false;
2078 OGLSystem oglSystem = displaySystem.driverData;
2080 // Pre process the bitmap... First make it 32 bit
2081 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
2084 uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
2085 //uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
2088 // Switch ARGB to RGBA
2089 //if(bitmap.format != pixelFormatRGBA)
2091 for(c=0; c<bitmap.size; c++)
2093 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
2095 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
2096 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
2099 bitmap.pixelFormat = pixelFormat888;
2102 glGenTextures(1, &glBitmap);
2105 int error = glGetError();
2110 glBindTexture(GL_TEXTURE_2D, glBitmap);
2111 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
2113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2115 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2117 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2118 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2123 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2127 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2130 if(bitmap.width != w || bitmap.height != h)
2132 mipMap = Bitmap { };
2133 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
2135 Surface mipSurface = mipMap.GetSurface(0,0,null);
2136 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
2153 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2154 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2155 //printf("Calling glTexImage2D\n");
2156 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2157 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2158 if((error = glGetError()))
2160 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2161 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2165 if(mipMap != bitmap)
2170 if(!bitmap.keepData)
2171 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
2172 bitmap.driverData = (void *)glBitmap;
2173 bitmap.driver = displaySystem.driver;
2176 FreeBitmap(displaySystem, bitmap);
2177 else if(oglSystem.loadingFont)
2179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2181 oglSystem.loadingFont = false;
2187 void ReleaseSurface(Display display, Surface surface)
2189 glDisable(GL_SCISSOR_TEST);
2190 delete surface.driverData;
2191 surface.driverData = null;
2194 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2199 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2201 bool result = false;
2202 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2204 //Logf("GetSurface\n");
2208 if(displayWidth != display.width || displayHeight != display.height)
2210 displayWidth = display.width;
2211 displayHeight = display.height;
2213 glViewport(0,0,display.width,display.height);
2215 glOrtho(0,display.width,display.height,0,0.0,1.0);
2218 surface.offset.x = x;
2219 surface.offset.y = y;
2220 surface.unclippedBox = surface.box = clip;
2221 oglSurface.bitmapMult[0] = 1;
2222 oglSurface.bitmapMult[1] = 1;
2223 oglSurface.bitmapMult[2] = 1;
2224 oglSurface.bitmapMult[3] = 1;
2226 glEnable(GL_SCISSOR_TEST);
2229 (display.height) -(y+clip.bottom)-1,
2230 clip.right-clip.left+1,
2231 clip.bottom-clip.top+1);
2237 void Clip(Display display, Surface surface, Box clip)
2246 box.Clip(surface.unclippedBox);
2250 box = surface.box = surface.unclippedBox;
2251 box.left += surface.offset.x;
2252 box.top += surface.offset.y;
2253 box.right+= surface.offset.x;
2254 box.bottom += surface.offset.y;
2257 box.left,display.height - box.bottom - 1,
2258 box.right-box.left+1, box.bottom-box.top+1);
2261 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2263 bool result = false;
2264 OGLDisplay oglDisplay = display.driverData;
2265 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2267 if(oglDisplay.flippingBuffer)
2269 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2272 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2278 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2279 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2280 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2281 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2282 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2285 for(row = 0; row<h; row++)
2286 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2293 void SetForeground(Display display, Surface surface, ColorAlpha color)
2295 OGLSurface oglSurface = surface.driverData;
2297 //Logf("SetForeground\n");
2299 oglSurface.foreground[0] = color.color.r/255.0f;
2300 oglSurface.foreground[1] = color.color.g/255.0f;
2301 oglSurface.foreground[2] = color.color.b/255.0f;
2302 //oglSurface.foreground[3] = 1.0f;
2303 oglSurface.foreground[3] = color.a/255.0f;
2305 //if(!oglSurface.foreground[3])printf("bug");
2308 void SetBackground(Display display, Surface surface, ColorAlpha color)
2310 OGLSurface oglSurface = surface.driverData;
2312 //Logf("SetBackground\n");
2314 oglSurface.background[0] = color.color.r/255.0f;
2315 oglSurface.background[1] = color.color.g/255.0f;
2316 oglSurface.background[2] = color.color.b/255.0f;
2317 //oglSurface.background[3] = 1.0;
2318 oglSurface.background[3] = color.a/255.0f;
2321 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2323 OGLSurface oglSurface = surface.driverData;
2325 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2326 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2327 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2328 oglSurface.bitmapMult[3] = color.a/255.0f;
2331 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2336 void PutPixel(Display display, Surface surface,int x,int y)
2338 OGLSurface oglSurface = surface.driverData;
2340 //Logf("PutPixel\n");
2342 glColor4fv(oglSurface.foreground);
2344 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2345 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2350 void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
2352 OGLSurface oglSurface = surface.driverData;
2353 if(x1 == x2) { y2++; y1--; }
2354 else if(y1 == y2) { x2++; x1--; }
2358 glColor4fv(oglSurface.foreground);
2361 glVertex2i(x1+surface.offset.x, y1+surface.offset.y);
2362 glVertex2i(x2+surface.offset.x, y2+surface.offset.y);
2364 glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
2365 glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
2370 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2372 OGLSurface oglSurface = surface.driverData;
2374 //Logf("Rectangle\n");
2376 glColor4fv(oglSurface.foreground);
2377 glBegin(GL_LINE_LOOP);
2379 glVertex2i(x1+surface.offset.x, y1+surface.offset.y);
2380 glVertex2i(x1+surface.offset.x, y2+surface.offset.y);
2381 glVertex2i(x2+surface.offset.x, y2+surface.offset.y);
2382 glVertex2i(x2+surface.offset.x, y1+surface.offset.y);
2384 glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
2385 glVertex2f(x1+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
2386 glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
2387 glVertex2f(x2+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
2392 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2394 OGLSurface oglSurface = surface.driverData;
2397 glColor4fv(oglSurface.background);
2398 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2399 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2402 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2403 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2407 void Clear(Display display, Surface surface, ClearType type)
2409 OGLDisplay oglDisplay = display.driverData;
2410 OGLSurface oglSurface = surface.driverData;
2413 if(type != depthBuffer)
2414 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2415 if(type != colorBuffer && !oglDisplay.depthWrite)
2417 glDepthMask((byte)bool::true);
2419 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2420 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2421 if(type != colorBuffer && !oglDisplay.depthWrite)
2423 glDepthMask((byte)bool::false);
2427 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2432 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2434 OGLSurface oglSurface = surface.driverData;
2436 #if !defined(__OLDX__)
2437 // WHY DO WE HAVE GL_ONE HERE ?
2438 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2439 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2442 if(!oglSurface.writingText)
2444 // glTranslatef(-0.375f, -0.375f, 0.0f);
2445 glEnable(GL_TEXTURE_2D);
2446 glColor4fv(oglSurface.bitmapMult);
2448 else if(oglSurface.xOffset)
2449 glTranslatef(oglSurface.xOffset / 64.0f/*-0.375f*/, 0.0f, 0.0f);
2451 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2456 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2457 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2458 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2459 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2460 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2461 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2462 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2463 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2468 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2469 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2470 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2471 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2472 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2473 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2474 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2475 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2478 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2479 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2480 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2481 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2482 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2483 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2484 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2485 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2489 if(!oglSurface.writingText)
2491 glDisable(GL_TEXTURE_2D);
2493 //glTranslatef(0.375f, 0.375f, 0.0f);
2495 else if(oglSurface.xOffset)
2496 glTranslatef(-oglSurface.xOffset / 64.0f/*+0.375f*/, 0.0f, 0.0f);
2498 #if !defined(__OLDX__)
2499 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2500 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2504 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2506 OGLSurface oglSurface = surface.driverData;
2508 //glTranslatef(-0.375f, -0.375f, 0.0f);
2510 //Logf("Stretch\n");
2512 #if !defined(__OLDX__)
2513 /*if(glBlendFuncSeparate)
2514 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2517 glEnable(GL_TEXTURE_2D);
2518 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2520 glColor4fv(oglSurface.bitmapMult);
2526 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2527 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2529 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2530 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2532 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2533 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2535 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2536 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2540 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2541 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2543 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2544 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2546 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2547 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2549 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2550 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2555 glDisable(GL_TEXTURE_2D);
2557 //glTranslatef(0.375f, 0.375f, 0.0f);
2558 #if !defined(__OLDX__)
2559 /*if(glBlendFuncSeparate)
2560 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2565 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2567 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2570 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2572 float s2dw,s2dh,d2sw,d2sh;
2573 bool flipX = false, flipY = false;
2575 //Logf("StretchDI\n");
2577 if(Sgn(w) != Sgn(sw))
2583 if(Sgn(h) != Sgn(sh))
2595 //Clip against the edges of the source
2598 dx+=(int)((0-sx) * s2dw);
2599 w-=(int)((0-sx) * s2dw);
2605 dy+=(int)((0-sy) * s2dh);
2606 h-=(int)((0-sy) * s2dh);
2611 if(sx+sw>bitmap.width-1)
2613 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2614 sw-=sx+sw-(bitmap.width-1)-1;
2616 if(sy+sh>(bitmap.height-1))
2618 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2619 sh-=sy+sh-(bitmap.height-1)-1;
2621 //Clip against the edges of the surfaceination
2622 if(dx<surface.box.left)
2625 sx+=(int)((surface.box.left-dx)*d2sw);
2626 sw-=(int)((surface.box.left-dx)*d2sw);
2627 w-=surface.box.left-dx;
2628 dx=surface.box.left;
2630 if(dy<surface.box.top)
2632 sy+=(int)((surface.box.top-dy)*d2sh);
2633 sh-=(int)((surface.box.top-dy)*d2sh);
2634 h-=surface.box.top-dy;
2637 if(dx+w>surface.box.right)
2639 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2640 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2641 w-=dx+w-surface.box.right-1;
2643 if(dy+h>surface.box.bottom)
2645 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2646 h-=dy+h-surface.box.bottom-1;
2648 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2650 dx += surface.offset.x;
2651 dy += surface.offset.y;
2653 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2655 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2656 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2657 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2658 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2659 glRasterPos2d(dx,dy);
2660 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2661 glPixelZoom(s2dw, -s2dh);
2662 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2663 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2664 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2668 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2672 //Clip against the edges of the source
2685 if(sx+w>bitmap.width-1)
2686 w-=sx+w-(bitmap.width-1)-1;
2687 if(sy+h>bitmap.height-1)
2688 h-=sy+h-(bitmap.height-1)-1;
2689 //Clip against the edges of the surfaceination
2690 if(dx<surface.box.left)
2693 sx+=surface.box.left-dx;
2694 w-=surface.box.left-dx;
2695 dx=surface.box.left;
2697 if(dy<surface.box.top)
2699 sy+=surface.box.top-dy;
2700 h-=surface.box.top-dy;
2703 if(dx+w>surface.box.right)
2705 //if(flip) sx+=dx+w-surface.box.right-1;
2706 w-=dx+w-surface.box.right-1;
2708 if(dy+h>surface.box.bottom)
2709 h-=dy+h-surface.box.bottom-1;
2713 dx += surface.offset.x;
2714 dy += surface.offset.y;
2716 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2718 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2719 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2720 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2721 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2722 glRasterPos2d(dx,dy);
2724 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2725 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2726 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2730 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2732 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2735 void UnloadFont(DisplaySystem displaySystem, Font font)
2737 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2740 Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
2743 OGLSystem oglSystem = displaySystem.driverData;
2744 oglSystem.loadingFont = true;
2745 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
2749 void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
2751 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
2754 void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
2756 OGLSurface oglSurface = surface.driverData;
2757 OGLSystem oglSystem = display.displaySystem.driverData;
2758 oglSystem.loadingFont = true;
2760 //glTranslatef(-0.375f, -0.375f, 0.0f);
2764 if(surface.textOpacity)
2767 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
2768 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2771 oglSurface.writingText = true;
2773 glEnable(GL_TEXTURE_2D);
2774 glColor4fv(oglSurface.foreground);
2776 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2777 oglSurface.writingText = false;
2778 oglSystem.loadingFont = false;
2780 glDisable(GL_TEXTURE_2D);
2782 //glTranslatef(0.375f, 0.375f, 0.0f);
2785 void TextFont(Display display, Surface surface, Font font)
2787 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2790 void TextOpacity(Display display, Surface surface, bool opaque)
2792 OGLSurface oglSurface = surface.driverData;
2793 oglSurface.opaqueText = opaque;
2796 void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
2798 OGLSurface oglSurface = surface.driverData;
2799 OGLSystem oglSystem = display.displaySystem.driverData;
2800 oglSystem.loadingFont = true;
2801 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
2802 oglSystem.loadingFont = false;
2805 void DrawingChar(Display display, Surface surface, char character)
2810 void LineStipple(Display display, Surface surface, uint32 stipple)
2812 //Logf("Stipple\n");
2816 glLineStipple(1, (uint16)stipple);
2817 glEnable(GL_LINE_STIPPLE);
2820 glDisable(GL_LINE_STIPPLE);
2823 void SetRenderState(Display display, RenderState state, uint value)
2825 OGLDisplay oglDisplay = display.driverData;
2826 //Logf("RenderState\n");
2832 glEnable(GL_MULTISAMPLE_ARB);
2834 glDisable(GL_MULTISAMPLE_ARB);
2837 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2840 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2843 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2844 oglDisplay.depthWrite = value;
2848 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2849 glFogfv(GL_FOG_COLOR, (float *)&color);
2853 glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
2856 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2860 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2861 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2866 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2871 #if defined(__WIN32__)
2872 wglSwapIntervalEXT(value ? 1 : 0);
2879 void SetLight(Display display, int id, Light light)
2881 //Logf("SetLight\n");
2885 Object lightObject = light.lightObject;
2886 float position[4] = { 0, 0, 0, 0 };
2887 float color[4] = { 0, 0, 0, 1 };
2889 glEnable(GL_LIGHT0 + id);
2891 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2892 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2893 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2896 if(!light.multiplier) light.multiplier = 1.0f;
2898 color[0] = light.diffuse.r * light.multiplier;
2899 color[1] = light.diffuse.g * light.multiplier;
2900 color[2] = light.diffuse.b * light.multiplier;
2901 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2903 color[0] = light.ambient.r * light.multiplier;
2904 color[1] = light.ambient.g * light.multiplier;
2905 color[2] = light.ambient.b * light.multiplier;
2906 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2907 color[0] = light.specular.r * light.multiplier;
2908 color[1] = light.specular.g * light.multiplier;
2909 color[2] = light.specular.b * light.multiplier;
2910 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2914 Vector3D positionVector;
2915 if(light.flags.spot)
2917 if(lightObject.flags.root || !lightObject.parent)
2919 positionVector = lightObject.transform.position;
2920 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2924 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
2925 if(display.display3D.camera)
2926 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2932 if(!light.direction.x && !light.direction.y && !light.direction.z)
2934 Vector3Df vector { 0,0,-1 };
2936 mat.RotationQuaternion(light.orientation);
2937 positionVector.MultMatrixf(vector, mat);
2941 positionVector = light.direction;
2946 position[0] = (float)positionVector.x;
2947 position[1] = (float)positionVector.y;
2948 position[2] = (float)positionVector.z;
2950 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2953 // Display Light Position
2954 glDisable(GL_LIGHTING);
2955 glDisable(GL_DEPTH_TEST);
2959 glVertex3fv(position);
2961 glEnable(GL_DEPTH_TEST);
2962 glEnable(GL_LIGHTING);
2966 if(lightObject.flags.root || !lightObject.parent)
2968 positionVector = light.target.transform.position;
2969 positionVector.Subtract(positionVector, display.camera.cPosition);
2973 positionVector.MultMatrix(light.target.transform.position,
2974 lightObject.light.target.parent.matrix);
2975 positionVector.Subtract(positionVector, display.camera.cPosition);
2978 position[0] = positionVector.x;
2979 position[1] = positionVector.y;
2980 position[2] = positionVector.z;
2982 glDisable(GL_LIGHTING);
2983 glDisable(GL_DEPTH_TEST);
2987 glVertex3fv(position);
2989 glEnable(GL_DEPTH_TEST);
2990 glEnable(GL_LIGHTING);
2993 if(light.flags.attenuation)
2995 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
2996 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
2997 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
3000 if(light.flags.spot)
3003 #define MAXLIGHT 0.9
3004 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
3005 // Figure out exponent out of the hot spot
3006 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
3008 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
3009 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
3010 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
3016 Vector3Df vector { 0,0,-1 };
3017 Vector3Df direction;
3020 mat.RotationQuaternion(light.orientation);
3021 direction.MultMatrix(vector, mat);
3023 position[0] = direction.x;
3024 position[1] = direction.y;
3025 position[2] = direction.z;
3027 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
3031 glDisable(GL_LIGHT0 + id);
3034 void SetCamera(Display display, Surface surface, Camera camera)
3036 OGLDisplay oglDisplay = display.driverData;
3037 //Logf("SetCamera\n");
3041 int left = surface.box.left + surface.offset.x;
3042 int top = surface.box.top + surface.offset.y;
3043 int right = surface.box.right + surface.offset.x;
3044 int bottom = surface.box.bottom + surface.offset.y;
3045 float origX = surface.offset.x + camera.origin.x;
3046 float origY = surface.offset.y + camera.origin.y;
3048 int y = display.height - bottom - 1;
3049 int w = right - left + 1;
3050 int h = bottom - top + 1;
3053 glViewport(x, y, w, h);
3055 // *** Projection Matrix ***
3056 if(!display.display3D.camera)
3059 glMatrixMode(GL_PROJECTION);
3060 if(display.display3D.collectingHits)
3062 float pickX = display.display3D.pickX + surface.offset.x;
3063 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
3067 w / display.display3D.pickWidth, 0, 0, 0,
3068 0, h / display.display3D.pickHeight, 0, 0,
3070 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
3071 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
3074 glLoadMatrixd(pickMatrix.array);
3079 (left - origX) * camera.zMin / camera.focalX,
3080 (right - origX) * camera.zMin / camera.focalX,
3081 (bottom - origY) * camera.zMin / camera.focalY,
3082 (top - origY) * camera.zMin / camera.focalY,
3083 camera.zMin, camera.zMax);
3085 glDisable(GL_BLEND);
3087 // *** Z Inverted Identity Matrix ***
3088 glMatrixMode(GL_MODELVIEW);
3089 if(!display.display3D.camera)
3093 glScalef(1.0f, 1.0f, -1.0f);
3095 // *** View Matrix ***
3096 glMultMatrixd(camera.viewMatrix.array);
3101 glEnable(GL_DEPTH_TEST);
3102 glEnable(GL_LIGHTING);
3103 glShadeModel(GL_SMOOTH);
3104 glDepthMask((byte)bool::true);
3105 oglDisplay.depthWrite = true;
3107 glEnable(GL_MULTISAMPLE_ARB);
3109 else if(display.display3D.camera)
3111 oglDisplay.depthWrite = false;
3112 glViewport(0,0,display.width,display.height);
3114 glDisable(GL_CULL_FACE);
3115 glDisable(GL_DEPTH_TEST);
3116 glDisable(GL_LIGHTING);
3118 glDisable(GL_TEXTURE_2D);
3119 glShadeModel(GL_FLAT);
3121 glDisable(GL_MULTISAMPLE_ARB);
3123 // *** Restore 2D MODELVIEW Matrix ***
3126 // *** Restore 2D PROJECTION Matrix ***
3127 glMatrixMode(GL_PROJECTION);
3132 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3135 void ApplyMaterial(Display display, Material material, Mesh mesh)
3137 //Logf("ApplyMaterial\n");
3140 if(material.flags.doubleSided)
3142 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3143 glDisable(GL_CULL_FACE);
3147 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3148 glEnable(GL_CULL_FACE);
3152 if(material.flags.noFog)
3158 if(material.baseMap && mesh.texCoords)
3160 Bitmap map = material.baseMap;
3161 glEnable(GL_TEXTURE_2D);
3162 glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
3164 if(material.flags.tile)
3166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3171 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3176 glDisable(GL_TEXTURE_2D);
3178 if(mesh.flags.colors)
3180 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3181 glEnable(GL_COLOR_MATERIAL);
3185 glDisable(GL_COLOR_MATERIAL);
3187 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3188 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3191 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3192 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3196 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3197 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3200 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3201 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3204 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3207 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3209 OGLMesh oglMesh = mesh.data;
3212 if(!mesh.flags.vertices)
3214 if(oglMesh.vertices)
3216 glDeleteBuffersARB(1, &oglMesh.vertices);
3217 oglMesh.vertices = 0;
3219 delete mesh.vertices;
3221 if(!mesh.flags.normals)
3225 glDeleteBuffersARB(1, &oglMesh.normals);
3226 oglMesh.normals = 0;
3228 delete mesh.normals;
3230 if(!mesh.flags.texCoords1)
3232 if(oglMesh.texCoords)
3234 glDeleteBuffersARB(1, &oglMesh.texCoords);
3235 oglMesh.texCoords = 0;
3237 delete mesh.texCoords;
3239 if(!mesh.flags.texCoords2)
3241 if(oglMesh.texCoords2)
3243 glDeleteBuffersARB(1, &oglMesh.texCoords2);
3244 oglMesh.texCoords2 = 0;
3247 delete mesh.texCoords2;
3250 if(!mesh.flags.colors)
3254 glDeleteBuffersARB(1, &oglMesh.colors);
3266 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
3268 bool result = false;
3271 mesh.data = OGLMesh { };
3274 OGLMesh oglMesh = mesh.data;
3276 if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
3278 mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3280 glGenBuffersARB(1, &oglMesh.vertices);
3282 if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
3285 glGenBuffersARB( 1, &oglMesh.normals);
3286 mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3288 if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
3291 glGenBuffersARB( 1, &oglMesh.texCoords);
3292 mesh.texCoords = new Pointf[mesh.nVertices];
3294 if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
3297 glGenBuffersARB( 1, &oglMesh.colors);
3298 mesh.colors = new ColorRGBAf[mesh.nVertices];
3305 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3307 OGLMesh oglMesh = mesh.data;
3308 if(!flags) flags = mesh.flags;
3312 if(!(flags.vertices) || oglMesh.vertices)
3314 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3315 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3318 if(!(flags.normals) || oglMesh.normals)
3320 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3321 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
3324 if(!(flags.texCoords1) || oglMesh.texCoords)
3326 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3327 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3330 if(!(flags.colors) || oglMesh.colors)
3332 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3333 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3336 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3340 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3347 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3351 if(oglIndices.buffer)
3352 glDeleteBuffersARB(1, &oglIndices.buffer);
3353 delete oglIndices.indices;
3358 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3360 OGLIndices oglIndices = OGLIndices { };
3363 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3365 glGenBuffersARB( 1, &oglIndices.buffer);
3366 oglIndices.nIndices = nIndices;
3371 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3375 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3376 glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3377 oglIndices.indices, GL_STATIC_DRAW_ARB);
3378 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3382 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3385 return oglIndices.indices;
3388 void SelectMesh(Display display, Mesh mesh)
3390 //Logf("SelectMesh\n");
3393 if(display.display3D.mesh && glUnlockArraysEXT)
3394 glUnlockArraysEXT();
3398 OGLDisplay oglDisplay = display.driverData;
3399 OGLMesh oglMesh = mesh.data;
3401 // *** Vertex Stream ***
3402 glEnableClientState(GL_VERTEX_ARRAY);
3403 if(!display.display3D.collectingHits && oglMesh)
3406 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3407 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
3409 // *** Normals Stream ***
3412 glEnableClientState(GL_NORMAL_ARRAY);
3414 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3415 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
3418 glDisableClientState(GL_NORMAL_ARRAY);
3420 // *** Texture Coordinates Stream ***
3423 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3425 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3426 glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
3429 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3431 // *** Color Stream ***
3434 glEnableClientState(GL_COLOR_ARRAY);
3436 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3437 glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
3440 glDisableClientState(GL_COLOR_ARRAY);
3446 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3447 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3448 if(mesh.normals && !display.display3D.collectingHits)
3450 glEnableClientState(GL_NORMAL_ARRAY);
3451 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3454 glDisableClientState(GL_NORMAL_ARRAY);
3455 if(mesh.texCoords && !display.display3D.collectingHits)
3457 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3458 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3461 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3462 if(mesh.colors && !display.display3D.collectingHits)
3464 glEnableClientState(GL_COLOR_ARRAY);
3465 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3468 glDisableClientState(GL_COLOR_ARRAY);
3472 if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
3475 else if(glBindBufferARB)
3476 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3479 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3481 OGLDisplay oglDisplay = display.driverData;
3482 //Logf("DrawPrimitives\n");
3484 if(primitive->type.vertexRange)
3485 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3488 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3489 // HACK TO SPEED THINGS UP...
3490 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3493 glBegin(primitiveTypes[primitive->type.primitiveType]);
3496 OGLIndices oglIndices = primitive->data;
3497 MeshFeatures flags = mesh.flags;
3498 for(c = 0; c<primitive->nIndices; c++)
3500 short index = ((short *) oglIndices.indices)[c];
3501 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3502 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3503 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3504 glVertex3fv((float *)&mesh.vertices[index]);
3511 OGLIndices oglIndices = primitive->data;
3513 if(!display.display3D.collectingHits && glBindBufferARB && oglIndices)
3515 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3516 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3517 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3518 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3521 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3522 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices.indices);
3524 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3525 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, primitive->indices);
3530 void PushMatrix(Display display)
3535 void PopMatrix(Display display, bool setMatrix)
3540 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3542 Matrix matrix = transMatrix;
3543 Camera camera = useCamera ? display.display3D.camera : null;
3548 glScalef(1.0f, 1.0f, -1.0f);
3553 matrix.m[3][0] - camera.cPosition.x,
3554 matrix.m[3][1] - camera.cPosition.y,
3555 matrix.m[3][2] - camera.cPosition.z);
3567 glMultMatrixd(matrix.array);
3571 public void UseSingleGLContext(bool useSingle)
3573 useSingleGLContext = useSingle;