Added sound positioning

This commit is contained in:
Евгений Смирнов 2020-09-20 17:40:51 +03:00
parent 437566283c
commit 9d1862842d
6 changed files with 60 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package ru.windcorp.progressia.client; package ru.windcorp.progressia.client;
import ru.windcorp.progressia.client.audio.backend.ALTest;
import ru.windcorp.progressia.client.comms.DefaultClientCommsListener; import ru.windcorp.progressia.client.comms.DefaultClientCommsListener;
import ru.windcorp.progressia.client.comms.ServerCommsChannel; import ru.windcorp.progressia.client.comms.ServerCommsChannel;
import ru.windcorp.progressia.client.graphics.world.Camera; import ru.windcorp.progressia.client.graphics.world.Camera;

View File

@ -23,8 +23,7 @@ import ru.windcorp.progressia.client.audio.backend.ALTest;
public class ProgressiaClientMain { public class ProgressiaClientMain {
public static void main(String[] args) { public static void main(String[] args) {
ALTest al = new ALTest(); ALTest.execute();
al.execute();
ProgressiaLauncher.launch(args, new ClientProxy()); ProgressiaLauncher.launch(args, new ClientProxy());
} }

View File

@ -1,6 +1,14 @@
package ru.windcorp.progressia.client.audio; package ru.windcorp.progressia.client.audio;
import com.sun.jna.platform.unix.X11;
import glm.vec._3.Vec3;
import jglm.Vec;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import ru.windcorp.progressia.client.Client;
import ru.windcorp.progressia.client.ClientState;
import ru.windcorp.progressia.client.graphics.world.Camera;
import ru.windcorp.progressia.common.util.FloatMathUtils;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
@ -8,16 +16,33 @@ import static org.lwjgl.openal.AL10.*;
//TODO add getters and setters //TODO add getters and setters
public class Listener { public class Listener {
private static FloatBuffer position = private static Vec3 position = new Vec3();
(FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private static Vec3 velocity = new Vec3();
private static FloatBuffer velocity = private static Vec3 oriAt = new Vec3();
(FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private static Vec3 oriUp = new Vec3();
private static FloatBuffer orientation =
(FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}).flip(); private static Client client;
private static Camera.Anchor anchor;
public static void update() { public static void update() {
alListenerfv(AL_POSITION, position); if (client == null) {
alListenerfv(AL_VELOCITY, velocity); client = ClientState.getInstance();
alListenerfv(AL_ORIENTATION, orientation); } else if (anchor == null) {
anchor = client.getCamera().getAnchor();
} else {
anchor.getCameraPosition(position);
float pitch = anchor.getCameraPitch();
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) );
alListener3f(AL_POSITION, position.x, position.y, position.z);
alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z);
alListenerfv(AL_ORIENTATION, new float[] {oriAt.x, oriAt.y, oriAt.z, oriUp.x, oriUp.y, oriUp.z});
}
} }
} }

View File

@ -13,10 +13,10 @@ public class Sound {
//Characteristics //Characteristics
private FloatBuffer position = private FloatBuffer position =
(FloatBuffer) BufferUtils.createFloatBuffer(3) (FloatBuffer) BufferUtils.createFloatBuffer(3)
.put(new float[]{0.0f, 0.0f, 0.0f}).flip(); .put(new float[]{0.0f, 0.0f, 0.0f}).rewind();
private FloatBuffer velocity = private FloatBuffer velocity =
(FloatBuffer) BufferUtils.createFloatBuffer(3) (FloatBuffer) BufferUtils.createFloatBuffer(3)
.put(new float[]{0.0f, 0.0f, 0.0f}).flip(); .put(new float[]{0.0f, 0.0f, 0.0f}).rewind();
private float pitch = 1.0f; private float pitch = 1.0f;
private float gain = 1.0f; private float gain = 1.0f;
@ -91,6 +91,7 @@ public class Sound {
public void setSource(int source) { public void setSource(int source) {
this.source = source; this.source = source;
alSourcei(this.source, AL_BUFFER, audio); alSourcei(this.source, AL_BUFFER, audio);
//alSourcei(this.source, AL_SOURCE_RELATIVE, AL_TRUE);
} }
public int getSource() { public int getSource() {

View File

@ -13,15 +13,15 @@ import static org.lwjgl.openal.ALC10.*;
public class ALTest { public class ALTest {
// Position of the listener // Position of the listener
private FloatBuffer listenerPos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private static FloatBuffer listenerPos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Velocity of the listener // Velocity of the listener
private FloatBuffer listenerVel = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private static FloatBuffer listenerVel = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Orientation of the listener. (first 3 elements are "at", second 3 are "up") // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
private FloatBuffer listenerOri = private static FloatBuffer listenerOri =
(FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip(); (FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip();
private void initializeAL() { static private void initializeAL() {
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER); String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
long device = alcOpenDevice(defaultDeviceName); long device = alcOpenDevice(defaultDeviceName);
@ -30,30 +30,38 @@ public class ALTest {
alcMakeContextCurrent(context); alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(device); ALCCapabilities deviceCaps = ALC.createCapabilities(device);
ALCapabilities alcaps = AL.createCapabilities(deviceCaps); ALCapabilities alcaps = AL.createCapabilities(deviceCaps);
} }
void loadALData() { static void loadALData() {
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound(); Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound();
music.forcePlay(); music.forcePlay();
checkALError();
} }
void killALData() { static void killALData() {
//alDeleteSources(source); //alDeleteSources(source);
//alDeleteBuffers(buffer); //alDeleteBuffers(buffer);
} }
public void execute() { public static void execute() {
initializeAL(); initializeAL();
checkALError(); checkALError();
Listener.update(); checkALError();
checkALError();
loadALData(); loadALData();
checkALError(); checkALError();
checkALError(); checkALError();
} }
public void checkALError() { public static void update() {
Listener.update();
}
public static void checkALError() {
int i = alGetError(); int i = alGetError();
if(alGetError() != AL_NO_ERROR) { if(alGetError() != AL_NO_ERROR) {
throw new RuntimeException(i+""); throw new RuntimeException(i+"");

View File

@ -25,6 +25,7 @@ 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.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;
@ -81,6 +82,7 @@ public class Camera {
public Camera() {} public Camera() {}
public void apply(WorldRenderHelper helper) { public void apply(WorldRenderHelper helper) {
ALTest.update();
applyPerspective(helper); applyPerspective(helper);
rotateCoordinateSystem(helper); rotateCoordinateSystem(helper);