Camera now stores last applied position and direction

Also FloatMathUtils now has sin, cos and tan
This commit is contained in:
OLEGSHA 2020-10-04 19:50:52 +03:00
parent a9d2a7f6c5
commit b59c4bdc2b
2 changed files with 110 additions and 3 deletions

View File

@ -18,6 +18,7 @@
package ru.windcorp.progressia.client.graphics.world; package ru.windcorp.progressia.client.graphics.world;
import static java.lang.Math.*; import static java.lang.Math.*;
import static ru.windcorp.progressia.common.util.FloatMathUtils.*;
import java.util.Collection; import java.util.Collection;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -74,11 +75,32 @@ public class Camera {
private float fieldOfView; private float fieldOfView;
/*
* Cache
*/
private final Vec3 lastAnchorPosition = new Vec3();
private float lastAnchorYaw;
private float lastAnchorPitch;
private final Mat4 lastCameraMatrix = new Mat4();
private final Vec3 lastAnchorLookingAt = new Vec3();
private final Vec3 lastAnchorUp = new Vec3();
{
invalidateCache();
}
public Camera(float fieldOfView) { public Camera(float fieldOfView) {
setFieldOfView(fieldOfView); setFieldOfView(fieldOfView);
} }
public Camera() {} public Camera() {}
/*
* apply() and subroutines
*/
public void apply(WorldRenderHelper helper) { public void apply(WorldRenderHelper helper) {
applyPerspective(helper); applyPerspective(helper);
@ -87,6 +109,8 @@ public class Camera {
applyMode(helper); applyMode(helper);
applyDirection(helper); applyDirection(helper);
applyPosition(helper); applyPosition(helper);
cacheCameraTransform(helper);
} }
private void applyPerspective(WorldRenderHelper helper) { private void applyPerspective(WorldRenderHelper helper) {
@ -121,22 +145,48 @@ public class Camera {
} }
private void applyDirection(WorldRenderHelper helper) { private void applyDirection(WorldRenderHelper helper) {
float pitch = anchor.getCameraPitch();
float yaw = anchor.getCameraYaw();
helper.pushViewTransform() helper.pushViewTransform()
.rotateY(-anchor.getCameraPitch()) .rotateY(-pitch)
.rotateZ(-anchor.getCameraYaw()); .rotateZ(-yaw);
this.lastAnchorYaw = yaw;
this.lastAnchorPitch = pitch;
this.lastAnchorLookingAt.set(
cos(pitch) * cos(yaw),
cos(pitch) * sin(yaw),
sin(pitch)
);
this.lastAnchorUp.set(
cos(pitch + PI_F / 2) * cos(yaw),
cos(pitch + PI_F / 2) * sin(yaw),
sin(pitch + PI_F / 2)
);
} }
private void applyPosition(WorldRenderHelper helper) { private void applyPosition(WorldRenderHelper helper) {
Vec3 v = Vectors.grab3(); Vec3 v = Vectors.grab3();
anchor.getCameraPosition(v); anchor.getCameraPosition(v);
v.negate(); this.lastAnchorPosition.set(v);
v.negate();
helper.pushViewTransform().translate(v); helper.pushViewTransform().translate(v);
Vectors.release(v); Vectors.release(v);
} }
private void cacheCameraTransform(WorldRenderHelper helper) {
this.lastCameraMatrix.set(helper.getViewTransform());
}
/*
* FOV management
*/
private float computeFovY() { private float computeFovY() {
float widthOverHeight = GraphicsInterface.getAspectRatio(); float widthOverHeight = GraphicsInterface.getAspectRatio();
@ -159,6 +209,10 @@ public class Camera {
this.fieldOfView = fieldOfView; this.fieldOfView = fieldOfView;
} }
/*
* Anchor management
*/
public Anchor getAnchor() { public Anchor getAnchor() {
return anchor; return anchor;
} }
@ -171,6 +225,7 @@ public class Camera {
if (anchor == null) { if (anchor == null) {
this.anchor = null; this.anchor = null;
this.modes = null; this.modes = null;
invalidateCache();
return; return;
} }
@ -189,6 +244,22 @@ public class Camera {
this.currentModeIndex = 0; this.currentModeIndex = 0;
} }
private void invalidateCache() {
this.lastAnchorPosition.set(Float.NaN);
this.lastAnchorYaw = Float.NaN;
this.lastAnchorPitch = Float.NaN;
this.lastCameraMatrix.set(
Float.NaN, Float.NaN, Float.NaN, Float.NaN,
Float.NaN, Float.NaN, Float.NaN, Float.NaN,
Float.NaN, Float.NaN, Float.NaN, Float.NaN,
Float.NaN, Float.NaN, Float.NaN, Float.NaN
);
this.lastAnchorLookingAt.set(Float.NaN);
this.lastAnchorUp.set(Float.NaN);
}
public Anchor.Mode getMode() { public Anchor.Mode getMode() {
return modes[currentModeIndex]; return modes[currentModeIndex];
} }
@ -201,4 +272,28 @@ public class Camera {
} }
} }
public float getLastAnchorYaw() {
return lastAnchorYaw;
}
public float getLastAnchorPitch() {
return lastAnchorPitch;
}
public Vec3 getLastAnchorPosition() {
return lastAnchorPosition;
}
public Mat4 getLastCameraMatrix() {
return lastCameraMatrix;
}
public Vec3 getLastAnchorLookingAt() {
return lastAnchorLookingAt;
}
public Vec3 getLastAnchorUp() {
return lastAnchorUp;
}
} }

View File

@ -14,6 +14,18 @@ public class FloatMathUtils {
return a - 2*PI_F * floor((a + PI_F) / (2*PI_F)); return a - 2*PI_F * floor((a + PI_F) / (2*PI_F));
} }
public static float sin(float x) {
return (float) Math.sin(x);
}
public static float cos(float x) {
return (float) Math.cos(x);
}
public static float tan(float x) {
return (float) Math.tan(x);
}
private FloatMathUtils() {} private FloatMathUtils() {}
} }