ecere/gfx3D/Mesh;Display/drivers: Support for adding vertices and features
authorJerome St-Louis <jerome@ecere.com>
Thu, 7 Aug 2014 01:59:02 +0000 (21:59 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 7 Aug 2014 02:41:34 +0000 (22:41 -0400)
ecere/src/gfx/3D/Mesh.ec
ecere/src/gfx/Display.ec
ecere/src/gfx/drivers/Direct3D8DisplayDriver.ec
ecere/src/gfx/drivers/Direct3D9DisplayDriver.ec
ecere/src/gfx/drivers/LFBDisplayDriver.ec
ecere/src/gfx/drivers/OpenGLDisplayDriver.ec

index 29815ed..efeed1e 100644 (file)
@@ -50,6 +50,7 @@ public:
       struct { int first, nVertices; };
    };
    Material material;
+
 private:
    void * data;
 };
@@ -122,14 +123,14 @@ public:
    bool Allocate(MeshFeatures what, int nVertices, DisplaySystem displaySystem)
    {
       bool result = false;
-      if((!this.nVertices || this.nVertices == nVertices) && (!this.displaySystem || this.displaySystem == displaySystem))
+      if(!this.displaySystem || this.displaySystem == displaySystem)
       {
-         flags |= what;
-         this.nVertices = nVertices;
          driver = displaySystem ? displaySystem.driver : (subclass(DisplayDriver))class(LFBDisplayDriver);
          if(driver.AllocateMesh == DisplayDriver::AllocateMesh) driver = (subclass(DisplayDriver))class(LFBDisplayDriver);
-         if(driver.AllocateMesh(displaySystem, this))
+         if(driver.AllocateMesh(displaySystem, this, what, nVertices))
          {
+            flags |= what;
+            this.nVertices = nVertices;
             if(Lock(what))
                result = true;
          }
index 8ac831d..7013496 100644 (file)
@@ -158,7 +158,7 @@ public:
    virtual void ::SetRenderState(Display, RenderState, uint);
    virtual void ::SetLight(Display, int, Light);
    virtual void ::SetCamera(Display, Surface, Camera);
-   virtual bool ::AllocateMesh(DisplaySystem, Mesh);
+   virtual bool ::AllocateMesh(DisplaySystem, Mesh, MeshFeatures, int nVertices);
    virtual void ::FreeMesh(DisplaySystem, Mesh);
    virtual bool ::LockMesh(DisplaySystem, Mesh, MeshFeatures flags);
    virtual void ::UnlockMesh(DisplaySystem, Mesh, MeshFeatures flags);
index 83de7fb..e0e4f52 100644 (file)
@@ -1351,7 +1351,7 @@ class Direct3D8DisplayDriver : DisplayDriver
       }
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       D3D8System d3dSystem = displaySystem.driverData;
       bool result = false;
@@ -1363,26 +1363,76 @@ class Direct3D8DisplayDriver : DisplayDriver
       {
          D3D8Mesh d3dMesh = mesh.data;
          result = true;
-         if((mesh.flags .vertices) && !d3dMesh.vertices)
+         if(mesh.nVertices == nVertices)
          {
-            mesh.vertices = new Vector3Df[mesh.nVertices];
-            if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices))
-               result = false;
-         }
-         if((mesh.flags.normals) && !d3dMesh.normals)
-         {
-            mesh.normals = new Vector3Df[mesh.nVertices];
-            if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals))
-               result = false;
+            if(mesh.flags != flags)
+            {
+               // Same number of vertices, adding features (Leaves the other features pointers alone)
+               if(flags.vertices && !d3dMesh.vertices)
+               {
+                  mesh.vertices = new Vector3Df[nVertices];
+                  if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices))
+                     result = false;
+               }
+               if(flags.normals && !d3dMesh.normals)
+               {
+                  mesh.normals = new Vector3Df[nVertices];
+                  if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals))
+                     result = false;
+               }
+               if(flags.texCoords1 && !d3dMesh.texCoords)
+               {
+                  mesh.texCoords = new Pointf[nVertices];
+                  if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords))
+                     result = false;
+               }
+            }
          }
-         if((mesh.flags.texCoords1) && !d3dMesh.texCoords)
+         else
          {
-            mesh.texCoords = new Pointf[mesh.nVertices];
-            if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords))
-               result = false;
+            // New number of vertices, reallocate all current and new features
+            flags |= mesh.flags;
+
+            // Same number of vertices, adding features (Leaves the other features pointers alone)
+            if(flags.vertices)
+            {
+               if(d3dMesh.vertices)
+               {
+                  IDirect3DVertexBuffer8_Release(d3dMesh.vertices);
+                  d3dMesh.vertices = null;
+               }
+               mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
+               if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices))
+                  result = false;
+            }
+            if(flags.normals)
+            {
+               if(d3dMesh.normals)
+               {
+                  IDirect3DVertexBuffer8_Release(d3dMesh.normals);
+                  d3dMesh.normals = null;
+               }
+               mesh.normals = renew mesh.normals Vector3Df[nVertices];
+               if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals))
+                  result = false;
+            }
+            if(flags.texCoords1)
+            {
+               if(d3dMesh.texCoords)
+               {
+                  IDirect3DVertexBuffer8_Release(d3dMesh.texCoords);
+                  d3dMesh.texCoords = null;
+               }
+               mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+               if(IDirect3DDevice8_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords))
+                  result = false;
+            }
          }
       }
       return result;
