Rev 1817 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid2.graphics.model;
import com.gebauz.bauzoid2.game.Engine;
import com.gebauz.bauzoid2.graphics.util.Transform;
import com.gebauz.bauzoid2.math.Matrix4;
import com.gebauz.bauzoid2.math.Quaternion;
import com.gebauz.bauzoid2.math.Vector3;
/**
* Created by chris on 14.12.2014.
*/
public class ModelPart
{
// Constants========================================================================================
// Fields===========================================================================================
private String mName =
null;
private MeshGroup mMeshGroup =
null;
private Material mMaterial =
null;
private ModelNode
[] mBoneInfluences =
null;
private Transform
[] mBoneTransforms =
null; // bind pose?
private Matrix4
[] mBoneTransformMatrices =
null;
public com.
badlogic.
gdx.
math.
Matrix4[] mGdxBones =
null;
// Methods==========================================================================================
public ModelPart
(String name, MeshGroup group, Material material
)
{
mName = name
;
mMeshGroup = group
;
mMaterial = material
;
}
public Matrix4
[] calcBoneMatrices
()
{
Matrix4
[] matrices =
new Matrix4
[mBoneInfluences.
length];
for (int i =
0; i
< matrices.
length; i++
)
{
ModelNode bone = mBoneInfluences
[i
];
//Quaternion q = new Quaternion(0.5f, -0.5f, 0.5f, 0.5f);
matrices
[i
] =
new Matrix4
();
/*matrices[i].identity();
matrices[i].multiply(bone.getGlobalTransform());
Matrix4 a = new Matrix4();
bone.getGlobalTransform().copyTo(a);
Matrix4 b = new Matrix4();
mBoneTransformMatrices[i].copyTo(b);
matrices[i].multiply(mBoneTransformMatrices[i].getInverse());*/
/*Matrix4 test = new Matrix4();
test.copyFrom(bone.getGlobalTransform());
test.multiply(mBoneTransformMatrices[i].getInverse());*/
Matrix4 test = Matrix4.
multiply(mBoneTransformMatrices
[i
].
getInverse(), bone.
getGlobalTransform());
/*com.badlogic.gdx.math.Matrix4 invBone = new com.badlogic.gdx.math.Matrix4(mGdxBones[i]).inv();
com.badlogic.gdx.math.Matrix4 resultMatrix = new com.badlogic.gdx.math.Matrix4();
resultMatrix.set(bone.globalTransform).mul(invBone);*/
//matrices[i].set(resultMatrix.getValues());
matrices
[i
] = test
;
/*Engine.log("Global Transform:");
output(bone.getGlobalTransform().values);
output(bone.globalTransform.getValues());
Engine.log("Bind Matrix");
output(mBoneTransformMatrices[i].values);
output(mGdxBones[i].getValues());
Engine.log("Inverse Bind Matrix");
output(mBoneTransformMatrices[i].getInverse().values);
output(invBone.getValues());
Engine.log("Global*InvBind");
output(test.values);
output(resultMatrix.getValues());
Engine.log("i = " + i);*/
}
//Engine.log("-----------------------");
/*Matrix4 mA = new Matrix4();
mA.values[ 0] = 0; mA.values[ 1] = 1; mA.values[ 2] = 0; mA.values[ 3] = 0;
mA.values[ 4] = -1; mA.values[ 5] = 0; mA.values[ 6] = 0; mA.values[ 7] = 0;
mA.values[ 8] = 0; mA.values[ 9] = 0; mA.values[10] = 1; mA.values[11] = 0;
mA.values[12] = 0; mA.values[13] = 0; mA.values[14] = 0; mA.values[15] = 1;
Matrix4 mB = new Matrix4();
mB.values[ 0] = 0; mB.values[ 1] = -1; mB.values[ 2] = 0; mB.values[ 3] = 0;
mB.values[ 4] = 0; mB.values[ 5] = 0; mB.values[ 6] = -1; mB.values[ 7] = 0;
mB.values[ 8] = 1; mB.values[ 9] = 0; mB.values[10] = 0; mB.values[11] = 0;
mB.values[12] = 0; mB.values[13] = 0; mB.values[14] = 0; mB.values[15] = 1;
Matrix4 r = Matrix4.multiply(mB, mA);
output(r.values);*/
return matrices
;
}
public void output
(float[] v
)
{
Engine.
log(String.
format("| %.2f %.2f %.2f %.2f |", v
[0], v
[1], v
[2], v
[3]));
Engine.
log(String.
format("| %.2f %.2f %.2f %.2f |", v
[4], v
[5], v
[6], v
[7]));
Engine.
log(String.
format("| %.2f %.2f %.2f %.2f |", v
[8], v
[9], v
[10], v
[11]));
Engine.
log(String.
format("| %.2f %.2f %.2f %.2f |", v
[12], v
[13], v
[14], v
[15]));
Engine.
log("--");
}
// Getters/Setters==================================================================================
public final Material getMaterial
() { return mMaterial
; }
public final MeshGroup getMeshGroup
() { return mMeshGroup
; }
private final void setBoneInfluences
(ModelNode
[] influences
)
{
mBoneInfluences = influences
;
}
public final ModelNode
[] getBoneInfluences
() { return mBoneInfluences
; }
private final void setBoneTransforms
(Transform
[] transforms
)
{
mBoneTransforms = transforms
;
}
public final Transform
[] getBoneTransforms
() { return mBoneTransforms
; }
public final void setBones
(ModelNode
[] influences, Transform
[] transforms
)
{
setBoneInfluences
(influences
);
setBoneTransforms
(transforms
);
mBoneTransformMatrices =
new Matrix4
[transforms.
length];
mGdxBones =
new com.
badlogic.
gdx.
math.
Matrix4[transforms.
length];
for (int i =
0; i
< transforms.
length; i++
)
{
mBoneTransformMatrices
[i
] = Matrix4.
createIdentity();
mBoneTransformMatrices
[i
].
multiply(Matrix4.
createScale(transforms
[i
].
scale));
mBoneTransformMatrices
[i
].
multiply(transforms
[i
].
rotation.
toMatrix());
mBoneTransformMatrices
[i
].
multiply(Matrix4.
createTranslation(transforms
[i
].
translation));
com.
badlogic.
gdx.
math.
Quaternion tempQ =
new com.
badlogic.
gdx.
math.
Quaternion();
mGdxBones
[i
] =
new com.
badlogic.
gdx.
math.
Matrix4();
mGdxBones
[i
].
translate(transforms
[i
].
translation.
x,transforms
[i
].
translation.
y, transforms
[i
].
translation.
z);
mGdxBones
[i
].
rotate(tempQ.
set(transforms
[i
].
rotation.
x, transforms
[i
].
rotation.
y, transforms
[i
].
rotation.
z, transforms
[i
].
rotation.
w));
mGdxBones
[i
].
scale(transforms
[i
].
scale.
x, transforms
[i
].
scale.
y, transforms
[i
].
scale.
z);
}
}
public final boolean isSkinned
()
{
return (mBoneInfluences
!=
null);
}
}