OSDN Git Service

fix MeshConverter bug
authorKazuhiko Kobayashi <chototsu_moushinp@yahoo.co.jp>
Sun, 25 Sep 2011 02:10:29 +0000 (11:10 +0900)
committerKazuhiko Kobayashi <chototsu_moushinp@yahoo.co.jp>
Sun, 25 Sep 2011 02:10:29 +0000 (11:10 +0900)
remove all memory allocation code in rendering loop

12 files changed:
src/projectkyoto/jme3/mmd/PMDNode.java
src/projectkyoto/jme3/mmd/ik/IKControl.java
src/projectkyoto/jme3/mmd/nativebullet/PMDPhysicsWorld.java
src/projectkyoto/jme3/mmd/nativebullet/PMDRigidBody.java
src/projectkyoto/jme3/mmd/vmd/VMDControl.java
src/projectkyoto/mmd/file/PMDMaterial.java
src/projectkyoto/mmd/file/PMDVertex.java
src/projectkyoto/mmd/file/XColorRGB.java
src/projectkyoto/mmd/file/XColorRGBA.java
src/projectkyoto/mmd/file/util2/MeshConverter.java
src/projectkyoto/mmd/file/util2/MeshData.java
src/projectkyoto/mmd/file/util2/SkinMeshData.java

