use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class MD5AnimationLoader method createAnimation.
private MeshAnimationData createAnimation(MD5 md5) {
List<String> boneNames = Lists.newArrayListWithCapacity(md5.numJoints);
TIntList boneParents = new TIntArrayList(md5.numJoints);
for (int i = 0; i < md5.numJoints; ++i) {
boneNames.add(md5.joints[i].name);
boneParents.add(md5.joints[i].parent);
}
float timePerFrame = 1.0f / md5.frameRate;
List<MeshAnimationFrame> frames = Lists.newArrayList();
for (int frameIndex = 0; frameIndex < md5.numFrames; ++frameIndex) {
MD5Frame frame = md5.frames[frameIndex];
List<Vector3f> positions = Lists.newArrayListWithExpectedSize(md5.numJoints);
List<Vector3f> rawRotations = Lists.newArrayListWithExpectedSize(md5.numJoints);
for (int i = 0; i < md5.numJoints; ++i) {
positions.add(new Vector3f(md5.baseFramePosition[i]));
rawRotations.add(new Vector3f(md5.baseFrameOrientation[i]));
}
for (int jointIndex = 0; jointIndex < md5.numJoints; ++jointIndex) {
int compIndex = 0;
if ((md5.joints[jointIndex].flags & POSITION_X_FLAG) != 0) {
positions.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & POSITION_Y_FLAG) != 0) {
positions.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & POSITION_Z_FLAG) != 0) {
positions.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & ORIENTATION_X_FLAG) != 0) {
rawRotations.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & ORIENTATION_Y_FLAG) != 0) {
rawRotations.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & ORIENTATION_Z_FLAG) != 0) {
rawRotations.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
}
}
List<Quat4f> rotations = rawRotations.stream().map(rot -> MD5ParserCommon.completeQuat4f(rot.x, rot.y, rot.z)).collect(Collectors.toCollection(ArrayList::new));
// Rotate just the root bone to correct for coordinate system differences
rotations.set(0, MD5ParserCommon.correctQuat4f(rotations.get(0)));
positions.set(0, MD5ParserCommon.correctOffset(positions.get(0)));
frames.add(new MeshAnimationFrame(positions, rotations));
}
AABB aabb = AABB.createEncompassing(Arrays.asList(md5.bounds));
return new MeshAnimationData(boneNames, boneParents, frames, timePerFrame, aabb);
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class MD5ParserCommon method completeQuat4f.
public static Quat4f completeQuat4f(float x, float y, float z) {
float t = 1.0f - (x * x) - (y * y) - (z * z);
float w = 0;
if (t > 0.0f) {
w = (float) -Math.sqrt(t);
}
Quat4f result = new Quat4f(x, y, z, w);
result.normalize();
return result;
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class Block method setCollision.
/**
* Set the collision box for the block
*
* @param offset The offset to the block's center
* @param shape The shape of collision box
*/
public void setCollision(Vector3f offset, CollisionShape shape) {
collisionShape = shape;
collisionOffset = offset;
bounds = shape.getAABB(new Transform(offset, new Quat4f(0, 0, 0, 1), 1.0f));
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class Bone method getLocalPosition.
public Vector3f getLocalPosition() {
Vector3f pos = new Vector3f(objectSpacePos);
if (parent != null) {
pos.sub(parent.getObjectPosition());
Quat4f inverseParentRot = new Quat4f();
inverseParentRot.inverse(parent.getObjectRotation());
inverseParentRot.rotate(pos, pos);
}
return pos;
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class OpenVRStereoCamera method updateMatrices.
@Override
public void updateMatrices(float fov) {
prevViewProjectionMatrix.set(viewProjectionMatrix);
org.joml.Matrix4f leftEyeProjection = vrProvider.getState().getEyeProjectionMatrix(0);
org.joml.Matrix4f rightEyeProjection = vrProvider.getState().getEyeProjectionMatrix(1);
org.joml.Matrix4f leftEyePose = vrProvider.getState().getEyePose(0);
org.joml.Matrix4f rightEyePose = vrProvider.getState().getEyePose(1);
float halfIPD = (float) Math.sqrt(Math.pow(leftEyePose.m30() - rightEyePose.m30(), 2) + Math.pow(leftEyePose.m31() - rightEyePose.m31(), 2) + Math.pow(leftEyePose.m32() - rightEyePose.m32(), 2)) / 2.0f;
// set camera orientation
Vector4f vecQuaternion = OpenVRUtil.convertToQuaternion(leftEyePose);
Quaternionf quaternion = new Quaternionf(vecQuaternion.x, vecQuaternion.y, vecQuaternion.z, vecQuaternion.w);
setOrientation(new Quat4f(quaternion.x, quaternion.y, quaternion.z, quaternion.w));
// view matrix is inverse of pose matrix
leftEyePose = leftEyePose.invert();
rightEyePose = rightEyePose.invert();
if (Math.sqrt(Math.pow(leftEyePose.m30(), 2) + Math.pow(leftEyePose.m31(), 2) + Math.pow(leftEyePose.m32(), 2)) < 0.25) {
return;
}
jomlMatrix4f(leftEyeProjection, projectionMatrixLeftEye);
jomlMatrix4f(rightEyeProjection, projectionMatrixRightEye);
projectionMatrix = projectionMatrixLeftEye;
jomlMatrix4f(leftEyePose, viewMatrixLeftEye);
jomlMatrix4f(rightEyePose, viewMatrixRightEye);
viewMatrix = viewMatrixLeftEye;
normViewMatrix = viewMatrixLeftEye;
reflectionMatrix.setRow(0, 1.0f, 0.0f, 0.0f, 0.0f);
reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 2f * (-position.y + 32f));
reflectionMatrix.setRow(2, 0.0f, 0.0f, 1.0f, 0.0f);
reflectionMatrix.setRow(3, 0.0f, 0.0f, 0.0f, 1.0f);
viewMatrixReflected.mul(viewMatrix, reflectionMatrix);
reflectionMatrix.setRow(1, 0.0f, -1.0f, 0.0f, 0.0f);
normViewMatrixReflected.mul(normViewMatrix, reflectionMatrix);
viewTranslationLeftEye.setIdentity();
viewTranslationLeftEye.setTranslation(new Vector3f(halfIPD, 0.0f, 0.0f));
viewTranslationRightEye.setIdentity();
viewTranslationRightEye.setTranslation(new Vector3f(-halfIPD, 0.0f, 0.0f));
viewMatrixReflectedLeftEye.mul(viewMatrixReflected, viewTranslationLeftEye);
viewMatrixReflectedRightEye.mul(viewMatrixReflected, viewTranslationRightEye);
viewProjectionMatrixLeftEye = MatrixUtils.calcViewProjectionMatrix(viewMatrixLeftEye, projectionMatrixLeftEye);
viewProjectionMatrixRightEye = MatrixUtils.calcViewProjectionMatrix(viewMatrixRightEye, projectionMatrixRightEye);
inverseViewProjectionMatrixLeftEye = new Matrix4f(viewProjectionMatrixLeftEye);
inverseViewProjectionMatrixRightEye = new Matrix4f(viewProjectionMatrixRightEye);
inverseViewProjectionMatrixLeftEye.invert(viewProjectionMatrixLeftEye);
inverseViewProjectionMatrixRightEye.invert(viewProjectionMatrixRightEye);
inverseProjectionMatrixLeftEye = new Matrix4f(projectionMatrixLeftEye);
inverseProjectionMatrixRightEye = new Matrix4f(projectionMatrixRightEye);
inverseProjectionMatrixLeftEye.invert(projectionMatrixLeftEye);
inverseProjectionMatrixRightEye.invert(projectionMatrixRightEye);
updateFrustum();
}
Aggregations