ecere/gfx3D/Mesh: Temporary Workaround to access OpenGL VBOs
[sdk] / ecere / src / gfx / 3D / Mesh.ec
index db4988a..df33a3b 100644 (file)
@@ -14,7 +14,14 @@ public enum RenderPrimitiveType : PrimitiveGroupType
    quads,
    quadStrip,
    lineStrip
+
+   /* ,
+   lineLoop,
+   lineStrip,
+   polygon
+   */
 };
+
 public class MaterialFlags { public bool doubleSided:1, translucent:1, tile:1, noFog:1, singleSideLight:1; };
 public class Material : struct
 {
@@ -31,6 +38,7 @@ public:
    Bitmap bumpMap;
    Bitmap envMap;
    MaterialFlags flags;
+   float uScale, vScale;
 
    void Free()
    {
@@ -45,13 +53,38 @@ public:
    PrimitiveGroupType type;
    union
    {
-      struct { uint16 * indices; int nIndices; };
+      struct { union { uint16 * indices; uint * indices32; }; int nIndices; };
       struct { int first, nVertices; };
    };
    Material material;
+
 private:
    void * data;
-};   
+};
+
+/*
+public class PrimitiveGroupIndices16 : PrimitiveGroup
+{
+   property Array<uint16> indices
+   {
+      set { }
+   }
+}
+
+public class PrimitiveGroupIndices32 : PrimitiveGroup
+{
+   property Array<uint> indices
+   {
+      set { }
+   }
+}
+
+public class PrimitiveGroupVertexRange : PrimitiveGroup
+{
+   property int first { set { } }
+   property int nVertices { set { } }
+}
+*/
 
 public struct PrimitiveSingle
 {
@@ -59,7 +92,7 @@ public:
    PrimitiveGroupType type;
    union
    {
-      struct { uint16 * indices; int nIndices; };
+      struct { union { uint16 * indices; uint * indices32; }; int nIndices; };
       struct { int first, nVertices; };
    };
    Material material;
@@ -121,14 +154,14 @@ public:
    bool Allocate(MeshFeatures what, int nVertices, DisplaySystem displaySystem)
    {
       bool result = false;
-      if((!nVertices || 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;
          }
@@ -282,12 +315,12 @@ public:
          FillBytes(normals, 0, nVertices * sizeof(Vector3Df));
          for(group = groups.first; group; group = group.next)
          {
-            int nPrimitives = 0, c;
+            int c;
             int offset = 0;
             int strip = 0;
             int nPoints, nIndex;
             uint16 * indices16 = group.indices;
-            uint32 * indices32 = group.indices;
+            uint32 * indices32 = group.indices32;
             bool i32bit = group.type.indices32bit;
 
             if(group.type.primitiveType == triangles)
@@ -319,10 +352,10 @@ public:
 
                if(group.type.primitiveType == triFan)
                {
-                  plane.FromPointsf(vertices[group.indices[0]], 
-                                   vertices[group.indices[c]], 
+                  plane.FromPointsf(vertices[group.indices[0]],
+                                   vertices[group.indices[c]],
                                    vertices[group.indices[c-1]]);
-                  planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z }; 
+                  planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z };
 
                   normals[group.indices[0]].Add(normals[group.indices[0]], planeNormal);
                   numShared[group.indices[0]]++;
@@ -333,10 +366,10 @@ public:
                }
                else if(group.type.primitiveType == triStrip || group.type.primitiveType == quadStrip)
                {
-                  plane.FromPointsf(vertices[group.indices[c-1-strip]], 
-                                   vertices[group.indices[c-2+strip]], 
+                  plane.FromPointsf(vertices[group.indices[c-1-strip]],
+                                   vertices[group.indices[c-2+strip]],
                                    vertices[group.indices[c]]);
-                  planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z }; 
+                  planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z };
 
                   normals[group.indices[c-1-strip]].Add(normals[group.indices[c-1-strip]], planeNormal);
                   numShared[group.indices[c-1-strip]]++;
@@ -351,10 +384,10 @@ public:
                {
                   if(group.type.vertexRange)
                   {
-                     plane.FromPointsf(vertices[c+2], 
-                                      vertices[c+1], 
+                     plane.FromPointsf(vertices[c+2],
+                                      vertices[c+1],
                                       vertices[c]);
-                     planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z }; 
+                     planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z };
 
                      for(i = c; i<c+nIndex; i++)
                      {
@@ -364,10 +397,10 @@ public:
                   }
                   else
                   {
-                     plane.FromPointsf(vertices[i32bit ? indices32[c+2] : indices16[c+2]], 
-                                      vertices[i32bit ? indices32[c+1] : indices16[c+1]], 
+                     plane.FromPointsf(vertices[i32bit ? indices32[c+2] : indices16[c+2]],
+                                      vertices[i32bit ? indices32[c+1] : indices16[c+1]],
                                       vertices[i32bit ? indices32[c] : indices16[c]]);
-                     planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z }; 
+                     planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z };
 
                      for(i = c; i<c+nIndex; i++)
                      {
@@ -387,10 +420,10 @@ public:
 
             Plane plane;
             Vector3Df planeNormal;
-            plane.FromPointsf(vertices[primitive->indices[2]], 
-                             vertices[primitive->indices[1]], 
+            plane.FromPointsf(vertices[primitive->indices[2]],
+                             vertices[primitive->indices[1]],
                              vertices[primitive->indices[0]]);
-            planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z }; 
+            planeNormal = { (float) plane.normal.x, (float) plane.normal.y, (float) plane.normal.z };
 
             if(primitive->material.flags.doubleSided && plane.d < 0)
             {
@@ -591,6 +624,11 @@ public:
       return result;
    }
 
+   void * GetData()
+   {
+      return data;
+   }
+
 private:
 
    void SetMinMaxRadius(void)
@@ -600,16 +638,22 @@ private:
       float xRadius, yRadius, zRadius;
 
       min = { MAXFLOAT, MAXFLOAT, MAXFLOAT };
-      max = { MINFLOAT, MINFLOAT, MINFLOAT };
+      max = {-MAXFLOAT,-MAXFLOAT,-MAXFLOAT };
 
       for(c = 0; c<nVertices; c++)
       {
-         min.x = Min(min.x, vertices[c].x);
-         min.y = Min(min.y, vertices[c].y);
-         min.z = Min(min.z, vertices[c].z);
-         max.x = Max(max.x, vertices[c].x);
-         max.y = Max(max.y, vertices[c].y);
-         max.z = Max(max.z, vertices[c].z);
+         float x = vertices[c].x, y = vertices[c].y, z = vertices[c].z;
+         if(x.isNan || y.isNan || z.isNan);
+         else if(x > 1E20 || x < -1E20 || y > 1E20 || y < -1E20 || z > 1E20 || z < -1E20);
+         else
+         {
+            min.x = Min(min.x, x);
+            min.y = Min(min.y, y);
+            min.z = Min(min.z, z);
+            max.x = Max(max.x, x);
+            max.y = Max(max.y, y);
+            max.z = Max(max.z, z);
+         }
       }
       xRadius = Max(max.x, -min.x);
       yRadius = Max(max.y, -min.y);