TriangleSample.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "TriangleSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Draw Triangle", TriangleSample, 1);
  5. #endif
  6. /**
  7. * Creates a triangle mesh with vertex colors.
  8. */
  9. static Mesh* createTriangleMesh()
  10. {
  11. // Calculate the vertices of the equilateral triangle.
  12. float a = 0.5f; // length of the side
  13. Vector2 p1(0.0f, a / sqrtf(3.0f));
  14. Vector2 p2(-a / 2.0f, -a / (2.0f * sqrtf(3.0f)));
  15. Vector2 p3( a / 2.0f, -a / (2.0f * sqrtf(3.0f)));
  16. // Create 3 vertices. Each vertex has position (x, y, z) and color (red, green, blue)
  17. float vertices[] =
  18. {
  19. p1.x, p1.y, 0.0f, 1.0f, 0.0f, 0.0f,
  20. p2.x, p2.y, 0.0f, 0.0f, 1.0f, 0.0f,
  21. p3.x, p3.y, 0.0f, 0.0f, 0.0f, 1.0f,
  22. };
  23. unsigned int vertexCount = 3;
  24. VertexFormat::Element elements[] =
  25. {
  26. VertexFormat::Element(VertexFormat::POSITION, 3),
  27. VertexFormat::Element(VertexFormat::COLOR, 3)
  28. };
  29. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), vertexCount, false);
  30. if (mesh == NULL)
  31. {
  32. GP_ERROR("Failed to create mesh.");
  33. return NULL;
  34. }
  35. mesh->setPrimitiveType(Mesh::TRIANGLES);
  36. mesh->setVertexData(vertices, 0, vertexCount);
  37. return mesh;
  38. }
  39. TriangleSample::TriangleSample()
  40. : _font(NULL), _model(NULL), _spinDirection(-1.0f)
  41. {
  42. }
  43. void TriangleSample::initialize()
  44. {
  45. // Create the font for drawing the framerate.
  46. _font = Font::create("res/common/arial.gpb");
  47. // Create an orthographic projection matrix.
  48. float width = getWidth() / (float)getHeight();
  49. float height = 1.0f;
  50. Matrix::createOrthographic(width, height, -1.0f, 1.0f, &_worldViewProjectionMatrix);
  51. // Create the triangle mesh.
  52. Mesh* mesh = createTriangleMesh();
  53. // Create a model for the triangle mesh. A model is an instance of a Mesh that can be drawn with a specified material.
  54. _model = Model::create(mesh);
  55. SAFE_RELEASE(mesh);
  56. // Create a material from the built-in "colored-unlit" vertex and fragment shaders.
  57. // This sample doesn't use lighting so the unlit shader is used.
  58. // This sample uses vertex color so VERTEX_COLOR is defined. Look at the shader source files to see the supported defines.
  59. _model->setMaterial("res/shaders/colored.vert", "res/shaders/colored.frag", "VERTEX_COLOR");
  60. }
  61. void TriangleSample::finalize()
  62. {
  63. // Model and font are reference counted and should be released before closing this sample.
  64. SAFE_RELEASE(_model);
  65. SAFE_RELEASE(_font);
  66. }
  67. void TriangleSample::update(float elapsedTime)
  68. {
  69. // Update the rotation of the triangle. The speed is 180 degrees per second.
  70. _worldViewProjectionMatrix.rotateZ( _spinDirection * MATH_PI * elapsedTime * 0.001f);
  71. }
  72. void TriangleSample::render(float elapsedTime)
  73. {
  74. // Clear the color and depth buffers
  75. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  76. // Bind the view projection matrix to the model's parameter. This will transform the vertices when the model is drawn.
  77. _model->getMaterial()->getParameter("u_worldViewProjectionMatrix")->setValue(_worldViewProjectionMatrix);
  78. _model->draw();
  79. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  80. }
  81. void TriangleSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  82. {
  83. switch (evt)
  84. {
  85. case Touch::TOUCH_PRESS:
  86. if (x < 75 && y < 50)
  87. {
  88. // Toggle Vsync if the user touches the top left corner
  89. setVsync(!isVsync());
  90. }
  91. else
  92. {
  93. // Reverse the spin direction if the user touches the screen.
  94. _spinDirection *= -1.0f;
  95. }
  96. break;
  97. case Touch::TOUCH_RELEASE:
  98. break;
  99. case Touch::TOUCH_MOVE:
  100. break;
  101. };
  102. }