Skip to content

Commit

Permalink
fix mono
Browse files Browse the repository at this point in the history
  • Loading branch information
valoeghese committed Oct 15, 2021
1 parent d3b0d2c commit a96d7e4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (org.gradle.internal.os.OperatingSystem.current() == org.gradle.internal.os.O
}

group 'valoeghese'
version '1.1.3'
version '1.1.4'

switch (org.gradle.internal.os.OperatingSystem.current()) {
case org.gradle.internal.os.OperatingSystem.LINUX:
Expand Down
63 changes: 62 additions & 1 deletion src/main/java/valoeghese/scalpel/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ protected void generateBuffers() {
public void destroy() {
for (VertexArray array : this.vertexArrays) {
glDeleteVertexArrays(array.vao);
glDeleteBuffers(array.ebo);
glDeleteBuffers(array.vbo);
}

this.vertexArrays = new ArrayList<>();

this.vTempIndex = 0;
}

Expand All @@ -101,6 +104,33 @@ public final void render(Matrix4f transform) {
unbind();
}

/**
* @return the {@linkplain VertexArray vertex array} objects containing the opengl object ids.
* @apiNote only use this if you KNOW WHAT YOU ARE DOING. This allows more direct access to precise OpenGL calls.
*/
public Iterable<VertexArray> getOpenglHandles() {
return this.vertexArrays;
}

/**
* @return the {@linkplain VertexArray vertex array} object containing the opengl object ids stored at this position in this model.
* @apiNote only use this if you KNOW WHAT YOU ARE DOING. This allows more direct access to precise OpenGL calls.
*/
public VertexArray getHandleAt(int index) throws ArrayIndexOutOfBoundsException {
return this.vertexArrays.get(index);
}

/**
* Removes and destroys the {@linkplain VertexArray vertex array} object at this position.
* @apiNote only use this if you KNOW WHAT YOU ARE DOING. This allows more direct access to precise OpenGL calls.
*/
public void destroyHandleAt(int index) {
VertexArray array = this.vertexArrays.remove(index);
glDeleteVertexArrays(array.vao);
glDeleteBuffers(array.ebo);
glDeleteBuffers(array.vbo);
}

@Nullable
public Shader getShader() {
return this.shader;
Expand All @@ -110,7 +140,10 @@ public static final void unbind() {
glBindVertexArray(0);
}

private static class VertexArray {
/**
* A container containing
*/
public static class VertexArray {
private VertexArray(int vbo, int ebo, int vao, int elementCount) {
this.vbo = vbo;
this.ebo = ebo;
Expand All @@ -122,5 +155,33 @@ private VertexArray(int vbo, int ebo, int vao, int elementCount) {
private final int ebo;
private final int vao;
private final int elementCount;

/**
* @return the vertex buffer object opengl handle.
*/
public int getVBOHandle() {
return this.vbo;
}

/**
* @return the index buffer object opengl handle.
*/
public int getEBOHandle() {
return this.ebo;
}

/**
* @return the array buffer object opengl handle.
*/
public int getVAOHandle() {
return this.vao;
}

/**
* @return the length of the indices buffer object array.
*/
public int getElementCount() {
return this.elementCount;
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/valoeghese/scalpel/audio/AudioSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public void play() {
public void destroy() {
alDeleteSources(this.source);
}

public boolean isPlaying() {
return alGetSourcei(this.source, AL_SOURCE_STATE) == AL_PLAYING;
}
}
2 changes: 1 addition & 1 deletion src/main/java/valoeghese/scalpel/util/ALUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static AudioBuffer createBuffer(String fileName) throws IOException { //
stb_vorbis_get_info(decoder, info);

int channels = info.channels();
pcm = MemoryUtil.memAllocShort(stb_vorbis_stream_length_in_samples(decoder) * 2);
pcm = MemoryUtil.memAllocShort(stb_vorbis_stream_length_in_samples(decoder) * channels);
pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
stb_vorbis_close(decoder);
}
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/valoeghese/scalpel/test/TestAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ private TestAudio() {
super(100 / 20);
}

AudioBuffer audio;
AudioBuffer audioStereo;
AudioBuffer audioMono;
AudioSource source;
private Window window;
boolean playedMonoYet = false;

@Override
protected void preInit() {
Expand All @@ -31,7 +33,8 @@ protected void preInit() {
ALUtils.setListenerVelocity(0, 0, 0);

try {
audio = ALUtils.createBuffer("assets/sound/Test_Sound.ogg");
audioStereo = ALUtils.createBuffer("assets/sound/Test_Sound.ogg");
audioMono = ALUtils.createBuffer("assets/sound/Test_Mono.ogg");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -41,7 +44,7 @@ protected void preInit() {
source.setPitch(1.0f);
source.setPosition(0, 0, 0);
source.setVelocity(0, 0, 0);
source.attachBufferData(audio);
source.attachBufferData(audioStereo);
}

@Override
Expand All @@ -65,12 +68,15 @@ protected boolean shouldRun() {

@Override
protected void tick() {

}

@Override
protected void render() {

if (!this.playedMonoYet && !this.source.isPlaying()) {
this.playedMonoYet = true;
this.source.attachBufferData(audioMono);
source.play();
}
}

@Override
Expand Down
Binary file added src/test/resources/assets/sound/Test_Mono.ogg
Binary file not shown.

0 comments on commit a96d7e4

Please sign in to comment.