Просмотр исходного кода

finish first version of ALRenderer

mikymod 12 лет назад
Родитель
Сommit
4c0c71ce8b
2 измененных файлов с 130 добавлено и 30 удалено
  1. 92 17
      engine/renderers/al/ALRenderer.cpp
  2. 38 13
      engine/renderers/al/ALRenderer.h

+ 92 - 17
engine/renderers/al/ALRenderer.cpp

@@ -30,7 +30,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-
 //-----------------------------------------------------------------------------
 static const char* al_error_to_string(ALenum error)
 {
@@ -81,6 +80,19 @@ void ALRenderer::init()
 	}
 
 	AL_CHECK(alcMakeContextCurrent(m_context));
+
+	AL_CHECK(alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED));
+
+	AL_CHECK(alDopplerFactor(1.0f));
+	AL_CHECK(alDopplerVelocity(343.0f));
+
+	// Default listener
+	Vec3 pos(0.0f, 0.0f, 0.0f);
+	Vec3 vel(0.0f, 0.0f, 0.0f);
+	Vec3 at(0.0f, 0.0f, -1.0f);
+	Vec3 up(0.0f, 1.0f, 0.0f);
+
+	set_listener(pos, vel, at, up);
 }
 
 //-----------------------------------------------------------------------------
@@ -91,19 +103,13 @@ void ALRenderer::shutdown()
 }
 
 //-----------------------------------------------------------------------------
-SoundListener create_listener(float gain, Vec3 position, Vec3 velocity, Vec3 orientation_up, Vec3 orientation_at)
+void ALRenderer::set_listener(Vec3& position, Vec3& velocity, Vec3& orientation_up, Vec3& orientation_at)
 {
 	AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z));
 	AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z));
 
-	ALfloat orientation[] = {	
-								orientation_up.x, 
-								orientation_up.y, 
-								orientation_up.z,
-								orientation_at.x, 
-								orientation_at.y, 
-								orientation_at.z
-							};
+	ALfloat orientation[] = { orientation_up.x, orientation_up.y, orientation_up.z,
+								orientation_at.x, orientation_at.y, orientation_at.z };
 
 	AL_CHECK(alListenerfv(AL_ORIENTATION, orientation));
 }
@@ -178,8 +184,9 @@ void ALRenderer::destroy_buffer(SoundBufferId id)
 	AL_CHECK(alDeleteBuffers(1, &al_buffer.id));
 }
 
+
 //-----------------------------------------------------------------------------
