25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

gradient.cpp 3.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <memory>
  14. LOLFX_RESOURCE_DECLARE(gradient);
  15. namespace lol
  16. {
  17. /*
  18. * Gradient implementation class
  19. */
  20. class GradientData
  21. {
  22. friend class Gradient;
  23. private:
  24. std::shared_ptr<Shader> shader;
  25. std::shared_ptr<VertexDeclaration> m_vdecl;
  26. std::shared_ptr<VertexBuffer> m_vbo, m_cbo;
  27. };
  28. /*
  29. * Public Gradient class
  30. */
  31. Gradient::Gradient(vec3 aa, vec3 bb)
  32. : data(std::make_unique<GradientData>())
  33. {
  34. /* FIXME: this should not be hardcoded */
  35. m_position = aa;
  36. m_aabb.aa = aa;
  37. m_aabb.bb = bb;
  38. }
  39. void Gradient::tick_game(float seconds)
  40. {
  41. entity::tick_game(seconds);
  42. }
  43. void Gradient::tick_draw(float seconds, Scene &scene)
  44. {
  45. entity::tick_draw(seconds, scene);
  46. float const vertex[] = { m_aabb.aa.x, m_aabb.aa.y, 0.0f,
  47. m_aabb.bb.x, m_aabb.aa.y, 0.0f,
  48. m_aabb.aa.x, m_aabb.bb.y, 0.0f,
  49. m_aabb.bb.x, m_aabb.bb.y, 0.0f,
  50. m_aabb.aa.x, m_aabb.bb.y, 0.0f,
  51. m_aabb.bb.x, m_aabb.aa.y, 0.0f, };
  52. float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
  53. 0.73f, 0.85f, 0.85f, 1.0f,
  54. 0.0f, 0.0f, 1.0f, 1.0f,
  55. 0.0f, 0.0f, 1.0f, 1.0f,
  56. 0.0f, 0.0f, 1.0f, 1.0f,
  57. 0.73f, 0.85f, 0.85f, 1.0f, };
  58. if (!data->shader)
  59. {
  60. data->shader = Shader::Create(LOLFX_RESOURCE_NAME(gradient));
  61. data->m_vbo = std::make_shared<VertexBuffer>(sizeof(vertex));
  62. data->m_cbo = std::make_shared<VertexBuffer>(sizeof(color));
  63. data->m_vdecl = std::make_shared<VertexDeclaration>
  64. (VertexStream<vec3>(VertexUsage::Position),
  65. VertexStream<vec4>(VertexUsage::Color));
  66. }
  67. mat4 model_matrix = mat4(1.0f);
  68. ShaderUniform uni_mat;
  69. ShaderAttrib attr_pos, attr_col;
  70. attr_pos = data->shader->GetAttribLocation(VertexUsage::Position, 0);
  71. attr_col = data->shader->GetAttribLocation(VertexUsage::Color, 0);
  72. data->shader->Bind();
  73. uni_mat = data->shader->GetUniformLocation("u_projection");
  74. uni_mat = data->shader->GetUniformLocation("u_view");
  75. uni_mat = data->shader->GetUniformLocation("u_model");
  76. data->shader->SetUniform(uni_mat, scene.GetCamera()->GetProjection());
  77. data->shader->SetUniform(uni_mat, scene.GetCamera()->GetView());
  78. data->shader->SetUniform(uni_mat, model_matrix);
  79. data->shader->Bind();
  80. data->m_vdecl->Bind();
  81. void *tmp = data->m_vbo->Lock(0, 0);
  82. memcpy(tmp, vertex, sizeof(vertex));
  83. data->m_vbo->Unlock();
  84. tmp = data->m_cbo->Lock(0, 0);
  85. memcpy(tmp, color, sizeof(color));
  86. data->m_cbo->Unlock();
  87. /* Bind vertex and color buffers */
  88. data->m_vdecl->SetStream(data->m_vbo, attr_pos);
  89. data->m_vdecl->SetStream(data->m_cbo, attr_col);
  90. /* Draw arrays */
  91. data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  92. }
  93. Gradient::~Gradient()
  94. {
  95. }
  96. } /* namespace lol */