Audio3DSample.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "Audio3DSample.h"
  2. #include "Grid.h"
  3. #include "SamplesGame.h"
  4. #if defined(ADD_SAMPLE)
  5. ADD_SAMPLE("Audio", "3D Audio", Audio3DSample, 1);
  6. #endif
  7. static const unsigned int MOVE_FORWARD = 1;
  8. static const unsigned int MOVE_BACKWARD = 2;
  9. static const unsigned int MOVE_LEFT = 4;
  10. static const unsigned int MOVE_RIGHT = 8;
  11. static const unsigned int MOVE_UP = 16;
  12. static const unsigned int MOVE_DOWN = 32;
  13. static const float MOVE_SPEED = 15.0f;
  14. static const float UP_DOWN_SPEED = 10.0f;
  15. Audio3DSample::Audio3DSample()
  16. : _font(NULL), _scene(NULL), _cubeNode(NULL), _gamepad(NULL), _moveFlags(0), _prevX(0), _prevY(0), _buttonPressed(false)
  17. {
  18. }
  19. void Audio3DSample::initialize()
  20. {
  21. setMultiTouch(true);
  22. _font = Font::create("res/common/arial.gpb");
  23. // Load game scene from file
  24. _scene = Scene::load("res/common/box.gpb");
  25. // Get light node
  26. Node* lightNode = _scene->findNode("directionalLight1");
  27. Light* light = lightNode->getLight();
  28. // Initialize box model
  29. Node* boxNode = _scene->findNode("box");
  30. Model* boxModel = boxNode->getModel();
  31. Material* boxMaterial = boxModel->setMaterial("res/common/box.material#lambert1");
  32. boxMaterial->getParameter("u_directionalLightColor[0]")->setValue(light->getColor());
  33. boxMaterial->getParameter("u_directionalLightDirection[0]")->setValue(lightNode->getForwardVectorWorld());
  34. // Remove the cube from the scene but keep a reference to it.
  35. _cubeNode = boxNode;
  36. _cubeNode->addRef();
  37. _scene->removeNode(_cubeNode);
  38. loadGrid(_scene);
  39. // Initialize cameraa
  40. Vector3 cameraPosition(5, 5, 1);
  41. if (Camera* camera = _scene->getActiveCamera())
  42. {
  43. camera->getNode()->getTranslation(&cameraPosition);
  44. }
  45. _fpCamera.initialize();
  46. _fpCamera.setPosition(cameraPosition);
  47. _scene->addNode(_fpCamera.getRootNode());
  48. _scene->setActiveCamera(_fpCamera.getCamera());
  49. _gamepad = getGamepad(0);
  50. // This is needed because the virtual gamepad is shared between all samples.
  51. // SamplesGame always ensures the virtual gamepad is disabled when a sample is exited.
  52. if (_gamepad && _gamepad->isVirtual())
  53. _gamepad->getForm()->setEnabled(true);
  54. }
  55. void Audio3DSample::finalize()
  56. {
  57. SAFE_RELEASE(_scene);
  58. SAFE_RELEASE(_font);
  59. SAFE_RELEASE(_cubeNode);
  60. for (std::map<std::string, Node*>::iterator it = _audioNodes.begin(); it != _audioNodes.end(); ++it)
  61. {
  62. it->second->release();
  63. }
  64. _audioNodes.clear();
  65. }
  66. void Audio3DSample::update(float elapsedTime)
  67. {
  68. float time = (float)elapsedTime / 1000.0f;
  69. Vector2 move;
  70. if (_moveFlags != 0)
  71. {
  72. // Forward motion
  73. if (_moveFlags & MOVE_FORWARD)
  74. {
  75. move.y = 1;
  76. }
  77. else if (_moveFlags & MOVE_BACKWARD)
  78. {
  79. move.y = -1;
  80. }
  81. // Strafing
  82. if (_moveFlags & MOVE_LEFT)
  83. {
  84. move.x = 1;
  85. }
  86. else if (_moveFlags & MOVE_RIGHT)
  87. {
  88. move.x = -1;
  89. }
  90. move.normalize();
  91. // Up and down
  92. if (_moveFlags & MOVE_UP)
  93. {
  94. _fpCamera.moveUp(time * UP_DOWN_SPEED);
  95. }
  96. else if (_moveFlags & MOVE_DOWN)
  97. {
  98. _fpCamera.moveDown(time * UP_DOWN_SPEED);
  99. }
  100. }
  101. else if (_gamepad->getJoystickCount() > 0)
  102. {
  103. _gamepad->getJoystickValues(0, &move);
  104. move.x = -move.x;
  105. }
  106. if (_gamepad->getJoystickCount() > 1)
  107. {
  108. Vector2 joy2;
  109. _gamepad->getJoystickValues(1, &joy2);
  110. _fpCamera.rotate(MATH_DEG_TO_RAD(joy2.x * 2.0f), MATH_DEG_TO_RAD(joy2.y * 2.0f));
  111. }
  112. if (!move.isZero())
  113. {
  114. move.scale(time * MOVE_SPEED);
  115. _fpCamera.moveForward(move.y);
  116. _fpCamera.moveLeft(move.x);
  117. }
  118. if (!_buttonPressed && _gamepad->isButtonDown(Gamepad::BUTTON_A))
  119. {
  120. addSound("footsteps.wav");
  121. }
  122. _buttonPressed = _gamepad->isButtonDown(Gamepad::BUTTON_A);
  123. }
  124. void Audio3DSample::render(float elapsedTime)
  125. {
  126. // Clear the color and depth buffers
  127. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  128. // Visit all the nodes in the scene for drawing
  129. _scene->visit(this, &Audio3DSample::drawScene);
  130. drawDebugText(0, _font->getSize());
  131. _gamepad->draw();
  132. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  133. }
  134. bool Audio3DSample::drawScene(Node* node)
  135. {
  136. // If the node visited contains a model, draw it
  137. Model* model = node->getModel();
  138. if (model)
  139. {
  140. model->draw();
  141. }
  142. return true;
  143. }
  144. void Audio3DSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  145. {
  146. switch (evt)
  147. {
  148. case Touch::TOUCH_PRESS:
  149. if (x < 75 && y < 50)
  150. {
  151. // Toggle Vsync if the user touches the top left corner
  152. setVsync(!isVsync());
  153. }
  154. _prevX = x;
  155. _prevY = y;
  156. break;
  157. case Touch::TOUCH_RELEASE:
  158. _prevX = 0;
  159. _prevY = 0;
  160. break;
  161. case Touch::TOUCH_MOVE:
  162. {
  163. int deltaX = x - _prevX;
  164. int deltaY = y - _prevY;
  165. _prevX = x;
  166. _prevY = y;
  167. float pitch = -MATH_DEG_TO_RAD(deltaY * 0.5f);
  168. float yaw = MATH_DEG_TO_RAD(deltaX * 0.5f);
  169. _fpCamera.rotate(yaw, pitch);
  170. break;
  171. }
  172. };
  173. }
  174. void Audio3DSample::keyEvent(Keyboard::KeyEvent evt, int key)
  175. {
  176. if (evt == Keyboard::KEY_PRESS)
  177. {
  178. switch (key)
  179. {
  180. case Keyboard::KEY_W:
  181. _moveFlags |= MOVE_FORWARD;
  182. break;
  183. case Keyboard::KEY_S:
  184. _moveFlags |= MOVE_BACKWARD;
  185. break;
  186. case Keyboard::KEY_A:
  187. _moveFlags |= MOVE_LEFT;
  188. break;
  189. case Keyboard::KEY_D:
  190. _moveFlags |= MOVE_RIGHT;
  191. break;
  192. case Keyboard::KEY_Q:
  193. _moveFlags |= MOVE_DOWN;
  194. break;
  195. case Keyboard::KEY_E:
  196. _moveFlags |= MOVE_UP;
  197. break;
  198. case Keyboard::KEY_PG_UP:
  199. _fpCamera.rotate(0, MATH_PIOVER4);
  200. break;
  201. case Keyboard::KEY_PG_DOWN:
  202. _fpCamera.rotate(0, -MATH_PIOVER4);
  203. break;
  204. case Keyboard::KEY_ONE:
  205. case Keyboard::KEY_SPACE:
  206. addSound("footsteps.wav");
  207. break;
  208. }
  209. }
  210. else if (evt == Keyboard::KEY_RELEASE)
  211. {
  212. switch (key)
  213. {
  214. case Keyboard::KEY_W:
  215. _moveFlags &= ~MOVE_FORWARD;
  216. break;
  217. case Keyboard::KEY_S:
  218. _moveFlags &= ~MOVE_BACKWARD;
  219. break;
  220. case Keyboard::KEY_A:
  221. _moveFlags &= ~MOVE_LEFT;
  222. break;
  223. case Keyboard::KEY_D:
  224. _moveFlags &= ~MOVE_RIGHT;
  225. break;
  226. case Keyboard::KEY_Q:
  227. _moveFlags &= ~MOVE_DOWN;
  228. break;
  229. case Keyboard::KEY_E:
  230. _moveFlags &= ~MOVE_UP;
  231. break;
  232. }
  233. }
  234. }
  235. bool Audio3DSample::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  236. {
  237. switch (evt)
  238. {
  239. case Mouse::MOUSE_WHEEL:
  240. _fpCamera.moveForward(wheelDelta * MOVE_SPEED / 2.0f );
  241. return true;
  242. }
  243. return false;
  244. }
  245. void Audio3DSample::addSound(const std::string& file)
  246. {
  247. std::string path("res/common/");
  248. path.append(file);
  249. Node* node = NULL;
  250. std::map<std::string, Node*>::iterator it = _audioNodes.find(path);
  251. if (it != _audioNodes.end())
  252. {
  253. node = it->second->clone();
  254. }
  255. else
  256. {
  257. AudioSource* audioSource = AudioSource::create(path.c_str());
  258. assert(audioSource);
  259. audioSource->setLooped(true);
  260. node = _cubeNode->clone();
  261. node->setId(file.c_str());
  262. node->setAudioSource(audioSource);
  263. audioSource->release();
  264. _audioNodes[path] = node;
  265. node->addRef();
  266. }
  267. assert(node);
  268. Node* cameraNode = _scene->getActiveCamera()->getNode();
  269. // Position the sound infront of the user
  270. node->setTranslation(cameraNode->getTranslationWorld());
  271. Vector3 dir = cameraNode->getForwardVectorWorld().normalize();
  272. dir.scale(2);
  273. node->translate(dir);
  274. _scene->addNode(node);
  275. node->getAudioSource()->play();
  276. node->release();
  277. }
  278. void Audio3DSample::drawDebugText(int x, int y)
  279. {
  280. _font->start();
  281. static const int V_SPACE = 16;
  282. AudioListener* audioListener = AudioListener::getInstance();
  283. drawVector3("Position", audioListener->getPosition(), x, y);
  284. drawVector3("Forward", audioListener->getOrientationForward(), x, y+=_font->getSize());
  285. drawVector3("Orientation", audioListener->getOrientationUp(), x, y+=_font->getSize());
  286. drawVector3("Velocity", audioListener->getVelocity(), x, y+=_font->getSize());
  287. _font->finish();
  288. }
  289. void Audio3DSample::drawVector3(const char* str, const Vector3& vector, int x, int y)
  290. {
  291. char buffer[255];
  292. sprintf(buffer, "%s: (%f, %f, %f)", str, vector.x, vector.y, vector.z);
  293. _font->drawText(buffer, x, y, Vector4::one(), _font->getSize());
  294. }
  295. void Audio3DSample::loadGrid(Scene* scene)
  296. {
  297. assert(scene);
  298. Model* gridModel = createGridModel();
  299. assert(gridModel);
  300. gridModel->setMaterial("res/common/grid.material");
  301. Node* node = scene->addNode("grid");
  302. node->setModel(gridModel);
  303. gridModel->release();
  304. }
  305. void Audio3DSample::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
  306. {
  307. switch(evt)
  308. {
  309. case Gamepad::CONNECTED_EVENT:
  310. case Gamepad::DISCONNECTED_EVENT:
  311. _gamepad = getGamepad(0);
  312. // This is needed because the virtual gamepad is shared between all samples.
  313. // SamplesGame always ensures the virtual gamepad is disabled when a sample is exited.
  314. if (_gamepad && _gamepad->isVirtual())
  315. _gamepad->getForm()->setEnabled(true);
  316. break;
  317. }
  318. }