MeshBatchSample.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "MeshBatchSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Mesh Batch", MeshBatchSample, 3);
  5. #endif
  6. static Material* createMaterial()
  7. {
  8. Material* material = Material::create("res/shaders/colored.vert", "res/shaders/colored.frag", "VERTEX_COLOR");
  9. GP_ASSERT(material && material->getStateBlock());
  10. return material;
  11. }
  12. static MeshBatch* createMeshBatch(Mesh::PrimitiveType primitiveType)
  13. {
  14. Material* material = createMaterial();
  15. VertexFormat::Element elements[] =
  16. {
  17. VertexFormat::Element(VertexFormat::POSITION, 3),
  18. VertexFormat::Element(VertexFormat::COLOR, 3)
  19. };
  20. unsigned int elementCount = sizeof(elements) / sizeof(VertexFormat::Element);
  21. MeshBatch* meshBatch = MeshBatch::create(VertexFormat(elements, elementCount), primitiveType, material, false);
  22. SAFE_RELEASE(material);
  23. return meshBatch;
  24. }
  25. static Vector3 randomColor()
  26. {
  27. return Vector3(MATH_RANDOM_0_1(), MATH_RANDOM_0_1(), MATH_RANDOM_0_1());
  28. }
  29. MeshBatchSample::MeshBatchSample()
  30. : _font(NULL), _meshBatch(NULL), _lastTriangleAdded(0)
  31. {
  32. _vertices.push_back(Vertex(Vector3(0, 50, 0), randomColor()));
  33. _vertices.push_back(Vertex(Vector3(-50, -50, 0), randomColor()));
  34. _vertices.push_back(Vertex(Vector3(50, -50, 0), randomColor()));
  35. }
  36. void MeshBatchSample::initialize()
  37. {
  38. setMultiTouch(true);
  39. // Create the font for drawing the framerate.
  40. _font = Font::create("res/ui/arial.gpb");
  41. Matrix::createOrthographic(getWidth(), getHeight(), -1.0f, 1.0f, &_worldViewProjectionMatrix);
  42. _meshBatch = createMeshBatch(Mesh::TRIANGLES);
  43. }
  44. void MeshBatchSample::finalize()
  45. {
  46. SAFE_RELEASE(_font);
  47. SAFE_DELETE(_meshBatch);
  48. }
  49. void MeshBatchSample::update(float elapsedTime)
  50. {
  51. }
  52. void MeshBatchSample::render(float elapsedTime)
  53. {
  54. // Clear the color and depth buffers
  55. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  56. // Draw all of the triangles as one mesh batch.
  57. _meshBatch->start();
  58. _meshBatch->add(&_vertices[0], (unsigned int)_vertices.size());
  59. _meshBatch->finish();
  60. _meshBatch->getMaterial()->getParameter("u_worldViewProjectionMatrix")->setValue(_worldViewProjectionMatrix);
  61. _meshBatch->draw();
  62. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  63. _font->start();
  64. char text[1024];
  65. sprintf(text, "Touch to add triangles (%d)", (int)(_vertices.size() / 3));
  66. _font->drawText(text, 10, getHeight() - _font->getSize() - 10, Vector4::one(), 18);
  67. _font->finish();
  68. }
  69. void MeshBatchSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  70. {
  71. switch (evt)
  72. {
  73. case Touch::TOUCH_PRESS:
  74. if (x < 75 && y < 50)
  75. {
  76. // Toggle Vsync if the user touches the top left corner
  77. setVsync(!isVsync());
  78. }
  79. else
  80. {
  81. addTriangle( x - (getWidth() >> 1), (getHeight() >> 1) - y);
  82. }
  83. break;
  84. case Touch::TOUCH_RELEASE:
  85. break;
  86. case Touch::TOUCH_MOVE:
  87. if (Game::getInstance()->getAbsoluteTime() - _lastTriangleAdded > 20)
  88. {
  89. addTriangle( x - (getWidth() >>1), (getHeight() >> 1) - y);
  90. }
  91. break;
  92. };
  93. }
  94. void MeshBatchSample::addTriangle(int x, int y)
  95. {
  96. // Calculate the vertices of the equilateral triangle.
  97. // length of the side (between 40 and 120)
  98. float a = MATH_RANDOM_0_1() * 80.0 + 40.0f;
  99. Vector3 p1(0.0f, a / sqrtf(3.0f), 0);
  100. Vector3 p2(-a / 2.0f, -a / (2.0f * sqrtf(3.0f)), 0);
  101. Vector3 p3( a / 2.0f, -a / (2.0f * sqrtf(3.0f)), 0);
  102. // Transform each point to x,y and rotate it randomly.
  103. Matrix m;
  104. m.translate(x, y, 0);
  105. m.rotateZ(MATH_RANDOM_MINUS1_1() * MATH_PI);
  106. m.transformPoint(p1, &p1);
  107. m.transformPoint(p2, &p2);
  108. m.transformPoint(p3, &p3);
  109. // Added the triangle to the list with random vertex colors.
  110. _vertices.push_back(Vertex(p1, randomColor()));
  111. _vertices.push_back(Vertex(p2, randomColor()));
  112. _vertices.push_back(Vertex(p3, randomColor()));
  113. _lastTriangleAdded = Game::getInstance()->getAbsoluteTime();
  114. }