Search in sources :

Example 36 with Matrix4f

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

the class BVHAnimation method getBoneHierarchyTransformationMatrix.

/*public Matrix4f getBoneHierarchyTransformationMatrix(String boneName, double animationTime) {
		BVHTreeBone bone = getBone(boneName);
		return bone.getTransformationMatrix(animationTime);
	}*/
public Matrix4f getBoneHierarchyTransformationMatrix(String boneName, double animationTime) {
    Matrix4f matrix = new Matrix4f();
    if (frames == 0) {
        System.out.println("fack you");
        return matrix;
    }
    double frame = animationTime / 1000.0 / frameTime;
    double frameUpperBound = Math.ceil(frame);
    double frameLowerBound = Math.floor(frame);
    double interp = frame % 1.0;
    // Don't try to interpolate if we're on an exact frame
    if (frameLowerBound == frameUpperBound)
        interp = 0.0;
    int frameLower = (int) (frameLowerBound) % frames;
    int frameUpper = (int) (frameUpperBound) % frames;
    matrix = getBone(boneName).getTransformationMatrixInterpolatedRecursive(frameLower, frameUpper, interp);
    return matrix;
}
Also used : Matrix4f(org.joml.Matrix4f)

Example 37 with Matrix4f

use of org.joml.Matrix4f 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 38 with Matrix4f

use of org.joml.Matrix4f in project chunkstories-core by Hugobros3.

the class HitBoxImpl method lineIntersection.

/**
 * Tricky maths; transforms the inbound ray so the hitbox would be at 0.0.0 and axis-aligned
 */
public Vector3dc lineIntersection(Vector3dc lineStart, Vector3dc lineDirection) {
    Matrix4f fromAABBToWorld = new Matrix4f(entity.getAnimatedSkeleton().getBoneHierarchyTransformationMatrix(skeletonPart, System.currentTimeMillis() % 1000000));
    Matrix4f worldPositionTransformation = new Matrix4f();
    Location entityLoc = entity.getLocation();
    Vector3f pos = new Vector3f((float) entityLoc.x, (float) entityLoc.y, (float) entityLoc.z);
    worldPositionTransformation.translate(pos);
    // Creates from AABB space to worldspace
    worldPositionTransformation.mul(fromAABBToWorld, fromAABBToWorld);
    // Invert it.
    Matrix4f fromWorldToAABB = new Matrix4f();
    fromAABBToWorld.invert(fromWorldToAABB);
    // Transform line start into AABB space
    Vector4f lineStart4 = new Vector4f((float) lineStart.x(), (float) lineStart.y(), (float) lineStart.z(), 1.0f);
    Vector4f lineDirection4 = new Vector4f((float) lineDirection.x(), (float) lineDirection.y(), (float) lineDirection.z(), 0.0f);
    fromWorldToAABB.transform(lineStart4);
    fromWorldToAABB.transform(lineDirection4);
    Vector3d lineStartTransformed = new Vector3d(lineStart4.x(), lineStart4.y(), lineStart4.z());
    Vector3d lineDirectionTransformed = new Vector3d(lineDirection4.x(), lineDirection4.y(), lineDirection4.z());
    // Actual computation
    Vector3dc hitPoint = box.lineIntersection(lineStartTransformed, lineDirectionTransformed);
    if (hitPoint == null)
        return null;
    // Transform hitPoint back into world
    Vector4f hitPoint4 = new Vector4f((float) hitPoint.x(), (float) hitPoint.y(), (float) hitPoint.z(), 1.0f);
    fromAABBToWorld.transform(hitPoint4);
    return new Vector3d((double) (float) hitPoint4.x(), (double) (float) hitPoint4.y(), (double) (float) hitPoint4.z());
}
Also used : Vector3dc(org.joml.Vector3dc) Matrix4f(org.joml.Matrix4f) Vector4f(org.joml.Vector4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) Location(io.xol.chunkstories.api.Location)

Example 39 with Matrix4f

use of org.joml.Matrix4f in project chunkstories-core by Hugobros3.

the class HitBoxImpl method draw.

/**
 * Debug method to figure out if the hitbox match with the model
 */
