Search in sources :

Example 66 with Vector3d

use of org.joml.Vector3d in project chunkstories by Hugobros3.

the class ChunkMeshesRenderer method renderChunks.

@Override
public void renderChunks(RenderingInterface renderingInterface) {
    RenderPass currentPass = renderingInterface.getCurrentPass();
    List<ChunkRenderCommand> culledChunks = currentPass.name.startsWith("shadow") ? culledChunksShadow : culledChunksNormal;
    ShadingType shadingType = currentPass.name.startsWith("water") ? ShadingType.LIQUIDS : ShadingType.OPAQUE;
    if (currentPass.name.startsWith("shadow"))
        renderingInterface.currentShader().setUniform1f("useVoxelCoordinates", 1f);
    Matrix4f matrix = new Matrix4f();
    for (ChunkRenderCommand command : culledChunks) {
        matrix.identity();
        matrix.translate(new Vector3f(command.displayWorldX, command.displayWorldY, command.displayWorldZ));
        renderingInterface.setObjectMatrix(matrix);
        Vector3d chunkPos = new Vector3d(command.displayWorldX + 16, command.displayWorldY + 16, command.displayWorldZ + 16);
        double distance = renderingInterface.getCamera().getCameraPosition().distance(chunkPos);
        RenderLodLevel lodToUse;
        lodToUse = distance < Math.max(64, world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance") / 4.0) ? RenderLodLevel.HIGH : RenderLodLevel.LOW;
        ((ClientChunk) command.chunk).getChunkRenderData().renderPass(renderingInterface, lodToUse, shadingType);
    }
    if (currentPass.name.startsWith("shadow"))
        renderingInterface.currentShader().setUniform1f("useVoxelCoordinates", 0f);
}
Also used : ShadingType(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.ShadingType) Matrix4f(org.joml.Matrix4f) RenderLodLevel(io.xol.chunkstories.renderer.chunks.ChunkRenderDataHolder.RenderLodLevel) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) RenderPass(io.xol.chunkstories.api.rendering.pass.RenderPass)

Example 67 with Vector3d

use of org.joml.Vector3d in project chunkstories by Hugobros3.

the class BVHAnimation method main.

