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(__EMSCRIPTEN__) && !defined(__ODROID__)
55 #define property _property
59 #define Window X11Window
60 #define Cursor X11Cursor
62 #define Display X11Display
64 #define KeyCode X11KeyCode
65 #define Picture X11Picture
69 #include <X11/Xutil.h>
71 #include <X11/extensions/XShm.h>
74 #include <X11/extensions/Xrender.h>
75 #include <X11/extensions/shape.h>
93 #if defined(__APPLE__)
94 #include <OpenGl/gl.h>
97 #if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
99 #if defined(__WIN32__)
100 //#define WIN32_LEAN_AND_MEAN
102 #define _WIN32_WINNT 0x0502
103 #define String Sting_
108 #if defined(__ANDROID__) || defined(__ODROID__)
111 #define property _property
114 #define Window X11Window
115 #define Cursor X11Cursor
117 #define Display X11Display
119 #define KeyCode X11KeyCode
120 #define Picture X11Picture
138 #elif defined(__EMSCRIPTEN__)
140 #define property _property
145 //#include <GLES/gl.h>
146 //#include <EGL/egl.h>
148 //#include <GLES2/gl.h>
149 //#include <EGL/egl.h>
151 //#include <GLES2/gl2.h>
153 #include <emscripten/emscripten.h>
162 #if defined(__ODROID__) && !defined(ES1_1)
166 #if defined(__EMSCRIPTEN__)
177 #if defined(__unix__) || defined(__APPLE__)
179 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
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(__EMSCRIPTEN__) && !defined(__ODROID__)
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(__EMSCRIPTEN__) && !defined(__ODROID__)
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(__EMSCRIPTEN__) && !defined(__ODROID__)
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(__EMSCRIPTEN__) || defined(__ODROID__)
683 glXMakeCurrent(xGlobalDisplay, None, null);
689 bool Lock(Display display)
691 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
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(__EMSCRIPTEN__) || defined(__ODROID__)
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(__EMSCRIPTEN__) && !defined(__ODROID__)
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(__EMSCRIPTEN__) || defined(__ODROID__)
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);
1090 GLABBindBuffer(GL_ARRAY_BUFFER, 0);
1091 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1093 #if defined(__WIN32__)
1094 if(glBlendFuncSeparate)
1095 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1097 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1099 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1103 glMatrixMode(MatrixMode::modelView);
1104 glLoadIdentity(); // For setting up GLES stack
1105 glScaled(1.0, 1.0, -1.0);
1106 // glTranslatef(0.375f, 0.375f, 0.0f);
1107 // glTranslatef(-0.625f, -0.625f, 0.0f);
1108 glMatrixMode(MatrixMode::projection);
1109 #if !defined(EM_MODE) && !defined(SHADERS)
1110 glShadeModel(GL_FLAT);
1112 // #define GL_LIGHT_MODEL_LOCAL_VIEWER 0x0B51
1114 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1115 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1116 glFogi(GL_FOG_MODE, GL_EXP);
1117 glFogf(GL_FOG_DENSITY, 0);
1118 glEnable(GL_NORMALIZE);
1120 glDepthFunc(GL_LESS);
1122 glDisable(GL_MULTISAMPLE_ARB);
1124 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
1125 display.ambient = Color { 50,50,50 };
1128 if(!useSingleGLContext)
1130 #if defined(__WIN32__)
1131 wglMakeCurrent(null, null);
1132 #elif defined(__unix__) || defined(__APPLE__)
1133 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1136 glXMakeCurrent(xGlobalDisplay, None, null);
1142 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1150 bool DisplaySize(Display display, int width, int height)
1152 OGLDisplay oglDisplay = display.driverData;
1154 bool result = false;
1156 //printf("Inside DisplaySize\n");
1157 #if defined(__WIN32__) || defined(USEPBUFFER)
1158 OGLSystem oglSystem = display.displaySystem.driverData;
1159 if(display.alphaBlend)
1161 #if defined(__WIN32__)
1162 const int attributes[]=
1164 /*WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB,
1165 WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, */0
1167 int pixelFormat = 0;
1168 if(wglChoosePixelFormatARB)
1172 float fAttributes[] = {0,0};
1175 //WGL_DRAW_TO_BITMAP_ARB, GL_TRUE,
1176 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1177 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1178 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
1179 WGL_COLOR_BITS_ARB,24,
1180 WGL_ALPHA_BITS_ARB,8,
1181 WGL_DEPTH_BITS_ARB,16,
1182 WGL_STENCIL_BITS_ARB,0,
1183 WGL_DOUBLE_BUFFER_ARB,GL_FALSE,
1184 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
1185 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
1189 //Log("Found wglChoosePixelFormatARB\n");
1191 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1192 if(!valid || !numFormats)
1194 //Log("Can't find 4x multi sampling\n");
1195 iAttributes[19] = 2;
1196 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1197 if(!valid || !numFormats)
1199 // Log("Can't find 2x multi sampling\n");
1200 iAttributes[16] = 0;
1201 iAttributes[17] = 0;
1202 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1203 if(!valid || !numFormats)
1207 WGL_DRAW_TO_PBUFFER_ARB,GL_TRUE,
1208 //WGL_DRAW_TO_BITMAP_ARB,GL_TRUE,
1209 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
1210 WGL_COLOR_BITS_ARB,24,
1211 WGL_ALPHA_BITS_ARB,8,
1212 WGL_DEPTH_BITS_ARB,16,
1215 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
1219 if(valid && numFormats)
1221 wglMakeCurrent(null, null);
1225 wglMakeCurrent( null, null );
1226 wglMakeCurrent( oglDisplay.hdc, oglDisplay.glrc );
1227 if(oglDisplay.hdc && oglDisplay.pBuffer)
1228 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
1230 wglDestroyPbufferARB(oglDisplay.pBuffer);
1232 if(!useSingleGLContext)
1233 wglMakeCurrent( null, null );
1236 wglDeleteContext(oglDisplay.glrc);
1238 oglDisplay.pBuffer = wglCreatePbufferARB(oglSystem.hdc, pixelFormat, width, height, attributes);
1239 oglDisplay.hdc = wglGetPbufferDCARB(oglDisplay.pBuffer);
1240 if((oglDisplay.glrc = winCreateContext(oglDisplay.hdc)))
1243 HDC hdc = GetDC(display.window);
1245 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1246 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1248 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
1249 //wglQueryPbufferARB(pBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
1251 // glDeleteBuffersARB(2, oglDisplay.imageBuffers);
1253 if((info = (BITMAPINFO *)new0 byte[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256]))
1257 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
1258 oglDisplay.memDC = CreateCompatibleDC(hdc);
1259 SetMapMode(oglDisplay.memDC, MM_TEXT);
1260 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1261 info->bmiHeader.biPlanes = 1;
1262 info->bmiHeader.biCompression = BI_RGB;
1263 info->bmiHeader.biBitCount = 32; //(uint16)GetDeviceCaps(hdc, BITSPIXEL);
1264 info->bmiHeader.biWidth = width;
1265 info->bmiHeader.biHeight = height;
1266 newBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, &oglDisplay.picture, null, 0);
1269 SelectObject(oglDisplay.memDC, newBitmap);
1270 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
1273 PIXELFORMATDESCRIPTOR pfd = { 0 };
1274 pfd.nSize = (short)sizeof(pfd);
1276 pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
1277 pfd.iPixelType = PFD_TYPE_RGBA;
1278 pfd.cColorBits = 32;
1279 //pfd.cAlphaBits = 8;
1280 pfd.cDepthBits = 24;
1281 pfd.iLayerType = PFD_MAIN_PLANE;
1283 oglDisplay.hdc = oglDisplay.memDC;
1285 pixelFormat = ChoosePixelFormat(oglSystem.hdc, &pfd);
1286 DescribePixelFormat(oglDisplay.hdc, pixelFormat, sizeof(pfd), &pfd);
1287 SetPixelFormat(oglDisplay.hdc, pixelFormat, &pfd);
1289 oglDisplay.glrc = wglCreateContext(oglDisplay.hdc);
1290 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1291 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1296 const int imageSize = width * height * 4;
1298 glGenBuffersARB(2, oglDisplay.imageBuffers);
1300 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1301 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize, null, GL_STREAM_READ);
1302 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1303 // glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, imageSize / 2, null, GL_STREAM_READ);
1306 oglDisplay.memBitmap = newBitmap;
1307 oglDisplay.stride = width;
1313 ReleaseDC(display.window, hdc);
1315 #elif defined(__unix__) || defined(__APPLE__)
1316 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1321 GLX_DOUBLEBUFFER, True,
1327 GLX_STENCIL_SIZE, 1,
1328 //GLX_DEPTH_SIZE, 24,
1329 GLX_RENDER_TYPE, GLX_RGBA_BIT,
1330 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT | GLX_WINDOW_BIT,
1336 GLX_PBUFFER_WIDTH, width,
1337 GLX_PBUFFER_HEIGHT, height,
1338 GLX_LARGEST_PBUFFER, False,
1342 // choose a pixel format that meets our minimum requirements
1345 GLXFBConfig *config = glXChooseFBConfig(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrib, &count);
1348 if(oglDisplay.pixmap)
1350 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1351 oglDisplay.pixmap = None;
1353 if(oglDisplay.shapePixmap)
1355 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
1356 oglDisplay.shapePixmap = None;
1359 // Free Shared Memory Pixmap
1360 if(oglDisplay.image)
1362 if(oglDisplay.shminfoShape.shmid != -1)
1364 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
1365 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1366 shmdt(oglDisplay.shminfo.shmaddr);
1367 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
1369 XDestroyImage(oglDisplay.image);
1370 oglDisplay.image = None;
1372 if(oglDisplay.shapeImage)
1374 if(oglDisplay.shminfoShape.shmid != -1)
1376 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
1377 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1378 shmdt(oglDisplay.shminfoShape.shmaddr);
1379 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
1381 XDestroyImage(oglDisplay.shapeImage);
1382 oglDisplay.shapeImage = None;
1385 if(oglDisplay.windowPicture)
1386 XRenderFreePicture(xGlobalDisplay, oglDisplay.windowPicture);
1387 if(oglDisplay.pixmapPicture)
1388 XRenderFreePicture(xGlobalDisplay, oglDisplay.pixmapPicture);
1390 if(oglDisplay.pixmap)
1391 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
1393 if(oglDisplay.glContext)
1394 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
1395 if(oglDisplay.pBuffer)
1396 glXDestroyPbuffer(xGlobalDisplay, oglDisplay.pBuffer);
1398 oglDisplay.pBuffer = glXCreatePbuffer(xGlobalDisplay, config[0], PBattrib);
1399 if(oglDisplay.pBuffer)
1401 oglDisplay.glContext = glXCreateNewContext(xGlobalDisplay, config[0], GLX_RGBA_TYPE, oglSystem.glContext, True);
1402 if(oglDisplay.glContext)
1404 glXMakeCurrent(xGlobalDisplay, None, null);
1405 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1407 // Initialize Shared Memory Pixmap
1408 oglDisplay.image = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 32,
1409 ZPixmap, null, &oglDisplay.shminfo, width, height);
1410 if(oglDisplay.image)
1412 oglDisplay.shminfo.shmid = shmget(IPC_PRIVATE,
1413 oglDisplay.image->bytes_per_line * oglDisplay.image->height, IPC_CREAT|0777);
1414 if(oglDisplay.shminfo.shmid != -1)
1416 oglDisplay.shminfo.shmaddr = shmat(oglDisplay.shminfo.shmid, 0, 0);
1417 if(oglDisplay.shminfo.shmaddr != (void *)-1)
1419 oglDisplay.shminfo.readOnly = False;
1420 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfo))
1422 oglDisplay.pixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfo.shmaddr,
1423 &oglDisplay.shminfo, width, height, 32);
1425 // Initialize Shared Memory Shape Pixmap
1426 oglDisplay.shapeImage = XShmCreateImage(xGlobalDisplay, DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 1,
1427 ZPixmap, null, &oglDisplay.shminfoShape, width, height);
1428 if(oglDisplay.shapeImage)
1430 oglDisplay.shminfoShape.shmid = shmget(IPC_PRIVATE,
1431 oglDisplay.shapeImage->bytes_per_line * oglDisplay.shapeImage->height, IPC_CREAT|0777);
1432 if(oglDisplay.shminfoShape.shmid != -1)
1434 oglDisplay.shminfoShape.shmaddr = shmat(oglDisplay.shminfoShape.shmid, 0, 0);
1435 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
1437 oglDisplay.shminfoShape.readOnly = False;
1438 if(XShmAttach(xGlobalDisplay, &oglDisplay.shminfoShape))
1440 oglDisplay.shapePixmap = XShmCreatePixmap(xGlobalDisplay, (X11Window)display.window, oglDisplay.shminfoShape.shmaddr,
1441 &oglDisplay.shminfoShape, width, height, 1);
1442 //oglDisplay.shapePixmap = XCreatePixmap(xGlobalDisplay, (X11Window)display.window, width, height, 1);
1445 XRenderPictureAttributes attributes = { 0 };
1446 XRenderPictFormat * format = XRenderFindStandardFormat(xGlobalDisplay, /*PictStandardRGB24*/ PictStandardARGB32);
1447 #if !defined(__APPLE__)
1448 attributes.repeat = RepeatNormal;
1450 attributes.repeat = 1;
1452 oglDisplay.pixmapPicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.pixmap, format, CPRepeat, &attributes);
1453 oglDisplay.windowPicture = XRenderCreatePicture(xGlobalDisplay, (X11Window)display.window, format, 0, &attributes);
1454 oglDisplay.shapePicture = XRenderCreatePicture(xGlobalDisplay, oglDisplay.shapePixmap,
1455 XRenderFindStandardFormat(xGlobalDisplay, PictStandardA1), 0, &attributes);
1458 oglDisplay.picture = oglDisplay.shminfo.shmaddr;
1459 oglDisplay.stride = oglDisplay.image->bytes_per_line / 4;
1476 CreateDisplay(display);
1477 #if defined(__WIN32__)
1478 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1479 #elif defined(__unix__) || defined(__APPLE__)
1480 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1484 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
1491 if(!result && display.alphaBlend)
1493 printf("Alpha blending windows not supported on this display\n");
1500 glViewport(0,0,width,height);
1501 glMatrixMode(MatrixMode::projection);
1503 glOrtho(0,width,height,0,0.0,1.0);
1504 displayWidth = display.width = width;
1505 displayHeight = display.height = height;
1507 if(!oglDisplay.flippingBuffer || oglDisplay.flipBufW < width || oglDisplay.flipBufH < height)
1509 oglDisplay.flipBufW = width;
1510 oglDisplay.flipBufH = height;
1514 oglDisplay.flippingBuffer = renew oglDisplay.flippingBuffer ColorAlpha [width * height];
1517 if(oglDisplay.flippingBuffer || !width || !height)
1523 void DisplayPosition(Display display, int x, int y)
1525 OGLDisplay oglDisplay = display.driverData;
1531 void SetPalette(Display display, ColorAlpha * palette, bool colorMatch)
1535 void RestorePalette(Display display)
1539 void StartUpdate(Display display)
1543 void EndUpdate(Display display)
1547 void Scroll(Display display, Box scroll, int x, int y, Extent dirty)
1551 void Update(Display display, Box updateBox)
1553 #if defined(__WIN32__) || defined(USEPBUFFER)
1554 OGLDisplay oglDisplay = display.driverData;
1556 //Logf("DisplayScreen\n");
1558 #if !defined(__ANDROID__)
1563 #if defined(__WIN32__) || defined(USEPBUFFER)
1564 if(display.alphaBlend)
1566 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1567 glPixelStorei(GL_PACK_ROW_LENGTH, oglDisplay.stride);
1568 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1569 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1570 glReadPixels(0,0,display.width,display.height,GL_BGRA_EXT,GL_UNSIGNED_BYTE, oglDisplay.picture);
1573 #if defined(__WIN32__)
1575 POINT point = { oglDisplay.x, oglDisplay.y};
1576 POINT srcPoint = { 0, 0 };
1577 BLENDFUNCTION blend = { 0 };
1579 size.cx = display.width;
1580 size.cy = display.height;
1581 blend.BlendOp = AC_SRC_OVER;
1582 blend.BlendFlags = 0;
1583 blend.SourceConstantAlpha = 255;
1584 blend.AlphaFormat = AC_SRC_ALPHA;
1587 // Process partial images. Mapping the buffer waits for
1588 // outstanding DMA transfers into the buffer to finish.
1589 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1590 oglDisplay.pboMemory1 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
1592 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1593 // oglDisplay.pboMemory2 = (byte *)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB,GL_READ_ONLY);
1596 memcpy(oglDisplay.picture, oglDisplay.pboMemory1, display.width * display.height * 4);
1597 //memcpy(oglDisplay.picture + display.width * display.height * 4 / 2, oglDisplay.pboMemory2, display.width * display.height * 4/ 2);
1600 UpdateLayeredWindow(display.window, hdc, &point, &size, oglDisplay.memDC, &srcPoint, 0, &blend, ULW_ALPHA);
1603 // Unmap the image buffers
1604 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1605 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1607 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1608 // glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
1610 // Bind two different buffer objects and start the glReadPixels
1611 // asynchronously. Each call will return directly after
1612 // starting the DMA transfer.
1613 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[0]);
1614 glReadPixels(0, 0, display.width, display.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1616 // glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, oglDisplay.imageBuffers[1]);
1617 // glReadPixels(0, display.height/2, display.width, display.height/2, GL_BGRA, GL_UNSIGNED_BYTE, 0);
1621 #elif defined(__unix__) || defined(__APPLE__)
1622 #if defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__ODROID__)
1624 XTransform transform =
1627 { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(0 * (1 << 16)) },
1628 { (int)(0.0f), (int)(-1.0f * (1<<16)), (int)(0 * (1<<16)) },
1629 { (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)), (int)(1.0f * (1<<16)) }
1632 XRenderSetPictureTransform(xGlobalDisplay, oglDisplay.pixmapPicture, &transform);
1633 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.shapePicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1634 XRenderComposite(xGlobalDisplay, PictOpSrc, oglDisplay.pixmapPicture, None, oglDisplay.windowPicture, 0, 0, 0, 0, 0, 0, display.width, display.height);
1635 #if !defined(__APPLE__)
1636 XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1638 XShapeCombineMask(xGlobalDisplay, display.window, 2, 0, 0, oglDisplay.shapePixmap, ShapeSet);
1640 XFlush(xGlobalDisplay);
1648 #if defined(__WIN32__)
1649 //wglSwapLayerBuffers(oglDisplay.hdc,WGL_SWAP_MAIN_PLANE);
1650 SwapBuffers(oglDisplay.hdc);
1651 #elif defined(__unix__) || defined(__APPLE__)
1652 #if defined(__ANDROID__) || defined(__ODROID__)
1653 eglSwapBuffers(eglDisplay, eglSurface);
1654 #elif defined(__EMSCRIPTEN__)
1657 glXSwapBuffers(xGlobalDisplay, (GLXDrawable)display.window);
1661 //Logf("Out of DisplayScreen\n");
1664 void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
1666 if(bitmap.driverData)
1668 GLuint tex = (GLuint)(uintptr)bitmap.driverData;
1669 glDeleteTextures(1, &tex);
1670 bitmap.driverData = 0;
1672 bitmap.driver = ((subclass(DisplayDriver))class(LFBDisplayDriver));
1675 bool AllocateBitmap(DisplaySystem displaySystem, Bitmap bitmap, int width, int height, int stride, PixelFormat format, bool allocatePalette)
1677 OGLSystem oglSystem = displaySystem.driverData;
1678 bool result = false;
1680 GLuint glBitmap = 0;
1682 uint w = width, h = height;
1683 if(oglSystem.pow2textures)
1688 w = Min(w, oglSystem.maxTextureSize);
1689 h = Min(h, oglSystem.maxTextureSize);
1691 glGenTextures(1, &glBitmap);
1692 glBindTexture(GL_TEXTURE_2D, glBitmap);
1694 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1696 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1697 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1699 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1700 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1702 #if !defined(SHADERS)
1703 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1706 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
1708 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1709 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1713 bitmap.driverData = (void *)(uintptr)glBitmap;
1714 bitmap.driver = displaySystem.driver;
1722 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
1724 bool result = false;
1725 OGLSystem oglSystem = displaySystem.driverData;
1726 Bitmap convBitmap = bitmap;
1730 convBitmap.Copy(bitmap);
1733 // Pre process the bitmap... First make it 32 bit
1734 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
1737 uint w = bitmap.width, h = bitmap.height;
1738 GLuint glBitmap = 0;
1739 if(oglSystem.pow2textures)
1744 w = Min(w, oglSystem.maxTextureSize);
1745 h = Min(h, oglSystem.maxTextureSize);
1749 while(w * 2 < h) w *= 2;
1750 while(h * 2 < w) h *= 2;
1753 // Switch ARGB to RGBA
1754 //if(bitmap.format != pixelFormatRGBA)
1756 for(c=0; c<bitmap.size; c++)
1758 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
1760 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
1761 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
1764 // convBitmap.pixelFormat = pixelFormat888;
1767 glGenTextures(1, &glBitmap);
1770 //int error = glGetError();
1774 glBindTexture(GL_TEXTURE_2D, glBitmap);
1775 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1777 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
1778 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1780 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1782 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1783 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1785 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1786 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1787 #if !defined(__ANDROID__) && !defined(SHADERS)
1788 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0 );
1791 #if !defined(SHADERS)
1792 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1797 for(level = 0; result && (w >= 1 || h >= 1); level++, w >>= 1, h >>= 1)
1802 if(bitmap.width != w || bitmap.height != h)
1804 mipMap = Bitmap { };
1805 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
1807 Surface mipSurface = mipMap.GetSurface(0,0,null);
1808 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
1818 mipMap = convBitmap;
1825 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1826 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1827 //printf("Calling glTexImage2D\n");
1828 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
1829 //printf("width = %d (Should be %d, %d)\n", width, w, h);
1830 if((error = glGetError()))
1832 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
1833 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
1837 if(mipMap != convBitmap)
1842 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
1843 bitmap.driverData = (void *)(uintptr)glBitmap;
1844 bitmap.driver = displaySystem.driver;
1849 FreeBitmap(displaySystem, bitmap);
1850 else if(oglSystem.loadingFont)
1852 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1853 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1854 oglSystem.loadingFont = false;
1860 void ReleaseSurface(Display display, Surface surface)
1862 glDisable(GL_SCISSOR_TEST);
1863 delete surface.driverData;
1864 surface.driverData = null;
1867 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
1872 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
1874 bool result = false;
1875 OGLSurface oglSurface = surface.driverData = OGLSurface { };
1877 //Logf("GetSurface\n");
1881 if(displayWidth != display.width || displayHeight != display.height)
1883 displayWidth = display.width;
1884 displayHeight = display.height;
1886 glViewport(0,0,display.width,display.height);
1888 glOrtho(0,display.width,display.height,0,0.0,1.0);
1891 surface.offset.x = x;
1892 surface.offset.y = y;
1893 surface.unclippedBox = surface.box = clip;
1894 oglSurface.bitmapMult[0] = 1;
1895 oglSurface.bitmapMult[1] = 1;
1896 oglSurface.bitmapMult[2] = 1;
1897 oglSurface.bitmapMult[3] = 1;
1899 glEnable(GL_SCISSOR_TEST);
1902 (display.height) -(y+clip.bottom)-1,
1903 clip.right-clip.left+1,
1904 clip.bottom-clip.top+1);
1910 void Clip(Display display, Surface surface, Box clip)
1919 box.Clip(surface.unclippedBox);
1923 box = surface.box = surface.unclippedBox;
1924 box.left += surface.offset.x;
1925 box.top += surface.offset.y;
1926 box.right+= surface.offset.x;
1927 box.bottom += surface.offset.y;
1930 box.left,display.height - box.bottom - 1,
1931 box.right-box.left+1, box.bottom-box.top+1);
1934 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
1936 bool result = false;
1937 OGLDisplay oglDisplay = display.driverData;
1938 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
1940 if(oglDisplay.flippingBuffer)
1942 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
1945 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
1951 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1952 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
1953 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1954 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1955 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
1958 for(row = 0; row<h; row++)
1959 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
1966 void SetForeground(Display display, Surface surface, ColorAlpha color)
1968 OGLSurface oglSurface = surface.driverData;
1970 //Logf("SetForeground\n");
1972 oglSurface.foreground[0] = color.color.r/255.0f;
1973 oglSurface.foreground[1] = color.color.g/255.0f;
1974 oglSurface.foreground[2] = color.color.b/255.0f;
1975 //oglSurface.foreground[3] = 1.0f;
1976 oglSurface.foreground[3] = color.a/255.0f;
1978 //if(!oglSurface.foreground[3])printf("bug");
1981 void SetBackground(Display display, Surface surface, ColorAlpha color)
1983 OGLSurface oglSurface = surface.driverData;
1985 //Logf("SetBackground\n");
1987 oglSurface.background[0] = color.color.r/255.0f;
1988 oglSurface.background[1] = color.color.g/255.0f;
1989 oglSurface.background[2] = color.color.b/255.0f;
1990 //oglSurface.background[3] = 1.0;
1991 oglSurface.background[3] = color.a/255.0f;
1994 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
1996 OGLSurface oglSurface = surface.driverData;
1998 oglSurface.bitmapMult[0] = color.color.r/255.0f;
1999 oglSurface.bitmapMult[1] = color.color.g/255.0f;
2000 oglSurface.bitmapMult[2] = color.color.b/255.0f;
2001 oglSurface.bitmapMult[3] = color.a/255.0f;
2004 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2009 void PutPixel(Display display, Surface surface,int x,int y)
2011 OGLSurface oglSurface = surface.driverData;
2013 //Logf("PutPixel\n");
2015 glColor4fv(oglSurface.foreground);
2017 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2018 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2023 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2025 OGLSurface oglSurface = surface.driverData;
2026 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2041 x1 += surface.offset.x;
2042 y1 += surface.offset.y;
2043 x2 += surface.offset.x;
2044 y2 += surface.offset.y;
2048 glColor4fv(oglSurface.foreground);
2050 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2053 glTexCoord2f(0.5f, 0);
2054 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2055 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2056 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2065 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2066 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2072 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2074 OGLSurface oglSurface = surface.driverData;
2075 x1 += surface.offset.x;
2076 y1 += surface.offset.y;
2077 x2 += surface.offset.x;
2078 y2 += surface.offset.y;
2080 //Logf("Rectangle\n");
2082 glColor4fv(oglSurface.foreground);
2083 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2088 glTexCoord2f(0.5f, 0);
2089 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2090 glTexCoord2f(y2-y1 + 0.5f, 0);
2091 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2093 glTexCoord2f(0.5f, 0);
2094 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2095 glTexCoord2f(x2 - x1 + 0.5f, 0);
2096 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2098 glTexCoord2f(0.5f, 0);
2099 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2100 glTexCoord2f(y1 - y2 + 0.5f, 0);
2101 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2103 glTexCoord2f(0.5f, 0);
2104 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2105 glTexCoord2f(x1 - x2 + 0.5f, 0);
2106 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2111 glBegin(GL_LINE_LOOP);
2118 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2119 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2120 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2121 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2126 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2128 OGLSurface oglSurface = surface.driverData;
2131 glColor4fv(oglSurface.background);
2135 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
2136 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
2137 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
2138 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
2141 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2142 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2145 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2146 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2150 void Clear(Display display, Surface surface, ClearType type)
2152 OGLDisplay oglDisplay = display.driverData;
2153 OGLSurface oglSurface = surface.driverData;
2156 if(type != depthBuffer)
2157 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2158 if(type != colorBuffer && !oglDisplay.depthWrite)
2160 glDepthMask((byte)bool::true);
2162 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2163 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2164 if(type != colorBuffer && !oglDisplay.depthWrite)
2166 glDepthMask((byte)bool::false);
2170 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2175 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2177 OGLSurface oglSurface = surface.driverData;
2179 if(!oglSurface.writingText)
2181 // glTranslatef(-0.375f, -0.375f, 0.0f);
2182 GLSetupTexturing(true);
2183 glColor4fv(oglSurface.bitmapMult);
2185 else if(oglSurface.xOffset)
2186 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2188 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2189 glBegin(GLIMTKMode::quads);
2193 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2194 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2195 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2196 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2197 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2198 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2199 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2200 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2205 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2206 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2207 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2208 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2209 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2210 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2211 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2212 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2215 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2216 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2217 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2218 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2219 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2220 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2221 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2222 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2226 if(!oglSurface.writingText)
2228 GLSetupTexturing(false);
2230 //glTranslate(0.375, 0.375, 0.0);
2232 else if(oglSurface.xOffset)
2233 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2236 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2238 OGLSurface oglSurface = surface.driverData;
2240 //glTranslate(-0.375, -0.375, 0.0);
2242 GLSetupTexturing(true);
2243 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2245 glColor4fv(oglSurface.bitmapMult);
2247 glBegin(GLIMTKMode::quads);
2251 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2252 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2254 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2255 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2257 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2258 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2260 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2261 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2265 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2266 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2268 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2269 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2271 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2272 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2274 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2275 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2280 GLSetupTexturing(false);
2282 //glTranslate(0.375, 0.375, 0.0);
2285 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2287 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2290 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2292 #if !defined(EM_MODE)
2293 float s2dw,s2dh,d2sw,d2sh;
2294 //bool flipX = false, flipY = false;
2296 //Logf("StretchDI\n");
2298 if(Sgn(w) != Sgn(sw))
2304 if(Sgn(h) != Sgn(sh))
2316 //Clip against the edges of the source
2319 dx+=(int)((0-sx) * s2dw);
2320 w-=(int)((0-sx) * s2dw);
2326 dy+=(int)((0-sy) * s2dh);
2327 h-=(int)((0-sy) * s2dh);
2332 if(sx+sw>bitmap.width-1)
2334 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2335 sw-=sx+sw-(bitmap.width-1)-1;
2337 if(sy+sh>(bitmap.height-1))
2339 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2340 sh-=sy+sh-(bitmap.height-1)-1;
2342 //Clip against the edges of the surfaceination
2343 if(dx<surface.box.left)
2346 sx+=(int)((surface.box.left-dx)*d2sw);
2347 sw-=(int)((surface.box.left-dx)*d2sw);
2348 w-=surface.box.left-dx;
2349 dx=surface.box.left;
2351 if(dy<surface.box.top)
2353 sy+=(int)((surface.box.top-dy)*d2sh);
2354 sh-=(int)((surface.box.top-dy)*d2sh);
2355 h-=surface.box.top-dy;
2358 if(dx+w>surface.box.right)
2360 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2361 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2362 w-=dx+w-surface.box.right-1;
2364 if(dy+h>surface.box.bottom)
2366 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2367 h-=dy+h-surface.box.bottom-1;
2369 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2371 dx += surface.offset.x;
2372 dy += surface.offset.y;
2374 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2376 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2377 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2378 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2379 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2380 #if !defined(SHADERS)
2381 glRasterPos2d(dx,dy);
2382 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2383 glPixelZoom(s2dw, -s2dh);
2384 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2386 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2387 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2388 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2389 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2394 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2396 #if !defined(EM_MODE)
2399 //Clip against the edges of the source
2412 if(sx+w>bitmap.width-1)
2413 w-=sx+w-(bitmap.width-1)-1;
2414 if(sy+h>bitmap.height-1)
2415 h-=sy+h-(bitmap.height-1)-1;
2416 //Clip against the edges of the surfaceination
2417 if(dx<surface.box.left)
2420 sx+=surface.box.left-dx;
2421 w-=surface.box.left-dx;
2422 dx=surface.box.left;
2424 if(dy<surface.box.top)
2426 sy+=surface.box.top-dy;
2427 h-=surface.box.top-dy;
2430 if(dx+w>surface.box.right)
2432 //if(flip) sx+=dx+w-surface.box.right-1;
2433 w-=dx+w-surface.box.right-1;
2435 if(dy+h>surface.box.bottom)
2436 h-=dy+h-surface.box.bottom-1;
2440 dx += surface.offset.x;
2441 dy += surface.offset.y;
2443 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2445 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2446 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2447 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2448 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2449 #if !defined(SHADERS)
2450 glRasterPos2d(dx,dy);
2452 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2454 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2455 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2456 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2457 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2462 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2464 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2467 void UnloadFont(DisplaySystem displaySystem, Font font)
2469 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2472 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
2475 OGLSystem oglSystem = displaySystem.driverData;
2476 oglSystem.loadingFont = true;
2477 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
2481 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
2483 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height);
2486 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
2488 OGLSurface oglSurface = surface.driverData;
2489 OGLSystem oglSystem = display.displaySystem.driverData;
2490 oglSystem.loadingFont = true;
2492 //glTranslated(-0.375, -0.375, 0.0);
2496 if(surface.textOpacity)
2499 FontExtent(display.displaySystem, surface.font, text, len, &w, &h);
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.outline.size)
2509 ColorAlpha outlineColor = surface.outline.color;
2510 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
2511 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2512 //glEnable(GL_BLEND);
2514 oglSurface.writingOutline = true;
2515 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2516 oglSurface.writingOutline = false;
2518 glColor4fv(oglSurface.foreground);
2520 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
2521 oglSurface.writingText = false;
2522 oglSystem.loadingFont = false;
2524 GLSetupTexturing(false);
2526 //glTranslated(0.375, 0.375, 0.0);
2529 void TextFont(Display display, Surface surface, Font font)
2531 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2534 void TextOpacity(Display display, Surface surface, bool opaque)
2536 OGLSurface oglSurface = surface.driverData;
2537 oglSurface.opaqueText = opaque;
2540 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
2542 OGLSurface oglSurface = surface.driverData;
2543 OGLSystem oglSystem = display.displaySystem.driverData;
2544 oglSystem.loadingFont = true;
2545 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height);
2546 oglSystem.loadingFont = false;
2549 void DrawingChar(Display display, Surface surface, char character)
2554 void LineStipple(Display display, Surface surface, uint32 stipple)
2556 //Logf("Stipple\n");
2560 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2561 stippleEnabled = true;
2562 glesLineStipple(1, (uint16)stipple);
2564 glLineStipple(1, (uint16)stipple);
2565 glEnable(GL_LINE_STIPPLE);
2570 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2571 stippleEnabled = false;
2572 glMatrixMode(GL_TEXTURE);
2574 glMatrixMode(MatrixMode::projection);
2575 GLSetupTexturing(false); // TODO: Special shading code for stipple?
2577 glDisable(GL_LINE_STIPPLE);
2581 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
2582 void SetRenderState(Display display, RenderState state, uint value)
2584 OGLDisplay oglDisplay = display.driverData;
2585 //Logf("RenderState\n");
2591 glEnable(GL_MULTISAMPLE_ARB);
2593 glDisable(GL_MULTISAMPLE_ARB);
2597 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2601 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2604 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2605 oglDisplay.depthWrite = (bool)value;
2609 #if !defined(SHADERS)
2610 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2611 glFogfv(GL_FOG_COLOR, (float *)&color);
2616 #if !defined(SHADERS)
2617 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
2621 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2625 #if defined(SHADERS)
2626 shader_setGlobalAmbient(((Color)value).r / 255.0f, ((Color)value).g / 255.0f, ((Color)value).b / 255.0f, 1.0f);
2627 #elif !defined(EM_MODE)
2628 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2629 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2635 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2640 #if defined(__WIN32__)
2641 wglSwapIntervalEXT(value ? 1 : 0);
2648 void SetLight(Display display, int id, Light light)
2650 #if defined(SHADERS)
2651 shader_setLight(display, id, light);
2652 #elif !defined(EM_MODE)
2653 //Logf("SetLight\n");
2657 Object lightObject = light.lightObject;
2658 float position[4] = { 0, 0, 0, 0 };
2659 float color[4] = { 0, 0, 0, 1 };
2661 glEnable(GL_LIGHT0 + id);
2663 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2664 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2665 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2668 if(!light.multiplier) light.multiplier = 1.0f;
2670 color[0] = light.diffuse.r * light.multiplier;
2671 color[1] = light.diffuse.g * light.multiplier;
2672 color[2] = light.diffuse.b * light.multiplier;
2673 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2675 color[0] = light.ambient.r * light.multiplier;
2676 color[1] = light.ambient.g * light.multiplier;
2677 color[2] = light.ambient.b * light.multiplier;
2678 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2679 color[0] = light.specular.r * light.multiplier;
2680 color[1] = light.specular.g * light.multiplier;
2681 color[2] = light.specular.b * light.multiplier;
2682 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2686 Vector3D positionVector;
2687 if(light.flags.spot)
2689 if(lightObject.flags.root || !lightObject.parent)
2691 positionVector = lightObject.transform.position;
2692 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2696 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
2697 if(display.display3D.camera)
2698 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2704 if(!light.direction.x && !light.direction.y && !light.direction.z)
2706 Vector3Df vector { 0,0,-1 };
2708 mat.RotationQuaternion(light.orientation);
2709 positionVector.MultMatrixf(vector, mat);
2713 positionVector = light.direction;
2718 position[0] = (float)positionVector.x;
2719 position[1] = (float)positionVector.y;
2720 position[2] = (float)positionVector.z;
2722 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2725 // Display Light Position
2726 glDisable(GL_LIGHTING);
2727 glDisable(GL_DEPTH_TEST);
2731 glVertex3fv(position);
2733 glEnable(GL_DEPTH_TEST);
2734 glEnable(GL_LIGHTING);
2738 if(lightObject.flags.root || !lightObject.parent)
2740 positionVector = light.target.transform.position;
2741 positionVector.Subtract(positionVector, display.camera.cPosition);
2745 positionVector.MultMatrix(light.target.transform.position,
2746 lightObject.light.target.parent.matrix);
2747 positionVector.Subtract(positionVector, display.camera.cPosition);
2750 position[0] = positionVector.x;
2751 position[1] = positionVector.y;
2752 position[2] = positionVector.z;
2754 glDisable(GL_LIGHTING);
2755 glDisable(GL_DEPTH_TEST);
2759 glVertex3fv(position);
2761 glEnable(GL_DEPTH_TEST);
2762 glEnable(GL_LIGHTING);
2765 if(light.flags.attenuation)
2767 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
2768 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
2769 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
2772 if(light.flags.spot)
2775 #define MAXLIGHT 0.9
2776 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
2777 // Figure out exponent out of the hot spot
2778 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
2780 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
2781 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
2782 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
2787 Vector3Df vector { 0,0,-1 };
2788 Vector3Df direction;
2791 mat.RotationQuaternion(light.orientation);
2792 direction.MultMatrix(vector, mat);
2794 position[0] = direction.x;
2795 position[1] = direction.y;
2796 position[2] = direction.z;
2798 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2802 glDisable(GL_LIGHT0 + id);
2806 void SetCamera(Display display, Surface surface, Camera camera)
2808 OGLDisplay oglDisplay = display.driverData;
2809 //Logf("SetCamera\n");
2813 int left = surface.box.left + surface.offset.x;
2814 int top = surface.box.top + surface.offset.y;
2815 int right = surface.box.right + surface.offset.x;
2816 int bottom = surface.box.bottom + surface.offset.y;
2817 float origX = surface.offset.x + camera.origin.x;
2818 float origY = surface.offset.y + camera.origin.y;
2820 int y = display.height - bottom - 1;
2821 int w = right - left + 1;
2822 int h = bottom - top + 1;
2825 glViewport(x, y, w, h);
2827 // *** Projection Matrix ***
2828 glMatrixMode(MatrixMode::projection);
2829 if(!display.display3D.camera)
2832 if(display.display3D.collectingHits)
2834 float pickX = display.display3D.pickX + surface.offset.x;
2835 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
2839 w / display.display3D.pickWidth, 0, 0, 0,
2840 0, h / display.display3D.pickHeight, 0, 0,
2842 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
2843 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
2846 glLoadMatrixd(pickMatrix.array);
2851 (left - origX) * camera.zMin / camera.focalX,
2852 (right - origX) * camera.zMin / camera.focalX,
2853 (bottom - origY) * camera.zMin / camera.focalY,
2854 (top - origY) * camera.zMin / camera.focalY,
2855 camera.zMin, camera.zMax);
2857 glDisable(GL_BLEND);
2859 // *** Z Inverted Identity Matrix ***
2860 glMatrixMode(MatrixMode::modelView);
2861 if(!display.display3D.camera)
2866 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
2868 // *** View Matrix ***
2869 glMultMatrixd(camera.viewMatrix.array);
2874 glEnable(GL_DEPTH_TEST);
2876 #if defined(SHADERS)
2877 shader_lighting(true);
2878 #elif !defined(EM_MODE)
2879 glEnable(GL_LIGHTING);
2880 glShadeModel(GL_SMOOTH);
2882 glDepthMask((byte)bool::true);
2883 oglDisplay.depthWrite = true;
2885 glEnable(GL_MULTISAMPLE_ARB);
2887 else if(display.display3D.camera)
2889 oglDisplay.depthWrite = false;
2890 glViewport(0,0,display.width,display.height);
2892 glDisable(GL_CULL_FACE);
2893 glDisable(GL_DEPTH_TEST);
2894 GLSetupTexturing(false);
2895 #if defined(SHADERS)
2896 shader_lighting(false);
2897 #elif !defined(EM_MODE)
2898 glDisable(GL_LIGHTING);
2900 glShadeModel(GL_FLAT);
2903 glDisable(GL_MULTISAMPLE_ARB);
2905 // *** Restore 2D MODELVIEW Matrix ***
2908 // *** Restore 2D PROJECTION Matrix ***
2909 glMatrixMode(MatrixMode::projection);
2915 void ApplyMaterial(Display display, Material material, Mesh mesh)
2917 //Logf("ApplyMaterial\n");
2920 if(material.flags.doubleSided)
2922 #if !defined(EM_MODE) && !defined(SHADERS)
2923 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
2925 glDisable(GL_CULL_FACE);
2929 #if !defined(EM_MODE) && !defined(SHADERS)
2930 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
2932 glEnable(GL_CULL_FACE);
2935 #if !defined(SHADERS)
2937 if(material.flags.noFog)
2944 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
2946 Bitmap map = material.baseMap;
2947 GLSetupTexturing(true);
2948 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
2950 glMatrixMode(GL_TEXTURE);
2952 if(material.uScale && material.vScale)
2953 glScalef(material.uScale, material.vScale, 1);
2954 glMatrixMode(MatrixMode::modelView);
2956 if(material.flags.tile)
2958 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2959 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2963 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2964 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2968 GLSetupTexturing(false);
2970 #if defined(EM_MODE) || defined(SHADERS)
2971 glimtkColor4f(material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity);
2973 if(mesh.flags.colors)
2975 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
2976 glEnable(GL_COLOR_MATERIAL);
2980 glDisable(GL_COLOR_MATERIAL);
2982 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
2983 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
2986 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
2987 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
2991 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
2992 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
2995 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
2996 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
2999 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
3003 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
3005 OGLMesh oglMesh = mesh.data;
3008 if(!mesh.flags.vertices)
3010 oglMesh.vertices.free();
3011 delete mesh.vertices;
3013 if(!mesh.flags.normals)
3015 oglMesh.normals.free();
3016 delete mesh.normals;
3018 if(!mesh.flags.texCoords1)
3020 oglMesh.texCoords.free();
3021 delete mesh.texCoords;
3023 if(!mesh.flags.texCoords2)
3025 oglMesh.texCoords2.free();
3026 // delete mesh.texCoords2;
3028 if(!mesh.flags.colors)
3030 oglMesh.colors.free();
3041 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3043 bool result = false;
3046 mesh.data = OGLMesh { };
3049 if(mesh.nVertices == nVertices)
3051 // Same number of vertices, adding features (Leaves the other features pointers alone)
3052 if(mesh.flags != flags)
3054 if(!mesh.flags.vertices && flags.vertices)
3056 if(flags.doubleVertices)
3058 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3061 mesh.vertices = new Vector3Df[nVertices];
3063 if(!mesh.flags.normals && flags.normals)
3065 if(flags.doubleNormals)
3067 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3070 mesh.normals = new Vector3Df[nVertices];
3072 if(!mesh.flags.texCoords1 && flags.texCoords1)
3074 mesh.texCoords = new Pointf[nVertices];
3076 if(!mesh.flags.colors && flags.colors)
3078 mesh.colors = new ColorRGBAf[nVertices];
3084 // New number of vertices, reallocate all current and new features
3085 flags |= mesh.flags;
3088 if(flags.doubleVertices)
3090 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3093 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3097 if(flags.doubleNormals)
3099 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3102 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3104 if(flags.texCoords1)
3106 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3110 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3118 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3120 OGLMesh oglMesh = mesh.data;
3121 if(!flags) flags = mesh.flags;
3126 oglMesh.vertices.upload(
3127 mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices); //, GL_STATIC_DRAW_ARB );
3130 oglMesh.normals.upload(
3131 mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals); //, GL_STATIC_DRAW_ARB );
3133 if(flags.texCoords1)
3134 oglMesh.texCoords.upload(
3135 mesh.nVertices * sizeof(Pointf), mesh.texCoords); //, GL_STATIC_DRAW_ARB );
3138 oglMesh.colors.upload(
3139 mesh.nVertices * sizeof(ColorRGBAf), mesh.colors); //, GL_STATIC_DRAW_ARB );
3143 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3150 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3154 oglIndices.buffer.free();
3155 delete oglIndices.indices;
3160 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3162 OGLIndices oglIndices = OGLIndices { };
3165 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3166 oglIndices.nIndices = nIndices;
3171 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3178 if(!oglIndices.buffer.buffer)
3179 glGenBuffers(1, &oglIndices.buffer.buffer);
3180 if(glabCurElementBuffer != oglIndices.buffer.buffer)
3181 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oglIndices.buffer.buffer);
3182 glimtkBufferDatai(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32) * nIndices, oglIndices.indices, GL_STATIC_DRAW_ARB);
3186 oglIndices.buffer.upload(
3187 nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3188 oglIndices.indices); //GL_STATIC_DRAW_ARB);
3192 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3195 return oglIndices.indices;
3198 void SelectMesh(Display display, Mesh mesh)
3200 //Logf("SelectMesh\n");
3202 #if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
3204 #if defined(__WIN32__)
3205 if(glUnlockArraysEXT)
3207 if(!vboAvailable && display.display3D.mesh)
3208 glUnlockArraysEXT();
3213 OGLMesh oglMesh = mesh.data;
3215 // *** Vertex Stream ***
3216 glEnableClientState(GL_VERTEX_ARRAY);
3217 if(!display.display3D.collectingHits && oglMesh)
3219 oglMesh.vertices.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, oglMesh.vertices.buffer ? null : (double *)mesh.vertices);
3221 // *** Normals Stream ***
3222 if(mesh.normals || mesh.flags.normals)
3224 glEnableClientState(GL_NORMAL_ARRAY);
3225 oglMesh.normals.use(normal, 3, GL_FLOAT, 0, oglMesh.normals.buffer ? null : mesh.normals);
3228 glDisableClientState(GL_NORMAL_ARRAY);
3230 // *** Texture Coordinates Stream ***
3231 if(mesh.texCoords || mesh.flags.texCoords1)
3233 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3234 oglMesh.texCoords.use(texCoord, 2, GL_FLOAT, 0, oglMesh.texCoords.buffer ? null : mesh.texCoords);
3237 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3239 // *** Color Stream ***
3240 if(mesh.colors || mesh.flags.colors)
3242 glEnableClientState(GL_COLOR_ARRAY);
3243 oglMesh.colors.use(color, 4, GL_FLOAT, 0, oglMesh.colors.buffer ? null : mesh.colors);
3246 glDisableClientState(GL_COLOR_ARRAY);
3250 noAB.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, (double *)mesh.vertices);
3251 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
3253 glEnableClientState(GL_NORMAL_ARRAY);
3254 noAB.use(normal, 3, GL_FLOAT, 0, mesh.normals);
3257 glDisableClientState(GL_NORMAL_ARRAY);
3258 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
3260 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3261 noAB.use(texCoord, 2, GL_FLOAT, 0, mesh.texCoords);
3264 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3265 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
3267 glEnableClientState(GL_COLOR_ARRAY);
3268 noAB.use(color, 4, GL_FLOAT, 0, mesh.colors);
3271 glDisableClientState(GL_COLOR_ARRAY);
3274 #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
3276 #if defined(__WIN32__)
3280 glLockArraysEXT(0, mesh.nVertices);
3286 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3288 //Logf("DrawPrimitives\n");
3290 if(primitive->type.vertexRange)
3291 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3294 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3295 // HACK TO SPEED THINGS UP...
3297 /*GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3298 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3301 glBegin((GLIMTKMode)primitiveTypes[primitive->type.primitiveType]);
3304 OGLIndices oglIndices = primitive->data;
3305 MeshFeatures flags = mesh.flags;
3306 for(c = 0; c<primitive->nIndices; c++)
3308 uint16 index = ((uint16 *) oglIndices.indices)[c];
3309 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3310 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3311 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3312 glVertex3fv((float *)&mesh.vertices[index]);
3321 OGLIndices oglIndices = primitive->data;
3322 GLEAB eab = ((!display.display3D.collectingHits && oglIndices) ? oglIndices.buffer : noEAB);
3324 eab.draw(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3325 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT,
3326 eab.buffer ? 0 : (oglIndices ? oglIndices.indices : primitive->indices));
3327 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
3332 void PushMatrix(Display display)
3337 void PopMatrix(Display display, bool setMatrix)
3342 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3344 Matrix matrix = transMatrix;
3345 Camera camera = useCamera ? display.display3D.camera : null;
3350 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3355 matrix.m[3][0] - camera.cPosition.x,
3356 matrix.m[3][1] - camera.cPosition.y,
3357 matrix.m[3][2] - camera.cPosition.z);
3369 glMultMatrixd(matrix.array);
3374 public void UseSingleGLContext(bool useSingle)
3376 useSingleGLContext = useSingle;
3379 default dllexport void *
3380 #if defined(__WIN32__)
3381 __attribute__((stdcall))
3383 IS_GLGetContext(DisplaySystem displaySystem)
3387 #if defined(__WIN32__)
3388 OGLSystem system = displaySystem.driverData;
3390 #elif defined(__ANDROID__) || defined(__ODROID__)
3392 #elif defined(__EMSCRIPTEN__)
3394 OGLSystem system = displaySystem.driverData;
3395 return system.glContext;