Skip to content

Commit a62e4a9

Browse files
committed
make the hrtf listener sync with the camera location and rotation.
1 parent 86c396a commit a62e4a9

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

attachments/simple_engine/audio_system.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#endif
2323

2424
#include "renderer.h"
25+
#include "engine.h"
2526

2627
// OpenAL error checking utility
2728
static void CheckOpenALError(const std::string& operation) {
@@ -537,7 +538,10 @@ void AudioSystem::GenerateSineWavePing(float* buffer, uint32_t sampleCount, uint
537538

538539
}
539540

540-
bool AudioSystem::Initialize(Renderer* renderer) {
541+
bool AudioSystem::Initialize(Engine* engine, Renderer* renderer) {
542+
// Store the engine reference for accessing active camera
543+
this->engine = engine;
544+
541545
if (renderer) {
542546
// Validate renderer if provided
543547
if (!renderer->IsInitialized()) {
@@ -588,6 +592,24 @@ void AudioSystem::Update(float deltaTime) {
588592
return;
589593
}
590594

595+
// Synchronize HRTF listener position and orientation with active camera
596+
if (engine) {
597+
CameraComponent* activeCamera = engine->GetActiveCamera();
598+
if (activeCamera) {
599+
// Get camera position
600+
glm::vec3 cameraPos = activeCamera->GetPosition();
601+
SetListenerPosition(cameraPos.x, cameraPos.y, cameraPos.z);
602+
603+
// Calculate camera forward and up vectors for orientation
604+
// The camera looks at its target, so forward = normalize(target - position)
605+
glm::vec3 target = activeCamera->GetTarget();
606+
glm::vec3 up = activeCamera->GetUp();
607+
glm::vec3 forward = glm::normalize(target - cameraPos);
608+
609+
SetListenerOrientation(forward.x, forward.y, forward.z, up.x, up.y, up.z);
610+
}
611+
}
612+
591613
// Update audio sources and process spatial audio
592614
for (auto& source : sources) {
593615
if (!source->IsPlaying()) {

attachments/simple_engine/audio_system.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class AudioSource {
7979

8080
// Forward declarations
8181
class Renderer;
82+
class Engine;
8283

8384
/**
8485
* @brief Interface for audio output devices.
@@ -154,10 +155,11 @@ class AudioSystem {
154155

155156
/**
156157
* @brief Initialize the audio system.
158+
* @param engine Pointer to the engine for accessing active camera.
157159
* @param renderer Pointer to the renderer for compute shader support.
158160
* @return True if initialization was successful, false otherwise.
159161
*/
160-
bool Initialize(Renderer* renderer = nullptr);
162+
bool Initialize(Engine* engine, Renderer* renderer = nullptr);
161163

162164
/**
163165
* @brief Update the audio system.
@@ -298,6 +300,9 @@ class AudioSystem {
298300
// Renderer for compute shader support
299301
Renderer* renderer = nullptr;
300302

303+
// Engine reference for accessing active camera
304+
Engine* engine = nullptr;
305+
301306
// Audio output device for sending processed audio to speakers
302307
std::unique_ptr<AudioOutputDevice> outputDevice = nullptr;
303308

attachments/simple_engine/camera_component.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ class CameraComponent : public Component {
182182
return transform ? transform->GetPosition() : glm::vec3(0.0f, 0.0f, 0.0f);
183183
}
184184

185+
/**
186+
* @brief Get the camera target.
187+
* @return The camera target.
188+
*/
189+
const glm::vec3& GetTarget() const {
190+
return target;
191+
}
192+
193+
/**
194+
* @brief Get the camera up vector.
195+
* @return The camera up vector.
196+
*/
197+
const glm::vec3& GetUp() const {
198+
return up;
199+
}
200+
185201
private:
186202
/**
187203
* @brief Update the view matrix based on the camera position and target.

attachments/simple_engine/engine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ bool Engine::Initialize(const std::string& appName, int width, int height, bool
133133
renderer->SetModelLoader(modelLoader.get());
134134

135135
// Initialize audio system
136-
if (!audioSystem->Initialize(renderer.get())) {
136+
if (!audioSystem->Initialize(this, renderer.get())) {
137137
return false;
138138
}
139139

@@ -516,7 +516,7 @@ bool Engine::InitializeAndroid(android_app* app, const std::string& appName, boo
516516
renderer->SetModelLoader(modelLoader.get());
517517

518518
// Initialize audio system
519-
if (!audioSystem->Initialize(renderer.get())) {
519+
if (!audioSystem->Initialize(this, renderer.get())) {
520520
return false;
521521
}
522522

0 commit comments

Comments
 (0)