3 namespace gfx::drivers;
6 #if defined(__unix__) || defined(__APPLE__)
8 #if !defined(__MINGW32__)
9 #define GL_GLEXT_PROTOTYPES
14 //#include <GL/miniglx.h>
18 #define property _property
22 #define Window X11Window
23 #define Cursor X11Cursor
25 #define Display X11Display
27 #define KeyCode X11KeyCode
28 #define Picture X11Picture
32 #include <X11/Xutil.h>
34 #include <X11/extensions/XShm.h>
37 #include <X11/extensions/Xrender.h>
38 #include <X11/extensions/shape.h>
56 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
58 #if defined(__WIN32__)
59 #define WIN32_LEAN_AND_MEAN
60 #define _WIN32_WINNT 0x0500
69 #if defined(__unix__) || defined(__APPLE__)
73 #define glLoadMatrix glLoadMatrixd
74 #define glMultMatrix glMultMatrixd
75 #define glGetMatrix glGetDoublev
76 #define glTranslate glTranslated
77 #define glScale glScaled
80 #define glVertex3v glVertex3dv
81 #define glNormal3v glNormal3dv
85 //#ifdef VERTEX_FORMAT_DOUBLE
87 #define glLoadMatrix glLoadMatrixd
88 #define glMultMatrix glMultMatrixd
89 #define glGetMatrix glGetDoublev
90 #define glVertex3v glVertex3dv
91 #define glNormal3v glNormal3dv
92 #define glTranslate glTranslated
93 #define glScale glScaled
94 //#define GL_VERTEX_FORMAT GL_DOUBLE
98 #define glLoadMatrix glLoadMatrixf
99 #define glMultMatrix glMultMatrixf
100 #define glGetMatrix glGetFloatv
101 #define glVertex3v glVertex3fv
102 #define glNormal3v glNormal3fv
103 #define glTranslate glTranslatef
104 #define glScale glScalef
105 //#define GL_VERTEX_FORMAT GL_FLOAT
110 #define GL_ARRAY_BUFFER_ARB 0x8892
111 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
112 #define GL_STATIC_DRAW_ARB 0x88E4
113 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
114 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
116 #define GL_MULTISAMPLE_ARB 0x809D
118 #if defined(__WIN32__)
120 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
121 #define WGL_SAMPLES_ARB 0x2042
123 #define WGL_WGLEXT_VERSION 1
124 #define WGL_FRONT_COLOR_BUFFER_BIT_ARB 0x00000001
125 #define WGL_BACK_COLOR_BUFFER_BIT_ARB 0x00000002
126 #define WGL_DEPTH_BUFFER_BIT_ARB 0x00000004
127 #define WGL_STENCIL_BUFFER_BIT_ARB 0x00000008
128 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
129 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
130 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
131 #define WGL_ACCELERATION_ARB 0x2003
132 #define WGL_NEED_PALETTE_ARB 0x2004
133 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
134 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
135 #define WGL_SWAP_METHOD_ARB 0x2007
136 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
137 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
138 #define WGL_TRANSPARENT_ARB 0x200A
139 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
140 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
141 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
142 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
143 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
144 #define WGL_SHARE_DEPTH_ARB 0x200C
145 #define WGL_SHARE_STENCIL_ARB 0x200D
146 #define WGL_SHARE_ACCUM_ARB 0x200E
147 #define WGL_SUPPORT_GDI_ARB 0x200F
148 #define WGL_SUPPORT_OPENGL_ARB 0x2010
149 #define WGL_DOUBLE_BUFFER_ARB 0x2011
150 #define WGL_STEREO_ARB 0x2012
151 #define WGL_PIXEL_TYPE_ARB 0x2013
152 #define WGL_COLOR_BITS_ARB 0x2014
153 #define WGL_RED_BITS_ARB 0x2015
154 #define WGL_RED_SHIFT_ARB 0x2016
155 #define WGL_GREEN_BITS_ARB 0x2017
156 #define WGL_GREEN_SHIFT_ARB 0x2018
157 #define WGL_BLUE_BITS_ARB 0x2019
158 #define WGL_BLUE_SHIFT_ARB 0x201A
159 #define WGL_ALPHA_BITS_ARB 0x201B
160 #define WGL_ALPHA_SHIFT_ARB 0x201C
161 #define WGL_ACCUM_BITS_ARB 0x201D
162 #define WGL_ACCUM_RED_BITS_ARB 0x201E
163 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
164 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
165 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
166 #define WGL_DEPTH_BITS_ARB 0x2022
167 #define WGL_STENCIL_BITS_ARB 0x2023
168 #define WGL_AUX_BUFFERS_ARB 0x2024
169 #define WGL_NO_ACCELERATION_ARB 0x2025
170 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
171 #define WGL_FULL_ACCELERATION_ARB 0x2027
172 #define WGL_SWAP_EXCHANGE_ARB 0x2028
173 #define WGL_SWAP_COPY_ARB 0x2029
174 #define WGL_SWAP_UNDEFINED_ARB 0x202A
175 #define WGL_TYPE_RGBA_ARB 0x202B
176 #define WGL_TYPE_COLORINDEX_ARB 0x202C
177 #define ERROR_INVALID_PIXEL_TYPE_ARB 0x2043
178 #define ERROR_INCOMPATIBLE_DEVICE_CONTEXTS_ARB 0x2054
179 #define WGL_DRAW_TO_PBUFFER_ARB 0x202D
180 #define WGL_MAX_PBUFFER_PIXELS_ARB 0x202E
181 #define WGL_MAX_PBUFFER_WIDTH_ARB 0x202F
182 #define WGL_MAX_PBUFFER_HEIGHT_ARB 0x2030
183 #define WGL_PBUFFER_LARGEST_ARB 0x2033
184 #define WGL_PBUFFER_WIDTH_ARB 0x2034
185 #define WGL_PBUFFER_HEIGHT_ARB 0x2035
186 #define WGL_PBUFFER_LOST_ARB 0x2036
187 #define ERROR_INVALID_PIXEL_TYPE_EXT 0x2043
188 #define WGL_NUMBER_PIXEL_FORMATS_EXT 0x2000
189 #define WGL_DRAW_TO_WINDOW_EXT 0x2001
190 #define WGL_DRAW_TO_BITMAP_EXT 0x2002
191 #define WGL_ACCELERATION_EXT 0x2003
192 #define WGL_NEED_PALETTE_EXT 0x2004
193 #define WGL_NEED_SYSTEM_PALETTE_EXT 0x2005
194 #define WGL_SWAP_LAYER_BUFFERS_EXT 0x2006
195 #define WGL_SWAP_METHOD_EXT 0x2007
196 #define WGL_NUMBER_OVERLAYS_EXT 0x2008
197 #define WGL_NUMBER_UNDERLAYS_EXT 0x2009
198 #define WGL_TRANSPARENT_EXT 0x200A
199 #define WGL_TRANSPARENT_VALUE_EXT 0x200B
200 #define WGL_SHARE_DEPTH_EXT 0x200C
201 #define WGL_SHARE_STENCIL_EXT 0x200D
202 #define WGL_SHARE_ACCUM_EXT 0x200E
203 #define WGL_SUPPORT_GDI_EXT 0x200F
204 #define WGL_SUPPORT_OPENGL_EXT 0x2010
205 #define WGL_DOUBLE_BUFFER_EXT 0x2011
206 #define WGL_STEREO_EXT 0x2012
207 #define WGL_PIXEL_TYPE_EXT 0x2013
208 #define WGL_COLOR_BITS_EXT 0x2014
209 #define WGL_RED_BITS_EXT 0x2015
210 #define WGL_RED_SHIFT_EXT 0x2016
211 #define WGL_GREEN_BITS_EXT 0x2017
212 #define WGL_GREEN_SHIFT_EXT 0x2018
213 #define WGL_BLUE_BITS_EXT 0x2019
214 #define WGL_BLUE_SHIFT_EXT 0x201A
215 #define WGL_ALPHA_BITS_EXT 0x201B
216 #define WGL_ALPHA_SHIFT_EXT 0x201C
217 #define WGL_ACCUM_BITS_EXT 0x201D
218 #define WGL_ACCUM_RED_BITS_EXT 0x201E
219 #define WGL_ACCUM_GREEN_BITS_EXT 0x201F
220 #define WGL_ACCUM_BLUE_BITS_EXT 0x2020
221 #define WGL_ACCUM_ALPHA_BITS_EXT 0x2021
222 #define WGL_DEPTH_BITS_EXT 0x2022
223 #define WGL_STENCIL_BITS_EXT 0x2023
224 #define WGL_AUX_BUFFERS_EXT 0x2024
225 #define WGL_NO_ACCELERATION_EXT 0x2025
226 #define WGL_GENERIC_ACCELERATION_EXT 0x2026
227 #define WGL_FULL_ACCELERATION_EXT 0x2027
228 #define WGL_SWAP_EXCHANGE_EXT 0x2028
229 #define WGL_SWAP_COPY_EXT 0x2029
230 #define WGL_SWAP_UNDEFINED_EXT 0x202A
231 #define WGL_TYPE_RGBA_EXT 0x202B
232 #define WGL_TYPE_COLORINDEX_EXT 0x202C
233 #define WGL_DRAW_TO_PBUFFER_EXT 0x202D
234 #define WGL_MAX_PBUFFER_PIXELS_EXT 0x202E
235 #define WGL_MAX_PBUFFER_WIDTH_EXT 0x202F
236 #define WGL_MAX_PBUFFER_HEIGHT_EXT 0x2030
237 #define WGL_OPTIMAL_PBUFFER_WIDTH_EXT 0x2031
238 #define WGL_OPTIMAL_PBUFFER_HEIGHT_EXT 0x2032
239 #define WGL_PBUFFER_LARGEST_EXT 0x2033
240 #define WGL_PBUFFER_WIDTH_EXT 0x2034
241 #define WGL_PBUFFER_HEIGHT_EXT 0x2035
242 #define WGL_DEPTH_FLOAT_EXT 0x2040
243 #define WGL_SAMPLE_BUFFERS_3DFX 0x2060
244 #define WGL_SAMPLES_3DFX 0x2061
245 #define WGL_SAMPLE_BUFFERS_EXT 0x2041
246 #define WGL_SAMPLES_EXT 0x2042
247 #define WGL_GENLOCK_SOURCE_MULTIVIEW_I3D 0x2044
248 #define WGL_GENLOCK_SOURCE_EXTENAL_SYNC_I3D 0x2045
249 #define WGL_GENLOCK_SOURCE_EXTENAL_FIELD_I3D 0x2046
250 #define WGL_GENLOCK_SOURCE_EXTENAL_TTL_I3D 0x2047
251 #define WGL_GENLOCK_SOURCE_DIGITAL_SYNC_I3D 0x2048
252 #define WGL_GENLOCK_SOURCE_DIGITAL_FIELD_I3D 0x2049
253 #define WGL_GENLOCK_SOURCE_EDGE_FALLING_I3D 0x204A
254 #define WGL_GENLOCK_SOURCE_EDGE_RISING_I3D 0x204B
255 #define WGL_GENLOCK_SOURCE_EDGE_BOTH_I3D 0x204C
256 #define WGL_GAMMA_TABLE_SIZE_I3D 0x204E
257 #define WGL_GAMMA_EXCLUDE_DESKTOP_I3D 0x204F
258 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_FRAMEBUFFER_I3D 0x2050
259 #define WGL_DIGITAL_VIDEO_CURSOR_ALPHA_VALUE_I3D 0x2051
260 #define WGL_DIGITAL_VIDEO_CURSOR_INCLUDED_I3D 0x2052
261 #define WGL_DIGITAL_VIDEO_GAMMA_CORRECTED_I3D 0x2053
262 #define WGL_ARB_buffer_region 1
263 #define WGL_ARB_extensions_string 1
264 #define WGL_ARB_pixel_format 1
265 #define WGL_ARB_make_current_read 1
266 #define WGL_ARB_pbuffer 1
267 #define WGL_EXT_display_color_table 1
268 #define WGL_EXT_extensions_string 1
269 #define WGL_EXT_make_current_read 1
270 #define WGL_EXT_pbuffer 1
271 #define WGL_EXT_pixel_format 1
272 #define WGL_EXT_swap_control 1
273 #define WGL_WGL_EXT_depth_float 1
274 #define WGL_WGL_3DFX_multisample 1
275 #define WGL_WGL_EXT_multisample 1
276 #define WGL_NV_allocate_memory 1
279 typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum target);
280 typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum target, GLfloat s, GLfloat t);
281 typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum target);
282 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
283 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
287 typedef int (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
288 typedef int (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
289 typedef int (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
290 typedef int (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
292 typedef int (APIENTRY * PFNWGLCHOOSEPIXELFORMATARBPROC) ();
293 typedef void * (APIENTRY * PFNWGLCREATEPBUFFERARBPROC) (HDC hDC, int iPixelFormat, int iWidth, int iHeight, const int *piAttribList);
294 typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
295 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
296 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
297 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
299 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
300 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
301 static PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = null;
302 static PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = null;
303 static PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = null;
304 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
305 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
306 static PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate = null;
308 static PFNGLGENBUFFERSARBPROC glGenBuffersARB = null;
309 static PFNGLBINDBUFFERARBPROC glBindBufferARB = null;
310 static PFNGLBUFFERDATAARBPROC glBufferDataARB = null;
311 static PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = null;
312 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
313 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
314 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
315 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
316 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
317 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
318 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
319 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
320 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
322 #ifdef WGL_WGLEXT_PROTOTYPES
323 extern BOOL WINAPI wglSwapIntervalEXT (int);
324 extern int WINAPI wglGetSwapIntervalEXT (void);
325 #endif /* WGL_WGLEXT_PROTOTYPES */
326 typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
327 typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC) (void);
329 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
333 void (APIENTRY * glBindBufferARB) (GLenum target, GLuint buffer);
334 void (APIENTRY * glGenBuffersARB) (GLsizei n, GLuint *buffers);
335 void (APIENTRY * glDeleteBuffersARB) (GLsizei n, const GLuint *buffers);
336 void (APIENTRY * glBufferDataARB) (GLenum target, int size, const GLvoid *data, GLenum usage);
340 static int displayWidth, displayHeight;
342 #define GL_CLAMP_TO_EDGE 0x812F
344 static bool useSingleGLContext = false;
345 class OGLDisplay : struct
347 #if defined(__WIN32__)
358 byte * pboMemory1, * pboMemory2;
361 GLXContext glContext;
364 XShmSegmentInfo shminfo;
366 XShmSegmentInfo shminfoShape;
371 X11Picture windowPicture;
372 X11Picture pixmapPicture;
374 X11Picture shapePicture;
377 ColorAlpha * flippingBuffer;
378 int flipBufH, flipBufW;
383 class OGLSystem : struct
385 #if defined(__WIN32__)
386 PIXELFORMATDESCRIPTOR pfd;
392 XVisualInfo * visualInfo;
393 GLXContext glContext;
395 GLXPixmap dummyGLXPixmap;
400 class OGLSurface : struct
407 float foreground[4], background[4], bitmapMult[4];
410 class OGLMesh : struct
419 class OGLIndices : struct
426 static int primitiveTypes[RenderPrimitiveType] =
428 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_LINE_STRIP
434 class OpenGLDisplayDriver : DisplayDriver
436 class_property(name) = "OpenGL";
438 bool LockSystem(DisplaySystem displaySystem)
440 OGLSystem oglSystem = displaySystem.driverData;
441 if(useSingleGLContext) return true;
442 #if defined(__WIN32__)
443 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
445 //if(previous) return true;
446 // printf("Making SYSTEM current\n");
447 /*#if defined(__APPLE__)
448 glXMakeCurrent(xGlobalDisplay, displaySystem.window, oglSystem.glContext);
450 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
452 //previous = oglSystem.glContext;
457 void UnlockSystem(DisplaySystem displaySystem)
459 if(useSingleGLContext) return;
460 #if defined(__WIN32__)
461 wglMakeCurrent(null, null);
463 // printf("Making NULL current\n");
464 glXMakeCurrent(xGlobalDisplay, None, null);
469 bool Lock(Display display)
471 OGLDisplay oglDisplay = display.driverData;
472 OGLSystem oglSystem = display.displaySystem.driverData;
474 if(useSingleGLContext) return true;
475 #if defined(__WIN32__)
476 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
478 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
479 // printf(" Making DISPLAY current\n");
480 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
485 void Unlock(Display display)
487 if(useSingleGLContext) return;
488 //printf(" Making NULL current\n");
489 //glXMakeCurrent(xGlobalDisplay, None, null);
491 LockSystem(display.displaySystem);
494 void DestroyDisplay(Display display)
496 OGLDisplay oglDisplay = display.driverData;
500 #if defined(__WIN32__)
501 wglMakeCurrent( null, null );
504 wglDeleteContext(oglDisplay.glrc);
506 if(oglDisplay.hdc && oglDisplay.pBuffer)
507 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
509 if(oglDisplay.pBuffer)
510 wglDestroyPbufferARB(oglDisplay.pBuffer);
513 ReleaseDC(display.window, oglDisplay.hdc);
515 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
516 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
519 if(oglDisplay.shapePixmap)
520 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
521 if(oglDisplay.pixmap)
522 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
525 if(oglDisplay.shminfoShape.shmid != -1)
527 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
528 if(oglDisplay.shminfo.shmaddr != (void *)-1)
529 shmdt(oglDisplay.shminfo.shmaddr);
530 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
533 if(oglDisplay.shapeImage)
535 if(oglDisplay.shminfoShape.shmid != -1)
537 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
538 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
539 shmdt(oglDisplay.shminfoShape.shmaddr);
540 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
542 XDestroyImage(oglDisplay.shapeImage);
543 oglDisplay.shapeImage = None;
546 glXMakeCurrent(xGlobalDisplay, None, null);
548 if(oglDisplay.glContext)
549 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
551 delete oglDisplay.flippingBuffer;
553 display.driverData = null;
557 bool CreateDisplaySystem(DisplaySystem displaySystem)
560 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
563 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
565 oglSystem.hdc = GetDC(oglSystem.hwnd);
569 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
570 oglSystem.pfd.nVersion = 1;
571 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
572 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
573 oglSystem.pfd.cColorBits = 32;
574 oglSystem.pfd.cDepthBits = 24;
575 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
577 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
578 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
580 if(oglSystem.pfd.cColorBits > 8)
582 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
583 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
586 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
588 // Get Pointers To The GL Functions
589 glActiveTextureARB = (void *) wglGetProcAddress("glActiveTextureARB");
590 glMultiTexCoord2fARB = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
591 glClientActiveTextureARB = (void *) wglGetProcAddress("glClientActiveTextureARB");
592 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
593 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
594 glGenBuffersARB = (void *) wglGetProcAddress("glGenBuffersARB");
595 glBindBufferARB = (void *) wglGetProcAddress("glBindBufferARB");
596 glBufferDataARB = (void *) wglGetProcAddress("glBufferDataARB");
597 glMapBufferARB = (void *) wglGetProcAddress("glMapBufferARB");
598 glUnmapBufferARB = (void *) wglGetProcAddress("glUnmapBufferARB");
599 glDeleteBuffersARB = (void *) wglGetProcAddress("glDeleteBuffersARB");
600 glBlendFuncSeparate = (void *) wglGetProcAddress("glBlendFuncSeparate");
602 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
603 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
604 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
605 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
606 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
607 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
608 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
609 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
610 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
612 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
614 // eSystem_LoggingMode(LOG_MSGBOX, null);
616 if(wglChoosePixelFormatARB)
621 float fAttributes[] = {0,0};
624 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
625 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
626 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
627 WGL_COLOR_BITS_ARB,24,
628 WGL_ALPHA_BITS_ARB,8,
629 WGL_DEPTH_BITS_ARB,16,
630 WGL_STENCIL_BITS_ARB,0,
631 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
632 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
633 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
637 //Log("Found wglChoosePixelFormatARB\n");
639 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
640 if(!valid || !numFormats)
642 //Log("Can't find 4x multi sampling\n");
644 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
645 if(!valid || !numFormats)
647 // Log("Can't find 2x multi sampling\n");
650 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
653 if(valid && numFormats)
655 oglSystem.format = pixelFormat;
656 wglMakeCurrent(null, null);
657 wglDeleteContext(oglSystem.glrc);
658 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
659 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
660 //Log("Successfully set pixel format\n");
662 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
663 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
667 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
671 wglMakeCurrent(null, null);
673 //eSystem_DumpErrors(true);
680 #ifndef ECERE_MINIGLX
681 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
684 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
688 oglSystem.visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
689 if(oglSystem.visualInfo)
691 //printf("glXChooseVisual returnded a visual info\n");
692 oglSystem.dummyPixmap = XCreatePixmap(xGlobalDisplay, (uint)displaySystem.window, 1, 1, oglSystem.visualInfo->depth);
693 oglSystem.dummyGLXPixmap = glXCreateGLXPixmap(xGlobalDisplay, oglSystem.visualInfo, oglSystem.dummyPixmap);
695 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
696 // printf("Creating system Context (%x)!\n", oglSystem.glContext);
697 if(oglSystem.glContext)
699 //printf("Got a Context\n");
700 /*#if defined(__APPLE__)
701 glXMakeCurrent(xGlobalDisplay, displaySystem.window, oglSystem.glContext);
704 glXMakeCurrent(xGlobalDisplay, oglSystem.dummyGLXPixmap /*displaySystem.window /*DefaultRootWindow(xGlobalDisplay)*/, oglSystem.glContext);
709 glXMakeCurrent(xGlobalDisplay, None, null);
716 displaySystem.flags.alpha = true;
717 displaySystem.flags.flipping = true;
718 displaySystem.pixelFormat = pixelFormat888;
722 void DestroyDisplaySystem(DisplaySystem displaySystem)
724 OGLSystem oglSystem = displaySystem.driverData;
726 #if defined(__WIN32__)
727 wglMakeCurrent( null, null );
730 wglDeleteContext(oglSystem.glrc);
733 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
734 DestroyWindow(oglSystem.hwnd);
738 if(oglSystem.visualInfo)
741 __miniglx_XFree(oglSystem.visualInfo);
743 XFree(oglSystem.visualInfo);
747 if(oglSystem.dummyGLXPixmap)
748 glXDestroyGLXPixmap(xGlobalDisplay, oglSystem.dummyGLXPixmap);
749 if(oglSystem.dummyPixmap);
750 XFreePixmap(xGlobalDisplay, oglSystem.dummyPixmap);
755 bool CreateDisplay(Display display)
758 OGLDisplay oglDisplay = display.driverData;
759 OGLSystem oglSystem = display.displaySystem.driverData;
761 oglDisplay = display.driverData = OGLDisplay { };
762 //printf("Inside CreateDisplay\n");
764 #if defined(__WIN32__) || defined(USEPBUFFER)
765 if(!display.alphaBlend)
768 #if defined(__WIN32__)
769 oglDisplay.hdc = GetDC(display.window);
770 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
771 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
773 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
774 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
778 ReleaseDC(display.window, oglDisplay.hdc);
780 XVisualInfo * visualInfo = null;
784 GLX_RENDER_TYPE, GLX_RGBA_BIT,
785 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
789 GLX_DOUBLEBUFFER, True,
794 //visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib);
795 visualInfo = ((XWindowData)display.windowDriverData).visual;
798 GLXFBConfig *fbconfigs, fbconfig;
800 fbconfigs = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &numfbconfigs);
804 for (i = 0; i < numfbconfigs; i++)
806 XRenderPictFormat * format;
807 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfigs[i]);
808 if (!visualInfo) continue;
809 format = XRenderFindVisualFormat(xGlobalDisplay, visualInfo->visual);
810 if (!format) continue;
812 if(format->direct.alphaMask > 0)
814 fbconfig = fbconfigs[i];
820 if (i == numfbconfigs)
822 fbconfig = fbconfigs[0];
823 visualInfo = glXGetVisualFromFBConfig(xGlobalDisplay, fbconfig);
830 //printf("visualInfo is not null\n");
831 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
832 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
836 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
837 if(oglDisplay.glContext)
839 //printf("CreateDisplay Got a Context\n");
840 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
845 #if defined(__WIN32__) || defined(USEPBUFFER)
851 #if !defined(__OLDX__)
852 if(glBlendFuncSeparate)
853 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
856 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
859 glMatrixMode(GL_MODELVIEW);
860 glScalef(1.0f, 1.0f, -1.0f);
861 // glTranslatef(0.375f, 0.375f, 0.0f);
862 // glTranslatef(-0.625f, -0.625f, 0.0f);
863 glMatrixMode(GL_PROJECTION);
864 glShadeModel(GL_FLAT);
866 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
867 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
868 glFogi(GL_FOG_MODE, GL_EXP);
869 glFogf(GL_FOG_DENSITY, 0);
870 glEnable(GL_NORMALIZE);
871 glDepthFunc(GL_LESS);
873 glDisable(GL_MULTISAMPLE_ARB);
875 display.ambient = Color { 50,50,50 };
877 if(!useSingleGLContext)
878 #if defined(__WIN32__)
879 wglMakeCurrent(null, null);
881 glXMakeCurrent(xGlobalDisplay, None, null);
887 bool DisplaySize(Display display, int width, int height)
889 OGLDisplay oglDisplay = display.driverData;
890 OGLSystem oglSystem = display.displaySystem.driverData;
894 //printf("Inside DisplaySize\n");
895 #if defined(__WIN32__) || defined(USEPBUFFER)
896 if(display.alphaBlend)
898 #if defined(__WIN32__)
899 const int attributes[]=
901 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
902 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
905 if(wglChoosePixelFormatARB)
909 float fAttributes[] = {0,0};
912 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
913 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
914 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
915 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
916 WGL_COLOR_BITS_ARB,24,
917 WGL_ALPHA_BITS_ARB,8,
918 WGL_DEPTH_BITS_ARB,16,
919 WGL_STENCIL_BITS_ARB,0,
920 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
921 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
922 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
926 //Log("Found wglChoosePixelFormatARB\n");
928 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
929 if(!valid || !numFormats)
931 //Log("Can't find 4x multi sampling\n");
933 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
934 if(!valid || !numFormats)
936 // Log("Can't find 2x multi sampling\n");
939 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
940 if(!valid || !numFormats)
944 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
945 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
946 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
947 WGL_COLOR_BITS_ARB,24,
948 WGL_ALPHA_BITS_ARB,8,
949 WGL_DEPTH_BITS_ARB,16,
952 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
956 if(valid && numFormats)
958 wglMakeCurrent(null, null);
962 wglMakeCurrent( null, null );
963 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
964 if(oglDisplay.hdc && oglDisplay.pBuffer)
965 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
967 wglDestroyPbufferARB(oglDisplay.pBuffer);
969 if(!useSingleGLContext)
970 wglMakeCurrent( null, null );
973 wglDeleteContext(oglDisplay.glrc);
975 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
976 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
977 if((oglDisplay.glrc = wglCreateContext(oglDisplay.hdc)))
980 HDC hdc = GetDC(display.window);
982 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
983 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
985 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
986 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
988 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
990 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
994 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
995 oglDisplay.memDC = CreateCompatibleDC(hdc);
996 SetMapMode(oglDisplay.memDC, MM_TEXT);
997 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
998 info->bmiHeader.biPlanes = 1;
999 info->bmiHeader.biCompression = BI_RGB;
1000 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1001 info->bmiHeader.biWidth = width;
1002 info->bmiHeader.biHeight = height;
1003 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1006 SelectObject(oglDisplay.memDC, newBitmap);
1007 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1010 PIXELFORMATDESCRIPTOR pfd = { 0 };
1011 pfd.nSize = (short)sizeof(pfd);
1013 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1014 pfd.iPixelType = PFD_TYPE_RGBA;
1015 pfd.cColorBits = 32;
1016 //pfd.cAlphaBits = 8;
1017 pfd.cDepthBits = 24;
1018 pfd.iLayerType = PFD_MAIN_PLANE;
1020 oglDisplay.hdc = oglDisplay.memDC;
1022 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1023 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1024 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1026 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1027 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1028 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1033 const int imageSize = width * height * 4;
1035 glGenBuffersARB(2, oglDisplay.imageBuffers);
1037 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1038 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1039 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1040 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1043 oglDisplay.memBitmap = newBitmap;
1044 oglDisplay.stride = width;
1050 ReleaseDC(display.window, hdc);
1055 GLX_DOUBLEBUFFER, True,
1061 GLX_STENCIL_SIZE, 1,
1062 //GLX_DEPTH_SIZE, 24,
1063 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1064 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1070 GLX_PBUFFER_WIDTH, width,
1071 GLX_PBUFFER_HEIGHT, height,
1072 GLX_LARGEST_PBUFFER, False,
1076 // choose a pixel format that meets our minimum requirements
1079 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1082 if(oglDisplay.pixmap)
1084 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1085 oglDisplay.pixmap = None;
1087 if(oglDisplay.shapePixmap)
1089 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1090 oglDisplay.shapePixmap = None;
1093 // Free Shared Memory Pixmap
1094 if(oglDisplay.image)
1096 if(oglDisplay.shminfoShape.shmid != -1)
1098 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1099 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1100 shmdt(oglDisplay.shminfo.shmaddr);
1101 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1103 XDestroyImage(oglDisplay.image);
1104 oglDisplay.image = None;
1106 if(oglDisplay.shapeImage)
1108 if(oglDisplay.shminfoShape.shmid != -1)
1110 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1111 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1112 shmdt(oglDisplay.shminfoShape.shmaddr);
1113 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1115 XDestroyImage(oglDisplay.shapeImage);
1116 oglDisplay.shapeImage = None;
1119 if(oglDisplay.windowPicture)
1120 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1121 if(oglDisplay.pixmapPicture)
1122 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1124 if(oglDisplay.pixmap)
1125 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1127 if(oglDisplay.glContext)
1128 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1129 if(oglDisplay.pBuffer)
1130 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1132 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1133 if(oglDisplay.pBuffer)
1135 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1136 if(oglDisplay.glContext)
1138 glXMakeCurrent(xGlobalDisplay, None, null);
1139 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1141 // Initialize Shared Memory Pixmap
1142 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1143 ZPixmap, null, &oglDisplay.shminfo, width, height);
1144 if(oglDisplay.image)
1146 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1147 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1148 if(oglDisplay.shminfo.shmid != -1)
1150 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1151 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1153 oglDisplay.shminfo.readOnly = False;
1154 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1156 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1157 &oglDisplay.shminfo, width, height, 32);
1159 // Initialize Shared Memory Shape Pixmap
1160 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1161 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1162 if(oglDisplay.shapeImage)
1164 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1165 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1166 if(oglDisplay.shminfoShape.shmid != -1)
1168 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1169 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1171 oglDisplay.shminfoShape.readOnly = False;
1172 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1174 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1175 &oglDisplay.shminfoShape, width, height, 1);
1176 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1179 XRenderPictureAttributes attributes = { 0 };
1180 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1181 #if !defined(__APPLE__) && !defined(__OLDX__)
1182 attributes.repeat = RepeatNormal;
1184 attributes.repeat = 1;
1186 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1187 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1188 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1189 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1192 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1193 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1209 CreateDisplay(display);
1210 #if defined(__WIN32__)
1211 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1213 glXMakeCurrent(xGlobalDisplay, (int)display.window, oglDisplay.glContext);
1219 if(!result && display.alphaBlend)
1221 printf("Alpha blending windows not supported on this display\n");
1228 glViewport(0,0,width,height);
1230 glOrtho(0,width,height,0,0.0,1.0);
1231 displayWidth = display.width = width;
1232 displayHeight = display.height = height;
1234 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1236 oglDisplay.flipBufW = width;
1237 oglDisplay.flipBufH = height;
1238 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1240 if(oglDisplay.flippingBuffer)
1246 void DisplayPosition(Display display, int x, int y)
1248 OGLDisplay oglDisplay = display.driverData;
1254 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1258 void RestorePalette(Display display)
1262 void StartUpdate(Display display)
1266 void EndUpdate(Display display)
1270 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1274 void Update(Display display, Box updateBox)
1276 OGLDisplay oglDisplay = display.driverData;
1277 //Logf("DisplayScreen\n");
1281 #if defined(__WIN32__) || defined(USEPBUFFER)
1282 if(display.alphaBlend)
1284 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1285 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1286 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1287 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1288 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1291 #if defined(__WIN32__)
1293 POINT point = { oglDisplay.x, oglDisplay.y};
1294 POINT srcPoint = { 0, 0 };
1295 BLENDFUNCTION blend = { 0 };
1297 size.cx = display.width;
1298 size.cy = display.height;
1299 blend.BlendOp = AC_SRC_OVER;
1300 blend.BlendFlags = 0;
1301 blend.SourceConstantAlpha = 255;
1302 blend.AlphaFormat = AC_SRC_ALPHA;
1305 // Process partial images. Mapping the buffer waits for
1306 // outstanding DMA transfers into the buffer to finish.
1307 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1308 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1310 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1311 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
1314 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
1315 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
1318 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
1321 // Unmap the image buffers
1322 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1323 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1325 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1326 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1328 // Bind two different buffer objects and start the glReadPixels
1329 // asynchronously. Each call will return directly after
1330 // starting the DMA transfer.
1331 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1332 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1334 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1335 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1340 XTransform transform =
1343 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
1344 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
1345 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
1348 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
1349 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1350 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1351 #if !defined(__APPLE__) && !defined(__OLDX__)
1352 XShapeCombineMask(xGlobalDisplay, (uint)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1354 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1356 XFlush(xGlobalDisplay);
1363 #if defined(__WIN32__)
1364 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
1365 SwapBuffers(oglDisplay.hdc);
1367 glXSwapBuffers(xGlobalDisplay, (int)display.window);
1370 //Logf("Out of DisplayScreen\n");
1373 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
1375 glDeleteTextures(1, (int *)&bitmap.driverData);
1376 bitmap.driverData = 0;
1378 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
1381 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
1383 bool result = false;
1387 uint w = pow2i(Min(width, 1024)), h = pow2i(Min(height, 1024));
1388 //uint w = pow2i(Min(width, 2048)), h = pow2i(Min(height, 2048));
1389 //uint w = pow2i(Min(width, 512)), h = pow2i(Min(height, 512));
1391 glGenTextures(1, &glBitmap);
1392 glBindTexture(GL_TEXTURE_2D, glBitmap);
1394 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1396 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1397 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1402 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1404 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
1406 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1409 bitmap.driverData = (void *)glBitmap;
1410 bitmap.driver = displaySystem.driver;
1418 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
1420 bool result = false;
1421 OGLSystem oglSystem = displaySystem.driverData;
1423 // Pre process the bitmap... First make it 32 bit
1424 if(/*bitmap.pixelFormat == pixelFormatRGBA || */bitmap.Convert(null, pixelFormat888, null))
1427 uint w = pow2i(Min(bitmap.width, 1024)), h = pow2i(Min(bitmap.height, 1024));
1428 //uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
1431 // Switch ARGB to RGBA
1432 //if(bitmap.format != pixelFormatRGBA)
1434 for(c=0; c<bitmap.size; c++)
1436 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
1438 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
1439 ((ColorRGBA *)bitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
1442 bitmap.pixelFormat = pixelFormat888;
1445 glGenTextures(1, &glBitmap);
1448 int error = glGetError();
1453 glBindTexture(GL_TEXTURE_2D, glBitmap);
1454 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
1458 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1460 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1461 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1463 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1464 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1466 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1470 for(level = 0; result && (w > 1 || h > 1); level++, w >>= 1, h >>= 1)
1473 if(bitmap.width != w || bitmap.height != h)
1475 mipMap = Bitmap { };
1476 if(mipMap.Allocate(null, w, h, w, bitmap.pixelFormat, false))
1478 Surface mipSurface = mipMap.GetSurface(0,0,null);
1479 mipSurface.Filter(bitmap, 0,0,0,0, w, h, bitmap.width, bitmap.height);
1496 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1497 //printf("Calling glTexImage2D\n");
1498 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
1499 //printf("width = %d (Should be %d, %d)\n", width, w, h);
1500 if((error = glGetError()))
1502 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
1503 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
1507 if(mipMap != bitmap)
1512 if(!bitmap.keepData)
1513 bitmap.driver.FreeBitmap(bitmap.displaySystem, bitmap);
1514 bitmap.driverData = (void *)glBitmap;
1515 bitmap.driver = displaySystem.driver;
1518 FreeBitmap(displaySystem, bitmap);
1519 else if(oglSystem.loadingFont)
1521 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1522 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1523 oglSystem.loadingFont = false;
1529 void ReleaseSurface(Display display, Surface surface)
1531 glDisable(GL_SCISSOR_TEST);
1532 delete surface.driverData;
1533 surface.driverData = null;
1536 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
1541 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
1543 bool result = false;
1544 OGLSurface oglSurface = surface.driverData = OGLSurface { };
1546 //Logf("GetSurface\n");
1550 if(displayWidth != display.width || displayHeight != display.height)
1552 displayWidth = display.width;
1553 displayHeight = display.height;
1555 glViewport(0,0,display.width,display.height);
1557 glOrtho(0,display.width,display.height,0,0.0,1.0);
1560 surface.offset.x = x;
1561 surface.offset.y = y;
1562 surface.unclippedBox = surface.box = clip;
1563 oglSurface.bitmapMult[0] = 1;
1564 oglSurface.bitmapMult[1] = 1;
1565 oglSurface.bitmapMult[2] = 1;
1566 oglSurface.bitmapMult[3] = 1;
1568 glEnable(GL_SCISSOR_TEST);
1571 (display.height) -(y+clip.bottom)-1,
1572 clip.right-clip.left+1,
1573 clip.bottom-clip.top+1);
1579 void Clip(Display display, Surface surface, Box clip)
1588 box.Clip(surface.unclippedBox);
1592 box = surface.box = surface.unclippedBox;
1593 box.left += surface.offset.x;
1594 box.top += surface.offset.y;
1595 box.right+= surface.offset.x;
1596 box.bottom += surface.offset.y;
1599 box.left,display.height - box.bottom - 1,
1600 box.right-box.left+1, box.bottom-box.top+1);
1603 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
1605 bool result = false;
1606 OGLDisplay oglDisplay = display.driverData;
1607 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
1609 if(oglDisplay.flippingBuffer)
1611 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
1614 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
1620 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1621 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
1622 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1623 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1624 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
1627 for(row = 0; row<h; row++)
1628 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
1635 void SetForeground(Display display, Surface surface, ColorAlpha color)
1637 OGLSurface oglSurface = surface.driverData;
1639 //Logf("SetForeground\n");
1641 oglSurface.foreground[0] = color.color.r/255.0f;
1642 oglSurface.foreground[1] = color.color.g/255.0f;
1643 oglSurface.foreground[2] = color.color.b/255.0f;
1644 //oglSurface.foreground[3] = 1.0f;
1645 oglSurface.foreground[3] = color.a/255.0f;
1647 //if(!oglSurface.foreground[3])printf("bug");
1650 void SetBackground(Display display, Surface surface, ColorAlpha color)
1652 OGLSurface oglSurface = surface.driverData;
1654 //Logf("SetBackground\n");
1656 oglSurface.background[0] = color.color.r/255.0f;
1657 oglSurface.background[1] = color.color.g/255.0f;
1658 oglSurface.background[2] = color.color.b/255.0f;
1659 //oglSurface.background[3] = 1.0;
1660 oglSurface.background[3] = color.a/255.0f;
1663 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
1665 OGLSurface oglSurface = surface.driverData;
1667 oglSurface.bitmapMult[0] = color.color.r/255.0f;
1668 oglSurface.bitmapMult[1] = color.color.g/255.0f;
1669 oglSurface.bitmapMult[2] = color.color.b/255.0f;
1670 oglSurface.bitmapMult[3] = color.a/255.0f;
1673 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
1678 void PutPixel(Display display, Surface surface,int x,int y)
1680 OGLSurface oglSurface = surface.driverData;
1682 //Logf("PutPixel\n");
1684 glColor4fv(oglSurface.foreground);
1686 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
1687 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
1692 void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
1694 OGLSurface oglSurface = surface.driverData;
1695 if(x1 == x2) { y2++; y1--; }
1696 else if(y1 == y2) { x2++; x1--; }
1700 glColor4fv(oglSurface.foreground);
1703 glVertex2i(x1+surface.offset.x, y1+surface.offset.y);
1704 glVertex2i(x2+surface.offset.x, y2+surface.offset.y);
1706 glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
1707 glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
1712 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
1714 OGLSurface oglSurface = surface.driverData;
1716 //Logf("Rectangle\n");
1718 glColor4fv(oglSurface.foreground);
1719 glBegin(GL_LINE_LOOP);
1721 glVertex2i(x1+surface.offset.x, y1+surface.offset.y);
1722 glVertex2i(x1+surface.offset.x, y2+surface.offset.y);
1723 glVertex2i(x2+surface.offset.x, y2+surface.offset.y);
1724 glVertex2i(x2+surface.offset.x, y1+surface.offset.y);
1726 glVertex2f(x1+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
1727 glVertex2f(x1+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
1728 glVertex2f(x2+surface.offset.x + 0.5f, y2+surface.offset.y + 0.5f);
1729 glVertex2f(x2+surface.offset.x + 0.5f, y1+surface.offset.y + 0.5f);
1734 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
1736 OGLSurface oglSurface = surface.driverData;
1739 glColor4fv(oglSurface.background);
1740 glRecti(x1+surface.offset.x, y1+surface.offset.y,
1741 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
1744 glRectf(x1+surface.offset.x, y1+surface.offset.y,
1745 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
1749 void Clear(Display display, Surface surface, ClearType type)
1751 OGLDisplay oglDisplay = display.driverData;
1752 OGLSurface oglSurface = surface.driverData;
1755 if(type != depthBuffer)
1756 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
1757 if(type != colorBuffer && !oglDisplay.depthWrite)
1759 glDepthMask((byte)bool::true);
1761 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
1762 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
1763 if(type != colorBuffer && !oglDisplay.depthWrite)
1765 glDepthMask((byte)bool::false);
1769 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
1774 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
1776 OGLSurface oglSurface = surface.driverData;
1778 #if !defined(__OLDX__)
1779 // WHY DO WE HAVE GL_ONE HERE ?
1780 /*if(glBlendFuncSeparate && !oglSurface.writingText)
1781 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
1784 if(!oglSurface.writingText)
1786 // glTranslatef(-0.375f, -0.375f, 0.0f);
1787 glEnable(GL_TEXTURE_2D);
1788 glColor4fv(oglSurface.bitmapMult);
1790 else if(oglSurface.xOffset)
1791 glTranslatef(oglSurface.xOffset / 64.0f/*-0.375f*/, 0.0f, 0.0f);
1793 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
1798 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
1799 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
1800 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
1801 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
1802 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
1803 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
1804 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
1805 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
1810 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
1811 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
1812 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
1813 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
1814 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
1815 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
1816 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
1817 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
1820 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
1821 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
1822 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
1823 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
1824 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
1825 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
1826 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
1827 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
1831 if(!oglSurface.writingText)
1833 glDisable(GL_TEXTURE_2D);
1835 //glTranslatef(0.375f, 0.375f, 0.0f);
1837 else if(oglSurface.xOffset)
1838 glTranslatef(-oglSurface.xOffset / 64.0f/*+0.375f*/, 0.0f, 0.0f);
1840 #if !defined(__OLDX__)
1841 /*if(glBlendFuncSeparate && !oglSurface.writingText)
1842 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
1846 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
1848 OGLSurface oglSurface = surface.driverData;
1850 //glTranslatef(-0.375f, -0.375f, 0.0f);
1852 //Logf("Stretch\n");
1854 #if !defined(__OLDX__)
1855 /*if(glBlendFuncSeparate)
1856 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
1859 glEnable(GL_TEXTURE_2D);
1860 glBindTexture(GL_TEXTURE_2D, (uint)bitmap.driverData);
1862 glColor4fv(oglSurface.bitmapMult);
1868 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
1869 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
1871 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
1872 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
1874 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
1875 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
1877 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
1878 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
1882 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
1883 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
1885 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
1886 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
1888 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
1889 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
1891 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
1892 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
1897 glDisable(GL_TEXTURE_2D);
1899 //glTranslatef(0.375f, 0.375f, 0.0f);
1900 #if !defined(__OLDX__)
1901 /*if(glBlendFuncSeparate)
1902 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);*/
1907 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
1909 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
1912 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
1914 float s2dw,s2dh,d2sw,d2sh;
1915 bool flipX = false, flipY = false;
1917 //Logf("StretchDI\n");
1919 if(Sgn(w) != Sgn(sw))
1925 if(Sgn(h) != Sgn(sh))
1937 //Clip against the edges of the source
1940 dx+=(int)((0-sx) * s2dw);
1941 w-=(int)((0-sx) * s2dw);
1947 dy+=(int)((0-sy) * s2dh);
1948 h-=(int)((0-sy) * s2dh);
1953 if(sx+sw>bitmap.width-1)
1955 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
1956 sw-=sx+sw-(bitmap.width-1)-1;
1958 if(sy+sh>(bitmap.height-1))
1960 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
1961 sh-=sy+sh-(bitmap.height-1)-1;
1963 //Clip against the edges of the surfaceination
1964 if(dx<surface.box.left)
1967 sx+=(int)((surface.box.left-dx)*d2sw);
1968 sw-=(int)((surface.box.left-dx)*d2sw);
1969 w-=surface.box.left-dx;
1970 dx=surface.box.left;
1972 if(dy<surface.box.top)
1974 sy+=(int)((surface.box.top-dy)*d2sh);
1975 sh-=(int)((surface.box.top-dy)*d2sh);
1976 h-=surface.box.top-dy;
1979 if(dx+w>surface.box.right)
1981 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
1982 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
1983 w-=dx+w-surface.box.right-1;
1985 if(dy+h>surface.box.bottom)
1987 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
1988 h-=dy+h-surface.box.bottom-1;
1990 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
1992 dx += surface.offset.x;
1993 dy += surface.offset.y;
1995 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
1997 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1998 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
1999 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2000 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2001 glRasterPos2d(dx,dy);
2002 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2003 glPixelZoom(s2dw, -s2dh);
2004 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2005 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2006 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2010 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2014 //Clip against the edges of the source
2027 if(sx+w>bitmap.width-1)
2028 w-=sx+w-(bitmap.width-1)-1;
2029 if(sy+h>bitmap.height-1)
2030 h-=sy+h-(bitmap.height-1)-1;
2031 //Clip against the edges of the surfaceination
2032 if(dx<surface.box.left)
2035 sx+=surface.box.left-dx;
2036 w-=surface.box.left-dx;
2037 dx=surface.box.left;
2039 if(dy<surface.box.top)
2041 sy+=surface.box.top-dy;
2042 h-=surface.box.top-dy;
2045 if(dx+w>surface.box.right)
2047 //if(flip) sx+=dx+w-surface.box.right-1;
2048 w-=dx+w-surface.box.right-1;
2050 if(dy+h>surface.box.bottom)
2051 h-=dy+h-surface.box.bottom-1;
2055 dx += surface.offset.x;
2056 dy += surface.offset.y;
2058 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2060 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2061 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2062 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2063 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2064 glRasterPos2d(dx,dy);
2066 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2067 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2068 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2072 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2074 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2077 void UnloadFont(DisplaySystem displaySystem, Font font)
2079 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2082 Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
2085 OGLSystem oglSystem = displaySystem.driverData;
2086 oglSystem.loadingFont = true;
2087 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
2091 void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
2093 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
2096 void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
2098 OGLSurface oglSurface = surface.driverData;
2099 OGLSystem oglSystem = display.displaySystem.driverData;
2100 oglSystem.loadingFont = true;
2102 //glTranslatef(-0.375f, -0.375f, 0.0f);
2106 if(surface.textOpacity)
2109 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
2110 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2113 oglSurface.writingText = true;
2115 glEnable(GL_TEXTURE_2D);
2116 glColor4fv(oglSurface.foreground);
2118 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2119 oglSurface.writingText = false;
2120 oglSystem.loadingFont = false;
2122 glDisable(GL_TEXTURE_2D);
2124 //glTranslatef(0.375f, 0.375f, 0.0f);
2127 void TextFont(Display display, Surface surface, Font font)
2129 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2132 void TextOpacity(Display display, Surface surface, bool opaque)
2134 OGLSurface oglSurface = surface.driverData;
2135 oglSurface.opaqueText = opaque;
2138 void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
2140 OGLSurface oglSurface = surface.driverData;
2141 OGLSystem oglSystem = display.displaySystem.driverData;
2142 oglSystem.loadingFont = true;
2143 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
2144 oglSystem.loadingFont = false;
2147 void DrawingChar(Display display, Surface surface, char character)
2152 void LineStipple(Display display, Surface surface, uint32 stipple)
2154 //Logf("Stipple\n");
2158 glLineStipple(1, (uint16)stipple);
2159 glEnable(GL_LINE_STIPPLE);
2162 glDisable(GL_LINE_STIPPLE);
2165 void SetRenderState(Display display, RenderState state, uint value)
2167 OGLDisplay oglDisplay = display.driverData;
2168 //Logf("RenderState\n");
2174 glEnable(GL_MULTISAMPLE_ARB);
2176 glDisable(GL_MULTISAMPLE_ARB);
2179 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2182 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2185 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2186 oglDisplay.depthWrite = value;
2190 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2191 glFogfv(GL_FOG_COLOR, (float *)&color);
2195 glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
2198 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2202 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2203 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2208 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2213 #if defined(__WIN32__)
2214 wglSwapIntervalEXT(value ? 1 : 0);
2221 void SetLight(Display display, int id, Light light)
2223 //Logf("SetLight\n");
2227 Object lightObject = light.lightObject;
2228 float position[4] = { 0, 0, 0, 0 };
2229 float color[4] = { 0, 0, 0, 1 };
2231 glEnable(GL_LIGHT0 + id);
2233 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2234 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2235 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2238 if(!light.multiplier) light.multiplier = 1.0f;
2240 color[0] = light.diffuse.r * light.multiplier;
2241 color[1] = light.diffuse.g * light.multiplier;
2242 color[2] = light.diffuse.b * light.multiplier;
2243 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2245 color[0] = light.ambient.r * light.multiplier;
2246 color[1] = light.ambient.g * light.multiplier;
2247 color[2] = light.ambient.b * light.multiplier;
2248 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2249 color[0] = light.specular.r * light.multiplier;
2250 color[1] = light.specular.g * light.multiplier;
2251 color[2] = light.specular.b * light.multiplier;
2252 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2256 Vector3D positionVector;
2257 if(light.flags.spot)
2259 if(lightObject.flags.root || !lightObject.parent)
2261 positionVector = lightObject.transform.position;
2262 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2266 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
2267 if(display.display3D.camera)
2268 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2274 if(!light.direction.x && !light.direction.y && !light.direction.z)
2276 Vector3Df vector { 0,0,-1 };
2278 mat.RotationQuaternion(light.orientation);
2279 positionVector.MultMatrixf(vector, mat);
2283 positionVector = light.direction;
2288 position[0] = (float)positionVector.x;
2289 position[1] = (float)positionVector.y;
2290 position[2] = (float)positionVector.z;
2292 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2295 // Display Light Position
2296 glDisable(GL_LIGHTING);
2297 glDisable(GL_DEPTH_TEST);
2301 glVertex3fv(position);
2303 glEnable(GL_DEPTH_TEST);
2304 glEnable(GL_LIGHTING);
2308 if(lightObject.flags.root || !lightObject.parent)
2310 positionVector = light.target.transform.position;
2311 positionVector.Subtract(positionVector, display.camera.cPosition);
2315 positionVector.MultMatrix(light.target.transform.position,
2316 lightObject.light.target.parent.matrix);
2317 positionVector.Subtract(positionVector, display.camera.cPosition);
2320 position[0] = positionVector.x;
2321 position[1] = positionVector.y;
2322 position[2] = positionVector.z;
2324 glDisable(GL_LIGHTING);
2325 glDisable(GL_DEPTH_TEST);
2329 glVertex3fv(position);
2331 glEnable(GL_DEPTH_TEST);
2332 glEnable(GL_LIGHTING);
2335 if(light.flags.attenuation)
2337 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
2338 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
2339 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
2342 if(light.flags.spot)
2345 #define MAXLIGHT 0.9
2346 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
2347 // Figure out exponent out of the hot spot
2348 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
2350 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
2351 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
2352 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
2358 Vector3Df vector { 0,0,-1 };
2359 Vector3Df direction;
2362 mat.RotationQuaternion(light.orientation);
2363 direction.MultMatrix(vector, mat);
2365 position[0] = direction.x;
2366 position[1] = direction.y;
2367 position[2] = direction.z;
2369 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2373 glDisable(GL_LIGHT0 + id);
2376 void SetCamera(Display display, Surface surface, Camera camera)
2378 OGLDisplay oglDisplay = display.driverData;
2379 //Logf("SetCamera\n");
2383 int left = surface.box.left + surface.offset.x;
2384 int top = surface.box.top + surface.offset.y;
2385 int right = surface.box.right + surface.offset.x;
2386 int bottom = surface.box.bottom + surface.offset.y;
2387 float origX = surface.offset.x + camera.origin.x;
2388 float origY = surface.offset.y + camera.origin.y;
2390 int y = display.height - bottom - 1;
2391 int w = right - left + 1;
2392 int h = bottom - top + 1;
2395 glViewport(x, y, w, h);
2397 // *** Projection Matrix ***
2398 if(!display.display3D.camera)
2401 glMatrixMode(GL_PROJECTION);
2402 if(display.display3D.collectingHits)
2404 float pickX = display.display3D.pickX + surface.offset.x;
2405 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
2409 w / display.display3D.pickWidth, 0, 0, 0,
2410 0, h / display.display3D.pickHeight, 0, 0,
2412 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
2413 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
2416 glLoadMatrixd(pickMatrix.array);
2421 (left - origX) * camera.zMin / camera.focalX,
2422 (right - origX) * camera.zMin / camera.focalX,
2423 (bottom - origY) * camera.zMin / camera.focalY,
2424 (top - origY) * camera.zMin / camera.focalY,
2425 camera.zMin, camera.zMax);
2427 glDisable(GL_BLEND);
2429 // *** Z Inverted Identity Matrix ***
2430 glMatrixMode(GL_MODELVIEW);
2431 if(!display.display3D.camera)
2435 glScalef(1.0f, 1.0f, -1.0f);
2437 // *** View Matrix ***
2438 glMultMatrixd(camera.viewMatrix.array);
2443 glEnable(GL_DEPTH_TEST);
2444 glEnable(GL_LIGHTING);
2445 glShadeModel(GL_SMOOTH);
2446 glDepthMask((byte)bool::true);
2447 oglDisplay.depthWrite = true;
2449 glEnable(GL_MULTISAMPLE_ARB);
2451 else if(display.display3D.camera)
2453 oglDisplay.depthWrite = false;
2454 glViewport(0,0,display.width,display.height);
2456 glDisable(GL_CULL_FACE);
2457 glDisable(GL_DEPTH_TEST);
2458 glDisable(GL_LIGHTING);
2460 glDisable(GL_TEXTURE_2D);
2461 glShadeModel(GL_FLAT);
2463 glDisable(GL_MULTISAMPLE_ARB);
2465 // *** Restore 2D MODELVIEW Matrix ***
2468 // *** Restore 2D PROJECTION Matrix ***
2469 glMatrixMode(GL_PROJECTION);
2474 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
2477 void ApplyMaterial(Display display, Material material, Mesh mesh)
2479 //Logf("ApplyMaterial\n");
2482 if(material.flags.doubleSided)
2484 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
2485 glDisable(GL_CULL_FACE);
2489 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
2490 glEnable(GL_CULL_FACE);
2494 if(material.flags.noFog)
2500 if(material.baseMap && mesh.texCoords)
2502 Bitmap map = material.baseMap;
2503 glEnable(GL_TEXTURE_2D);
2504 glBindTexture(GL_TEXTURE_2D, (uint)map.driverData);
2506 if(material.flags.tile)
2508 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2509 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2513 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2514 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2518 glDisable(GL_TEXTURE_2D);
2520 if(mesh.flags.colors)
2522 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
2523 glEnable(GL_COLOR_MATERIAL);
2527 glDisable(GL_COLOR_MATERIAL);
2529 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
2530 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
2533 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
2534 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
2538 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
2539 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
2542 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
2543 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
2546 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
2549 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
2551 OGLMesh oglMesh = mesh.data;
2554 if(!mesh.flags.vertices)
2556 if(oglMesh.vertices)
2558 glDeleteBuffersARB(1, &oglMesh.vertices);
2559 oglMesh.vertices = 0;
2561 delete mesh.vertices;
2563 if(!mesh.flags.normals)
2567 glDeleteBuffersARB(1, &oglMesh.normals);
2568 oglMesh.normals = 0;
2570 delete mesh.normals;
2572 if(!mesh.flags.texCoords1)
2574 if(oglMesh.texCoords)
2576 glDeleteBuffersARB(1, &oglMesh.texCoords);
2577 oglMesh.texCoords = 0;
2579 delete mesh.texCoords;
2581 if(!mesh.flags.texCoords2)
2583 if(oglMesh.texCoords2)
2585 glDeleteBuffersARB(1, &oglMesh.texCoords2);
2586 oglMesh.texCoords2 = 0;
2589 delete mesh.texCoords2;
2592 if(!mesh.flags.colors)
2596 glDeleteBuffersARB(1, &oglMesh.colors);
2608 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
2610 bool result = false;
2613 mesh.data = OGLMesh { };
2616 OGLMesh oglMesh = mesh.data;
2618 if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
2620 mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
2622 glGenBuffersARB(1, &oglMesh.vertices);
2624 if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
2627 glGenBuffersARB( 1, &oglMesh.normals);
2628 mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
2630 if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
2633 glGenBuffersARB( 1, &oglMesh.texCoords);
2634 mesh.texCoords = new Pointf[mesh.nVertices];
2636 if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
2639 glGenBuffersARB( 1, &oglMesh.colors);
2640 mesh.colors = new ColorRGBAf[mesh.nVertices];
2647 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
2649 OGLMesh oglMesh = mesh.data;
2650 if(!flags) flags = mesh.flags;
2654 if(!(flags.vertices) || oglMesh.vertices)
2656 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
2657 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
2660 if(!(flags.normals) || oglMesh.normals)
2662 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.normals);
2663 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
2666 if(!(flags.texCoords1) || oglMesh.texCoords)
2668 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
2669 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
2672 if(!(flags.colors) || oglMesh.colors)
2674 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
2675 glBufferDataARB( GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
2678 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
2682 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
2689 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
2693 if(oglIndices.buffer)
2694 glDeleteBuffersARB(1, &oglIndices.buffer);
2695 delete oglIndices.indices;
2700 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
2702 OGLIndices oglIndices = OGLIndices { };
2705 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
2707 glGenBuffersARB( 1, &oglIndices.buffer);
2708 oglIndices.nIndices = nIndices;
2713 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
2717 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
2718 glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
2719 oglIndices.indices, GL_STATIC_DRAW_ARB);
2720 glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
2724 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
2727 return oglIndices.indices;
2730 void SelectMesh(Display display, Mesh mesh)
2732 //Logf("SelectMesh\n");
2734 if(display.display3D.mesh && glUnlockArraysEXT)
2735 glUnlockArraysEXT();
2739 OGLDisplay oglDisplay = display.driverData;
2740 OGLMesh oglMesh = mesh.data;
2742 // *** Vertex Stream ***
2743 glEnableClientState(GL_VERTEX_ARRAY);
2744 if(!display.display3D.collectingHits && oglMesh)
2747 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.vertices );
2748 glVertexPointer(3, mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.vertices);
2750 // *** Normals Stream ***
2753 glEnableClientState(GL_NORMAL_ARRAY);
2755 glBindBufferARB(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
2756 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, glBindBufferARB ? null : mesh.normals);
2759 glDisableClientState(GL_NORMAL_ARRAY);
2761 // *** Texture Coordinates Stream ***
2764 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2766 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
2767 glTexCoordPointer(2, GL_FLOAT, 0, glBindBufferARB ? null : mesh.texCoords);
2770 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2772 // *** Color Stream ***
2775 glEnableClientState(GL_COLOR_ARRAY);
2777 glBindBufferARB( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
2778 glColorPointer(4, GL_FLOAT, 0, glBindBufferARB ? null : mesh.colors);
2781 glDisableClientState(GL_COLOR_ARRAY);
2787 glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
2788 glVertexPointer(3,mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT,0,mesh.vertices);
2789 if(mesh.normals && !display.display3D.collectingHits)
2791 glEnableClientState(GL_NORMAL_ARRAY);
2792 glNormalPointer(mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, 0, mesh.normals);
2795 glDisableClientState(GL_NORMAL_ARRAY);
2796 if(mesh.texCoords && !display.display3D.collectingHits)
2798 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2799 glTexCoordPointer(2, GL_FLOAT, 0, mesh.texCoords);
2802 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2803 if(mesh.colors && !display.display3D.collectingHits)
2805 glEnableClientState(GL_COLOR_ARRAY);
2806 glColorPointer(4, GL_FLOAT, 0, mesh.colors);
2809 glDisableClientState(GL_COLOR_ARRAY);
2812 if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
2814 else if(glBindBufferARB)
2815 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
2818 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
2820 OGLDisplay oglDisplay = display.driverData;
2821 //Logf("DrawPrimitives\n");
2823 if(primitive->type.vertexRange)
2824 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
2827 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
2828 // HACK TO SPEED THINGS UP...
2829 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
2832 glBegin(primitiveTypes[primitive->type.primitiveType]);
2835 OGLIndices oglIndices = primitive->data;
2836 MeshFeatures flags = mesh.flags;
2837 for(c = 0; c<primitive->nIndices; c++)
2839 short index = ((short *) oglIndices.indices)[c];
2840 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
2841 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
2842 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
2843 glVertex3fv((float *)&mesh.vertices[index]);
2850 OGLIndices oglIndices = primitive->data;
2852 if(!display.display3D.collectingHits && glBindBufferARB && oglIndices)
2854 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, oglIndices.buffer);
2855 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
2856 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
2857 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
2860 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
2861 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, oglIndices.indices);
2863 glDrawElements(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
2864 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, primitive->indices);
2869 void PushMatrix(Display display)
2874 void PopMatrix(Display display, bool setMatrix)
2879 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
2881 Matrix matrix = transMatrix;
2882 Camera camera = useCamera ? display.display3D.camera : null;
2887 glScalef(1.0f, 1.0f, -1.0f);
2892 matrix.m[3][0] - camera.cPosition.x,
2893 matrix.m[3][1] - camera.cPosition.y,
2894 matrix.m[3][2] - camera.cPosition.z);
2906 glMultMatrixd(matrix.array);
2910 public void UseSingleGLContext(bool useSingle)
2912 useSingleGLContext = useSingle;