1 namespace gfx::drivers;
9 #define GL_BGRA_EXT 0x80E1
11 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
13 # include "gl_core_3_3.h"
15 # include "gl_compat_4_4.h"
26 #undef glEnableClientState
27 #undef glDisableClientState
28 #undef GL_VERTEX_ARRAY
29 #undef GL_NORMAL_ARRAY
30 #undef GL_TEXTURE_COORD_ARRAY
33 #define glEnableClientState glEnableVertexAttribArray
34 #define glDisableClientState glDisableVertexAttribArray
35 #define GL_VERTEX_ARRAY GLBufferContents::vertex
36 #define GL_NORMAL_ARRAY GLBufferContents::normal
37 #define GL_TEXTURE_COORD_ARRAY GLBufferContents::texCoord
38 #define GL_COLOR_ARRAY GLBufferContents::color
42 // We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
44 #if defined(__unix__) || defined(__APPLE__)
46 #if !defined(__MINGW32__)
47 #define GL_GLEXT_PROTOTYPES
50 #define pointer _pointer
53 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
55 #define property _property
59 #define Window X11Window
60 #define Cursor X11Cursor
62 #define Display X11Display
64 #define KeyCode X11KeyCode
65 #define Picture X11Picture
66 #define Glyph X11Glyph
70 #include <X11/Xutil.h>
72 #include <X11/extensions/XShm.h>
75 #include <X11/extensions/Xrender.h>
76 #include <X11/extensions/shape.h>
95 #if defined(__APPLE__)
96 #include <OpenGl/gl.h>
99 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
101 #if defined(__WIN32__)
102 //#define WIN32_LEAN_AND_MEAN
104 #define _WIN32_WINNT 0x0502
105 #define String Sting_
110 #if defined(__ANDROID__) || defined(__ODROID__)
113 #define property _property
116 #define Window X11Window
117 #define Cursor X11Cursor
119 #define Display X11Display
121 #define KeyCode X11KeyCode
122 #define Picture X11Picture
140 #elif defined(__EMSCRIPTEN__)
142 #define property _property
147 //#include <GLES/gl.h>
148 //#include <EGL/egl.h>
150 //#include <GLES2/gl.h>
151 //#include <EGL/egl.h>
153 //#include <GLES2/gl2.h>
155 #include <emscripten/emscripten.h>
164 #if defined(__ODROID__) && !defined(ES1_1)
168 #if defined(__EMSCRIPTEN__)
177 #if defined(__unix__) || defined(__APPLE__)
179 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
185 #define glLoadMatrix glLoadMatrixd
186 #define glMultMatrix glMultMatrixd
187 #define glGetMatrix glGetDoublev
188 #define glTranslate glTranslated
189 #define glScale glScaled
192 #define glVertex3v glVertex3dv
193 #define glNormal3v glNormal3dv
197 //#ifdef VERTEX_FORMAT_DOUBLE
199 #define glLoadMatrix glLoadMatrixd
200 #define glMultMatrix glMultMatrixd
201 #define glGetMatrix glGetDoublev
202 #define glVertex3v glVertex3dv
203 #define glNormal3v glNormal3dv
204 #define glTranslate glTranslated
205 #define glScale glScaled
206 //#define GL_VERTEX_FORMAT GL_DOUBLE
210 #define glLoadMatrix glLoadMatrixf
211 #define glMultMatrix glMultMatrixf
212 #define glGetMatrix glGetFloatv
213 #define glVertex3v glVertex3fv
214 #define glNormal3v glNormal3fv
215 #define glTranslate glTranslatef
216 #define glScale glScalef
217 //#define GL_VERTEX_FORMAT GL_FLOAT
222 #define GL_ARRAY_BUFFER_ARB 0x8892
223 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
224 #define GL_STATIC_DRAW_ARB 0x88E4
225 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
226 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
228 #define GL_MULTISAMPLE_ARB 0x809D
230 #if defined(__WIN32__)
233 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
234 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
236 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
237 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
239 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
240 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
241 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
242 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
243 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
244 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
245 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
246 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
247 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
248 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = null;
249 static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = null;
251 #elif defined(__ANDROID__) || defined(__ODROID__)
253 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
254 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
255 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
257 #define GL_POLYGON_STIPPLE 0xFFFF
258 #define GL_LINE_STIPPLE 0xFFFF
259 #define GL_LINE 0xFFFF
260 #define GL_FILL 0xFFFF
261 #define GL_ALL_ATTRIB_BITS 0xFFFF
262 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
268 #define GL_QUAD_STRIP 0
269 //#define GL_DOUBLE 0
270 //#define GL_UNSIGNED_INT 0
273 //#define GL_LINE_STIPPLE 0
274 #define GL_BGRA_EXT 0
275 #define GL_UNPACK_ROW_LENGTH 0
276 #define GL_UNPACK_SKIP_PIXELS 0
277 #define GL_UNPACK_SKIP_ROWS 0
279 #define GL_PACK_ROW_LENGTH 0
280 #define GL_PACK_SKIP_ROWS 0
281 #define GL_PACK_SKIP_PIXELS 0
286 #if defined(__ANDROID__) || defined(__ODROID__)
287 #define glBindFramebuffer glBindFramebufferOES
288 #define glBindRenderbuffer glBindRenderbufferOES
289 #define glFramebufferTexture2D glFramebufferTexture2DOES
290 #define glGenFramebuffers glGenFramebuffersOES
291 #define glGenRenderbuffers glGenRenderbuffersOES
292 #define glDeleteFramebuffers glDeleteFramebuffersOES
293 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
295 #define GL_INT 0x1404
296 #define GL_UNSIGNED_INT 0x1405
297 #define GL_DOUBLE 0x140A
301 #if defined(ES1_1) || defined(SHADERS)
332 #undef glLoadIdentity
337 #undef glColorMaterial
340 #define glRecti glimtkRecti
341 #define glBegin glimtkBegin
342 #define glTexCoord2i glimtkTexCoord2i
343 #define glVertex2i glimtkVertex2i
344 #define glTexCoord2d glimtkTexCoord2d
345 #define glVertex2d glimtkVertex2d
346 #define glTexCoord2f glimtkTexCoord2f
347 #define glVertex2f glimtkVertex2f
348 #define glEnd glimtkEnd
349 #define glColor3f glimtkColor3f
350 #define glColor4ub glimtkColor4ub
351 #define glColor4fv glimtkColor4fv
352 #define glNormal3fv glimtkNormal3fv
353 #define glNormal3f glimtkNormal3f
354 #define glTexCoord2fv glimtkTexCoord2fv
355 #define glVertex3d glimtkVertex3d
356 #define glVertex3dv glimtkVertex3dv
357 #define glVertex3f glimtkVertex3f
358 #define glVertex3fv glimtkVertex3fv
360 #define glLoadMatrixd glmsLoadMatrixd
361 #define glMultMatrixd glmsMultMatrixd
362 #define glFrustum glmsFrustum
363 #define glOrtho glmsOrtho
364 #define glScaled glmsScaled
365 #define glScalef glmsScaled
366 #define glTranslated glmsTranslated
367 #define glRotated glmsRotated
368 #define glMatrixMode glmsMatrixMode
369 #define glLoadIdentity glmsLoadIdentity
370 #define glPushMatrix glmsPushMatrix
371 #define glPopMatrix glmsPopMatrix
373 #define glLineStipple glesLineStipple
374 #define glColorMaterial glesColorMaterial
375 #define glLightModeli glesLightModeli
379 public void glesColorMaterial(int a, int b)
381 PrintLn("glColorMaterial stub");
384 static GLuint stippleTexture;
385 #if defined(ES1_1) || defined(SHADERS) || defined(EM_MODE)
386 static bool stippleEnabled;
389 public void glesLineStipple( int i, unsigned short j )
393 for(x = 0; x < 16; x++)
395 bool v = (j & (1 << x)) != 0;
396 texture[x] = v ? 0xFFFFFFFF : 0;
399 glGenTextures(1, &stippleTexture);
400 glBindTexture(GL_TEXTURE_2D, stippleTexture);
401 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
403 // TOOD: Special shading code for stippling?
404 GLSetupTexturing(true);
405 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
409 glMatrixMode(GL_TEXTURE);
411 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
412 glScaled(i/16.0, 1, 1.0f);
413 glTranslated(0.5, 0.5, 0);
414 glMatrixMode(MatrixMode::projection);
417 public void glesLightModeli( unsigned int pname, int param )
419 #if !defined(EM_MODE) && !defined(SHADERS)
420 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
421 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
425 #if defined(__ANDROID__) || defined(__ODROID__)
426 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
427 void glFogi( unsigned int pname, int param ) { }
428 void glPolygonMode( unsigned int i, unsigned int j ) { }
431 // *** Picking won't be supported for now ***
432 void glPushName( unsigned int i ) { }
433 void glLoadName( unsigned int i ) { }
436 // Probably replace by regular glBlendFunc ...
437 void glBlendFuncSeparate(int a, int b, int c, int d)
442 // For direct pixel blitting...
443 void glRasterPos2d(double a, double b) { }
444 void glPixelZoom(float a, float b) { }
445 void glDrawPixels(int a, int b, int c, int d, void * e) { }
449 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
450 static int primitiveTypes[RenderPrimitiveType] =
452 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GLIMTKMode::quads, GLIMTKMode::quadStrip, GL_LINE_STRIP
456 public void GLSetupTexturing(bool enable)
459 shader_texturing(enable);
461 (enable ? glEnable : glDisable)(GL_TEXTURE_2D);
466 // Non OpenGL ES friendly stuff
470 //#undef GL_UNSIGNED_INT
476 #undef GL_POLYGON_STIPPLE
477 #undef GL_LINE_STIPPLE
480 #undef GL_ALL_ATTRIB_BITS
481 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
485 static int displayWidth, displayHeight;
487 #define GL_CLAMP_TO_EDGE 0x812F
489 static bool vboAvailable;
491 static bool useSingleGLContext = false;
492 class OGLDisplay : struct
494 #if defined(__WIN32__)
505 byte * pboMemory1, * pboMemory2;
507 #elif !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
508 GLXContext glContext;
511 XShmSegmentInfo shminfo;
513 XShmSegmentInfo shminfoShape;
518 X11Picture windowPicture;
519 X11Picture pixmapPicture;
521 X11Picture shapePicture;
524 ColorAlpha * flippingBuffer;
525 int flipBufH, flipBufW;
531 static void APIENTRY openglCallbackFunction(GLenum source,
536 const GLchar* message,
537 const void* userParam)
539 PrintLn("---------------------opengl-callback-start------------");
540 PrintLn("message: ", message);
544 case GL_DEBUG_TYPE_ERROR: PrintLn("ERROR"); break;
545 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: PrintLn("DEPRECATED_BEHAVIOR"); break;
546 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: PrintLn("UNDEFINED_BEHAVIOR"); break;
547 case GL_DEBUG_TYPE_PORTABILITY: PrintLn("PORTABILITY"); break;
548 case GL_DEBUG_TYPE_PERFORMANCE: PrintLn("PERFORMANCE"); break;
549 case GL_DEBUG_TYPE_OTHER: PrintLn("OTHER"); break;
556 case GL_DEBUG_SEVERITY_LOW: PrintLn("LOW"); break;
557 case GL_DEBUG_SEVERITY_MEDIUM: PrintLn("MEDIUM"); break;
558 case GL_DEBUG_SEVERITY_HIGH: PrintLn("HIGH"); break;
560 PrintLn("---------------------opengl-callback-end--------------");
564 class OGLSystem : struct
569 #if defined(__WIN32__)
570 PIXELFORMATDESCRIPTOR pfd;
575 #elif !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
576 XVisualInfo * visualInfo;
577 GLXContext glContext;
578 GLXDrawable glxDrawable;
582 class OGLSurface : struct
590 float foreground[4], background[4], bitmapMult[4];
593 class OGLMesh : struct
602 class OGLIndices : struct
612 static void setupDebugging()
615 if(glDebugMessageCallback)
617 GLuint unusedIds = 0;
619 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
620 glDebugMessageCallback(openglCallbackFunction, null);
621 glDebugMessageControl(GL_DONT_CARE,
631 #if defined(__WIN32__)
632 static HGLRC winCreateContext(HDC hdc)
634 if(wglCreateContextAttribsARB)
638 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
639 WGL_CONTEXT_MINOR_VERSION_ARB, 4,
640 WGL_CONTEXT_FLAGS_ARB, /*WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | */WGL_CONTEXT_DEBUG_BIT_ARB,
641 WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB /*WGL_CONTEXT_CORE_PROFILE_BIT_ARB*/,
644 return wglCreateContextAttribsARB(hdc, null, attribs);
647 return wglCreateContext(hdc);
651 class OpenGLDisplayDriver : DisplayDriver
653 class_property(name) = "OpenGL";
655 bool LockSystem(DisplaySystem displaySystem)
657 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
658 OGLSystem oglSystem = displaySystem.driverData;
659 if(useSingleGLContext) return true;
660 #if defined(__WIN32__)
661 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
662 #elif defined(__unix__) || defined(__APPLE__)
663 //if(previous) return true;
664 // printf("Making SYSTEM current\n");
665 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
666 //previous = oglSystem.glContext;
669 GLABBindBuffer(GL_ARRAY_BUFFER, 0);
670 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
674 void UnlockSystem(DisplaySystem displaySystem)
676 if(useSingleGLContext) return;
677 #if defined(__WIN32__)
678 wglMakeCurrent(null, null);
679 #elif defined(__unix__) || defined(__APPLE__)
680 // printf("Making NULL current\n");
681 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
683 glXMakeCurrent(xGlobalDisplay, None, null);
689 bool Lock(Display display)
691 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
692 OGLDisplay oglDisplay = display.driverData;
693 if(useSingleGLContext) return true;
694 #if defined(__WIN32__)
695 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
696 #elif defined(__unix__) || defined(__APPLE__)
697 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
698 // printf(" Making DISPLAY current\n");
699 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
702 GLABBindBuffer(GL_ARRAY_BUFFER, 0);
703 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
707 void Unlock(Display display)
709 if(useSingleGLContext) return;
710 //printf(" Making NULL current\n");
711 //glXMakeCurrent(xGlobalDisplay, None, null);
713 LockSystem(display.displaySystem);
716 void DestroyDisplay(Display display)
718 OGLDisplay oglDisplay = display.driverData;
722 #if defined(__WIN32__)
723 wglMakeCurrent( null, null );
726 wglDeleteContext(oglDisplay.glrc);
728 if(oglDisplay.hdc && oglDisplay.pBuffer)
729 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
731 if(oglDisplay.pBuffer)
732 wglDestroyPbufferARB(oglDisplay.pBuffer);
735 ReleaseDC(display.window, oglDisplay.hdc);
737 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
738 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
740 #elif defined(__unix__) || defined(__APPLE__)
741 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
743 if(oglDisplay.shapePixmap)
744 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
745 if(oglDisplay.pixmap)
746 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
749 if(oglDisplay.shminfoShape.shmid != -1)
751 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
752 if(oglDisplay.shminfo.shmaddr != (void *)-1)
753 shmdt(oglDisplay.shminfo.shmaddr);
754 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
757 if(oglDisplay.shapeImage)
759 if(oglDisplay.shminfoShape.shmid != -1)
761 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
762 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
763 shmdt(oglDisplay.shminfoShape.shmaddr);
764 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
766 XDestroyImage(oglDisplay.shapeImage);
767 oglDisplay.shapeImage = None;
770 glXMakeCurrent(xGlobalDisplay, None, null);
772 if(oglDisplay.glContext)
773 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
776 delete oglDisplay.flippingBuffer;
778 display.driverData = null;
782 void ::CheckExtensions(OGLSystem oglSystem)
784 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
786 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
787 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
790 bool CreateDisplaySystem(DisplaySystem displaySystem)
793 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
796 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
798 oglSystem.hdc = GetDC(oglSystem.hwnd);
802 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
803 oglSystem.pfd.nVersion = 1;
804 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
805 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
806 oglSystem.pfd.cColorBits = 24;
807 oglSystem.pfd.cAlphaBits = 8;
808 oglSystem.pfd.cDepthBits = 24;
809 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
811 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
812 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
814 if(oglSystem.pfd.cColorBits > 8)
816 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
817 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
820 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
822 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
823 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
824 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
825 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
826 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
827 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
828 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
829 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
830 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
831 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
832 wglCreateContextAttribsARB = (void *)wglGetProcAddress("wglCreateContextAttribsARB");
834 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
835 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
837 // eSystem_LoggingMode(LOG_MSGBOX, null);
839 if(wglChoosePixelFormatARB)
844 float fAttributes[] = {0,0};
847 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
848 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
849 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
850 WGL_COLOR_BITS_ARB,24,
851 WGL_ALPHA_BITS_ARB,8,
852 WGL_DEPTH_BITS_ARB,16,
853 WGL_STENCIL_BITS_ARB,0,
854 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
855 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
856 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
860 //Log("Found wglChoosePixelFormatARB\n");
862 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
863 if(!valid || !numFormats)
865 //Log("Can't find 4x multi sampling\n");
867 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
868 if(!valid || !numFormats)
870 // Log("Can't find 2x multi sampling\n");
873 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
876 if(valid && numFormats)
878 oglSystem.format = pixelFormat;
879 wglMakeCurrent(null, null);
880 wglDeleteContext(oglSystem.glrc);
882 // *** DescribePixelFormat does not support WGL pixel formats! ***
883 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
884 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
885 //Log("Successfully set pixel format\n");
887 oglSystem.glrc = winCreateContext(oglSystem.hdc);
888 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
892 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
896 wglMakeCurrent(null, null);
898 //eSystem_DumpErrors(true);
902 #elif defined(__unix__) || defined(__APPLE__)
904 #if defined(__ANDROID__)
905 egl_init_display(guiApp.desktop.windowHandle);
906 #elif defined(__ODROID__)
907 egl_init_display((uint)displaySystem.window);
908 CheckExtensions(oglSystem);
910 #elif defined(__EMSCRIPTEN__)
911 if(glfwInit() == GL_TRUE)
913 const int width = 640, height = 480;
914 if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
920 printf("glfwOpenWindow() failed\n"); //glfwTerminate();
923 printf("glfwInit() failed\n"); //glfwTerminate();
926 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
927 XSetWindowAttributes attr;
932 #ifndef ECERE_MINIGLX
933 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
936 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
940 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
941 attr.background_pixel = 0;
942 attr.border_pixel = 0;
943 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
944 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
945 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
947 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
948 oglSystem.visualInfo->visual, mask, &attr );
950 if(oglSystem.visualInfo)
952 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
953 if(oglSystem.glContext)
955 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
957 CheckExtensions(oglSystem);
958 glXMakeCurrent(xGlobalDisplay, None, null);
965 displaySystem.flags.alpha = true;
966 displaySystem.flags.flipping = true;
967 displaySystem.pixelFormat = pixelFormat888;
971 void DestroyDisplaySystem(DisplaySystem displaySystem)
973 OGLSystem oglSystem = displaySystem.driverData;
975 #if defined(__WIN32__)
976 wglMakeCurrent( null, null );
979 wglDeleteContext(oglSystem.glrc);
982 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
983 DestroyWindow(oglSystem.hwnd);
985 #elif defined(__unix__) || defined(__APPLE__)
986 #if defined(__ANDROID__) || defined(__ODROID__)
988 #elif defined(__EMSCRIPTEN__)
991 if(oglSystem.visualInfo)
994 __miniglx_XFree(oglSystem.visualInfo);
996 XFree(oglSystem.visualInfo);
1000 if(oglSystem.glxDrawable)
1002 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
1003 oglSystem.glxDrawable = 0;
1010 bool CreateDisplay(Display display)
1012 bool result = false;
1013 OGLDisplay oglDisplay = display.driverData;
1014 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
1015 OGLSystem oglSystem = display.displaySystem.driverData;
1019 oglDisplay = display.driverData = OGLDisplay { };
1020 //printf("Inside CreateDisplay\n");
1022 #if defined(__WIN32__) || defined(USEPBUFFER)
1023 if(!display.alphaBlend)
1026 #if defined(__WIN32__)
1027 oglDisplay.hdc = GetDC(display.window);
1028 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1029 if((oglDisplay.glrc = winCreateContext(oglDisplay.hdc)))
1031 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1032 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1036 ReleaseDC(display.window, oglDisplay.hdc);
1037 #elif defined(__unix__) || defined(__APPLE__)
1038 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
1040 XVisualInfo * visualInfo = ((XWindowData)display.windowDriverData).visual;
1042 #if defined(__APPLE__)
1043 XVisualInfo template = { 0 };
1044 XWindowAttributes winAttr;
1046 XGetWindowAttributes(xGlobalDisplay, (X11Window)display.window, &winAttr);
1047 template.visualid = XVisualIDFromVisual(winAttr.visual);
1048 visualInfo = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &template, &n);
1050 printf("XGetVisualInfo visual ID = %d\n", template.visualid);
1051 printf("visualInfo visual ID = %d\n", visualInfo->visualid);
1052 printf("oglSystem.visualInfo visual ID = %d\n", oglSystem.visualInfo->visualid);
1053 printf("((XWindowData)display.windowDriverData).visual visual ID = %d\n", ((XWindowData)display.windowDriverData).visual->visualid);
1055 // visualInfo = oglSystem.visualInfo;
1060 //printf("visualInfo is not null\n");
1061 // printf("Creating Display Context, sharing with %x!\n", oglSystem.glContext);
1062 oglDisplay.glContext = glXCreateContext(xGlobalDisplay, visualInfo, oglSystem.glContext, True);
1063 //XFree(visualInfo);
1066 // oglDisplay.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, oglSystem.glContext, True);
1067 if(oglDisplay.glContext)
1069 //printf("CreateDisplay Got a Context\n");
1070 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1076 #if defined(__WIN32__) || defined(USEPBUFFER)
1082 ogl_LoadFunctions();
1083 CheckExtensions(oglSystem);
1084 vboAvailable = glBindBuffer != null;
1087 loadShaders("<:ecere>shaders/fixed.vertex", "<:ecere>shaders/fixed.frag");
1089 glEnableClientState(GL_VERTEX_ARRAY);
1091 GLABBindBuffer(GL_ARRAY_BUFFER, 0);
1092 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1094 #if defined(__WIN32__)
1095 if(glBlendFuncSeparate)
1096 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1098 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1100 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1104 glMatrixMode(MatrixMode::modelView);
1105 glLoadIdentity(); // For setting up GLES stack
1106 glScaled(1.0, 1.0, -1.0);
1107 // glTranslatef(0.375f, 0.375f, 0.0f);
1108 // glTranslatef(-0.625f, -0.625f, 0.0f);
1109 glMatrixMode(MatrixMode::projection);
1110 #if !defined(EM_MODE) && !defined(SHADERS)
1111 glShadeModel(GL_FLAT);
1113 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1114 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1115 glFogi(GL_FOG_MODE, GL_EXP);
1116 glFogf(GL_FOG_DENSITY, 0);
1117 glEnable(GL_NORMALIZE);
1119 glDepthFunc(GL_LESS);
1121 glDisable(GL_MULTISAMPLE_ARB);
1123 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1124 display.ambient = Color { 50,50,50 };
1127 if(!useSingleGLContext)
1129 #if defined(__WIN32__)
1130 wglMakeCurrent(null, null);
1131 #elif defined(__unix__) || defined(__APPLE__)
1132 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
1135 glXMakeCurrent(xGlobalDisplay, None, null);
1141 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1149 bool DisplaySize(Display display, int width, int height)
1151 OGLDisplay oglDisplay = display.driverData;
1153 bool result = false;
1155 //printf("Inside DisplaySize\n");
1156 #if defined(__WIN32__) || defined(USEPBUFFER)
1157 OGLSystem oglSystem = display.displaySystem.driverData;
1158 if(display.alphaBlend)
1160 #if defined(__WIN32__)
1161 const int attributes[]=
1163 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1164 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1166 int pixelFormat = 0;
1167 if(wglChoosePixelFormatARB)
1171 float fAttributes[] = {0,0};
1174 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1175 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1176 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1177 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1178 WGL_COLOR_BITS_ARB,24,
1179 WGL_ALPHA_BITS_ARB,8,
1180 WGL_DEPTH_BITS_ARB,16,
1181 WGL_STENCIL_BITS_ARB,0,
1182 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1183 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1184 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1188 //Log("Found wglChoosePixelFormatARB\n");
1190 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1191 if(!valid || !numFormats)
1193 //Log("Can't find 4x multi sampling\n");
1194 iAttributes[19] = 2;
1195 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1196 if(!valid || !numFormats)
1198 // Log("Can't find 2x multi sampling\n");
1199 iAttributes[16] = 0;
1200 iAttributes[17] = 0;
1201 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1202 if(!valid || !numFormats)
1206 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1207 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1208 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1209 WGL_COLOR_BITS_ARB,24,
1210 WGL_ALPHA_BITS_ARB,8,
1211 WGL_DEPTH_BITS_ARB,16,
1214 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1218 if(valid && numFormats)
1220 wglMakeCurrent(null, null);
1224 wglMakeCurrent( null, null );
1225 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1226 if(oglDisplay.hdc && oglDisplay.pBuffer)
1227 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1229 wglDestroyPbufferARB(oglDisplay.pBuffer);
1231 if(!useSingleGLContext)
1232 wglMakeCurrent( null, null );
1235 wglDeleteContext(oglDisplay.glrc);
1237 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1238 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1239 if((oglDisplay.glrc = winCreateContext(oglDisplay.hdc)))
1242 HDC hdc = GetDC(display.window);
1244 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1245 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1247 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1248 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1250 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1252 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1256 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1257 oglDisplay.memDC = CreateCompatibleDC(hdc);
1258 SetMapMode(oglDisplay.memDC, MM_TEXT);
1259 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1260 info->bmiHeader.biPlanes = 1;
1261 info->bmiHeader.biCompression = BI_RGB;
1262 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1263 info->bmiHeader.biWidth = width;
1264 info->bmiHeader.biHeight = height;
1265 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1268 SelectObject(oglDisplay.memDC, newBitmap);
1269 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1272 PIXELFORMATDESCRIPTOR pfd = { 0 };
1273 pfd.nSize = (short)sizeof(pfd);
1275 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1276 pfd.iPixelType = PFD_TYPE_RGBA;
1277 pfd.cColorBits = 32;
1278 //pfd.cAlphaBits = 8;
1279 pfd.cDepthBits = 24;
1280 pfd.iLayerType = PFD_MAIN_PLANE;
1282 oglDisplay.hdc = oglDisplay.memDC;
1284 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1285 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1286 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1288 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1289 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1290 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1295 const int imageSize = width * height * 4;
1297 glGenBuffersARB(2, oglDisplay.imageBuffers);
1299 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1300 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1301 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1302 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1305 oglDisplay.memBitmap = newBitmap;
1306 oglDisplay.stride = width;
1312 ReleaseDC(display.window, hdc);
1314 #elif defined(__unix__) || defined(__APPLE__)
1315 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
1320 GLX_DOUBLEBUFFER, True,
1326 GLX_STENCIL_SIZE, 1,
1327 //GLX_DEPTH_SIZE, 24,
1328 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1329 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1335 GLX_PBUFFER_WIDTH, width,
1336 GLX_PBUFFER_HEIGHT, height,
1337 GLX_LARGEST_PBUFFER, False,
1341 // choose a pixel format that meets our minimum requirements
1344 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1347 if(oglDisplay.pixmap)
1349 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1350 oglDisplay.pixmap = None;
1352 if(oglDisplay.shapePixmap)
1354 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1355 oglDisplay.shapePixmap = None;
1358 // Free Shared Memory Pixmap
1359 if(oglDisplay.image)
1361 if(oglDisplay.shminfoShape.shmid != -1)
1363 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1364 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1365 shmdt(oglDisplay.shminfo.shmaddr);
1366 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1368 XDestroyImage(oglDisplay.image);
1369 oglDisplay.image = None;
1371 if(oglDisplay.shapeImage)
1373 if(oglDisplay.shminfoShape.shmid != -1)
1375 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1376 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1377 shmdt(oglDisplay.shminfoShape.shmaddr);
1378 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1380 XDestroyImage(oglDisplay.shapeImage);
1381 oglDisplay.shapeImage = None;
1384 if(oglDisplay.windowPicture)
1385 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1386 if(oglDisplay.pixmapPicture)
1387 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1389 if(oglDisplay.pixmap)
1390 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1392 if(oglDisplay.glContext)
1393 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1394 if(oglDisplay.pBuffer)
1395 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1397 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1398 if(oglDisplay.pBuffer)
1400 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1401 if(oglDisplay.glContext)
1403 glXMakeCurrent(xGlobalDisplay, None, null);
1404 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1406 // Initialize Shared Memory Pixmap
1407 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1408 ZPixmap, null, &oglDisplay.shminfo, width, height);
1409 if(oglDisplay.image)
1411 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1412 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1413 if(oglDisplay.shminfo.shmid != -1)
1415 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1416 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1418 oglDisplay.shminfo.readOnly = False;
1419 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1421 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1422 &oglDisplay.shminfo, width, height, 32);
1424 // Initialize Shared Memory Shape Pixmap
1425 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1426 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1427 if(oglDisplay.shapeImage)
1429 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1430 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1431 if(oglDisplay.shminfoShape.shmid != -1)
1433 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1434 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1436 oglDisplay.shminfoShape.readOnly = False;
1437 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1439 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1440 &oglDisplay.shminfoShape, width, height, 1);
1441 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1444 XRenderPictureAttributes attributes = { 0 };
1445 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1446 #if !defined(__APPLE__)
1447 attributes.repeat = RepeatNormal;
1449 attributes.repeat = 1;
1451 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1452 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1453 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1454 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1457 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1458 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1475 CreateDisplay(display);
1476 #if defined(__WIN32__)
1477 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1478 #elif defined(__unix__) || defined(__APPLE__)
1479 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
1483 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1490 if(!result && display.alphaBlend)
1492 printf("Alpha blending windows not supported on this display\n");
1499 glViewport(0,0,width,height);
1500 glMatrixMode(MatrixMode::projection);
1502 glOrtho(0,width,height,0,0.0,1.0);
1503 displayWidth = display.width = width;
1504 displayHeight = display.height = height;
1506 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1508 oglDisplay.flipBufW = width;
1509 oglDisplay.flipBufH = height;
1513 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1516 if(oglDisplay.flippingBuffer || !width || !height)
1522 void DisplayPosition(Display display, int x, int y)
1524 OGLDisplay oglDisplay = display.driverData;
1530 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1534 void RestorePalette(Display display)
1538 void StartUpdate(Display display)
1542 void EndUpdate(Display display)
1546 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1550 void Update(Display display, Box updateBox)
1552 #if defined(__WIN32__) || defined(USEPBUFFER)
1553 OGLDisplay oglDisplay = display.driverData;
1555 //Logf("DisplayScreen\n");
1557 #if !defined(__ANDROID__)
1562 #if defined(__WIN32__) || defined(USEPBUFFER)
1563 if(display.alphaBlend)
1565 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1566 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1567 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1568 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1569 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1572 #if defined(__WIN32__)
1574 POINT point = { oglDisplay.x, oglDisplay.y};
1575 POINT srcPoint = { 0, 0 };
1576 BLENDFUNCTION blend = { 0 };
1578 size.cx = display.width;
1579 size.cy = display.height;
1580 blend.BlendOp = AC_SRC_OVER;
1581 blend.BlendFlags = 0;
1582 blend.SourceConstantAlpha = 255;
1583 blend.AlphaFormat = AC_SRC_ALPHA;
1586 // Process partial images. Mapping the buffer waits for
1587 // outstanding DMA transfers into the buffer to finish.
1588 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1589 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1591 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1592 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
1595 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
1596 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
1599 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
1602 // Unmap the image buffers
1603 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1604 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1606 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1607 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1609 // Bind two different buffer objects and start the glReadPixels
1610 // asynchronously. Each call will return directly after
1611 // starting the DMA transfer.
1612 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1613 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1615 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1616 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1620 #elif defined(__unix__) || defined(__APPLE__)
1621 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
1623 XTransform transform =
1626 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
1627 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
1628 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
1631 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
1632 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1633 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1634 #if !defined(__APPLE__)
1635 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1637 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1639 XFlush(xGlobalDisplay);
1647 #if defined(__WIN32__)
1648 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
1649 SwapBuffers(oglDisplay.hdc);
1650 #elif defined(__unix__) || defined(__APPLE__)
1651 #if defined(__ANDROID__) || defined(__ODROID__)
1652 eglSwapBuffers(eglDisplay, eglSurface);
1653 #elif defined(__EMSCRIPTEN__)
1656 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
1660 //Logf("Out of DisplayScreen\n");
1663 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
1665 if(bitmap.driverData)
1667 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
1668 glDeleteTextures(1, &tex);
1669 bitmap.driverData = 0;
1671 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
1674 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
1676 OGLSystem oglSystem = displaySystem.driverData;
1677 bool result = false;
1679 GLuint glBitmap = 0;
1681 uint w = width, h = height;
1682 if(oglSystem.pow2textures)
1687 w = Min(w, oglSystem.maxTextureSize);
1688 h = Min(h, oglSystem.maxTextureSize);
1690 glGenTextures(1, &glBitmap);
1691 glBindTexture(GL_TEXTURE_2D, glBitmap);
1693 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1695 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1696 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1698 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1699 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1701 #if !defined(SHADERS)
1702 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1705 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
1707 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1708 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1712 bitmap.driverData = (void *)(uintptr)glBitmap;
1713 bitmap.driver = displaySystem.driver;
1721 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
1723 bool result = false;
1724 OGLSystem oglSystem = displaySystem.driverData;
1725 Bitmap convBitmap = bitmap;
1729 convBitmap.Copy(bitmap);
1732 // Pre process the bitmap... First make it 32 bit
1733 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
1736 uint w = bitmap.width, h = bitmap.height;
1737 GLuint glBitmap = 0;
1738 if(oglSystem.pow2textures)
1743 w = Min(w, oglSystem.maxTextureSize);
1744 h = Min(h, oglSystem.maxTextureSize);
1748 while(w * 2 < h) w *= 2;
1749 while(h * 2 < w) h *= 2;
1752 // Switch ARGB to RGBA
1753 //if(bitmap.format != pixelFormatRGBA)
1755 for(c=0; c<bitmap.size; c++)
1757 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
1759 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
1760 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
1763 // convBitmap.pixelFormat = pixelFormat888;
1766 glGenTextures(1, &glBitmap);
1769 //int error = glGetError();
1773 glBindTexture(GL_TEXTURE_2D, glBitmap);
1774 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1776 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
1777 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1779 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1781 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1782 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1784 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1785 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1786 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
1787 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0 );
1790 #if !defined(SHADERS)
1791 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1796 for(level = 0; result && (w >= 1 || h >= 1); level++, w >>= 1, h >>= 1)
1801 if(bitmap.width != w || bitmap.height != h)
1803 mipMap = Bitmap { };
1804 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
1806 Surface mipSurface = mipMap.GetSurface(0,0,null);
1807 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
1817 mipMap = convBitmap;
1824 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1825 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1826 //printf("Calling glTexImage2D\n");
1827 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
1828 //printf("width = %d (Should be %d, %d)\n", width, w, h);
1829 if((error = glGetError()))
1831 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
1832 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
1836 if(mipMap != convBitmap)
1841 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
1842 bitmap.driverData = (void *)(uintptr)glBitmap;
1843 bitmap.driver = displaySystem.driver;
1848 FreeBitmap(displaySystem, bitmap);
1849 else if(oglSystem.loadingFont)
1851 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1852 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1853 oglSystem.loadingFont = false;
1859 void ReleaseSurface(Display display, Surface surface)
1861 glDisable(GL_SCISSOR_TEST);
1862 delete surface.driverData;
1863 surface.driverData = null;
1866 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
1871 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
1873 bool result = false;
1874 OGLSurface oglSurface = surface.driverData = OGLSurface { };
1876 //Logf("GetSurface\n");
1880 if(displayWidth != display.width || displayHeight != display.height)
1882 displayWidth = display.width;
1883 displayHeight = display.height;
1885 glViewport(0,0,display.width,display.height);
1887 glOrtho(0,display.width,display.height,0,0.0,1.0);
1890 surface.offset.x = x;
1891 surface.offset.y = y;
1892 surface.unclippedBox = surface.box = clip;
1893 oglSurface.bitmapMult[0] = 1;
1894 oglSurface.bitmapMult[1] = 1;
1895 oglSurface.bitmapMult[2] = 1;
1896 oglSurface.bitmapMult[3] = 1;
1898 glEnable(GL_SCISSOR_TEST);
1901 (display.height) -(y+clip.bottom)-1,
1902 clip.right-clip.left+1,
1903 clip.bottom-clip.top+1);
1909 void Clip(Display display, Surface surface, Box clip)
1918 box.Clip(surface.unclippedBox);
1922 box = surface.box = surface.unclippedBox;
1923 box.left += surface.offset.x;
1924 box.top += surface.offset.y;
1925 box.right+= surface.offset.x;
1926 box.bottom += surface.offset.y;
1929 box.left,display.height - box.bottom - 1,
1930 box.right-box.left+1, box.bottom-box.top+1);
1933 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
1935 bool result = false;
1936 OGLDisplay oglDisplay = display.driverData;
1937 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
1939 if(oglDisplay.flippingBuffer)
1941 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
1944 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
1950 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1951 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
1952 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1953 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1954 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
1957 for(row = 0; row<h; row++)
1958 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
1965 void SetForeground(Display display, Surface surface, ColorAlpha color)
1967 OGLSurface oglSurface = surface.driverData;
1969 //Logf("SetForeground\n");
1971 oglSurface.foreground[0] = color.color.r/255.0f;
1972 oglSurface.foreground[1] = color.color.g/255.0f;
1973 oglSurface.foreground[2] = color.color.b/255.0f;
1974 //oglSurface.foreground[3] = 1.0f;
1975 oglSurface.foreground[3] = color.a/255.0f;
1977 //if(!oglSurface.foreground[3])printf("bug");
1980 void SetBackground(Display display, Surface surface, ColorAlpha color)
1982 OGLSurface oglSurface = surface.driverData;
1984 //Logf("SetBackground\n");
1986 oglSurface.background[0] = color.color.r/255.0f;
1987 oglSurface.background[1] = color.color.g/255.0f;
1988 oglSurface.background[2] = color.color.b/255.0f;
1989 //oglSurface.background[3] = 1.0;
1990 oglSurface.background[3] = color.a/255.0f;
1993 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
1995 OGLSurface oglSurface = surface.driverData;
1997 oglSurface.bitmapMult[0] = color.color.r/255.0f;
1998 oglSurface.bitmapMult[1] = color.color.g/255.0f;
1999 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2000 oglSurface.bitmapMult[3] = color.a/255.0f;
2003 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2008 void PutPixel(Display display, Surface surface,int x,int y)
2010 OGLSurface oglSurface = surface.driverData;
2012 //Logf("PutPixel\n");
2014 glColor4fv(oglSurface.foreground);
2016 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2017 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2022 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2024 OGLSurface oglSurface = surface.driverData;
2025 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2040 x1 += surface.offset.x;
2041 y1 += surface.offset.y;
2042 x2 += surface.offset.x;
2043 y2 += surface.offset.y;
2047 glColor4fv(oglSurface.foreground);
2049 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2052 glTexCoord2f(0.5f, 0);
2053 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2054 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2055 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2064 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2065 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2071 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2073 OGLSurface oglSurface = surface.driverData;
2074 x1 += surface.offset.x;
2075 y1 += surface.offset.y;
2076 x2 += surface.offset.x;
2077 y2 += surface.offset.y;
2079 //Logf("Rectangle\n");
2081 glColor4fv(oglSurface.foreground);
2082 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2087 glTexCoord2f(0.5f, 0);
2088 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2089 glTexCoord2f(y2-y1 + 0.5f, 0);
2090 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2092 glTexCoord2f(0.5f, 0);
2093 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2094 glTexCoord2f(x2 - x1 + 0.5f, 0);
2095 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2097 glTexCoord2f(0.5f, 0);
2098 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2099 glTexCoord2f(y1 - y2 + 0.5f, 0);
2100 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2102 glTexCoord2f(0.5f, 0);
2103 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2104 glTexCoord2f(x1 - x2 + 0.5f, 0);
2105 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2110 glBegin(GL_LINE_LOOP);
2117 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2118 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2119 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2120 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2125 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2127 OGLSurface oglSurface = surface.driverData;
2130 glColor4fv(oglSurface.background);
2134 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
2135 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
2136 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
2137 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
2140 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2141 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2144 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2145 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2149 void Clear(Display display, Surface surface, ClearType type)
2151 OGLDisplay oglDisplay = display.driverData;
2152 OGLSurface oglSurface = surface.driverData;
2155 if(type != depthBuffer)
2156 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2157 if(type != colorBuffer && !oglDisplay.depthWrite)
2159 glDepthMask((byte)bool::true);
2161 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2162 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2163 if(type != colorBuffer && !oglDisplay.depthWrite)
2165 glDepthMask((byte)bool::false);
2169 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2174 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2176 OGLSurface oglSurface = surface.driverData;
2178 if(!oglSurface.writingText)
2180 // glTranslatef(-0.375f, -0.375f, 0.0f);
2181 GLSetupTexturing(true);
2182 glColor4fv(oglSurface.bitmapMult);
2184 else if(oglSurface.xOffset)
2185 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2187 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2188 glBegin(GLIMTKMode::quads);
2192 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2193 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2194 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2195 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2196 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2197 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2198 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2199 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2204 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2205 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2206 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2207 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2208 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2209 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2210 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2211 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2214 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2215 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2216 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2217 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2218 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2219 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2220 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2221 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2225 if(!oglSurface.writingText)
2227 GLSetupTexturing(false);
2229 //glTranslate(0.375, 0.375, 0.0);
2231 else if(oglSurface.xOffset)
2232 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2235 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2237 OGLSurface oglSurface = surface.driverData;
2239 //glTranslate(-0.375, -0.375, 0.0);
2241 GLSetupTexturing(true);
2242 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2244 glColor4fv(oglSurface.bitmapMult);
2246 glBegin(GLIMTKMode::quads);
2250 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2251 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2253 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2254 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2256 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2257 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2259 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2260 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2264 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2265 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2267 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2268 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2270 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2271 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2273 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2274 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2279 GLSetupTexturing(false);
2281 //glTranslate(0.375, 0.375, 0.0);
2284 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2286 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2289 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2291 #if !defined(EM_MODE)
2292 float s2dw,s2dh,d2sw,d2sh;
2293 //bool flipX = false, flipY = false;
2295 //Logf("StretchDI\n");
2297 if(Sgn(w) != Sgn(sw))
2303 if(Sgn(h) != Sgn(sh))
2315 //Clip against the edges of the source
2318 dx+=(int)((0-sx) * s2dw);
2319 w-=(int)((0-sx) * s2dw);
2325 dy+=(int)((0-sy) * s2dh);
2326 h-=(int)((0-sy) * s2dh);
2331 if(sx+sw>bitmap.width-1)
2333 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2334 sw-=sx+sw-(bitmap.width-1)-1;
2336 if(sy+sh>(bitmap.height-1))
2338 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2339 sh-=sy+sh-(bitmap.height-1)-1;
2341 //Clip against the edges of the surfaceination
2342 if(dx<surface.box.left)
2345 sx+=(int)((surface.box.left-dx)*d2sw);
2346 sw-=(int)((surface.box.left-dx)*d2sw);
2347 w-=surface.box.left-dx;
2348 dx=surface.box.left;
2350 if(dy<surface.box.top)
2352 sy+=(int)((surface.box.top-dy)*d2sh);
2353 sh-=(int)((surface.box.top-dy)*d2sh);
2354 h-=surface.box.top-dy;
2357 if(dx+w>surface.box.right)
2359 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2360 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2361 w-=dx+w-surface.box.right-1;
2363 if(dy+h>surface.box.bottom)
2365 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2366 h-=dy+h-surface.box.bottom-1;
2368 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2370 dx += surface.offset.x;
2371 dy += surface.offset.y;
2373 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2375 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2376 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2377 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2378 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2379 #if !defined(SHADERS)
2380 glRasterPos2d(dx,dy);
2381 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2382 glPixelZoom(s2dw, -s2dh);
2383 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2385 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2386 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2387 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2388 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2393 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2395 #if !defined(EM_MODE)
2398 //Clip against the edges of the source
2411 if(sx+w>bitmap.width-1)
2412 w-=sx+w-(bitmap.width-1)-1;
2413 if(sy+h>bitmap.height-1)
2414 h-=sy+h-(bitmap.height-1)-1;
2415 //Clip against the edges of the surfaceination
2416 if(dx<surface.box.left)
2419 sx+=surface.box.left-dx;
2420 w-=surface.box.left-dx;
2421 dx=surface.box.left;
2423 if(dy<surface.box.top)
2425 sy+=surface.box.top-dy;
2426 h-=surface.box.top-dy;
2429 if(dx+w>surface.box.right)
2431 //if(flip) sx+=dx+w-surface.box.right-1;
2432 w-=dx+w-surface.box.right-1;
2434 if(dy+h>surface.box.bottom)
2435 h-=dy+h-surface.box.bottom-1;
2439 dx += surface.offset.x;
2440 dy += surface.offset.y;
2442 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2444 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2445 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2446 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2447 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2448 #if !defined(SHADERS)
2449 glRasterPos2d(dx,dy);
2451 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2453 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2454 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2455 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2456 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2461 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2463 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2466 void UnloadFont(DisplaySystem displaySystem, Font font)
2468 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2471 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags, float outlineSize, float outlineFade)
2474 OGLSystem oglSystem = displaySystem.driverData;
2475 oglSystem.loadingFont = true;
2476 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags, outlineSize, outlineFade);
2480 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * adv)
2482 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height, prevGlyph, rPrevGlyph, adv);
2485 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len, int prevGlyph, int * rPrevGlyph)
2487 OGLSurface oglSurface = surface.driverData;
2488 OGLSystem oglSystem = display.displaySystem.driverData;
2489 oglSystem.loadingFont = true;
2491 //glTranslated(-0.375, -0.375, 0.0);
2495 if(surface.textOpacity)
2498 FontExtent(display.displaySystem, surface.font, text, len, &w, &h, 0, null, &adv);
2500 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2503 oglSurface.writingText = true;
2505 GLSetupTexturing(true);
2507 if(surface.font.outlineSize)
2509 ColorAlpha outlineColor = surface.outlineColor;
2510 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
2511 oglSurface.writingOutline = true;
2512 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len, prevGlyph, rPrevGlyph);
2513 oglSurface.writingOutline = false;
2515 glColor4fv(oglSurface.foreground);
2517 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len, prevGlyph, rPrevGlyph);
2518 oglSurface.writingText = false;
2519 oglSystem.loadingFont = false;
2521 GLSetupTexturing(false);
2523 //glTranslated(0.375, 0.375, 0.0);
2526 void TextFont(Display display, Surface surface, Font font)
2528 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2531 void TextOpacity(Display display, Surface surface, bool opaque)
2533 OGLSurface oglSurface = surface.driverData;
2534 oglSurface.opaqueText = opaque;
2537 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * adv)
2539 OGLSurface oglSurface = surface.driverData;
2540 OGLSystem oglSystem = display.displaySystem.driverData;
2541 oglSystem.loadingFont = true;
2542 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height, prevGlyph, rPrevGlyph, adv);
2543 oglSystem.loadingFont = false;
2546 void DrawingChar(Display display, Surface surface, char character)
2551 void LineStipple(Display display, Surface surface, uint32 stipple)
2553 //Logf("Stipple\n");
2557 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2558 stippleEnabled = true;
2559 glesLineStipple(1, (uint16)stipple);
2561 glLineStipple(1, (uint16)stipple);
2562 glEnable(GL_LINE_STIPPLE);
2567 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2568 stippleEnabled = false;
2569 glMatrixMode(GL_TEXTURE);
2571 glMatrixMode(MatrixMode::projection);
2572 GLSetupTexturing(false); // TODO: Special shading code for stipple?
2574 glDisable(GL_LINE_STIPPLE);
2578 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
2579 void SetRenderState(Display display, RenderState state, uint value)
2581 OGLDisplay oglDisplay = display.driverData;
2582 //Logf("RenderState\n");
2588 glEnable(GL_MULTISAMPLE_ARB);
2590 glDisable(GL_MULTISAMPLE_ARB);
2594 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2598 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2601 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2602 oglDisplay.depthWrite = (bool)value;
2606 #if !defined(SHADERS)
2607 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2608 glFogfv(GL_FOG_COLOR, (float *)&color);
2613 #if !defined(SHADERS)
2614 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
2618 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2622 #if !defined(EM_MODE) && !defined(SHADERS)
2623 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2624 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2630 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2635 #if defined(__WIN32__)
2636 wglSwapIntervalEXT(value ? 1 : 0);
2643 void SetLight(Display display, int id, Light light)
2645 #if !defined(EM_MODE) && !defined(SHADERS)
2646 //Logf("SetLight\n");
2650 Object lightObject = light.lightObject;
2651 float position[4] = { 0, 0, 0, 0 };
2652 float color[4] = { 0, 0, 0, 1 };
2654 glEnable(GL_LIGHT0 + id);
2656 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2657 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2658 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2661 if(!light.multiplier) light.multiplier = 1.0f;
2663 color[0] = light.diffuse.r * light.multiplier;
2664 color[1] = light.diffuse.g * light.multiplier;
2665 color[2] = light.diffuse.b * light.multiplier;
2666 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2668 color[0] = light.ambient.r * light.multiplier;
2669 color[1] = light.ambient.g * light.multiplier;
2670 color[2] = light.ambient.b * light.multiplier;
2671 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2672 color[0] = light.specular.r * light.multiplier;
2673 color[1] = light.specular.g * light.multiplier;
2674 color[2] = light.specular.b * light.multiplier;
2675 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2679 Vector3D positionVector;
2680 if(light.flags.spot)
2682 if(lightObject.flags.root || !lightObject.parent)
2684 positionVector = lightObject.transform.position;
2685 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2689 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
2690 if(display.display3D.camera)
2691 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2697 if(!light.direction.x && !light.direction.y && !light.direction.z)
2699 Vector3Df vector { 0,0,-1 };
2701 mat.RotationQuaternion(light.orientation);
2702 positionVector.MultMatrixf(vector, mat);
2706 positionVector = light.direction;
2711 position[0] = (float)positionVector.x;
2712 position[1] = (float)positionVector.y;
2713 position[2] = (float)positionVector.z;
2715 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2718 // Display Light Position
2719 glDisable(GL_LIGHTING);
2720 glDisable(GL_DEPTH_TEST);
2724 glVertex3fv(position);
2726 glEnable(GL_DEPTH_TEST);
2727 glEnable(GL_LIGHTING);
2731 if(lightObject.flags.root || !lightObject.parent)
2733 positionVector = light.target.transform.position;
2734 positionVector.Subtract(positionVector, display.camera.cPosition);
2738 positionVector.MultMatrix(light.target.transform.position,
2739 lightObject.light.target.parent.matrix);
2740 positionVector.Subtract(positionVector, display.camera.cPosition);
2743 position[0] = positionVector.x;
2744 position[1] = positionVector.y;
2745 position[2] = positionVector.z;
2747 glDisable(GL_LIGHTING);
2748 glDisable(GL_DEPTH_TEST);
2752 glVertex3fv(position);
2754 glEnable(GL_DEPTH_TEST);
2755 glEnable(GL_LIGHTING);
2758 if(light.flags.attenuation)
2760 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
2761 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
2762 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
2765 if(light.flags.spot)
2768 #define MAXLIGHT 0.9
2769 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
2770 // Figure out exponent out of the hot spot
2771 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
2773 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
2774 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
2775 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
2781 Vector3Df vector { 0,0,-1 };
2782 Vector3Df direction;
2785 mat.RotationQuaternion(light.orientation);
2786 direction.MultMatrix(vector, mat);
2788 position[0] = direction.x;
2789 position[1] = direction.y;
2790 position[2] = direction.z;
2792 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2796 glDisable(GL_LIGHT0 + id);
2800 void SetCamera(Display display, Surface surface, Camera camera)
2802 OGLDisplay oglDisplay = display.driverData;
2803 //Logf("SetCamera\n");
2805 if(surface && camera)
2807 int left = surface.box.left + surface.offset.x;
2808 int top = surface.box.top + surface.offset.y;
2809 int right = surface.box.right + surface.offset.x;
2810 int bottom = surface.box.bottom + surface.offset.y;
2811 float origX = surface.offset.x + camera.origin.x;
2812 float origY = surface.offset.y + camera.origin.y;
2814 int y = display.height - bottom - 1;
2815 int w = right - left + 1;
2816 int h = bottom - top + 1;
2819 glViewport(x, y, w, h);
2821 // *** Projection Matrix ***
2822 glMatrixMode(MatrixMode::projection);
2823 if(!display.display3D.camera)
2826 if(display.display3D.collectingHits)
2828 float pickX = display.display3D.pickX + surface.offset.x;
2829 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
2833 w / display.display3D.pickWidth, 0, 0, 0,
2834 0, h / display.display3D.pickHeight, 0, 0,
2836 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
2837 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
2840 glLoadMatrixd(pickMatrix.array);
2845 (left - origX) * camera.zMin / camera.focalX,
2846 (right - origX) * camera.zMin / camera.focalX,
2847 (bottom - origY) * camera.zMin / camera.focalY,
2848 (top - origY) * camera.zMin / camera.focalY,
2849 camera.zMin, camera.zMax);
2851 glDisable(GL_BLEND);
2853 // *** Z Inverted Identity Matrix ***
2854 glMatrixMode(MatrixMode::modelView);
2855 if(!display.display3D.camera)
2860 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
2862 // *** View Matrix ***
2863 glMultMatrixd(camera.viewMatrix.array);
2868 glEnable(GL_DEPTH_TEST);
2870 #if !defined(EM_MODE) && !defined(SHADERS)
2871 glEnable(GL_LIGHTING);
2872 glShadeModel(GL_SMOOTH);
2874 glDepthMask((byte)bool::true);
2875 oglDisplay.depthWrite = true;
2877 glEnable(GL_MULTISAMPLE_ARB);
2879 else if(surface && display.display3D.camera)
2881 oglDisplay.depthWrite = false;
2882 glViewport(0,0,display.width,display.height);
2884 glDisable(GL_CULL_FACE);
2885 glDisable(GL_DEPTH_TEST);
2886 GLSetupTexturing(false);
2887 #if !defined(EM_MODE) && !defined(SHADERS)
2888 glDisable(GL_LIGHTING);
2890 glShadeModel(GL_FLAT);
2893 glDisable(GL_MULTISAMPLE_ARB);
2895 // *** Restore 2D MODELVIEW Matrix ***
2898 // *** Restore 2D PROJECTION Matrix ***
2899 glMatrixMode(MatrixMode::projection);
2905 void ApplyMaterial(Display display, Material material, Mesh mesh)
2907 //Logf("ApplyMaterial\n");
2910 if(material.flags.doubleSided)
2912 #if !defined(EM_MODE) && !defined(SHADERS)
2913 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
2915 glDisable(GL_CULL_FACE);
2919 #if !defined(EM_MODE) && !defined(SHADERS)
2920 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
2922 glEnable(GL_CULL_FACE);
2925 #if !defined(SHADERS)
2927 if(material.flags.noFog)
2934 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
2936 Bitmap map = material.baseMap;
2937 GLSetupTexturing(true);
2938 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
2940 glMatrixMode(GL_TEXTURE);
2942 if(material.uScale && material.vScale)
2943 glScalef(material.uScale, material.vScale, 1);
2944 glMatrixMode(MatrixMode::modelView);
2946 if(material.flags.tile)
2948 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2949 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2953 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2954 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2958 GLSetupTexturing(false);
2960 #if defined(EM_MODE) || defined(SHADERS)
2961 glimtkColor4f(material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity);
2963 if(mesh.flags.colors)
2965 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
2966 glEnable(GL_COLOR_MATERIAL);
2970 glDisable(GL_COLOR_MATERIAL);
2972 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
2973 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
2976 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
2977 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
2981 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
2982 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
2985 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
2986 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
2989 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
2993 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
2995 OGLMesh oglMesh = mesh.data;
2998 if(!mesh.flags.vertices)
3000 oglMesh.vertices.free();
3001 delete mesh.vertices;
3003 if(!mesh.flags.normals)
3005 oglMesh.normals.free();
3006 delete mesh.normals;
3008 if(!mesh.flags.texCoords1)
3010 oglMesh.texCoords.free();
3011 delete mesh.texCoords;
3013 if(!mesh.flags.texCoords2)
3015 oglMesh.texCoords2.free();
3016 // delete mesh.texCoords2;
3018 if(!mesh.flags.colors)
3020 oglMesh.colors.free();
3031 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3033 bool result = false;
3036 mesh.data = OGLMesh { };
3039 if(mesh.nVertices == nVertices)
3041 // Same number of vertices, adding features (Leaves the other features pointers alone)
3042 if(mesh.flags != flags)
3044 if(!mesh.flags.vertices && flags.vertices)
3046 if(flags.doubleVertices)
3048 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3051 mesh.vertices = new Vector3Df[nVertices];
3053 if(!mesh.flags.normals && flags.normals)
3055 if(flags.doubleNormals)
3057 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3060 mesh.normals = new Vector3Df[nVertices];
3062 if(!mesh.flags.texCoords1 && flags.texCoords1)
3064 mesh.texCoords = new Pointf[nVertices];
3066 if(!mesh.flags.colors && flags.colors)
3068 mesh.colors = new ColorRGBAf[nVertices];
3074 // New number of vertices, reallocate all current and new features
3075 flags |= mesh.flags;
3078 if(flags.doubleVertices)
3080 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3083 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3087 if(flags.doubleNormals)
3089 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3092 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3094 if(flags.texCoords1)
3096 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3100 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3108 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3110 OGLMesh oglMesh = mesh.data;
3111 if(!flags) flags = mesh.flags;
3116 oglMesh.vertices.upload(
3117 mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices); //, GL_STATIC_DRAW_ARB );
3120 oglMesh.normals.upload(
3121 mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals); //, GL_STATIC_DRAW_ARB );
3123 if(flags.texCoords1)
3124 oglMesh.texCoords.upload(
3125 mesh.nVertices * sizeof(Pointf), mesh.texCoords); //, GL_STATIC_DRAW_ARB );
3128 oglMesh.colors.upload(
3129 mesh.nVertices * sizeof(ColorRGBAf), mesh.colors); //, GL_STATIC_DRAW_ARB );
3133 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3140 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3144 oglIndices.buffer.free();
3145 delete oglIndices.indices;
3150 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3152 OGLIndices oglIndices = OGLIndices { };
3155 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3156 oglIndices.nIndices = nIndices;
3161 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3168 if(!oglIndices.buffer.buffer)
3169 glGenBuffers(1, &oglIndices.buffer.buffer);
3170 if(glabCurElementBuffer != oglIndices.buffer.buffer)
3171 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oglIndices.buffer.buffer);
3172 glimtkBufferDatai(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32) * nIndices, oglIndices.indices, GL_STATIC_DRAW_ARB);
3176 oglIndices.buffer.upload(
3177 nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3178 oglIndices.indices); //GL_STATIC_DRAW_ARB);
3182 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3185 return oglIndices.indices;
3188 void SelectMesh(Display display, Mesh mesh)
3190 //Logf("SelectMesh\n");
3192 #if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
3194 #if defined(__WIN32__)
3195 if(glUnlockArraysEXT)
3197 if(!vboAvailable && display.display3D.mesh)
3198 glUnlockArraysEXT();
3203 OGLMesh oglMesh = mesh.data;
3205 // *** Vertex Stream ***
3206 glEnableClientState(GL_VERTEX_ARRAY);
3207 if(!display.display3D.collectingHits && oglMesh)
3209 oglMesh.vertices.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, oglMesh.vertices.buffer ? null : (double *)mesh.vertices);
3211 // *** Normals Stream ***
3212 if(mesh.normals || mesh.flags.normals)
3214 glEnableClientState(GL_NORMAL_ARRAY);
3215 oglMesh.normals.use(normal, 3, GL_FLOAT, 0, oglMesh.normals.buffer ? null : mesh.normals);
3218 glDisableClientState(GL_NORMAL_ARRAY);
3220 // *** Texture Coordinates Stream ***
3221 if(mesh.texCoords || mesh.flags.texCoords1)
3223 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3224 oglMesh.texCoords.use(texCoord, 2, GL_FLOAT, 0, oglMesh.texCoords.buffer ? null : mesh.texCoords);
3227 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3229 // *** Color Stream ***
3230 if(mesh.colors || mesh.flags.colors)
3232 glEnableClientState(GL_COLOR_ARRAY);
3233 oglMesh.colors.use(color, 4, GL_FLOAT, 0, oglMesh.colors.buffer ? null : mesh.colors);
3236 glDisableClientState(GL_COLOR_ARRAY);
3240 noAB.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, (double *)mesh.vertices);
3241 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
3243 glEnableClientState(GL_NORMAL_ARRAY);
3244 noAB.use(normal, 3, GL_FLOAT, 0, mesh.normals);
3247 glDisableClientState(GL_NORMAL_ARRAY);
3248 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
3250 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3251 noAB.use(texCoord, 2, GL_FLOAT, 0, mesh.texCoords);
3254 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3255 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
3257 glEnableClientState(GL_COLOR_ARRAY);
3258 noAB.use(color, 4, GL_FLOAT, 0, mesh.colors);
3261 glDisableClientState(GL_COLOR_ARRAY);
3264 #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
3266 #if defined(__WIN32__)
3270 glLockArraysEXT(0, mesh.nVertices);
3276 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3278 //Logf("DrawPrimitives\n");
3280 if(primitive->type.vertexRange)
3281 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3284 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3285 // HACK TO SPEED THINGS UP...
3287 /*GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3288 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3291 glBegin((GLIMTKMode)primitiveTypes[primitive->type.primitiveType]);
3294 OGLIndices oglIndices = primitive->data;
3295 MeshFeatures flags = mesh.flags;
3296 for(c = 0; c<primitive->nIndices; c++)
3298 uint16 index = ((uint16 *) oglIndices.indices)[c];
3299 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3300 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3301 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3302 glVertex3fv((float *)&mesh.vertices[index]);
3311 OGLIndices oglIndices = primitive->data;
3312 GLEAB eab = ((!display.display3D.collectingHits && oglIndices) ? oglIndices.buffer : noEAB);
3314 eab.draw(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3315 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT,
3316 eab.buffer ? 0 : (oglIndices ? oglIndices.indices : primitive->indices));
3317 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
3322 void PushMatrix(Display display)
3327 void PopMatrix(Display display, bool setMatrix)
3332 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3334 Matrix matrix = transMatrix;
3335 Camera camera = useCamera ? display.display3D.camera : null;
3340 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3345 matrix.m[3][0] - camera.cPosition.x,
3346 matrix.m[3][1] - camera.cPosition.y,
3347 matrix.m[3][2] - camera.cPosition.z);
3359 glMultMatrixd(matrix.array);
3364 public void UseSingleGLContext(bool useSingle)
3366 useSingleGLContext = useSingle;
3369 default dllexport void *
3370 #if defined(__WIN32__)
3371 __attribute__((stdcall))
3373 IS_GLGetContext(DisplaySystem displaySystem)
3377 #if defined(__WIN32__)
3378 OGLSystem system = displaySystem.driverData;
3380 #elif defined(__ANDROID__) || defined(__ODROID__)
3382 #elif defined(__EMSCRIPTEN__)
3384 OGLSystem system = displaySystem.driverData;
3385 return system.glContext;