Explorar o código

Fixed IndexArrayOutOfBound that may occur in BatchNode.Also optimized temp array allocation in case there is no tangents buffer

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8965 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
rem..om %!s(int64=14) %!d(string=hai) anos
pai
achega
93a181b4c7
Modificáronse 1 ficheiros con 24 adicións e 19 borrados
  1. 24 19
      engine/src/core/com/jme3/scene/BatchNode.java

+ 24 - 19
engine/src/core/com/jme3/scene/BatchNode.java

@@ -78,6 +78,8 @@ public class BatchNode extends Node implements Savable {
     private float[] tmpFloat;
     private float[] tmpFloat;
     private float[] tmpFloatN;
     private float[] tmpFloatN;
     private float[] tmpFloatT;
     private float[] tmpFloatT;
+    int maxVertCount = 0;
+    boolean useTangents = false;
 
 
     /**
     /**
      * Construct a batchNode
      * Construct a batchNode
@@ -178,7 +180,7 @@ public class BatchNode extends Node implements Savable {
     protected void doBatch() {
     protected void doBatch() {
         ///List<Geometry> tmpList = new ArrayList<Geometry>();
         ///List<Geometry> tmpList = new ArrayList<Geometry>();
         Map<Material, List<Geometry>> matMap = new HashMap<Material, List<Geometry>>();
         Map<Material, List<Geometry>> matMap = new HashMap<Material, List<Geometry>>();
-
+        maxVertCount = 0;
         gatherGeomerties(matMap, this);
         gatherGeomerties(matMap, this);
         batches.clear();
         batches.clear();
         int nbGeoms = 0;
         int nbGeoms = 0;
@@ -200,6 +202,12 @@ public class BatchNode extends Node implements Savable {
             batch.geometry.getMesh().updateBound();
             batch.geometry.getMesh().updateBound();
             batches.put(material, batch);
             batches.put(material, batch);
         }
         }
+        //init temp float arrays
+        tmpFloat = new float[maxVertCount * 3];
+        tmpFloatN = new float[maxVertCount * 3];
+        if (useTangents) {
+            tmpFloatT = new float[maxVertCount * 4];
+        }
         logger.log(Level.INFO, "Batched {0} geometries in {1} batches.", new Object[]{nbGeoms, batches.size()});
         logger.log(Level.INFO, "Batched {0} geometries in {1} batches.", new Object[]{nbGeoms, batches.size()});
     }
     }
 
 
@@ -352,7 +360,6 @@ public class BatchNode extends Node implements Savable {
         int totalVerts = 0;
         int totalVerts = 0;
         int totalTris = 0;
         int totalTris = 0;
         int totalLodLevels = 0;
         int totalLodLevels = 0;
-        int maxVertCount = 0;
 
 
         Mesh.Mode mode = null;
         Mesh.Mode mode = null;
         for (Geometry geom : geometries) {
         for (Geometry geom : geometries) {
@@ -415,9 +422,9 @@ public class BatchNode extends Node implements Savable {
 
 
             Buffer data;
             Buffer data;
             if (i == VertexBuffer.Type.Index.ordinal()) {
             if (i == VertexBuffer.Type.Index.ordinal()) {
-                data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);                
+                data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
             } else {
             } else {
-                data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);                
+                data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
             }
             }
 
 
             VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
             VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
@@ -464,6 +471,9 @@ public class BatchNode extends Node implements Savable {
                     FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                     FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                     FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                     FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                     doCopyBuffer(inPos, globalVertIndex, outPos);
                     doCopyBuffer(inPos, globalVertIndex, outPos);
+                    if (VertexBuffer.Type.Tangent.ordinal() == bufType) {
+                        useTangents = true;
+                    }
                 } else {
                 } else {
                     for (int vert = 0; vert < geomVertCount; vert++) {
                     for (int vert = 0; vert < geomVertCount; vert++) {
                         int curGlobalVertIndex = globalVertIndex + vert;
                         int curGlobalVertIndex = globalVertIndex + vert;
@@ -475,9 +485,6 @@ public class BatchNode extends Node implements Savable {
             globalVertIndex += geomVertCount;
             globalVertIndex += geomVertCount;
             globalTriIndex += geomTriCount;
             globalTriIndex += geomTriCount;
         }
         }
-        tmpFloat = new float[maxVertCount * 3];
-        tmpFloatN = new float[maxVertCount * 3];
-        tmpFloatT = new float[maxVertCount * 4];
     }
     }
 
 
     private void doTransforms(FloatBuffer bufPos, FloatBuffer bufNorm, int start, int end, Matrix4f transform) {
     private void doTransforms(FloatBuffer bufPos, FloatBuffer bufNorm, int start, int end, Matrix4f transform) {
@@ -491,7 +498,7 @@ public class BatchNode extends Node implements Savable {
         // convert to be in component units
         // convert to be in component units
         int offset = start * 3;
         int offset = start * 3;
         bufPos.position(offset);
         bufPos.position(offset);
-        bufNorm.position(offset);     
+        bufNorm.position(offset);
         bufPos.get(tmpFloat, 0, length);
         bufPos.get(tmpFloat, 0, length);
         bufNorm.get(tmpFloatN, 0, length);
         bufNorm.get(tmpFloatN, 0, length);
         int index = 0;
         int index = 0;
@@ -537,14 +544,14 @@ public class BatchNode extends Node implements Savable {
         // convert to be in component units
         // convert to be in component units
         int offset = start * 3;
         int offset = start * 3;
         int tanOffset = start * 4;
         int tanOffset = start * 4;
-        
+
         bufPos.position(offset);
         bufPos.position(offset);
         bufNorm.position(offset);
         bufNorm.position(offset);
         bufTangents.position(tanOffset);
         bufTangents.position(tanOffset);
         bufPos.get(tmpFloat, 0, length);
         bufPos.get(tmpFloat, 0, length);
-        bufNorm.get(tmpFloatN, 0, length);               
+        bufNorm.get(tmpFloatN, 0, length);
         bufTangents.get(tmpFloatT, 0, tanLength);
         bufTangents.get(tmpFloatT, 0, tanLength);
-        
+
         int index = 0;
         int index = 0;
         int tanIndex = 0;
         int tanIndex = 0;
         while (index < length) {
         while (index < length) {
@@ -554,11 +561,11 @@ public class BatchNode extends Node implements Savable {
             norm.y = tmpFloatN[index++];
             norm.y = tmpFloatN[index++];
             pos.z = tmpFloat[index];
             pos.z = tmpFloat[index];
             norm.z = tmpFloatN[index];
             norm.z = tmpFloatN[index];
-            
+
             tan.x = tmpFloatT[tanIndex++];
             tan.x = tmpFloatT[tanIndex++];
             tan.y = tmpFloatT[tanIndex++];
             tan.y = tmpFloatT[tanIndex++];
             tan.z = tmpFloatT[tanIndex++];
             tan.z = tmpFloatT[tanIndex++];
-                        
+
 
 
             transform.mult(pos, pos);
             transform.mult(pos, pos);
             transform.multNormal(norm, norm);
             transform.multNormal(norm, norm);
@@ -566,18 +573,18 @@ public class BatchNode extends Node implements Savable {
 
 
             index -= 2;
             index -= 2;
             tanIndex -= 3;
             tanIndex -= 3;
-            
+
             tmpFloat[index] = pos.x;
             tmpFloat[index] = pos.x;
             tmpFloatN[index++] = norm.x;
             tmpFloatN[index++] = norm.x;
             tmpFloat[index] = pos.y;
             tmpFloat[index] = pos.y;
             tmpFloatN[index++] = norm.y;
             tmpFloatN[index++] = norm.y;
             tmpFloat[index] = pos.z;
             tmpFloat[index] = pos.z;
             tmpFloatN[index++] = norm.z;
             tmpFloatN[index++] = norm.z;
-            
+
             tmpFloatT[tanIndex++] = tan.x;
             tmpFloatT[tanIndex++] = tan.x;
             tmpFloatT[tanIndex++] = tan.y;
             tmpFloatT[tanIndex++] = tan.y;
             tmpFloatT[tanIndex++] = tan.z;
             tmpFloatT[tanIndex++] = tan.z;
-            
+
             tanIndex++;
             tanIndex++;
 
 
         }
         }
@@ -587,14 +594,12 @@ public class BatchNode extends Node implements Savable {
         bufPos.put(tmpFloat, 0, length);
         bufPos.put(tmpFloat, 0, length);
         bufNorm.position(offset);
         bufNorm.position(offset);
         //using bulk put as it's faster
         //using bulk put as it's faster
-        bufNorm.put(tmpFloatN, 0, length);        
+        bufNorm.put(tmpFloatN, 0, length);
         bufTangents.position(tanOffset);
         bufTangents.position(tanOffset);
         //using bulk put as it's faster
         //using bulk put as it's faster
         bufTangents.put(tmpFloatT, 0, tanLength);
         bufTangents.put(tmpFloatT, 0, tanLength);
     }
     }
 
 
-  
-
     private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf) {
     private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf) {
         TempVars vars = TempVars.get();
         TempVars vars = TempVars.get();
         Vector3f pos = vars.vect1;
         Vector3f pos = vars.vect1;