ecere/gfx/3D: Updates to Direct3D driver to not crash on bump maps / cube maps
authorJerome St-Louis <jerome@ecere.com>
Wed, 3 Aug 2016 11:49:22 +0000 (07:49 -0400)
committerJerome St-Louis <jerome@ecere.com>
Wed, 3 Aug 2016 11:49:22 +0000 (07:49 -0400)
ecere/src/gfx/3D/Mesh.ec
ecere/src/gfx/3D/meshes/Sphere.ec
ecere/src/gfx/Bitmap.ec
ecere/src/gfx/drivers/Direct3D8DisplayDriver.ec
ecere/src/gfx/drivers/Direct3D9DisplayDriver.ec

index e7c304c..1019b82 100644 (file)
@@ -320,13 +320,14 @@ public:
       double * weightSum = new0 double[nVertices];
       PrimitiveGroup group;
 
-      if(Allocate({ normals = true, tangents = true }, nVertices, displaySystem))
+      if(Allocate({ normals = true, tangents = texCoords != null }, nVertices, displaySystem))
       {
          Vector3Df * normals = this.normals;
          Vector3Df * tangents = this.tangents;
          Pointf * texCoords = this.texCoords;
          FillBytes(normals, 0, nVertices * sizeof(Vector3Df));
-         FillBytes(tangents, 0, 2*nVertices * sizeof(Vector3Df));
+         if(tangents)
+            FillBytes(tangents, 0, 2*nVertices * sizeof(Vector3Df));
          for(group = groups.first; group; group = group.next)
          {
             int c;
@@ -432,7 +433,7 @@ public:
                         weightSum[index] += w;
                         //numShared[index] ++;
 
-                        if(texCoords)
+                        if(tangents)
                         {
                            uint ix0 = index;
                            uint prev = v ? i - 1 : c + nIndex-1;
@@ -495,10 +496,14 @@ public:
          for(c = 0; c<nVertices; c++)
          {
             float s = (float)(1.0 / weightSum[c]); // numShared[c]
-            Vector3Df * n = &normals[c], * t1 = &tangents[2*c], * t2 = &tangents[2*c+1];
+            Vector3Df * n = &normals[c];
             n->Scale(n, s), n->Normalize(n);
-            t1->Scale(t1, s), t1->Normalize(t1);
-            t2->Scale(t2, s), t2->Normalize(t2);
+            if(tangents)
+            {
+               Vector3Df * t1 = &tangents[2*c], * t2 = &tangents[2*c+1];
+               t1->Scale(t1, s), t1->Normalize(t1);
+               t2->Scale(t2, s), t2->Normalize(t2);
+            }
          }
          delete numShared;
          delete weightSum;
index 7670df2..60d5523 100644 (file)
@@ -51,18 +51,21 @@ public:
 
                      pNormals[index] = pVertices[index];
 
-                     pTangents[index*2] =
+                     if(pTangents)
                      {
-                        (float) (sin(theta) * cos(omega - Pi/2));
-                        (float) sin(omega - Pi/2);
-                        (float) (cos(theta) * cos(omega - Pi/2));
-                     };
-                     pTangents[index*2+1] =
-                     {
-                        (float) (sin(theta - Pi/2) * cosOmega);
-                        (float) sin(omega);
-                        (float) (cos(theta - Pi/2) * cosOmega);
-                     };
+                        pTangents[index*2] =
+                        {
+                           (float) (sin(theta) * cos(omega - Pi/2));
+                           (float) sin(omega - Pi/2);
+                           (float) (cos(theta) * cos(omega - Pi/2));
+                        };
+                        pTangents[index*2+1] =
+                        {
+                           (float) (sin(theta - Pi/2) * cosOmega);
+                           (float) sin(omega);
+                           (float) (cos(theta - Pi/2) * cosOmega);
+                        };
+                     }
 
                      //pTangents[index*2 + 1].CrossProduct(pNormals[index], pTangents[index*2]);
                      pTexCoords[index] = { (float)l / (w-1), (float)lat / (numLat) };
index c1d3725..4e85b2e 100644 (file)
@@ -949,10 +949,11 @@ public:
 public class CubeMap : Bitmap
 {
 public:
-   void Load(DisplaySystem displaySystem, const String * names, const String extension, bool oldStyle)
+   bool Load(DisplaySystem displaySystem, const String * names, const String extension, bool oldStyle)
    {
       int i;
-      for(i = 0; i < 6; i++)
+      bool result = true;
+      for(i = 0; result && i < 6; i++)
       {
          char location[MAX_LOCATION];
          Bitmap face = i > 0 ? { } : this;
@@ -962,13 +963,16 @@ public:
          if(face.Load(location, null, null))
          {
             face.driverData = driverData;
-            displaySystem.driver.MakeDDBitmap(displaySystem, face, true, (i + 1) | (oldStyle << 3));
+            result = displaySystem.driver.MakeDDBitmap(displaySystem, face, true, (i + 1) | (oldStyle << 3));
          }
+         else
+            result = false;
          if(i > 0)
          {
             face.driverData = 0;
             delete face;
          }
       }
+      return result;
    }
 };
index de87cb4..567e81e 100644 (file)
@@ -677,7 +677,7 @@ class Direct3D8DisplayDriver : DisplayDriver
    {
       bool result = false;
       D3D8System d3dSystem = displaySystem.driverData;
-      if(bitmap.Convert(null, pixelFormat888, null))
+      if(cubeMapFace && bitmap.Convert(null, pixelFormat888, null))
       {
          IDirect3DTexture8 * texture;
          uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));
index b57cfa1..b21d4c9 100644 (file)
@@ -675,7 +675,7 @@ class Direct3D9DisplayDriver : DisplayDriver
    {
       bool result = false;
       D3DSystem d3dSystem = displaySystem.driverData;
-      if(bitmap.Convert(null, pixelFormat888, null))
+      if(!cubeMapFace && bitmap.Convert(null, pixelFormat888, null))
       {
          IDirect3DTexture9 * texture;
          uint w = pow2i(Min(bitmap.width, 512)), h = pow2i(Min(bitmap.height, 512));