use of net.drewke.tdme.math.Matrix4x4 in project tdme by andreasdr.
the class Model method computeTransformationsMatrix.
/**
* Computes a transformations matrix at a given frame for a given group id recursivly
* @param groups
* @param parent transformations matrix
* @param frame
* @param group id
* @return group transformations matrix or null
*/
protected Matrix4x4 computeTransformationsMatrix(HashMap<String, Group> groups, Matrix4x4 parentTransformationsMatrix, int frame, String groupId) {
// iterate through groups
for (Group group : groups.getValuesIterator()) {
// group transformation matrix
Matrix4x4 transformationsMatrix = null;
// compute animation matrix if animation setups exist
Animation animation = group.getAnimation();
if (animation != null) {
Matrix4x4[] animationMatrices = animation.getTransformationsMatrices();
transformationsMatrix = animationMatrices[frame % animationMatrices.length].clone();
}
// do we have no animation matrix?
if (transformationsMatrix == null) {
// no animation matrix, set up local transformation matrix up as group matrix
transformationsMatrix = group.getTransformationsMatrix().clone();
} else {
// we have animation matrix, so multiply it with group transformation matrix
transformationsMatrix.multiply(group.getTransformationsMatrix());
}
// apply parent transformation matrix
if (parentTransformationsMatrix != null) {
transformationsMatrix.multiply(parentTransformationsMatrix);
}
// return matrix if group matches
if (group.getId().equals(groupId))
return transformationsMatrix;
// calculate sub groups
HashMap<String, Group> subGroups = group.getSubGroups();
if (subGroups.size() > 0) {
Matrix4x4 tmp = computeTransformationsMatrix(subGroups, transformationsMatrix, frame, groupId);
if (tmp != null)
return tmp;
}
}
//
return null;
}
use of net.drewke.tdme.math.Matrix4x4 in project tdme by andreasdr.
the class BoundingBox method fromBoundingVolumeWithTransformations.
/*
* (non-Javadoc)
* @see net.drewke.tdme.primitives.BoundingVolume#fromBoundingVolumeWithTransformations(net.drewke.tdme.primitives.BoundingVolume, net.drewke.tdme.engine.Transformations)
*/
public void fromBoundingVolumeWithTransformations(BoundingVolume original, Transformations transformations) {
// check for same type of original
if (original instanceof BoundingBox == false) {
System.out.println("BoundingBox::fromBoundingVolumeWithTransformations(): original is not of same type");
return;
}
//
BoundingBox boundingBox = (BoundingBox) original;
//
Matrix4x4 transformationsMatrix = transformations.getTransformationsMatrix();
Vector3[] _vertices = boundingBox.getVertices();
// apply transformations from original vertices to local vertices
for (int i = 0; i < vertices.length; i++) {
transformationsMatrix.multiply(_vertices[i], vertices[i]);
}
// determine axis aligned bounding box constraints based on local vertices
float[] vertexXYZ = vertices[0].getArray();
float minX = vertexXYZ[0], minY = vertexXYZ[1], minZ = vertexXYZ[2];
float maxX = vertexXYZ[0], maxY = vertexXYZ[1], maxZ = vertexXYZ[2];
for (int vertexIndex = 1; vertexIndex < vertices.length; vertexIndex++) {
Vector3 vertex = vertices[vertexIndex];
vertexXYZ = vertex.getArray();
if (vertexXYZ[0] < minX)
minX = vertexXYZ[0];
if (vertexXYZ[1] < minY)
minY = vertexXYZ[1];
if (vertexXYZ[2] < minZ)
minZ = vertexXYZ[2];
if (vertexXYZ[0] > maxX)
maxX = vertexXYZ[0];
if (vertexXYZ[1] > maxY)
maxY = vertexXYZ[1];
if (vertexXYZ[2] > maxZ)
maxZ = vertexXYZ[2];
}
// set up new aabb
min.set(minX, minY, minZ);
max.set(maxX, maxY, maxZ);
// compute new vertices based on aabb constraints
update();
}
use of net.drewke.tdme.math.Matrix4x4 in project tdme by andreasdr.
the class Capsule method fromBoundingVolumeWithTransformations.
/*
* (non-Javadoc)
* @see net.drewke.tdme.primitives.BoundingVolume#fromBoundingVolumeWithTransformations(net.drewke.tdme.primitives.BoundingVolume, net.drewke.tdme.engine.Transformations)
*/
public void fromBoundingVolumeWithTransformations(BoundingVolume original, Transformations transformations) {
// check for same type of original
if (original instanceof Capsule == false) {
System.out.println("Capsule::fromBoundingVolumeWithTransformations(): original is not of same type");
return;
}
//
Capsule capsule = (Capsule) original;
//
Matrix4x4 transformationsMatrix = transformations.getTransformationsMatrix();
transformationsMatrix.multiply(capsule.a, a);
transformationsMatrix.multiply(capsule.b, b);
// note:
// capsule radius can only be scaled the same on all axes
// thats why its enough to only take x axis to determine scaling
side.set(capsule.a).addX(capsule.radius);
transformationsMatrix.multiply(side, side);
radius = side.sub(a).computeLength();
//
update();
}
use of net.drewke.tdme.math.Matrix4x4 in project tdme by andreasdr.
the class Object3DBase method computeTransformationsMatrices.
/**
* Calculates all groups transformation matrices
* @param groups
* @param parent transformations matrix
* @param animation state
* @param depth
*/
protected void computeTransformationsMatrices(HashMap<String, Group> groups, Matrix4x4 parentTransformationsMatrix, AnimationState animationState, int depth) {
// iterate through groups
for (Group group : groups.getValuesIterator()) {
// check for overlay animation
AnimationState overlayAnimation = overlayAnimationsByJointId.get(group.getId());
if (overlayAnimation != null)
animationState = overlayAnimation;
// group transformation matrix
Matrix4x4 transformationsMatrix = null;
// compute animation matrix if animation setups exist
Animation animation = group.getAnimation();
if (animation != null && animationState.finished == false) {
Matrix4x4[] animationMatrices = animation.getTransformationsMatrices();
int frames = animationState.setup.getFrames();
float fps = model.getFPS();
// determine current and last matrix
float frameAtLast = (animationState.lastAtTime / 1000f) * fps;
float frameAtCurrent = (animationState.currentAtTime / 1000f) * fps;
// check if looping is disabled
if (animationState.setup.isLoop() == false && frameAtCurrent >= frames) {
// set frame at current to last frame
frameAtLast = frames - 1;
frameAtCurrent = frames - 1;
animationState.finished = true;
}
//
int matrixAtLast = ((int) frameAtLast % frames);
int matrixAtCurrent = ((int) frameAtCurrent % frames);
animationState.time = frames <= 1 ? 0.0f : (float) matrixAtCurrent / (float) (frames - 1);
// compute animation transformations matrix
float t = frameAtCurrent - (float) Math.floor(frameAtLast);
if (t < 1f) {
if (matrixAtLast == matrixAtCurrent) {
matrixAtCurrent = ((matrixAtCurrent + 1) % frames);
}
transformationsMatrix = Matrix4x4.interpolateLinear(animationMatrices[matrixAtLast + animationState.setup.getStartFrame()], animationMatrices[matrixAtCurrent + animationState.setup.getStartFrame()], t, tmpMatrix1);
} else {
transformationsMatrix = tmpMatrix1.set(animationMatrices[matrixAtCurrent + animationState.setup.getStartFrame()]);
}
}
// do we have no animation matrix?
if (transformationsMatrix == null) {
// no animation matrix, set up local transformation matrix up as group matrix
transformationsMatrix = tmpMatrix1.set(group.getTransformationsMatrix());
}
// apply parent transformation matrix
if (parentTransformationsMatrix != null) {
transformationsMatrix.multiply(parentTransformationsMatrix);
}
// put and associate transformation matrices with group
transformationsMatrices.get(group.getId()).set(transformationsMatrix);
// calculate for sub groups
HashMap<String, Group> subGroups = group.getSubGroups();
if (subGroups.size() > 0) {
// put to matrices stack
transformationsMatricesStack[depth].set(transformationsMatrix);
// compute sub groups transformations
computeTransformationsMatrices(subGroups, transformationsMatricesStack[depth], animationState, depth + 1);
}
}
}
use of net.drewke.tdme.math.Matrix4x4 in project tdme by andreasdr.
the class Object3DGroup method createGroups.
/**
* Creates a object 3d groups recursively for given group and it sub groups
* @param object 3D base
* @param object 3D groups
* @param groups
* @param animated
* @param use mesh manager
*/
private static void createGroups(Object3DBase object3D, ArrayList<Object3DGroup> object3DGroups, HashMap<String, Group> groups, boolean animated, boolean useMeshManager, Engine.AnimationProcessingTarget animationProcessingTarget) {
for (Group group : groups.getValuesIterator()) {
// skip on joints
if (group.isJoint() == true) {
continue;
}
// determine face count
int faceCount = group.getFaceCount();
// skip on groups without faces
if (faceCount > 0) {
// create group render data
Object3DGroup object3DGroup = new Object3DGroup();
// add it to group render data list
object3DGroups.add(object3DGroup);
// determine mesh id
object3DGroup.id = group.getModel().getId() + ":" + group.getId() + ":" + animationProcessingTarget.toString().toLowerCase();
if (animated == true && (animationProcessingTarget == AnimationProcessingTarget.CPU || animationProcessingTarget == AnimationProcessingTarget.CPU_NORENDERING)) {
//
object3DGroup.id += ":" + (counter++);
}
object3DGroup.object = object3D;
object3DGroup.group = group;
object3DGroup.animated = animated;
if (useMeshManager == true) {
MeshManager meshManager = Engine.getInstance().getMeshManager();
object3DGroup.mesh = meshManager.getMesh(object3DGroup.id);
if (object3DGroup.mesh == null) {
object3DGroup.mesh = Object3DGroupMesh.createMesh(animationProcessingTarget, group, object3D.transformationsMatrices);
meshManager.addMesh(object3DGroup.id, object3DGroup.mesh);
}
} else {
object3DGroup.mesh = Object3DGroupMesh.createMesh(animationProcessingTarget, group, object3D.transformationsMatrices);
}
object3DGroup.materialDiffuseTextureIdsByEntities = new int[group.getFacesEntities().length];
object3DGroup.dynamicDiffuseTextureIdsByEntities = new int[group.getFacesEntities().length];
object3DGroup.materialSpecularTextureIdsByEntities = new int[group.getFacesEntities().length];
object3DGroup.materialDisplacementTextureIdsByEntities = new int[group.getFacesEntities().length];
object3DGroup.materialNormalTextureIdsByEntities = new int[group.getFacesEntities().length];
for (int j = 0; j < group.getFacesEntities().length; j++) {
object3DGroup.materialDiffuseTextureIdsByEntities[j] = GLTEXTUREID_NONE;
object3DGroup.dynamicDiffuseTextureIdsByEntities[j] = GLTEXTUREID_NONE;
object3DGroup.materialSpecularTextureIdsByEntities[j] = GLTEXTUREID_NONE;
object3DGroup.materialDisplacementTextureIdsByEntities[j] = GLTEXTUREID_NONE;
object3DGroup.materialNormalTextureIdsByEntities[j] = GLTEXTUREID_NONE;
}
// determine renderer
object3DGroup.renderer = new Object3DGroupVBORenderer(object3DGroup);
// skinning
Skinning skinning = group.getSkinning();
if (skinning != null) {
object3DGroup.groupTransformationsMatricesVector = new ArrayList<Matrix4x4>();
for (Joint joint : skinning.getJoints()) {
object3DGroup.groupTransformationsMatricesVector.add(object3D.transformationsMatrices.get(joint.getGroupId()));
}
} else {
object3DGroup.groupTransformationsMatricesVector = null;
}
//
object3DGroup.groupTransformationsMatrix = object3D.transformationsMatrices.get(group.getId());
}
// but still check sub groups
createGroups(object3D, object3DGroups, group.getSubGroups(), animated, useMeshManager, animationProcessingTarget);
}
}
Aggregations