Audio3DSample.cpp 9.7 KB

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