mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
cabin sound positioning
This commit is contained in:
@@ -156,5 +156,6 @@ TButton::play( sound* Sound ) {
|
||||
|
||||
Sound->stop();
|
||||
Sound->play();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -131,5 +131,5 @@ include_directories(${LUAJIT_INCLUDE_DIR})
|
||||
target_link_libraries(${PROJECT_NAME} ${LUAJIT_LIBRARIES})
|
||||
|
||||
if (UNIX)
|
||||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
|
||||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -g")
|
||||
endif()
|
||||
|
||||
@@ -25,6 +25,11 @@ class float3
|
||||
};
|
||||
float Length() const;
|
||||
float LengthSquared() const;
|
||||
|
||||
operator glm::vec3() const
|
||||
{
|
||||
return glm::vec3(x, y, z);
|
||||
}
|
||||
};
|
||||
|
||||
inline bool operator==(const float3 &v1, const float3 &v2)
|
||||
|
||||
28
Gauge.cpp
28
Gauge.cpp
@@ -19,6 +19,8 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "Model3d.h"
|
||||
#include "Timer.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)
|
||||
{ // ustawienie parametrów animacji submodelu
|
||||
@@ -315,14 +317,30 @@ void TGauge::UpdateValue()
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
TGauge::play( sound *Sound ) {
|
||||
extern TWorld World;
|
||||
|
||||
if( Sound == nullptr ) { return; }
|
||||
void TGauge::play( sound *Sound )
|
||||
{
|
||||
if (!Sound)
|
||||
return;
|
||||
|
||||
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();
|
||||
return;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
2
Gauge.h
2
Gauge.h
@@ -68,7 +68,7 @@ class TGauge {
|
||||
void AssignDouble(double *dValue);
|
||||
void AssignInt(int *iValue);
|
||||
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
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -1060,10 +1060,7 @@ bool TWorld::Update()
|
||||
Camera.SetMatrix(cam_matrix);
|
||||
|
||||
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);
|
||||
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0) * glm::mat3(cam_matrix);
|
||||
|
||||
sound_man->set_listener(pos, at, up);
|
||||
sound_man->set_listener(pos, glm::mat3(cam_matrix));
|
||||
sound_man->update(dt);
|
||||
}
|
||||
|
||||
|
||||
3
World.h
3
World.h
@@ -108,8 +108,7 @@ TWorld();
|
||||
void OnCommandGet(DaneRozkaz *pRozkaz);
|
||||
bool Update();
|
||||
void TrainDelete(TDynamicObject *d = NULL);
|
||||
TTrain const *
|
||||
train() const { return Train; }
|
||||
TTrain* train() { return Train; }
|
||||
// switches between static and dynamic daylight calculation
|
||||
void ToggleDaylight();
|
||||
|
||||
|
||||
9
dumb3d.h
9
dumb3d.h
@@ -118,6 +118,10 @@ class vector3
|
||||
return true;
|
||||
};
|
||||
|
||||
operator glm::vec3() const
|
||||
{
|
||||
return glm::vec3(x, y, z);
|
||||
}
|
||||
private:
|
||||
};
|
||||
|
||||
@@ -208,6 +212,11 @@ class matrix4x4
|
||||
return true;
|
||||
}
|
||||
|
||||
operator glm::mat4() const
|
||||
{
|
||||
return glm::make_mat4(e);
|
||||
}
|
||||
|
||||
private:
|
||||
scalar_t e[16];
|
||||
};
|
||||
|
||||
53
sound.cpp
53
sound.cpp
@@ -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;
|
||||
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 };
|
||||
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()
|
||||
{
|
||||
id = 0;
|
||||
alGenSources(1, &id);
|
||||
if (!id)
|
||||
throw std::runtime_error("sound: cannot generate source");
|
||||
alSourcei(id, AL_SOURCE_RELATIVE, AL_TRUE);
|
||||
dist(5.0f * 3.82f);
|
||||
spatial = false;
|
||||
set_mode(global);
|
||||
gain_off = 0.0f;
|
||||
gain_mul = 1.0f;
|
||||
pitch_off = 0.0f;
|
||||
@@ -288,6 +286,18 @@ sound::sound()
|
||||
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()
|
||||
{
|
||||
looping = false;
|
||||
@@ -308,22 +318,25 @@ simple_sound::~simple_sound()
|
||||
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);
|
||||
spatial = true;
|
||||
set_mode(spatial);
|
||||
last_pos = p;
|
||||
}
|
||||
|
||||
if (p != pos)
|
||||
if (p != pos || mode == anchored)
|
||||
{
|
||||
pos = p;
|
||||
pos_dirty = true;
|
||||
|
||||
if (mode == anchored)
|
||||
p = (p - sound_man->pos) * glm::inverse(sound_man->rot);
|
||||
|
||||
alSourcefv(id, AL_POSITION, glm::value_ptr(p));
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -343,7 +356,7 @@ sound& sound::dist(float dist)
|
||||
|
||||
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;
|
||||
|
||||
alSourcePlay(id);
|
||||
@@ -361,9 +374,9 @@ void simple_sound::stop()
|
||||
|
||||
void sound::update(float dt)
|
||||
{
|
||||
if (spatial)
|
||||
if (mode == spatial)
|
||||
dt_sum += dt;
|
||||
if (spatial && pos_dirty)
|
||||
if (mode == spatial && pos_dirty)
|
||||
{
|
||||
glm::vec3 velocity = (pos - last_pos) / dt_sum; // m/s
|
||||
alSourcefv(id, AL_VELOCITY, glm::value_ptr(velocity));
|
||||
@@ -383,7 +396,7 @@ void simple_sound::update(float dt)
|
||||
alGetSourcei(id, AL_SOURCE_STATE, &v);
|
||||
if (v != AL_PLAYING)
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -464,7 +477,7 @@ void complex_sound::play()
|
||||
if (cs != state::post)
|
||||
return;
|
||||
|
||||
if (spatial && glm::distance(pos, sound_man->pos) > max_dist)
|
||||
if (mode != global && glm::distance(pos, sound_man->pos) > max_dist)
|
||||
return;
|
||||
|
||||
alSourceRewind(id);
|
||||
@@ -554,7 +567,7 @@ void complex_sound::update(float dt)
|
||||
}
|
||||
|
||||
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;
|
||||
stop();
|
||||
|
||||
17
sound.h
17
sound.h
@@ -45,9 +45,17 @@ class sound
|
||||
glm::vec3 last_pos;
|
||||
float dt_sum;
|
||||
|
||||
public:
|
||||
enum mode_t
|
||||
{
|
||||
global,
|
||||
spatial,
|
||||
anchored
|
||||
};
|
||||
|
||||
protected:
|
||||
float max_dist;
|
||||
bool spatial;
|
||||
mode_t mode;
|
||||
glm::vec3 pos;
|
||||
int samplerate;
|
||||
|
||||
@@ -68,12 +76,13 @@ public:
|
||||
virtual void stop() = 0;
|
||||
virtual void update(float dt);
|
||||
|
||||
sound& set_mode(mode_t);
|
||||
sound& dist(float);
|
||||
sound& gain(float);
|
||||
sound& pitch(float);
|
||||
virtual sound& loop(bool loop = true) = 0;
|
||||
|
||||
sound& position(glm::vec3 const &);
|
||||
sound& position(glm::vec3);
|
||||
sound& position(Math3D::vector3 const &);
|
||||
};
|
||||
|
||||
@@ -155,6 +164,7 @@ class sound_manager
|
||||
|
||||
public:
|
||||
glm::vec3 pos, last_pos;
|
||||
glm::mat3 rot;
|
||||
|
||||
sound_manager();
|
||||
~sound_manager();
|
||||
@@ -166,8 +176,7 @@ public:
|
||||
void destroy_sound(sound**);
|
||||
|
||||
void update(float dt);
|
||||
void set_listener(glm::vec3 const &pos, glm::vec3 const &at, glm::vec3 const &up);
|
||||
void set_listener(Math3D::vector3 const &pos, Math3D::vector3 const &at, Math3D::vector3 const &up);
|
||||
void set_listener(glm::vec3 const &pos, glm::mat3 const &rot);
|
||||
};
|
||||
|
||||
extern std::unique_ptr<sound_manager> sound_man;
|
||||
|
||||
Reference in New Issue
Block a user