From 4692375491fb16da59642022c8d7c891d68ba665 Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Tue, 27 Jul 2010 12:12:06 +0200 Subject: [PATCH] Added convenience classes for connecting sound to Ogre --- input/clients/ogre_input_capture.hpp | 6 +-- sound/clients/ogre_listener_mover.hpp | 69 +++++++++++++++++++++++++++ sound/clients/ogre_output_updater.hpp | 31 ++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 sound/clients/ogre_listener_mover.hpp create mode 100644 sound/clients/ogre_output_updater.hpp diff --git a/input/clients/ogre_input_capture.hpp b/input/clients/ogre_input_capture.hpp index 6a7beae4d2..2e77dc10b1 100644 --- a/input/clients/ogre_input_capture.hpp +++ b/input/clients/ogre_input_capture.hpp @@ -15,15 +15,15 @@ namespace Input { { Mangle::Input::Driver &driver; - ExitListener(Mangle::Input::Driver &drv) + OgreInputCapture(Mangle::Input::Driver &drv) : driver(drv) {} - bool frameStarted(const FrameEvent &evt) + bool frameStarted(const Ogre::FrameEvent &evt) { driver.capture(); return true; } }; -} +}} #endif diff --git a/sound/clients/ogre_listener_mover.hpp b/sound/clients/ogre_listener_mover.hpp new file mode 100644 index 0000000000..d1cfcb65ce --- /dev/null +++ b/sound/clients/ogre_listener_mover.hpp @@ -0,0 +1,69 @@ +#ifndef MANGLE_SOUND_OGRELISTENERMOVER_H +#define MANGLE_SOUND_OGRELISTENERMOVER_H + +#include +#include +#include "../output.hpp" + +namespace Mangle { +namespace Sound { + + /** This class lets a sound listener (ie. the SoundFactory) track a + given camera in Ogre3D. The poisition and orientation of the + listener will be updated to match the camera whenever the camera + is moved. + */ + struct OgreListenerMover : Ogre::Camera::Listener + { + OgreListenerMover(Mangle::Sound::SoundFactoryPtr snd) + : soundFact(snd), camera(NULL) + {} + + /// Follow a camera. WARNING: This will OVERRIDE any other + /// MovableObject::Listener you may have attached to the camera. + void followCamera(Ogre::Camera *cam) + { + camera = cam; + camera->addListener(this); + } + + private: + Mangle::Sound::SoundFactoryPtr soundFact; + Ogre::Camera *camera; + Ogre::Vector3 pos, dir, up; + + /// From Camera::Listener. This is called once per + /// frame. Unfortunately, Ogre doesn't allow us to be notified + /// only when the camera itself has moved, so we must poll every + /// frame. + void cameraPreRenderScene(Ogre::Camera *cam) + { + assert(cam == camera); + + Ogre::Vector3 nPos, nDir, nUp; + + nPos = camera->getPosition(); + nDir = camera->getDirection(); + nUp = camera->getUp(); + + // Don't bother the sound system needlessly + if(nDir != dir || nPos != pos || nUp != up) + { + pos = nPos; + dir = nDir; + up = nUp; + + soundFact->setListenerPos(pos.x, pos.y, pos.z, + dir.x, dir.y, dir.z, + up.x, up.y, up.z); + } + } + + void cameraDestroyed(Ogre::Camera *cam) + { + assert(cam == camera); + camera = NULL; + } + }; +}} +#endif diff --git a/sound/clients/ogre_output_updater.hpp b/sound/clients/ogre_output_updater.hpp new file mode 100644 index 0000000000..5072e9f1e8 --- /dev/null +++ b/sound/clients/ogre_output_updater.hpp @@ -0,0 +1,31 @@ +#ifndef MANGLE_SOUND_OGREUPDATER_H +#define MANGLE_SOUND_OGREUPDATER_H + +/* + This Ogre FrameListener calls update on a SoundFactory + */ + +#include +#include "../output.hpp" +#include + +namespace Mangle { +namespace Sound { + + struct OgreOutputUpdater : Ogre::FrameListener + { + Mangle::Sound::SoundFactory &driver; + + OgreOutputUpdater(Mangle::Sound::SoundFactory &drv) + : driver(drv) + { assert(drv.needsUpdate); } + + bool frameStarted(const Ogre::FrameEvent &evt) + { + driver.update(); + return true; + } + }; +}} + +#endif