index 71de605..db6f39e 100755 (executable)
@@ -75,6 +75,7 @@ public class PMDNode extends Node {
     PMDSkinMesh[] skinTargets;
     PMDGeometry[] pmdGeometryArray;
     Map<String, Skin> skinMap = new HashMap<String, Skin>();
+    Skin[] skinArray = new Skin[0];
     javax.vecmath.Vector3f skinPosArray[];
     javax.vecmath.Vector3f skinNormalArray[];
     javax.vecmath.Vector3f skinPosArrayOrig[];
@@ -148,7 +149,7 @@ public class PMDNode extends Node {
         offsetMatrices = skeleton.computeSkinningMatrices();
         return offsetMatrices;
     }
-
+boolean setBoneMatricesFlag = true;
     public void update() {
 //        skeleton.reset(); // reset skeleton to bind pose
         if (true /*
@@ -217,8 +218,8 @@ public class PMDNode extends Node {
         skinTargets[0].skinvb2.setUpdateNeeded();
         skinTargets[0].skinnb2.setUpdateNeeded();
         for(PMDSkinMesh skinMesh : skinTargets) {
-            skinMesh.clearBuffer(Type.Position);
-            skinMesh.clearBuffer(Type.Normal);
+//            skinMesh.clearBuffer(Type.Position);
+//            skinMesh.clearBuffer(Type.Normal);
             skinMesh.setBuffer(skinTargets[0].getSkinvb2());
             skinMesh.setBuffer(skinTargets[0].getSkinnb2());
         }
@@ -332,7 +333,7 @@ public class PMDNode extends Node {
         for(int i=skinPosArray.length-1;i>=0;i--) {
             skinPosArray[i].set(skinPosArrayOrig[i]);
         }
-        for (Skin skin : skinMap.values()) {
+        for (Skin skin : skinArray) {
             if (true || skin.isUpdateNeeded()) {
                 if (skin.getWeight() != 0f) {
                     for (PMDSkinVertData svd : skin.getSkinData().getSkinVertData()) {
@@ -401,7 +402,7 @@ public class PMDNode extends Node {
         VertexBuffer nb = skinMesh.getBuffer(Type.Normal);
         FloatBuffer fnb = (FloatBuffer) nb.getData();
 
-        for (Skin skin : skinMap.values()) {
+        for (Skin skin : skinArray) {
             if (skin.isUpdateNeeded()) {
                 if (skin.getWeight() != 1f) {
                     for (PMDSkinVertData svd : skin.getSkinData().getSkinVertData()) {
@@ -487,7 +488,7 @@ public class PMDNode extends Node {
         for (PMDMesh mesh : targets) {
             resetToBind(mesh);
         }
-        for (Skin skin : skinMap.values()) {
+        for (Skin skin : skinArray) {
             skin.setWeight(0f);
         }
         setUpdateNeeded(true);
@@ -815,6 +816,7 @@ public class PMDNode extends Node {
                 skin = skin.clone();
                 newPMDNode.skinMap.put(skinName, skin);
             }
+            newPMDNode.skinArray = newPMDNode.skinMap.values().toArray(new Skin[newPMDNode.skinMap.size()]);
             newPMDNode.skinPosArray = new javax.vecmath.Vector3f[skinPosArray.length];
             for(int i=0;i<skinPosArray.length;i++) {
                 newPMDNode.skinPosArray[i] = new javax.vecmath.Vector3f(skinPosArray[i]);
index 9c4a33b..d6466cd 100755 (executable)
@@ -165,7 +165,7 @@ public class IKControl extends AbstractControl{
                                 }
                                 axis.normalizeLocal();
 //            System.out.println("axis = " + axis);
-                                rot.fromAngleAxis(angle, axis);
+                                rot.fromAngleNormalAxis(angle, axis);
 //            rot.normalize();
                                 if (hizaFlag) {
                                     if (iterationCount == -1) {
@@ -314,7 +314,7 @@ public class IKControl extends AbstractControl{
                 Vector3f axis = effectorBonePos.cross(targetBonePos, tmpV5);
                 axis.normalizeLocal();
                 Quaternion rot = tmpQ1;
-                rot.fromAngleAxis(angle, axis);
+                rot.fromAngleNormalAxis(angle, axis);
 //                rot.multLocal(ashiBone.getLocalRotation());
 //                ashiBone.getLocalRotation().set(rot);
                 ashiBone.getLocalRotation().multLocal(rot);
index 45a6ace..18ac7f1 100755 (executable)
@@ -64,6 +64,7 @@ public class PMDPhysicsWorld {
     static final Object lockObject = new Object();
     PhysicsSpace physicsSpace;
     Map<PMDNode, PMDRigidBody[]> rigidBodyMap = new HashMap<PMDNode, PMDRigidBody[]>();
+    PMDRigidBody[][] nodeRigidBodyArray = new PMDRigidBody[0][];
     Map<PMDNode, SixDofJoint[]> constraintMap = new HashMap<PMDNode, SixDofJoint[]>();
     float accuracy = 1f / 120;
 
@@ -131,6 +132,7 @@ public class PMDPhysicsWorld {
             constArray[i] = constraint;
             physicsSpace.add(constraint);
         }
+        nodeRigidBodyArray = rigidBodyMap.values().toArray(new PMDRigidBody[rigidBodyMap.size()][]);
 //        physicsSpace.update(1 / 60f, 1);
     }
 
@@ -459,7 +461,7 @@ public class PMDPhysicsWorld {
     }
 
     public void updateKinematicPos() {
-        for (PMDRigidBody rbarray[] : rigidBodyMap.values()) {
+        for (PMDRigidBody rbarray[] : nodeRigidBodyArray) {
             for (int i = 0; i < rbarray.length; i++) {
                 PMDRigidBody rb = rbarray[i];
                 PMDNode pmdNode = rb.getPmdNode();
@@ -513,7 +515,7 @@ public class PMDPhysicsWorld {
 //            CollisionObject obj = btWorld.getCollisionObjectArray().getQuick(i);
 //            if (obj instanceof PMDRigidBody) {
 //                PMDRigidBody rb = (PMDRigidBody)obj;
-        for (PMDRigidBody rbarray[] : rigidBodyMap.values()) {
+        for (PMDRigidBody rbarray[] : nodeRigidBodyArray) {
             for (int i = 0; i < rbarray.length; i++) {
                 PMDRigidBody rb = rbarray[i];
                 if (/*
@@ -543,7 +545,7 @@ public class PMDPhysicsWorld {
         }
     }
     public void resetRigidBodyPos() {
-        for (PMDRigidBody rbarray[] : rigidBodyMap.values()) {
+        for (PMDRigidBody rbarray[] : nodeRigidBodyArray) {
             for (int i = 0; i < rbarray.length; i++) {
                 PMDRigidBody rb = rbarray[i];
                 if (true) {
@@ -557,7 +559,7 @@ public class PMDPhysicsWorld {
         }
     }
     public void updateRigidBodyPos() {
-        for (PMDRigidBody rbarray[] : rigidBodyMap.values()) {
+        for (PMDRigidBody rbarray[] : nodeRigidBodyArray) {
             for (int i = 0; i < rbarray.length; i++) {
                 PMDRigidBody rb = rbarray[i];
                 if (true) {
index 1c08bc2..0fec652 100755 (executable)
@@ -32,6 +32,7 @@ package projectkyoto.jme3.mmd.nativebullet;
 import com.jme3.animation.Bone;
 import com.jme3.bullet.collision.shapes.CollisionShape;
 import com.jme3.bullet.objects.PhysicsRigidBody;
+import com.jme3.math.Matrix3f;
 import com.jme3.math.Matrix4f;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector3f;
@@ -91,26 +92,31 @@ public class PMDRigidBody extends PhysicsRigidBody {
 
     public void updateFromBoneMatrix() {
         if (bone != null) {
-            tmpV.set(bone.getModelSpacePosition());
-            tmpV.addLocal(pos);
-            tmpQ.set(bone.getModelSpaceRotation());
-            tmpQ.multLocal(rot);
+//            tmpV.set(bone.getModelSpacePosition());
+//            tmpV.addLocal(pos);
+//            tmpQ.set(bone.getModelSpaceRotation());
+//            tmpQ.multLocal(rot);
             m2.setTranslation(bone.getModelSpacePosition());
             m2.setRotationQuaternion(bone.getModelSpaceRotation());
             m2.multLocal(m);
-            super.setPhysicsLocation(m2.toTranslationVector());
-            super.setPhysicsRotation(m2.toRotationQuat());
+            m2.toRotationMatrix(tmpMatrix3f);
+            tmpQ.fromRotationMatrix(tmpMatrix3f);
+            super.setPhysicsRotation(tmpQ);
+            m2.toTranslationVector(tmpV);
+            super.setPhysicsLocation(tmpV);
         } else {
-            tmpV.set(centerBone.getModelSpacePosition());
-            tmpV.addLocal(pos);
-            tmpQ.set(centerBone.getModelSpaceRotation());
-            tmpQ.multLocal(rot);
+//            tmpV.set(centerBone.getModelSpacePosition());
+//            tmpV.addLocal(pos);
+//            tmpQ.set(centerBone.getModelSpaceRotation());
+//            tmpQ.multLocal(rot);
             m2.setTranslation(centerBone.getModelSpacePosition());
             m2.setRotationQuaternion(centerBone.getModelSpaceRotation());
             m2.multLocal(m);
-            super.setPhysicsRotation(m2.toRotationQuat());
-            super.setPhysicsLocation(m2.toTranslationVector());
-
+            m2.toRotationMatrix(tmpMatrix3f);
+            tmpQ.fromRotationMatrix(tmpMatrix3f);
+            super.setPhysicsRotation(tmpQ);
+            m2.toTranslationVector(tmpV);
+            super.setPhysicsLocation(tmpV);
         }
 //        System.out.println("objectId = "+objectId+(bone != null ? " name = "+bone.getName() : "")+" pos = "+getPhysicsLocation());
     }
@@ -122,21 +128,24 @@ public class PMDRigidBody extends PhysicsRigidBody {
     }
     Matrix4f m2 = new Matrix4f();
     Matrix4f m3 = new Matrix4f();
+    Matrix3f tmpMatrix3f = new Matrix3f();
 
     public void updateToBoneMatrix() {
 //        System.out.println("objectId = "+objectId+" name = "+bone.getName()+" pos = "+getPhysicsLocation());
         if (bone != null) {
             if (rigidBodyType == 2) {
                 if (true/*!bone.getName().contains("センター")*/) {
-                    tmpV.set(super.getPhysicsLocation());
-                    tmpQ.set(super.getPhysicsRotation());
+                    super.getPhysicsLocation(tmpV);
+                    super.getPhysicsRotation(tmpQ);
                     m2.setRotationQuaternion(tmpQ);
                     m2.setTranslation(tmpV);
                     m2.multLocal(invM);
 //                System.out.println("updateToBoneMatrix:tmpV = "+tmpV);
 //                tmpV.addLocal(invPos);
 //                tmpQ.multLocal(invRot);
-                    bone.getModelSpaceRotation().set(m2.toRotationQuat());
+                    m2.toRotationMatrix(tmpMatrix3f);
+                    bone.getModelSpaceRotation().fromRotationMatrix(tmpMatrix3f);
+//                    m2.toRotationQuat(bone.getModelSpaceRotation());
 //                bone.getModelSpacePosition().set(m2.toTranslationVector());
 //                    updateFromBoneMatrix();
 //                super.getPhysicsLocation();
@@ -144,15 +153,17 @@ public class PMDRigidBody extends PhysicsRigidBody {
                 }
             } else {
                 if (rigidBodyType == 1 && !centerFlag) {
-                    tmpV.set(super.getPhysicsLocation());
-                    tmpQ.set(super.getPhysicsRotation());
+                    super.getPhysicsLocation(tmpV);
+                    super.getPhysicsRotation(tmpQ);
                     m2.setRotationQuaternion(tmpQ);
                     m2.setTranslation(tmpV);
                     m2.multLocal(invM);
 //                tmpV.addLocal(invPos);
 //                tmpQ.multLocal(invRot);
-                    bone.getModelSpaceRotation().set(m2.toRotationQuat());
-                    bone.getModelSpacePosition().set(m2.toTranslationVector());
+                    m2.toRotationMatrix(tmpMatrix3f);
+                    bone.getModelSpaceRotation().fromRotationMatrix(tmpMatrix3f);
+//                    m2.toRotationQuat(bone.getModelSpaceRotation());
+                    m2.toTranslationVector(bone.getModelSpacePosition());
                 }
             }
         }
index 247dd5f..1104950 100755 (executable)
@@ -227,7 +227,7 @@ public class VMDControl extends AbstractControl {
 //        }
 //        tpf = stepTime;
 //        tpf = 1f/15f;
-        boolean needUpdateSkin = true;
+        boolean needUpdateSkin = false;
         if (tpf != 0 && !pause) {
             tpf += prevTpf;
             for(;tpf > accuracy ; tpf -= accuracy ) {
@@ -238,7 +238,7 @@ public class VMDControl extends AbstractControl {
             physicsControl.getWorld().applyResultToBone();
             prevTpf = tpf;
             if (needUpdateSkin) {
-//                resetSkins();
+                resetSkins();
                 calcSkins();
             }
         }
index 193b881..ed6a2dc 100755 (executable)
@@ -145,9 +145,6 @@ public class PMDMaterial {
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
         if (obj == null) {
             return false;
         }
@@ -170,6 +167,8 @@ public class PMDMaterial {
         return true;
     }
 
+
+
     @Override
     public int hashCode() {
         int hash = 7;
index 02f0b77..32f680c 100755 (executable)
@@ -133,6 +133,38 @@ public class PMDVertex {
 
     @Override
     public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final PMDVertex other = (PMDVertex) obj;
+        if (this.pos != other.pos && (this.pos == null || !this.pos.equals(other.pos))) {
+            return false;
+        }
+        if (this.normal != other.normal && (this.normal == null || !this.normal.equals(other.normal))) {
+            return false;
+        }
+        if (this.uv != other.uv && (this.uv == null || !this.uv.equals(other.uv))) {
+            return false;
+        }
+        if (this.boneNum1 != other.boneNum1) {
+            return false;
+        }
+        if (this.boneNum2 != other.boneNum2) {
+            return false;
+        }
+        if (this.boneWeight != other.boneWeight) {
+            return false;
+        }
+        if (this.edgeFlag != other.edgeFlag) {
+            return false;
+        }
+        return true;
+    }
+
+    public boolean equals2(Object obj) {
         if (this == obj) {
             return true;
         }
index 025fac2..f08d0b7 100755 (executable)
@@ -91,4 +91,34 @@ public class XColorRGB {
                 +" blue = "+green
                 +"}";
     }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final XColorRGB other = (XColorRGB) obj;
+        if (Float.floatToIntBits(this.red) != Float.floatToIntBits(other.red)) {
+            return false;
+        }
+        if (Float.floatToIntBits(this.green) != Float.floatToIntBits(other.green)) {
+            return false;
+        }
+        if (Float.floatToIntBits(this.blue) != Float.floatToIntBits(other.blue)) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = 5;
+        hash = 41 * hash + Float.floatToIntBits(this.red);
+        hash = 41 * hash + Float.floatToIntBits(this.green);
+        hash = 41 * hash + Float.floatToIntBits(this.blue);
+        return hash;
+    }
 }
index 5c891eb..1dfa301 100755 (executable)
@@ -71,4 +71,21 @@ public class XColorRGBA extends XColorRGB {
                 + " alpha = " + alpha
                 + "}";
     }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!super.equals(obj)) {
+            return false;
+        }
+        final XColorRGBA other = (XColorRGBA) obj;
+        if (Float.floatToIntBits(this.alpha) != Float.floatToIntBits(other.alpha)) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return super.hashCode();
+    }
 }
index 8c8e71e..6371958 100755 (executable)
@@ -55,7 +55,7 @@ public class MeshConverter {
     List<MeshData> meshDataList = new ArrayList<MeshData>();
     SkinMeshData skinMeshData;
     HashMap<PMDVertex, Integer> meshTmpVertMap = new HashMap<PMDVertex, Integer>();
-    HashMap<PMDVertex, Integer> skinTmpVertMap = new HashMap<PMDVertex, Integer>();
+    HashMap<Integer, Integer> skinTmpVertMap = new HashMap<Integer, Integer>();
 
     public MeshConverter(PMDModel model) {
         this.model = model;
@@ -93,11 +93,25 @@ public class MeshConverter {
         int faceVertNo = 0;
         for (int materialNo = 0; materialNo < model.getMaterialCount(); materialNo++) {
             PMDMaterial material = model.getMaterial()[materialNo];
+            // find same material
+            MeshData meshData = new MeshData(model, maxBoneSize, material);
+            for(int meshIndex = meshDataList.size()-1;meshIndex >=0;meshIndex--) {
+                PMDMaterial material2 = meshDataList.get(meshIndex).getMaterial();
+                if (material.equals(material2)) {
+                    meshData = meshDataList.get(meshIndex);
+                    for(int i=meshData.getVertexList().size()-1;i>=0;i--) {
+                        PMDVertex v = meshData.getVertexList().get(i);
+                        meshTmpVertMap.put(v, i);
+                    }
+                    break;
+                }
+            }
             if (material.getFaceVertCount() == 0) {
                 continue;
             }
-            MeshData meshData = new MeshData(model, maxBoneSize, material);
-            meshDataList.add(meshData);
+            if (!meshDataList.contains(meshData)) {
+                meshDataList.add(meshData);
+            }
             for (int materialFaceVertNo = 0; materialFaceVertNo < material.getFaceVertCount(); materialFaceVertNo += 3) {
                 int i1 = model.getFaceVertIndex()[faceVertNo++];
                 int i2 = model.getFaceVertIndex()[faceVertNo++];
@@ -136,6 +150,7 @@ public class MeshConverter {
         for(MeshData meshData : meshDataList) {
 //            meshData.printTrinangles();
         }
+        System.out.println("meshDataCount = "+meshDataList.size());
     }
     void printMeshData(MeshData meshData) {
             System.out.println("vertSize = " + meshData.getVertexList().size()
index e378496..72db47b 100755 (executable)
@@ -90,7 +90,7 @@ public class MeshData {
         } else {
             newVertIndex = vertexList.size();
             vertexList.add(v);
-            mc.meshTmpVertMap.put(v, index);
+            mc.meshTmpVertMap.put(v, newVertIndex);
         }
         indexList.add(newVertIndex);
     }
@@ -147,4 +147,19 @@ public class MeshData {
             System.out.println(v);
         }
     }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final MeshData other = (MeshData) obj;
+        if (this != obj) {
+            return false;
+        }
+        return true;
+    }
 }
index d484d94..057579a 100755 (executable)
@@ -59,7 +59,7 @@ public class SkinMeshData {
                 for(int i=0;i<sd.getSkinVertCount();i++) {
                     PMDVertex v = model.getVertexList()[sd.getSkinVertData()[i].getSkinVertIndex()];
                     vertexList.add(v);
-                    mc.skinTmpVertMap.put(v, i);
+                    mc.skinTmpVertMap.put(sd.getSkinVertData()[i].getSkinVertIndex(), i);
                 }
             }
         }
@@ -91,14 +91,14 @@ public class SkinMeshData {
 
     private void addVertex(MeshConverter mc, List<Integer>indexList, int vertIndex) {
         PMDVertex v = model.getVertexList()[vertIndex];
-        Integer index = mc.skinTmpVertMap.get(v);
+        Integer index = mc.skinTmpVertMap.get(vertIndex);
         int newVertIndex;
         if (index != null /*vertexList.contains(v)*/) {
             newVertIndex = index.intValue(); //vertexList.indexOf(v);
         } else {
             newVertIndex = vertexList.size();
             vertexList.add(v);
-            mc.skinTmpVertMap.put(v, newVertIndex);
+            mc.skinTmpVertMap.put(vertIndex, newVertIndex);
             index = newVertIndex;
         }
         indexList.add(index/*newVertIndex*/);