1 namespace gfx::drivers;
9 #define GL_BGRA_EXT 0x80E1
11 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
12 # if 0 //defined(SHADERS)
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
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__)
175 #if defined(__unix__) || defined(__APPLE__)
177 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
183 #define glLoadMatrix glLoadMatrixd
184 #define glMultMatrix glMultMatrixd
185 #define glGetMatrix glGetDoublev
186 #define glTranslate glTranslated
187 #define glScale glScaled
190 #define glVertex3v glVertex3dv
191 #define glNormal3v glNormal3dv
195 //#ifdef VERTEX_FORMAT_DOUBLE
197 #define glLoadMatrix glLoadMatrixd
198 #define glMultMatrix glMultMatrixd
199 #define glGetMatrix glGetDoublev
200 #define glVertex3v glVertex3dv
201 #define glNormal3v glNormal3dv
202 #define glTranslate glTranslated
203 #define glScale glScaled
204 //#define GL_VERTEX_FORMAT GL_DOUBLE
208 #define glLoadMatrix glLoadMatrixf
209 #define glMultMatrix glMultMatrixf
210 #define glGetMatrix glGetFloatv
211 #define glVertex3v glVertex3fv
212 #define glNormal3v glNormal3fv
213 #define glTranslate glTranslatef
214 #define glScale glScalef
215 //#define GL_VERTEX_FORMAT GL_FLOAT
220 #define GL_ARRAY_BUFFER_ARB 0x8892
221 #define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
222 #define GL_STATIC_DRAW_ARB 0x88E4
223 #define GL_LIGHT_MODEL_COLOR_CONTROL 0x81F8
224 #define GL_SEPARATE_SPECULAR_COLOR 0x81FA
226 #define GL_MULTISAMPLE_ARB 0x809D
228 #if defined(__WIN32__)
231 typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
232 typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
234 static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
235 static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
237 static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
238 static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
239 static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
240 static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
241 static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
242 static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
243 static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
244 static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
245 static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
246 static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = null;
247 static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = null;
249 #elif defined(__ANDROID__) || defined(__ODROID__)
251 #define GL_FRAMEBUFFER GL_FRAMEBUFFER_OES
252 #define GL_RENDERBUFFER GL_RENDERBUFFER_OES
253 #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_OES
255 #define GL_POLYGON_STIPPLE 0xFFFF
256 #define GL_LINE_STIPPLE 0xFFFF
257 #define GL_LINE 0xFFFF
258 #define GL_FILL 0xFFFF
259 #define GL_ALL_ATTRIB_BITS 0xFFFF
260 #define GL_LIGHT_MODEL_LOCAL_VIEWER 0xFFFF
266 #define GL_QUAD_STRIP 0
267 //#define GL_DOUBLE 0
268 //#define GL_UNSIGNED_INT 0
271 //#define GL_LINE_STIPPLE 0
272 #define GL_BGRA_EXT 0
273 #define GL_UNPACK_ROW_LENGTH 0
274 #define GL_UNPACK_SKIP_PIXELS 0
275 #define GL_UNPACK_SKIP_ROWS 0
277 #define GL_PACK_ROW_LENGTH 0
278 #define GL_PACK_SKIP_ROWS 0
279 #define GL_PACK_SKIP_PIXELS 0
284 #if defined(__ANDROID__) || defined(__ODROID__)
285 #define glBindFramebuffer glBindFramebufferOES
286 #define glBindRenderbuffer glBindRenderbufferOES
287 #define glFramebufferTexture2D glFramebufferTexture2DOES
288 #define glGenFramebuffers glGenFramebuffersOES
289 #define glGenRenderbuffers glGenRenderbuffersOES
290 #define glDeleteFramebuffers glDeleteFramebuffersOES
291 #define glDeleteRenderbuffers glDeleteRenderbuffersOES
293 #define GL_INT 0x1404
294 #define GL_UNSIGNED_INT 0x1405
295 #define GL_DOUBLE 0x140A
299 #if defined(ES1_1) || defined(SHADERS)
330 #undef glLoadIdentity
335 #undef glColorMaterial
338 #define glRecti glimtkRecti
339 #define glBegin glimtkBegin
340 #define glTexCoord2i glimtkTexCoord2i
341 #define glVertex2i glimtkVertex2i
342 #define glTexCoord2d glimtkTexCoord2d
343 #define glVertex2d glimtkVertex2d
344 #define glTexCoord2f glimtkTexCoord2f
345 #define glVertex2f glimtkVertex2f
346 #define glEnd glimtkEnd
347 #define glColor3f glimtkColor3f
348 #define glColor4ub glimtkColor4ub
349 #define glColor4fv glimtkColor4fv
350 #define glNormal3fv glimtkNormal3fv
351 #define glNormal3f glimtkNormal3f
352 #define glTexCoord2fv glimtkTexCoord2fv
353 #define glVertex3d glimtkVertex3d
354 #define glVertex3dv glimtkVertex3dv
355 #define glVertex3f glimtkVertex3f
356 #define glVertex3fv glimtkVertex3fv
358 #define glLoadMatrixd glmsLoadMatrixd
359 #define glMultMatrixd glmsMultMatrixd
360 #define glFrustum glmsFrustum
361 #define glOrtho glmsOrtho
362 #define glScaled glmsScaled
363 #define glScalef glmsScaled
364 #define glTranslated glmsTranslated
365 #define glRotated glmsRotated
366 #define glMatrixMode glmsMatrixMode
367 #define glLoadIdentity glmsLoadIdentity
368 #define glPushMatrix glmsPushMatrix
369 #define glPopMatrix glmsPopMatrix
371 #define glLineStipple glesLineStipple
372 #define glColorMaterial glesColorMaterial
373 #define glLightModeli glesLightModeli
377 public void glesColorMaterial(int a, int b)
379 PrintLn("glColorMaterial stub");
382 static GLuint stippleTexture;
383 #if defined(ES1_1) || defined(SHADERS) || defined(EM_MODE)
384 static bool stippleEnabled;
387 public void glesLineStipple( int i, unsigned short j )
391 for(x = 0; x < 16; x++)
393 bool v = (j & (1 << x)) != 0;
394 texture[x] = v ? 0xFFFFFFFF : 0;
397 glGenTextures(1, &stippleTexture);
398 glBindTexture(GL_TEXTURE_2D, stippleTexture);
399 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
400 glEnable(GL_TEXTURE_2D);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
405 glMatrixMode(GL_TEXTURE);
407 //glTranslated(1.0/backAttrib->texW/2.0f, 1.0/backAttrib->texH/2.0f, 0.0f);
408 glScaled(i/16.0, 1, 1.0f);
409 glTranslated(0.5, 0.5, 0);
410 glMatrixMode(MatrixMode::projection);
413 public void glesLightModeli( unsigned int pname, int param )
415 #if !defined(EM_MODE)
416 if(pname == GL_LIGHT_MODEL_TWO_SIDE)
417 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
421 #if defined(__ANDROID__) || defined(__ODROID__)
422 void glClearDepth( double depth ) { glClearDepthf((float)depth); }
423 void glFogi( unsigned int pname, int param ) { }
424 void glPolygonMode( unsigned int i, unsigned int j ) { }
427 // *** Picking won't be supported for now ***
428 void glPushName( unsigned int i ) { }
429 void glLoadName( unsigned int i ) { }
432 // Probably replace by regular glBlendFunc ...
433 void glBlendFuncSeparate(int a, int b, int c, int d)
438 // For direct pixel blitting...
439 void glRasterPos2d(double a, double b) { }
440 void glPixelZoom(float a, float b) { }
441 void glDrawPixels(int a, int b, int c, int d, void * e) { }
445 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
446 static int primitiveTypes[RenderPrimitiveType] =
448 GL_POINTS, GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GLIMTKMode::quads, GLIMTKMode::quadStrip, GL_LINE_STRIP
453 // Non OpenGL ES friendly stuff
457 //#undef GL_UNSIGNED_INT
463 #undef GL_POLYGON_STIPPLE
464 #undef GL_LINE_STIPPLE
467 #undef GL_ALL_ATTRIB_BITS
468 #undef GL_LIGHT_MODEL_LOCAL_VIEWER
472 static int displayWidth, displayHeight;
474 #define GL_CLAMP_TO_EDGE 0x812F
476 static bool vboAvailable;
478 static bool useSingleGLContext = false;
479 class OGLDisplay : struct
481 #if defined(__WIN32__)
492 byte * pboMemory1, * pboMemory2;
494 #elif !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
495 GLXContext glContext;
498 XShmSegmentInfo shminfo;
500 XShmSegmentInfo shminfoShape;
505 X11Picture windowPicture;
506 X11Picture pixmapPicture;
508 X11Picture shapePicture;
511 ColorAlpha * flippingBuffer;
512 int flipBufH, flipBufW;
518 static void APIENTRY openglCallbackFunction(GLenum source,
523 const GLchar* message,
524 const void* userParam)
526 PrintLn("---------------------opengl-callback-start------------");
527 PrintLn("message: ", message);
531 case GL_DEBUG_TYPE_ERROR: PrintLn("ERROR"); break;
532 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: PrintLn("DEPRECATED_BEHAVIOR"); break;
533 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: PrintLn("UNDEFINED_BEHAVIOR"); break;
534 case GL_DEBUG_TYPE_PORTABILITY: PrintLn("PORTABILITY"); break;
535 case GL_DEBUG_TYPE_PERFORMANCE: PrintLn("PERFORMANCE"); break;
536 case GL_DEBUG_TYPE_OTHER: PrintLn("OTHER"); break;
543 case GL_DEBUG_SEVERITY_LOW: PrintLn("LOW"); break;
544 case GL_DEBUG_SEVERITY_MEDIUM: PrintLn("MEDIUM"); break;
545 case GL_DEBUG_SEVERITY_HIGH: PrintLn("HIGH"); break;
547 PrintLn("---------------------opengl-callback-end--------------");
551 class OGLSystem : struct
556 #if defined(__WIN32__)
557 PIXELFORMATDESCRIPTOR pfd;
562 #elif !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
563 XVisualInfo * visualInfo;
564 GLXContext glContext;
565 GLXDrawable glxDrawable;
569 class OGLSurface : struct
577 float foreground[4], background[4], bitmapMult[4];
580 class OGLMesh : struct
589 class OGLIndices : struct
599 static void setupDebugging()
602 if(glDebugMessageCallback)
604 GLuint unusedIds = 0;
606 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
607 glDebugMessageCallback(openglCallbackFunction, null);
608 glDebugMessageControl(GL_DONT_CARE,
618 #if defined(__WIN32__)
619 static HGLRC winCreateContext(HDC hdc)
621 if(wglCreateContextAttribsARB)
625 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
626 WGL_CONTEXT_MINOR_VERSION_ARB, 4,
627 WGL_CONTEXT_FLAGS_ARB, /*WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | */WGL_CONTEXT_DEBUG_BIT_ARB,
628 WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB /*WGL_CONTEXT_CORE_PROFILE_BIT_ARB*/,
631 return wglCreateContextAttribsARB(hdc, null, attribs);
634 return wglCreateContext(hdc);
638 class OpenGLDisplayDriver : DisplayDriver
640 class_property(name) = "OpenGL";
642 bool LockSystem(DisplaySystem displaySystem)
644 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
645 OGLSystem oglSystem = displaySystem.driverData;
646 if(useSingleGLContext) return true;
647 #if defined(__WIN32__)
648 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
649 #elif defined(__unix__) || defined(__APPLE__)
650 //if(previous) return true;
651 // printf("Making SYSTEM current\n");
652 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
653 //previous = oglSystem.glContext;
656 GLABBindBuffer(GL_ARRAY_BUFFER, 0);
657 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
661 void UnlockSystem(DisplaySystem displaySystem)
663 if(useSingleGLContext) return;
664 #if defined(__WIN32__)
665 wglMakeCurrent(null, null);
666 #elif defined(__unix__) || defined(__APPLE__)
667 // printf("Making NULL current\n");
668 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
670 glXMakeCurrent(xGlobalDisplay, None, null);
676 bool Lock(Display display)
678 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
679 OGLDisplay oglDisplay = display.driverData;
680 if(useSingleGLContext) return true;
681 #if defined(__WIN32__)
682 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
683 #elif defined(__unix__) || defined(__APPLE__)
684 // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
685 // printf(" Making DISPLAY current\n");
686 glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
689 GLABBindBuffer(GL_ARRAY_BUFFER, 0);
690 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
694 void Unlock(Display display)
696 if(useSingleGLContext) return;
697 //printf(" Making NULL current\n");
698 //glXMakeCurrent(xGlobalDisplay, None, null);
700 LockSystem(display.displaySystem);
703 void DestroyDisplay(Display display)
705 OGLDisplay oglDisplay = display.driverData;
709 #if defined(__WIN32__)
710 wglMakeCurrent( null, null );
713 wglDeleteContext(oglDisplay.glrc);
715 if(oglDisplay.hdc && oglDisplay.pBuffer)
716 wglReleasePbufferDCARB(oglDisplay.pBuffer, oglDisplay.hdc);
718 if(oglDisplay.pBuffer)
719 wglDestroyPbufferARB(oglDisplay.pBuffer);
722 ReleaseDC(display.window, oglDisplay.hdc);
724 if(oglDisplay.memDC) DeleteDC(oglDisplay.memDC);
725 if(oglDisplay.memBitmap) DeleteObject(oglDisplay.memBitmap);
727 #elif defined(__unix__) || defined(__APPLE__)
728 #if defined(__ANDROID__) || defined(__ODROID__) || defined(__EMSCRIPTEN__)
730 if(oglDisplay.shapePixmap)
731 XFreePixmap(xGlobalDisplay, oglDisplay.shapePixmap);
732 if(oglDisplay.pixmap)
733 XFreePixmap(xGlobalDisplay, oglDisplay.pixmap);
736 if(oglDisplay.shminfoShape.shmid != -1)
738 XShmDetach(xGlobalDisplay, &oglDisplay.shminfo);
739 if(oglDisplay.shminfo.shmaddr != (void *)-1)
740 shmdt(oglDisplay.shminfo.shmaddr);
741 shmctl(oglDisplay.shminfo.shmid, IPC_RMID, 0);
744 if(oglDisplay.shapeImage)
746 if(oglDisplay.shminfoShape.shmid != -1)
748 XShmDetach(xGlobalDisplay, &oglDisplay.shminfoShape);
749 if(oglDisplay.shminfoShape.shmaddr != (void *)-1)
750 shmdt(oglDisplay.shminfoShape.shmaddr);
751 shmctl(oglDisplay.shminfoShape.shmid, IPC_RMID, 0);
753 XDestroyImage(oglDisplay.shapeImage);
754 oglDisplay.shapeImage = None;
757 glXMakeCurrent(xGlobalDisplay, None, null);
759 if(oglDisplay.glContext)
760 glXDestroyContext(xGlobalDisplay, oglDisplay.glContext);
763 delete oglDisplay.flippingBuffer;
765 display.driverData = null;
769 void ::CheckExtensions(OGLSystem oglSystem)
771 const char * extensions = (const char *)glGetString(GL_EXTENSIONS);
773 oglSystem.pow2textures = strstr(extensions, "GL_ARB_texture_non_power_of_two") ? false : true;
774 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
777 bool CreateDisplaySystem(DisplaySystem displaySystem)
780 OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
783 oglSystem.hwnd = CreateWindow("static", null, 0,0,0,0,0,null,null,null,null);
785 oglSystem.hdc = GetDC(oglSystem.hwnd);
789 oglSystem.pfd.nSize = (short)sizeof(oglSystem.pfd);
790 oglSystem.pfd.nVersion = 1;
791 oglSystem.pfd.dwFlags = PFD_DRAW_TO_WINDOW /*PFD_DRAW_TO_BITMAP*/ | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
792 oglSystem.pfd.iPixelType = PFD_TYPE_RGBA;
793 oglSystem.pfd.cColorBits = 24;
794 oglSystem.pfd.cAlphaBits = 8;
795 oglSystem.pfd.cDepthBits = 24;
796 oglSystem.pfd.iLayerType = PFD_MAIN_PLANE;
798 oglSystem.format = ChoosePixelFormat(oglSystem.hdc, &oglSystem.pfd);
799 DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
801 if(oglSystem.pfd.cColorBits > 8)
803 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
804 oglSystem.glrc = wglCreateContext(oglSystem.hdc);
807 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
809 wglChoosePixelFormatARB = (void *) wglGetProcAddress("wglChoosePixelFormatARB");
810 wglGetExtensionsStringARB = (void *)wglGetProcAddress("wglGetExtensionsStringARB");
811 wglCreatePbufferARB = (void *)wglGetProcAddress("wglCreatePbufferARB");
812 wglGetPbufferDCARB = (void *)wglGetProcAddress("wglGetPbufferDCARB");
813 wglQueryPbufferARB = (void *)wglGetProcAddress("wglQueryPbufferARB");
814 wglDestroyPbufferARB = (void *)wglGetProcAddress("wglDestroyPbufferARB");
815 wglReleasePbufferDCARB = (void *)wglGetProcAddress("wglReleasePbufferDCARB");
816 wglBindTexImageARB = (void *)wglGetProcAddress("wglBindTexImageARB");
817 wglReleaseTexImageARB = (void *)wglGetProcAddress("wglReleaseTexImageARB");
818 wglSwapIntervalEXT = (void *)wglGetProcAddress("wglSwapIntervalEXT");
819 wglCreateContextAttribsARB = (void *)wglGetProcAddress("wglCreateContextAttribsARB");
821 glLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT" );
822 glUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
824 // eSystem_LoggingMode(LOG_MSGBOX, null);
826 if(wglChoosePixelFormatARB)
831 float fAttributes[] = {0,0};
834 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
835 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
836 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
837 WGL_COLOR_BITS_ARB,24,
838 WGL_ALPHA_BITS_ARB,8,
839 WGL_DEPTH_BITS_ARB,16,
840 WGL_STENCIL_BITS_ARB,0,
841 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
842 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
843 WGL_SAMPLES_ARB, 4, // Check For 4x Multisampling
847 //Log("Found wglChoosePixelFormatARB\n");
849 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
850 if(!valid || !numFormats)
852 //Log("Can't find 4x multi sampling\n");
854 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
855 if(!valid || !numFormats)
857 // Log("Can't find 2x multi sampling\n");
860 valid = wglChoosePixelFormatARB(oglSystem.hdc,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
863 if(valid && numFormats)
865 oglSystem.format = pixelFormat;
866 wglMakeCurrent(null, null);
867 wglDeleteContext(oglSystem.glrc);
869 // *** DescribePixelFormat does not support WGL pixel formats! ***
870 //DescribePixelFormat(oglSystem.hdc, oglSystem.format, sizeof(oglSystem.pfd), &oglSystem.pfd);
871 SetPixelFormat(oglSystem.hdc, oglSystem.format, &oglSystem.pfd);
872 //Log("Successfully set pixel format\n");
874 oglSystem.glrc = winCreateContext(oglSystem.hdc);
875 wglMakeCurrent(oglSystem.hdc, oglSystem.glrc);
879 eSystem_Logf("Can't find wglChoosePixelFormatARB\n");*/
883 wglMakeCurrent(null, null);
885 //eSystem_DumpErrors(true);
889 #elif defined(__unix__) || defined(__APPLE__)
891 #if defined(__ANDROID__)
892 egl_init_display(guiApp.desktop.windowHandle);
893 #elif defined(__ODROID__)
894 egl_init_display((uint)displaySystem.window);
895 CheckExtensions(oglSystem);
897 #elif defined(__EMSCRIPTEN__)
898 if(glfwInit() == GL_TRUE)
900 const int width = 640, height = 480;
901 if(glfwOpenWindow(width, height, 8, 8, 8, 8, 16, 0, GLFW_WINDOW) == GL_TRUE)
907 printf("glfwOpenWindow() failed\n"); //glfwTerminate();
910 printf("glfwInit() failed\n"); //glfwTerminate();
913 X11Window root = RootWindow( xGlobalDisplay, DefaultScreen( xGlobalDisplay ) );
914 XSetWindowAttributes attr;
919 #ifndef ECERE_MINIGLX
920 GLX_USE_GL, GLX_DEPTH_SIZE, 1,
923 GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
927 oglSystem.visualInfo = glXChooseVisual( xGlobalDisplay, DefaultScreen( xGlobalDisplay ), attrList );
928 attr.background_pixel = 0;
929 attr.border_pixel = 0;
930 attr.colormap = XCreateColormap( xGlobalDisplay, root, oglSystem.visualInfo->visual, AllocNone);
931 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
932 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
934 oglSystem.glxDrawable = XCreateWindow( xGlobalDisplay, root, 0, 0, 1, 1, 0, oglSystem.visualInfo->depth, InputOutput,
935 oglSystem.visualInfo->visual, mask, &attr );
937 if(oglSystem.visualInfo)
939 oglSystem.glContext = glXCreateContext(xGlobalDisplay, oglSystem.visualInfo, null, True);
940 if(oglSystem.glContext)
942 glXMakeCurrent(xGlobalDisplay, oglSystem.glxDrawable, oglSystem.glContext);
944 CheckExtensions(oglSystem);
945 glXMakeCurrent(xGlobalDisplay, None, null);
952 displaySystem.flags.alpha = true;
953 displaySystem.flags.flipping = true;
954 displaySystem.pixelFormat = pixelFormat888;
958 void DestroyDisplaySystem(DisplaySystem displaySystem)
960 OGLSystem oglSystem = displaySystem.driverData;
962 #if defined(__WIN32__)
963 wglMakeCurrent( null, null );
966 wglDeleteContext(oglSystem.glrc);
969 ReleaseDC(oglSystem.hwnd, oglSystem.hdc);
970 DestroyWindow(oglSystem.hwnd);
972 #elif defined(__unix__) || defined(__APPLE__)
973 #if defined(__ANDROID__) || defined(__ODROID__)
975 #elif defined(__EMSCRIPTEN__)
978 if(oglSystem.visualInfo)
981 __miniglx_XFree(oglSystem.visualInfo);
983 XFree(oglSystem.visualInfo);
987 if(oglSystem.glxDrawable)
989 XDestroyWindow(xGlobalDisplay, oglSystem.glxDrawable);
990 oglSystem.glxDrawable = 0;
997 bool CreateDisplay(Display display)
1000 OGLDisplay oglDisplay = display.driverData;
1001 #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
1002 OGLSystem oglSystem = display.displaySystem.driverData;
1006 oglDisplay = display.driverData = OGLDisplay { };
1007 //printf("Inside CreateDisplay\n");
1009 #if defined(__WIN32__) || defined(USEPBUFFER)
1010 if(!display.alphaBlend)
1013 #if defined(__WIN32__)
1014 oglDisplay.hdc = GetDC(display.window);
1015 SetPixelFormat(oglDisplay.hdc, oglSystem.format, &oglSystem.pfd);
1016 if((oglDisplay.glrc = winCreateContext(oglDisplay.hdc)))
1018 wglShareLists(oglSystem.glrc, oglDisplay.glrc);
1019 wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
1021 // Had to recreate the context here ? (Alpha blending visual)
1023 ogl_LoadFunctions();
1024 CheckExtensions(oglSystem);
1025 vboAvailable = glBindBuffer != null;
1029 loadShaders("<:ecere>shaders/fixed.vertex", "<:ecere>shaders/fixed.frag");
1031 glEnableClientState(GL_VERTEX_ARRAY);
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 glShadeModel(GL_FLAT);
1112 // glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, true);
1113 #if !defined(EM_MODE)
1114 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);
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 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1703 mipMap.Allocate(null, w, h, w, pixelFormatRGBA, false);
1705 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1706 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1710 bitmap.driverData = (void *)(uintptr)glBitmap;
1711 bitmap.driver = displaySystem.driver;
1719 bool MakeDDBitmap(DisplaySystem displaySystem, Bitmap bitmap, bool mipMaps)
1721 bool result = false;
1722 OGLSystem oglSystem = displaySystem.driverData;
1723 Bitmap convBitmap = bitmap;
1727 convBitmap.Copy(bitmap);
1730 // Pre process the bitmap... First make it 32 bit
1731 if(/*bitmap.pixelFormat == pixelFormatRGBA || */convBitmap.Convert(null, pixelFormat888, null))
1734 uint w = bitmap.width, h = bitmap.height;
1735 GLuint glBitmap = 0;
1736 if(oglSystem.pow2textures)
1741 w = Min(w, oglSystem.maxTextureSize);
1742 h = Min(h, oglSystem.maxTextureSize);
1746 while(w * 2 < h) w *= 2;
1747 while(h * 2 < w) h *= 2;
1750 // Switch ARGB to RGBA
1751 //if(bitmap.format != pixelFormatRGBA)
1753 for(c=0; c<bitmap.size; c++)
1755 // ((ColorRGBA *)bitmap.picture)[c] = ((ColorAlpha *)bitmap.picture)[c];
1757 ColorAlpha color = ((ColorAlpha *)convBitmap.picture)[c];
1758 ((ColorRGBA *)convBitmap.picture)[c] = ColorRGBA { color.color.r, color.color.g, color.color.b, color.a };
1761 // convBitmap.pixelFormat = pixelFormat888;
1764 glGenTextures(1, &glBitmap);
1767 //int error = glGetError();
1771 glBindTexture(GL_TEXTURE_2D, glBitmap);
1772 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1774 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
1775 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1777 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1779 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1780 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1782 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1783 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1784 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
1785 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0 );
1788 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1792 for(level = 0; result && (w >= 1 || h >= 1); level++, w >>= 1, h >>= 1)
1797 if(bitmap.width != w || bitmap.height != h)
1799 mipMap = Bitmap { };
1800 if(mipMap.Allocate(null, w, h, w, convBitmap.pixelFormat, false))
1802 Surface mipSurface = mipMap.GetSurface(0,0,null);
1803 mipSurface.Filter(convBitmap, 0,0,0,0, w, h, convBitmap.width, convBitmap.height);
1813 mipMap = convBitmap;
1820 // glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1821 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, mipMap.picture);
1822 //printf("Calling glTexImage2D\n");
1823 //glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
1824 //printf("width = %d (Should be %d, %d)\n", width, w, h);
1825 if((error = glGetError()))
1827 //Logf("OpenGL Bitmap MakeDD error: %d...\n", error);
1828 //printf("OpenGL Bitmap MakeDD error: %d...\n", error);
1832 if(mipMap != convBitmap)
1837 convBitmap.driver.FreeBitmap(convBitmap.displaySystem, convBitmap);
1838 bitmap.driverData = (void *)(uintptr)glBitmap;
1839 bitmap.driver = displaySystem.driver;
1844 FreeBitmap(displaySystem, bitmap);
1845 else if(oglSystem.loadingFont)
1847 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1848 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1849 oglSystem.loadingFont = false;
1855 void ReleaseSurface(Display display, Surface surface)
1857 glDisable(GL_SCISSOR_TEST);
1858 delete surface.driverData;
1859 surface.driverData = null;
1862 bool GetBitmapSurface(DisplaySystem displaySystem, Surface surface, Bitmap bitmap, int x, int y, Box clip)
1867 bool GetSurface(Display display, Surface surface, int x,int y, Box clip)
1869 bool result = false;
1870 OGLSurface oglSurface = surface.driverData = OGLSurface { };
1872 //Logf("GetSurface\n");
1876 if(displayWidth != display.width || displayHeight != display.height)
1878 displayWidth = display.width;
1879 displayHeight = display.height;
1881 glViewport(0,0,display.width,display.height);
1883 glOrtho(0,display.width,display.height,0,0.0,1.0);
1886 surface.offset.x = x;
1887 surface.offset.y = y;
1888 surface.unclippedBox = surface.box = clip;
1889 oglSurface.bitmapMult[0] = 1;
1890 oglSurface.bitmapMult[1] = 1;
1891 oglSurface.bitmapMult[2] = 1;
1892 oglSurface.bitmapMult[3] = 1;
1894 glEnable(GL_SCISSOR_TEST);
1897 (display.height) -(y+clip.bottom)-1,
1898 clip.right-clip.left+1,
1899 clip.bottom-clip.top+1);
1905 void Clip(Display display, Surface surface, Box clip)
1914 box.Clip(surface.unclippedBox);
1918 box = surface.box = surface.unclippedBox;
1919 box.left += surface.offset.x;
1920 box.top += surface.offset.y;
1921 box.right+= surface.offset.x;
1922 box.bottom += surface.offset.y;
1925 box.left,display.height - box.bottom - 1,
1926 box.right-box.left+1, box.bottom-box.top+1);
1929 bool GrabScreen(Display display, Bitmap bitmap, int x, int y, unsigned int w, unsigned int h)
1931 bool result = false;
1932 OGLDisplay oglDisplay = display.driverData;
1933 ColorAlpha * flippingBuffer = oglDisplay.flippingBuffer;
1935 if(oglDisplay.flippingBuffer)
1937 if(bitmap.pixelFormat != pixelFormat888 || bitmap.width < w || bitmap.height < h)
1940 bitmap.Allocate(null, w,h,w, pixelFormat888, false);
1946 glPixelStorei(GL_PACK_ALIGNMENT, 4);
1947 glPixelStorei(GL_PACK_ROW_LENGTH, bitmap.stride);
1948 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
1949 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
1950 glReadPixels(x,display.height-h-y,w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, flippingBuffer);
1953 for(row = 0; row<h; row++)
1954 CopyBytesBy4(((ColorAlpha *)bitmap.picture) + row * w, ((ColorAlpha *)flippingBuffer) + (h-row-1) * w, w);
1961 void SetForeground(Display display, Surface surface, ColorAlpha color)
1963 OGLSurface oglSurface = surface.driverData;
1965 //Logf("SetForeground\n");
1967 oglSurface.foreground[0] = color.color.r/255.0f;
1968 oglSurface.foreground[1] = color.color.g/255.0f;
1969 oglSurface.foreground[2] = color.color.b/255.0f;
1970 //oglSurface.foreground[3] = 1.0f;
1971 oglSurface.foreground[3] = color.a/255.0f;
1973 //if(!oglSurface.foreground[3])printf("bug");
1976 void SetBackground(Display display, Surface surface, ColorAlpha color)
1978 OGLSurface oglSurface = surface.driverData;
1980 //Logf("SetBackground\n");
1982 oglSurface.background[0] = color.color.r/255.0f;
1983 oglSurface.background[1] = color.color.g/255.0f;
1984 oglSurface.background[2] = color.color.b/255.0f;
1985 //oglSurface.background[3] = 1.0;
1986 oglSurface.background[3] = color.a/255.0f;
1989 void SetBlitTint(Display display, Surface surface, ColorAlpha color)
1991 OGLSurface oglSurface = surface.driverData;
1993 oglSurface.bitmapMult[0] = color.color.r/255.0f;
1994 oglSurface.bitmapMult[1] = color.color.g/255.0f;
1995 oglSurface.bitmapMult[2] = color.color.b/255.0f;
1996 oglSurface.bitmapMult[3] = color.a/255.0f;
1999 ColorAlpha GetPixel(Display display, Surface surface,int x,int y)
2004 void PutPixel(Display display, Surface surface,int x,int y)
2006 OGLSurface oglSurface = surface.driverData;
2008 //Logf("PutPixel\n");
2010 glColor4fv(oglSurface.foreground);
2012 // glVertex2i(x+surface.offset.x, y+surface.offset.y);
2013 glVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
2018 void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
2020 OGLSurface oglSurface = surface.driverData;
2021 float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
2036 x1 += surface.offset.x;
2037 y1 += surface.offset.y;
2038 x2 += surface.offset.x;
2039 y2 += surface.offset.y;
2043 glColor4fv(oglSurface.foreground);
2045 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2048 glTexCoord2f(0.5f, 0);
2049 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2050 glTexCoord2f(Max(x2-x1, y2-y1) + 0.5f, 0);
2051 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2060 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2061 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2067 void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
2069 OGLSurface oglSurface = surface.driverData;
2070 x1 += surface.offset.x;
2071 y1 += surface.offset.y;
2072 x2 += surface.offset.x;
2073 y2 += surface.offset.y;
2075 //Logf("Rectangle\n");
2077 glColor4fv(oglSurface.foreground);
2078 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2083 glTexCoord2f(0.5f, 0);
2084 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2085 glTexCoord2f(y2-y1 + 0.5f, 0);
2086 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2088 glTexCoord2f(0.5f, 0);
2089 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2090 glTexCoord2f(x2 - x1 + 0.5f, 0);
2091 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2093 glTexCoord2f(0.5f, 0);
2094 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2095 glTexCoord2f(y1 - y2 + 0.5f, 0);
2096 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2098 glTexCoord2f(0.5f, 0);
2099 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2100 glTexCoord2f(x1 - x2 + 0.5f, 0);
2101 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2106 glBegin(GL_LINE_LOOP);
2113 glVertex2f(x1 + 0.5f, y1 + 0.5f);
2114 glVertex2f(x1 + 0.5f, y2 + 0.5f);
2115 glVertex2f(x2 + 0.5f, y2 + 0.5f);
2116 glVertex2f(x2 + 0.5f, y1 + 0.5f);
2121 void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
2123 OGLSurface oglSurface = surface.driverData;
2126 glColor4fv(oglSurface.background);
2130 glVertex2f(x1+surface.offset.x, y1+surface.offset.y);
2131 glVertex2f(x1+surface.offset.x, y2+surface.offset.y+1);
2132 glVertex2f(x2+surface.offset.x+1, y2+surface.offset.y+1);
2133 glVertex2f(x2+surface.offset.x+1, y1+surface.offset.y);
2136 glRecti(x1+surface.offset.x, y1+surface.offset.y,
2137 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2140 glRectf(x1+surface.offset.x, y1+surface.offset.y,
2141 x2+surface.offset.x + 1, y2+surface.offset.y + 1);
2145 void Clear(Display display, Surface surface, ClearType type)
2147 OGLDisplay oglDisplay = display.driverData;
2148 OGLSurface oglSurface = surface.driverData;
2151 if(type != depthBuffer)
2152 glClearColor(oglSurface.background[0], oglSurface.background[1], oglSurface.background[2], oglSurface.background[3]);
2153 if(type != colorBuffer && !oglDisplay.depthWrite)
2155 glDepthMask((byte)bool::true);
2157 glClear(((type != depthBuffer) ? GL_COLOR_BUFFER_BIT : 0) |
2158 ((type != colorBuffer) ? GL_DEPTH_BUFFER_BIT : 0));
2159 if(type != colorBuffer && !oglDisplay.depthWrite)
2161 glDepthMask((byte)bool::false);
2165 bool ConvertBitmap(DisplaySystem displaySystem, Bitmap bitmap, PixelFormat format, ColorAlpha * palette)
2170 void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2172 OGLSurface oglSurface = surface.driverData;
2174 if(!oglSurface.writingText)
2176 // glTranslatef(-0.375f, -0.375f, 0.0f);
2177 glEnable(GL_TEXTURE_2D);
2178 glColor4fv(oglSurface.bitmapMult);
2180 else if(oglSurface.xOffset)
2181 glTranslated(oglSurface.xOffset / 64.0/*-0.375*/, 0.0, 0.0);
2183 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2184 glBegin(GLIMTKMode::quads);
2188 glTexCoord2f((float)sx/ bitmap.width, (float)(sy-h)/ bitmap.height);
2189 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2190 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy-h)/ bitmap.height);
2191 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2192 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2193 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2194 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2195 glVertex2i(dx+surface.offset.x, dy-h+surface.offset.y);
2200 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2201 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2202 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2203 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2204 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2205 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2206 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2207 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2210 glTexCoord2f((float)sx / bitmap.width, (float)sy/ bitmap.height);
2211 glVertex2f((float)dx+surface.offset.x, (float)dy+surface.offset.y);
2212 glTexCoord2f((float)(sx+w)/ bitmap.width, (float)sy/ bitmap.height);
2213 glVertex2f((float)dx+w+surface.offset.x, (float)dy+surface.offset.y);
2214 glTexCoord2f((float)(sx+w) / bitmap.width, (float)(sy+h)/ bitmap.height);
2215 glVertex2f((float)dx+w+surface.offset.x, (float)dy+h+surface.offset.y);
2216 glTexCoord2f((float)sx/ bitmap.width, (float)(sy+h)/ bitmap.height);
2217 glVertex2f((float)dx+surface.offset.x, (float)dy+h+surface.offset.y);
2221 if(!oglSurface.writingText)
2223 glDisable(GL_TEXTURE_2D);
2225 //glTranslate(0.375, 0.375, 0.0);
2227 else if(oglSurface.xOffset)
2228 glTranslated(-oglSurface.xOffset / 64.0/*+0.375*/, 0.0, 0.0);
2231 void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2233 OGLSurface oglSurface = surface.driverData;
2235 //glTranslate(-0.375, -0.375, 0.0);
2237 glEnable(GL_TEXTURE_2D);
2238 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
2240 glColor4fv(oglSurface.bitmapMult);
2242 glBegin(GLIMTKMode::quads);
2246 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2247 glVertex2i(dx+surface.offset.x, dy+surface.offset.y);
2249 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2250 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2252 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2253 glVertex2i(dx+w+surface.offset.x, dy-h+surface.offset.y);
2255 glTexCoord2f((float)(sx) / bitmap.width, (float)(sy)/ bitmap.height);
2256 glVertex2i(dx+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+surface.offset.y);
2263 glTexCoord2f((float)(sx+sw)/ bitmap.width, (float)(sy)/ bitmap.height);
2264 glVertex2i(dx+w+surface.offset.x, dy+surface.offset.y);
2266 glTexCoord2f((float)(sx+sw) / bitmap.width, (float)(sy+sh)/ bitmap.height);
2267 glVertex2i(dx+w+surface.offset.x, dy+h+surface.offset.y);
2269 glTexCoord2f((float)(sx)/ bitmap.width, (float)(sy+sh)/ bitmap.height);
2270 glVertex2i(dx+surface.offset.x, dy+h+surface.offset.y);
2275 glDisable(GL_TEXTURE_2D);
2277 //glTranslate(0.375, 0.375, 0.0);
2280 void Filter(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2282 Stretch(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2285 void StretchDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2287 #if !defined(EM_MODE)
2288 float s2dw,s2dh,d2sw,d2sh;
2289 //bool flipX = false, flipY = false;
2291 //Logf("StretchDI\n");
2293 if(Sgn(w) != Sgn(sw))
2299 if(Sgn(h) != Sgn(sh))
2311 //Clip against the edges of the source
2314 dx+=(int)((0-sx) * s2dw);
2315 w-=(int)((0-sx) * s2dw);
2321 dy+=(int)((0-sy) * s2dh);
2322 h-=(int)((0-sy) * s2dh);
2327 if(sx+sw>bitmap.width-1)
2329 w-=(int)((sx+sw-(bitmap.width-1)-1)*s2dw);
2330 sw-=sx+sw-(bitmap.width-1)-1;
2332 if(sy+sh>(bitmap.height-1))
2334 h-=(int)((sy+sh-(bitmap.height-1)-1)*s2dh);
2335 sh-=sy+sh-(bitmap.height-1)-1;
2337 //Clip against the edges of the surfaceination
2338 if(dx<surface.box.left)
2341 sx+=(int)((surface.box.left-dx)*d2sw);
2342 sw-=(int)((surface.box.left-dx)*d2sw);
2343 w-=surface.box.left-dx;
2344 dx=surface.box.left;
2346 if(dy<surface.box.top)
2348 sy+=(int)((surface.box.top-dy)*d2sh);
2349 sh-=(int)((surface.box.top-dy)*d2sh);
2350 h-=surface.box.top-dy;
2353 if(dx+w>surface.box.right)
2355 //if(flip) sx+=(int)((dx+w-surface.box.right-1)*d2sw);
2356 sw-=(int)((dx+w-surface.box.right-1)*d2sw);
2357 w-=dx+w-surface.box.right-1;
2359 if(dy+h>surface.box.bottom)
2361 sh-=(int)((dy+h-surface.box.bottom-1)*d2sh);
2362 h-=dy+h-surface.box.bottom-1;
2364 if((w<=0)||(h<=0)||(sw<=0)||(sh<=0)) return;
2366 dx += surface.offset.x;
2367 dy += surface.offset.y;
2369 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2371 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2372 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2373 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2374 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2375 glRasterPos2d(dx,dy);
2376 //glPixelZoom(flipX ? -s2dw : s2dw, flipY ? s2dh : -s2dh);
2377 glPixelZoom(s2dw, -s2dh);
2378 glDrawPixels(sw,sh,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2379 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2380 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2381 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2382 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2387 void BlitDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
2389 #if !defined(EM_MODE)
2392 //Clip against the edges of the source
2405 if(sx+w>bitmap.width-1)
2406 w-=sx+w-(bitmap.width-1)-1;
2407 if(sy+h>bitmap.height-1)
2408 h-=sy+h-(bitmap.height-1)-1;
2409 //Clip against the edges of the surfaceination
2410 if(dx<surface.box.left)
2413 sx+=surface.box.left-dx;
2414 w-=surface.box.left-dx;
2415 dx=surface.box.left;
2417 if(dy<surface.box.top)
2419 sy+=surface.box.top-dy;
2420 h-=surface.box.top-dy;
2423 if(dx+w>surface.box.right)
2425 //if(flip) sx+=dx+w-surface.box.right-1;
2426 w-=dx+w-surface.box.right-1;
2428 if(dy+h>surface.box.bottom)
2429 h-=dy+h-surface.box.bottom-1;
2433 dx += surface.offset.x;
2434 dy += surface.offset.y;
2436 if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
2438 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2439 glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
2440 glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
2441 glPixelStorei(GL_UNPACK_SKIP_ROWS, sy);
2442 glRasterPos2d(dx,dy);
2444 glDrawPixels(w,h,GL_BGRA_EXT,GL_UNSIGNED_BYTE, bitmap.picture);
2445 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2446 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2447 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
2448 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
2453 void FilterDI(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
2455 StretchDI(display, surface, bitmap, dx, dy, sx, sy, w, h, sw, sh);
2458 void UnloadFont(DisplaySystem displaySystem, Font font)
2460 ((subclass(DisplayDriver))class(LFBDisplayDriver)).UnloadFont(displaySystem, font);
2463 Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags, float outlineSize, float outlineFade)
2466 OGLSystem oglSystem = displaySystem.driverData;
2467 oglSystem.loadingFont = true;
2468 font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags, outlineSize, outlineFade);
2472 void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * adv)
2474 ((subclass(DisplayDriver))class(LFBDisplayDriver)).FontExtent(displaySystem, font, text, len, width, height, prevGlyph, rPrevGlyph, adv);
2477 void WriteText(Display display, Surface surface, int x, int y, const char * text, int len, int prevGlyph, int * rPrevGlyph)
2479 OGLSurface oglSurface = surface.driverData;
2480 OGLSystem oglSystem = display.displaySystem.driverData;
2481 oglSystem.loadingFont = true;
2483 //glTranslated(-0.375, -0.375, 0.0);
2487 if(surface.textOpacity)
2490 FontExtent(display.displaySystem, surface.font, text, len, &w, &h, 0, null, &adv);
2492 display.displaySystem.driver.Area(display, surface,x,y,x+w-1,y+h-1);
2495 oglSurface.writingText = true;
2497 glEnable(GL_TEXTURE_2D);
2498 if(surface.font.outlineSize)
2500 ColorAlpha outlineColor = surface.outlineColor;
2501 glColor4ub(outlineColor.color.r, outlineColor.color.g, outlineColor.color.b, outlineColor.a);
2502 oglSurface.writingOutline = true;
2503 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len, prevGlyph, rPrevGlyph);
2504 oglSurface.writingOutline = false;
2506 glColor4fv(oglSurface.foreground);
2508 ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len, prevGlyph, rPrevGlyph);
2509 oglSurface.writingText = false;
2510 oglSystem.loadingFont = false;
2512 glDisable(GL_TEXTURE_2D);
2514 //glTranslated(0.375, 0.375, 0.0);
2517 void TextFont(Display display, Surface surface, Font font)
2519 ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextFont(display, surface, font);
2522 void TextOpacity(Display display, Surface surface, bool opaque)
2524 OGLSurface oglSurface = surface.driverData;
2525 oglSurface.opaqueText = opaque;
2528 void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * adv)
2530 OGLSurface oglSurface = surface.driverData;
2531 OGLSystem oglSystem = display.displaySystem.driverData;
2532 oglSystem.loadingFont = true;
2533 FontExtent(display.displaySystem, oglSurface.font, text, len, width, height, prevGlyph, rPrevGlyph, adv);
2534 oglSystem.loadingFont = false;
2537 void DrawingChar(Display display, Surface surface, char character)
2542 void LineStipple(Display display, Surface surface, uint32 stipple)
2544 //Logf("Stipple\n");
2548 #if defined(ES1_1) || defined(EM_MODE) || defined(SHADERS)
2549 stippleEnabled = true;
2550 glesLineStipple(1, (uint16)stipple);
2552 glLineStipple(1, (uint16)stipple);
2553 glEnable(GL_LINE_STIPPLE);
2558 #if defined(ES1_1) || defined(EM_MODE)
2559 stippleEnabled = false;
2560 glMatrixMode(GL_TEXTURE);
2562 glMatrixMode(MatrixMode::projection);
2563 glDisable(GL_TEXTURE_2D);
2565 glDisable(GL_LINE_STIPPLE);
2569 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
2570 void SetRenderState(Display display, RenderState state, uint value)
2572 OGLDisplay oglDisplay = display.driverData;
2573 //Logf("RenderState\n");
2579 glEnable(GL_MULTISAMPLE_ARB);
2581 glDisable(GL_MULTISAMPLE_ARB);
2585 glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
2589 if(value) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
2592 if(value) glDepthMask((byte)bool::true); else glDepthMask((byte)bool::false);
2593 oglDisplay.depthWrite = (bool)value;
2597 float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2598 glFogfv(GL_FOG_COLOR, (float *)&color);
2602 glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
2605 if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
2609 #if !defined(EM_MODE)
2610 float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
2611 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
2617 if(value) glColorMask(1,1,1,1); else glColorMask(1,1,1,0);
2622 #if defined(__WIN32__)
2623 wglSwapIntervalEXT(value ? 1 : 0);
2630 void SetLight(Display display, int id, Light light)
2632 #if !defined(EM_MODE)
2633 //Logf("SetLight\n");
2637 Object lightObject = light.lightObject;
2638 float position[4] = { 0, 0, 0, 0 };
2639 float color[4] = { 0, 0, 0, 1 };
2641 glEnable(GL_LIGHT0 + id);
2643 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, (float *)&light.diffuse);
2644 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, (float *)&light.ambient);
2645 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,(float *)&light.specular);
2648 if(!light.multiplier) light.multiplier = 1.0f;
2650 color[0] = light.diffuse.r * light.multiplier;
2651 color[1] = light.diffuse.g * light.multiplier;
2652 color[2] = light.diffuse.b * light.multiplier;
2653 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, color);
2655 color[0] = light.ambient.r * light.multiplier;
2656 color[1] = light.ambient.g * light.multiplier;
2657 color[2] = light.ambient.b * light.multiplier;
2658 glLightfv(GL_LIGHT0 + id, GL_AMBIENT, color);
2659 color[0] = light.specular.r * light.multiplier;
2660 color[1] = light.specular.g * light.multiplier;
2661 color[2] = light.specular.b * light.multiplier;
2662 glLightfv(GL_LIGHT0 + id, GL_SPECULAR,color);
2666 Vector3D positionVector;
2667 if(light.flags.spot)
2669 if(lightObject.flags.root || !lightObject.parent)
2671 positionVector = lightObject.transform.position;
2672 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2676 positionVector.MultMatrix(lightObject.transform.position, lightObject.parent.matrix);
2677 if(display.display3D.camera)
2678 positionVector.Subtract(positionVector, display.display3D.camera.cPosition);
2684 if(!light.direction.x && !light.direction.y && !light.direction.z)
2686 Vector3Df vector { 0,0,-1 };
2688 mat.RotationQuaternion(light.orientation);
2689 positionVector.MultMatrixf(vector, mat);
2693 positionVector = light.direction;
2698 position[0] = (float)positionVector.x;
2699 position[1] = (float)positionVector.y;
2700 position[2] = (float)positionVector.z;
2702 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2705 // Display Light Position
2706 glDisable(GL_LIGHTING);
2707 glDisable(GL_DEPTH_TEST);
2711 glVertex3fv(position);
2713 glEnable(GL_DEPTH_TEST);
2714 glEnable(GL_LIGHTING);
2718 if(lightObject.flags.root || !lightObject.parent)
2720 positionVector = light.target.transform.position;
2721 positionVector.Subtract(positionVector, display.camera.cPosition);
2725 positionVector.MultMatrix(light.target.transform.position,
2726 lightObject.light.target.parent.matrix);
2727 positionVector.Subtract(positionVector, display.camera.cPosition);
2730 position[0] = positionVector.x;
2731 position[1] = positionVector.y;
2732 position[2] = positionVector.z;
2734 glDisable(GL_LIGHTING);
2735 glDisable(GL_DEPTH_TEST);
2739 glVertex3fv(position);
2741 glEnable(GL_DEPTH_TEST);
2742 glEnable(GL_LIGHTING);
2745 if(light.flags.attenuation)
2747 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, light.Kc);
2748 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, light.Kl);
2749 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, light.Kq);
2752 if(light.flags.spot)
2755 #define MAXLIGHT 0.9
2756 float direction[4] = { (float)light.direction.x, (float)light.direction.y, (float)light.direction.z, 1 };
2757 // Figure out exponent out of the hot spot
2758 exponent = (float)(log(MAXLIGHT) / log(cos((light.hotSpot / 2))));
2760 glLightfv(GL_LIGHT0 + id, GL_SPOT_DIRECTION, direction);
2761 glLightf(GL_LIGHT0 + id, GL_SPOT_CUTOFF, (float)(light.fallOff / 2));
2762 glLightf(GL_LIGHT0 + id, GL_SPOT_EXPONENT, exponent);
2768 Vector3Df vector { 0,0,-1 };
2769 Vector3Df direction;
2772 mat.RotationQuaternion(light.orientation);
2773 direction.MultMatrix(vector, mat);
2775 position[0] = direction.x;
2776 position[1] = direction.y;
2777 position[2] = direction.z;
2779 glLightfv(GL_LIGHT0 + id, GL_POSITION, position);
2783 glDisable(GL_LIGHT0 + id);
2787 void SetCamera(Display display, Surface surface, Camera camera)
2789 OGLDisplay oglDisplay = display.driverData;
2790 //Logf("SetCamera\n");
2792 if(surface && camera)
2794 int left = surface.box.left + surface.offset.x;
2795 int top = surface.box.top + surface.offset.y;
2796 int right = surface.box.right + surface.offset.x;
2797 int bottom = surface.box.bottom + surface.offset.y;
2798 float origX = surface.offset.x + camera.origin.x;
2799 float origY = surface.offset.y + camera.origin.y;
2801 int y = display.height - bottom - 1;
2802 int w = right - left + 1;
2803 int h = bottom - top + 1;
2806 glViewport(x, y, w, h);
2808 // *** Projection Matrix ***
2809 glMatrixMode(MatrixMode::projection);
2810 if(!display.display3D.camera)
2813 if(display.display3D.collectingHits)
2815 float pickX = display.display3D.pickX + surface.offset.x;
2816 float pickY = display.height - (display.display3D.pickY + surface.offset.y) - 1;
2820 w / display.display3D.pickWidth, 0, 0, 0,
2821 0, h / display.display3D.pickHeight, 0, 0,
2823 (w + 2.0f * (x - pickX)) / display.display3D.pickWidth,
2824 (h + 2.0f * (y - pickY)) / display.display3D.pickHeight, 0, 1
2827 glLoadMatrixd(pickMatrix.array);
2832 (left - origX) * camera.zMin / camera.focalX,
2833 (right - origX) * camera.zMin / camera.focalX,
2834 (bottom - origY) * camera.zMin / camera.focalY,
2835 (top - origY) * camera.zMin / camera.focalY,
2836 camera.zMin, camera.zMax);
2838 glDisable(GL_BLEND);
2840 // *** Z Inverted Identity Matrix ***
2841 glMatrixMode(MatrixMode::modelView);
2842 if(!display.display3D.camera)
2847 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
2849 // *** View Matrix ***
2850 glMultMatrixd(camera.viewMatrix.array);
2855 glEnable(GL_DEPTH_TEST);
2856 #if !defined(EM_MODE)
2857 glEnable(GL_LIGHTING);
2858 glShadeModel(GL_SMOOTH);
2860 glDepthMask((byte)bool::true);
2861 oglDisplay.depthWrite = true;
2863 glEnable(GL_MULTISAMPLE_ARB);
2865 else if(surface && display.display3D.camera)
2867 oglDisplay.depthWrite = false;
2868 glViewport(0,0,display.width,display.height);
2870 glDisable(GL_CULL_FACE);
2871 glDisable(GL_DEPTH_TEST);
2872 glDisable(GL_LIGHTING);
2874 glDisable(GL_TEXTURE_2D);
2875 #if !defined(EM_MODE)
2876 glShadeModel(GL_FLAT);
2879 glDisable(GL_MULTISAMPLE_ARB);
2881 // *** Restore 2D MODELVIEW Matrix ***
2884 // *** Restore 2D PROJECTION Matrix ***
2885 glMatrixMode(MatrixMode::projection);
2891 void ApplyMaterial(Display display, Material material, Mesh mesh)
2893 //Logf("ApplyMaterial\n");
2896 if(material.flags.doubleSided)
2898 #if !defined(EM_MODE)
2899 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
2901 glDisable(GL_CULL_FACE);
2905 #if !defined(EM_MODE)
2906 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
2908 glEnable(GL_CULL_FACE);
2912 if(material.flags.noFog)
2918 if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
2920 Bitmap map = material.baseMap;
2921 glEnable(GL_TEXTURE_2D);
2922 glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
2924 glMatrixMode(GL_TEXTURE);
2926 if(material.uScale && material.vScale)
2927 glScalef(material.uScale, material.vScale, 1);
2928 glMatrixMode(MatrixMode::modelView);
2930 if(material.flags.tile)
2932 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2933 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2937 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2938 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2942 glDisable(GL_TEXTURE_2D);
2945 glColor4f(material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity);
2947 if(mesh.flags.colors)
2949 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
2950 glEnable(GL_COLOR_MATERIAL);
2954 glDisable(GL_COLOR_MATERIAL);
2956 float color[4] = { material.diffuse.r, material.diffuse.g, material.diffuse.b, material.opacity };
2957 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
2960 float color[4] = { material.ambient.r, material.ambient.g, material.ambient.b, 0 };
2961 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
2965 float color[4] = { material.specular.r, material.specular.g, material.specular.b, 0 };
2966 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color);
2969 float color[4] = { material.emissive.r, material.emissive.g, material.emissive.b, 0 };
2970 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
2973 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &material.power);
2977 void FreeMesh(DisplaySystem displaySystem, Mesh mesh)
2979 OGLMesh oglMesh = mesh.data;
2982 if(!mesh.flags.vertices)
2984 oglMesh.vertices.free();
2985 delete mesh.vertices;
2987 if(!mesh.flags.normals)
2989 oglMesh.normals.free();
2990 delete mesh.normals;
2992 if(!mesh.flags.texCoords1)
2994 oglMesh.texCoords.free();
2995 delete mesh.texCoords;
2997 if(!mesh.flags.texCoords2)
2999 oglMesh.texCoords2.free();
3000 // delete mesh.texCoords2;
3002 if(!mesh.flags.colors)
3004 oglMesh.colors.free();
3015 bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
3017 bool result = false;
3020 mesh.data = OGLMesh { };
3023 if(mesh.nVertices == nVertices)
3025 // Same number of vertices, adding features (Leaves the other features pointers alone)
3026 if(mesh.flags != flags)
3028 if(!mesh.flags.vertices && flags.vertices)
3030 if(flags.doubleVertices)
3032 mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
3035 mesh.vertices = new Vector3Df[nVertices];
3037 if(!mesh.flags.normals && flags.normals)
3039 if(flags.doubleNormals)
3041 mesh.normals = (Vector3Df *)new Vector3D[nVertices];
3044 mesh.normals = new Vector3Df[nVertices];
3046 if(!mesh.flags.texCoords1 && flags.texCoords1)
3048 mesh.texCoords = new Pointf[nVertices];
3050 if(!mesh.flags.colors && flags.colors)
3052 mesh.colors = new ColorRGBAf[nVertices];
3058 // New number of vertices, reallocate all current and new features
3059 flags |= mesh.flags;
3062 if(flags.doubleVertices)
3064 mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
3067 mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
3071 if(flags.doubleNormals)
3073 mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
3076 mesh.normals = renew mesh.normals Vector3Df[nVertices];
3078 if(flags.texCoords1)
3080 mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
3084 mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
3092 void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3094 OGLMesh oglMesh = mesh.data;
3095 if(!flags) flags = mesh.flags;
3100 oglMesh.vertices.upload(
3101 mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices); //, GL_STATIC_DRAW_ARB );
3104 oglMesh.normals.upload(
3105 mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals); //, GL_STATIC_DRAW_ARB );
3107 if(flags.texCoords1)
3108 oglMesh.texCoords.upload(
3109 mesh.nVertices * sizeof(Pointf), mesh.texCoords); //, GL_STATIC_DRAW_ARB );
3112 oglMesh.colors.upload(
3113 mesh.nVertices * sizeof(ColorRGBAf), mesh.colors); //, GL_STATIC_DRAW_ARB );
3117 bool LockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
3124 void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3128 oglIndices.buffer.free();
3129 delete oglIndices.indices;
3134 void * AllocateIndices(DisplaySystem displaySystem, int nIndices, bool indices32bit)
3136 OGLIndices oglIndices = OGLIndices { };
3139 oglIndices.indices = (void *)(indices32bit ? new uint32[nIndices] : new uint16[nIndices]);
3140 oglIndices.nIndices = nIndices;
3145 void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
3152 if(!oglIndices.buffer.buffer)
3153 glGenBuffers(1, &oglIndices.buffer.buffer);
3154 if(glabCurElementBuffer != oglIndices.buffer.buffer)
3155 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oglIndices.buffer.buffer);
3156 glimtkBufferDatai(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32) * nIndices, oglIndices.indices, GL_STATIC_DRAW_ARB);
3160 oglIndices.buffer.upload(
3161 nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
3162 oglIndices.indices); //GL_STATIC_DRAW_ARB);
3166 uint16 * LockIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
3169 return oglIndices.indices;
3172 void SelectMesh(Display display, Mesh mesh)
3174 //Logf("SelectMesh\n");
3176 #if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
3178 #if defined(__WIN32__)
3179 if(glUnlockArraysEXT)
3181 if(!vboAvailable && display.display3D.mesh)
3182 glUnlockArraysEXT();
3187 OGLMesh oglMesh = mesh.data;
3189 // *** Vertex Stream ***
3190 glEnableClientState(GL_VERTEX_ARRAY);
3191 if(!display.display3D.collectingHits && oglMesh)
3193 oglMesh.vertices.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, oglMesh.vertices.buffer ? null : (double *)mesh.vertices);
3195 // *** Normals Stream ***
3196 if(mesh.normals || mesh.flags.normals)
3198 glEnableClientState(GL_NORMAL_ARRAY);
3199 oglMesh.normals.use(normal, 3, GL_FLOAT, 0, oglMesh.normals.buffer ? null : mesh.normals);
3202 glDisableClientState(GL_NORMAL_ARRAY);
3204 // *** Texture Coordinates Stream ***
3205 if(mesh.texCoords || mesh.flags.texCoords1)
3207 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3208 oglMesh.texCoords.use(texCoord, 2, GL_FLOAT, 0, oglMesh.texCoords.buffer ? null : mesh.texCoords);
3211 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3213 // *** Color Stream ***
3214 if(mesh.colors || mesh.flags.colors)
3216 glEnableClientState(GL_COLOR_ARRAY);
3217 oglMesh.colors.use(color, 4, GL_FLOAT, 0, oglMesh.colors.buffer ? null : mesh.colors);
3220 glDisableClientState(GL_COLOR_ARRAY);
3224 noAB.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, (double *)mesh.vertices);
3225 if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
3227 glEnableClientState(GL_NORMAL_ARRAY);
3228 noAB.use(normal, 3, GL_FLOAT, 0, mesh.normals);
3231 glDisableClientState(GL_NORMAL_ARRAY);
3232 if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
3234 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3235 noAB.use(texCoord, 2, GL_FLOAT, 0, mesh.texCoords);
3238 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3239 if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
3241 glEnableClientState(GL_COLOR_ARRAY);
3242 noAB.use(color, 4, GL_FLOAT, 0, mesh.colors);
3245 glDisableClientState(GL_COLOR_ARRAY);
3248 #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
3250 #if defined(__WIN32__)
3254 glLockArraysEXT(0, mesh.nVertices);
3260 void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
3262 //Logf("DrawPrimitives\n");
3264 if(primitive->type.vertexRange)
3265 glDrawArrays(primitiveTypes[primitive->type.primitiveType], primitive->first, primitive->nVertices);
3268 // *** Hoping the data won't be uploaded at all (Won't really work if another group of the mesh is using the mesh ) ***
3269 // HACK TO SPEED THINGS UP...
3271 /*GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
3272 if(primitive->nIndices < (mesh.nVertices >> 2) && !primitive->type.indices32bit)
3275 glBegin(primitiveTypes[primitive->type.primitiveType]);
3278 OGLIndices oglIndices = primitive->data;
3279 MeshFeatures flags = mesh.flags;
3280 for(c = 0; c<primitive->nIndices; c++)
3282 uint16 index = ((uint16 *) oglIndices.indices)[c];
3283 if(flags.normals) glNormal3fv((float *)&mesh.normals[index]);
3284 if(flags.texCoords1) glTexCoord2fv((float *)&mesh.texCoords[index]);
3285 if(flags.colors) glColor4fv((float *)&mesh.colors[index]);
3286 glVertex3fv((float *)&mesh.vertices[index]);
3295 OGLIndices oglIndices = primitive->data;
3296 GLEAB eab = ((!display.display3D.collectingHits && oglIndices) ? oglIndices.buffer : noEAB);
3298 eab.draw(primitiveTypes[primitive->type.primitiveType], primitive->nIndices,
3299 primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT,
3300 eab.buffer ? 0 : (oglIndices ? oglIndices.indices : primitive->indices));
3301 GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
3306 void PushMatrix(Display display)
3311 void PopMatrix(Display display, bool setMatrix)
3316 void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
3318 Matrix matrix = transMatrix;
3319 Camera camera = useCamera ? display.display3D.camera : null;
3324 glScaled(1.0/nearPlane, 1.0/nearPlane, -1.0/nearPlane);
3329 matrix.m[3][0] - camera.cPosition.x,
3330 matrix.m[3][1] - camera.cPosition.y,
3331 matrix.m[3][2] - camera.cPosition.z);
3343 glMultMatrixd(matrix.array);
3348 public void UseSingleGLContext(bool useSingle)
3350 useSingleGLContext = useSingle;
3353 default dllexport void *
3354 #if defined(__WIN32__)
3355 __attribute__((stdcall))
3357 IS_GLGetContext(DisplaySystem displaySystem)
3361 #if defined(__WIN32__)
3362 OGLSystem system = displaySystem.driverData;
3364 #elif defined(__ANDROID__) || defined(__ODROID__)
3366 #elif defined(__EMSCRIPTEN__)
3368 OGLSystem system = displaySystem.driverData;
3369 return system.glContext;