ecere/gfx3D/OpenGLDisplayDriver: Fixed broken fog density
[sdk] / ecere / src / gfx / drivers / OpenGLDisplayDriver.ec
index ff951a6..437051a 100644 (file)
@@ -69,7 +69,7 @@ namespace gfx::drivers;
 #if defined(__WIN32__)
 #define WIN32_LEAN_AND_MEAN
 #undef _WIN32_WINNT
-#define _WIN32_WINNT 0x0500
+#define _WIN32_WINNT 0x0502
 #define String Sting_
 #include <windows.h>
 #undef String
@@ -324,6 +324,9 @@ typedef HDC (APIENTRY * PFNWGLGETPBUFFERDCARBPROC) (void * hPbuffer);
 typedef int (APIENTRY * PFNWGLRELEASEPBUFFERDCARBPROC) (void * hPbuffer, HDC hDC);
 typedef BOOL (APIENTRY * PFNWGLDESTROYPBUFFERARBPROC) (void * hPbuffer);
 typedef BOOL (APIENTRY * PFNWGLQUERYPBUFFERARBPROC) (void * hPbuffer, int iAttribute, int *piValue);
+typedef const char * (APIENTRY * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
+typedef BOOL (APIENTRY * PFNWGLBINDTEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
+typedef BOOL (APIENTRY * PFNWGLRELEASETEXIMAGEARBPROC) (void * hPbuffer, int iBuffer);
 
 static PFNGLMAPBUFFERARBPROC glMapBufferARB = null;
 static PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = null;
@@ -462,7 +465,7 @@ static bool egl_init_display(ANativeWindow* window)
       EGL_SAMPLES, 0, //2,*/
       EGL_NONE
    };
-   EGLint w, h, dummy, format;
+   EGLint w, h, format;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
@@ -808,13 +811,14 @@ void glesLoadMatrixd(double * i)
 
 void glesOrtho( double l, double r, double b, double t, double n, double f )
 {
-   Matrix m =
+   Matrix m
    { {
       (2 / (r - l)), 0, 0, 0,
       0, (2 / (t - b)), 0, 0,
       0, 0, (-2 / (f - n)), 0,
       (-(r + l) / (r - l)), (-(t + b) / (t - b)), (-(f + n) / (f - n)), 1
-   } }, res;
+   } };
+   Matrix res;
    res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
    matrixStack[curStack][matrixIndex[curStack]] = res;
    LoadCurMatrix();
@@ -834,13 +838,14 @@ void glesFrustum( double l, double r, double b, double t, double n, double f )
       double B = ((t + b) / (t - b));
       double C = (-(f + n) / (f - n));
       double D = (-2*f*n/(f-n));
-      Matrix m =
+      Matrix m
       { {
          (2.0*n / (r - l)), 0, 0, 0,
          0, (2.0*n / (t - b)), 0, 0,
          A, B,             C,-1,
          0, 0,             D, 0
-      } }, res;
+      } };
+      Matrix res;
       res.Multiply(m, matrixStack[curStack][matrixIndex[curStack]]);
       matrixStack[curStack][matrixIndex[curStack]] = res;
       LoadCurMatrix();
@@ -1042,7 +1047,6 @@ static bool stippleEnabled;
 void glesLineStipple( int i, unsigned short j )
 {
    uint texture[1*16];
-   int c;
    int x;
    for(x = 0; x < 16; x++)
    {
@@ -1139,7 +1143,9 @@ void GLGenBuffers(int count, uint * buffer)
 #ifdef __ANDROID__
    glGenBuffers(count, buffer);
 #else
+#if defined(__WIN32__)
    if(glGenBuffersARB)
+#endif
       glGenBuffersARB(count, buffer);
 #endif
 }
@@ -1149,7 +1155,9 @@ void GLDeleteBuffers(int count, GLuint * buffer)
 #ifdef __ANDROID__
    glDeleteBuffers(count, buffer);
 #else
+#if defined(__WIN32__)
    if(glDeleteBuffersARB)
+#endif
       glDeleteBuffersARB(count, buffer);
 #endif
 }
@@ -1159,7 +1167,9 @@ void GLBindBuffer(int target, uint buffer)
 #ifdef __ANDROID__
    glBindBuffer(target, buffer);
 #else
+#if defined(__WIN32__)
    if(glBindBufferARB)
+#endif
       glBindBufferARB(target, buffer);
 #endif
 }
@@ -1168,13 +1178,16 @@ void GLBufferData(int type, GLenum target, int size, const GLvoid *data, GLenum
 {
 #ifdef __ANDROID__
    if(type == GL_DOUBLE)
-      glesBufferDatad(target, size, data, usage);
+      glesBufferDatad(target, size, (void *)data, usage);
    else if(type == GL_UNSIGNED_INT)
-      glesBufferDatai(target, size, data, usage);
+      glesBufferDatai(target, size, (void *)data, usage);
    else
       glBufferData(target, size, data, usage);
 #else
+
+#if defined(__WIN32__)
    if(glBufferDataARB)
+#endif
       glBufferDataARB(target, size, data, usage);
 
 #endif
@@ -1202,7 +1215,7 @@ class OGLDisplay : struct
    int imageBuffers[2];
    byte * pboMemory1, * pboMemory2;
    */
-#else
+#elif !defined(__ANDROID__)
    GLXContext glContext;
 
    Pixmap pixmap;
@@ -1236,7 +1249,7 @@ class OGLSystem : struct
    HDC hdc;
    HGLRC glrc;
    HWND hwnd;
-#else
+#elif !defined(__ANDROID__)
    XVisualInfo * visualInfo;
    GLXContext glContext;
    GLXDrawable glxDrawable;
@@ -1285,6 +1298,7 @@ class OpenGLDisplayDriver : DisplayDriver
 
    bool LockSystem(DisplaySystem displaySystem)
    {
+#if !defined(__ANDROID__)
       OGLSystem oglSystem = displaySystem.driverData;
       if(useSingleGLContext) return true;
    #if defined(__WIN32__)
@@ -1292,11 +1306,10 @@ class OpenGLDisplayDriver : DisplayDriver
    #elif defined(__unix__) || defined(__APPLE__)
       //if(previous) return true;
       // printf("Making SYSTEM current\n");
-#if !defined(__ANDROID__)
       glXMakeCurrent(xGlobalDisplay, (GLXDrawable)oglSystem.glxDrawable, oglSystem.glContext);
-#endif
       //previous = oglSystem.glContext;
    #endif
+#endif
       return true;
    }
 
@@ -1317,19 +1330,17 @@ class OpenGLDisplayDriver : DisplayDriver
 
    bool Lock(Display display)
    {
+#if !defined(__ANDROID__)
       OGLDisplay oglDisplay = display.driverData;
-
       if(useSingleGLContext) return true;
    #if defined(__WIN32__)
       wglMakeCurrent(oglDisplay.hdc, oglDisplay.glrc);
    #elif defined(__unix__) || defined(__APPLE__)
       // if(previous) glXMakeCurrent(xGlobalDisplay, None, null);
       // printf("   Making DISPLAY current\n");
-      #if defined(__ANDROID__)
-      #else
       glXMakeCurrent(xGlobalDisplay, (GLXDrawable)display.window, oglDisplay.glContext);
-      #endif
    #endif
+#endif
       return true;
    }
 
@@ -1637,7 +1648,9 @@ class OpenGLDisplayDriver : DisplayDriver
    {
       bool result = false;
       OGLDisplay oglDisplay = display.driverData;
+#if !defined(__ANDROID__)
       OGLSystem oglSystem = display.displaySystem.driverData;
+#endif
       if(!oglDisplay)
          oglDisplay = display.driverData = OGLDisplay { };
       //printf("Inside CreateDisplay\n");
@@ -1702,12 +1715,18 @@ class OpenGLDisplayDriver : DisplayDriver
 #endif
       if(result)
       {
-#if !defined(__OLDX__)
+#if defined(__WIN32__)
          if(glBlendFuncSeparate)
             glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
          else
-#endif
             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+#else
+#if !defined(__OLDX__)
+          glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+#else
+         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+#endif
+#endif
          glEnable(GL_BLEND);
 
          glMatrixMode(GL_MODELVIEW);
@@ -1749,12 +1768,12 @@ class OpenGLDisplayDriver : DisplayDriver
    bool DisplaySize(Display display, int width, int height)
    {
       OGLDisplay oglDisplay = display.driverData;
-      OGLSystem oglSystem = display.displaySystem.driverData;
 
       bool result = false;
 
       //printf("Inside DisplaySize\n");
 #if defined(__WIN32__) || defined(USEPBUFFER)
+      OGLSystem oglSystem = display.displaySystem.driverData;
       if(display.alphaBlend)
       {
 #if defined(__WIN32__)
@@ -2144,7 +2163,9 @@ class OpenGLDisplayDriver : DisplayDriver
 
    void Update(Display display, Box updateBox)
    {
+#if defined(__WIN32__) || defined(USEPBUFFER)
       OGLDisplay oglDisplay = display.driverData;
+#endif
       //Logf("DisplayScreen\n");
 
       glFlush();
@@ -3173,8 +3194,7 @@ class OpenGLDisplayDriver : DisplayDriver
             break;
          }
          case fogDensity:
-            value *= nearPlane;
-            glFogf(GL_FOG_DENSITY, *(float *)(void *)&value);
+            glFogf(GL_FOG_DENSITY, (float)(RenderStateFloat { ui = value }.f * nearPlane));
             break;
          case blend:
             if(value) glEnable(GL_BLEND); else glDisable(GL_BLEND);
@@ -3593,7 +3613,7 @@ class OpenGLDisplayDriver : DisplayDriver
       }
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       bool result = false;
 
@@ -3602,26 +3622,85 @@ class OpenGLDisplayDriver : DisplayDriver
       if(mesh.data)
       {
          OGLMesh oglMesh = mesh.data;
-
-         if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
+         if(mesh.nVertices == nVertices)
          {
-            mesh.vertices = mesh.flags.doubleVertices ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
-            GLGenBuffers(1, &oglMesh.vertices);
-         }
-         if(mesh.flags.normals && !oglMesh.normals && !mesh.normals)
-         {
-            GLGenBuffers( 1, &oglMesh.normals);
-            mesh.normals = mesh.flags.doubleNormals ? (Vector3Df *)new Vector3D[mesh.nVertices] : new Vector3Df[mesh.nVertices];
-         }
-         if(mesh.flags.texCoords1 && !oglMesh.texCoords && !mesh.texCoords)
-         {
-            GLGenBuffers( 1, &oglMesh.texCoords);
-            mesh.texCoords = new Pointf[mesh.nVertices];
+            // Same number of vertices, adding features (Leaves the other features pointers alone)
+            if(mesh.flags != flags)
+            {
+               if(!mesh.flags.vertices && flags.vertices)
+               {
+                  if(flags.doubleVertices)
+                  {
+                     mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
+                  }
+                  else
+                     mesh.vertices = new Vector3Df[nVertices];
+                  if(!oglMesh.vertices)
+                     GLGenBuffers(1, &oglMesh.vertices);
+               }
+               if(!mesh.flags.normals && flags.normals)
+               {
+                  if(flags.doubleNormals)
+                  {
+                     mesh.normals = (Vector3Df *)new Vector3D[nVertices];
+                  }
+                  else
+                     mesh.normals = new Vector3Df[nVertices];
+                  if(!oglMesh.normals)
+                     GLGenBuffers( 1, &oglMesh.normals);
+               }
+               if(!mesh.flags.texCoords1 && flags.texCoords1)
+               {
+                  mesh.texCoords = new Pointf[nVertices];
+                  if(!oglMesh.texCoords)
+                     GLGenBuffers( 1, &oglMesh.texCoords);
+               }
+               if(!mesh.flags.colors && flags.colors)
+               {
+                  mesh.colors = new ColorRGBAf[nVertices];
+                  if(!oglMesh.colors)
+                     GLGenBuffers( 1, &oglMesh.colors);
+               }
+            }
          }
-         if(mesh.flags.colors && !oglMesh.colors && !mesh.colors)
+         else
          {
-            GLGenBuffers( 1, &oglMesh.colors);
-            mesh.colors = new ColorRGBAf[mesh.nVertices];
+            // New number of vertices, reallocate all current and new features
+            flags |= mesh.flags;
+            if(flags.vertices)
+            {
+               if(flags.doubleVertices)
+               {
+                  mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
+               }
+               else
+                  mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
+               if(!oglMesh.vertices)
+                  GLGenBuffers(1, &oglMesh.vertices);
+            }
+            if(flags.normals)
+            {
+               if(flags.doubleNormals)
+               {
+                  mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
+               }
+               else
+                  mesh.normals = renew mesh.normals Vector3Df[nVertices];
+               if(!oglMesh.normals)
+                  GLGenBuffers( 1, &oglMesh.normals);
+            }
+            if(flags.texCoords1)
+            {
+               mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+               if(!oglMesh.texCoords)
+                  GLGenBuffers( 1, &oglMesh.texCoords);
+            }
+            if(flags.colors)
+            {
+               mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
+               if(!oglMesh.colors)
+                  GLGenBuffers( 1, &oglMesh.colors);
+            }
          }
          result = true;
       }
@@ -3635,25 +3714,25 @@ class OpenGLDisplayDriver : DisplayDriver
 
       if(vboAvailable)
       {
-         if(!(flags.vertices) || oglMesh.vertices)
+         if(flags.vertices && oglMesh.vertices)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.vertices);
             GLBufferData( mesh.flags.doubleVertices ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleVertices ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.vertices, GL_STATIC_DRAW_ARB );
          }
 
-         if(!(flags.normals) || oglMesh.normals)
+         if(flags.normals && oglMesh.normals)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.normals);
             GLBufferData( mesh.flags.doubleNormals ? GL_DOUBLE : GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * (mesh.flags.doubleNormals ? sizeof(Vector3D) : sizeof(Vector3Df)), mesh.normals, GL_STATIC_DRAW_ARB );
          }
 
-         if(!(flags.texCoords1) || oglMesh.texCoords)
+         if(flags.texCoords1 && oglMesh.texCoords)
          {
             GLBindBuffer(GL_ARRAY_BUFFER_ARB, oglMesh.texCoords);
             GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(Pointf), mesh.texCoords, GL_STATIC_DRAW_ARB );
          }
 
-         if(!(flags.colors) || oglMesh.colors)
+         if(flags.colors && oglMesh.colors)
          {
             GLBindBuffer( GL_ARRAY_BUFFER_ARB, oglMesh.colors);
             GLBufferData( GL_FLOAT, GL_ARRAY_BUFFER_ARB, mesh.nVertices * sizeof(ColorRGBAf), mesh.colors, GL_STATIC_DRAW_ARB );
@@ -3715,8 +3794,11 @@ class OpenGLDisplayDriver : DisplayDriver
       //Logf("SelectMesh\n");
 
 #if !defined( __ANDROID__) && !defined(__APPLE__)
-      if(display.display3D.mesh && glUnlockArraysEXT)
-         glUnlockArraysEXT();
+#if defined(__WIN32__)
+      if(glUnlockArraysEXT)
+#endif
+         if(display.display3D.mesh)
+            glUnlockArraysEXT();
 #endif
       if(mesh)
       {
@@ -3788,7 +3870,11 @@ class OpenGLDisplayDriver : DisplayDriver
          }
 
 #if !defined(__ANDROID__) && !defined(__APPLE__)
-         if(glLockArraysEXT) glLockArraysEXT(0, mesh.nVertices);
+
+#if defined(__WIN32__)
+         if(glLockArraysEXT)
+#endif
+            glLockArraysEXT(0, mesh.nVertices);
 #endif
       }
       else
@@ -3892,15 +3978,22 @@ public void UseSingleGLContext(bool useSingle)
    useSingleGLContext = useSingle;
 }
 
-default dllexport void * __attribute__((stdcall)) IS_GLGetContext(DisplaySystem displaySystem)
+default dllexport void *
+#if defined(__WIN32__)
+__attribute__((stdcall))
+#endif
+IS_GLGetContext(DisplaySystem displaySystem)
 {
    if(displaySystem)
    {
-      OGLSystem system = displaySystem.driverData;
 #if defined(__WIN32__)
+      OGLSystem system = displaySystem.driverData;
       return system.glrc;
-#else
+#elif !defined(__ANDROID__)
+      OGLSystem system = displaySystem.driverData;
       return system.glContext;
+#else
+      return eglContext;
 #endif
    }
    return null;