Initial commit
[camargo/neiasound.git] / src / nSoundListener.cpp
1 #include "nSoundListener.h"
2 #include "nSoundSystem.h"
3 #ifdef NEIA
4 #include "../scene/nSceneCamera.h"
5 #include "OgreCamera.h"
6 #endif
7
8 #include "AL/al.h"
9
10 nSoundListener::nSoundListener(nSoundSystem * parent) :
11     QObject(parent)
12 {
13 #ifdef NEIA
14     m_camera = 0;
15 #endif
16     m_updating = true;
17 }
18
19 #ifdef NEIA
20 void nSoundListener::setSourceCamera(nSceneCamera * cam)
21 {
22     if(m_camera)
23     {
24         disconnect(this, SLOT(cameraDestroyed()));
25     }
26     m_camera = cam;
27     if(m_camera) //camera can be set to null
28     {
29         m_camLastPos = m_camera->ogreCamera()->getPosition();
30         connect(m_camera, SIGNAL(destroyed()), SLOT(cameraDestroyed()));
31     }
32 }
33 void nSoundListener::cameraDestroyed()
34 {
35     disconnect(this, SLOT(cameraDestroyed()));
36     m_camera = 0;
37 }
38 #endif
39
40 void nSoundListener::update(qreal frameTime)
41 {
42 #ifdef NEIA
43     if(m_updating && m_camera)
44     {
45         nVec3 pos(m_camera->ogreCamera()->getRealPosition()),
46                 dir(m_camera->ogreCamera()->getRealDirection()),
47                 up(m_camera->ogreCamera()->getRealUp());
48         nVec3 vel = pos - m_camLastPos;
49         m_camLastPos = pos;
50         vel/=frameTime;
51
52
53         updateManual(QVector3D());
54     }
55 #endif
56
57 }
58
59 void nSoundListener::updateManual(QVector3D pos, QVector3D dir, QVector3D up, QVector3D vel)
60 {
61     float orient[6];
62     orient[0] = dir[0];
63     orient[1] = dir[1];
64     orient[2] = dir[2];
65     orient[3] = up[0];
66     orient[4] = up[1];
67     orient[5] = up[2];
68
69     alGetError();
70     alListener3f(AL_POSITION, pos[0], pos[1], pos[2]);
71     alListenerfv(AL_ORIENTATION, orient);
72     alListener3f(AL_VELOCITY, vel[0], vel[1], vel[2]);
73
74     if(alGetError()!=AL_NO_ERROR)
75         throw QString("nSoundListener: Failed to update listener transformations.");
76 }