-SoundSourceId ALRenderer::create_source(Vec3 position, Vec3 velocity, Vec3 direction)
+SoundSourceId ALRenderer::create_source(Vec3& position, Vec3& velocity, Vec3& direction, bool loop)
 {
 	SoundSourceId id = m_sources_id_table.create();
 
@@ -188,11 +195,11 @@ SoundSourceId ALRenderer::create_source(Vec3 position, Vec3 velocity, Vec3 direc
 	// Creates AL source
 	AL_CHECK(alGenSources(1, &al_source.id));
 
-    AL_CHECK(alSourcef(al_source.id, AL_GAIN, 1));
+	AL_CHECK(alSourcef(al_source.id, AL_PITCH, 1.0f));
 
-    AL_CHECK(alSourcef(al_source.id, AL_MIN_GAIN, 0.0f));
+	AL_CHECK(alSourcef(al_source.id, AL_REFERENCE_DISTANCE, 0.1f));
 
-    AL_CHECK(alSourcef(al_source.id, AL_MAX_GAIN, 1.0f));
+	AL_CHECK(alSourcef(al_source.id, AL_MAX_DISTANCE, 1000.0f));
 
 	AL_CHECK(alSource3f(al_source.id, AL_POSITION, position.x, position.y, position.z));
 
@@ -200,7 +207,14 @@ SoundSourceId ALRenderer::create_source(Vec3 position, Vec3 velocity, Vec3 direc
 
 	AL_CHECK(alSource3f(al_source.id, AL_DIRECTION, direction.x, direction.y, direction.z));
 
-	AL_CHECK(alSourcei(al_source.id, AL_LOOPING, AL_FALSE));
+	if (loop)
+	{
+		AL_CHECK(alSourcei(al_source.id, AL_LOOPING, AL_TRUE));
+	}
+	else
+	{
+		AL_CHECK(alSourcei(al_source.id, AL_LOOPING, AL_FALSE));
+	}
 
 	return id;
 }
@@ -222,7 +236,7 @@ void ALRenderer::pause_source(SoundSourceId id)
 
 	SoundSource& al_source = m_sources[id.index];
 
-	if (is_source_playing(id))
+	if (source_playing(id))
 	{
 		AL_CHECK(alSourcePause(al_source.id));
 	}
@@ -238,9 +252,70 @@ void ALRenderer::destroy_source(SoundSourceId id)
 	alDeleteSources(1, &al_source.id);
 }
 
+//-----------------------------------------------------------------------------
+void ALRenderer::bind_buffer(SoundSourceId sid, SoundBufferId bid)
+{
+	CE_ASSERT(m_sources_id_table.has(sid), "SoundSource does not exist");
+	CE_ASSERT(m_buffers_id_table.has(bid), "SoundBuffer does not exist");
+
+	SoundSource& al_source = m_sources[sid.index];
+	SoundBuffer& al_buffer = m_buffers[bid.index];
+
+	alSourcei(al_source.id, AL_BUFFER, al_buffer.id);
+}
+
+//-----------------------------------------------------------------------------
+void ALRenderer::set_source_position(SoundSourceId id, Vec3& pos)
+{
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
+
+	SoundSource& al_source = m_sources[id.index];
+
+	AL_CHECK(alSource3f(al_source.id, AL_POSITION, pos.x, pos.y, pos.z));
+}
+
+//-----------------------------------------------------------------------------
+void ALRenderer::set_source_velocity(SoundSourceId id, Vec3& vel)
+{
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
+
+	SoundSource& al_source = m_sources[id.index];
+
+	AL_CHECK(alSource3f(al_source.id, AL_VELOCITY, vel.x, vel.y, vel.z));
+}
+
+//-----------------------------------------------------------------------------
+void ALRenderer::set_source_direction(SoundSourceId id, Vec3& dir)
+{
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
+
+	SoundSource& al_source = m_sources[id.index];
+
+	AL_CHECK(alSource3f(al_source.id, AL_DIRECTION, dir.x, dir.y, dir.z));
+}
+
+//-----------------------------------------------------------------------------
+void ALRenderer::set_source_gain(SoundSourceId id, float gain)
+{
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
+
+	SoundSource& al_source = m_sources[id.index];
+
+	AL_CHECK(alSourcef(al_source.id, AL_GAIN, gain));	
+}
+
+//-----------------------------------------------------------------------------
+void ALRenderer::set_source_rolloff(SoundSourceId id, float rolloff)
+{
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
+
+	SoundSource& al_source = m_sources[id.index];
+
+	AL_CHECK(alSourcef(al_source.id, AL_ROLLOFF_FACTOR, rolloff));		
+}
 
 //-----------------------------------------------------------------------------
-bool ALRenderer::is_source_playing(SoundSourceId id)
+bool ALRenderer::source_playing(SoundSourceId id)
 {
 	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
 

+ 38 - 13
engine/renderers/al/ALRenderer.h

@@ -42,20 +42,11 @@ typedef Id SoundId;
 typedef Id SoundBufferId;
 typedef Id SoundSourceId;
 
-//-----------------------------------------------------------------------------
-struct SoundListener
-{
-	ALfloat gain;
-	ALfloat position[3];
-	ALfloat velocity[3];
-	ALfloat orientation[6];
-};
-
 //-----------------------------------------------------------------------------
 struct SoundBuffer
 {
 	ALuint 		id;
-	
+
 	ALenum 		format;
 	ALsizei		size;
 	ALsizei		freq;
@@ -86,20 +77,54 @@ public:
 
 							ALRenderer();
 
+	/// Init AL renderer. Must be called first
 	void					init();
+
+	/// Shutdown AL Renderer
 	void					shutdown();
 
-	SoundListener			create_listener(float gain, Vec3 position, Vec3 velocity, Vec3 orientation_up, Vec3 orientation_at);
+	/// Sets listener parameters. In OpenAL, @a position affects audibility of sounds, 
+	/// @a velocity affects doppler shift and @a orientation affects how a sound could be heard
+	void					set_listener(Vec3& position, Vec3& velocity, Vec3& orientation_up, Vec3& orientation_at);
 
+	/// Creates AL buffer's @a data, with a specific @a size, which contains sound raw data.
+	/// More parameters must be specified, such as @a sample_rate, that is the number of sample per unit of time 
+	/// taken from a continuous signal to make a discrete signal, @a channels which specifies if sound is mono or stereo and @ bxs
+	/// (bits per sample) which specifies the magnitude of samples information.
+	/// N.B: stereo sound can not be attenuated
 	SoundBufferId			create_buffer(const void* data, uint32_t size, uint32_t sample_rate, uint32_t channels, uint32_t bxs);
+
+	/// Destroys AL buffer
 	void					destroy_buffer(SoundBufferId id);
 
-	SoundSourceId			create_source(Vec3 position, Vec3 velocity, Vec3 direction);
+	/// Creates AL source of sound at the given @position in 3D space
+	/// @a velocity affects doppler shift and @a direction affects how a sound could be heard
+	SoundSourceId			create_source(Vec3& position, Vec3& velocity, Vec3& direction, bool loop);
+
+	/// Plays a sound, specified by @a id, previously created
 	void 					play_source(SoundSourceId id);
+
+	/// Pauses a sound, specified by @a id, previously created
 	void					pause_source(SoundSourceId id);
+
+	/// Destroys a sound, specified by @a id, previously created
 	void 					destroy_source(SoundSourceId id);
 
-	bool					is_source_playing(SoundSourceId id);
+	/// Binds a AL buffer to AL source
+	void					bind_buffer(SoundSourceId sid, SoundBufferId bid);
+
+	/// Sets source's @a position. It affects sound audibility
+	void					set_source_position(SoundSourceId id, Vec3& pos);
+	/// Sets source's @a velocity. It affects doppler shift
+	void					set_source_velocity(SoundSourceId id, Vec3& vel);
+	/// Sets source's @a direction. It affects how a sound could be heard
+	void					set_source_direction(SoundSourceId id, Vec3& dir);
+	/// Sets source's @a gain, that is measure sound's amplification
+	void 					set_source_gain(SoundSourceId id, float gain);
+	/// Sets source's @a rolloff factor. Greater it is, greater sound's attenuation is
+	void					set_source_rolloff(SoundSourceId id, float rolloff);
+	/// Is source #@a id playing?
+	bool					source_playing(SoundSourceId id);
 
 private: