16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 17:29:18 +02:00

cabin sound positioning

This commit is contained in:
milek7
2017-09-16 20:35:43 +02:00
parent 6c8290f66f
commit e5ad6be865
10 changed files with 88 additions and 37 deletions

View File

@@ -156,5 +156,6 @@ TButton::play( sound* Sound ) {
Sound->stop(); Sound->stop();
Sound->play(); Sound->play();
return; return;
} }

View File

@@ -131,5 +131,5 @@ include_directories(${LUAJIT_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${LUAJIT_LIBRARIES}) target_link_libraries(${PROJECT_NAME} ${LUAJIT_LIBRARIES})
if (UNIX) if (UNIX)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -g")
endif() endif()

View File

@@ -25,6 +25,11 @@ class float3
}; };
float Length() const; float Length() const;
float LengthSquared() const; float LengthSquared() const;
operator glm::vec3() const
{
return glm::vec3(x, y, z);
}
}; };
inline bool operator==(const float3 &v1, const float3 &v2) inline bool operator==(const float3 &v1, const float3 &v2)

View File

@@ -19,6 +19,8 @@ http://mozilla.org/MPL/2.0/.
#include "Model3d.h" #include "Model3d.h"
#include "Timer.h" #include "Timer.h"
#include "Logs.h" #include "Logs.h"
#include "World.h"
#include "Train.h"
void TGauge::Init(TSubModel *NewSubModel, TGaugeType eNewType, double fNewScale, double fNewOffset, double fNewFriction, double fNewValue) void TGauge::Init(TSubModel *NewSubModel, TGaugeType eNewType, double fNewScale, double fNewOffset, double fNewFriction, double fNewValue)
{ // ustawienie parametrów animacji submodelu { // ustawienie parametrów animacji submodelu
@@ -315,14 +317,30 @@ void TGauge::UpdateValue()
} }
}; };
void extern TWorld World;
TGauge::play( sound *Sound ) {
if( Sound == nullptr ) { return; } void TGauge::play( sound *Sound )
{
if (!Sound)
return;
Sound->stop(); Sound->stop();
if (SubModel && World.train())
{
float4x4 mat;
SubModel->ParentMatrix(&mat);
glm::vec3 pos = *mat.TranslationGet();
if (glm::length(pos) > 1.0f)
{
pos = glm::vec3(glm::vec4(pos, 1.0f) * glm::inverse((glm::mat4)World.train()->Dynamic()->mMatrix));
pos = pos + (glm::vec3)World.train()->Dynamic()->GetPosition();
Sound->set_mode(sound::anchored).dist(1.5f).position(pos);
}
}
Sound->play(); Sound->play();
return; return;
} }
//---------------------------------------------------------------------------

View File

@@ -68,7 +68,7 @@ class TGauge {
void AssignDouble(double *dValue); void AssignDouble(double *dValue);
void AssignInt(int *iValue); void AssignInt(int *iValue);
void UpdateValue(); void UpdateValue();
TSubModel *SubModel; // McZapkie-310302: zeby mozna bylo sprawdzac czy zainicjowany poprawnie TSubModel *SubModel = nullptr; // McZapkie-310302: zeby mozna bylo sprawdzac czy zainicjowany poprawnie
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@@ -1060,10 +1060,7 @@ bool TWorld::Update()
Camera.SetMatrix(cam_matrix); Camera.SetMatrix(cam_matrix);
glm::vec3 pos(Camera.Pos.x, Camera.Pos.y, Camera.Pos.z); glm::vec3 pos(Camera.Pos.x, Camera.Pos.y, Camera.Pos.z);
glm::vec3 at = glm::vec3(0.0, 0.0, -1.0) * glm::mat3(cam_matrix); sound_man->set_listener(pos, glm::mat3(cam_matrix));
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0) * glm::mat3(cam_matrix);
sound_man->set_listener(pos, at, up);
sound_man->update(dt); sound_man->update(dt);
} }

View File

