OpenAL problems
I''m haveing a serious problem with getting openAL to work properly. I''m setting it up correctly, actually to the letter of the tutorials that I''ve read, but my car engine sound just plays at the normal volume no matter what distance the listener is from the source. I just can''t figure why the volume doesn''t increase/decrease with distance. Can anyone help? Here''s my code (oh and updateSound is called every tick):
#include "VehicleSound.h"
VehicleSound::VehicleSound()
{
//Secure the pointers
v = 0;
cam = 0;
}
VehicleSound::VehicleSound(Vehicle& theVehicle, VehicleCamera theCamera)
{
v = &theVehicle
cam = &theCamera
alutInit(NULL, 0);
ALenum format;
ALsizei frequency, size;
ALvoid* data;
ALboolean loop;
alGenBuffers(1, &buffer);
alGetError();
alGenBuffers(1, &buffer);
if(alGetError() != AL_NO_ERROR){
MessageBox(NULL, "Could Not Generate the Buffer",
"SOUND ERROR!", MB_ICONSTOP | MB_OK);
return;
}
alutLoadWAVFile("Sound/ferrari51.wav", &format, &data, &size, &frequency, &loop);
alBufferData(buffer, format, data, size, frequency);
alutUnloadWAV(format, data, size, frequency);
alGenSources(1, &source);
if(alGetError() != AL_NO_ERROR){
MessageBox(NULL, "Could Not Generate the Source",
"SOUND ERROR!", MB_ICONSTOP | MB_OK);
return;
}
setSourceProperties();
alSourcei(source, AL_BUFFER, buffer);
alSourcef(source, AL_PITCH, 1.0f);
alSourcef(source, AL_GAIN, 1.0f);
alSourcei(source, AL_LOOPING, AL_TRUE);
if(alGetError() != AL_NO_ERROR){
MessageBox(NULL, "Could Not Initialise the Source",
"SOUND ERROR!", MB_ICONSTOP | MB_OK);
return;
}
setListenerProperties();
alSourcePlay(source);
}
VehicleSound::VehicleSound(const VehicleSound& vs)
{
v = vs.v;
cam = vs.cam;
buffer = vs.buffer;
source = vs.source;
/*
sourcePosition = vs.sourcePosition;
sourceVelocity = vs.sourceVelocity;
listenerPosition = vs.listenerPosition;
listenerVelocity = vs.listenerVelocity;
listererOrientation = vs.listenerOrientation;
*/
}
VehicleSound::~VehicleSound()
{
//secure the pointers
v = 0;
cam = 0;
alDeleteBuffers(1, &buffer);
alDeleteSources(1, &source);
alutExit();
}
void VehicleSound::setListenerProperties()
{
//Listener position is the camera''s position
listenerPosition[0] = cam->theCamera.e11;
listenerPosition[1] = cam->theCamera.e12;
listenerPosition[2] = cam->theCamera.e13;
//Just give the listener the same velocity as the Vehicle (for now)
listenerVelocity[0] = v->worldVelocity.x;
listenerVelocity[1] = v->worldVelocity.y;
listenerVelocity[2] = v->worldVelocity.z;
//The camera''s look at is a normalised vector, so the look at point
//will be the camera position plus that vector
listenerOrientation[0] = cam->theCamera.e11 + cam->theCamera.e21;
listenerOrientation[2] = cam->theCamera.e12 + cam->theCamera.e22;
listenerOrientation[3] = cam->theCamera.e13 + cam->theCamera.e23;
alListenerfv(AL_POSITION, listenerPosition);
alListenerfv(AL_VELOCITY, listenerVelocity);
alListenerfv(AL_ORIENTATION, listenerOrientation);
}
void VehicleSound::setSourceProperties()
{
sourcePosition[0] = v->worldPosition.x;
sourcePosition[1] = v->worldPosition.y;
sourcePosition[2] = v->worldPosition.z;
sourceVelocity[0] = v->worldVelocity.x;
sourceVelocity[1] = v->worldVelocity.y;
sourceVelocity[2] = v->worldVelocity.z;
alSourcefv(source, AL_POSITION, sourcePosition);
alSourcefv(source, AL_VELOCITY, sourceVelocity);
}
void VehicleSound::updateSound()
{
setListenerProperties();
setSourceProperties();
}
Cheers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement