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
401 #define glLoadMatrixd glesLoadMatrixd
402 #define glMultMatrixd glesMultMatrixd
403 #define glOrtho glesOrtho
404 #define glScaled glesScaled
405 #define glTranslated glesTranslated
406 #define glRotated glesRotated
407 #define glVertex3d glesVertex3d
408 #define glVertex3f glesVertex3f
409 #define glVertex3fv glesVertex3fv
413 #define GL_QUAD_STRIP 0
415 #define GL_UNSIGNED_INT 0
418 //#define GL_LINE_STIPPLE 0
419 #define GL_BGRA_EXT 0
420 #define GL_UNPACK_ROW_LENGTH 0
421 #define GL_UNPACK_SKIP_PIXELS 0
422 #define GL_UNPACK_SKIP_ROWS 0
424 #define GL_PACK_ROW_LENGTH 0
425 #define GL_PACK_SKIP_ROWS 0
426 #define GL_PACK_SKIP_PIXELS 0
428 static EGLDisplay eglDisplay;
429 static EGLSurface eglSurface;
430 static EGLContext eglContext;
431 static int eglWidth, eglHeight;
433 static bool egl_init_display(ANativeWindow* window)
435 const EGLint attribs[] =
437 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
443 EGLint w, h, dummy, format;
449 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
450 eglInitialize(display, 0, 0);
451 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
452 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
454 surface = eglCreateWindowSurface(display, config, window, null);
455 context = eglCreateContext(display, config, null, null);
457 if(!eglMakeCurrent(display, surface, surface, context))
460 eglQuerySurface(display, surface, EGL_WIDTH, &w);
461 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
463 eglDisplay = display;
464 eglContext = context;
465 eglSurface = surface;
469 glEnableClientState(GL_VERTEX_ARRAY);
471 // Initialize GL state.
472 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
473 glEnable(GL_CULL_FACE);
474 glShadeModel(GL_SMOOTH);
475 glDisable(GL_DEPTH_TEST);
477 glDisable(GL_CULL_FACE);
478 glDisable(GL_DEPTH_TEST);
480 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
483 glMatrixMode(GL_MODELVIEW);
484 glScalef(1.0f, 1.0f, -1.0f);
485 glMatrixMode(GL_PROJECTION);
486 glShadeModel(GL_FLAT);
488 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
489 glFogi(GL_FOG_MODE, GL_EXP);
490 glFogf(GL_FOG_DENSITY, 0);
491 glEnable(GL_NORMALIZE);
492 glDepthFunc(GL_LESS);
494 glDisable(GL_MULTISAMPLE_ARB);
498 glOrtho(0,w,h,0,0.0,1.0);
502 static void egl_term_display()
504 if(eglDisplay != EGL_NO_DISPLAY)
506 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
507 if(eglContext != EGL_NO_CONTEXT)
508 eglDestroyContext(eglDisplay, eglContext);
509 if(eglSurface != EGL_NO_SURFACE)
510 eglDestroySurface(eglDisplay, eglSurface);
511 eglTerminate(eglDisplay);
513 eglDisplay = EGL_NO_DISPLAY;
514 eglContext = EGL_NO_CONTEXT;
515 eglSurface = EGL_NO_SURFACE;
518 // OpenGL Immediate Mode Porting Kit
519 static int beginCount;
520 static int vertexCount;
521 static float *vertexPointer;
522 static GLenum beginMode;
523 static unsigned int beginBufferSize;
525 void glesRecti(int a, int b, int c, int d)
535 void glesBegin(GLenum mode)
542 beginBufferSize = 1024; // default number of vertices
543 vertexPointer = new float[beginBufferSize * 4];
547 void glesTexCoord2f(float x, float y)
549 int count = vertexCount;
551 if(vertexCount + 4 > beginBufferSize)
553 beginBufferSize = beginBufferSize + beginBufferSize/2;
554 vertexPointer = renew vertexPointer float[beginBufferSize * 4];
557 vertexPointer[count*4 ] = x;
558 vertexPointer[count*4+1] = y;
561 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
563 vertexPointer[count*4 ] = vertexPointer[(count-4)*4];
564 vertexPointer[count*4+1] = vertexPointer[(count-4)*4+1];
566 vertexPointer[count*4 ] = vertexPointer[(count-3)*4];
567 vertexPointer[count*4+1] = vertexPointer[(count-3)*4+1];
571 void glesTexCoord2i(int x, int y) { glesTexCoord2f((float)x, (float)y); }
572 void glesTexCoord2d(double x, double y) { glesTexCoord2f((float)x, (float)y); }
574 void glesVertex2f(float x, float y)
576 if(vertexCount + 4 > beginBufferSize)
578 beginBufferSize = beginBufferSize + beginBufferSize/2;
579 vertexPointer = renew vertexPointer float[beginBufferSize * 4];
582 vertexPointer[vertexCount*4+2] = x;
583 vertexPointer[vertexCount*4+3] = y;
586 if(beginMode == GL_QUADS && ((beginCount % 4) == 3))
588 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-4)*4+2];
589 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-4)*4+3];
591 vertexPointer[vertexCount*4+2] = vertexPointer[(vertexCount-3)*4+2];
592 vertexPointer[vertexCount*4+3] = vertexPointer[(vertexCount-3)*4+3];
597 void glesVertex2i(int x, int y) { glesVertex2f((float)x, (float)y); }
598 void glesVertex2d(double x, double y) { glesVertex2f((float)x, (float)y); }
602 int mode = beginMode;
603 if(mode == GL_QUADS) mode = GL_TRIANGLES;
604 else if(mode == GL_POLYGON) mode = GL_TRIANGLE_FAN;
606 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
607 glTexCoordPointer(2, GL_FLOAT, 2*2*sizeof(float),vertexPointer);
608 glVertexPointer (2, GL_FLOAT, 2*2*sizeof(float),vertexPointer+2);
609 glDrawArrays(mode, 0, vertexCount);
610 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
614 static float *floatVPBuffer = null;
615 static short *shortVPBuffer = null;
616 static unsigned int shortVPSize = 0, floatVPSize = 0;
619 //static float *floatVPBuffer = null; // For floats we reuse floatVPBuffer
620 static unsigned short *shortBDBuffer = null;
621 static unsigned int shortBDSize = 0/*, floatVPSize = 0*/;
623 void glesVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
628 if(numVertices*numCoords > shortVPSize)
630 shortVPSize = numVertices*numCoords;
631 shortVPBuffer = renew shortVPBuffer short[shortVPSize];
633 for(i = 0; i < numVertices*numCoords; i++)
634 shortVPBuffer[i] = (short)pointer[i];
635 glVertexPointer(numCoords, GL_SHORT, stride, shortVPBuffer);
638 glVertexPointer(numCoords, GL_SHORT, stride, 0);
641 void glesVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
646 if(numVertices*numCoords > floatVPSize)
648 floatVPSize = numVertices*numCoords;
649 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
651 for(i = 0; i < numVertices*numCoords; i++)
652 floatVPBuffer[i] = (float)pointer[i];
653 glVertexPointer(numCoords, GL_FLOAT, stride, floatVPBuffer);
656 glVertexPointer(numCoords, GL_FLOAT, stride, 0);
659 void glesTexReuseIntVP(int numCoords)
661 glTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
664 void glesTexReuseDoubleVP(int numCoords)
666 glTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
669 void glesColor3f( float r, float g, float b )
671 glColor4f(r, g, b, 1.0f);
674 void glesColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
676 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
679 void glesColor4fv(float * a)
681 glColor4f(a[0], a[1], a[2], a[3]);
684 void glesBufferDatad(int target, int size, void * data, int usage)
686 int numElems = size/sizeof(double);
687 double * dblPtr = (double *)data;
689 if (numElems > floatVPSize)
691 floatVPSize = numElems;
692 floatVPBuffer = renew floatVPBuffer float[floatVPSize];
694 for (i=0; i< numElems; i++)
695 floatVPBuffer[i] = (float)dblPtr[i];
697 glBufferData(target, numElems*sizeof(float), floatVPBuffer, usage);
700 void glesBufferDatai(int target, int size, void * data, int usage)
702 int numElems = size/sizeof(unsigned int);
703 unsigned int * pointer = (unsigned int *)data;
705 if (numElems > shortBDSize)
707 shortBDSize = numElems;
708 shortBDBuffer = renew shortBDBuffer uint16[shortBDSize];
710 for (i=0; i< numElems; i++)
711 shortBDBuffer[i] = (unsigned short)pointer[i];
713 glBufferData(target, numElems*sizeof(unsigned short), shortBDBuffer, usage);
716 /// *** These might require an implementation to get things working ***
717 void glesLoadMatrixd( double * i )
721 (float)i[0],(float)i[1],(float)i[2],(float)i[3],
722 (float)i[4],(float)i[5],(float)i[6],(float)i[7],
723 (float)i[8],(float)i[9],(float)i[10],(float)i[11],
724 (float)i[12],(float)i[13],(float)i[14],(float)i[15]
729 void glesOrtho( double l, double r, double b, double t, double n, double f )
733 { (float)(2 / (r - l)), 0, 0, 0 },
734 { 0, (float)(2 / (t - b)), 0, 0 },
735 { 0, 0, (float)(-2 / (f - n)), 0 },
736 { (float)(-(r + l) / (r - l)), (float)(-(t + b) / (t - b)), (float)(-(f + n) / (f - n)), 1 }
738 glMultMatrixf((float *)matrix);
741 void glesRotated( double a, double b, double c, double d ) { glRotatef((float)a, (float)b, (float)c, (float)d); }
742 void glesScaled( double a, double b, double c ) { glScalef((float)a, (float)b, (float)c); }
743 void glesTranslated( double a, double b, double c ) { glTranslatef((float)a, (float)b, (float)c); }
745 void glesMultMatrixd( double * i ) { }
746 void glesVertex3d( double x, double y, double z ) { }
747 void glesVertex3f( float x, float y, float z ) { }
748 void glesVertex3fv( float* coords ) { }
752 delete vertexPointer;
755 delete floatVPBuffer;
758 delete shortVPBuffer;
761 delete shortBDBuffer;
766 void glPushName( unsigned int i ) { }
767 void glLoadName( unsigned int i ) { }
769 void glLineStipple( int i, unsigned short j ) { }
770 void glPopAttrib() { }
771 void glPushAttrib( unsigned int i ) { }
772 void glBitmap( int w, int h, float xo, float yo, float xm, float ym, const unsigned int* bmp ) { }
773 void glCallLists( int n, unsigned int type, const void* lists ) { }
774 void glClearDepth( double depth ) { }
775 void glFogi( unsigned int pname, int param ) { }
776 void glFrustum( double a, double b, double c, double d, double e, double f ) { }
777 void glLightModeli( unsigned int pname, int param ) { }
778 void glListBase( unsigned int base ) { }
779 void glPolygonMode( unsigned int i, unsigned int j ) { }
780 void glRasterPos3f( float x, float y, float z ) { }
781 void glBlendFuncSeparate(int a, int b, int c, int d) { }
782 void glRasterPos2d(double a, double b) { }
783 void glPixelZoom(float a, float b) { }
784 void glDrawPixels(int a, int b, int c, int d, void * e) { }
785 void glColorMaterial(int a, int b) { }
786 void glUnlockArraysEXT() { }
787 void glLockArraysEXT(int a, int b) { }
788 void glNormal3fv(float * a) { }
789 void glTexCoord2fv(float * a) { }
793 /* Non OpenGL ES friendly stuff
794 #undef GL_UNSIGNED_INT
800 #undef GL_POLYGON_STIPPLE
801 #undef GL_LINE_STIPPLE
804 #undef GL_ALL_ATTRIB_BITS
805 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
810 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
811 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
812 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
813 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
817 static int currentVertexBuffer;
819 bool GLSelectVBO(uint vbo)
821 if(currentVertexBuffer != vbo)
823 GLBindBuffer(GL_ARRAY_BUFFER, vbo);
824 currentVertexBuffer = vbo;
830 void GLBindBuffer(int target, uint buffer)
833 glBindBuffer(target, buffer);
835 glBindBufferARB(target, buffer);
839 static int displayWidth, displayHeight;
841 #define GL_CLAMP_TO_EDGE 0x812F
843 static bool useSingleGLContext = false;
844 class OGLDisplay : struct
846 #if defined(__WIN32__)
857 byte * pboMemory1, * pboMemory2;
860 GLXContext glContext;
863 XShmSegmentInfo shminfo;
865 XShmSegmentInfo shminfoShape;
870 X11Picture windowPicture;
871 X11Picture pixmapPicture;
873 X11Picture shapePicture;
876 ColorAlpha * flippingBuffer;
877 int flipBufH, flipBufW;
882 class OGLSystem : struct
884 #if defined(__WIN32__)
885 PIXELFORMATDESCRIPTOR pfd;
891 XVisualInfo * visualInfo;
892 GLXContext glContext;
894 GLXPixmap dummyGLXPixmap;
899 class OGLSurface : struct
906 float foreground[4], background[4], bitmapMult[4];
909 class OGLMesh : struct
918 class OGLIndices : struct
925 static int primitiveTypes[RenderPrimitiveType] =
927 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
933 class OpenGLDisplayDriver : DisplayDriver
935 class_property(name) = "OpenGL";
937 bool LockSystem(DisplaySystem displaySystem)
939 OGLSystem oglSystem = displaySystem.driverData;
940 if(useSingleGLContext) return true;
941 #if defined(__WIN32__)
942 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
943 #elif defined(__unix__)
944 //if(previous) return true;
945 // printf("Making SYSTEM current\n");
946 /*#if defined(__APPLE__)
947 //glXMakeCurrent(xGlobalDisplay, displaySystem.window, oglSystem.glContext);
949 #if defined(__ANDROID__)
951 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /-*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
954 //previous = oglSystem.glContext;
959 void UnlockSystem(DisplaySystem displaySystem)
961 if(useSingleGLContext) return;
962 #if defined(__WIN32__)
963 wglMakeCurrent(null, null);
964 #elif defined(__unix__)
965 // printf("Making NULL current\n");
966 #if defined(__ANDROID__)
968 glXMakeCurrent(xGlobalDisplay, None, null);
974 bool Lock(Display display)
976 OGLDisplay oglDisplay = display.driverData;
977 OGLSystem oglSystem = display.displaySystem.driverData;
979 if(useSingleGLContext) return true;
980 #if defined(__WIN32__)
981 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
982 #elif defined(__unix__)
983 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
984 // printf(" Making DISPLAY current\n");
985 #if defined(__ANDROID__)
987 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
993 void Unlock(Display display)
995 if(useSingleGLContext) return;
996 //printf(" Making NULL current\n");
997 //glXMakeCurrent(xGlobalDisplay, None, null);
999 LockSystem(display.displaySystem);
1002 void DestroyDisplay(Display display)
1004 OGLDisplay oglDisplay = display.driverData;
1008 #if defined(__WIN32__)
1009 wglMakeCurrent( null, null );
1012 wglDeleteContext(oglDisplay.glrc);
1014 if(oglDisplay.hdc && oglDisplay.pBuffer)
1015 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1017 if(oglDisplay.pBuffer)
1018 wglDestroyPbufferARB(oglDisplay.pBuffer);
1021 ReleaseDC(display.window, oglDisplay.hdc);
1023 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1024 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1026 #elif defined(__unix__)
1027 #if defined(__ANDROID__)
1029 if(oglDisplay.shapePixmap)
1030 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1031 if(oglDisplay.pixmap)
1032 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1033 if(oglDisplay.image)
1035 if(oglDisplay.shminfoShape.shmid != -1)
1037 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1038 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1039 shmdt(oglDisplay.shminfo.shmaddr);
1040 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1043 if(oglDisplay.shapeImage)
1045 if(oglDisplay.shminfoShape.shmid != -1)
1047 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1048 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1049 shmdt(oglDisplay.shminfoShape.shmaddr);
1050 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1052 XDestroyImage(oglDisplay.shapeImage);
1053 oglDisplay.shapeImage = None;
1056 glXMakeCurrent(xGlobalDisplay, None, null);
1058 if(oglDisplay.glContext)
1059 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1062 delete oglDisplay.flippingBuffer;
1064 display.driverData = null;
1068 bool CreateDisplaySystem(DisplaySystem displaySystem)
1070 bool result = false;
1071 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
1074 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
1076 oglSystem.hdc = GetDC(oglSystem.hwnd);
1080 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
1081 oglSystem.pfd.nVersion = 1;
1082 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1083 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
1084 oglSystem.pfd.cColorBits = 24;
1085 oglSystem.pfd.cAlphaBits = 8;
1086 oglSystem.pfd.cDepthBits = 24;
1087 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
1089 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
1090 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1092 if(oglSystem.pfd.cColorBits > 8)
1094 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1095 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1098 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1100 // Get Pointers To The GL Functions
1101 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
1102 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
1103 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
1104 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
1105 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
1106 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
1107 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
1108 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
1109 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
1110 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
1111 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
1112 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
1114 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
1115 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
1116 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
1117 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
1118 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
1119 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
1120 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
1121 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
1122 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
1124 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
1126 // eSystem_LoggingMode(LOG_MSGBOX, null);
1128 if(wglChoosePixelFormatARB)
1133 float fAttributes[] = {0,0};
1136 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
1137 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1138 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1139 WGL_COLOR_BITS_ARB,24,
1140 WGL_ALPHA_BITS_ARB,8,
1141 WGL_DEPTH_BITS_ARB,16,
1142 WGL_STENCIL_BITS_ARB,0,
1143 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
1144 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1145 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1149 //Log("Found wglChoosePixelFormatARB\n");
1151 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1152 if(!valid || !numFormats)
1154 //Log("Can't find 4x multi sampling\n");
1155 iAttributes[19] = 2;
1156 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1157 if(!valid || !numFormats)
1159 // Log("Can't find 2x multi sampling\n");
1160 iAttributes[16] = 0;
1161 iAttributes[17] = 0;
1162 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1165 if(valid && numFormats)
1167 oglSystem.format = pixelFormat;
1168 wglMakeCurrent(null, null);
1169 wglDeleteContext(oglSystem.glrc);
1171 // *** DescribePixelFormat does not support WGL pixel formats! ***
1172 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
1173 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
1174 //Log("Successfully set pixel format\n");
1176 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
1177 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
1181 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
1185 wglMakeCurrent(null, null);
1187 //eSystem_DumpErrors(true);
1191 #elif defined(__unix__)
1192 #if defined(__ANDROID__)
1193 egl_init_display(guiApp.desktop.windowHandle);
1198 #ifndef ECERE_MINIGLX
1199 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
1202 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
1206 oglSystem.visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
1207 if(oglSystem.visualInfo)
1209 //printf("glXChooseVisual returnded a visual info\n");
1210 oglSystem.dummyPixmap = XCreatePixmap(xGlobalDisplay, (uint)displaySystem.window, 1, 1, oglSystem.visualInfo->depth);
1211 oglSystem.dummyGLXPixmap = glXCreateGLXPixmap(xGlobalDisplay, oglSystem.visualInfo, oglSystem.dummyPixmap);
1213 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
1214 // printf("Creating system Context (%x)!\n", oglSystem.glContext);
1215 if(oglSystem.glContext)
1217 //printf("Got a Context\n");
1218 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /-*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
1222 glXMakeCurrent(xGlobalDisplay, None, null);
1230 displaySystem.flags.alpha = true;
1231 displaySystem.flags.flipping = true;
1232 displaySystem.pixelFormat = pixelFormat888;
1236 void DestroyDisplaySystem(DisplaySystem displaySystem)
1238 OGLSystem oglSystem = displaySystem.driverData;
1240 #if defined(__WIN32__)
1241 wglMakeCurrent( null, null );
1244 wglDeleteContext(oglSystem.glrc);
1247 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
1248 DestroyWindow(oglSystem.hwnd);
1250 #elif defined(__unix__)
1251 #if defined(__ANDROID__)
1254 if(oglSystem.visualInfo)
1256 #ifdef ECERE_MINIGLX
1257 __miniglx_XFree(oglSystem.visualInfo);
1259 XFree(oglSystem.visualInfo);
1263 if(oglSystem.dummyGLXPixmap)
1264 glXDestroyGLXPixmap(xGlobalDisplay, oglSystem.dummyGLXPixmap);
1265 if(oglSystem.dummyPixmap);
1266 XFreePixmap(xGlobalDisplay, oglSystem.dummyPixmap);
1272 bool CreateDisplay(Display display)
1274 bool result = false;
1275 OGLDisplay oglDisplay = display.driverData;
1276 OGLSystem oglSystem = display.displaySystem.driverData;
1278 oglDisplay = display.driverData = OGLDisplay { };
1279 //printf("Inside CreateDisplay\n");
1281 #if defined(__WIN32__) || defined(USEPBUFFER)
1282 if(!display.alphaBlend)
1285 #if defined(__WIN32__)
1286 oglDisplay.hdc = GetDC(display.window);
1287 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1288 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1290 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1291 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1295 ReleaseDC(display.window, oglDisplay.hdc);
1296 #elif defined(__unix__)
1297 #if defined(__ANDROID__)
1299 XVisualInfo * visualInfo = null;
1303 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1304 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
1308 GLX_DOUBLEBUFFER, True,
1313 //visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
1314 visualInfo = ((XWindowData)display.windowDriverData).visual;
1317 GLXFBConfig *fbconfigs, fbconfig;
1319 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
1323 for (i = 0; i < numfbconfigs; i++)
1325 XRenderPictFormat * format;
1326 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
1327 if (!visualInfo) continue;
1328 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
1329 if (!format) continue;
1331 if(format->direct.alphaMask > 0)
1333 fbconfig = fbconfigs[i];
1336 //XFree(visualInfo);
1339 if (i == numfbconfigs)
1341 fbconfig = fbconfigs[0];
1342 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
1349 //printf("visualInfo is not null\n");
1350 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1351 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1352 //XFree(visualInfo);
1355 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1356 if(oglDisplay.glContext)
1358 //printf("CreateDisplay Got a Context\n");
1359 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1365 #if defined(__WIN32__) || defined(USEPBUFFER)
1371 #if !defined(__OLDX__)
1372 if(glBlendFuncSeparate)
1373 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1376 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1379 glMatrixMode(GL_MODELVIEW);
1380 glScalef(1.0f, 1.0f, -1.0f);
1381 // glTranslatef(0.375f, 0.375f, 0.0f);
1382 // glTranslatef(-0.625f, -0.625f, 0.0f);
1383 glMatrixMode(GL_PROJECTION);
1384 glShadeModel(GL_FLAT);
1386 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1387 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1388 glFogi(GL_FOG_MODE, GL_EXP);
1389 glFogf(GL_FOG_DENSITY, 0);
1390 glEnable(GL_NORMALIZE);
1391 glDepthFunc(GL_LESS);
1393 glDisable(GL_MULTISAMPLE_ARB);
1395 display.ambient = Color { 50,50,50 };
1397 if(!useSingleGLContext)
1398 #if defined(__WIN32__)
1399 wglMakeCurrent(null, null);
1400 #elif defined(__unix__)
1401 #if defined(__ANDROID__)
1404 glXMakeCurrent(xGlobalDisplay, None, null);
1411 bool DisplaySize(Display display, int width, int height)
1413 OGLDisplay oglDisplay = display.driverData;
1414 OGLSystem oglSystem = display.displaySystem.driverData;
1416 bool result = false;
1418 //printf("Inside DisplaySize\n");
1419 #if defined(__WIN32__) || defined(USEPBUFFER)
1420 if(display.alphaBlend)
1422 #if defined(__WIN32__)
1423 const int attributes[]=
1425 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1426 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1428 int pixelFormat = 0;
1429 if(wglChoosePixelFormatARB)
1433 float fAttributes[] = {0,0};
1436 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1437 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1438 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1439 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1440 WGL_COLOR_BITS_ARB,24,
1441 WGL_ALPHA_BITS_ARB,8,
1442 WGL_DEPTH_BITS_ARB,16,
1443 WGL_STENCIL_BITS_ARB,0,
1444 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1445 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1446 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1450 //Log("Found wglChoosePixelFormatARB\n");
1452 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1453 if(!valid || !numFormats)
1455 //Log("Can't find 4x multi sampling\n");
1456 iAttributes[19] = 2;
1457 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1458 if(!valid || !numFormats)
1460 // Log("Can't find 2x multi sampling\n");
1461 iAttributes[16] = 0;
1462 iAttributes[17] = 0;
1463 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1464 if(!valid || !numFormats)
1468 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1469 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1470 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1471 WGL_COLOR_BITS_ARB,24,
1472 WGL_ALPHA_BITS_ARB,8,
1473 WGL_DEPTH_BITS_ARB,16,
1476 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1480 if(valid && numFormats)
1482 wglMakeCurrent(null, null);
1486 wglMakeCurrent( null, null );
1487 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1488 if(oglDisplay.hdc && oglDisplay.pBuffer)
1489 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1491 wglDestroyPbufferARB(oglDisplay.pBuffer);
1493 if(!useSingleGLContext)
1494 wglMakeCurrent( null, null );
1497 wglDeleteContext(oglDisplay.glrc);
1499 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1500 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1501 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
1504 HDC hdc = GetDC(display.window);
1506 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1507 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1509 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1510 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1512 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1514 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1518 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1519 oglDisplay.memDC = CreateCompatibleDC(hdc);
1520 SetMapMode(oglDisplay.memDC, MM_TEXT);
1521 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1522 info->bmiHeader.biPlanes = 1;
1523 info->bmiHeader.biCompression = BI_RGB;
1524 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1525 info->bmiHeader.biWidth = width;
1526 info->bmiHeader.biHeight = height;
1527 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1530 SelectObject(oglDisplay.memDC, newBitmap);
1531 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1534 PIXELFORMATDESCRIPTOR pfd = { 0 };
1535 pfd.nSize = (short)sizeof(pfd);
1537 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1538 pfd.iPixelType = PFD_TYPE_RGBA;
1539 pfd.cColorBits = 32;
1540 //pfd.cAlphaBits = 8;
1541 pfd.cDepthBits = 24;
1542 pfd.iLayerType = PFD_MAIN_PLANE;
1544 oglDisplay.hdc = oglDisplay.memDC;
1546 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1547 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1548 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1550 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1551 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1552 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1557 const int imageSize = width * height * 4;
1559 glGenBuffersARB(2, oglDisplay.imageBuffers);
1561 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1562 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1563 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1564 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1567 oglDisplay.memBitmap = newBitmap;
1568 oglDisplay.stride = width;
1574 ReleaseDC(display.window, hdc);
1576 #elif defined(__unix__)
1577 #if defined(__ANDROID__)
1582 GLX_DOUBLEBUFFER, True,
1588 GLX_STENCIL_SIZE, 1,
1589 //GLX_DEPTH_SIZE, 24,
1590 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1591 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1597 GLX_PBUFFER_WIDTH, width,
1598 GLX_PBUFFER_HEIGHT, height,
1599 GLX_LARGEST_PBUFFER, False,
1603 // choose a pixel format that meets our minimum requirements
1606 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1609 if(oglDisplay.pixmap)
1611 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1612 oglDisplay.pixmap = None;
1614 if(oglDisplay.shapePixmap)
1616 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1617 oglDisplay.shapePixmap = None;
1620 // Free Shared Memory Pixmap
1621 if(oglDisplay.image)
1623 if(oglDisplay.shminfoShape.shmid != -1)
1625 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1626 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1627 shmdt(oglDisplay.shminfo.shmaddr);
1628 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1630 XDestroyImage(oglDisplay.image);
1631 oglDisplay.image = None;
1633 if(oglDisplay.shapeImage)
1635 if(oglDisplay.shminfoShape.shmid != -1)
1637 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1638 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1639 shmdt(oglDisplay.shminfoShape.shmaddr);
1640 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1642 XDestroyImage(oglDisplay.shapeImage);
1643 oglDisplay.shapeImage = None;
1646 if(oglDisplay.windowPicture)
1647 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1648 if(oglDisplay.pixmapPicture)
1649 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1651 if(oglDisplay.pixmap)
1652 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1654 if(oglDisplay.glContext)
1655 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1656 if(oglDisplay.pBuffer)
1657 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1659 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1660 if(oglDisplay.pBuffer)
1662 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1663 if(oglDisplay.glContext)
1665 glXMakeCurrent(xGlobalDisplay, None, null);
1666 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1668 // Initialize Shared Memory Pixmap
1669 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1670 ZPixmap, null, &oglDisplay.shminfo, width, height);
1671 if(oglDisplay.image)
1673 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1674 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1675 if(oglDisplay.shminfo.shmid != -1)
1677 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1678 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1680 oglDisplay.shminfo.readOnly = False;
1681 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1683 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1684 &oglDisplay.shminfo, width, height, 32);
1686 // Initialize Shared Memory Shape Pixmap
1687 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1688 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1689 if(oglDisplay.shapeImage)
1691 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1692 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1693 if(oglDisplay.shminfoShape.shmid != -1)
1695 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1696 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1698 oglDisplay.shminfoShape.readOnly = False;
1699 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1701 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1702 &oglDisplay.shminfoShape, width, height, 1);
1703 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1706 XRenderPictureAttributes attributes = { 0 };
1707 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1708 #if !defined(__APPLE__) && !defined(__OLDX__)
1709 attributes.repeat = RepeatNormal;
1711 attributes.repeat = 1;
1713 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1714 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1715 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1716 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1719 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1720 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1737 CreateDisplay(display);
1738 #if defined(__WIN32__)
1739 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1740 #elif defined(__unix__)
1741 #if defined(__ANDROID__)
1745 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1752 if(!result && display.alphaBlend)
1754 printf("Alpha blending windows not supported on this display\n");
1761 glViewport(0,0,width,height);
1763 glOrtho(0,width,height,0,0.0,1.0);
1764 displayWidth = display.width = width;
1765 displayHeight = display.height = height;
1767 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1769 oglDisplay.flipBufW = width;
1770 oglDisplay.flipBufH = height;
1771 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1773 if(oglDisplay.flippingBuffer)
1779 void DisplayPosition(Display display, int x, int y)
1781 OGLDisplay oglDisplay = display.driverData;
1787 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1791 void RestorePalette(Display display)
1795 void StartUpdate(Display display)
1799 void EndUpdate(Display display)
1803 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1807 void Update(Display display, Box updateBox)
1809 OGLDisplay oglDisplay = display.driverData;
1810 //Logf("DisplayScreen\n");
1814 #if defined(__WIN32__) || defined(USEPBUFFER)
1815 if(display.alphaBlend)
1817 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1818 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1819 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1820 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1821 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1824 #if defined(__WIN32__)
1826 POINT point = { oglDisplay.x, oglDisplay.y};
1827 POINT srcPoint = { 0, 0 };
1828 BLENDFUNCTION blend = { 0 };
1830 size.cx = display.width;
1831 size.cy = display.height;
1832 blend.BlendOp = AC_SRC_OVER;
1833 blend.BlendFlags = 0;
1834 blend.SourceConstantAlpha = 255;
1835 blend.AlphaFormat = AC_SRC_ALPHA;
1838 // Process partial images. Mapping the buffer waits for
1839 // outstanding DMA transfers into the buffer to finish.
1840 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1841 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1843 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1844 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
1847 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
1848 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
1851 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
1854 // Unmap the image buffers
1855 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1856 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1858 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1859 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1861 // Bind two different buffer objects and start the glReadPixels
1862 // asynchronously. Each call will return directly after
1863 // starting the DMA transfer.
1864 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1865 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1867 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1868 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1872 #elif defined(__unix__)
1873 #if defined(__ANDROID__)
1875 XTransform transform =
1878 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
1879 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
1880 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
1883 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
1884 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1885 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1886 #if !defined(__APPLE__) && !defined(__OLDX__)
1887 XShapeCombineMask(xGlobalDisplay, (uint)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1889 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1891 XFlush(xGlobalDisplay);
1899 #if defined(__WIN32__)
1900 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
1901 SwapBuffers(oglDisplay.hdc);
1902 #elif defined(__unix__)
1903 #if defined(__ANDROID__)
1904 eglSwapBuffers(eglDisplay, eglSurface);
1906 glXSwapBuffers(xGlobalDisplay, (int)display.window);
1910 //Logf("Out of DisplayScreen\n");
1913 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
1915 glDeleteTextures(1, (int *)&bitmap.driverData);
1916 bitmap.driverData = 0;
1918 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
1921 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
1923 bool result = false;
1927 uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
1928 //uint w = pow2i(Min(width, 2048)), h = pow2i(Min(height, 2048));
1929 //uint w = pow2i(Min(width, 512)), h = pow2i(Min(height, 512));
1931 glGenTextures(1, &glBitmap);
1932 glBindTexture(GL_TEXTURE_2D, glBitmap);
1934 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1936 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1937 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1939 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1940 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1942 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1944 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
1946 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1947 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1951 bitmap.driverData = (void *)glBitmap;
1952 bitmap.driver = displaySystem.driver;
1960 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
1962 bool result = false;
1963 OGLSystem oglSystem = displaySystem.driverData;
1965 // Pre process the bitmap... First make it 32 bit
1966 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
1969 uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
1970 //uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
1973 // Switch ARGB to RGBA
1974 //if(bitmap.format != pixelFormatRGBA)
1976 for(c=0; c<bitmap.size; c++)
1978 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
1980 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
1981 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
1984 bitmap.pixelFormat = pixelFormat888;
1987 glGenTextures(1, &glBitmap);
1990 int error = glGetError();
1995 glBindTexture(GL_TEXTURE_2D, glBitmap);
1996 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1998 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1999 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
2000 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2002 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2003 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2005 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2006 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2008 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
2012 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
2015 if(bitmap.width != w || bitmap.height != h)
2017 mipMap = Bitmap { };
2018 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
2020 Surface mipSurface = mipMap.GetSurface(0,0,null);
2021 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
2038 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2039 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
2040 //printf("Calling glTexImage2D\n");
2041 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
2042 //printf("width = %d (Should be %d, %d)\n", width, w, h);
2043 if((error = glGetError()))
2045 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
2046 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
2050 if(mipMap != bitmap)
2055 if(!bitmap.keepData)
2056 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
2057 bitmap.driverData = (void *)glBitmap;
2058 bitmap.driver = displaySystem.driver;
2061 FreeBitmap(displaySystem, bitmap);
2062 else if(oglSystem.loadingFont)
2064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2065 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2066 oglSystem.loadingFont = false;
2072 void ReleaseSurface(Display display, Surface surface)
2074 glDisable(GL_SCISSOR_TEST);
2075 delete surface.driverData;
2076 surface.driverData = null;
2079 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
2084 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
2086 bool result = false;
2087 OGLSurface oglSurface = surface.driverData = OGLSurface { };
2089 //Logf("GetSurface\n");
2093 if(displayWidth != display.width || displayHeight != display.height)
2095 displayWidth = display.width;
2096 displayHeight = display.height;
2098 glViewport(0,0,display.width,display.height);
2100 glOrtho(0,display.width,display.height,0,0.0,1.0);
2103 surface.offset.x = x;
2104 surface.offset.y = y;
2105 surface.unclippedBox = surface.box = clip;
2106 oglSurface.bitmapMult[0] = 1;
2107 oglSurface.bitmapMult[1] = 1;
2108 oglSurface.bitmapMult[2] = 1;
2109 oglSurface.bitmapMult[3] = 1;
2111 glEnable(GL_SCISSOR_TEST);
2114 (display.height) -(y+clip.bottom)-1,
2115 clip.right-clip.left+1,
2116 clip.bottom-clip.top+1);
2122 void Clip(Display display, Surface surface, Box clip)
2131 box.Clip(surface.unclippedBox);
2135 box = surface.box = surface.unclippedBox;
2136 box.left += surface.offset.x;
2137 box.top += surface.offset.y;
2138 box.right+= surface.offset.x;
2139 box.bottom += surface.offset.y;
2142 box.left,display.height - box.bottom - 1,
2143 box.right-box.left+1, box.bottom-box.top+1);
2146 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
2148 bool result = false;
2149 OGLDisplay oglDisplay = display.driverData;
2150 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
2152 if(oglDisplay.flippingBuffer)
2154 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
2157 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
2163 glPixelStorei(GL_PACK_ALIGNMENT, 4);
2164 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
2165 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
2166 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
2167 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
2170 for(row = 0; row<h; row++)
2171 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
2178 void SetForeground(Display display, Surface surface, ColorAlpha color)
2180 OGLSurface oglSurface = surface.driverData;
2182 //Logf("SetForeground\n");
2184 oglSurface.foreground[0] = color.color.r/255.0f;
2185 oglSurface.foreground[1] = color.color.g/255.0f;
2186 oglSurface.foreground[2] = color.color.b/255.0f;
2187 //oglSurface.foreground[3] = 1.0f;
2188 oglSurface.foreground[3] = color.a/255.0f;
2190 //if(!oglSurface.foreground[3])printf("bug");
2193 void SetBackground(Display display, Surface surface, ColorAlpha color)
2195 OGLSurface oglSurface = surface.driverData;
2197 //Logf("SetBackground\n");
2199 oglSurface.background[0] = color.color.r/255.0f;
2200 oglSurface.background[1] = color.color.g/255.0f;
2201 oglSurface.background[2] = color.color.b/255.0f;
2202 //oglSurface.background[3] = 1.0;
2203 oglSurface.background[3] = color.a/255.0f;
2206 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
2208 OGLSurface oglSurface = surface.driverData;
2210 oglSurface.bitmapMult[0] = color.color.r/255.0f;
2211 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2212 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2213 oglSurface.bitmapMult[3] = color.a/255.0f;
2216 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2221 void PutPixel(Display display, Surface surface,int x,int y)
2223 OGLSurface oglSurface = surface.driverData;
2225 //Logf("PutPixel\n");
2227 glColor4fv(oglSurface.foreground);
2229 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2230 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2235 void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
2237 OGLSurface oglSurface = surface.driverData;
2238 if(x1 == x2) { y2++; y1--; }
2239 else if(y1 == y2) { x2++; x1--; }
2243 glColor4fv(oglSurface.foreground);
2246 glVertex2i(x1+surface.offset.x, y1+surface.offset.y);
2247 glVertex2i(x2+surface.offset.x, y2+surface.offset.y);
2249 glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
2250 glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
2255 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2257 OGLSurface oglSurface = surface.driverData;
2259 //Logf("Rectangle\n");
2261 glColor4fv(oglSurface.foreground);
2262 glBegin(GL_LINE_LOOP);
2264 glVertex2i(x1+surface.offset.x, y1+surface.offset.y);
2265 glVertex2i(x1+surface.offset.x, y2+surface.offset.y);
2266 glVertex2i(x2+surface.offset.x, y2+surface.offset.y);
2267 glVertex2i(x2+surface.offset.x, y1+surface.offset.y);
2269 glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
2270 glVertex2f(x1+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
2271 glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
2272 glVertex2f(x2+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
2277 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2279 OGLSurface oglSurface = surface.driverData;
2282 glColor4fv(oglSurface.background);
2283 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2284 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2287 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2288 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2292 void Clear(Display display, Surface surface, ClearType type)
2294 OGLDisplay oglDisplay = display.driverData;
2295 OGLSurface oglSurface = surface.driverData;
2298 if(type != depthBuffer)
2299 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2300 if(type != colorBuffer && !oglDisplay.depthWrite)
2302 glDepthMask((byte)bool::true);
2304 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2305 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2306 if(type != colorBuffer && !oglDisplay.depthWrite)
2308 glDepthMask((byte)bool::false);
2312 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2317 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2319 OGLSurface oglSurface = surface.driverData;
2321 #if !defined(__OLDX__)
2322 // WHY DO WE HAVE GL_ONE HERE ?
2323 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2324 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2327 if(!oglSurface.writingText)
2329 // glTranslatef(-0.375f, -0.375f, 0.0f);
2330 glEnable(GL_TEXTURE_2D);
2331 glColor4fv(oglSurface.bitmapMult);
2333 else if(oglSurface.xOffset)
2334 glTranslatef(oglSurface.xOffset / 64.0f/*-0.375f*/, 0.0f, 0.0f);
2336 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2341 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2342 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2343 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2344 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2345 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2346 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2347 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2348 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2353 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2354 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2355 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2356 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2357 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2358 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2359 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2360 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2363 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2364 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2365 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2366 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2367 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2368 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2369 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2370 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2374 if(!oglSurface.writingText)
2376 glDisable(GL_TEXTURE_2D);
2378 //glTranslatef(0.375f, 0.375f, 0.0f);
2380 else if(oglSurface.xOffset)
2381 glTranslatef(-oglSurface.xOffset / 64.0f/*+0.375f*/, 0.0f, 0.0f);
2383 #if !defined(__OLDX__)
2384 /*if(glBlendFuncSeparate && !oglSurface.writingText)
2385 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2389 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2391 OGLSurface oglSurface = surface.driverData;
2393 //glTranslatef(-0.375f, -0.375f, 0.0f);
2395 //Logf("Stretch\n");
2397 #if !defined(__OLDX__)
2398 /*if(glBlendFuncSeparate)
2399 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2402 glEnable(GL_TEXTURE_2D);
2403 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
2405 glColor4fv(oglSurface.bitmapMult);
2411 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2412 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2414 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2415 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2417 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2418 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2420 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2421 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2425 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2426 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2428 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2429 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2431 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2432 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2434 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2435 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2440 glDisable(GL_TEXTURE_2D);
2442 //glTranslatef(0.375f, 0.375f, 0.0f);
2443 #if !defined(__OLDX__)
2444 /*if(glBlendFuncSeparate)
2445 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
2450 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2452 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2455 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2457 float s2dw,s2dh,d2sw,d2sh;
2458 bool flipX = false, flipY = false;
2460 //Logf("StretchDI\n");
2462 if(Sgn(w) != Sgn(sw))
2468 if(Sgn(h) != Sgn(sh))
2480 //Clip against the edges of the source
2483 dx+=(int)((0-sx) * s2dw);
2484 w-=(int)((0-sx) * s2dw);
2490 dy+=(int)((0-sy) * s2dh);
2491 h-=(int)((0-sy) * s2dh);
2496 if(sx+sw>bitmap.width-1)
2498 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2499 sw-=sx+sw-(bitmap.width-1)-1;
2501 if(sy+sh>(bitmap.height-1))
2503 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2504 sh-=sy+sh-(bitmap.height-1)-1;
2506 //Clip against the edges of the surfaceination
2507 if(dx<surface.box.left)
2510 sx+=(int)((surface.box.left-dx)*d2sw);
2511 sw-=(int)((surface.box.left-dx)*d2sw);
2512 w-=surface.box.left-dx;
2513 dx=surface.box.left;
2515 if(dy<surface.box.top)
2517 sy+=(int)((surface.box.top-dy)*d2sh);
2518 sh-=(int)((surface.box.top-dy)*d2sh);
2519 h-=surface.box.top-dy;
2522 if(dx+w>surface.box.right)
2524 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2525 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2526 w-=dx+w-surface.box.right-1;
2528 if(dy+h>surface.box.bottom)
2530 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2531 h-=dy+h-surface.box.bottom-1;
2533 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2535 dx += surface.offset.x;
2536 dy += surface.offset.y;
2538 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2540 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2541 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2542 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2543 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2544 glRasterPos2d(dx,dy);
2545 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2546 glPixelZoom(s2dw, -s2dh);
2547 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2548 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2549 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2553 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2557 //Clip against the edges of the source
2570 if(sx+w>bitmap.width-1)
2571 w-=sx+w-(bitmap.width-1)-1;
2572 if(sy+h>bitmap.height-1)
2573 h-=sy+h-(bitmap.height-1)-1;
2574 //Clip against the edges of the surfaceination
2575 if(dx<surface.box.left)
2578 sx+=surface.box.left-dx;
2579 w-=surface.box.left-dx;
2580 dx=surface.box.left;
2582 if(dy<surface.box.top)
2584 sy+=surface.box.top-dy;
2585 h-=surface.box.top-dy;
2588 if(dx+w>surface.box.right)
2590 //if(flip) sx+=dx+w-surface.box.right-1;
2591 w-=dx+w-surface.box.right-1;
2593 if(dy+h>surface.box.bottom)
2594 h-=dy+h-surface.box.bottom-1;
2598 dx += surface.offset.x;
2599 dy += surface.offset.y;
2601 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2603 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2604 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2605 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2606 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2607 glRasterPos2d(dx,dy);
2609 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2610 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2611 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2615 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2617 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2620 void UnloadFont(DisplaySystem displaySystem, Font font)
2622 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2625 Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
2628 OGLSystem oglSystem = displaySystem.driverData;
2629 oglSystem.loadingFont = true;
2630 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
2634 void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
2636 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
2639 void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
2641 OGLSurface oglSurface = surface.driverData;
2642 OGLSystem oglSystem = display.displaySystem.driverData;
2643 oglSystem.loadingFont = true;
2645 //glTranslatef(-0.375f, -0.375f, 0.0f);
2649 if(surface.textOpacity)
2652 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
2653 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2656 oglSurface.writingText = true;
2658 glEnable(GL_TEXTURE_2D);
2659 glColor4fv(oglSurface.foreground);
2661 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2662 oglSurface.writingText = false;
2663 oglSystem.loadingFont = false;
2665 glDisable(GL_TEXTURE_2D);
2667 //glTranslatef(0.375f, 0.375f, 0.0f);
2670 void TextFont(Display display, Surface surface, Font font)
2672 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2675 void TextOpacity(Display display, Surface surface, bool opaque)
2677 OGLSurface oglSurface = surface.driverData;
2678 oglSurface.opaqueText = opaque;
2681 void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
2683 OGLSurface oglSurface = surface.driverData;
2684 OGLSystem oglSystem = display.displaySystem.driverData;
2685 oglSystem.loadingFont = true;
2686 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
2687 oglSystem.loadingFont = false;
2690 void DrawingChar(Display display, Surface surface, char character)
2695 void LineStipple(Display display, Surface surface, uint32 stipple)
2697 //Logf("Stipple\n");
2701 glLineStipple(1, (uint16)stipple);
2702 glEnable(GL_LINE_STIPPLE);
2705 glDisable(GL_LINE_STIPPLE);
2708 void SetRenderState(Display display, RenderState state, uint value)
2710 OGLDisplay oglDisplay = display.driverData;
2711 //Logf("RenderState\n");
2717 glEnable(GL_MULTISAMPLE_ARB);
2719 glDisable(GL_MULTISAMPLE_ARB);
2722 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2725 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2728 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2729 oglDisplay.depthWrite = value;
2733 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2734 glFogfv(GL_FOG_COLOR, (float *)&color);
2738 glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
2741 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2745 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2746 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2751 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2756 #if defined(__WIN32__)
2757 wglSwapIntervalEXT(value ? 1 : 0);
2764 void SetLight(Display display, int id, Light light)
2766 //Logf("SetLight\n");
2770 Object lightObject = light.lightObject;
2771 float position[4] = { 0, 0, 0, 0 };
2772 float color[4] = { 0, 0, 0, 1 };
2774 glEnable(GL_LIGHT0 + id);
2776 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2777 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2778 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2781 if(!light.multiplier) light.multiplier = 1.0f;
2783 color[0] = light.diffuse.r * light.multiplier;
2784 color[1] = light.diffuse.g * light.multiplier;
2785 color[2] = light.diffuse.b * light.multiplier;
2786 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2788 color[0] = light.ambient.r * light.multiplier;
2789 color[1] = light.ambient.g * light.multiplier;
2790 color[2] = light.ambient.b * light.multiplier;
2791 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2792 color[0] = light.specular.r * light.multiplier;
2793 color[1] = light.specular.g * light.multiplier;
2794 color[2] = light.specular.b * light.multiplier;
2795 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2799 Vector3D positionVector;
2800 if(light.flags.spot)
2802 if(lightObject.flags.root || !lightObject.parent)
2804 positionVector = lightObject.transform.position;
2805 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2809 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
2810 if(display.display3D.camera)
2811 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2817 if(!light.direction.x && !light.direction.y && !light.direction.z)
2819 Vector3Df vector { 0,0,-1 };
2821 mat.RotationQuaternion(light.orientation);
2822 positionVector.MultMatrixf(vector, mat);
2826 positionVector = light.direction;
2831 position[0] = (float)positionVector.x;
2832 position[1] = (float)positionVector.y;
2833 position[2] = (float)positionVector.z;
2835 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2838 // Display Light Position
2839 glDisable(GL_LIGHTING);
2840 glDisable(GL_DEPTH_TEST);
2844 glVertex3fv(position);
2846 glEnable(GL_DEPTH_TEST);
2847 glEnable(GL_LIGHTING);
2851 if(lightObject.flags.root || !lightObject.parent)
2853 positionVector = light.target.transform.position;
2854 positionVector.Subtract(positionVector, display.camera.cPosition);
2858 positionVector.MultMatrix(light.target.transform.position,
2859 lightObject.light.target.parent.matrix);
2860 positionVector.Subtract(positionVector, display.camera.cPosition);
2863 position[0] = positionVector.x;
2864 position[1] = positionVector.y;
2865 position[2] = positionVector.z;
2867 glDisable(GL_LIGHTING);
2868 glDisable(GL_DEPTH_TEST);
2872 glVertex3fv(position);
2874 glEnable(GL_DEPTH_TEST);
2875 glEnable(GL_LIGHTING);
2878 if(light.flags.attenuation)
2880 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
2881 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
2882 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
2885 if(light.flags.spot)
2888 #define MAXLIGHT 0.9
2889 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
2890 // Figure out exponent out of the hot spot
2891 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
2893 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
2894 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
2895 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
2901 Vector3Df vector { 0,0,-1 };
2902 Vector3Df direction;
2905 mat.RotationQuaternion(light.orientation);
2906 direction.MultMatrix(vector, mat);
2908 position[0] = direction.x;
2909 position[1] = direction.y;
2910 position[2] = direction.z;
2912 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2916 glDisable(GL_LIGHT0 + id);
2919 void SetCamera(Display display, Surface surface, Camera camera)
2921 OGLDisplay oglDisplay = display.driverData;
2922 //Logf("SetCamera\n");
2926 int left = surface.box.left + surface.offset.x;
2927 int top = surface.box.top + surface.offset.y;
2928 int right = surface.box.right + surface.offset.x;
2929 int bottom = surface.box.bottom + surface.offset.y;
2930 float origX = surface.offset.x + camera.origin.x;
2931 float origY = surface.offset.y + camera.origin.y;
2933 int y = display.height - bottom - 1;
2934 int w = right - left + 1;
2935 int h = bottom - top + 1;
2938 glViewport(x, y, w, h);
2940 // *** Projection Matrix ***
2941 if(!display.display3D.camera)
2944 glMatrixMode(GL_PROJECTION);
2945 if(display.display3D.collectingHits)
2947 float pickX = display.display3D.pickX + surface.offset.x;
2948 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
2952 w / display.display3D.pickWidth, 0, 0, 0,
2953 0, h / display.display3D.pickHeight, 0, 0,
2955 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
2956 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
2959 glLoadMatrixd(pickMatrix.array);
2964 (left - origX) * camera.zMin / camera.focalX,
2965 (right - origX) * camera.zMin / camera.focalX,
2966 (bottom - origY) * camera.zMin / camera.focalY,
2967 (top - origY) * camera.zMin / camera.focalY,
2968 camera.zMin, camera.zMax);
2970 glDisable(GL_BLEND);
2972 // *** Z Inverted Identity Matrix ***
2973 glMatrixMode(GL_MODELVIEW);
2974 if(!display.display3D.camera)
2978 glScalef(1.0f, 1.0f, -1.0f);
2980 // *** View Matrix ***
2981 glMultMatrixd(camera.viewMatrix.array);
2986 glEnable(GL_DEPTH_TEST);
2987 glEnable(GL_LIGHTING);
2988 glShadeModel(GL_SMOOTH);
2989 glDepthMask((byte)bool::true);
2990 oglDisplay.depthWrite = true;
2992 glEnable(GL_MULTISAMPLE_ARB);
2994 else if(display.display3D.camera)
2996 oglDisplay.depthWrite = false;
2997 glViewport(0,0,display.width,display.height);
2999 glDisable(GL_CULL_FACE);
3000 glDisable(GL_DEPTH_TEST);
3001 glDisable(GL_LIGHTING);
3003 glDisable(GL_TEXTURE_2D);
3004 glShadeModel(GL_FLAT);
3006 glDisable(GL_MULTISAMPLE_ARB);
3008 // *** Restore 2D MODELVIEW Matrix ***
3011 // *** Restore 2D PROJECTION Matrix ***
3012 glMatrixMode(GL_PROJECTION);
3017 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3020 void ApplyMaterial(Display display, Material material, Mesh mesh)
3022 //Logf("ApplyMaterial\n");
3025 if(material.flags.doubleSided)
3027 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
3028 glDisable(GL_CULL_FACE);
3032 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
3033 glEnable(GL_CULL_FACE);
3037 if(material.flags.noFog)
3043 if(material.baseMap && mesh.texCoords)
3045 Bitmap map = material.baseMap;
3046 glEnable(GL_TEXTURE_2D);
3047 glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
3049 if(material.flags.tile)
3051 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3052 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3056 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
3057 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
3061 glDisable(GL_TEXTURE_2D);
3063 if(mesh.flags.colors)
3065 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
3066 glEnable(GL_COLOR_MATERIAL);
3070 glDisable(GL_COLOR_MATERIAL);
3072 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
3073 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
3076 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
3077 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
3081 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
3082 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
3085 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
3086 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
3089 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3092 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3094 OGLMesh oglMesh = mesh.data;
3097 if(!mesh.flags.vertices)
3099 if(oglMesh.vertices)
3101 glDeleteBuffersARB(1, &oglMesh.vertices);
3102 oglMesh.vertices = 0;
3104 delete mesh.vertices;
3106 if(!mesh.flags.normals)
3110 glDeleteBuffersARB(1, &oglMesh.normals);
3111 oglMesh.normals = 0;
3113 delete mesh.normals;
3115 if(!mesh.flags.texCoords1)
3117 if(oglMesh.texCoords)
3119 glDeleteBuffersARB(1, &oglMesh.texCoords);
3120 oglMesh.texCoords = 0;
3122 delete mesh.texCoords;
3124 if(!mesh.flags.texCoords2)
3126 if(oglMesh.texCoords2)
3128 glDeleteBuffersARB(1, &oglMesh.texCoords2);
3129 oglMesh.texCoords2 = 0;
3132 delete mesh.texCoords2;
3135 if(!mesh.flags.colors)
3139 glDeleteBuffersARB(1, &oglMesh.colors);
3151 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
3153 bool result = false;
3156 mesh.data = OGLMesh { };
3159 OGLMesh oglMesh = mesh.data;
3161 if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
3163 mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3165 glGenBuffersARB(1, &oglMesh.vertices);
3167 if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
3170 glGenBuffersARB( 1, &oglMesh.normals);
3171 mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
3173 if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
3176 glGenBuffersARB( 1, &oglMesh.texCoords);
3177 mesh.texCoords = new Pointf[mesh.nVertices];
3179 if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
3182 glGenBuffersARB( 1, &oglMesh.colors);
3183 mesh.colors = new ColorRGBAf[mesh.nVertices];
3190 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3192 OGLMesh oglMesh = mesh.data;
3193 if(!flags) flags = mesh.flags;
3197 if(!(flags.vertices) || oglMesh.vertices)
3199 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
3200 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
3203 if(!(flags.normals) || oglMesh.normals)
3205 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3206 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
3209 if(!(flags.texCoords1) || oglMesh.texCoords)
3211 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3212 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
3215 if(!(flags.colors) || oglMesh.colors)
3217 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3218 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
3221 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3225 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3232 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3236 if(oglIndices.buffer)
3237 glDeleteBuffersARB(1, &oglIndices.buffer);
3238 delete oglIndices.indices;
3243 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3245 OGLIndices oglIndices = OGLIndices { };
3248 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3250 glGenBuffersARB( 1, &oglIndices.buffer);
3251 oglIndices.nIndices = nIndices;
3256 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3260 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3261 glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3262 oglIndices.indices, GL_STATIC_DRAW_ARB);
3263 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3267 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3270 return oglIndices.indices;
3273 void SelectMesh(Display display, Mesh mesh)
3275 //Logf("SelectMesh\n");
3277 if(display.display3D.mesh && glUnlockArraysEXT)
3278 glUnlockArraysEXT();
3282 OGLDisplay oglDisplay = display.driverData;
3283 OGLMesh oglMesh = mesh.data;
3285 // *** Vertex Stream ***
3286 glEnableClientState(GL_VERTEX_ARRAY);
3287 if(!display.display3D.collectingHits && oglMesh)
3290 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
3291 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
3293 // *** Normals Stream ***
3296 glEnableClientState(GL_NORMAL_ARRAY);
3298 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
3299 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
3302 glDisableClientState(GL_NORMAL_ARRAY);
3304 // *** Texture Coordinates Stream ***
3307 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3309 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
3310 glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
3313 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3315 // *** Color Stream ***
3318 glEnableClientState(GL_COLOR_ARRAY);
3320 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
3321 glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
3324 glDisableClientState(GL_COLOR_ARRAY);
3330 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
3331 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
3332 if(mesh.normals && !display.display3D.collectingHits)
3334 glEnableClientState(GL_NORMAL_ARRAY);
3335 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
3338 glDisableClientState(GL_NORMAL_ARRAY);
3339 if(mesh.texCoords && !display.display3D.collectingHits)
3341 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3342 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
3345 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3346 if(mesh.colors && !display.display3D.collectingHits)
3348 glEnableClientState(GL_COLOR_ARRAY);
3349 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
3352 glDisableClientState(GL_COLOR_ARRAY);
3355 if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
3357 else if(glBindBufferARB)
3358 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
3361 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3363 OGLDisplay oglDisplay = display.driverData;
3364 //Logf("DrawPrimitives\n");
3366 if(primitive->type.vertexRange)
3367 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3370 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3371 // HACK TO SPEED THINGS UP...
3372 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3375 glBegin(primitiveTypes[primitive->type.primitiveType]);
3378 OGLIndices oglIndices = primitive->data;
3379 MeshFeatures flags = mesh.flags;
3380 for(c = 0; c<primitive->nIndices; c++)
3382 short index = ((short *) oglIndices.indices)[c];
3383 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3384 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3385 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3386 glVertex3fv((float *)&mesh.vertices[index]);
3393 OGLIndices oglIndices = primitive->data;
3395 if(!display.display3D.collectingHits && glBindBufferARB && oglIndices)
3397 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
3398 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3399 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
3400 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3403 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3404 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices.indices);
3406 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3407 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, primitive->indices);
3412 void PushMatrix(Display display)
3417 void PopMatrix(Display display, bool setMatrix)
3422 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3424 Matrix matrix = transMatrix;
3425 Camera camera = useCamera ? display.display3D.camera : null;
3430 glScalef(1.0f, 1.0f, -1.0f);
3435 matrix.m[3][0] - camera.cPosition.x,
3436 matrix.m[3][1] - camera.cPosition.y,
3437 matrix.m[3][2] - camera.cPosition.z);
3449 glMultMatrixd(matrix.array);
3453 public void UseSingleGLContext(bool useSingle)
3455 useSingleGLContext = useSingle;