Search in sources :

Example 6 with Vector3fc

use of org.joml.Vector3fc in project Terasology by MovingBlocks.

the class VertexGLAttributeTest method testVector3fBinding.

@Test
public void testVector3fBinding() {
    VertexResourceBuilder builder = new VertexResourceBuilder();
    VertexAttributeBinding<Vector3fc, Vector3f> a1 = builder.add(0, GLAttributes.VECTOR_3_F_VERTEX_ATTRIBUTE);
    VertexResource resource = builder.build();
    a1.put(new Vector3f(10, 150, 1.5f));
    a1.put(new Vector3f(15.1f, 15.04f, 15.5f));
    a1.put(new Vector3f(16f, 150, 31.5f));
    assertEquals(3, a1.getPosition());
    int stride = Float.BYTES * 3;
    resource.writeBuffer(buffer -> {
        assertEquals(3 * 3 * Float.BYTES, buffer.limit());
        assertEquals(10, buffer.getFloat(Float.BYTES * 0), 0.001f);
        assertEquals(150, buffer.getFloat(Float.BYTES * 1), 0.001f);
        assertEquals(1.5f, buffer.getFloat(Float.BYTES * 2), 0.001f);
        assertEquals(15.1f, buffer.getFloat((stride) + Float.BYTES * 0), 0.001f);
        assertEquals(15.04f, buffer.getFloat((stride) + Float.BYTES * 1), 0.001f);
        assertEquals(15.5f, buffer.getFloat((stride) + Float.BYTES * 2), 0.001f);
        assertEquals(16f, buffer.getFloat((stride * 2) + Float.BYTES * 0), 0.001f);
        assertEquals(150f, buffer.getFloat((stride * 2) + Float.BYTES * 1), 0.001f);
        assertEquals(31.5f, buffer.getFloat((stride * 2) + Float.BYTES * 2), 0.001f);
    });
}
Also used : VertexResource(org.terasology.engine.rendering.assets.mesh.resource.VertexResource) Vector3fc(org.joml.Vector3fc) VertexResourceBuilder(org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder) Vector3f(org.joml.Vector3f) Test(org.junit.Test)

Example 7 with Vector3fc

use of org.joml.Vector3fc in project Terasology by MovingBlocks.

the class BulletPhysics method getShapeFor.

/**
 * Returns the shape belonging to the given entity. It currently knows 4 different shapes: Sphere, Capsule, Cylinder
 * or arbitrary. The shape is determined based on the shape component of the given entity. If the entity has somehow
 * got multiple shapes, only one is picked. The order of priority is: Sphere, Capsule, Cylinder, arbitrary.
 * <br><br>
 * TODO: Flyweight this (take scale as parameter)
 *
 * @param entityRef the entity to get the shape of.
 * @return the shape of the entity, ready to be used by Bullet.
 */
private btCollisionShape getShapeFor(EntityRef entityRef) {
    BoxShapeComponent box = entityRef.getComponent(BoxShapeComponent.class);
    if (box != null) {
        Vector3f halfExtents = new Vector3f(box.extents);
        return new btBoxShape(halfExtents.mul(.5f));
    }
    SphereShapeComponent sphere = entityRef.getComponent(SphereShapeComponent.class);
    if (sphere != null) {
        return new btSphereShape(sphere.radius);
    }
    CapsuleShapeComponent capsule = entityRef.getComponent(CapsuleShapeComponent.class);
    if (capsule != null) {
        return new btCapsuleShape(capsule.radius, capsule.height);
    }
    CylinderShapeComponent cylinder = entityRef.getComponent(CylinderShapeComponent.class);
    if (cylinder != null) {
        return new btCylinderShape(new Vector3f(cylinder.radius, 0.5f * cylinder.height, cylinder.radius));
    }
    HullShapeComponent hull = entityRef.getComponent(HullShapeComponent.class);
    if (hull != null) {
        VertexAttributeBinding<Vector3fc, Vector3f> positions = hull.sourceMesh.vertices();
        final int numVertices = hull.sourceMesh.elementCount();
        FloatBuffer buffer = BufferUtils.createFloatBuffer(numVertices * 3);
        Vector3f pos = new Vector3f();
        for (int i = 0; i < numVertices; i++) {
            positions.get(i, pos);
            buffer.put(pos.x);
            buffer.put(pos.y);
            buffer.put(pos.z);
        }
        return new btConvexHullShape(buffer, numVertices, 3 * Float.BYTES);
    }
    CharacterMovementComponent characterMovementComponent = entityRef.getComponent(CharacterMovementComponent.class);
    if (characterMovementComponent != null) {
        return new btCapsuleShape(characterMovementComponent.pickupRadius, characterMovementComponent.height - 2 * characterMovementComponent.radius);
    }
    logger.error("Creating physics object that requires a ShapeComponent or CharacterMovementComponent, but has " + "neither. Entity: {}", entityRef);
    throw new IllegalArgumentException("Creating physics object that requires a ShapeComponent or " + "CharacterMovementComponent, but has neither. Entity: " + entityRef);
}
Also used : com.badlogic.gdx.physics.bullet.collision.btBoxShape(com.badlogic.gdx.physics.bullet.collision.btBoxShape) com.badlogic.gdx.physics.bullet.collision.btCylinderShape(com.badlogic.gdx.physics.bullet.collision.btCylinderShape) SphereShapeComponent(org.terasology.engine.physics.components.shapes.SphereShapeComponent) CylinderShapeComponent(org.terasology.engine.physics.components.shapes.CylinderShapeComponent) HullShapeComponent(org.terasology.engine.physics.components.shapes.HullShapeComponent) CharacterMovementComponent(org.terasology.engine.logic.characters.CharacterMovementComponent) FloatBuffer(java.nio.FloatBuffer) com.badlogic.gdx.physics.bullet.collision.btManifoldPoint(com.badlogic.gdx.physics.bullet.collision.btManifoldPoint) CapsuleShapeComponent(org.terasology.engine.physics.components.shapes.CapsuleShapeComponent) Vector3fc(org.joml.Vector3fc) com.badlogic.gdx.physics.bullet.collision.btSphereShape(com.badlogic.gdx.physics.bullet.collision.btSphereShape) Vector3f(org.joml.Vector3f) com.badlogic.gdx.physics.bullet.collision.btCapsuleShape(com.badlogic.gdx.physics.bullet.collision.btCapsuleShape) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) com.badlogic.gdx.physics.bullet.collision.btConvexHullShape(com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)

Example 8 with Vector3fc

use of org.joml.Vector3fc in project Terasology by MovingBlocks.

the class SkeletonRenderer method renderOpaque.

@Override
public void renderOpaque() {
    Vector3fc cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quaternionf worldRot = new Quaternionf();
    Vector3f worldPos = new Vector3f();
    Quaternionf inverseWorldRot = new Quaternionf();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
        SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
        if (skeletalMesh.mesh == null || skeletalMesh.material == null || skeletalMesh.boneEntities == null || !skeletalMesh.material.isRenderable()) {
            continue;
        }
        AABBf aabb;
        MeshAnimation animation = skeletalMesh.animation;
        if (animation != null) {
            aabb = animation.getAabb();
        } else {
            aabb = skeletalMesh.mesh.getStaticAabb();
        }
        LocationComponent location = entity.getComponent(LocationComponent.class);
        location.getWorldRotation(worldRot);
        worldRot.invert(inverseWorldRot);
        location.getWorldPosition(worldPos);
        float worldScale = location.getWorldScale();
        aabb = aabb.transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
        // Scale bounding box for skeletalMesh.
        Vector3f scale = skeletalMesh.scale;
        Vector3f aabbCenter = aabb.center(new Vector3f());
        Vector3f scaledExtents = aabb.extent(new Vector3f()).mul(scale.x(), scale.y(), scale.z());
        aabb = new AABBf(aabbCenter, aabbCenter).expand(scaledExtents);
        if (!worldRenderer.getActiveCamera().hasInSight(aabb)) {
            continue;
        }
        skeletalMesh.material.enable();
        skeletalMesh.material.setFloat("sunlight", 1.0f, true);
        skeletalMesh.material.setFloat("blockLight", 1.0f, true);
        skeletalMesh.material.setFloat3("colorOffset", skeletalMesh.color.rf(), skeletalMesh.color.gf(), skeletalMesh.color.bf(), true);
        skeletalMesh.material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
        skeletalMesh.material.bindTextures();
        Vector3f worldPositionCameraSpace = new Vector3f();
        worldPos.sub(cameraPosition, worldPositionCameraSpace);
        worldPositionCameraSpace.y += skeletalMesh.heightOffset;
        Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, worldRot, worldScale);
        Matrix4f modelViewMatrix = worldRenderer.getActiveCamera().getViewMatrix().mul(matrixCameraSpace, new Matrix4f());
        modelViewMatrix.get(tempMatrixBuffer44);
        skeletalMesh.material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
        modelViewMatrix.normal(new Matrix3f()).get(tempMatrixBuffer33);
        skeletalMesh.material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
        skeletalMesh.material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
        skeletalMesh.material.setFloat("blockLight", worldRenderer.getBlockLightIntensityAt(worldPos), true);
        Matrix4f[] boneTransforms = new Matrix4f[skeletalMesh.mesh.getBones().size()];
        for (Bone bone : skeletalMesh.mesh.getBones()) {
            EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
            if (boneEntity == null) {
                boneEntity = EntityRef.NULL;
            }
            LocationComponent boneLocation = boneEntity.getComponent(LocationComponent.class);
            if (boneLocation != null) {
                Matrix4f boneTransform = new Matrix4f();
                boneLocation.getRelativeTransform(boneTransform, entity);
                boneTransform.mul(bone.getInverseBindMatrix());
                boneTransforms[bone.getIndex()] = boneTransform.transpose();
            } else {
                logger.warn("Unable to resolve bone \"{}\"", bone.getName());
                boneTransforms[bone.getIndex()] = new Matrix4f();
            }
        }
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).render(Arrays.asList(boneTransforms));
    }
}
Also used : Quaternionf(org.joml.Quaternionf) FloatBuffer(java.nio.FloatBuffer) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Vector3fc(org.joml.Vector3fc) Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Matrix3f(org.joml.Matrix3f) OpenGLSkeletalMesh(org.terasology.engine.rendering.opengl.OpenGLSkeletalMesh) Vector3f(org.joml.Vector3f) MeshAnimation(org.terasology.engine.rendering.assets.animation.MeshAnimation) Bone(org.terasology.engine.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 9 with Vector3fc

use of org.joml.Vector3fc in project Terasology by MovingBlocks.

the class Tessellator method addMeshPart.

private void addMeshPart(BlockMeshPart part, boolean doubleSided) {
    for (int i = 0; i < part.size(); ++i) {
        Vector3fc vertex = part.getVertex(i);
        meshData.position.put(vertex);
        meshData.color0.put(activeColor);
        meshData.normal.put(part.getNormal(i));
        meshData.uv0.put(part.getTexCoord(i));
        meshData.light0.put(lighting);
    }
    for (int i = 0; i < part.indicesSize(); ++i) {
        meshData.indices.put(nextIndex + part.getIndex(i));
    }
    if (doubleSided) {
        for (int i = 0; i < part.indicesSize(); i += 3) {
            meshData.indices.put(nextIndex + part.getIndex(i + 1));
            meshData.indices.put(nextIndex + part.getIndex(i));
            meshData.indices.put(nextIndex + part.getIndex(i + 2));
        }
    }
    nextIndex += part.size();
}
Also used : Vector3fc(org.joml.Vector3fc)

Example 10 with Vector3fc

use of org.joml.Vector3fc in project Terasology by MovingBlocks.

the class MeshBuilder method addPoly.

/**
 * @param v1
 * @param v2
 * @param v3
 * @param vn
 * @return
 */
public MeshBuilder addPoly(Vector3fc v1, Vector3fc v2, Vector3fc v3, Vector3fc... vn) {
    for (int i = 0; i < vn.length + 1; i++) {
        addIndices(vertexCount, vertexCount + i + 2, vertexCount + i + 1);
    }
    addVertex(v1);
    addVertex(v2);
    addVertex(v3);
    for (Vector3fc v : vn) {
        addVertex(v);
    }
    return this;
}
Also used : Vector3fc(org.joml.Vector3fc)

Aggregations

Vector3fc (org.joml.Vector3fc)19 Vector3f (org.joml.Vector3f)15 Test (org.junit.Test)8 VertexResourceBuilder (org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder)8 VertexResource (org.terasology.engine.rendering.assets.mesh.resource.VertexResource)7 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)4 LocationComponent (org.terasology.engine.logic.location.LocationComponent)4 FloatBuffer (java.nio.FloatBuffer)2 Matrix4f (org.joml.Matrix4f)2 Quaternionf (org.joml.Quaternionf)2 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)2 VertexByteAttributeBinding (org.terasology.engine.rendering.assets.mesh.resource.VertexByteAttributeBinding)2 Color (org.terasology.nui.Color)2 com.badlogic.gdx.physics.bullet.collision.btBoxShape (com.badlogic.gdx.physics.bullet.collision.btBoxShape)1 com.badlogic.gdx.physics.bullet.collision.btCapsuleShape (com.badlogic.gdx.physics.bullet.collision.btCapsuleShape)1 com.badlogic.gdx.physics.bullet.collision.btConvexHullShape (com.badlogic.gdx.physics.bullet.collision.btConvexHullShape)1 com.badlogic.gdx.physics.bullet.collision.btCylinderShape (com.badlogic.gdx.physics.bullet.collision.btCylinderShape)1 com.badlogic.gdx.physics.bullet.collision.btManifoldPoint (com.badlogic.gdx.physics.bullet.collision.btManifoldPoint)1 com.badlogic.gdx.physics.bullet.collision.btSphereShape (com.badlogic.gdx.physics.bullet.collision.btSphereShape)1 ItemOverlay (io.xol.chunkstories.api.item.interfaces.ItemOverlay)1