Refactored sound update logic

- Audio Listener now uses Camera cache to determine position and
orientation
- SoundManager.update() is not triggered from RenderThread directly
This commit is contained in:
OLEGSHA 2020-10-06 12:15:58 +03:00
parent 3057910269
commit 5c90a8025b
3 changed files with 34 additions and 29 deletions

View File

@ -3,6 +3,7 @@ package ru.windcorp.progressia.client.audio;
import glm.vec._3.Vec3; import glm.vec._3.Vec3;
import ru.windcorp.progressia.client.Client; import ru.windcorp.progressia.client.Client;
import ru.windcorp.progressia.client.ClientState; import ru.windcorp.progressia.client.ClientState;
import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface;
import ru.windcorp.progressia.client.graphics.world.Camera; import ru.windcorp.progressia.client.graphics.world.Camera;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
@ -25,37 +26,41 @@ public class Listener {
private final Vec3 oriAt = new Vec3(); private final Vec3 oriAt = new Vec3();
private final Vec3 oriUp = new Vec3(); private final Vec3 oriUp = new Vec3();
private boolean isClientConnected = false; private boolean isInWorld = false;
private Camera.Anchor anchor;
public void update() { public void update() {
Client client = ClientState.getInstance(); Client client = ClientState.getInstance();
if (client == null) { Camera camera = client == null ? null : client.getCamera();
if (isClientConnected) {
isClientConnected = false; boolean wasInWorld = isInWorld;
resetParams(); isInWorld = client != null && camera.getAnchor() != null;
applyParams();
} if (isInWorld) {
} else {
isClientConnected = true; if (wasInWorld) {
if (anchor == null) { velocity.set(camera.getLastAnchorPosition()).sub(position).div(
anchor = client.getCamera().getAnchor(); (float) GraphicsInterface.getFrameLength()
);
} else { } else {
anchor.getCameraPosition(position); // If !wasInWorld, previous position is nonsence. Assume 0.
float pitch = anchor.getCameraPitch(); velocity.set(0);
float yaw = anchor.getCameraYaw();
oriAt.set(
(float) (Math.cos(pitch) * Math.cos(yaw)),
(float) (Math.cos(pitch) * Math.sin(yaw)),
(float) Math.sin(pitch)
);
oriUp.set(
(float) (Math.cos(pitch + Math.PI / 2) * Math.cos(yaw)),
(float) (Math.cos(pitch + Math.PI / 2) * Math.sin(yaw)),
(float) Math.sin(pitch + Math.PI / 2)
);
applyParams();
} }
position.set(camera.getLastAnchorPosition());
oriAt.set(camera.getLastAnchorLookingAt());
oriUp.set(camera.getLastAnchorUp());
} else if (wasInWorld) { // Do not reset if we weren't in world
resetParams();
}
/*
* Only apply if there is a chance that params changed.
* This can only happen if we are in world now (isInWorld) or we just
* left world (wasInWorld, then we need to reset).
*/
if (isInWorld || wasInWorld) {
applyParams();
} }
} }
@ -73,4 +78,5 @@ public class Listener {
oriAt.x, oriAt.y, oriAt.z, oriUp.x, oriUp.y, oriUp.z oriAt.x, oriAt.y, oriAt.z, oriUp.x, oriUp.y, oriUp.z
}); });
} }
} }

View File

@ -20,6 +20,7 @@ package ru.windcorp.progressia.client.graphics.backend;
import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;
import ru.windcorp.progressia.client.audio.SoundManager;
import ru.windcorp.progressia.client.graphics.GUI; import ru.windcorp.progressia.client.graphics.GUI;
class RenderThread extends Thread { class RenderThread extends Thread {
@ -60,6 +61,7 @@ class RenderThread extends Thread {
private void doRender() { private void doRender() {
GUI.render(); GUI.render();
SoundManager.update();
} }
private void waitForFrame() { private void waitForFrame() {

View File

@ -26,8 +26,6 @@ import java.util.function.Consumer;
import glm.Glm; import glm.Glm;
import glm.mat._4.Mat4; import glm.mat._4.Mat4;
import glm.vec._3.Vec3; import glm.vec._3.Vec3;
import ru.windcorp.progressia.client.audio.SoundManager;
import ru.windcorp.progressia.client.audio.backend.ALTest;
import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface; import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface;
import ru.windcorp.progressia.client.graphics.world.Camera.Anchor.Mode; import ru.windcorp.progressia.client.graphics.world.Camera.Anchor.Mode;
import ru.windcorp.progressia.common.util.Vectors; import ru.windcorp.progressia.common.util.Vectors;
@ -105,7 +103,6 @@ public class Camera {
*/ */
public void apply(WorldRenderHelper helper) { public void apply(WorldRenderHelper helper) {
SoundManager.update();
applyPerspective(helper); applyPerspective(helper);
rotateCoordinateSystem(helper); rotateCoordinateSystem(helper);