Search in sources :

Example 1 with OpenGLSkeletalMesh

use of org.terasology.rendering.opengl.OpenGLSkeletalMesh in project Terasology by MovingBlocks.

the class SkeletonRenderer method renderOpaque.

@Override
public void renderOpaque() {
    Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
    Quat4f worldRot = new Quat4f();
    Vector3f worldPos = new Vector3f();
    Quat4f inverseWorldRot = new Quat4f();
    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;
        }
        AABB 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);
        inverseWorldRot.inverse(worldRot);
        location.getWorldPosition(worldPos);
        float worldScale = location.getWorldScale();
        aabb = aabb.transform(worldRot, worldPos, worldScale);
        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();
        worldPositionCameraSpace.sub(worldPos, cameraPosition);
        worldPos.y -= skeletalMesh.heightOffset;
        Matrix4f matrixCameraSpace = new Matrix4f(worldRot, worldPositionCameraSpace, worldScale);
        Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
        MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
        skeletalMesh.material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true);
        MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
        skeletalMesh.material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
        skeletalMesh.material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
        skeletalMesh.material.setFloat("blockLight", worldRenderer.getBlockLightIntensityAt(worldPos), true);
        List<Vector3f> bonePositions = Lists.newArrayListWithCapacity(skeletalMesh.mesh.getVertexCount());
        List<Quat4f> boneRotations = Lists.newArrayListWithCapacity(skeletalMesh.mesh.getVertexCount());
        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) {
                Vector3f pos = boneLocation.getWorldPosition();
                pos.sub(worldPos);
                inverseWorldRot.rotate(pos, pos);
                bonePositions.add(pos);
                Quat4f rot = new Quat4f(inverseWorldRot);
                rot.mul(boneLocation.getWorldRotation());
                boneRotations.add(rot);
            } else {
                logger.warn("Unable to resolve bone \"{}\"", bone.getName());
                bonePositions.add(new Vector3f());
                boneRotations.add(new Quat4f());
            }
        }
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
        ((OpenGLSkeletalMesh) skeletalMesh.mesh).render(bonePositions, boneRotations);
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) LocationComponent(org.terasology.logic.location.LocationComponent) Matrix4f(org.terasology.math.geom.Matrix4f) OpenGLSkeletalMesh(org.terasology.rendering.opengl.OpenGLSkeletalMesh) Vector3f(org.terasology.math.geom.Vector3f) BaseVector3f(org.terasology.math.geom.BaseVector3f) MeshAnimation(org.terasology.rendering.assets.animation.MeshAnimation) Bone(org.terasology.rendering.assets.skeletalmesh.Bone) EntityRef(org.terasology.entitySystem.entity.EntityRef) BaseQuat4f(org.terasology.math.geom.BaseQuat4f) Quat4f(org.terasology.math.geom.Quat4f) AABB(org.terasology.math.AABB)

Aggregations

FloatBuffer (java.nio.FloatBuffer)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 AABB (org.terasology.math.AABB)1 BaseQuat4f (org.terasology.math.geom.BaseQuat4f)1 BaseVector3f (org.terasology.math.geom.BaseVector3f)1 Matrix4f (org.terasology.math.geom.Matrix4f)1 Quat4f (org.terasology.math.geom.Quat4f)1 Vector3f (org.terasology.math.geom.Vector3f)1 MeshAnimation (org.terasology.rendering.assets.animation.MeshAnimation)1 Bone (org.terasology.rendering.assets.skeletalmesh.Bone)1 OpenGLSkeletalMesh (org.terasology.rendering.opengl.OpenGLSkeletalMesh)1