在我的3D游戏中,我目前有通过工厂类“声音”工作的声音。我通过我的camera类初始化OpenAL,在加载时,它将存储其位置、方向和速度的全局浮动缓冲区
private static FloatBuffer listenerPosition = BufferUtils.createFloatBuffer( 3 ).put( new float[] { X(), Y(), Z() } );
private static FloatBuffer listenerOrientation = BufferUtils.createFloatBuffer( 6 ).put (new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f } );
private static FloatBuffer listenerVelocity = BufferUtils.createFloatBuffer( 3 ).put (new float[] { Velocity.x, Velocity.y, Velocity.z } );然后,在相机移动或旋转的每个节拍上,它都会用代码更新这些代码
listenerVelocity.put(0, Velocity.x);
listenerVelocity.put(1, Velocity.y);
listenerVelocity.put(2, Velocity.z);
alListener( AL_POSITION, listenerPosition );
alListener( AL_ORIENTATION, listenerOrientation );
alListener( AL_VELOCITY, listenerVelocity );我认为这个类不能让OpenAL知道我是如何想要声音的,尽管我给了它所有我所知道的所需的信息。
private int ID;
public Sound(String name) {
try {
ID = alGenBuffers();
WaveData data = WaveData.create(new BufferedInputStream(new FileInputStream("res/Sound/"+name+".wav")));
alBufferData(ID, data.format, data.data, data.samplerate);
data.dispose();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Could not find \"" + name + "\"", "IO Exception", JOptionPane.ERROR_MESSAGE);
Display.destroy();
System.exit(1);
}
}
public void play(float x, float y, float z) {
playSound(ID, new Vector3f(x,y,z));
}
private static void playSound(int buffer, Vector3f pos) {
while(alGetSourcei(Sources.get(currentsource), AL10.AL_SOURCE_STATE) == AL_PLAYING) {
currentsource++;
currentsource %= 10; //there are only 10 sources
}
alSourcei(Sources.get(currentsource), AL_BUFFER, buffer );
alSourcef(Sources.get(currentsource), AL_PITCH, 1.0f );
alSourcef(Sources.get(currentsource), AL_GAIN, 1.0f );
alSourcei(Sources.get(currentsource), AL_LOOPING, AL_FALSE);
alSourcef(Sources.get(currentsource), AL_REFERENCE_DISTANCE, 0);
alSourcef(Sources.get(currentsource), AL_MAX_DISTANCE, 100);
alSourcePlay(Sources.get(currentsource));
}
public static boolean hasLoaded(){return loaded;}我的怀疑是playSound方法,有没有更好的方法来找到非播放源代码?还有,有没有我没有给它的属性会导致声音没有任何3D属性?
发布于 2013-08-27 22:27:43
您应该查看音频文件通道,因为openAL仅对单声道声音应用衰减。
发布于 2013-06-28 20:13:10
您的源位置未设置。您可能听不到声音,因为默认情况下,它位于零位置,而您的收听者离那里很远。在playSound中,尝试设置如下内容:
alSourcefv(Sources.get(currentsource), AL_POSITION, sourcePosition);https://stackoverflow.com/questions/17328421
复制相似问题