index bfa7587..e743421 100644 (file)
@@ -1377,7 +1377,7 @@ class Direct3D9DisplayDriver : DisplayDriver
       }
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       D3DSystem d3dSystem = displaySystem.driverData;
       bool result = false;
@@ -1389,26 +1389,76 @@ class Direct3D9DisplayDriver : DisplayDriver
       {
          D3DMesh d3dMesh = mesh.data;
          result = true;
-         if((mesh.flags.vertices) && !d3dMesh.vertices)
+         if(mesh.nVertices == nVertices)
          {
-            mesh.vertices = new Vector3Df[mesh.nVertices];
-            if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices, null))
-               result = false;
-         }
-         if((mesh.flags.normals) && !d3dMesh.normals)
-         {
-            mesh.normals = new Vector3Df[mesh.nVertices];
-            if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals, null))
-               result = false;
+            if(mesh.flags != flags)
+            {
+               // Same number of vertices, adding features (Leaves the other features pointers alone)
+               if(flags.vertices && !d3dMesh.vertices)
+               {
+                  mesh.vertices = new Vector3Df[nVertices];
+                  if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices, null))
+                     result = false;
+               }
+               if(flags.normals && !d3dMesh.normals)
+               {
+                  mesh.normals = new Vector3Df[nVertices];
+                  if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals, null))
+                     result = false;
+               }
+               if(flags.texCoords1 && !d3dMesh.texCoords)
+               {
+                  mesh.texCoords = new Pointf[nVertices];
+                  if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * nVertices,
+                     d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords, null))
+                     result = false;
+               }
+            }
          }
-         if((mesh.flags.texCoords1) && !d3dMesh.texCoords)
+         else
          {
-            mesh.texCoords = new Pointf[mesh.nVertices];
-            if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * mesh.nVertices,
-               d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords, null))
-               result = false;
+            // New number of vertices, reallocate all current and new features
+            flags |= mesh.flags;
+
+            // Same number of vertices, adding features (Leaves the other features pointers alone)
+            if(flags.vertices)
+            {
+               if(d3dMesh.vertices)
+               {
+                  IDirect3DVertexBuffer9_Release(d3dMesh.vertices);
+                  d3dMesh.vertices = null;
+               }
+               mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
+               if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.vertices, null))
+                  result = false;
+            }
+            if(flags.normals)
+            {
+               if(d3dMesh.normals)
+               {
+                  IDirect3DVertexBuffer9_Release(d3dMesh.normals);
+                  d3dMesh.normals = null;
+               }
+               mesh.normals = renew mesh.normals Vector3Df[nVertices];
+               if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Vector3Df) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.normals, null))
+                  result = false;
+            }
+            if(flags.texCoords1)
+            {
+               if(d3dMesh.texCoords)
+               {
+                  IDirect3DVertexBuffer9_Release(d3dMesh.texCoords);
+                  d3dMesh.texCoords = null;
+               }
+               mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+               if(IDirect3DDevice9_CreateVertexBuffer(d3dDevice, sizeof(Pointf) * nVertices,
+                  d3dSystem.usage, 0, D3DPOOL_MANAGED, &d3dMesh.texCoords, null))
+                  result = false;
+            }
          }
       }
       return result;
index c45f815..31d7e50 100644 (file)
@@ -3661,14 +3661,65 @@ public class LFBDisplayDriver : DisplayDriver
          delete mesh.texCoords;
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       bool result = false;
-
-      if((!mesh.flags.vertices   || mesh.vertices  || (mesh.vertices  = new Vector3Df[mesh.nVertices])) &&
-         (!mesh.flags.normals    || mesh.normals   || (mesh.normals   = new Vector3Df[mesh.nVertices])) &&
-         (!mesh.flags.texCoords1 || mesh.texCoords || (mesh.texCoords = new Pointf  [mesh.nVertices])))
-         result = true;
+      if(mesh.nVertices == 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(!mesh.flags.normals && flags.normals)
+            {
+               if(flags.doubleNormals)
+               {
+                  mesh.normals = (Vector3Df *)new Vector3D[nVertices];
+               }
+               else
+                  mesh.normals = new Vector3Df[nVertices];
+            }
+            if(!mesh.flags.texCoords1 && flags.texCoords1)
+               mesh.texCoords = new Pointf[nVertices];
+            if(!mesh.flags.colors && flags.colors)
+               mesh.colors = new ColorRGBAf[nVertices];
+         }
+      }
+      else
+      {
+         // 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(flags.normals)
+         {
+            if(flags.doubleNormals)
+            {
+               mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
+            }
+            else
+               mesh.normals = renew mesh.normals Vector3Df[nVertices];
+         }
+         if(flags.texCoords1)
+            mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+         if(flags.colors)
+            mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
+      }
       return result;
    }
 
index edc6447..842e326 100644 (file)
@@ -3613,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;
 
@@ -3622,26 +3622,85 @@ class OpenGLDisplayDriver : DisplayDriver
       if(mesh.data)
       {
          OGLMesh oglMesh = mesh.data;
-
-         if(mesh.flags.vertices && !oglMesh.vertices && !mesh.vertices)
-         {
-            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)
+         if(mesh.nVertices == nVertices)
          {
-            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;
       }
@@ -3655,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 );