Skip to content

Commit

Permalink
Here's a spatial sound system implementation; it's actually pretty co…
Browse files Browse the repository at this point in the history
…mplete (see CratesMode for example). If you've already come up with one, let me know, I'd love to see your take on how to manage it.
  • Loading branch information
ixchow committed Sep 8, 2018
1 parent dcdccfc commit 7b8da69
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 2 deletions.
34 changes: 32 additions & 2 deletions CratesMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "MenuMode.hpp"
#include "Load.hpp"
#include "Sound.hpp"
#include "MeshBuffer.hpp"
#include "gl_errors.hpp" //helper for dumpping OpenGL error messages
#include "read_chunk.hpp" //helper for reading a vector of structures from a file
Expand All @@ -26,6 +27,12 @@ Load< GLuint > crates_meshes_for_vertex_color_program(LoadTagDefault, [](){
return new GLuint(crates_meshes->make_vao_for_program(vertex_color_program->program));
});

Load< Sound::Sample > sample_dot(LoadTagDefault, [](){
return new Sound::Sample(data_path("dot.wav"));
});
Load< Sound::Sample > sample_loop(LoadTagDefault, [](){
return new Sound::Sample(data_path("loop.wav"));
});

CratesMode::CratesMode() {
//----------------
Expand All @@ -49,13 +56,13 @@ CratesMode::CratesMode() {
//Crate at the origin:
Scene::Transform *transform1 = scene.new_transform();
transform1->position = glm::vec3(1.0f, 0.0f, 0.0f);
attach_object(transform1, "Crate");
large_crate = attach_object(transform1, "Crate");
//smaller crate on top:
Scene::Transform *transform2 = scene.new_transform();
transform2->set_parent(transform1);
transform2->position = glm::vec3(0.0f, 0.0f, 1.5f);
transform2->scale = glm::vec3(0.5f);
attach_object(transform2, "Crate");
small_crate = attach_object(transform2, "Crate");
}

{ //Camera looking at the origin:
Expand All @@ -65,9 +72,13 @@ CratesMode::CratesMode() {
transform->rotation = glm::angleAxis(glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
camera = scene.new_camera(transform);
}

//start the 'loop' sample playing at the large crate:
loop = sample_loop->play(large_crate->transform->position, 1.0f, Sound::Loop);
}

CratesMode::~CratesMode() {
if (loop) loop->stop();
}

bool CratesMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size) {
Expand Down Expand Up @@ -132,6 +143,25 @@ void CratesMode::update(float elapsed) {
if (controls.left) camera->transform->position -= amt * directions[0];
if (controls.backward) camera->transform->position += amt * directions[2];
if (controls.forward) camera->transform->position -= amt * directions[2];

{ //set sound positions:
glm::mat4 cam_to_world = camera->transform->make_local_to_world();
Sound::listener.set_position( cam_to_world[3] );
//camera looks down -z, so right is +x:
Sound::listener.set_right( glm::normalize(cam_to_world[0]) );

if (loop) {
glm::mat4 large_crate_to_world = large_crate->transform->make_local_to_world();
loop->set_position( large_crate_to_world * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f) );
}
}

dot_countdown -= elapsed;
if (dot_countdown <= 0.0f) {
dot_countdown = (rand() / float(RAND_MAX) * 2.0f) + 0.5f;
glm::mat4x3 small_crate_to_world = small_crate->transform->make_local_to_world();
sample_dot->play( small_crate_to_world * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f) );
}
}

void CratesMode::draw(glm::uvec2 const &drawable_size) {
Expand Down
10 changes: 10 additions & 0 deletions CratesMode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "MeshBuffer.hpp"
#include "GL.hpp"
#include "Scene.hpp"
#include "Sound.hpp"

#include <SDL.h>
#include <glm/glm.hpp>
Expand Down Expand Up @@ -43,4 +44,13 @@ struct CratesMode : public Mode {

Scene scene;
Scene::Camera *camera = nullptr;

Scene::Object *large_crate = nullptr;
Scene::Object *small_crate = nullptr;

//when this reaches zero, the 'dot' sample is triggered at the small crate:
float dot_countdown = 1.0f;

//this 'loop' sample is played at the large crate:
std::shared_ptr< Sound::PlayingSample > loop;
};
1 change: 1 addition & 0 deletions Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ NAMES =
Load
MeshBuffer
draw_text
Sound
;

if $(OS) = NT {
Expand Down
Loading

0 comments on commit 7b8da69

Please sign in to comment.