MeshGame.cpp 5.9 KB

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