From 90efd1e656e68f1f4304f3d5bc4043bf970c66f8 Mon Sep 17 00:00:00 2001 From: Chrystian Farias Date: Tue, 18 Jan 2022 22:40:58 -0300 Subject: [PATCH] 3D Audio --- GTAFmod/FMODAudio.cpp | 10 ++++++---- GTAFmod/FMODAudio.h | 3 ++- GTAFmod/GTAFmod.cpp | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/GTAFmod/FMODAudio.cpp b/GTAFmod/FMODAudio.cpp index 2271c56..bb41afa 100644 --- a/GTAFmod/FMODAudio.cpp +++ b/GTAFmod/FMODAudio.cpp @@ -38,14 +38,16 @@ void FMODAudio::LoadBank(FMOD::Studio::System* fmodSystem, char* bank, char* abs //Set 3D space m_RpmEventInstance->setVolume(.4); + m_RpmEventInstance->setReverbLevel(0, 2); m_RpmEventInstance->set3DAttributes(&m_Attributes); m_RpmEventInstance->setParameterByID(m_RpmDesc.id, 0.0f); m_RpmEventInstance->setParameterByID(m_LoadDesc.id, 1.0f); - m_Attributes = { { 0 } }; - m_Attributes.forward.z = 1.0f; - m_Attributes.up.y = 1.0f; - CheckError(fmodSystem->setListenerAttributes(0, &m_Attributes), "Failed on set 3d ambient"); + m_ListenerAttributes = { { 0 } }; + m_ListenerAttributes.forward.x = 1.0f; + m_ListenerAttributes.up.z = 1.0f; + + CheckError(fmodSystem->setListenerAttributes(0, &m_ListenerAttributes), "Failed on set 3d ambient"); m_bIsLoaded = true; } \ No newline at end of file diff --git a/GTAFmod/FMODAudio.h b/GTAFmod/FMODAudio.h index 1092ff2..ee039d8 100644 --- a/GTAFmod/FMODAudio.h +++ b/GTAFmod/FMODAudio.h @@ -15,7 +15,8 @@ class FMODAudio FMOD::Studio::EventInstance* m_GearEventInstance; FMOD_STUDIO_PARAMETER_DESCRIPTION m_RpmDesc; FMOD_STUDIO_PARAMETER_DESCRIPTION m_LoadDesc; - FMOD_3D_ATTRIBUTES m_Attributes; + FMOD_3D_ATTRIBUTES m_Attributes; + FMOD_3D_ATTRIBUTES m_ListenerAttributes; void LoadBank(FMOD::Studio::System* fmodSystem, char* bank, char* absolutePath); diff --git a/GTAFmod/GTAFmod.cpp b/GTAFmod/GTAFmod.cpp index dcb0516..70624a9 100644 --- a/GTAFmod/GTAFmod.cpp +++ b/GTAFmod/GTAFmod.cpp @@ -7,6 +7,7 @@ #include "fmod_studio.hpp" #include "fmod_errors.h" #include "Curve.h" +#include "CModelInfo.h" #include #include @@ -245,14 +246,41 @@ class GTAFmod { //Set 3D space position CVector camPos = TheCamera.GetPosition(); CVector vehiclePos = vehicle->GetPosition(); - - audio->m_Attributes.position.x = vehiclePos.x - camPos.x; - audio->m_Attributes.position.y = vehiclePos.y - camPos.y; - audio->m_Attributes.position.z = vehiclePos.z - camPos.z; + CVector pos = vehiclePos - camPos; + CVector dirFor; + CVector dirUp; + CVector offsetFor = CVector(0, -1, 0); + CVector offsetUp = CVector(0, 0, 1); + CMatrix* matrix = vehicle->m_matrix; + RwV3dTransformPoint((RwV3d*)&dirFor, (RwV3d*)&offsetFor, (RwMatrix*)matrix); + RwV3dTransformPoint((RwV3d*)&dirUp, (RwV3d*)&offsetUp, (RwMatrix*)matrix); + + audio->m_Attributes.position.x = vehiclePos.x; + audio->m_Attributes.position.y = vehiclePos.y; + audio->m_Attributes.position.z = vehiclePos.z; + audio->m_Attributes.velocity.x = vehicle->m_vecMoveSpeed.x; + audio->m_Attributes.velocity.y = vehicle->m_vecMoveSpeed.y; + audio->m_Attributes.velocity.z = vehicle->m_vecMoveSpeed.z; + + audio->m_Attributes.forward.x = dirFor.x - vehiclePos.x; + audio->m_Attributes.forward.y = dirFor.y - vehiclePos.y; + audio->m_Attributes.forward.z = dirFor.z - vehiclePos.z; + audio->m_Attributes.up.x = dirUp.x - vehiclePos.x; + audio->m_Attributes.up.y = dirUp.y - vehiclePos.y; + audio->m_Attributes.up.z = dirUp.z - vehiclePos.z; + audio->m_RpmEventInstance->set3DAttributes(&audio->m_Attributes); audio->m_BackFireEventInstance->set3DAttributes(&audio->m_Attributes); audio->m_GearEventInstance->set3DAttributes(&audio->m_Attributes); + audio->m_ListenerAttributes.position.x = camPos.x; + audio->m_ListenerAttributes.position.y = camPos.y; + audio->m_ListenerAttributes.position.z = camPos.z; + audio->m_ListenerAttributes.forward = audio->m_Attributes.forward; + audio->m_ListenerAttributes.up = audio->m_Attributes.up; + + fmodSystem->setListenerAttributes(0, &audio->m_ListenerAttributes); + //Get gas pedal float gasPedal = abs(vehicle->m_fGasPedal);