ecere/gfx/drivers/OpenGL: Shaders improvements; GLCapabilities Tweaks
authorJerome St-Louis <jerome@ecere.com>
Sun, 10 Jul 2016 09:23:09 +0000 (05:23 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 28 Jul 2016 21:35:40 +0000 (17:35 -0400)
- Setting Normal, ModelView X Projection matrix and Model View Z column
  (Solves normalization with non-uniform scale, avoids multiplying ModelView X Projection for each vertex)
- Using glcaps* globals for GL capabilities to avoid invasive changes and pointer dereferencing throughout

12 files changed:
ecere/src/gfx/3D/Matrix.ec
ecere/src/gfx/Display.ec
ecere/src/gfx/drivers/OpenGLDisplayDriver.ec
ecere/src/gfx/drivers/gl3/fixed.frag
ecere/src/gfx/drivers/gl3/fixed.vertex
ecere/src/gfx/drivers/gl3/glHelpers.h [new file with mode: 0644]
ecere/src/gfx/drivers/gl3/glab.ec
ecere/src/gfx/drivers/gl3/immediate.ec
ecere/src/gfx/drivers/gl3/matrixStack.ec
ecere/src/gfx/drivers/gl3/shading.ec
ecere/src/gfx/drivers/gl3/wglDefs.h
ecere/src/gui/Window.ec

index 53a2ed6..ab6818b 100644 (file)
@@ -45,7 +45,7 @@ public union Matrix
 
    void Multiply(Matrix a, Matrix b)
    {
-#if 1 // defined(_GLES) || defined(SHADERS)
+#if 1
       // We need a full matrix multiplication for the Projection matrix
       m[0][0]=a.m[0][0]*b.m[0][0] + a.m[0][1]*b.m[1][0] + a.m[0][2]*b.m[2][0] + a.m[0][3]*b.m[3][0];
       m[0][1]=a.m[0][0]*b.m[0][1] + a.m[0][1]*b.m[1][1] + a.m[0][2]*b.m[2][1] + a.m[0][3]*b.m[3][1];
@@ -175,7 +175,7 @@ public union Matrix
    {
       double det = source.Determinant();
       // if(Abs(det) < 0.0005)
-      if(Abs(det) < 0.00000001)
+      if(Abs(det) < 0.00000000000001)
          Identity();
       else
       {
index 4d296e2..6062969 100644 (file)
@@ -43,6 +43,7 @@ public:
    bool nonPow2Textures :1;
    bool vertexBuffer    :1;
    bool frameBuffer     :1;
+   bool pointSize       :1;
 
    // To be able to disable these at runtime independently...
    bool immediate       :1;
@@ -1161,7 +1162,15 @@ public:
                value.shaders = true;
             if(!oglDisplay.originalCapabilities.shaders)
                value.fixedFunction = true;
+            // Disable things that don't work with shaders
+            if(value.shaders)
+            {
+               value.fixedFunction = false;
+               value.legacy = false;
+               value.immediate = false;
+            }
             oglDisplay.capabilities = oglDisplay.originalCapabilities & value;
+
             Lock(true);
             OpenGLDisplayDriver::initialDisplaySetup(this);
             Unlock();
index b2982eb..e1811ab 100644 (file)
@@ -1,50 +1,50 @@
-// #define DIAGNOSTICS
-
-namespace gfx::drivers;
-
-#if defined(__ANDROID__)
-#include <android/native_activity.h>
-#endif
+#if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
 
-#if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
-#     include "gl_compat_4_4.h"
+// #define DIAGNOSTICS
+#if defined(_DEBUG) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
+ #define GL_DEBUGGING
 #endif
 
-#if defined(__ANDROID__) || defined(__ODROID__)
-import "egl"
-#endif
+import "Display"
 
 import "glab"
 import "immediate"
 import "matrixStack"
 import "shading"
 
-#define GL_BGRA_EXT  0x80E1
-
-#if defined(__ANDROID__)
-#include <android/log.h>
-#define printf(...)  ((void)__android_log_print(ANDROID_LOG_VERBOSE, "ecere-app", __VA_ARGS__))
-#endif
+namespace gfx::drivers;
 
-void CheckGLErrors()
-{
-   int e, nCount = 0;
-   while((e = glGetError()) && nCount++ < 10)
-      printf("GL error %d!\n", e);
-}
+#include "glHelpers.h"
 
-// We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
-// #define USEPBUFFER
+// **********   GL PLATFORMS INCLUDES   **********
+// UNIX
 #if defined(__unix__) || defined(__APPLE__)
 
-   #if !defined(__MINGW32__)
-      #define GL_GLEXT_PROTOTYPES
-   #endif
+   // EGL
+   #if defined(__ANDROID__) || defined(__ODROID__)
+      import "egl"
 
-   #define pointer _pointer
+      #if defined(__ANDROID__)
+         #include <android/native_activity.h>
+         #include <android/log.h>
+         #define printf(...)  ((void)__android_log_print(ANDROID_LOG_VERBOSE, "ecere-app", __VA_ARGS__))
+      #endif
 
+   // Emscripten
+   #elif defined(__EMSCRIPTEN__)
+      #define property _property
+      #define uint _uint
 
-   #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
+      #include <emscripten/emscripten.h>
+      #include <emscripten/html5.h>
+
+      #undef property
+      #undef uint
+
+   // GLX
+   #elif !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
+      #define pointer _pointer
+      #define GL_GLEXT_PROTOTYPES
 
       #define property _property
       #define new _new
@@ -81,92 +81,41 @@ void CheckGLErrors()
       #undef new
       #undef property
       #undef class
+      #undef pointer
 
-   #endif
-
-#endif
-
-#if defined(__APPLE__)
-   #include <OpenGl/gl.h>
-#endif
-
-#if defined(__WIN32__) || defined(__unix__) || defined(__APPLE__)
-
-   #if defined(__WIN32__)
-      //#define WIN32_LEAN_AND_MEAN
-      #undef _WIN32_WINNT
-      #define _WIN32_WINNT 0x0502
-      #define String Sting_
-      #include <windows.h>
-      #undef String
-   #endif
-
-   #if defined(__ANDROID__) || defined(__ODROID__)
-      #if !defined(_GLES)
-         #define _GLES
+      #if !defined(__APPLE__)
+      GLAPI void APIENTRY glLockArraysEXT (GLint first, GLsizei count);
+      GLAPI void APIENTRY glUnlockArraysEXT (void);
       #endif
 
-      #define uint _uint
-      #define property _property
-      #define new _new
-      #define class _class
-      #define Window    X11Window
-      #define Cursor    X11Cursor
-      #define Font      X11Font
-      #define Display   X11Display
-      #define Time      X11Time
-      #define KeyCode   X11KeyCode
-      #define Picture   X11Picture
-      #define Bool      X11Bool
-
-      #include <GLES/gl.h>
-      #include <GLES/glext.h>
-
-      #undef Bool
-      #undef Picture
-      #undef Window
-      #undef Cursor
-      #undef Font
-      #undef Display
-      #undef Time
-      #undef KeyCode
-      #undef uint
-      #undef new
-      #undef property
-      #undef class
-
-   #elif defined(__EMSCRIPTEN__)
-#if !defined(_GLES2)
-      #define _GLES2
-#endif
-      // #define _GLES
+      import "XInterface"
 
-      #define property _property
-      #define uint _uint
-
-      #include <GLES2/gl2.h>
-
-      #include <emscripten/emscripten.h>
-      #include <emscripten/html5.h>
+      // We were using PBUFFER for alpha compositing on Linux before, but it does not seem to work, nor be required anymore.
+      // #define USEPBUFFER
 
-      #undef property
-      #undef uint
-
-   #else
-      #include <GL/gl.h>
    #endif
 
-   #undef pointer
-
-   import "Display"
+// Apple
+#elif defined(__APPLE__)
+   #include <OpenGl/gl.h>
 
-   #if defined(__unix__) || defined(__APPLE__)
+// WGL
+#elif defined(__WIN32__)
+   //#define WIN32_LEAN_AND_MEAN
+   #undef _WIN32_WINNT
+   #define _WIN32_WINNT 0x0502
+   #define String Sting_
+   #include <windows.h>
+   #undef String
 
-   #if !defined(__ANDROID__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
-   import "XInterface"
-   #endif
+   #include "wglDefs.h"
+#endif
 
-   #endif
+#if defined(__WIN32__)
+#elif !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
+default:
+private:
+#endif
 
 /*                                                            OpenGL Versions Features Quick Reference
 
@@ -211,212 +160,91 @@ void CheckGLErrors()
 // bool mapBuffer       :1; //   |      -      |      X      |     ~      |     X      |       X       |      X      |      ~      |     -      |     ~      |     -
 */
 
-// Compiled In Capabilities
-#define ENABLE_GL_SHADERS  (!defined(_GLES))
-#define ENABLE_GL_FFP      (!defined(_GLES2))
-#define ENABLE_GL_POINTER  (!defined(__EMSCRIPTEN__))
-#define ENABLE_GL_FBO      (!defined(__EMSCRIPTEN__))
-#define ENABLE_GL_LEGACY   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_INTDBL   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_MAPBUF   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_SELECT   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_COLORMAT (ENABLE_GL_FFP   && !defined(_GLES))
-
-#if ENABLE_GL_SHADERS && ENABLE_GL_FFP
-   #define GLEnableClientState            (shaders ? glEnableVertexAttribArray : glEnableClientState)
-   #define GLDisableClientState           (shaders ? glDisableVertexAttribArray : glDisableClientState)
-   #define VERTICES                       (shaders ? GLBufferContents::vertex : GL_VERTEX_ARRAY)
-   #define NORMALS                        (shaders ? GLBufferContents::normal : GL_NORMAL_ARRAY)
-   #define TEXTURECOORDS                  (shaders ? GLBufferContents::texCoord : GL_TEXTURE_COORD_ARRAY)
-   #define COLORS                         (shaders ? GLBufferContents::color : GL_COLOR_ARRAY)
-   #define GLVertexPointer(n, t, s, p)    (shaders ? glVertexAttribPointer(GLBufferContents::vertex,   n, t, GL_FALSE, s, p) : glVertexPointer(n, t, s, p))
-   #define GLTexCoordPointer(n, t, s, p)  (shaders ? glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p) : glTexCoordPointer(n, t, s, p))
-#elif ENABLE_GL_SHADERS
-   #define GLEnableClientState            glEnableVertexAttribArray
-   #define GLDisableClientState           glDisableVertexAttribArray
-   #define VERTICES                       GLBufferContents::vertex
-   #define NORMALS                        GLBufferContents::normal
-   #define TEXTURECOORDS                  GLBufferContents::texCoord
-   #define COLORS                         GLBufferContents::color
-   #define GLVertexPointer(n, t, s, p)    glVertexAttribPointer(GLBufferContents::vertex,   n, t, GL_FALSE, s, p)
-   #define GLTexCoordPointer(n, t, s, p)  glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p)
-#else
-   #define GLEnableClientState            glEnableClientState
-   #define GLDisableClientState           glDisableClientState
-   #define VERTICES                       GL_VERTEX_ARRAY
-   #define NORMALS                        GL_NORMAL_ARRAY
-   #define TEXTURECOORDS                  GL_TEXTURE_COORD_ARRAY
-   #define COLORS                         GL_COLOR_ARRAY
-   #define GLVertexPointer                glVertexPointer
-   #define GLTexCoordPointer              glTexCoordPointer
-#endif
-
-#define GL_ARRAY_BUFFER_ARB            0x8892
-#define GL_ELEMENT_ARRAY_BUFFER_ARB    0x8893
-#define GL_STATIC_DRAW_ARB             0x88E4
-#define GL_LIGHT_MODEL_COLOR_CONTROL   0x81F8
-#define GL_SEPARATE_SPECULAR_COLOR     0x81FA
-
-#define GL_MULTISAMPLE_ARB             0x809D
-
-#if defined(__WIN32__)
-   #include "wglDefs.h"
-
-   typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
-   typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
-
-   static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
-   static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
-
-   static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
-   static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
-   static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
-   static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
-   static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
-   static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
-   static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
-   static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
-   static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
-   static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = null;
-   static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = null;
-
-#elif !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
 default:
-   GLAPI void APIENTRY glLockArraysEXT (GLint first, GLsizei count);
-   GLAPI void APIENTRY glUnlockArraysEXT (void);
+// Capabilities Global set to capabilities of Display being rendered to
+GLCapabilities glcaps;
+// Requiring Graphics Reload:
+bool glcaps_nonPow2Textures, glcaps_vertexBuffer, glcaps_quads, glcaps_intAndDouble;
+// Might toggle without Reload:
+bool glcaps_shaders, glcaps_fixedFunction, glcaps_immediate, glcaps_legacy, glcaps_pointSize, glcaps_frameBuffer;
+// bool mapBuffer;
 private:
-#endif
 
+
+// **********   Errors and Debugging   **********
+/*
+void CheckGLErrors()
+{
+   int e, nCount = 0;
+   while((e = glGetError()) && nCount++ < 10)
+      printf("GL error %d!\n", e);
+}
+*/
+#ifdef GL_DEBUGGING
 #ifndef APIENTRY
    #define APIENTRY
 #endif
+static void APIENTRY openglCallbackFunction(GLenum source,
+                                           GLenum type,
+                                           GLuint id,
+                                           GLenum severity,
+                                           GLsizei length,
+                                           const GLchar* message,
+                                           const void* userParam)
+{
+   if(severity == GL_DEBUG_SEVERITY_NOTIFICATION)
+      return;
+   PrintLn("---------------------opengl-callback-start------------");
+   PrintLn("message: ", message);
+   PrintLn("type: ");
+   switch (type)
+   {
+      case GL_DEBUG_TYPE_ERROR: PrintLn("ERROR"); break;
+      case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: PrintLn("DEPRECATED_BEHAVIOR"); break;
+      case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: PrintLn("UNDEFINED_BEHAVIOR"); break;
+      case GL_DEBUG_TYPE_PORTABILITY: PrintLn("PORTABILITY"); break;
+      case GL_DEBUG_TYPE_PERFORMANCE: PrintLn("PERFORMANCE"); break;
+      case GL_DEBUG_TYPE_OTHER: PrintLn("OTHER"); break;
+   }
 
-#if defined(__ANDROID__) || defined(__ODROID__)
-   // Frame Buffer Extensions
-   #define GL_FRAMEBUFFER           GL_FRAMEBUFFER_OES
-   #define GL_RENDERBUFFER          GL_RENDERBUFFER_OES
-   #define GL_COLOR_ATTACHMENT0     GL_COLOR_ATTACHMENT0_OES
-   #define glBindFramebuffer        glBindFramebufferOES
-   #define glBindRenderbuffer       glBindRenderbufferOES
-   #define glFramebufferTexture2D   glFramebufferTexture2DOES
-   #define glGenFramebuffers        glGenFramebuffersOES
-   #define glGenRenderbuffers       glGenRenderbuffersOES
-   #define glDeleteFramebuffers     glDeleteFramebuffersOES
-   #define glDeleteRenderbuffers    glDeleteRenderbuffersOES
-
-   // TOFIX: Grab Screen and BlitDI/StretchDI will have wrong colors
-   #undef  GL_BGRA_EXT
-   #define GL_BGRA_EXT               GL_RGBA
-#endif
-
-#if !ENABLE_GL_INTDBL
-   #define GL_INT                                  0x1404
-   #define GL_UNSIGNED_INT                         0x1405
-   #define GL_DOUBLE                               0x140A
-#endif
+   PrintLn("id: ", id);
+   Print("severity: ");
+   switch (severity)
+   {
+      case GL_DEBUG_SEVERITY_LOW: PrintLn("LOW"); break;
+      case GL_DEBUG_SEVERITY_MEDIUM: PrintLn("MEDIUM"); break;
+      case GL_DEBUG_SEVERITY_HIGH: PrintLn("HIGH"); break;
+      default: PrintLn("(other)");
+   }
+   PrintLn("---------------------opengl-callback-end--------------");
+}
 
-#if ENABLE_GL_STIPPLES
-   #define GLLineStipple                     (stipples ? glLineStipple : glsupLineStipple)
-#else
-   #define GLLineStipple                     glsupLineStipple
-#endif
+static void setupDebugging()
+{
+   if(glDebugMessageCallback)
+   {
+      GLuint unusedIds = 0;
 
-#if ENABLE_GL_COLORMAT
-   #define GLColorMaterial(a,b)              glColorMaterial(a,b)
-#else
-   #define GLColorMaterial(a,b)
-#endif
+      glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
 
-#ifdef _GLES
-   #define GLLightModeli                     glsupLightModeli
-#else
-   #define GLLightModeli                     glLightModeli
+      glDebugMessageCallback(openglCallbackFunction, null);
+      glDebugMessageControl(GL_DONT_CARE,
+          GL_DONT_CARE,
+          GL_DONT_CARE,
+          0,
+          &unusedIds,
+          GL_TRUE);
+   }
+}
 #endif
 
-#if ENABLE_GL_LEGACY
-   #define GLRecti(x1, y1, x2, y2)           (immediate && !shaders ? glRecti(x1, y1, x2, y2) : glimtkRecti(capabilities, x1, y1, x2, y2))
-   #define GLBegin(m)                        (immediate && !shaders ? glBegin(m) : glimtkBegin(m))
-   #define GLTexCoord2i                      (immediate && !shaders ? glTexCoord2i : glimtkTexCoord2i)
-   #define GLVertex2i                        (immediate && !shaders ? glVertex2i : glimtkVertex2i)
-   #define GLTexCoord2d                      (immediate && !shaders ? glTexCoord2d : glimtkTexCoord2d)
-   #define GLVertex2d                        (immediate && !shaders ? glVertex2d : glimtkVertex2d)
-   #define GLTexCoord2f                      (immediate && !shaders ? glTexCoord2f : glimtkTexCoord2f)
-   #define GLVertex2f                        (immediate && !shaders ? glVertex2f : glimtkVertex2f)
-   #define GLEnd()                           (immediate && !shaders ? glEnd() : glimtkEnd(capabilities))
-   #define GLColor3f(a,b,c)                  (immediate && !shaders ? glColor3f(a,b,c) : glimtkColor3f(shaders, a,b,c))
-   #define GLColor4ub(a,b,c,d)               (immediate && !shaders ? glColor4ub(a,b,c,d) : glimtkColor4ub(shaders,a,b,c,d))
-   #define GLColor4f(a,b,c,d)                (immediate && !shaders ? glColor4f(a,b,c,d) : glimtkColor4f(shaders,a,b,c,d))
-   #define GLColor4fv(v)                     (immediate && !shaders ? glColor4fv(v) : glimtkColor4fv(shaders, v))
-   #define GLNormal3fv                       (immediate && !shaders ? glNormal3fv : glimtkNormal3fv)
-   #define GLNormal3f                        (immediate && !shaders ? glNormal3f : glimtkNormal3f)
-   #define GLTexCoord2fv                     (immediate && !shaders ? glTexCoord2fv : glimtkTexCoord2fv)
-   #define GLVertex3d                        (immediate && !shaders ? glVertex3d : glimtkVertex3d)
-   #define GLVertex3dv                       (immediate && !shaders ? glVertex3dv : glimtkVertex3dv)
-   #define GLVertex3f                        (immediate && !shaders ? glVertex3f : glimtkVertex3f)
-   #define GLVertex3fv                       (immediate && !shaders ? glVertex3fv : glimtkVertex3fv)
-
-   #define GLLoadMatrixd(m)                  (fixedFunction && !shaders ? glLoadMatrixd(m) : glmsLoadMatrixd(shaders, m))
-   #define GLMultMatrixd(m)                  (fixedFunction && !shaders ? glMultMatrixd(m) : glmsMultMatrixd(shaders, m))
-   #define GLFrustum(a,b,c,d,e,f)            (fixedFunction && !shaders ? glFrustum(a,b,c,d,e,f) : glmsFrustum(shaders, a,b,c,d,e,f))
-   #define GLOrtho(a,b,c,d,e,f)              (fixedFunction && !shaders ? glOrtho(a,b,c,d,e,f) : glmsOrtho(shaders, a,b,c,d,e,f))
-   #define GLScaled(x, y, z)                 (fixedFunction && !shaders ? glScaled(x, y, z) : glmsScaled(shaders, x,y,z))
-   #define GLScalef(x, y, z)                 (fixedFunction && !shaders ? glScalef(x, y, z) : glmsScaled(shaders, x,y,z))
-   #define GLTranslated(x, y, z)             (fixedFunction && !shaders ? glTranslated(x,y,z) : glmsTranslated(shaders, x,y,z))
-   #define GLRotated(a, x, y, z)             (fixedFunction && !shaders ? glRotated : glmsRotated)
-   #define GLMatrixMode(m)                   (fixedFunction && !shaders ? glMatrixMode(m) : glmsMatrixMode(shaders, m))
-   #define GLLoadIdentity()                  (fixedFunction && !shaders ? glLoadIdentity() : glmsLoadIdentity(shaders))
-   #define GLPushMatrix                      (fixedFunction && !shaders ? glPushMatrix : glmsPushMatrix)
-   #define GLPopMatrix()                     (fixedFunction && !shaders ? glPopMatrix() : glmsPopMatrix(shaders))
-#else
-   #define GLRecti(x1, y1, x2, y2)           glimtkRecti(capabilities, x1, y1, x2, y2)
-   #define GLBegin(m)                        glimtkBegin(m)
-   #define GLTexCoord2i                      glimtkTexCoord2i
-   #define GLVertex2i                        glimtkVertex2i
-   #define GLTexCoord2d                      glimtkTexCoord2d
-   #define GLVertex2d                        glimtkVertex2d
-   #define GLTexCoord2f                      glimtkTexCoord2f
-   #define GLVertex2f                        glimtkVertex2f
-   #define GLEnd()                           glimtkEnd(capabilities)
-   #define GLColor3f(a,b,c)                  glimtkColor3f(shaders, a,b,c)
-   #define GLColor4ub(a,b,c,d)               glimtkColor4ub(shaders,a,b,c,d)
-   #define GLColor4f(a,b,c,d)                glimtkColor4f(shaders,a,b,c,d)
-   #define GLColor4fv(v)                     glimtkColor4fv(shaders, v)
-   #define GLNormal3fv                       glimtkNormal3fv
-   #define GLNormal3f                        glimtkNormal3f
-   #define GLTexCoord2fv                     glimtkTexCoord2fv
-   #define GLVertex3d                        glimtkVertex3d
-   #define GLVertex3dv                       glimtkVertex3dv
-   #define GLVertex3f                        glimtkVertex3f
-   #define GLVertex3fv                       glimtkVertex3fv
-
-   #define GLLoadMatrixd(m)                  glmsLoadMatrixd(shaders, m)
-   #define GLMultMatrixd(m)                  glmsMultMatrixd(shaders, m)
-   #define GLFrustum(a,b,c,d,e,f)            glmsFrustum(shaders, a,b,c,d,e,f)
-   #define GLOrtho(a,b,c,d,e,f)              glmsOrtho(shaders, a,b,c,d,e,f)
-   #define GLScaled(a,b,c)                   glmsScaled(shaders, a,b,c)
-   #define GLScalef(a,b,c)                   glmsScaled(shaders, a,b,c)
-   #define GLTranslated(a,b,c)               glmsTranslated(shaders, a,b,c)
-   #define GLRotated(a, x, y, z)             glmsRotated(shaders, a, x, y, z)
-   #define GLMatrixMode(m)                   glmsMatrixMode(shaders, m)
-   #define GLLoadIdentity()                  glmsLoadIdentity(shaders)
-   #define GLPushMatrix                      glmsPushMatrix
-   #define GLPopMatrix()                     glmsPopMatrix(shaders)
-#endif
-
-#define GLLoadMatrix GLLoadMatrixd
-#define GLMultMatrix GLMultMatrixd
-#define GLGetMatrix  GLGetDoublev
-#define GLTranslate  GLTranslated
-#define GLScale      GLScaled
 
 static GLuint stippleTexture;
 static bool stippleEnabled;
-                              // TOCHECK: Do we really need to pass shaders?
-public void glsupLineStipple( bool shaders, int i, unsigned short j )
+
+                              // TOCHECK: Do we really need to pass glcaps_shaders?
+public void glsupLineStipple( int i, unsigned short j )
 {
-#if ENABLE_GL_LEGACY
-   bool fixedFunction = false;
-#endif
    uint texture[1*16];
    int x;
    for(x = 0; x < 16; x++)
@@ -430,7 +258,7 @@ public void glsupLineStipple( bool shaders, int i, unsigned short j )
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
 
    // TOOD: Special shading code for stippling?
-   GLSetupTexturing(shaders, true);
+   GLSetupTexturing(true);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@@ -443,13 +271,16 @@ public void glsupLineStipple( bool shaders, int i, unsigned short j )
    GLMatrixMode(MatrixMode::projection);
 }
 
-#ifdef _GLES
+   // Exported to build _GLES version...
    public void glsupLightModeli( unsigned int pname, int param )
    {
+#if ENABLE_GL_FFP
       if(pname == GL_LIGHT_MODEL_TWO_SIDE)
          glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, param);
+#endif
    }
 
+#ifdef _GLES
    void glFogi( unsigned int pname, int param ) { }
    void glPolygonMode( unsigned int i, unsigned int j ) { }
    void glBlendFuncSeparate(int a, int b, int c, int d)
@@ -473,7 +304,7 @@ void glPopName() { }
 #endif
 
 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
-static inline uint getPrimitiveType(bool quadsSupport, RenderPrimitiveType type)
+static inline uint getPrimitiveType(RenderPrimitiveType type)
 {
    static int primitiveTypes[RenderPrimitiveType] =
    {
@@ -483,47 +314,47 @@ static inline uint getPrimitiveType(bool quadsSupport, RenderPrimitiveType type)
       GL_LINE_STRIP
    };
    // NOTE: This will only work for single quads
-   return (type == quads && !quadsSupport) ? GL_TRIANGLE_FAN : primitiveTypes[type];
+   return (type == quads && !glcaps_quads) ? GL_TRIANGLE_FAN : primitiveTypes[type];
 }
 
-public void GLSetupTexturing(bool shaders, bool enable)
+public void GLSetupTexturing(bool enable)
 {
 #if ENABLE_GL_SHADERS
-   if(shaders)
+   if(glcaps_shaders)
       shader_texturing(enable);
 #endif
 
 #if ENABLE_GL_FFP
-   if(!shaders)
+   if(!glcaps_shaders)
       (enable ? glEnable : glDisable)(GL_TEXTURE_2D);
 #endif
 }
 
-public void GLSetupFog(bool shaders, bool enable)
+public void GLSetupFog(bool enable)
 {
 #if ENABLE_GL_SHADERS
-   if(shaders)
+   if(glcaps_shaders)
       shader_fog(enable);
 #endif
 
 #if ENABLE_GL_FFP
-   if(!shaders)
+   if(!glcaps_shaders)
       (enable ? glEnable : glDisable)(GL_FOG);
 #endif
 }
 
 bool lightingEnabled;
 
-public void GLSetupLighting(bool shaders, bool enable)
+public void GLSetupLighting(bool enable)
 {
    lightingEnabled = enable;
 #if ENABLE_GL_SHADERS
-   if(shaders)
+   if(glcaps_shaders)
       shader_lighting(enable);
 #endif
 
 #if ENABLE_GL_FFP
-   if(!shaders)
+   if(!glcaps_shaders)
       (enable ? glEnable : glDisable)(GL_LIGHTING);
 #endif
 }
@@ -574,47 +405,6 @@ class OGLDisplay : struct
    int x, y;
 };
 
-#if defined(_DEBUG) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
-// #define GL_DEBUGGING
-#endif
-
-#ifdef GL_DEBUGGING
-static void APIENTRY openglCallbackFunction(GLenum source,
-                                           GLenum type,
-                                           GLuint id,
-                                           GLenum severity,
-                                           GLsizei length,
-                                           const GLchar* message,
-                                           const void* userParam)
-{
-   if(severity == GL_DEBUG_SEVERITY_NOTIFICATION)
-      return;
-   PrintLn("---------------------opengl-callback-start------------");
-   PrintLn("message: ", message);
-   PrintLn("type: ");
-   switch (type)
-   {
-      case GL_DEBUG_TYPE_ERROR: PrintLn("ERROR"); break;
-      case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: PrintLn("DEPRECATED_BEHAVIOR"); break;
-      case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: PrintLn("UNDEFINED_BEHAVIOR"); break;
-      case GL_DEBUG_TYPE_PORTABILITY: PrintLn("PORTABILITY"); break;
-      case GL_DEBUG_TYPE_PERFORMANCE: PrintLn("PERFORMANCE"); break;
-      case GL_DEBUG_TYPE_OTHER: PrintLn("OTHER"); break;
-   }
-
-   PrintLn("id: ", id);
-   Print("severity: ");
-   switch (severity)
-   {
-      case GL_DEBUG_SEVERITY_LOW: PrintLn("LOW"); break;
-      case GL_DEBUG_SEVERITY_MEDIUM: PrintLn("MEDIUM"); break;
-      case GL_DEBUG_SEVERITY_HIGH: PrintLn("HIGH"); break;
-      default: PrintLn("(other)");
-   }
-   PrintLn("---------------------opengl-callback-end--------------");
-}
-#endif
-
 class OGLSystem : struct
 {
    int maxTextureSize;
@@ -670,26 +460,6 @@ class OGLIndices : struct
 int current;
 void * previous;
 
-#ifdef GL_DEBUGGING
-static void setupDebugging()
-{
-   if(glDebugMessageCallback)
-   {
-      GLuint unusedIds = 0;
-
-      glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
-
-      glDebugMessageCallback(openglCallbackFunction, null);
-      glDebugMessageControl(GL_DONT_CARE,
-          GL_DONT_CARE,
-          GL_DONT_CARE,
-          0,
-          &unusedIds,
-          GL_TRUE);
-   }
-}
-#endif
-
 #if defined(__WIN32__)
 static HGLRC winCreateContext(HDC hdc, int * contextVersion, bool * isCompatible)
 {
@@ -719,7 +489,7 @@ static HGLRC winCreateContext(HDC hdc, int * contextVersion, bool * isCompatible
          #ifdef _DEBUG
                   WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
          #endif
-                  WGL_CONTEXT_PROFILE_MASK_ARB, coreNotion ? (tryingCompat ? WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB : WGL_CONTEXT_CORE_PROFILE_BIT_ARB) : 0,
+                  coreNotion ? WGL_CONTEXT_PROFILE_MASK_ARB : 0, coreNotion ? (tryingCompat ? WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB : WGL_CONTEXT_CORE_PROFILE_BIT_ARB) : 0,
                   0,0
                };
                result = wglCreateContextAttribsARB(hdc, null, attribs);
@@ -894,14 +664,15 @@ class OpenGLDisplayDriver : DisplayDriver
       glGetIntegerv(GL_MAX_TEXTURE_SIZE, &oglSystem.maxTextureSize);
 
 #if defined(_GLES)
-      capabilities = { fixedFunction = true, vertexBuffer = true, frameBuffer = extensions && strstr(extensions, "GL_OES_framebuffer_object") };
+      capabilities = { fixedFunction = true, vertexBuffer = true, pointSize = true, frameBuffer = extensions && strstr(extensions, "GL_OES_framebuffer_object") };
 #elif defined(_GLES2)
-      capabilities = { shaders = true, vertexBuffer = true, frameBuffer = true };
+      capabilities = { glcaps_shaders = true, vertexBuffer = true, pointSize = true, frameBuffer = true };
 #else
       capabilities =
       {
          nonPow2Textures = extensions && strstr(extensions, "GL_ARB_texture_non_power_of_two");
          intAndDouble = true;
+         pointSize = true;
 #if ENABLE_GL_LEGACY
          legacy         = glBegin != null;
          immediate      = glBegin != null;
@@ -931,16 +702,13 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       bool result = false;
       OGLSystem oglSystem = displaySystem.driverData = OGLSystem { };
-#if defined(__ANDROID__) || defined(__ODROID__)
-      bool shaders = false;
-#endif
 
 #ifdef _GLES
-      oglSystem.capabilities = { fixedFunction = true, vertexBuffer = true, frameBuffer = true };
+      oglSystem.capabilities = { fixedFunction = true, vertexBuffer = true, frameBuffer = true, pointSize = true };
 #elif defined(_GLES2)
-      oglSystem.capabilities = { shaders = true, vertexBuffer = true, frameBuffer = true };
+      oglSystem.capabilities = { shaders = true, vertexBuffer = true, frameBuffer = true, pointSize = true };
 #else
-      oglSystem.capabilities = { shaders = true, fixedFunction = true, immediate = true, legacy = true, quads = true, intAndDouble = true, vertexBuffer = true, frameBuffer = true, nonPow2Textures = true };
+      oglSystem.capabilities = { shaders = true, fixedFunction = true, immediate = true, legacy = true, pointSize = true, quads = true, intAndDouble = true, vertexBuffer = true, frameBuffer = true, nonPow2Textures = true };
 #endif
 
 #ifdef DIAGNOSTICS
@@ -1094,14 +862,16 @@ class OpenGLDisplayDriver : DisplayDriver
          GLMatrixMode(GL_PROJECTION);
          glShadeModel(GL_FLAT);
 
-         if(!shaders)
+#if !defined(_GLES)
+         if(!glcaps_shaders)
             GLLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
+#endif
          glFogi(GL_FOG_MODE, GL_EXP);
          glFogf(GL_FOG_DENSITY, 0);
          glEnable(GL_NORMALIZE);
          glDepthFunc(GL_LESS);
          glClearDepth(1.0);
-         glDisable(GL_MULTISAMPLE_ARB);
+         glDisable(GL_MULTISAMPLE);
 
          glViewport(0,0,eglWidth,eglHeight);
          GLLoadIdentity();
@@ -1243,14 +1013,14 @@ class OpenGLDisplayDriver : DisplayDriver
    /*static */bool ::initialDisplaySetup(Display display)
    {
       OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
+      OGLSystem oglSystem = display.displaySystem.driverData;
       bool result = true;
-      bool shaders = capabilities.shaders;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
+
+      oglSystem.capabilities = oglDisplay.capabilities;
+      SETCAPS(oglDisplay.capabilities);
+
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          loadShaders(display.displaySystem, "<:ecere>shaders/fixed.vertex", "<:ecere>shaders/fixed.frag");
 #if ENABLE_GL_LEGACY
       else
@@ -1279,6 +1049,9 @@ class OpenGLDisplayDriver : DisplayDriver
 #endif
       glEnable(GL_BLEND);
 
+      GLMatrixMode(MatrixMode::texture);
+      GLLoadIdentity();
+
       GLMatrixMode(MatrixMode::modelView);
       GLLoadIdentity(); // For setting up GLES stack
       GLScaled(1.0, 1.0, -1.0);
@@ -1290,14 +1063,16 @@ class OpenGLDisplayDriver : DisplayDriver
          GLOrtho(0,display.width,display.height,0,0.0,1.0);
 
 #if ENABLE_GL_FFP
-      if(!shaders)
+      if(!glcaps_shaders)
       {
          glShadeModel(GL_FLAT);
          /*
          #define GL_LIGHT_MODEL_LOCAL_VIEWER 0x0B51
          GLLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
          */
+#if !defined(_GLES)
          GLLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
+#endif
          glFogi(GL_FOG_MODE, GL_EXP);
          glFogf(GL_FOG_DENSITY, 0);
          glEnable(GL_NORMALIZE);
@@ -1305,7 +1080,10 @@ class OpenGLDisplayDriver : DisplayDriver
 #endif
       glDepthFunc(GL_LESS);
       glClearDepth(1.0);
-      glDisable(GL_MULTISAMPLE_ARB);
+#if !defined(__EMSCRIPTEN__)
+      glDisable(GL_MULTISAMPLE);
+#endif
+
 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
       display.ambient = Color { 50,50,50 };
 #endif
@@ -1421,12 +1199,23 @@ class OpenGLDisplayDriver : DisplayDriver
 
             oglDisplay.originalCapabilities = oglDisplay.capabilities;
 
-            // Re-enable shaders if no fixed function support
+            // Re-enable glcaps_shaders if no fixed function support
             if(!oglDisplay.capabilities.fixedFunction)
                capabilities.shaders = true;
-            // Re-enable fixed function if no shaders support
+            // Re-enable fixed function if no glcaps_shaders support
             if(!oglDisplay.capabilities.shaders)
+            {
                capabilities.fixedFunction = true;
+               capabilities.shaders = false;
+            }
+
+            // Disable things that don't work with glcaps_shaders
+            if(capabilities.shaders)
+            {
+               capabilities.fixedFunction = false;
+               capabilities.legacy = false;
+               capabilities.immediate = false;
+            }
 
             #if !ENABLE_GL_POINTER
             // Re-enable vertex buffer if no pointer support
@@ -1466,11 +1255,6 @@ class OpenGLDisplayDriver : DisplayDriver
    bool DisplaySize(Display display, int width, int height)
    {
       OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
       bool result = false;
 
 #if defined(__WIN32__) || defined(USEPBUFFER)
@@ -1810,6 +1594,8 @@ class OpenGLDisplayDriver : DisplayDriver
 #endif
          result = true;
 
+      SETCAPS(oglDisplay.capabilities);
+
       if(display.alphaBlend && result)
          initialDisplaySetup(display);
 
@@ -2200,15 +1986,10 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       bool result = false;
       OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
       OGLSurface oglSurface = surface.driverData = OGLSurface { };
-
       if(oglSurface)
       {
+         SETCAPS(oglDisplay.capabilities);
          if(displayWidth != display.width || displayHeight != display.height)
          {
             displayWidth = display.width;
@@ -2336,30 +2117,15 @@ class OpenGLDisplayDriver : DisplayDriver
    void PutPixel(Display display, Surface surface,int x,int y)
    {
       OGLSurface oglSurface = surface.driverData;
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-#endif
-      bool shaders = capabilities.shaders;
-
       GLColor4fv(oglSurface.foreground);
       GLBegin(GL_POINTS);
       // glVertex2i(x+surface.offset.x, y+surface.offset.y);
       GLVertex2f(x+surface.offset.x + 0.5f, y+surface.offset.y + 0.5f);
-
       GLEnd();
    }
 
    void DrawLine(Display display, Surface surface, int _x1, int _y1, int _x2, int _y2)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-#endif
-      bool shaders = capabilities.shaders;
-
       OGLSurface oglSurface = surface.driverData;
       float x1 = _x1, x2 = _x2, y1 = _y1, y2 = _y2;
       if(_x1 == _x2)
@@ -2406,12 +2172,6 @@ class OpenGLDisplayDriver : DisplayDriver
    void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
    {
       OGLSurface oglSurface = surface.driverData;
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-#endif
-      bool shaders = capabilities.shaders;
       x1 += surface.offset.x;
       y1 += surface.offset.y;
       x2 += surface.offset.x;
@@ -2462,12 +2222,6 @@ class OpenGLDisplayDriver : DisplayDriver
    void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
    {
       OGLSurface oglSurface = surface.driverData;
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-#endif
-      bool shaders = capabilities.shaders;
 
       GLColor4fv(oglSurface.background);
 
@@ -2507,18 +2261,11 @@ class OpenGLDisplayDriver : DisplayDriver
    void Blit(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h)
    {
       OGLSurface oglSurface = surface.driverData;
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
 
       if(!oglSurface.writingText)
       {
          // glTranslatef(-0.375f, -0.375f, 0.0f);
-         GLSetupTexturing(shaders, true);
+         GLSetupTexturing(true);
          GLColor4fv(oglSurface.bitmapMult);
       }
       else if(oglSurface.xOffset)
@@ -2564,7 +2311,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(!oglSurface.writingText)
       {
-         GLSetupTexturing(shaders, false);
+         GLSetupTexturing(false);
 
          //glTranslate(0.375, 0.375, 0.0);
       }
@@ -2575,16 +2322,10 @@ class OpenGLDisplayDriver : DisplayDriver
    void Stretch(Display display, Surface surface, Bitmap bitmap, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
    {
       OGLSurface oglSurface = surface.driverData;
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-#endif
-      bool shaders = capabilities.shaders;
 
       //glTranslate(-0.375, -0.375, 0.0);
 
-      GLSetupTexturing(shaders, true);
+      GLSetupTexturing(true);
       glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)bitmap.driverData);
 
       GLColor4fv(oglSurface.bitmapMult);
@@ -2622,7 +2363,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       GLEnd();
 
-      GLSetupTexturing(shaders, false);
+      GLSetupTexturing(false);
 
       //glTranslate(0.375, 0.375, 0.0);
    }
@@ -2715,15 +2456,9 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
       {
-#if ENABLE_GL_LEGACY
-         OGLDisplay oglDisplay = display.driverData;
-         GLCapabilities capabilities = oglDisplay.capabilities;
-         bool legacy = capabilities.legacy;
-#endif
-
          glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
 #if ENABLE_GL_LEGACY
-         if(legacy)
+         if(glcaps_legacy)
          {
             glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
             glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
@@ -2789,14 +2524,9 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(bitmap.pixelFormat == pixelFormat888 && !bitmap.paletteShades)
       {
-#if ENABLE_GL_LEGACY
-         OGLDisplay oglDisplay = display.driverData;
-         GLCapabilities capabilities = oglDisplay.capabilities;
-         bool legacy = capabilities.legacy;
-#endif
          glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
 #if ENABLE_GL_LEGACY
-         if(legacy)
+         if(glcaps_legacy)
          {
             glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap.stride);
             glPixelStorei(GL_UNPACK_SKIP_PIXELS, sx);
@@ -2841,12 +2571,6 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       OGLSurface oglSurface = surface.driverData;
       OGLSystem oglSystem = display.displaySystem.driverData;
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool immediate = capabilities.immediate;
-#endif
-      bool shaders = capabilities.shaders;
       oglSystem.loadingFont = true;
 
       //glTranslated(-0.375, -0.375, 0.0);
@@ -2861,7 +2585,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
       oglSurface.writingText = true;
 
-      GLSetupTexturing(shaders, true);
+      GLSetupTexturing(true);
 
       if(surface.font.outlineSize)
       {
@@ -2877,7 +2601,7 @@ class OpenGLDisplayDriver : DisplayDriver
       oglSurface.writingText = false;
       oglSystem.loadingFont = false;
 
-      GLSetupTexturing(shaders, false);
+      GLSetupTexturing(false);
 
       //glTranslated(0.375, 0.375, 0.0);
    }
@@ -2909,31 +2633,25 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void LineStipple(Display display, Surface surface, uint32 stipple)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool legacy = capabilities.legacy;
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
-
       if(stipple)
       {
 #if ENABLE_GL_LEGACY
-         if(legacy)
+         if(glcaps_legacy)
          {
-            stippleEnabled = true;
             glLineStipple(1, (uint16)stipple);
             glEnable(GL_LINE_STIPPLE);
          }
          else
 #endif
-            glsupLineStipple(shaders, 1, (uint16)stipple);
+         {
+            stippleEnabled = true;
+            glsupLineStipple(1, (uint16)stipple);
+         }
       }
       else
       {
 #if ENABLE_GL_LEGACY
-         if(legacy)
+         if(glcaps_legacy)
             glDisable(GL_LINE_STIPPLE);
          else
 #endif
@@ -2942,7 +2660,7 @@ class OpenGLDisplayDriver : DisplayDriver
             GLMatrixMode(GL_TEXTURE);
             GLLoadIdentity();
             GLMatrixMode(MatrixMode::projection);
-            GLSetupTexturing(shaders, false);   // TODO: Special shading code for stipple?
+            GLSetupTexturing(false);   // TODO: Special shading code for stipple?
          }
       }
    }
@@ -2951,25 +2669,19 @@ class OpenGLDisplayDriver : DisplayDriver
    void SetRenderState(Display display, RenderState state, uint value)
    {
       OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool legacy = capabilities.legacy;
-#endif
-      bool shaders = capabilities.shaders;
-
       switch(state)
       {
          case antiAlias:
 #ifndef __EMSCRIPTEN__
             if(value)
-               glEnable(GL_MULTISAMPLE_ARB);
+               glEnable(GL_MULTISAMPLE);
             else
-               glDisable(GL_MULTISAMPLE_ARB);
+               glDisable(GL_MULTISAMPLE);
 #endif
             break;
          case fillMode:
 #if ENABLE_GL_LEGACY
-            if(legacy)
+            if(glcaps_legacy)
                glPolygonMode(GL_FRONT_AND_BACK, ((FillModeValue)value == solid) ? GL_FILL : GL_LINE);
 #endif
             break;
@@ -2984,24 +2696,24 @@ class OpenGLDisplayDriver : DisplayDriver
          {
             float color[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
 #if ENABLE_GL_SHADERS
-            if(shaders)
+            if(glcaps_shaders)
                shader_fogColor(color[0], color[1], color[2]);
 #endif
 
 #if ENABLE_GL_FFP
-            if(!shaders)
+            if(!glcaps_shaders)
                glFogfv(GL_FOG_COLOR, (float *)&color);
 #endif
             break;
          }
          case fogDensity:
 #if ENABLE_GL_SHADERS
-            if(shaders)
+            if(glcaps_shaders)
                shader_fogDensity((float)(RenderStateFloat { ui = value }.f * nearPlane));
 #endif
 
 #if ENABLE_GL_FFP
-            if(!shaders)
+            if(!glcaps_shaders)
                glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
 #endif
             break;
@@ -3013,12 +2725,12 @@ class OpenGLDisplayDriver : DisplayDriver
          case ambient:
          {
 #if ENABLE_GL_SHADERS
-            if(shaders)
+            if(glcaps_shaders)
                shader_setGlobalAmbient(((Color)value).r / 255.0f, ((Color)value).g / 255.0f, ((Color)value).b / 255.0f, 1.0f);
 #endif
 
 #if ENABLE_GL_FFP
-            if(!shaders)
+            if(!glcaps_shaders)
             {
                float ambient[4] = { ((Color)value).r/255.0f, ((Color)value).g/255.0f, ((Color)value).b/255.0f, 1.0f };
                glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
@@ -3044,17 +2756,13 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void SetLight(Display display, int id, Light light)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-      bool shaders = capabilities.shaders;
-
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          shader_setLight(display, id, light);
 #endif
 
 #if ENABLE_GL_FFP
-      if(!shaders)
+      if(!glcaps_shaders)
       {
          if(light != null)
          {
@@ -3206,11 +2914,6 @@ class OpenGLDisplayDriver : DisplayDriver
    void SetCamera(Display display, Surface surface, Camera camera)
    {
       OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
 
       if(surface && camera)
       {
@@ -3277,16 +2980,16 @@ class OpenGLDisplayDriver : DisplayDriver
 
          glEnable(GL_DEPTH_TEST);
 
-         GLSetupLighting(shaders, true);
+         GLSetupLighting(true);
 #if ENABLE_GL_FFP
-         if(!shaders)
+         if(!glcaps_shaders)
             glShadeModel(GL_SMOOTH);
 #endif
          glDepthMask((byte)bool::true);
          oglDisplay.depthWrite = true;
 
 #ifndef __EMSCRIPTEN__
-         glEnable(GL_MULTISAMPLE_ARB);
+         glEnable(GL_MULTISAMPLE);
 #endif
       }
       else if(surface && display.display3D.camera)
@@ -3298,24 +3001,24 @@ class OpenGLDisplayDriver : DisplayDriver
          glDisable(GL_CULL_FACE);
          glDisable(GL_DEPTH_TEST);
 
-         GLSetupTexturing(shaders, false);
-         GLSetupLighting(shaders, false);
-         GLSetupFog(shaders, false);
+         GLSetupTexturing(false);
+         GLSetupLighting(false);
+         GLSetupFog(false);
 
          GLDisableClientState(COLORS);
 
 #if ENABLE_GL_SHADERS
-         if(shaders)
+         if(glcaps_shaders)
             shader_setPerVertexColor(false);
 #endif
 
 #if ENABLE_GL_FFP
-         if(!shaders)
+         if(!glcaps_shaders)
             glShadeModel(GL_FLAT);
 #endif
          glEnable(GL_BLEND);
 #if !defined(__EMSCRIPTEN__)
-         glDisable(GL_MULTISAMPLE_ARB);
+         glDisable(GL_MULTISAMPLE);
 #endif
 
          // *** Restore 2D MODELVIEW Matrix ***
@@ -3330,18 +3033,11 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void ApplyMaterial(Display display, Material material, Mesh mesh)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
-
       // Basic Properties
       if(material.flags.doubleSided)
       {
 #if ENABLE_GL_FFP
-         if(!shaders)
+         if(!glcaps_shaders)
             GLLightModeli(GL_LIGHT_MODEL_TWO_SIDE, !material.flags.singleSideLight);
 #endif
          glDisable(GL_CULL_FACE);
@@ -3349,20 +3045,20 @@ class OpenGLDisplayDriver : DisplayDriver
       else
       {
 #if ENABLE_GL_FFP
-         if(!shaders)
+         if(!glcaps_shaders)
             GLLightModeli(GL_LIGHT_MODEL_TWO_SIDE, bool::false);
 #endif
          glEnable(GL_CULL_FACE);
       }
 
       // Fog
-      GLSetupFog(shaders, !material.flags.noFog);
+      GLSetupFog(!material.flags.noFog);
 
       // Maps
       if(material.baseMap && (mesh.texCoords || mesh.flags.texCoords1))
       {
          Bitmap map = material.baseMap;
-         GLSetupTexturing(shaders, true);
+         GLSetupTexturing(true);
          glBindTexture(GL_TEXTURE_2D, (GLuint)(uintptr)map.driverData);
 
          GLMatrixMode(GL_TEXTURE);
@@ -3383,15 +3079,15 @@ class OpenGLDisplayDriver : DisplayDriver
          }
       }
       else
-         GLSetupTexturing(shaders, false);
+         GLSetupTexturing(false);
 
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          shader_setMaterial(material, mesh.flags.colors);
 #endif
 
 #if ENABLE_GL_FFP
-      if(!shaders)
+      if(!glcaps_shaders)
       {
          if(mesh.flags.colors)
          {
@@ -3429,32 +3125,31 @@ class OpenGLDisplayDriver : DisplayDriver
       OGLMesh oglMesh = mesh.data;
       if(oglMesh)
       {
-         OGLDisplay oglSystem = displaySystem.driverData;
-         GLCapabilities capabilities = oglSystem.capabilities;
-         bool vertexBuffer = capabilities.vertexBuffer;
+         OGLSystem oglSystem = displaySystem.driverData;
+         SETCAPS(oglSystem.capabilities);
          if(!mesh.flags.vertices)
          {
-            oglMesh.vertices.free(vertexBuffer);
+            oglMesh.vertices.free(glcaps_vertexBuffer);
             delete mesh.vertices;
          }
          if(!mesh.flags.normals)
          {
-            oglMesh.normals.free(vertexBuffer);
+            oglMesh.normals.free(glcaps_vertexBuffer);
             delete mesh.normals;
          }
          if(!mesh.flags.texCoords1)
          {
-            oglMesh.texCoords.free(vertexBuffer);
+            oglMesh.texCoords.free(glcaps_vertexBuffer);
             delete mesh.texCoords;
          }
          if(!mesh.flags.texCoords2)
          {
-            oglMesh.texCoords2.free(vertexBuffer);
+            oglMesh.texCoords2.free(glcaps_vertexBuffer);
             // delete mesh.texCoords2;
          }
          if(!mesh.flags.colors)
          {
-            oglMesh.colors.free(vertexBuffer);
+            oglMesh.colors.free(glcaps_vertexBuffer);
             delete mesh.colors;
          }
          if(!mesh.flags)
@@ -3545,27 +3240,26 @@ class OpenGLDisplayDriver : DisplayDriver
    void UnlockMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags)
    {
       OGLSystem oglSystem = displaySystem.driverData;
-      GLCapabilities capabilities = oglSystem.capabilities;
-      bool vertexBuffer = capabilities.vertexBuffer;
-      if(vertexBuffer)
+      SETCAPS(oglSystem.capabilities);
+      if(glcaps_vertexBuffer)
       {
          OGLMesh oglMesh = mesh.data;
          if(!flags) flags = mesh.flags;
          if(flags.vertices)
-            oglMesh.vertices.upload(vertexBuffer,
-               mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices); //, GL_STATIC_DRAW_ARB );
+            oglMesh.vertices.upload(
+               mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices);
 
          if(flags.normals)
-            oglMesh.normals.upload(vertexBuffer,
-               mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals); //, GL_STATIC_DRAW_ARB );
+            oglMesh.normals.upload(
+               mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals);
 
          if(flags.texCoords1)
-            oglMesh.texCoords.upload(vertexBuffer,
-               mesh.nVertices * sizeof(Pointf), mesh.texCoords); //, GL_STATIC_DRAW_ARB );
+            oglMesh.texCoords.upload(
+               mesh.nVertices * sizeof(Pointf), mesh.texCoords);
 
          if(flags.colors)
-            oglMesh.colors.upload(vertexBuffer,
-               mesh.nVertices * sizeof(ColorRGBAf), mesh.colors); //, GL_STATIC_DRAW_ARB );
+            oglMesh.colors.upload(
+               mesh.nVertices * sizeof(ColorRGBAf), mesh.colors);
       }
    }
 
@@ -3579,11 +3273,10 @@ class OpenGLDisplayDriver : DisplayDriver
    void FreeIndices(DisplaySystem displaySystem, OGLIndices oglIndices)
    {
       OGLSystem oglSystem = displaySystem.driverData;
-      GLCapabilities capabilities = oglSystem.capabilities;
-      bool vertexBuffer = capabilities.vertexBuffer;
+      SETCAPS(oglSystem.capabilities);
       if(oglIndices)
       {
-         oglIndices.buffer.free(vertexBuffer);
+         oglIndices.buffer.free(glcaps_vertexBuffer);
          delete oglIndices.indices;
          delete oglIndices;
       }
@@ -3603,9 +3296,8 @@ class OpenGLDisplayDriver : DisplayDriver
    void UnlockIndices(DisplaySystem displaySystem, OGLIndices oglIndices, bool indices32bit, int nIndices)
    {
       OGLSystem oglSystem = displaySystem.driverData;
-      GLCapabilities capabilities = oglSystem.capabilities;
-      bool vertexBuffer = capabilities.vertexBuffer;
-      if(vertexBuffer)
+      SETCAPS(oglSystem.capabilities);
+      if(glcaps_vertexBuffer)
       {
 #if !ENABLE_GL_INTDBL
          if(indices32bit)
@@ -3614,13 +3306,13 @@ class OpenGLDisplayDriver : DisplayDriver
                glGenBuffers(1, &oglIndices.buffer.buffer);
             if(glabCurElementBuffer != oglIndices.buffer.buffer)
                GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, oglIndices.buffer.buffer);
-            glimtkBufferDatai(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32) * nIndices, oglIndices.indices, GL_STATIC_DRAW_ARB);
+            glimtkBufferDatai(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32) * nIndices, oglIndices.indices, GL_STATIC_DRAW);
          }
          else
 #endif
-         oglIndices.buffer.upload(vertexBuffer,
+         oglIndices.buffer.upload(
             nIndices * (indices32bit ? sizeof(uint32) : sizeof(uint16)),
-            oglIndices.indices); //GL_STATIC_DRAW_ARB);
+            oglIndices.indices);
       }
    }
 
@@ -3632,21 +3324,12 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void SelectMesh(Display display, Mesh mesh)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_SHADERS && ENABLE_GL_FFP
-      bool shaders = capabilities.shaders;
-#endif
-
 #if !defined( __ANDROID__) && !defined(__APPLE__) && !defined(__ODROID__) && !defined(__EMSCRIPTEN__)
-      bool vertexBuffer = capabilities.vertexBuffer;
-
 #if defined(__WIN32__)
       if(glUnlockArraysEXT)
 #endif
-         if(!vertexBuffer && display.display3D.mesh)
+         if(!glcaps_vertexBuffer && display.display3D.mesh)
             glUnlockArraysEXT();
-
 #endif
       if(mesh)
       {
@@ -3656,13 +3339,13 @@ class OpenGLDisplayDriver : DisplayDriver
          GLEnableClientState(VERTICES);
          if(!display.display3D.collectingHits && oglMesh)
          {
-            oglMesh.vertices.use(capabilities, vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, oglMesh.vertices.buffer ? null : (double *)mesh.vertices);
+            oglMesh.vertices.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, oglMesh.vertices.buffer ? null : (double *)mesh.vertices);
 
             // *** Normals Stream ***
             if(mesh.normals || mesh.flags.normals)
             {
                GLEnableClientState(NORMALS);
-               oglMesh.normals.use(capabilities, normal, 3, GL_FLOAT, 0, oglMesh.normals.buffer ? null : mesh.normals);
+               oglMesh.normals.use(normal, 3, GL_FLOAT, 0, oglMesh.normals.buffer ? null : mesh.normals);
             }
             else
                GLDisableClientState(NORMALS);
@@ -3670,42 +3353,42 @@ class OpenGLDisplayDriver : DisplayDriver
             // *** Texture Coordinates Stream ***
             if(mesh.texCoords || mesh.flags.texCoords1)
             {
-               GLEnableClientState(TEXTURECOORDS);
-               oglMesh.texCoords.use(capabilities, texCoord, 2, GL_FLOAT, 0, oglMesh.texCoords.buffer ? null : mesh.texCoords);
+               GLEnableClientState(TEXCOORDS);
+               oglMesh.texCoords.use(texCoord, 2, GL_FLOAT, 0, oglMesh.texCoords.buffer ? null : mesh.texCoords);
             }
             else
-               GLDisableClientState(TEXTURECOORDS);
+               GLDisableClientState(TEXCOORDS);
 
             // *** Color Stream ***
             if(mesh.colors || mesh.flags.colors)
             {
                GLEnableClientState(COLORS);
-               oglMesh.colors.use(capabilities, color, 4, GL_FLOAT, 0, oglMesh.colors.buffer ? null : mesh.colors);
+               oglMesh.colors.use(color, 4, GL_FLOAT, 0, oglMesh.colors.buffer ? null : mesh.colors);
             }
             else
                GLDisableClientState(COLORS);
          }
          else
          {
-            noAB.use(capabilities, vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, (double *)mesh.vertices);
+            noAB.use(vertex, 3, (mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT), 0, (double *)mesh.vertices);
             if((mesh.normals || mesh.flags.normals) && !display.display3D.collectingHits)
             {
                GLEnableClientState(NORMALS);
-               noAB.use(capabilities, normal, 3, GL_FLOAT, 0, mesh.normals);
+               noAB.use(normal, 3, GL_FLOAT, 0, mesh.normals);
             }
             else
                GLDisableClientState(NORMALS);
             if((mesh.texCoords || mesh.flags.texCoords1) && !display.display3D.collectingHits)
             {
-               GLEnableClientState(TEXTURECOORDS);
-               noAB.use(capabilities, texCoord, 2, GL_FLOAT, 0, mesh.texCoords);
+               GLEnableClientState(TEXCOORDS);
+               noAB.use(texCoord, 2, GL_FLOAT, 0, mesh.texCoords);
             }
             else
-               GLDisableClientState(TEXTURECOORDS);
+               GLDisableClientState(TEXCOORDS);
             if((mesh.colors || mesh.flags.colors) && !display.display3D.collectingHits)
             {
                GLEnableClientState(COLORS);
-               noAB.use(capabilities, color, 4, GL_FLOAT, 0, mesh.colors);
+               noAB.use(color, 4, GL_FLOAT, 0, mesh.colors);
             }
             else
                GLDisableClientState(COLORS);
@@ -3716,7 +3399,7 @@ class OpenGLDisplayDriver : DisplayDriver
 #if defined(__WIN32__)
          if(glLockArraysEXT)
 #endif
-            if(!vertexBuffer)
+            if(!glcaps_vertexBuffer)
                glLockArraysEXT(0, mesh.nVertices);
 #endif
       }
@@ -3724,30 +3407,29 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void DrawPrimitives(Display display, PrimitiveSingle * primitive, Mesh mesh)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-      bool vertexBuffer = capabilities.vertexBuffer;
-
       if(primitive->type.vertexRange)
-         glDrawArrays(getPrimitiveType(oglDisplay.capabilities.quads, primitive->type.primitiveType), primitive->first, primitive->nVertices);
+      {
+         GLFlushMatrices();
+         glDrawArrays(getPrimitiveType(primitive->type.primitiveType), primitive->first, primitive->nVertices);
+      }
       else
       {
          OGLIndices oglIndices = primitive->data;
-         GLEAB eab = ((!display.display3D.collectingHits && oglIndices && vertexBuffer) ? oglIndices.buffer : noEAB);
+         GLEAB eab = ((!display.display3D.collectingHits && oglIndices && glcaps_vertexBuffer) ? oglIndices.buffer : noEAB);
 #if !ENABLE_GL_INTDBL
-         if(!vertexBuffer && primitive->type.indices32bit)
+         if(!glcaps_vertexBuffer && primitive->type.indices32bit)
          {
             uint16 * temp = new uint16[primitive->nIndices];
             uint32 * src = (uint32 *)(oglIndices ? oglIndices.indices : primitive->indices);
             int i;
             for(i = 0; i < primitive->nIndices; i++)
                temp[i] = (uint16)src[i];
-            eab.draw(vertexBuffer, getPrimitiveType(oglDisplay.capabilities.quads, primitive->type.primitiveType), primitive->nIndices, GL_UNSIGNED_SHORT, temp);
+            eab.draw(getPrimitiveType(primitive->type.primitiveType), primitive->nIndices, GL_UNSIGNED_SHORT, temp);
             delete temp;
          }
          else
 #endif
-            eab.draw(vertexBuffer, getPrimitiveType(oglDisplay.capabilities.quads, primitive->type.primitiveType), primitive->nIndices,
+            eab.draw(getPrimitiveType(primitive->type.primitiveType), primitive->nIndices,
                primitive->type.indices32bit ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT,
                eab.buffer ? 0 : (oglIndices ? oglIndices.indices : primitive->indices));
          GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
@@ -3756,34 +3438,16 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void PushMatrix(Display display)
    {
-#if ENABLE_GL_LEGACY
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-      bool fixedFunction = capabilities.fixedFunction;
-      bool shaders = capabilities.shaders;
-#endif
       GLPushMatrix();
    }
 
    void PopMatrix(Display display, bool setMatrix)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
       GLPopMatrix();
    }
 
    void SetTransform(Display display, Matrix transMatrix, bool viewSpace, bool useCamera)
    {
-      OGLDisplay oglDisplay = display.driverData;
-      GLCapabilities capabilities = oglDisplay.capabilities;
-#if ENABLE_GL_LEGACY
-      bool fixedFunction = capabilities.fixedFunction;
-#endif
-      bool shaders = capabilities.shaders;
       Matrix matrix = transMatrix;
       Camera camera = useCamera ? display.display3D.camera : null;
 
index eeb66a2..91404dc 100644 (file)
@@ -7,21 +7,16 @@ uniform bool texturingOn;
 uniform bool lightingOn;
 uniform mat4 texture_matrix;
 uniform bool matTwoSided;
-
-/*
-in vec2 fTexCoord;
-in vec4 fColor;
-in vec4 fColorBack;
-*/
+uniform int swizzleMode; // 0: off   1: alpha     2: red
 
 varying vec2 fTexCoord;
-varying  vec4 fColor;
-varying  vec4 fColorBack;
-
-// out vec4 fragColor;
+varying vec4 fColor;
+varying vec4 fColorBack;
 
 void main(void)
 {
-   vec4 c = (matTwoSided && lightingOn && !gl_FrontFacing) ? fColorBack : fColor;
-   gl_FragColor = texturingOn ? c * texture2D(diffuseTex, (vec4(fTexCoord, 0, 1) * texture_matrix).xy) : c;
+   vec4 c = (lightingOn && matTwoSided && !gl_FrontFacing) ? fColorBack : fColor;
+   vec4 texel = texture2D(diffuseTex, (vec4(fTexCoord, 0, 1) * texture_matrix).xy);
+   if(swizzleMode != 0) texel = (swizzleMode == 1) ? vec4(1,1,1,texel.a) : vec4(1,1,1,texel.r);
+   gl_FragColor = texturingOn ? c * texel : c;
 }
index ae45459..2a240eb 100644 (file)
@@ -1,7 +1,10 @@
 #version 100
 
-uniform mat4 projection_matrix;
-uniform mat4 modelview_matrix;
+//uniform mat4 projection_matrix;
+//uniform mat4 modelview_matrix;
+uniform vec4 mvmatrix_z;
+uniform mat4 proj_modelview_matrix;
+uniform mat4 normals_matrix;
 uniform vec4 current_color;
 
 // Lights
@@ -30,17 +33,6 @@ uniform bool fogOn;
 uniform float fogDensity;
 uniform vec3 fogColor;
 
-/*
-in vec2 texCoord;
-in vec3 vertex;
-in vec3 normal;
-in vec4 color;
-
-out vec2 fTexCoord;
-out vec4 fColor;
-out vec4 fColorBack;
-*/
-
 attribute vec2 texCoord;
 attribute vec3 vertex;
 attribute vec3 normal;
@@ -53,14 +45,20 @@ varying vec4 fColorBack;
 void main(void)
 {
    float f = 1.0;
-   vec3 n = normalize(mat3(modelview_matrix) * normal);
-   vec4 pos = modelview_matrix * vec4(vertex, 1.0);
+   vec3 n = normalize(mat3(normals_matrix) * normal);
+   float z = dot(mvmatrix_z, vec4(vertex, 1.0));
 
+   /*
+   vec4 pos = modelview_matrix * vec4(vertex, 1.0);
    gl_Position = projection_matrix * pos;
+   */
+
+   gl_Position = proj_modelview_matrix * vec4(vertex, 1.0);
+
    fTexCoord = texCoord;
 
    if(fogOn)
-      f = clamp(exp(fogDensity * pos.z), 0.0, 1.0);
+      f = clamp(exp(fogDensity * /*pos.*/ z), 0.0, 1.0);
 
    if(lightingOn)
    {
@@ -71,10 +69,12 @@ void main(void)
       vec3 c2 = vec3(0);
       // vec3 ecPosition3 = vec3(gl_Position) / gl_Position.w;
       vec3 eye = vec3(0.0, 0.0, 1.0);
+      vec3 diffuse = vec3(0), diffuse2 = vec3(0), specular = vec3(0), specular2 = vec3(0), ambient = vec3(0);
 
       fColor = vec4(0);
       if(matTwoSided)
          fColorBack = vec4(0);
+
       for(int i = 0; i < 8; i++)
          if(lightsOn[i])
          {
@@ -96,9 +96,9 @@ void main(void)
                }
             }
 
-            c += diffuseColor * min(1.0, max(0.0, d)) * lightsDiffuse[i];
-            c += matSpecular * pf * lightsSpecular[i];
-            c += matAmbient * lightsAmbient[i];
+            diffuse += min(1.0, max(0.0, d)) * lightsDiffuse[i];
+            specular += pf * lightsSpecular[i];
+            ambient += lightsAmbient[i];
 
             if(matTwoSided)
             {
@@ -114,14 +114,17 @@ void main(void)
                   }
                }
 
-               c2 += diffuseColor * min(1.0, max(0.0, d2)) * lightsDiffuse[i];
-               c2 += matSpecular * pf * lightsSpecular[i];
-               c2 += matAmbient * lightsAmbient[i];
+               diffuse2 += min(1.0, max(0.0, d2)) * lightsDiffuse[i];
+               specular2 += pf * lightsSpecular[i];
             }
          }
 
-      c += ambientColor * globalAmbient;
+      c += diffuseColor * diffuse;
+      c += matSpecular * specular;
+      c += ambientColor * (ambient + globalAmbient);
       c += matEmissive;
+      c = min(c, vec3(1,1,1));
+
       fColor = vec4(c, opacity);
 
       if(fogOn)
@@ -129,8 +132,11 @@ void main(void)
 
       if(matTwoSided)
       {
-         c2 += ambientColor * globalAmbient;
-         c2 += matEmissive;
+         c2 += diffuseColor * diffuse2;
+         c2 += matSpecular * specular2;
+         c2 += ambientColor * (ambient + globalAmbient);
+         c2 = min(c2, vec3(1,1,1));
+
          fColorBack = vec4(c2, opacity);
 
          if(fogOn)
diff --git a/ecere/src/gfx/drivers/gl3/glHelpers.h b/ecere/src/gfx/drivers/gl3/glHelpers.h
new file mode 100644 (file)
index 0000000..dc15c83
--- /dev/null
@@ -0,0 +1,255 @@
+#if defined(__EMSCRIPTEN__)
+   #if !defined(_GLES2)
+      #define _GLES2
+   #endif
+#elif defined(__ANDROID__) || defined(__ODROID__)
+   #ifndef _GLES
+      #define _GLES
+   #endif
+#endif
+
+#if defined(_GLES2)
+   #include <GLES2/gl2.h>
+#elif defined(_GLES)
+/*
+#define uint _uint
+#define property _property
+#define new _new
+#define class _class
+#define Window    X11Window
+#define Cursor    X11Cursor
+#define Font      X11Font
+#define Display   X11Display
+#define Time      X11Time
+#define KeyCode   X11KeyCode
+#define Picture   X11Picture
+#define Bool      X11Bool
+*/
+   #define GL_GLEXT_PROTOTYPES
+   #include <GLES/gl.h>
+   #include <GLES/glext.h>
+
+/*
+#undef Bool
+#undef Picture
+#undef Window
+#undef Cursor
+#undef Font
+#undef Display
+#undef Time
+#undef KeyCode
+#undef uint
+#undef new
+#undef property
+#undef class
+*/
+#else
+   #include <gl_compat_4_4.h>
+#endif
+
+#ifdef _GLES
+   // Frame Buffer Extensions
+   #define GL_FRAMEBUFFER           GL_FRAMEBUFFER_OES
+   #define GL_RENDERBUFFER          GL_RENDERBUFFER_OES
+   #define GL_COLOR_ATTACHMENT0     GL_COLOR_ATTACHMENT0_OES
+   #define glBindFramebuffer        glBindFramebufferOES
+   #define glBindRenderbuffer       glBindRenderbufferOES
+   #define glFramebufferTexture2D   glFramebufferTexture2DOES
+   #define glGenFramebuffers        glGenFramebuffersOES
+   #define glGenRenderbuffers       glGenRenderbuffersOES
+   #define glDeleteFramebuffers     glDeleteFramebuffersOES
+   #define glDeleteRenderbuffers    glDeleteRenderbuffersOES
+   #define glBlitFramebuffer        glBlitFramebufferOES // (a,b,c,d,,f,g,h,i,j)
+
+   // TOFIX: Grab Screen and BlitDI/StretchDI will have wrong colors
+   #undef GL_BGRA_EXT
+   #define GL_BGRA_EXT               GL_RGBA
+#else
+   #define GL_BGRA_EXT               0x80E1
+#endif
+
+// Compiled In Capabilities
+#define ENABLE_GL_SHADERS  (!defined(_GLES))
+#define ENABLE_GL_FFP      (!defined(_GLES2))
+#define ENABLE_GL_POINTER  (!defined(__EMSCRIPTEN__))
+#define ENABLE_GL_FBO      (!defined(__EMSCRIPTEN__))
+#define ENABLE_GL_LEGACY   (!defined(_GLES) && !defined(_GLES2))
+#define ENABLE_GL_INTDBL   (!defined(_GLES) && !defined(_GLES2))
+#define ENABLE_GL_MAPBUF   (!defined(_GLES) && !defined(_GLES2))
+#define ENABLE_GL_SELECT   (!defined(_GLES) && !defined(_GLES2))
+#define ENABLE_GL_COLORMAT (ENABLE_GL_FFP   && !defined(_GLES))
+
+#if ENABLE_GL_SHADERS && ENABLE_GL_FFP
+   #define GLEnableClientState            (glcaps_shaders ? glEnableVertexAttribArray : glEnableClientState)
+   #define GLDisableClientState           (glcaps_shaders ? glDisableVertexAttribArray : glDisableClientState)
+   #define VERTICES                       (glcaps_shaders ? GLBufferContents::vertex : GL_VERTEX_ARRAY)
+   #define NORMALS                        (glcaps_shaders ? GLBufferContents::normal : GL_NORMAL_ARRAY)
+   #define TEXCOORDS                      (glcaps_shaders ? GLBufferContents::texCoord : GL_TEXTURE_COORD_ARRAY)
+   #define COLORS                         (glcaps_shaders ? GLBufferContents::color : GL_COLOR_ARRAY)
+   #define GLVertexPointer(n, t, s, p)    (glcaps_shaders ? glVertexAttribPointer(GLBufferContents::vertex,   n, t, GL_FALSE, s, p) : glVertexPointer(n, t, s, p))
+   #define GLColorPointer(n, t, s, p)     (glcaps_shaders ? glVertexAttribPointer(GLBufferContents::color,    n, t, GL_FALSE, s, p) : glColorPointer(n, t, s, p))
+   #define GLTexCoordPointer(n, t, s, p)  (glcaps_shaders ? glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p) : glTexCoordPointer(n, t, s, p))
+#elif ENABLE_GL_SHADERS
+   #define GLEnableClientState            glEnableVertexAttribArray
+   #define GLDisableClientState           glDisableVertexAttribArray
+   #define VERTICES                       GLBufferContents::vertex
+   #define NORMALS                        GLBufferContents::normal
+   #define TEXCOORDS                      GLBufferContents::texCoord
+   #define COLORS                         GLBufferContents::color
+   #define GLVertexPointer(n, t, s, p)    glVertexAttribPointer(GLBufferContents::vertex,   n, t, GL_FALSE, s, p)
+   #define GLColorPointer(n, t, s, p)     glVertexAttribPointer(GLBufferContents::color,    n, t, GL_FALSE, s, p)
+   #define GLTexCoordPointer(n, t, s, p)  glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p)
+#else
+   #define GLEnableClientState            glEnableClientState
+   #define GLDisableClientState           glDisableClientState
+   #define VERTICES                       GL_VERTEX_ARRAY
+   #define NORMALS                        GL_NORMAL_ARRAY
+   #define TEXCOORDS                      GL_TEXTURE_COORD_ARRAY
+   #define COLORS                         GL_COLOR_ARRAY
+   #define GLVertexPointer                glVertexPointer
+   #define GLColorPointer                 glColorPointer
+   #define GLTexCoordPointer              glTexCoordPointer
+#endif
+
+#if ENABLE_GL_INTDBL && ENABLE_GL_SHADERS
+   #define GLLoadMatrixd(m)               (glcaps_fixedFunction ? glLoadMatrixd(m) : glmsLoadMatrixd(m))
+   #define GLMultMatrixd(m)               (glcaps_fixedFunction ? glMultMatrixd(m) : glmsMultMatrixd(m))
+   #define GLFrustum(a,b,c,d,e,f)         (glcaps_fixedFunction ? glFrustum(a,b,c,d,e,f) : glmsFrustum(a,b,c,d,e,f))
+   #define GLOrtho(a,b,c,d,e,f)           (glcaps_fixedFunction ? glOrtho(a,b,c,d,e,f) : glmsOrtho(a,b,c,d,e,f))
+   #define GLScaled(x, y, z)              (glcaps_fixedFunction ? glScaled(x, y, z) : glmsScaled(x,y,z))
+   #define GLScalef(x, y, z)              (glcaps_fixedFunction ? glScalef(x, y, z) : glmsScaled(x,y,z))
+   #define GLTranslated(x, y, z)          (glcaps_fixedFunction ? glTranslated(x,y,z) : glmsTranslated(x,y,z))
+   #define GLRotated(a, x, y, z)          (glcaps_fixedFunction ? glRotated(a, x,y,z) : glmsRotated(a,x,y,z))
+   #define GLMatrixMode(m)                (glcaps_fixedFunction ? glMatrixMode(m) : glmsMatrixMode(m))
+   #define GLLoadIdentity()               (glcaps_fixedFunction ? glLoadIdentity() : glmsLoadIdentity())
+   #define GLPushMatrix()                 (glcaps_fixedFunction ? glPushMatrix() : glmsPushMatrix())
+   #define GLPopMatrix()                  (glcaps_fixedFunction ? glPopMatrix() : glmsPopMatrix())
+   #define GLFlushMatrices()              (glcaps_fixedFunction ? (void)0 : glmsFlushMatrices())
+   #define GLLoadMatrix(m)                (glmsLoadMatrix(m), glmsFlushMatrices())
+#elif ENABLE_GL_INTDBL
+   #define GLLoadMatrixd                  glLoadMatrixd
+   #define GLMultMatrixd                  glMultMatrixd
+   #define GLFrustum                      glFrustum
+   #define GLOrtho                        glOrtho
+   #define GLScaled                       glScaled
+   #define GLScalef                       glScalef
+   #define GLTranslated                   glTranslated
+   #define GLRotated                      glRotated
+   #define GLMatrixMode                   glMatrixMode
+   #define GLLoadIdentity                 glLoadIdentity
+   #define GLPushMatrix                   glPushMatrix
+   #define GLPopMatrix                    glPopMatrix
+   #define GLFlushMatrices()              ((void)0)
+   #define GLLoadMatrix(m)                (glmsLoadMatrix(m), glmsFlushMatrices())
+#else
+   #define GLLoadMatrixd                  glmsLoadMatrixd
+   #define GLMultMatrixd                  glmsMultMatrixd
+   #define GLFrustum                      glmsFrustum
+   #define GLOrtho                        glmsOrtho
+   #define GLScaled                       glmsScaled
+   #define GLScalef                       glmsScaled
+   #define GLTranslated                   glmsTranslated
+   #define GLRotated                      glmsRotated
+   #define GLMatrixMode                   glmsMatrixMode
+   #define GLLoadIdentity                 glmsLoadIdentity
+   #define GLPushMatrix                   glmsPushMatrix
+   #define GLPopMatrix                    glmsPopMatrix
+   #define GLFlushMatrices()              glmsFlushMatrices()
+   #define GLLoadMatrix(m)                (glmsLoadMatrix(m), glmsFlushMatrices())
+#endif
+
+#if ENABLE_GL_LEGACY
+   #define GLRecti(x1, y1, x2, y2)           (glcaps_immediate ? glRecti(x1, y1, x2, y2) : glimtkRecti(x1, y1, x2, y2))
+   #define GLBegin(m)                        (glcaps_immediate ? glBegin(m) : glimtkBegin(m))
+   #define GLEnd()                           (glcaps_immediate ? glEnd() : glimtkEnd())
+   #define GLVertex2i(x,y)                   (glcaps_immediate ? glVertex2i(x,y) : glimtkVertex2i(x,y))
+   #define GLVertex2f(x,y)                   (glcaps_immediate ? glVertex2f(x,y) : glimtkVertex2f(x,y))
+   #define GLVertex2d(x,y)                   (glcaps_immediate ? glVertex2d(x,y) : glimtkVertex2d(x,y))
+   #define GLVertex3f(x,y,z)                 (glcaps_immediate ? glVertex3f(x,y,z) : glimtkVertex3f(x,y,z))
+   #define GLVertex3d(x,y,z)                 (glcaps_immediate ? glVertex3d(x,y,z) : glimtkVertex3d(x,y,z))
+   #define GLVertex3fv(v)                    (glcaps_immediate ? glVertex3fv(v) : glimtkVertex3fv(v))
+   #define GLVertex3dv(v)                    (glcaps_immediate ? glVertex3dv(v) : glimtkVertex3dv(v))
+   #define GLTexCoord2i(x,y)                 (glcaps_immediate ? glTexCoord2i(x,y) : glimtkTexCoord2i(x,y))
+   #define GLTexCoord2f(x,y)                 (glcaps_immediate ? glTexCoord2f(x,y) : glimtkTexCoord2f(x,y))
+   #define GLTexCoord2d(x,y)                 (glcaps_immediate ? glTexCoord2d(x,y) : glimtkTexCoord2d(x,y))
+   #define GLTexCoord2fv(v)                  (glcaps_immediate ? glTexCoord2fv(v) : glimtkTexCoord2fv(v))
+   #define GLNormal3f(x,y,z)                 (glcaps_immediate ? glNormal3f : glimtkNormal3f)
+   #define GLNormal3fv(v)                    (glcaps_immediate ? glNormal3fv(v) : glimtkNormal3fv(v))
+   #define GLColor3f(a,b,c)                  (glcaps_immediate ? glColor3f(a,b,c) : glimtkColor3f(a,b,c))
+   #define GLColor4ub(a,b,c,d)               (glcaps_immediate ? glColor4ub(a,b,c,d) : glimtkColor4ub(a,b,c,d))
+   #define GLColor4f(a,b,c,d)                (glcaps_immediate ? glColor4f(a,b,c,d) : glimtkColor4f(a,b,c,d))
+   #define GLColor4fv(v)                     (glcaps_immediate ? glColor4fv(v) : glimtkColor4fv(v))
+#else
+   #define GLRecti                           glimtkRecti
+   #define GLBegin                           glimtkBegin
+   #define GLTexCoord2i                      glimtkTexCoord2i
+   #define GLVertex2i                        glimtkVertex2i
+   #define GLTexCoord2d                      glimtkTexCoord2d
+   #define GLVertex2d                        glimtkVertex2d
+   #define GLTexCoord2f                      glimtkTexCoord2f
+   #define GLVertex2f                        glimtkVertex2f
+   #define GLEnd                             glimtkEnd
+   #define GLColor3f                         glimtkColor3f
+   #define GLColor4ub                        glimtkColor4ub
+   #define GLColor4f                         glimtkColor4f
+   #define GLColor4fv                        glimtkColor4fv
+   #define GLNormal3fv                       glimtkNormal3fv
+   #define GLNormal3f                        glimtkNormal3f
+   #define GLTexCoord2fv                     glimtkTexCoord2fv
+   #define GLVertex3d                        glimtkVertex3d
+   #define GLVertex3dv                       glimtkVertex3dv
+   #define GLVertex3f                        glimtkVertex3f
+   #define GLVertex3fv                       glimtkVertex3fv
+#endif
+
+#define GLMultMatrix GLMultMatrixd
+#define GLGetMatrix  GLGetDoublev
+#define GLTranslate  GLTranslated
+#define GLScale      GLScaled
+
+#if !ENABLE_GL_INTDBL
+   #define GL_INT                                  0x1404
+   #define GL_UNSIGNED_INT                         0x1405
+   #define GL_DOUBLE                               0x140A
+#endif
+
+#if ENABLE_GL_STIPPLES
+   #define GLLineStipple                     (stipples ? glLineStipple : glsupLineStipple)
+#else
+   #define GLLineStipple                     glsupLineStipple
+#endif
+
+#if ENABLE_GL_COLORMAT
+   #define GLColorMaterial(a,b)              glColorMaterial(a,b)
+#else
+   #define GLColorMaterial(a,b)
+#endif
+
+#ifdef _GLES
+   #define GLLightModeli                     glsupLightModeli
+#else
+   #define GLLightModeli                     glLightModeli
+#endif
+
+#define SETCAPS(caps) \
+   glcaps                     = caps; \
+   glcaps_shaders             = glcaps.shaders; \
+   glcaps_fixedFunction       = glcaps.fixedFunction; \
+   glcaps_nonPow2Textures     = glcaps.nonPow2Textures; \
+   glcaps_vertexBuffer        = glcaps.vertexBuffer; \
+   glcaps_quads               = glcaps.quads; \
+   glcaps_intAndDouble        = glcaps.intAndDouble; \
+   glcaps_immediate           = glcaps.immediate; \
+   glcaps_legacy              = glcaps.legacy; \
+   glcaps_pointSize           = glcaps.pointSize; \
+   glcaps_frameBuffer         = glcaps.frameBuffer;
+
+extern GLCapabilities glcaps;
+extern bool glcaps_nonPow2Textures, glcaps_vertexBuffer, glcaps_quads, glcaps_intAndDouble;
+extern bool glcaps_shaders, glcaps_fixedFunction, glcaps_immediate, glcaps_legacy, glcaps_pointSize, glcaps_frameBuffer;
+
+#if ENABLE_GL_INTDBL
+   #define GL_INDEX_INT GL_UNSIGNED_INT
+#else
+   #define GL_INDEX_INT GL_UNSIGNED_SHORT
+#endif
index 9e6f814..8d7618b 100644 (file)
@@ -1,38 +1,10 @@
 // GL Array Buffer Manipulation
-
-#if defined(__ANDROID__) || defined(__ODROID__)
-   #include <GLES/gl.h>
-
-   #define GL_INT    0x1404
-   #define GL_DOUBLE 0x140A
-
-   #if !defined(_GLES)
-      #define _GLES
-   #endif
-#elif defined(__EMSCRIPTEN__)
-   #include <GLES2/gl2.h>
-
-   #if !defined(_GLES2)
-      #define _GLES2
-   #endif
-
-   #define GL_INT    0x1404
-   #define GL_DOUBLE 0x140A
-#else
-   #include "gl_compat_4_4.h"
-#endif
-
-#define ENABLE_GL_SHADERS  (!defined(_GLES))
-#define ENABLE_GL_FFP      (!defined(_GLES2))
-#define ENABLE_GL_POINTER  (!defined(__EMSCRIPTEN__))
-#define ENABLE_GL_LEGACY   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_INTDBL   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_MAPBUF   (!defined(_GLES) && !defined(_GLES2))
-
 import "immediate"
 import "Display"
 import "OpenGLDisplayDriver"
 
+#include "glHelpers.h"
+
 // Kept public for now
 
 // NOTE: Don't call if without vertexBuffer
@@ -74,11 +46,11 @@ public struct GLAB
 {
    uint buffer;
 
-   void upload(bool vertexBuffer, uint size, void * data)
+   void upload(uint size, void * data)
    {
       if(this != null && data)
       {
-         if(vertexBuffer)
+         if(glcaps_vertexBuffer)
          {
             if(!buffer)
                glGenBuffers(1, &buffer);
@@ -91,29 +63,27 @@ public struct GLAB
       }
    }
 
-   void free(bool vertexBuffer)
+   void free()
    {
       if(this != null && buffer)
       {
-         if(vertexBuffer)
+         if(glcaps_vertexBuffer)
             GLABDeleteBuffers(1, this);
          buffer = 0;
       }
    }
 
-   void use(GLCapabilities capabilities, GLBufferContents contents, int n, int type, uint stride, void * pointer)
+   void use(GLBufferContents contents, int n, int type, uint stride, void * pointer)
    {
-      bool vertexBuffer = capabilities.vertexBuffer;
-      bool shaders = capabilities.shaders;
-      if(glabCurArrayBuffer != ((this != null) ? buffer : 0) && vertexBuffer)
+      if(glabCurArrayBuffer != ((this != null) ? buffer : 0) && glcaps_vertexBuffer)
          GLABBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          glVertexAttribPointer(contents, n, type, GL_FALSE, stride, pointer);
 #endif
 
 #if ENABLE_GL_FFP
-      if(!shaders)
+      if(!glcaps_shaders)
          switch(contents)
          {
             case normal:   glNormalPointer(type, stride, pointer); break;
@@ -124,29 +94,27 @@ public struct GLAB
 #endif
    }
 
-   void useVertTrans(GLCapabilities capabilities, uint count, int n, int type, uint stride, void * pointer)
+   void useVertTrans(uint count, int n, int type, uint stride, void * pointer)
    {
 #if !ENABLE_GL_INTDBL
-      bool vertexBuffer = capabilities.vertexBuffer;
-      bool shaders = capabilities.shaders;
-      if(glabCurArrayBuffer != ((this != null) ? buffer : 0) && vertexBuffer)
+      if(glabCurArrayBuffer != ((this != null) ? buffer : 0) && glcaps_vertexBuffer)
          GLABBindBuffer(GL_ARRAY_BUFFER, ((this != null) ? buffer : 0));
       if(type == GL_INT)
-         glimtkVertexPointeri(shaders, n, stride, pointer, count);
+         glimtkVertexPointeri(n, stride, pointer, count);
       else if(type == GL_DOUBLE)
       {
 #if ENABLE_GL_SHADERS
-         if(shaders)
+         if(glcaps_shaders)
             glVertexAttribPointer(GLBufferContents::vertex, n, GL_DOUBLE, GL_FALSE, stride, pointer);
 #endif
 
 #if ENABLE_GL_FFP
-         if(!shaders)
+         if(!glcaps_shaders)
             glVertexPointer(n, GL_DOUBLE, stride, pointer);
 #endif
       }
 #else
-      use(capabilities, vertex, n, type, stride, pointer);
+      use(vertex, n, type, stride, pointer);
 #endif
    }
 };
@@ -159,16 +127,16 @@ public struct GLEAB
 {
    uint buffer;
 
-   void upload(bool vertexBuffer, uint size, void * data)
+   void upload(uint size, void * data)
    {
       if(this != null && data)
       {
-         if(vertexBuffer)
+         if(glcaps_vertexBuffer)
          {
             if(!buffer)
                glGenBuffers(1, &buffer);
 
-            if(vertexBuffer && glabCurElementBuffer != buffer)
+            if(glcaps_vertexBuffer && glabCurElementBuffer != buffer)
                GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
             if(size)
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);  //GL_DYNAMIC_DRAW);
@@ -180,29 +148,30 @@ public struct GLEAB
       }
    }
 
-   void free(bool vertexBuffer)
+   void free()
    {
       if(this != null && buffer)
       {
-         if(vertexBuffer)
+         if(glcaps_vertexBuffer)
             GLABDeleteBuffers(1, (GLAB *)this);
          buffer = 0;
       }
    }
 
-   void draw(bool vertexBuffer, int primType, int count, int type, void * indices)
+   void draw(int primType, int count, int type, void * indices)
    {
-      if(vertexBuffer
+      if(glcaps_vertexBuffer
 #if ENABLE_GL_POINTER
          || (!buffer && indices)
 #endif
          )
       {
-         if(vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
+         if(glcaps_vertexBuffer && glabCurElementBuffer != ((this != null) ? buffer : 0))
             GLABBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((this != null) ? buffer : 0));
 #if !ENABLE_GL_INTDBL
          type = GL_UNSIGNED_SHORT;
 #endif
+         GLFlushMatrices();
          glDrawElements(primType, count, type, indices);
       }
    }
index fc2e1d5..9052f7c 100644 (file)
@@ -1,60 +1,8 @@
 // OpenGL Immediate Mode Transition Kit
-
-#if defined(__ANDROID__) || defined(__ODROID__)
-   #include <GLES/gl.h>
-   #ifndef _GLES
-      #define _GLES
-   #endif
-#elif defined(__EMSCRIPTEN__)
-   #include <GLES2/gl2.h>
-   #ifndef _GLES2
-      #define _GLES2
-   #endif
-#else
-   #include "gl_compat_4_4.h"
-#endif
-
 import "glab"
 import "shading"
 
-#define ENABLE_GL_SHADERS  (!defined(_GLES))
-#define ENABLE_GL_FFP      (!defined(_GLES2))
-#define ENABLE_GL_POINTER  (!defined(__EMSCRIPTEN__))
-#define ENABLE_GL_FBO      (!defined(__EMSCRIPTEN__))
-#define ENABLE_GL_LEGACY   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_INTDBL   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_MAPBUF   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_SELECT   (!defined(_GLES) && !defined(_GLES2))
-#define ENABLE_GL_COLORMAT (ENABLE_GL_FFP   && !defined(_GLES))
-
-#if ENABLE_GL_SHADERS && ENABLE_GL_FFP
-   #define GLEnableClientState            (shaders ? glEnableVertexAttribArray : glEnableClientState)
-   #define GLDisableClientState           (shaders ? glDisableVertexAttribArray : glDisableClientState)
-   #define VERTICES                       (shaders ? GLBufferContents::vertex : GL_VERTEX_ARRAY)
-   #define NORMALS                        (shaders ? GLBufferContents::normal : GL_NORMAL_ARRAY)
-   #define TEXTURECOORDS                  (shaders ? GLBufferContents::texCoord : GL_TEXTURE_COORD_ARRAY)
-   #define COLORS                         (shaders ? GLBufferContents::color : GL_COLOR_ARRAY)
-   #define GLVertexPointer(n, t, s, p)    (shaders ? glVertexAttribPointer(GLBufferContents::vertex,   n, t, GL_FALSE, s, p) : glVertexPointer(n, t, s, p))
-   #define GLTexCoordPointer(n, t, s, p)  (shaders ? glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p) : glTexCoordPointer(n, t, s, p))
-#elif ENABLE_GL_SHADERS
-   #define GLEnableClientState            glEnableVertexAttribArray
-   #define GLDisableClientState           glDisableVertexAttribArray
-   #define VERTICES                       GLBufferContents::vertex
-   #define NORMALS                        GLBufferContents::normal
-   #define TEXTURECOORDS                  GLBufferContents::texCoord
-   #define COLORS                         GLBufferContents::color
-   #define GLVertexPointer(n, t, s, p)    glVertexAttribPointer(GLBufferContents::vertex,   n, t, GL_FALSE, s, p)
-   #define GLTexCoordPointer(n, t, s, p)  glVertexAttribPointer(GLBufferContents::texCoord, n, t, GL_FALSE, s, p)
-#else
-   #define GLEnableClientState            glEnableClientState
-   #define GLDisableClientState           glDisableClientState
-   #define VERTICES                       GL_VERTEX_ARRAY
-   #define NORMALS                        GL_NORMAL_ARRAY
-   #define TEXTURECOORDS                  GL_TEXTURE_COORD_ARRAY
-   #define COLORS                         GL_COLOR_ARRAY
-   #define GLVertexPointer                glVertexPointer
-   #define GLTexCoordPointer              glTexCoordPointer
-#endif
+#include "glHelpers.h"
 
 public enum GLIMTKMode
 {
@@ -83,14 +31,14 @@ static bool vertexColorValues = false;
 static int vertexStride = 4;
 static int vertexOffset = 2;
 
-public void glimtkRecti(GLCapabilities capabilities, int a, int b, int c, int d)
+public void glimtkRecti(int a, int b, int c, int d)
 {
    glimtkBegin(quads);
    glimtkVertex2i(a, b);
    glimtkVertex2i(a, d);
    glimtkVertex2i(c, d);
    glimtkVertex2i(c, b);
-   glimtkEnd(capabilities);
+   glimtkEnd();
 }
 
 public void glimtkBegin(GLIMTKMode mode)
@@ -170,57 +118,54 @@ public void glimtkVertex2d(double x, double y)   { glimtkVertex2f((float)x, (flo
 
 GLAB streamVecAB, streamNorAB;
 
-public void glimtkEnd(GLCapabilities capabilities)
+public void glimtkEnd()
 {
-   bool vertexBuffer = capabilities.vertexBuffer;
-#if ENABLE_GL_SHADERS
-   bool shaders = capabilities.shaders;
-#endif
    GLIMTKMode mode = beginMode;
    if(mode == quads)        mode = triangles;
    else if(mode == polygon) mode = triangleFan;
 
-   GLEnableClientState(TEXTURECOORDS);
+   GLEnableClientState(TEXCOORDS);
 
-   if(vertexBuffer)
+   if(glcaps_vertexBuffer)
    {
-      streamVecAB.upload(vertexBuffer, vertexStride * sizeof(float) * vertexCount, vertexPointer);
-      streamVecAB.use(capabilities, texCoord, 2, GL_FLOAT, vertexStride * sizeof(float), 0);
+      streamVecAB.upload(vertexStride * sizeof(float) * vertexCount, vertexPointer);
+      streamVecAB.use(texCoord, 2, GL_FLOAT, vertexStride * sizeof(float), 0);
    }
    else
-      noAB.use(capabilities, texCoord, 2, GL_FLOAT, vertexStride * sizeof(float), vertexPointer);
+      noAB.use(texCoord, 2, GL_FLOAT, vertexStride * sizeof(float), vertexPointer);
 
    if(vertexColorValues)
    {
       GLEnableClientState(COLORS);
-      if(vertexBuffer)
-         streamVecAB.use(capabilities, color, 4, GL_FLOAT, vertexStride * sizeof(float), (void *)(2 * sizeof(float)));
+      if(glcaps_vertexBuffer)
+         streamVecAB.use(color, 4, GL_FLOAT, vertexStride * sizeof(float), (void *)(2 * sizeof(float)));
       else
-         noAB.use(capabilities, color, 4, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + 2);
+         noAB.use(color, 4, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + 2);
 
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          shader_setPerVertexColor(true);
 #endif
    }
 
-   if(vertexBuffer)
-      streamVecAB.use(capabilities, vertex, numVertexCoords, GL_FLOAT, vertexStride * sizeof(float), (void *)(vertexOffset * sizeof(float)));
+   if(glcaps_vertexBuffer)
+      streamVecAB.use(vertex, numVertexCoords, GL_FLOAT, vertexStride * sizeof(float), (void *)(vertexOffset * sizeof(float)));
    else
-      noAB.use(capabilities, vertex, numVertexCoords, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + vertexOffset);
+      noAB.use(vertex, numVertexCoords, GL_FLOAT, vertexStride * sizeof(float), vertexPointer + vertexOffset);
 
    if(normalCount && normalCount == vertexCount)
    {
       GLEnableClientState(NORMALS);
-      if(vertexBuffer)
+      if(glcaps_vertexBuffer)
       {
-         streamNorAB.upload(vertexBuffer, 3*sizeof(float) * vertexCount, normalPointer);
-         streamNorAB.use(capabilities, normal, 3, GL_FLOAT, 3*sizeof(float), 0);
+         streamNorAB.upload(3*sizeof(float) * vertexCount, normalPointer);
+         streamNorAB.use(normal, 3, GL_FLOAT, 3*sizeof(float), 0);
       }
       else
-         noAB.use(capabilities, normal, 3, GL_FLOAT, 3*sizeof(float),normalPointer);
+         noAB.use(normal, 3, GL_FLOAT, 3*sizeof(float),normalPointer);
    }
 
+   GLFlushMatrices();
    glDrawArrays(mode, 0, vertexCount);
 
    if(normalCount)
@@ -230,12 +175,12 @@ public void glimtkEnd(GLCapabilities capabilities)
       GLDisableClientState(COLORS);
 
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          shader_setPerVertexColor(false);
 #endif
 
    }
-   GLDisableClientState(TEXTURECOORDS);
+   GLDisableClientState(TEXCOORDS);
 
    normalCount = 0;
    vertexColorValues = false;
@@ -252,7 +197,7 @@ static unsigned int shortVPSize = 0, floatVPSize = 0;
 static unsigned short *shortBDBuffer = null;
 static unsigned int shortBDSize = 0;
 
-public void glimtkVertexPointeri(bool shaders, int numCoords, int stride, int *pointer, int numVertices)
+public void glimtkVertexPointeri(int numCoords, int stride, int *pointer, int numVertices)
 {
    if(pointer)
    {
@@ -271,7 +216,7 @@ public void glimtkVertexPointeri(bool shaders, int numCoords, int stride, int *p
       GLVertexPointer(numCoords, GL_SHORT, stride, 0);
 }
 
-public void glimtkVertexPointerd(bool shaders, int numCoords, int stride, double *pointer, int numVertices)
+public void glimtkVertexPointerd(int numCoords, int stride, double *pointer, int numVertices)
 {
    if(pointer)
    {
@@ -289,17 +234,17 @@ public void glimtkVertexPointerd(bool shaders, int numCoords, int stride, double
       GLVertexPointer(numCoords, GL_FLOAT, stride, 0);
 }
 
-public void glimtkTexReuseIntVP(bool shaders, int numCoords)
+public void glimtkTexReuseIntVP(int numCoords)
 {
    GLTexCoordPointer(numCoords, GL_SHORT, 0, floatVPBuffer);
 }
 
-public void glimtkTexReuseDoubleVP(bool shaders, int numCoords)
+public void glimtkTexReuseDoubleVP(int numCoords)
 {
    GLTexCoordPointer(numCoords, GL_FLOAT, 0, floatVPBuffer);
 }
 
-public void glimtkColor4f(bool shaders, float r, float g, float b, float a)
+public void glimtkColor4f(float r, float g, float b, float a)
 {
    if(beginMode != unset)
    {
@@ -338,12 +283,12 @@ public void glimtkColor4f(bool shaders, float r, float g, float b, float a)
    else
    {
 #if ENABLE_GL_SHADERS
-      if(shaders)
+      if(glcaps_shaders)
          shader_color(r, g, b, a);
 #endif
 
 #if ENABLE_GL_FFP
-      if(!shaders)
+      if(!glcaps_shaders)
       {
          glColor4f(r, g, b, a);
          if(lightingEnabled)
@@ -357,19 +302,19 @@ public void glimtkColor4f(bool shaders, float r, float g, float b, float a)
    }
 }
 
-public void glimtkColor3f( bool usingShaders, float r, float g, float b )
+public void glimtkColor3f( float r, float g, float b )
 {
-   glimtkColor4f(usingShaders, r, g, b, 1.0f);
+   glimtkColor4f(r, g, b, 1.0f);
 }
 
-public void glimtkColor4ub(bool usingShaders, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
+public void glimtkColor4ub(byte r, byte g, byte b, byte a)
 {
-   glimtkColor4f(usingShaders, r/255.0f, g/255.0f, b/255.0f, a/255.0f);
+   glimtkColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
 }
 
-public void glimtkColor4fv(bool shaders, float * a)
+public void glimtkColor4fv(float * a)
 {
-   glimtkColor4f(shaders, a[0], a[1], a[2], a[3]);
+   glimtkColor4f(a[0], a[1], a[2], a[3]);
 }
 
 public void glimtkBufferDatad(int target, int size, void * data, int usage)
index 60cc5bf..413b851 100644 (file)
@@ -1,24 +1,8 @@
 // Matrix Stack Implementation
-
-#if defined(__ANDROID__) || defined(__ODROID__)
-   #include <GLES/gl.h>
-   #ifndef _GLES
-      #define _GLES
-   #endif
-#elif defined(__EMSCRIPTEN__)
-   #include <GLES2/gl2.h>
-   #ifndef _GLES2
-      #define _GLES2
-   #endif
-#else
-   #include "gl_compat_4_4.h"
-#endif
-
 import "Display"
 import "shading"
 
-#define ENABLE_GL_SHADERS  (!defined(_GLES))
-#define ENABLE_GL_FFP      (!defined(_GLES2))
+#include "glHelpers.h"
 
 #if defined(ECERE_NO3D) || defined(ECERE_VANILLA)
 public union Matrix
@@ -89,7 +73,7 @@ public union Matrix
 #endif
 
 public enum MatrixMode { modelView = 0x1700, projection = 0x1701, texture = 0x1702 };
-public enum GLMSWhatToGet { projectionMatrix = 0x0BA7, modelViewMatrix = 0x0BA6, textureMatrix = 0x0BA8 };
+public enum GLMSWhatToGet { modelViewMatrix = 0x0BA6, projectionMatrix = 0x0BA7, textureMatrix = 0x0BA8 };
 
 double nearPlane = 1;
 
@@ -106,56 +90,18 @@ public void glmsSetNearPlane(double value)
 /*static */Matrix matrixStack[3][32];
 /*static */int matrixIndex[3];
 /*static */int curStack = 0;
+bool stackModified[3];
 
-static void LoadCurMatrix(bool shaders)
+public void glmsLoadMatrix(Matrix matrix)
 {
-   double * i = matrixStack[curStack][matrixIndex[curStack]].array;
-   float m[16] =
-   {
-      (float)i[0],(float)i[1],(float)i[2],(float)i[3],
-      (float)i[4],(float)i[5],(float)i[6],(float)i[7],
-      (float)i[8],(float)i[9],(float)i[10],(float)i[11],
-      (float)i[12],(float)i[13],(float)i[14],(float)i[15]
-   };
-
-#if ENABLE_GL_SHADERS
-   if(shaders)
-      shader_LoadMatrixf((MatrixMode) (0x1700 + curStack), m);
-#endif
-
-#if ENABLE_GL_FFP
-   if(!shaders)
-      glLoadMatrixf(m);
-#endif
-}
-
-public void glmsLoadMatrix(bool shaders, Matrix matrix)
-{
-   float m[16] =
-   {
-      (float)matrix.m[0][0], (float)matrix.m[0][1], (float)matrix.m[0][2], (float)matrix.m[0][3],
-      (float)matrix.m[1][0], (float)matrix.m[1][1], (float)matrix.m[1][2], (float)matrix.m[1][3],
-      (float)matrix.m[2][0], (float)matrix.m[2][1], (float)matrix.m[2][2], (float)matrix.m[2][3],
-      (float)matrix.m[3][0], (float)matrix.m[3][1], (float)matrix.m[3][2], (float)matrix.m[3][3]
-   };
-#if ENABLE_GL_SHADERS
-   if(shaders)
-   {
-      memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrix.array, sizeof(Matrix));
-      shader_LoadMatrixf((MatrixMode) (0x1700 + curStack), m);
-   }
-#endif
-
-#if ENABLE_GL_FFP
-   if(!shaders)
-      glLoadMatrixf(m);
-#endif
+   memcpy(matrixStack[curStack][matrixIndex[curStack]].array, matrix.array, sizeof(Matrix));
+   stackModified[curStack] = true;
 }
 
-public void glmsLoadIdentity(bool shaders)
+public void glmsLoadIdentity()
 {
    matrixStack[curStack][matrixIndex[curStack]].Identity();
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
 public void glmsPushMatrix()
@@ -167,16 +113,16 @@ public void glmsPushMatrix()
    }
 }
 
-public void glmsPopMatrix(bool shaders)
+public void glmsPopMatrix()
 {
    if(matrixIndex[curStack] > 0)
    {
       matrixIndex[curStack]--;
-      LoadCurMatrix(shaders);
+      stackModified[curStack] = true;
    }
 }
 
-public void glmsLoadMatrixf(bool shaders, float * i)
+public void glmsLoadMatrixf(float * i)
 {
    double m[16] =
    {
@@ -186,16 +132,16 @@ public void glmsLoadMatrixf(bool shaders, float * i)
       i[3*4+0], i[3*4+1], i[3*4+2], i[3*4+3]
    };
    memcpy(matrixStack[curStack][matrixIndex[curStack]].array, m, 16*sizeof(double));
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
-public void glmsLoadMatrixd(bool shaders, double * i)
+public void glmsLoadMatrixd(double * i)
 {
    memcpy(matrixStack[curStack][matrixIndex[curStack]].array, i, sizeof(Matrix));
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
-public void glmsOrtho(bool shaders, double l, double r, double b, double t, double n, double f )
+public void glmsOrtho(double l, double r, double b, double t, double n, double f )
 {
    Matrix m
    { {
@@ -207,10 +153,10 @@ public void glmsOrtho(bool shaders, double l, double r, double b, double t, doub
    Matrix res;
    res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = res;
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
-public void glmsScaled( bool shaders, double a, double b, double c )
+public void glmsScaled(double a, double b, double c)
 {
    Matrix m, r;
 
@@ -218,10 +164,10 @@ public void glmsScaled( bool shaders, double a, double b, double c )
    m.Scale(a,b,c);
    r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = r;
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
-public void glmsTranslated( bool shaders, double a, double b, double c )
+public void glmsTranslated( double a, double b, double c)
 {
    Matrix m, r;
 
@@ -229,11 +175,11 @@ public void glmsTranslated( bool shaders, double a, double b, double c )
    m.Translate(a,b,c);
    r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = r;
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
 #if !defined(ECERE_NO3D) && !defined(ECERE_VANILLA)
-public void glmsFrustum( bool shaders, double l, double r, double b, double t, double n, double f )
+public void glmsFrustum(double l, double r, double b, double t, double n, double f)
 {
    nearPlane = n;
    n = 1;
@@ -257,11 +203,11 @@ public void glmsFrustum( bool shaders, double l, double r, double b, double t, d
       Matrix res;
       res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
       matrixStack[curStack][matrixIndex[curStack]] = res;
-      LoadCurMatrix(shaders);
+      stackModified[curStack] = true;
    }
 }
 
-public void glmsRotated( bool shaders, double angle, double x, double y, double z)
+public void glmsRotated( double angle, double x, double y, double z)
 {
    Quaternion q;
    Matrix m, r;
@@ -272,15 +218,15 @@ public void glmsRotated( bool shaders, double angle, double x, double y, double
    m.RotationQuaternion(q);
    r.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = r;
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
-public void glmsMultMatrixd( bool shaders, double * i )
+public void glmsMultMatrixd(double * i)
 {
    Matrix r;
    r.Multiply((Matrix *)i, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = r;
-   LoadCurMatrix(shaders);
+   stackModified[curStack] = true;
 }
 
 public void glmsGetDoublev(GLMSWhatToGet what, double * i)
@@ -304,12 +250,97 @@ public void glmsGetDoublev(GLMSWhatToGet what, double * i)
 }
 #endif
 
-public void glmsMatrixMode(bool shaders, MatrixMode mode)
+public void glmsMatrixMode(MatrixMode mode)
 {
    curStack = (mode == modelView) ? 0 : (mode == projection) ? 1 : 2;
+}
+
+public void glmsFlushMatrices()
+{
+   int stack;
+#if ENABLE_GL_SHADERS
+   bool mvModified = false, prjViewModified = false;
+#endif
+   for(stack = 0; stack < 3; stack++)
+   {
+      if(stackModified[stack])
+      {
+         Matrix * matrix = &matrixStack[stack][matrixIndex[stack]];
+#if ENABLE_GL_SHADERS
+         if(glcaps_shaders)
+         {
+            if(stack == 0)
+            {
+               Matrix t, inv;
+               double * i = inv.array;
+
+               mvModified = true;
+               prjViewModified = true;
+
+               t.Transpose(matrix);
+               inv.Inverse(t);
+               {
+                  float m[16] =
+                  {
+                     (float)i[0],(float)i[1],(float)i[2],(float)i[3],
+                     (float)i[4],(float)i[5],(float)i[6],(float)i[7],
+                     (float)i[8],(float)i[9],(float)i[10],(float)i[11],
+                     (float)i[12],(float)i[13],(float)i[14],(float)i[15]
+                  };
+                  shader_LoadNormalMatrix(m);
+               }
+            }
+            else if(stack == 1)
+               prjViewModified = true;
+            else
+            {
+               float m[16] =
+               {
+                  (float)matrix->m[0][0], (float)matrix->m[0][1], (float)matrix->m[0][2], (float)matrix->m[0][3],
+                  (float)matrix->m[1][0], (float)matrix->m[1][1], (float)matrix->m[1][2], (float)matrix->m[1][3],
+                  (float)matrix->m[2][0], (float)matrix->m[2][1], (float)matrix->m[2][2], (float)matrix->m[2][3],
+                  (float)matrix->m[3][0], (float)matrix->m[3][1], (float)matrix->m[3][2], (float)matrix->m[3][3]
+               };
+               shader_LoadMatrixf((MatrixMode) (0x1700 + stack), m);
+            }
+         }
+#endif
 
 #if ENABLE_GL_FFP
-   if(!shaders)
-      glMatrixMode(mode);
+         if(!glcaps_shaders)
+         {
+            float m[16] =
+            {
+               (float)matrix->m[0][0], (float)matrix->m[0][1], (float)matrix->m[0][2], (float)matrix->m[0][3],
+               (float)matrix->m[1][0], (float)matrix->m[1][1], (float)matrix->m[1][2], (float)matrix->m[1][3],
+               (float)matrix->m[2][0], (float)matrix->m[2][1], (float)matrix->m[2][2], (float)matrix->m[2][3],
+               (float)matrix->m[3][0], (float)matrix->m[3][1], (float)matrix->m[3][2], (float)matrix->m[3][3]
+            };
+            glMatrixMode(stack + 0x1700);
+            glLoadMatrixf(m);
+         }
+#endif
+         stackModified[stack] = false;
+      }
+#if ENABLE_GL_SHADERS
+      if(glcaps_shaders && stack == 1 && prjViewModified)
+      {
+         Matrix * mv = &matrixStack[0][matrixIndex[0]];
+         Matrix * prj = &matrixStack[1][matrixIndex[1]];
+         Matrix mat;
+         mat.Multiply(mv, prj);
+         {
+            float m[16] =
+            {
+               (float)mat.m[0][0], (float)mat.m[0][1], (float)mat.m[0][2], (float)mat.m[0][3],
+               (float)mat.m[1][0], (float)mat.m[1][1], (float)mat.m[1][2], (float)mat.m[1][3],
+               (float)mat.m[2][0], (float)mat.m[2][1], (float)mat.m[2][2], (float)mat.m[2][3],
+               (float)mat.m[3][0], (float)mat.m[3][1], (float)mat.m[3][2], (float)mat.m[3][3]
+            };
+            float mvz[4] = { (float)mv->m[0][2], (float)mv->m[1][2], (float)mv->m[2][2], (float)mv->m[3][2] };
+            shader_LoadPrjMVMatrix(m, mvModified ? mvz : null);
+         }
+      }
 #endif
+   }
 }
index 378cdff..5e0dc56 100644 (file)
@@ -26,11 +26,15 @@ import "glab"
 #if ENABLE_GL_SHADERS
 
 // Uniforms
-int uPrjMatrix;
-int uMVMatrix;
+// int uPrjMatrix;
+// int uMVMatrix;
+int uMVMatrix_z;
+int uPrjMVMatrix;
 int uTextureMatrix;
+int uNormalsMatrix;
 int uColor;
 int uTexturingOn;
+int uSwizzleMode;
 int uLightingOn;
 int uFogOn;
 int uFogDensity;
@@ -54,10 +58,22 @@ void shader_LoadMatrixf(MatrixMode mode, float * m)
 {
    if(mode == texture)
       glUniformMatrix4fv(uTextureMatrix, 1, GL_FALSE, m);
-   else if(mode == projection)
+   /*else if(mode == projection)
       glUniformMatrix4fv(uPrjMatrix, 1, GL_FALSE, m);
    else
-      glUniformMatrix4fv(uMVMatrix, 1, GL_FALSE, m);
+      glUniformMatrix4fv(uMVMatrix, 1, GL_FALSE, m);*/
+}
+
+void shader_LoadNormalMatrix(float * m)
+{
+   glUniformMatrix4fv(uNormalsMatrix, 1, GL_FALSE, m);
+}
+
+void shader_LoadPrjMVMatrix(float * m, float * mvz)
+{
+   glUniformMatrix4fv(uPrjMVMatrix, 1, GL_FALSE, m);
+   if(mvz)
+      glUniform4fv(uMVMatrix_z, 1, mvz);
 }
 
 void shader_setGlobalAmbient(float r, float g, float b, float a)
@@ -95,6 +111,11 @@ void shader_texturing(bool on)
    glUniform1i(uTexturingOn, on);
 }
 
+void shader_swizzle(int swizzle)
+{
+   glUniform1i(uSwizzleMode, swizzle);
+}
+
 #if !defined(ECERE_NO3D)
 public void shader_setMaterial(Material material, bool perVertexColor)
 {
@@ -226,9 +247,10 @@ void shader_setLight(Display display, uint id, Light light)
          Vector3Df direction;
          Matrix mat;
          Matrix m = matrixStack[0][matrixIndex[0]];
-         m.Scale(nearPlane, nearPlane, nearPlane);
 
+         m.Scale(nearPlane, nearPlane, nearPlane);
          mat.RotationQuaternion(light.orientation);
+
          direction.MultMatrix(vector, mat);
 
          vector.MultMatrix(direction, m);
@@ -365,11 +387,15 @@ bool loadShaders(DisplaySystem displaySystem, const String vertexShaderFile, con
                puts(compileLog[0] ? compileLog : "Success.");
             }
 
-            uPrjMatrix     = glGetUniformLocation(program, "projection_matrix");
-            uMVMatrix      = glGetUniformLocation(program, "modelview_matrix");
+            //uPrjMatrix     = glGetUniformLocation(program, "projection_matrix");
+            //uMVMatrix      = glGetUniformLocation(program, "modelview_matrix");
+            uMVMatrix_z      = glGetUniformLocation(program, "mvmatrix_z");
+            uPrjMVMatrix   = glGetUniformLocation(program, "proj_modelview_matrix");
             uTextureMatrix = glGetUniformLocation(program, "texture_matrix");
+            uNormalsMatrix = glGetUniformLocation(program, "normals_matrix");
             uColor         = glGetUniformLocation(program, "current_color");
             uTexturingOn   = glGetUniformLocation(program, "texturingOn");
+            uSwizzleMode   = glGetUniformLocation(program, "swizzleMode");
             uLightingOn    = glGetUniformLocation(program, "lightingOn");
             uFogOn         = glGetUniformLocation(program, "fogOn");
             uFogDensity    = glGetUniformLocation(program, "fogDensity");
@@ -418,12 +444,12 @@ bool loadShaders(DisplaySystem displaySystem, const String vertexShaderFile, con
       glUseProgram(oglSystem.shadingProgram);
 
       // Initialize uniforms to defaults
-      glmsMatrixMode(true, texture);
-      glmsLoadIdentity(true);
-      glmsMatrixMode(true, projection);
-      glmsLoadIdentity(true);
-      glmsMatrixMode(true, modelView);
-      glmsLoadIdentity(true);
+      glmsMatrixMode(texture);
+      glmsLoadIdentity();
+      glmsMatrixMode(projection);
+      glmsLoadIdentity();
+      glmsMatrixMode(modelView);
+      glmsLoadIdentity();
       shader_color(1.0, 1.0, 1.0, 1.0);
    }
    return result;
index 9d04142..e9ca5c2 100644 (file)
@@ -185,6 +185,24 @@ typedef BOOL (APIENTRY * PFNWGLSWAPINTERVALEXTPROC) (int interval);
 typedef int (APIENTRY * PFNWGLGETSWAPINTERVALEXTPROC) (void);
 typedef HGLRC (APIENTRY * PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int*);
 
+typedef void (APIENTRY * PFNGLLOCKARRAYSEXTPROC) (GLint first, GLsizei count);
+typedef void (APIENTRY * PFNGLUNLOCKARRAYSEXTPROC) (void);
+
+static PFNGLLOCKARRAYSEXTPROC glLockArraysEXT = null;
+static PFNGLUNLOCKARRAYSEXTPROC glUnlockArraysEXT = null;
+
+static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = null;
+static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = null;
+static PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB = null;
+static PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB = null;
+static PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB = null;
+static PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB = null;
+static PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB = null;
+static PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB = null;
+static PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB = null;
+static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = null;
+static PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = null;
+
 #ifdef WGL_WGLEXT_PROTOTYPES
 extern BOOL WINAPI wglSwapIntervalEXT (int);
 extern int WINAPI wglGetSwapIntervalEXT (void);
index 6231218..c41433f 100644 (file)
@@ -9811,7 +9811,7 @@ private:
    void * windowData;
    CreationActivationOption creationActivation;
    GLCapabilities glCapabilities;
-   glCapabilities = { true, true, true, true, true, true, true, true };
+   glCapabilities = { true, true, true, true, true, true, true, true, true, true };
    struct
    {
       bool active:1;            // true if window and ancestors are active