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.
 
 
 

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