MeshGame.cpp 6.0 KB

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