Search in sources :

Example 46 with Vector3f

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);
        }
    }
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Quat4f(org.terasology.math.geom.Quat4f)

Example 47 with Vector3f

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;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f)

Example 48 with Vector3f

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;
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) Vector3f(org.terasology.math.geom.Vector3f) TFloatList(gnu.trove.list.TFloatList) TIntList(gnu.trove.list.TIntList)

Example 49 with Vector3f

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;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f)

Example 50 with Vector3f

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;
}
Also used : LocalPlayer(org.terasology.logic.players.LocalPlayer) Vector3f(org.terasology.math.geom.Vector3f)

Aggregations

Vector3f (org.terasology.math.geom.Vector3f)194 LocationComponent (org.terasology.logic.location.LocationComponent)45 EntityRef (org.terasology.entitySystem.entity.EntityRef)44 Quat4f (org.terasology.math.geom.Quat4f)33 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)26 Vector3i (org.terasology.math.geom.Vector3i)21 Test (org.junit.Test)20 ClientComponent (org.terasology.network.ClientComponent)15 Command (org.terasology.logic.console.commandSystem.annotations.Command)14 Matrix4f (org.terasology.math.geom.Matrix4f)13 BaseVector3f (org.terasology.math.geom.BaseVector3f)9 Block (org.terasology.world.block.Block)9 Vector2f (org.terasology.math.geom.Vector2f)8 HitResult (org.terasology.physics.HitResult)8 CharacterTeleportEvent (org.terasology.logic.characters.CharacterTeleportEvent)7 IOException (java.io.IOException)6 FloatBuffer (java.nio.FloatBuffer)6 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)5 AABB (org.terasology.math.AABB)5 ChunkMesh (org.terasology.rendering.primitives.ChunkMesh)5