I am hobbyist c++ programmer and I learn fmod api library. I have a problem with my program, it compile but doesn’t work as it should. It creates 3D sound in front of listener like there is a wall between, and this sound shouldn’t be audible for listener. But after clicking any key it is audible but it shouldn’t be.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <fmod.hpp>
#define ercheck( r ) if( r != FMOD_OK) std::cout<<"error: " << r;
int main()
{
FMOD_RESULT result;
FMOD::System *system =0;
FMOD::Sound* sound=0;
FMOD::Channel *channel =0;
FMOD::Geometry *object_geometry =0;
result = FMOD::System_Create(&system);
result = system->init(100, FMOD_INIT_NORMAL | FMOD_INIT_CHANNEL_LOWPASS | FMOD_INIT_3D_RIGHTHANDED , 0);
result = system->set3DSettings(1.f, 1.f, 0.1f);
ercheck( result );
result = system->setGeometrySettings( 1000 ); //resize world to occlusion
ercheck( result );
FMOD_VECTOR position_object = { 500, 100, 495 }; //position objectgeometry
FMOD_VECTOR forward_object = { 0, 0, -1 }; // rotation object geometry
FMOD_VECTOR up_object = { 0, 1, 0 };
//position vertices 4 to rectangle
FMOD_VECTOR rectangle[4] = {
{ 450, 50, 495}, { 450, 150, 495}, { 550, 150, 495}, { 550, 50, 495} //rectangle beetwen sound at listener
};
result = system->createGeometry(1, 4, &object_geometry ); //create geometry to occlusion
ercheck( result );
object_geometry->setPosition( &position_object ); //using to position object geometry
object_geometry->setRotation( &forward_object, &up_object ); //using to rotation object geometry
int index =0; //index to occlusion
result = object_geometry->addPolygon( 1, 1, true, 4 ,rectangle, &index); // add polygon to object geometry
ercheck( result );
//create sound
result = system->createSound( "test.wav", FMOD_3D, 0, &sound);
ercheck( result );
result = sound->set3DMinMaxDistance( 0.5f , 5000);
ercheck( result );
//sound position
FMOD_VECTOR pos_sound = { 500, 100, 490};
FMOD_VECTOR vel_sound = { 0, 0, 0};
//listener
FMOD_VECTOR pos_listener = { 500, 100, 500};
FMOD_VECTOR vel_listener = { 0, 0, 0};
FMOD_VECTOR forward_listener = { 0, 0, -1};
FMOD_VECTOR up_listener = { 0, 1, 0};
bool close = false;
while( !close )
{
auto key =_getch();
if(key == 27) //key escape = close program
close = true;
result = system->update(); // update to system fmod
ercheck( result );
//listener
result = system->set3DListenerAttributes(0, &pos_listener, &vel_listener, &forward_listener, &up_listener);
ercheck( result );
float dir = 1, revb = 1;
result = system->getGeometryOcclusion( &pos_listener, &pos_sound,&dir, &revb ); //i don't know is it good?
ercheck( result );
Sleep( 10 );
result = system->playSound( sound, 0, true, &channel);
ercheck( result );
result = channel->set3DAttributes(&pos_sound, &vel_sound);
result = channel->setPaused(false);
}
//shut down
sound->release();
object_geometry->release();
result = system->close();
ercheck( result );
system->release();
return 0;
}