Search in sources :

Example 96 with Vector3f

use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.

the class LocationComponentTest method testSetWorldPositionWorksWithNestedRotatedParent.

@Test
public void testSetWorldPositionWorksWithNestedRotatedParent() {
    LocationComponent first = new LocationComponent();
    EntityRef firstEntity = createFakeEntityWith(first);
    LocationComponent second = new LocationComponent();
    EntityRef secondEntity = createFakeEntityWith(second);
    LocationComponent third = new LocationComponent();
    EntityRef thirdEntity = createFakeEntityWith(third);
    Location.attachChild(firstEntity, secondEntity);
    second.setLocalPosition(new Vector3f(1, 0, 0));
    first.setLocalRotation(yawRotation);
    TeraAssert.assertEquals(new Vector3f(0, 0, -1), second.getWorldPosition(), 0.000001f);
    Location.attachChild(secondEntity, thirdEntity);
    second.setLocalRotation(pitchRotation);
    third.setLocalPosition(new Vector3f(0, 0, 0));
    TeraAssert.assertEquals(new Vector3f(0, 0, -1), third.getWorldPosition(), 0.000001f);
    third.setLocalPosition(new Vector3f(0, 0, 1));
    TeraAssert.assertEquals(new Vector3f(0.5f * (float) Math.sqrt(2), -0.5f * (float) Math.sqrt(2), -1), third.getWorldPosition(), 0.000001f);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 97 with Vector3f

use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.

the class LocationComponentTest method testPositionMaintainedWhenAttachedToParent.

@Test
public void testPositionMaintainedWhenAttachedToParent() {
    LocationComponent parent = new LocationComponent();
    EntityRef parentEntity = createFakeEntityWith(parent);
    parent.setWorldPosition(new Vector3f(1, 0, 0));
    loc.setWorldPosition(new Vector3f(2, 0, 0));
    Location.attachChild(parentEntity, entity);
    TeraAssert.assertEquals(new Vector3f(2, 0, 0), loc.getWorldPosition(), 0.000001f);
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Example 98 with Vector3f

use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.

the class ChunkMesh method render.

/**
 * Renders the phase-appropriate mesh of the chunk. I.e. if the RenderPhase is OPAQUE only the opaque mesh
 * is rendered: other meshes stored in an instance of this class are rendered in separate rendering steps.
 *
 * @param phase a RenderPhase value
 * @param chunkPosition a Vector3f storing the world position of the chunk.
 * @param cameraPosition a Vector3f storing the world position of the point of view from which the chunk is rendered.
 * @return Returns an integer representing the number of triangles rendered.
 */
public int render(ChunkMesh.RenderPhase phase, Vector3f chunkPosition, Vector3f cameraPosition) {
    GL11.glPushMatrix();
    // chunkPositionRelativeToCamera = chunkCoordinates * chunkDimensions - cameraCoordinate
    final Vector3f chunkPositionRelativeToCamera = new Vector3f(chunkPosition.x * ChunkConstants.SIZE_X - cameraPosition.x, chunkPosition.y * ChunkConstants.SIZE_Y - cameraPosition.y, chunkPosition.z * ChunkConstants.SIZE_Z - cameraPosition.z);
    GL11.glTranslatef(chunkPositionRelativeToCamera.x, chunkPositionRelativeToCamera.y, chunkPositionRelativeToCamera.z);
    // this is where the chunk is actually rendered
    render(phase);
    GL11.glPopMatrix();
    return triangleCount();
}
Also used : Vector3f(org.terasology.math.geom.Vector3f)

Example 99 with Vector3f

use of org.terasology.math.geom.Vector3f in project Terasology by MovingBlocks.

the class RegionOutlineRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (entityToRegionOutlineMap.isEmpty()) {
        // skip everything if there is nothing to do to avoid possibly costly draw mode changes
        return;
    }
    glDisable(GL_DEPTH_TEST);
    glLineWidth(2);
    Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
    FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
    FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
    material.setFloat("sunlight", 1.0f, true);
    material.setFloat("blockLight", 1.0f, true);
    material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
    Vector3f worldPos = new Vector3f();
    Vector3f worldPositionCameraSpace = new Vector3f();
    worldPositionCameraSpace.sub(worldPos, cameraPosition);
    Matrix4f matrixCameraSpace = new Matrix4f(new Quat4f(0, 0, 0, 1), worldPositionCameraSpace, 1.0f);
    Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
    MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
    material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true);
    MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
    material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
    for (RegionOutlineComponent regionOutline : entityToRegionOutlineMap.values()) {
        material.setFloat3("colorOffset", regionOutline.color.rf(), regionOutline.color.gf(), regionOutline.color.bf(), true);
        drawRegionOutline(regionOutline);
    }
    glEnable(GL_DEPTH_TEST);
}
Also used : Matrix4f(org.terasology.math.geom.Matrix4f) Vector3f(org.terasology.math.geom.Vector3f) FloatBuffer(java.nio.FloatBuffer) Quat4f(org.terasology.math.geom.Quat4f)

Example 100 with Vector3f

use of org.terasology.math.geom.Vector3f 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

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