MeshGame.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "MeshGame.h"
  2. // Declare our game instance
  3. MeshGame game;
  4. MeshGame::MeshGame()
  5. : _font(NULL), _scene(NULL), _modelNode(NULL), _touched(false), _touchX(0)
  6. {
  7. }
  8. MeshGame::~MeshGame()
  9. {
  10. }
  11. void MeshGame::initialize()
  12. {
  13. // Display the gameplay splash screen for at least 1 second.
  14. displayScreen(this, &MeshGame::drawSplash, NULL, 1000L);
  15. // Load the font
  16. _font = Font::create("res/arial.gpb");
  17. // Load the scene from file
  18. _scene = Scene::load("res/mesh.scene");
  19. // Get the duck node
  20. _modelNode = _scene->findNode("duck");
  21. // Find the light node
  22. Node* lightNode = _scene->findNode("directionalLight1");
  23. // Bind the light node's direction into duck's material.
  24. _modelNode->getModel()->getMaterial()->getParameter("u_directionalLightColor[0]")->setValue(lightNode->getLight()->getColor());
  25. _modelNode->getModel()->getMaterial()->getParameter("u_directionalLightDirection[0]")->bindValue(lightNode, &Node::getForwardVectorView);
  26. // Update the aspect ratio for our scene's camera to match the current device resolution
  27. _scene->getActiveCamera()->setAspectRatio(getAspectRatio());
  28. // Create the grid and add it to the scene.
  29. Model* model = createGridModel();
  30. _scene->addNode("grid")->setModel(model);
  31. model->release();
  32. }
  33. void MeshGame::finalize()
  34. {
  35. SAFE_RELEASE(_font);
  36. SAFE_RELEASE(_scene);
  37. }
  38. void MeshGame::update(float elapsedTime)
  39. {
  40. // Rotate model
  41. if (!_touched)
  42. _modelNode->rotateY(elapsedTime * MATH_DEG_TO_RAD(0.05f));
  43. }
  44. void MeshGame::render(float elapsedTime)
  45. {
  46. // Clear the color and depth buffers.
  47. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  48. // Visit all the nodes in the scene, drawing the models/mesh.
  49. _scene->visit(this, &MeshGame::drawScene);
  50. // Draw the fps
  51. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  52. }
  53. void MeshGame::keyEvent(Keyboard::KeyEvent evt, int key)
  54. {
  55. if (evt == Keyboard::KEY_PRESS)
  56. {
  57. switch (key)
  58. {
  59. case Keyboard::KEY_ESCAPE:
  60. exit();
  61. break;
  62. }
  63. }
  64. }
  65. void MeshGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  66. {
  67. switch (evt)
  68. {
  69. case Touch::TOUCH_PRESS:
  70. {
  71. _touched = true;
  72. _touchX = x;
  73. }
  74. break;
  75. case Touch::TOUCH_RELEASE:
  76. {
  77. _touched = false;
  78. _touchX = 0;
  79. }
  80. break;
  81. case Touch::TOUCH_MOVE:
  82. {
  83. int deltaX = x - _touchX;
  84. _touchX = x;
  85. _modelNode->rotateY(MATH_DEG_TO_RAD(deltaX * 0.5f));
  86. }
  87. break;
  88. default:
  89. break;
  90. };
  91. }
  92. bool MeshGame::drawScene(Node* node)
  93. {
  94. Model* model = node->getModel();
  95. if (model)
  96. model->draw();
  97. return true;
  98. }
  99. void MeshGame::drawFrameRate(Font* font, const Vector4& color, unsigned int x, unsigned int y, unsigned int fps)
  100. {
  101. char buffer[10];
  102. sprintf(buffer, "%u", fps);
  103. font->start();
  104. font->drawText(buffer, x, y, color, font->getSize());
  105. font->finish();
  106. }
  107. void MeshGame::drawSplash(void* param)
  108. {
  109. clear(CLEAR_COLOR_DEPTH, Vector4(0, 0, 0, 1), 1.0f, 0);
  110. SpriteBatch* batch = SpriteBatch::create("res/logo_powered_white.png");
  111. batch->start();
  112. batch->draw(this->getWidth() * 0.5f, this->getHeight() * 0.5f, 0.0f, 512.0f, 512.0f, 0.0f, 1.0f, 1.0f, 0.0f, Vector4::one(), true);
  113. batch->finish();
  114. SAFE_DELETE(batch);
  115. }
  116. Model* MeshGame::createGridModel(unsigned int lineCount)
  117. {
  118. // There needs to be an odd number of lines
  119. lineCount |= 1;
  120. const unsigned int pointCount = lineCount * 4;
  121. const unsigned int verticesSize = pointCount * (3 + 3); // (3 (position(xyz) + 3 color(rgb))
  122. std::vector<float> vertices;
  123. vertices.resize(verticesSize);
  124. const float gridLength = (float)(lineCount / 2);
  125. float value = -gridLength;
  126. for (unsigned int i = 0; i < verticesSize; ++i)
  127. {
  128. // Default line color is dark grey
  129. Vector4 color(0.3f, 0.3f, 0.3f, 1.0f);
  130. // Very 10th line is brighter grey
  131. if (((int)value) % 10 == 0)
  132. {
  133. color.set(0.45f, 0.45f, 0.45f, 1.0f);
  134. }
  135. // The Z axis is blue
  136. if (value == 0.0f)
  137. {
  138. color.set(0.15f, 0.15f, 0.7f, 1.0f);
  139. }
  140. // Build the lines
  141. vertices[i] = value;
  142. vertices[++i] = 0.0f;
  143. vertices[++i] = -gridLength;
  144. vertices[++i] = color.x;
  145. vertices[++i] = color.y;
  146. vertices[++i] = color.z;
  147. vertices[++i] = value;
  148. vertices[++i] = 0.0f;
  149. vertices[++i] = gridLength;
  150. vertices[++i] = color.x;
  151. vertices[++i] = color.y;
  152. vertices[++i] = color.z;
  153. // The X axis is red
  154. if (value == 0.0f)
  155. {
  156. color.set(0.7f, 0.15f, 0.15f, 1.0f);
  157. }
  158. vertices[++i] = -gridLength;
  159. vertices[++i] = 0.0f;
  160. vertices[++i] = value;
  161. vertices[++i] = color.x;
  162. vertices[++i] = color.y;
  163. vertices[++i] = color.z;
  164. vertices[++i] = gridLength;
  165. vertices[++i] = 0.0f;
  166. vertices[++i] = value;
  167. vertices[++i] = color.x;
  168. vertices[++i] = color.y;
  169. vertices[++i] = color.z;
  170. value += 1.0f;
  171. }
  172. VertexFormat::Element elements[] =
  173. {
  174. VertexFormat::Element(VertexFormat::POSITION, 3),
  175. VertexFormat::Element(VertexFormat::COLOR, 3)
  176. };
  177. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), pointCount, false);
  178. if (mesh == NULL)
  179. {
  180. return NULL;
  181. }
  182. mesh->setPrimitiveType(Mesh::LINES);
  183. mesh->setVertexData(&vertices[0], 0, pointCount);
  184. Model* model = Model::create(mesh);
  185. model->setMaterial("res/mesh.material#grid");
  186. SAFE_RELEASE(mesh);
  187. return model;
  188. }