Search in sources :

Example 6 with AABBf

use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.

the class MeshRenderer method renderEntitiesByMaterial.

private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
    Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quaternionf worldRot = new Quaternionf();
    Vector3f worldPos = new Vector3f();
    Matrix3f normalMatrix = new Matrix3f();
    Matrix4f matrixCameraSpace = new Matrix4f();
    Matrix4f modelViewMatrix = new Matrix4f();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    for (Material material : meshByMaterial.keySet()) {
        if (material.isRenderable()) {
            material.enable();
            material.setFloat("sunlight", 1.0f, true);
            material.setFloat("blockLight", 1.0f, true);
            material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
            material.bindTextures();
            Set<EntityRef> entities = meshByMaterial.get(material);
            lastRendered = entities.size();
            for (EntityRef entity : entities) {
                MeshComponent meshComp = entity.getComponent(MeshComponent.class);
                LocationComponent location = entity.getComponent(LocationComponent.class);
                if (isHidden(entity, meshComp) || location == null || meshComp.mesh == null) {
                    continue;
                }
                Vector3f worldPosition = location.getWorldPosition(new Vector3f());
                if (!worldPosition.isFinite() && !isRelevant(entity, worldPosition)) {
                    continue;
                }
                if (meshComp.mesh.isDisposed()) {
                    logger.error("Attempted to render disposed mesh");
                    continue;
                }
                worldRot.set(location.getWorldRotation(new Quaternionf()));
                worldPos.set(location.getWorldPosition(new Vector3f()));
                float worldScale = location.getWorldScale();
                Vector3f offsetFromCamera = worldPos.sub(cameraPosition, new Vector3f());
                matrixCameraSpace.translationRotateScale(offsetFromCamera, worldRot, worldScale);
                AABBf aabb = meshComp.mesh.getAABB().transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
                if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
                    modelViewMatrix.set(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
                    modelViewMatrix.get(tempMatrixBuffer44);
                    modelViewMatrix.normal(normalMatrix).get(tempMatrixBuffer33);
                    material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
                    material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
                    material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
                    material.setFloat3("colorOffset", meshComp.color.rf(), meshComp.color.gf(), meshComp.color.bf(), true);
                    material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
                    material.setFloat("blockLight", Math.max(worldRenderer.getBlockLightIntensityAt(worldPos), meshComp.selfLuminance), true);
                    meshComp.mesh.render();
                }
            }
        }
    }
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Matrix3f(org.joml.Matrix3f) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) FloatBuffer(java.nio.FloatBuffer) Material(org.terasology.engine.rendering.assets.material.Material) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 7 with AABBf

use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.

the class SkeletalMeshDataBuilder method build.

public SkeletalMeshData build() {
    int rootBones = 0;
    for (Bone bone : bones) {
        if (bone.getParent() == null) {
            rootBones++;
        }
    }
    Vector3f minOfAABB = new Vector3f(vertices.get(0));
    Vector3f maxOfAABB = new Vector3f(vertices.get(0));
    for (Vector3f vert : vertices) {
        minOfAABB.min(vert);
        maxOfAABB.max(vert);
    }
    if (rootBones == 0) {
        throw new IllegalStateException("Cannot create a skeleton with no root bones");
    } else if (rootBones > 1) {
        throw new IllegalStateException("Cannot create a skeleton with multiple root bones");
    }
    AABBf staticAabb = new AABBf(minOfAABB, maxOfAABB);
    return new SkeletalMeshData(bones, vertices, normals, weights, uvs, indices, staticAabb);
}
Also used : AABBf(org.terasology.joml.geom.AABBf) Vector3f(org.joml.Vector3f)

Example 8 with AABBf

use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.

the class AABBTypeHandlerTest method testSerializeAABB1.

@Test
public void testSerializeAABB1() {
    AABB3Test aabb3 = new AABB3Test();
    aabb3.a1 = new AABBf(10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
    aabb3.a2 = new AABBi(0, 0, 0, 10, 10, 10);
    JsonElement tree = gson.toJsonTree(aabb3);
    JsonObject obj = tree.getAsJsonObject();
    assertTrue(obj.has("a1"));
    assertAABBf(obj.get("a1"), 10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
    assertTrue(obj.has("a2"));
    assertAABBi(obj.get("a2"), 0, 0, 0, 10, 10, 10);
}
Also used : AABBf(org.terasology.joml.geom.AABBf) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) AABBi(org.terasology.joml.geom.AABBi) Test(org.junit.jupiter.api.Test)

Example 9 with AABBf

use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.

the class AABBTypeHandlerTest method testSerializeAABB1Missing.

@Test
public void testSerializeAABB1Missing() {
    AABB3Test aabb3 = new AABB3Test();
    aabb3.a1 = new AABBf(10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
    JsonElement tree = gson.toJsonTree(aabb3);
    JsonObject obj = tree.getAsJsonObject();
    assertTrue(obj.has("a1"));
    assertAABBf(obj.get("a1"), 10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
    assertFalse(obj.has("a2"));
}
Also used : AABBf(org.terasology.joml.geom.AABBf) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Test(org.junit.jupiter.api.Test)

Example 10 with AABBf

use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.

the class AABBfTypeHandler method deserialize.

@Override
public Optional<AABBf> deserialize(PersistedData data) {
    if (!data.isNull() && data.isValueMap()) {
        PersistedDataMap map = data.getAsValueMap();
        PersistedDataArray minDataArr = map.get(MIN_FIELD).getAsArray();
        PersistedDataArray maxDataArr = map.get(MAX_FIELD).getAsArray();
        TFloatList minArr = minDataArr.getAsFloatArray();
        TFloatList maxArr = maxDataArr.getAsFloatArray();
        return Optional.of(new AABBf(minArr.get(0), minArr.get(1), minArr.get(2), maxArr.get(0), maxArr.get(1), maxArr.get(2)));
    }
    return Optional.empty();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) AABBf(org.terasology.joml.geom.AABBf) PersistedDataMap(org.terasology.persistence.typeHandling.PersistedDataMap) TFloatList(gnu.trove.list.TFloatList)

Aggregations

AABBf (org.terasology.joml.geom.AABBf)13 Vector3f (org.joml.Vector3f)7 Matrix4f (org.joml.Matrix4f)5 Quaternionf (org.joml.Quaternionf)5 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 Test (org.junit.jupiter.api.Test)3 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)3 LocationComponent (org.terasology.engine.logic.location.LocationComponent)3 TFloatList (gnu.trove.list.TFloatList)2 FloatBuffer (java.nio.FloatBuffer)2 Matrix3f (org.joml.Matrix3f)2 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)2 TIntList (gnu.trove.list.TIntList)1 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Vector3fc (org.joml.Vector3fc)1 Physics (org.terasology.engine.physics.Physics)1