public void draw(RenderingInterface context) {
    if (!context.currentShader().getShaderName().equals("overlay")) {
        context.useShader("overlay");
        context.getCamera().setupShader(context.currentShader());
    }
    context.currentShader().setUniform1i("doTransform", 1);
    Matrix4f boneTransormation = new Matrix4f(entity.getAnimatedSkeleton().getBoneHierarchyTransformationMatrix(skeletonPart, System.currentTimeMillis() % 1000000));
    Matrix4f worldPositionTransformation = new Matrix4f();
    Location loc = entity.getLocation();
    Vector3f pos = new Vector3f((float) loc.x, (float) loc.y, (float) loc.z);
    worldPositionTransformation.translate(pos);
    worldPositionTransformation.mul(boneTransormation, boneTransormation);
    // Scales/moves the identity box to reflect collisionBox shape
    boneTransormation.translate(new Vector3f((float) box.xpos, (float) box.ypos, (float) box.zpos));
    boneTransormation.scale(new Vector3f((float) box.xw, (float) box.h, (float) box.zw));
    context.currentShader().setUniformMatrix4f("transform", boneTransormation);
    context.unbindAttributes();
    context.bindAttribute("vertexIn", context.meshes().getIdentityCube().asAttributeSource(VertexFormat.FLOAT, 3));
    context.currentShader().setUniform4f("colorIn", 0.0, 1.0, 0.0, 1.0);
    // Check for intersection with player
    EntityControllable ec = ((WorldClient) entity.getWorld()).getClient().getPlayer().getControlledEntity();
    if (ec != null) {
        if (lineIntersection((Vector3d) context.getCamera().getCameraPosition(), ((EntityPlayer) ec).getDirectionLookingAt()) != null)
            context.currentShader().setUniform4f("colorIn", 1.0, 0.0, 0.0, 1.0);
    }
    context.draw(Primitive.LINE, 0, 24);
    context.currentShader().setUniform1i("doTransform", 0);
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldClient(io.xol.chunkstories.api.world.WorldClient) Location(io.xol.chunkstories.api.Location)

Example 40 with Matrix4f

use of org.joml.Matrix4f in project chunkstories-core by Hugobros3.

the class ShadowPass method render.

public void render(RenderingInterface renderingContext) {
    if (this.getShadowVisibility() == 0f)
        // No shadows at night :)
        return;
    GameWindow gameWindow = pipeline.getRenderingInterface().getWindow();
    // Resize the texture if needed
    int shadowMapTextureSize = renderingContext.getClient().getConfiguration().getIntOption("client.rendering.shadowsResolution");
    if (shadowDepthTexture.getWidth() != shadowMapTextureSize) {
        fbo.resize(shadowMapTextureSize, shadowMapTextureSize);
    }
    // The size of the shadow range depends on the shadowmap resolution
    int shadowRange = 128;
    if (shadowMapTextureSize > 1024)
        shadowRange = 192;
    else if (shadowMapTextureSize > 2048)
        shadowRange = 256;
    int shadowDepthRange = 200;
    // Builds the shadow matrix
    // MatrixHelper.getOrthographicMatrix(-shadowRange, shadowRange, -shadowRange, shadowRange, -shadowDepthRange, shadowDepthRange);
    Matrix4f depthProjectionMatrix = new Matrix4f().ortho(-shadowRange, shadowRange, -shadowRange, shadowRange, -shadowDepthRange, shadowDepthRange);
    Matrix4f depthViewMatrix = new Matrix4f().lookAt(skyRenderer.getSunPosition(), new Vector3f(0, 0, 0), new Vector3f(0, 1, 0));
    Matrix4f shadowMVP = new Matrix4f();
    depthProjectionMatrix.mul(depthViewMatrix, shadowMVP);
    // Matrix4f.mul(depthProjectionMatrix, depthViewMatrix, shadowMVP);
    shadowMatrix = new Matrix4f(shadowMVP);
    Vector3dc posd = renderingContext.getCamera().getCameraPosition();
    Vector3f pos = new Vector3f((float) posd.x(), (float) posd.y(), (float) posd.z());
    pos.negate();
    shadowMVP.translate(pos);
    // Set appropriate fixed function stuff
    renderingContext.setCullingMode(CullingMode.COUNTERCLOCKWISE);
    renderingContext.setBlendMode(BlendMode.DISABLED);
    renderingContext.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
    // Bind relevant FBO and clear it
    renderingContext.getRenderTargetManager().setConfiguration(fbo);
    renderingContext.getRenderTargetManager().clearBoundRenderTargetZ(1.0f);
    Shader shadowsPassShader = renderingContext.useShader("shadows");
    shadowsPassShader.setUniform1f("animationTimer", worldRenderer.getAnimationTimer());
    shadowsPassShader.setUniformMatrix4f("depthMVP", shadowMVP);
    shadowsPassShader.setUniform1f("isUsingInstancedData", 0f);
    shadowsPassShader.setUniform1f("useVoxelCoordinates", 1f);
    Texture2D blocksAlbedoTexture = gameWindow.getClient().getContent().voxels().textures().getDiffuseAtlasTexture();
    renderingContext.bindAlbedoTexture(blocksAlbedoTexture);
    renderingContext.setObjectMatrix(null);
    // We render the world from that perspective
    // Hackish way of enabling the shader input for the fake "wind" effect vegetation can have
    shadowsPassShader.setUniform1f("allowForWavyStuff", 1);
    worldRenderer.getChunksRenderer().renderChunks(renderingContext);
    // In turn, disabling it while we do the entities
    shadowsPassShader.setUniform1f("allowForWavyStuff", 0);
    worldRenderer.getEntitiesRenderer().renderEntities(renderingContext);
}
Also used : GameWindow(io.xol.chunkstories.api.rendering.GameWindow) Vector3dc(org.joml.Vector3dc) Matrix4f(org.joml.Matrix4f) Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Vector3f(org.joml.Vector3f) Shader(io.xol.chunkstories.api.rendering.shader.Shader)

Aggregations

Matrix4f (org.joml.Matrix4f)62 Vector3f (org.joml.Vector3f)34 Quaternionf (org.joml.Quaternionf)13 LocationComponent (org.terasology.engine.logic.location.LocationComponent)7 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)5 Vector3d (org.joml.Vector3d)5 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)5 Shader (io.xol.chunkstories.api.rendering.shader.Shader)4 AABBf (org.terasology.joml.geom.AABBf)4 com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject (com.badlogic.gdx.physics.bullet.collision.btPairCachingGhostObject)3 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)3 Location (io.xol.chunkstories.api.Location)3 GameWindow (io.xol.chunkstories.api.rendering.GameWindow)3 ByteBuffer (java.nio.ByteBuffer)3 Matrix3f (org.joml.Matrix3f)3 Vector4f (org.joml.Vector4f)3 PointerBuffer (org.lwjgl.PointerBuffer)3 Camera (org.terasology.engine.rendering.cameras.Camera)3 ClosestRayResultCallback (com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback)2 TFloatList (gnu.trove.list.TFloatList)2