public static void main(String[] a) throws FileNotFoundException {
    BVHAnimation test = new BVHAnimation(new FileInputStream(new File("res/animations/human/human.bvh")));
    System.out.println(test.toString());
    float rotX = (float) (Math.random() * 2.0 * Math.PI);
    float rotY = (float) (Math.random() * 2.0 * Math.PI);
    float rotZ = (float) (Math.random() * 2.0 * Math.PI);
    Quaternion4d quaternionXLower = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotX);
    Quaternion4d quaternionYLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotY);
    Quaternion4d quaternionZLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZ);
    Quaternion4d total = new Quaternion4d(quaternionXLower);
    total = total.mult(quaternionYLower);
    total.normalize();
    total = total.mult(quaternionZLower);
    Matrix4f matrix = new Matrix4f();
    // Apply rotations
    matrix.rotate(rotX, new Vector3f(1, 0, 0));
    matrix.rotate(rotY, new Vector3f(0, 1, 0));
    matrix.rotate(rotZ, new Vector3f(0, 0, 1));
    Matrix4f mX = new Matrix4f();
    Matrix4f mY = new Matrix4f();
    Matrix4f mZ = new Matrix4f();
    mX.rotate(rotX, new Vector3f(1, 0, 0));
    mY.rotate(rotY, new Vector3f(0, 1, 0));
    mZ.rotate(rotZ, new Vector3f(0, 0, 1));
    /*System.out.println("Old:\n"+matrix);
		System.out.println("New:\n"+Matrix4f.mul(Matrix4f.mul(mX, mY, null), mZ, null));
		
		System.out.println("mX:\n"+mX);
		System.out.println("mY:\n"+mY);
		System.out.println("mZ:\n"+mZ);*/
    // System.out.println(quaternionXLower);
    // System.out.println(quaternionYLower);
    // System.out.println(quaternionZLower);
    mX = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotX).toMatrix4f();
    mY = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotY).toMatrix4f();
    mZ = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZ).toMatrix4f();
    // System.out.println("Old:\n"+matrix);
    // System.out.println("New:\n"+Matrix4f.mul(Matrix4f.mul(mX, mY, null), mZ, null));
    // System.out.println("mX:\n"+mX);
    // System.out.println("mY:\n"+mY);
    // System.out.println("mZ:\n"+mZ);
    // total = Matrix4f.invert(total, null);
    // System.out.println("Qml:\n"+total.toMatrix4f());
    // System.out.println("Inv:\n"+Matrix4f.invert(total.toMatrix4f(), null));
    // Swaps Y and Z axises arround
    // matrix = new Matrix4f();
    System.out.println(matrix);
    System.out.println("");
    Matrix4f blender2ingame = new Matrix4f();
    blender2ingame.m11(0);
    blender2ingame.m22(0);
    blender2ingame.m12(1);
    blender2ingame.m21(1);
    // Rotate the matrix first to apply the transformation in blender space
    blender2ingame.mul(matrix, matrix);
    // Matrix4f.mul(blender2ingame, matrix, matrix);
    // Mirror it
    Matrix4f mirror = new Matrix4f();
    mirror.m22(-1);
    mirror.mul(matrix, matrix);
    // Matrix4f.mul(mirror, matrix, matrix);
    // Rotate again after so it's back the correct way arround
    matrix.mul(blender2ingame);
    // Matrix4f.mul(matrix, blender2ingame, matrix);
    matrix.mul(mirror);
    // Matrix4f.mul(matrix, mirror, matrix);
    System.out.println(matrix);
}
Also used : Quaternion4d(io.xol.chunkstories.api.math.Quaternion4d) Matrix4f(org.joml.Matrix4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 68 with Vector3d

use of org.joml.Vector3d in project chunkstories by Hugobros3.

the class BVHTreeBone method getTransformationMatrixInterpolatedInternal.

private Matrix4f getTransformationMatrixInterpolatedInternal(int frameLower, int frameUpper, double t) {
    // Read rotation data from where it is
    float rotXLower;
    float rotYLower;
    float rotZLower;
    if (channels == 6) {
        rotXLower = toRad(animationData[frameLower][3]);
        rotYLower = toRad(animationData[frameLower][4]);
        rotZLower = toRad(animationData[frameLower][5]);
    } else {
        rotXLower = toRad(animationData[frameLower][0]);
        rotYLower = toRad(animationData[frameLower][1]);
        rotZLower = toRad(animationData[frameLower][2]);
    }
    float rotXUpper;
    float rotYUpper;
    float rotZUpper;
    if (channels == 6) {
        rotXUpper = toRad(animationData[frameUpper][3]);
        rotYUpper = toRad(animationData[frameUpper][4]);
        rotZUpper = toRad(animationData[frameUpper][5]);
    } else {
        rotXUpper = toRad(animationData[frameUpper][0]);
        rotYUpper = toRad(animationData[frameUpper][1]);
        rotZUpper = toRad(animationData[frameUpper][2]);
    }
    Quaternion4d quaternionXLower = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotXLower);
    Quaternion4d quaternionYLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotYLower);
    Quaternion4d quaternionZLower = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZLower);
    Quaternion4d totalLower = quaternionXLower.mult(quaternionYLower).mult(quaternionZLower);
    Quaternion4d quaternionXUpper = Quaternion4d.fromAxisAngle(new Vector3d(1.0, 0.0, 0.0), rotXUpper);
    Quaternion4d quaternionYUpper = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 1.0, 0.0), rotYUpper);
    Quaternion4d quaternionZUpper = Quaternion4d.fromAxisAngle(new Vector3d(0.0, 0.0, 1.0), rotZUpper);
    Quaternion4d totalUpper = (quaternionXUpper.mult(quaternionYUpper)).mult(quaternionZUpper);
    Quaternion4d total = Quaternion4d.slerp(totalLower, totalUpper, t);
    Matrix4f matrix = total.toMatrix4f();
    // Apply transformations
    if (channels == 6) {
        matrix.m30(matrix.m30() + Math2.mix(animationData[frameLower][0], animationData[frameUpper][0], t));
        matrix.m31(matrix.m31() + Math2.mix(animationData[frameLower][1], animationData[frameUpper][1], t));
        matrix.m32(matrix.m32() + Math2.mix(animationData[frameLower][2], animationData[frameUpper][2], t));
    } else // TODO check on that, I'm not sure if you should apply both when possible
    {
        matrix.m30(matrix.m30() + offset.x());
        matrix.m31(matrix.m31() + offset.y());
        matrix.m32(matrix.m32() + offset.z());
    }
    return BVHAnimation.transformBlenderBVHExportToChunkStoriesWorldSpace(matrix);
}
Also used : Quaternion4d(io.xol.chunkstories.api.math.Quaternion4d) Matrix4f(org.joml.Matrix4f) Vector3d(org.joml.Vector3d)

Example 69 with Vector3d

use of org.joml.Vector3d in project chunkstories by Hugobros3.

the class DefaultWorldCollisionsManager method raytraceSolid.

