use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.
the class FbxNode method setWorldBindPose.
public void setWorldBindPose(Matrix4f worldBindPose) {
if (cachedWorldBindPose != null) {
if (!cachedWorldBindPose.equals(worldBindPose)) {
throw new UnsupportedOperationException("Bind poses don't match");
}
}
cachedWorldBindPose = worldBindPose;
this.jmeWorldBindPose = new Transform();
this.jmeWorldBindPose.setTranslation(worldBindPose.toTranslationVector());
this.jmeWorldBindPose.setRotation(worldBindPose.toRotationQuat());
this.jmeWorldBindPose.setScale(worldBindPose.toScaleVector());
System.out.println("\tBind Pose for " + getName());
System.out.println(jmeWorldBindPose);
float[] angles = new float[3];
jmeWorldBindPose.getRotation().toAngles(angles);
System.out.println("Angles: " + angles[0] * FastMath.RAD_TO_DEG + ", " + angles[1] * FastMath.RAD_TO_DEG + ", " + angles[2] * FastMath.RAD_TO_DEG);
}
use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.
the class FbxNode method fromElement.
@Override
public void fromElement(FbxElement element) {
super.fromElement(element);
Vector3f localTranslation = new Vector3f();
Quaternion localRotation = new Quaternion();
Vector3f localScale = new Vector3f(Vector3f.UNIT_XYZ);
Quaternion preRotation = new Quaternion();
for (FbxElement e2 : element.getFbxProperties()) {
String propName = (String) e2.properties.get(0);
String type = (String) e2.properties.get(3);
if (propName.equals("Lcl Translation")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
//.divideLocal(unitSize);
localTranslation.set((float) x, (float) y, (float) z);
} else if (propName.equals("Lcl Rotation")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
localRotation.fromAngles((float) x * FastMath.DEG_TO_RAD, (float) y * FastMath.DEG_TO_RAD, (float) z * FastMath.DEG_TO_RAD);
} else if (propName.equals("Lcl Scaling")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
//.multLocal(unitSize);
localScale.set((float) x, (float) y, (float) z);
} else if (propName.equals("PreRotation")) {
double x = (Double) e2.properties.get(4);
double y = (Double) e2.properties.get(5);
double z = (Double) e2.properties.get(6);
preRotation.set(FbxNodeUtil.quatFromBoneAngles((float) x * FastMath.DEG_TO_RAD, (float) y * FastMath.DEG_TO_RAD, (float) z * FastMath.DEG_TO_RAD));
} else if (propName.equals("InheritType")) {
int inheritType = (Integer) e2.properties.get(4);
inheritMode = InheritMode.values()[inheritType];
} else if (propName.equals("Visibility")) {
visibility = (Double) e2.properties.get(4);
} else if (type.contains("U")) {
String userDataKey = (String) e2.properties.get(0);
String userDataType = (String) e2.properties.get(1);
Object userDataValue;
if (userDataType.equals("KString")) {
userDataValue = (String) e2.properties.get(4);
} else if (userDataType.equals("int")) {
userDataValue = (Integer) e2.properties.get(4);
} else if (userDataType.equals("double")) {
// NOTE: jME3 does not support doubles in UserData.
// Need to convert to float.
userDataValue = ((Double) e2.properties.get(4)).floatValue();
} else if (userDataType.equals("Vector")) {
float x = ((Double) e2.properties.get(4)).floatValue();
float y = ((Double) e2.properties.get(5)).floatValue();
float z = ((Double) e2.properties.get(6)).floatValue();
userDataValue = new Vector3f(x, y, z);
} else {
logger.log(Level.WARNING, "Unsupported user data type: {0}. Ignoring.", userDataType);
continue;
}
userData.put(userDataKey, userDataValue);
}
}
// Create local transform
// TODO: take into account Maya-style transforms (pre / post rotation ..)
jmeLocalNodeTransform.setTranslation(localTranslation);
jmeLocalNodeTransform.setRotation(localRotation);
jmeLocalNodeTransform.setScale(localScale);
if (element.getChildById("Vertices") != null) {
// This is an old-style FBX 6.1
// Meshes could be embedded inside the node..
// Inject the mesh into ourselves..
FbxMesh mesh = new FbxMesh(assetManager, sceneFolderName);
mesh.fromElement(element);
connectObject(mesh);
}
}
use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.
the class FbxNode method updateWorldTransforms.
public void updateWorldTransforms(Transform jmeParentNodeTransform, Transform parentBindPose) {
Transform fbxLocalTransform = computeFbxLocalTransform();
jmeLocalNodeTransform.set(fbxLocalTransform);
if (jmeParentNodeTransform != null) {
jmeParentNodeTransform = jmeParentNodeTransform.clone();
switch(inheritMode) {
case NoParentScale:
case ScaleAfterChildRotation:
case ScaleBeforeChildRotation:
jmeWorldNodeTransform.set(jmeLocalNodeTransform);
jmeWorldNodeTransform.combineWithParent(jmeParentNodeTransform);
break;
}
} else {
jmeWorldNodeTransform.set(jmeLocalNodeTransform);
}
if (jmeWorldBindPose != null) {
jmeLocalBindPose = new Transform();
// Need to derive local bind pose from world bind pose
// (this is to be expected for FBX limbs)
jmeLocalBindPose.set(jmeWorldBindPose);
jmeLocalBindPose.combineWithParent(parentBindPose.invert());
// Its somewhat odd for the transforms to differ ...
System.out.println("Bind Pose for: " + getName());
if (!jmeLocalBindPose.equals(jmeLocalNodeTransform)) {
System.out.println("Local Bind: " + jmeLocalBindPose);
System.out.println("Local Trans: " + jmeLocalNodeTransform);
}
if (!jmeWorldBindPose.equals(jmeWorldNodeTransform)) {
System.out.println("World Bind: " + jmeWorldBindPose);
System.out.println("World Trans: " + jmeWorldNodeTransform);
}
} else {
// World pose derived from local transforms
// (this is to be expected for FBX nodes)
jmeLocalBindPose = new Transform();
jmeWorldBindPose = new Transform();
jmeLocalBindPose.set(jmeLocalNodeTransform);
if (parentBindPose != null) {
jmeWorldBindPose.set(jmeLocalNodeTransform);
jmeWorldBindPose.combineWithParent(parentBindPose);
} else {
jmeWorldBindPose.set(jmeWorldNodeTransform);
}
}
for (FbxNode child : children) {
child.updateWorldTransforms(jmeWorldNodeTransform, jmeWorldBindPose);
}
}
use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.
the class FbxNode method computeFbxLocalTransform.
public Transform computeFbxLocalTransform() {
// TODO: implement the actual algorithm, which is this:
// Render Local Translation =
// Inv Scale Pivot * Lcl Scale * Scale Pivot * Scale Offset * Inv Rota Pivot * Post Rotation * Rotation * Pre Rotation * Rotation Pivot * Rotation Offset * Translation
// LclTranslation,
// LclRotation,
// PreRotation,
// PostRotation,
// RotationPivot,
// RotationOffset,
// LclScaling,
// ScalingPivot,
// ScalingOffset
Matrix4f scaleMat = new Matrix4f();
scaleMat.setScale(jmeLocalNodeTransform.getScale());
Matrix4f rotationMat = new Matrix4f();
rotationMat.setRotationQuaternion(jmeLocalNodeTransform.getRotation());
Matrix4f translationMat = new Matrix4f();
translationMat.setTranslation(jmeLocalNodeTransform.getTranslation());
Matrix4f result = new Matrix4f();
result.multLocal(scaleMat).multLocal(rotationMat).multLocal(translationMat);
Transform t = new Transform();
t.fromTransformMatrix(result);
return t;
}
use of com.jme3.math.Transform in project jmonkeyengine by jMonkeyEngine.
the class SceneLoader method endElement.
@Override
public void endElement(String uri, String name, String qName) throws SAXException {
if (qName.equals("node")) {
node = node.getParent();
} else if (qName.equals("nodes")) {
node = null;
} else if (qName.equals("entity")) {
node = entityNode.getParent();
entityNode = null;
} else if (qName.equals("camera")) {
node = cameraNode.getParent();
cameraNode = null;
} else if (qName.equals("light")) {
// apply the node's world transform on the light..
root.updateGeometricState();
if (light != null) {
if (light instanceof DirectionalLight) {
DirectionalLight dl = (DirectionalLight) light;
Quaternion q = node.getWorldRotation();
Vector3f dir = dl.getDirection();
q.multLocal(dir);
dl.setDirection(dir);
} else if (light instanceof PointLight) {
PointLight pl = (PointLight) light;
Vector3f pos = node.getWorldTranslation();
pl.setPosition(pos);
} else if (light instanceof SpotLight) {
SpotLight sl = (SpotLight) light;
Vector3f pos = node.getWorldTranslation();
sl.setPosition(pos);
Quaternion q = node.getWorldRotation();
Vector3f dir = sl.getDirection();
q.multLocal(dir);
sl.setDirection(dir);
}
}
light = null;
}
checkTopNode(qName);
elementStack.pop();
}
Aggregations