Browse Source

Fix issue #1197: ClassCastException in LODGeomap.createMesh() (#1198)

Ali-RS 6 years ago
parent
commit
b1db497a00
1 changed files with 13 additions and 11 deletions
  1. 13 11
      jme3-terrain/src/main/java/com/jme3/terrain/geomipmap/LODGeomap.java

+ 13 - 11
jme3-terrain/src/main/java/com/jme3/terrain/geomipmap/LODGeomap.java

@@ -45,8 +45,8 @@ import com.jme3.terrain.GeoMap;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.TempVars;
 import com.jme3.util.TempVars;
 import java.io.IOException;
 import java.io.IOException;
-import java.nio.Buffer;
 import java.nio.BufferUnderflowException;
 import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.FloatBuffer;
 import java.nio.IntBuffer;
 import java.nio.IntBuffer;
 import java.nio.ShortBuffer;
 import java.nio.ShortBuffer;
@@ -81,12 +81,7 @@ public class LODGeomap extends GeoMap {
         FloatBuffer pb = writeVertexArray(null, scale, center);
         FloatBuffer pb = writeVertexArray(null, scale, center);
         FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
         FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
         FloatBuffer nb = writeNormalArray(null, scale);
         FloatBuffer nb = writeNormalArray(null, scale);
-        Buffer ib;
-        IndexBuffer idxB = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
-        if (idxB.getBuffer() instanceof IntBuffer)
-            ib = (IntBuffer)idxB.getBuffer();
-        else
-            ib = (ShortBuffer)idxB.getBuffer();
+        IndexBuffer ib = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
         FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
         FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
         FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
         FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
         writeTangentArray(nb, tanb, bb, texb, scale);
         writeTangentArray(nb, tanb, bb, texb, scale);
@@ -97,10 +92,17 @@ public class LODGeomap extends GeoMap {
         m.setBuffer(Type.Tangent, 3, tanb);
         m.setBuffer(Type.Tangent, 3, tanb);
         m.setBuffer(Type.Binormal, 3, bb);
         m.setBuffer(Type.Binormal, 3, bb);
         m.setBuffer(Type.TexCoord, 2, texb);
         m.setBuffer(Type.TexCoord, 2, texb);
-        if (ib instanceof IntBuffer)
-            m.setBuffer(Type.Index, 3, (IntBuffer)ib);
-        else if (ib instanceof ShortBuffer)
-            m.setBuffer(Type.Index, 3, (ShortBuffer)ib);
+        switch (ib.getFormat()) {
+            case UnsignedInt:
+                m.setBuffer(Type.Index, 3, (IntBuffer) ib.getBuffer());
+                break;
+            case UnsignedShort:
+                m.setBuffer(Type.Index, 3, (ShortBuffer) ib.getBuffer());
+                break;
+            case UnsignedByte:
+                m.setBuffer(Type.Index, 3, (ByteBuffer) ib.getBuffer());
+                break;
+        }
         m.setStatic();
         m.setStatic();
         m.updateBound();
         m.updateBound();
         return m;
         return m;