use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.
the class SkeletalMeshData method calculateNormals.
private void calculateNormals() {
// TODO: Better algorithm (take into account triangle size and angles
List<Vector3f> vertices = getBindPoseVertexPositions();
List<Vector3f> normals = Lists.newArrayListWithCapacity(vertices.size());
for (int i = 0; i < vertices.size(); ++i) {
normals.add(new Vector3f());
}
Vector3f v1 = new Vector3f();
Vector3f v2 = new Vector3f();
Vector3f norm = new Vector3f();
for (int i = 0; i < indices.size() / 3; ++i) {
Vector3f baseVert = vertices.get(indices.get(i * 3));
v1.sub(vertices.get(indices.get(i * 3 + 1)), baseVert);
v2.sub(vertices.get(indices.get(i * 3 + 2)), baseVert);
v1.normalize();
v2.normalize();
norm.cross(v1, v2);
normals.get(indices.get(i * 3)).add(norm);
normals.get(indices.get(i * 3 + 1)).add(norm);
normals.get(indices.get(i * 3 + 2)).add(norm);
}
normals.forEach(Vector3f::normalize);
Quat4f inverseRot = new Quat4f();
for (int vertIndex = 0; vertIndex < vertices.size(); ++vertIndex) {
Vector3f normal = normals.get(vertIndex);
for (int weightIndex = 0; weightIndex < vertexWeightCounts.get(vertIndex); ++weightIndex) {
BoneWeight weight = weights.get(weightIndex + vertexStartWeights.get(vertIndex));
inverseRot.inverse(bones.get(weight.getBoneIndex()).getObjectRotation());
inverseRot.rotate(normal, norm);
weight.setNormal(norm);
}
}
}
use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.
the class SkeletalMeshData method getVertexPositions.
public List<Vector3f> getVertexPositions(List<Vector3f> bonePositions, List<Quat4f> boneRotations) {
List<Vector3f> results = Lists.newArrayListWithCapacity(getVertexCount());
for (int i = 0; i < vertexStartWeights.size(); ++i) {
Vector3f vertexPos = new Vector3f();
for (int weightIndexOffset = 0; weightIndexOffset < vertexWeightCounts.get(i); ++weightIndexOffset) {
int weightIndex = vertexStartWeights.get(i) + weightIndexOffset;
BoneWeight weight = weights.get(weightIndex);
Vector3f current = boneRotations.get(weight.getBoneIndex()).rotate(weight.getPosition(), new Vector3f());
current.add(bonePositions.get(weight.getBoneIndex()));
current.scale(weight.getBias());
vertexPos.add(current);
}
results.add(vertexPos);
}
return results;
}
use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.
the class SkeletalMeshDataBuilder method addMesh.
public SkeletalMeshDataBuilder addMesh(Bone bone, MeshData data) {
TFloatList meshVertices = data.getVertices();
TIntList meshIndices = data.getIndices();
TFloatList texCoord0 = data.getTexCoord0();
int weightsStart = weights.size();
addBone(bone);
for (int i = 0; i < meshVertices.size() / 3; i++) {
float x = meshVertices.get(i * 3);
float y = meshVertices.get(i * 3 + 1);
float z = meshVertices.get(i * 3 + 2);
Vector3f pos = new Vector3f(x, y, z);
BoneWeight weight = new BoneWeight(pos, 1, bone.getIndex());
// TODO Meshes may contain normal vectors and we may copy them to the weight here
// - but they are recalculated later on in either case. needs some rework
addWeight(weight);
vertexStartWeights.add(weightsStart + i);
vertexWeightCounts.add(1);
uvs.add(new Vector2f(texCoord0.get(i * 2), texCoord0.get(i * 2 + 1)));
}
for (int i = 0; i < meshIndices.size(); i++) {
indices.add(meshIndices.get(i) + weightsStart);
}
return this;
}
use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.
the class SkeletalMeshDataBuilder method addWeight.
public SkeletalMeshDataBuilder addWeight(BoneWeight boneWeight) {
Vector3f pos = boneWeight.getPosition();
if (minOfAABB == null) {
minOfAABB = new Vector3f(pos);
} else {
minOfAABB.min(pos);
}
if (maxOfAABB == null) {
maxOfAABB = new Vector3f(pos);
} else {
maxOfAABB.max(pos);
}
weights.add(boneWeight);
return this;
}
use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.
the class ViewFrustum method intersects.
/**
* Returns true if this view frustum intersects the given AABB.
*/
public boolean intersects(AABB aabb) {
Vector3f[] aabbVertices = aabb.getVertices();
Vector3f cp = CoreRegistry.get(LocalPlayer.class).getViewPosition();
for (int i = 0; i < 6; i++) {
if (planes[i].getA() * (aabbVertices[0].x - cp.x) + planes[i].getB() * (aabbVertices[0].y - cp.y) + planes[i].getC() * (aabbVertices[0].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[1].x - cp.x) + planes[i].getB() * (aabbVertices[1].y - cp.y) + planes[i].getC() * (aabbVertices[1].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[2].x - cp.x) + planes[i].getB() * (aabbVertices[2].y - cp.y) + planes[i].getC() * (aabbVertices[2].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[3].x - cp.x) + planes[i].getB() * (aabbVertices[3].y - cp.y) + planes[i].getC() * (aabbVertices[3].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[4].x - cp.x) + planes[i].getB() * (aabbVertices[4].y - cp.y) + planes[i].getC() * (aabbVertices[4].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[5].x - cp.x) + planes[i].getB() * (aabbVertices[5].y - cp.y) + planes[i].getC() * (aabbVertices[5].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[6].x - cp.x) + planes[i].getB() * (aabbVertices[6].y - cp.y) + planes[i].getC() * (aabbVertices[6].z - cp.z) + planes[i].getD() > 0) {
continue;
}
if (planes[i].getA() * (aabbVertices[7].x - cp.x) + planes[i].getB() * (aabbVertices[7].y - cp.y) + planes[i].getC() * (aabbVertices[7].z - cp.z) + planes[i].getD() > 0) {
continue;
}
return false;
}
return true;
}
Aggregations