私の 3D ゲームでは、現在、ファクトリ クラスの「サウンド」で動作するサウンドがあります。カメラ クラスを使用して OpenAL を初期化しています。ロード時に、位置、向き、速度のグローバルな floatbuffer を保存します。
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 に提供しているにもかかわらず、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 属性を持たないサウンドになるような、私が指定していない属性はありますか?