@@ -108,8 +108,7 @@ TWorld();
void OnCommandGet(DaneRozkaz *pRozkaz); void OnCommandGet(DaneRozkaz *pRozkaz);
bool Update(); bool Update();
void TrainDelete(TDynamicObject *d = NULL); void TrainDelete(TDynamicObject *d = NULL);
TTrain const * TTrain* train() { return Train; }
train() const { return Train; }
// switches between static and dynamic daylight calculation // switches between static and dynamic daylight calculation
void ToggleDaylight(); void ToggleDaylight();

View File

@@ -118,6 +118,10 @@ class vector3
return true; return true;
}; };
operator glm::vec3() const
{
return glm::vec3(x, y, z);
}
private: private:
}; };
@@ -208,6 +212,11 @@ class matrix4x4
return true; return true;
} }
operator glm::mat4() const
{
return glm::make_mat4(e);
}
private: private:
scalar_t e[16]; scalar_t e[16];
}; };

View File

@@ -259,28 +259,26 @@ void sound_manager::update(float dt)
} }
} }
void sound_manager::set_listener(glm::vec3 const &p, glm::vec3 const &at, glm::vec3 const &up) void sound_manager::set_listener(glm::vec3 const &p, glm::mat3 const &r)
{ {
pos = p; pos = p;
alListenerfv(AL_POSITION, glm::value_ptr(p)); rot = r;
glm::vec3 at = glm::vec3(0.0, 0.0, -1.0) * r;
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0) * r;
alListenerfv(AL_POSITION, glm::value_ptr(pos));
glm::vec3 ori[] = { at, up }; glm::vec3 ori[] = { at, up };
alListenerfv(AL_ORIENTATION, (ALfloat*)ori); alListenerfv(AL_ORIENTATION, (ALfloat*)ori);
} }
void sound_manager::set_listener(Math3D::vector3 const &p, Math3D::vector3 const &at, Math3D::vector3 const &up)
{
set_listener((glm::vec3)glm::make_vec3(&p.x), (glm::vec3)glm::make_vec3(&at.x), (glm::vec3)glm::make_vec3(&up.x));
}
sound::sound() sound::sound()
{ {
id = 0; id = 0;
alGenSources(1, &id); alGenSources(1, &id);
if (!id) if (!id)
throw std::runtime_error("sound: cannot generate source"); throw std::runtime_error("sound: cannot generate source");
alSourcei(id, AL_SOURCE_RELATIVE, AL_TRUE);
dist(5.0f * 3.82f); dist(5.0f * 3.82f);
spatial = false; set_mode(global);
gain_off = 0.0f; gain_off = 0.0f;
gain_mul = 1.0f; gain_mul = 1.0f;
pitch_off = 0.0f; pitch_off = 0.0f;
@@ -288,6 +286,18 @@ sound::sound()
dt_sum = 0.0f; dt_sum = 0.0f;
} }
sound& sound::set_mode(mode_t m)
{
mode = m;
if (mode == global || mode == anchored)
alSourcei(id, AL_SOURCE_RELATIVE, AL_TRUE);
else if (mode == spatial)
alSourcei(id, AL_SOURCE_RELATIVE, AL_FALSE);
return *this;
}
simple_sound::simple_sound(sound_buffer *buf) : sound::sound() simple_sound::simple_sound(sound_buffer *buf) : sound::sound()
{ {
looping = false; looping = false;
@@ -308,22 +318,25 @@ simple_sound::~simple_sound()
buffer->unref(); buffer->unref();
} }
sound& sound::position(glm::vec3 const &p) sound& sound::position(glm::vec3 p)
{ {
if (!spatial) if (mode == global)
{ {
alSourcei(id, AL_SOURCE_RELATIVE, AL_FALSE); set_mode(spatial);
spatial = true;
last_pos = p; last_pos = p;
} }
if (p != pos) if (p != pos || mode == anchored)
{ {
pos = p; pos = p;
pos_dirty = true; pos_dirty = true;
if (mode == anchored)
p = (p - sound_man->pos) * glm::inverse(sound_man->rot);
alSourcefv(id, AL_POSITION, glm::value_ptr(p)); alSourcefv(id, AL_POSITION, glm::value_ptr(p));
} }
return *this; return *this;
} }
@@ -343,7 +356,7 @@ sound& sound::dist(float dist)
void simple_sound::play() void simple_sound::play()
{ {
if (playing || (spatial && glm::distance(pos, sound_man->pos) > max_dist)) if (playing || (mode != global && glm::distance(pos, sound_man->pos) > max_dist))
return; return;
alSourcePlay(id); alSourcePlay(id);
@@ -361,9 +374,9 @@ void simple_sound::stop()
void sound::update(float dt) void sound::update(float dt)
{ {
if (spatial) if (mode == spatial)
dt_sum += dt; dt_sum += dt;
if (spatial && pos_dirty) if (mode == spatial && pos_dirty)
{ {
glm::vec3 velocity = (pos - last_pos) / dt_sum; // m/s glm::vec3 velocity = (pos - last_pos) / dt_sum; // m/s
alSourcefv(id, AL_VELOCITY, glm::value_ptr(velocity)); alSourcefv(id, AL_VELOCITY, glm::value_ptr(velocity));
@@ -383,7 +396,7 @@ void simple_sound::update(float dt)
alGetSourcei(id, AL_SOURCE_STATE, &v); alGetSourcei(id, AL_SOURCE_STATE, &v);
if (v != AL_PLAYING) if (v != AL_PLAYING)
playing = false; playing = false;
else if (spatial && glm::distance(pos, sound_man->pos) > max_dist) else if (mode != global && glm::distance(pos, sound_man->pos) > max_dist)
stop(); stop();
} }
} }
@@ -464,7 +477,7 @@ void complex_sound::play()
if (cs != state::post) if (cs != state::post)
return; return;
if (spatial && glm::distance(pos, sound_man->pos) > max_dist) if (mode != global && glm::distance(pos, sound_man->pos) > max_dist)
return; return;
alSourceRewind(id); alSourceRewind(id);
@@ -554,7 +567,7 @@ void complex_sound::update(float dt)
} }
if (cs == state::main) if (cs == state::main)
if (spatial && glm::distance(pos, sound_man->pos) > max_dist) if (mode != global && glm::distance(pos, sound_man->pos) > max_dist)
{ {
shut_by_dist = true; shut_by_dist = true;
stop(); stop();

17
sound.h
View File

@@ -45,9 +45,17 @@ class sound
glm::vec3 last_pos; glm::vec3 last_pos;
float dt_sum; float dt_sum;
public:
enum mode_t
{
global,
spatial,
anchored
};
protected: protected:
float max_dist; float max_dist;
bool spatial; mode_t mode;
glm::vec3 pos; glm::vec3 pos;
int samplerate; int samplerate;
@@ -68,12 +76,13 @@ public:
virtual void stop() = 0; virtual void stop() = 0;
virtual void update(float dt); virtual void update(float dt);
sound& set_mode(mode_t);
sound& dist(float); sound& dist(float);
sound& gain(float); sound& gain(float);
sound& pitch(float); sound& pitch(float);
virtual sound& loop(bool loop = true) = 0; virtual sound& loop(bool loop = true) = 0;
sound& position(glm::vec3 const &); sound& position(glm::vec3);
sound& position(Math3D::vector3 const &); sound& position(Math3D::vector3 const &);
}; };
@@ -155,6 +164,7 @@ class sound_manager
public: public:
glm::vec3 pos, last_pos; glm::vec3 pos, last_pos;
glm::mat3 rot;
sound_manager(); sound_manager();
~sound_manager(); ~sound_manager();
@@ -166,8 +176,7 @@ public:
void destroy_sound(sound**); void destroy_sound(sound**);
void update(float dt); void update(float dt);
void set_listener(glm::vec3 const &pos, glm::vec3 const &at, glm::vec3 const &up); void set_listener(glm::vec3 const &pos, glm::mat3 const &rot);
void set_listener(Math3D::vector3 const &pos, Math3D::vector3 const &at, Math3D::vector3 const &up);
}; };
extern std::unique_ptr<sound_manager> sound_man; extern std::unique_ptr<sound_manager> sound_man;