Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

gradient.cpp 3.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "lolgl.h"
  15. using namespace std;
  16. LOLFX_RESOURCE_DECLARE(gradient);
  17. namespace lol
  18. {
  19. /*
  20. * Gradient implementation class
  21. */
  22. class GradientData
  23. {
  24. friend class Gradient;
  25. private:
  26. Shader *shader;
  27. VertexDeclaration *m_vdecl;
  28. VertexBuffer *m_vbo, *m_cbo;
  29. };
  30. /*
  31. * Public Gradient class
  32. */
  33. Gradient::Gradient(vec3 aa, vec3 bb)
  34. : data(new GradientData())
  35. {
  36. /* FIXME: this should not be hardcoded */
  37. m_position = aa;
  38. m_bbox[0] = aa;
  39. m_bbox[1] = bb;
  40. data->shader = nullptr;
  41. }
  42. void Gradient::TickGame(float seconds)
  43. {
  44. Entity::TickGame(seconds);
  45. }
  46. void Gradient::TickDraw(float seconds)
  47. {
  48. Entity::TickDraw(seconds);
  49. float const vertex[] = { m_bbox[0].x, m_bbox[0].y, 0.0f,
  50. m_bbox[1].x, m_bbox[0].y, 0.0f,
  51. m_bbox[0].x, m_bbox[1].y, 0.0f,
  52. m_bbox[1].x, m_bbox[1].y, 0.0f,
  53. m_bbox[0].x, m_bbox[1].y, 0.0f,
  54. m_bbox[1].x, m_bbox[0].y, 0.0f, };
  55. float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
  56. 0.73f, 0.85f, 0.85f, 1.0f,
  57. 0.0f, 0.0f, 1.0f, 1.0f,
  58. 0.0f, 0.0f, 1.0f, 1.0f,
  59. 0.0f, 0.0f, 1.0f, 1.0f,
  60. 0.73f, 0.85f, 0.85f, 1.0f, };
  61. if (!data->shader)
  62. {
  63. data->shader = Shader::Create(LOLFX_RESOURCE_NAME(gradient));
  64. data->m_vbo = new VertexBuffer(sizeof(vertex));
  65. data->m_cbo = new VertexBuffer(sizeof(color));
  66. data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
  67. VertexStream<vec4>(VertexUsage::Color));
  68. }
  69. mat4 model_matrix = mat4(1.0f);
  70. ShaderUniform uni_mat;
  71. ShaderAttrib attr_pos, attr_col;
  72. attr_pos = data->shader->GetAttribLocation("in_Vertex", VertexUsage::Position, 0);
  73. attr_col = data->shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
  74. data->shader->Bind();
  75. uni_mat = data->shader->GetUniformLocation("proj_matrix");
  76. data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetCamera()->GetProjection());
  77. uni_mat = data->shader->GetUniformLocation("view_matrix");
  78. data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetCamera()->GetView());
  79. uni_mat = data->shader->GetUniformLocation("model_matrix");
  80. data->shader->SetUniform(uni_mat, model_matrix);
  81. data->shader->Bind();
  82. data->m_vdecl->Bind();
  83. void *tmp = data->m_vbo->Lock(0, 0);
  84. memcpy(tmp, vertex, sizeof(vertex));
  85. data->m_vbo->Unlock();
  86. tmp = data->m_cbo->Lock(0, 0);
  87. memcpy(tmp, color, sizeof(color));
  88. data->m_cbo->Unlock();
  89. /* Bind vertex and color buffers */
  90. data->m_vdecl->SetStream(data->m_vbo, attr_pos);
  91. data->m_vdecl->SetStream(data->m_cbo, attr_col);
  92. /* Draw arrays */
  93. data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  94. }
  95. Gradient::~Gradient()
  96. {
  97. /* FIXME: destroy shader */
  98. delete data;
  99. }
  100. } /* namespace lol */