private Location raytraceSolid(Vector3dc initialPosition, Vector3dc directionIn, double limit, boolean outer, boolean selectable) {
    Vector3d direction = new Vector3d();
    directionIn.normalize(direction);
    // direction.scale(0.02);
    // float distance = 0f;
    CellData cell;
    // Voxel vox;
    int x, y, z;
    x = (int) Math.floor(initialPosition.x());
    y = (int) Math.floor(initialPosition.y());
    z = (int) Math.floor(initialPosition.z());
    // DDA algorithm
    // It requires double arrays because it works using loops over each dimension
    double[] rayOrigin = new double[3];
    double[] rayDirection = new double[3];
    rayOrigin[0] = initialPosition.x();
    rayOrigin[1] = initialPosition.y();
    rayOrigin[2] = initialPosition.z();
    rayDirection[0] = direction.x();
    rayDirection[1] = direction.y();
    rayDirection[2] = direction.z();
    int[] voxelCoords = new int[] { x, y, z };
    int[] voxelDelta = new int[] { 0, 0, 0 };
    double[] deltaDist = new double[3];
    double[] next = new double[3];
    int[] step = new int[3];
    int side = 0;
    // Prepare distances
    for (int i = 0; i < 3; ++i) {
        double deltaX = rayDirection[0] / rayDirection[i];
        double deltaY = rayDirection[1] / rayDirection[i];
        double deltaZ = rayDirection[2] / rayDirection[i];
        deltaDist[i] = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
        if (rayDirection[i] < 0.f) {
            step[i] = -1;
            next[i] = (rayOrigin[i] - voxelCoords[i]) * deltaDist[i];
        } else {
            step[i] = 1;
            next[i] = (voxelCoords[i] + 1.f - rayOrigin[i]) * deltaDist[i];
        }
    }
    do {
        // DDA steps
        side = 0;
        for (int i = 1; i < 3; ++i) {
            if (next[side] > next[i]) {
                side = i;
            }
        }
        next[side] += deltaDist[side];
        voxelCoords[side] += step[side];
        voxelDelta[side] += step[side];
        x = voxelCoords[0];
        y = voxelCoords[1];
        z = voxelCoords[2];
        cell = world.peekSafely(x, y, z);
        if (cell.getVoxel().getDefinition().isSolid() || (selectable && cell.getVoxel().getDefinition().isSelectable())) {
            boolean collides = false;
            for (CollisionBox box : cell.getTranslatedCollisionBoxes()) {
                // System.out.println(box);
                Vector3dc collisionPoint = box.lineIntersection(initialPosition, direction);
                if (collisionPoint != null) {
                    collides = true;
                // System.out.println("collides @ "+collisionPoint);
                }
            }
            if (collides) {
                if (!outer)
                    return new Location(world, x, y, z);
                else {
                    // Back off a bit
                    switch(side) {
                        case 0:
                            x -= step[side];
                            break;
                        case 1:
                            y -= step[side];
                            break;
                        case 2:
                            z -= step[side];
                            break;
                    }
                    return new Location(world, x, y, z);
                }
            }
        }
    // distance += deltaDist[side];
    } while (voxelDelta[0] * voxelDelta[0] + voxelDelta[1] * voxelDelta[1] + voxelDelta[2] * voxelDelta[2] < limit * limit);
    return null;
}
Also used : Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) CellData(io.xol.chunkstories.api.world.cell.CellData) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox) Location(io.xol.chunkstories.api.Location)

Example 70 with Vector3d

use of org.joml.Vector3d in project chunkstories by Hugobros3.

the class DefaultWorldCollisionsManager method rayTraceEntities.

@Override
public Iterator<Entity> rayTraceEntities(Vector3dc initialPosition, Vector3dc direction, double limit) {
    double blocksLimit = limit;
    Vector3d blocksCollision = this.raytraceSolid(initialPosition, direction, limit);
    if (blocksCollision != null)
        blocksLimit = blocksCollision.distance(initialPosition);
    return raytraceEntitiesIgnoringVoxels(initialPosition, direction, Math.min(blocksLimit, limit));
}
Also used : Vector3d(org.joml.Vector3d)

Aggregations

Vector3d (org.joml.Vector3d)117 Vector3dc (org.joml.Vector3dc)33 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)20 BlockPos (net.minecraft.util.math.BlockPos)19 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)18 Location (io.xol.chunkstories.api.Location)12 Entity (io.xol.chunkstories.api.entity.Entity)11 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)9 World (net.minecraft.world.World)9 ShipData (org.valkyrienskies.mod.common.ships.ShipData)9 WorldClient (io.xol.chunkstories.api.world.WorldClient)8 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)8 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)7 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)6 IBlockState (net.minecraft.block.state.IBlockState)6 Vec3d (net.minecraft.util.math.Vec3d)6 Vector3f (org.joml.Vector3f)6 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5