You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

129 lines
3.3 KiB

  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. using namespace std;
  15. LOLFX_RESOURCE_DECLARE(gradient);
  16. namespace lol
  17. {
  18. /*
  19. * Gradient implementation class
  20. */
  21. class GradientData
  22. {
  23. friend class Gradient;
  24. private:
  25. Shader *shader;
  26. VertexDeclaration *m_vdecl;
  27. VertexBuffer *m_vbo, *m_cbo;
  28. };
  29. /*
  30. * Public Gradient class
  31. */
  32. Gradient::Gradient(vec3 aa, vec3 bb)
  33. : data(new GradientData())
  34. {
  35. /* FIXME: this should not be hardcoded */
  36. m_position = aa;
  37. m_bbox[0] = aa;
  38. m_bbox[1] = bb;
  39. data->shader = nullptr;
  40. }
  41. void Gradient::TickGame(float seconds)
  42. {
  43. Entity::TickGame(seconds);
  44. }
  45. void Gradient::TickDraw(float seconds)
  46. {
  47. Entity::TickDraw(seconds);
  48. float const vertex[] = { m_bbox[0].x, m_bbox[0].y, 0.0f,
  49. m_bbox[1].x, m_bbox[0].y, 0.0f,
  50. m_bbox[0].x, m_bbox[1].y, 0.0f,
  51. m_bbox[1].x, m_bbox[1].y, 0.0f,
  52. m_bbox[0].x, m_bbox[1].y, 0.0f,
  53. m_bbox[1].x, m_bbox[0].y, 0.0f, };
  54. float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
  55. 0.73f, 0.85f, 0.85f, 1.0f,
  56. 0.0f, 0.0f, 1.0f, 1.0f,
  57. 0.0f, 0.0f, 1.0f, 1.0f,
  58. 0.0f, 0.0f, 1.0f, 1.0f,
  59. 0.73f, 0.85f, 0.85f, 1.0f, };
  60. if (!data->shader)
  61. {
  62. data->shader = Shader::Create(LOLFX_RESOURCE_NAME(gradient));
  63. data->m_vbo = new VertexBuffer(sizeof(vertex));
  64. data->m_cbo = new VertexBuffer(sizeof(color));
  65. data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
  66. VertexStream<vec4>(VertexUsage::Color));
  67. }
  68. mat4 model_matrix = mat4(1.0f);
  69. ShaderUniform uni_mat;
  70. ShaderAttrib attr_pos, attr_col;
  71. attr_pos = data->shader->GetAttribLocation(VertexUsage::Position, 0);
  72. attr_col = data->shader->GetAttribLocation(VertexUsage::Color, 0);
  73. data->shader->Bind();
  74. uni_mat = data->shader->GetUniformLocation("u_projection");
  75. data->shader->SetUniform(uni_mat, g_scene->GetCamera()->GetProjection());
  76. uni_mat = data->shader->GetUniformLocation("u_view");
  77. data->shader->SetUniform(uni_mat, g_scene->GetCamera()->GetView());
  78. uni_mat = data->shader->GetUniformLocation("u_model");
  79. data->shader->SetUniform(uni_mat, model_matrix);
  80. data->shader->Bind();
  81. data->m_vdecl->Bind();
  82. void *tmp = data->m_vbo->Lock(0, 0);
  83. memcpy(tmp, vertex, sizeof(vertex));
  84. data->m_vbo->Unlock();
  85. tmp = data->m_cbo->Lock(0, 0);
  86. memcpy(tmp, color, sizeof(color));
  87. data->m_cbo->Unlock();
  88. /* Bind vertex and color buffers */
  89. data->m_vdecl->SetStream(data->m_vbo, attr_pos);
  90. data->m_vdecl->SetStream(data->m_cbo, attr_col);
  91. /* Draw arrays */
  92. data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  93. }
  94. Gradient::~Gradient()
  95. {
  96. /* FIXME: destroy shader */
  97. delete data;
  98. }
  99. } /* namespace lol */