Add last values

This commit is contained in:
Kneelawk 2024-03-01 14:48:06 -08:00
parent 116b2df6a9
commit 7eb7c4cca0
No known key found for this signature in database
GPG Key ID: DB4E28870D6D260A
2 changed files with 55 additions and 11 deletions

View File

@ -2,13 +2,14 @@ package com.jozufozu.flywheel.backend.engine.uniform;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.lwjgl.system.MemoryUtil;
import com.jozufozu.flywheel.api.event.RenderContext;
import com.jozufozu.flywheel.api.visualization.VisualizationManager;
import com.jozufozu.flywheel.lib.math.MatrixMath;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
@ -22,10 +23,19 @@ public class FrameUniforms implements UniformProvider {
private final Matrix4f view = new Matrix4f();
private final Matrix4f viewInverse = new Matrix4f();
private final Matrix4f lastView = new Matrix4f();
private final Matrix4f projection = new Matrix4f();
private final Matrix4f projectionInverse = new Matrix4f();
private final Matrix4f lastProjection = new Matrix4f();
private final Matrix4f viewProjection = new Matrix4f();
private final Matrix4f viewProjectionInverse = new Matrix4f();
private final Matrix4f lastViewProjection = new Matrix4f();
private final Vector3f lastCameraPosition = new Vector3f();
private final Vector3f lastCameraLook = new Vector3f();
private final Vector2f lastCameraRot = new Vector2f();
private boolean lastInit = false;
public int byteSize() {
return SIZE;
@ -63,9 +73,30 @@ public class FrameUniforms implements UniformProvider {
ptr += 96;
// manage last values of matrices
if (!lastInit) {
lastView.set(view);
lastProjection.set(projection);
lastViewProjection.set(lastViewProjection);
}
ptr = writeMatrices(ptr);
lastView.set(view);
lastProjection.set(projection);
lastViewProjection.set(viewProjection);
ptr = writeCamera(ptr, camX, camY, camZ, camera);
ptr = writeCamera(ptr, camX, camY, camZ, camera.getLookVector(), camera.getXRot(), camera.getYRot());
// last values for camera
if (!lastInit) {
lastCameraPosition.set(camX, camY, camZ);
lastCameraLook.set(camera.getLookVector());
lastCameraRot.set(camera.getXRot(), camera.getYRot());
}
ptr = writeCamera(ptr, lastCameraPosition.x, lastCameraPosition.y, lastCameraPosition.z, lastCameraLook,
lastCameraRot.x, lastCameraRot.y);
lastCameraPosition.set(camX, camY, camZ);
lastCameraLook.set(camera.getLookVector());
lastCameraRot.set(camera.getXRot(), camera.getYRot());
var window = Minecraft.getInstance()
.getWindow();
@ -81,25 +112,30 @@ public class FrameUniforms implements UniformProvider {
ptr = writeTime(ptr);
MemoryUtil.memPutInt(ptr, debugMode);
lastInit = true;
}
private long writeMatrices(long ptr) {
MatrixMath.writeUnsafe(view, ptr);
MatrixMath.writeUnsafe(view.invert(viewInverse), ptr + 64);
MatrixMath.writeUnsafe(projection, ptr + 64 * 2);
MatrixMath.writeUnsafe(projection.invert(projectionInverse), ptr + 64 * 3);
MatrixMath.writeUnsafe(viewProjection, ptr + 64 * 4);
MatrixMath.writeUnsafe(viewProjection.invert(viewProjectionInverse), ptr + 64 * 5);
return ptr + 64 * 6;
MatrixMath.writeUnsafe(lastView, ptr + 64 * 2);
MatrixMath.writeUnsafe(projection, ptr + 64 * 3);
MatrixMath.writeUnsafe(projection.invert(projectionInverse), ptr + 64 * 4);
MatrixMath.writeUnsafe(lastProjection, ptr + 64 * 5);
MatrixMath.writeUnsafe(viewProjection, ptr + 64 * 6);
MatrixMath.writeUnsafe(viewProjection.invert(viewProjectionInverse), ptr + 64 * 7);
MatrixMath.writeUnsafe(lastViewProjection, ptr + 64 * 8);
return ptr + 64 * 9;
}
private static long writeCamera(long ptr, float camX, float camY, float camZ, Camera camera) {
private static long writeCamera(long ptr, float camX, float camY, float camZ, Vector3f lookVector, float xRot,
float yRot) {
ptr = writeVec3(ptr, camX, camY, camZ);
var lookVector = camera.getLookVector();
ptr = writeVec3(ptr, lookVector.x, lookVector.y, lookVector.z);
ptr = writeVec2(ptr, camera.getXRot(), camera.getYRot());
ptr = writeVec2(ptr, xRot, yRot);
return ptr;
}

View File

@ -14,14 +14,20 @@ layout(std140) uniform _FlwFrameUniforms {
mat4 flw_view;
mat4 flw_viewInverse;
mat4 flw_lastView;
mat4 flw_projection;
mat4 flw_projectionInverse;
mat4 flw_lastProjection;
mat4 flw_viewProjection;
mat4 flw_viewProjectionInverse;
mat4 flw_lastViewProjection;
vec4 _flw_cameraPos;
vec4 _flw_cameraLook;
vec2 flw_cameraRot;
vec4 _flw_lastCameraPos;
vec4 _flw_lastCameraLook;
vec2 flw_lastCameraRot;
vec2 flw_viewportSize;
float flw_defaultLineWidth;
@ -39,3 +45,5 @@ layout(std140) uniform _FlwFrameUniforms {
#define flw_cameraPos _flw_cameraPos.xyz
#define flw_cameraLook _flw_cameraLook.xyz
#define flw_lastCameraPos _flw_lastCameraPos.xyz
#define flw_lastCameraLook _flw_lastCameraLook.xyz