💿🐜 Antkeeper source code https://antkeeper.com
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.

121 lines
3.0 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <engine/scene/static-mesh.hpp>
  20. #include <engine/render/model.hpp>
  21. #include <engine/render/material.hpp>
  22. #include <engine/scene/camera.hpp>
  23. namespace scene {
  24. static_mesh::static_mesh(std::shared_ptr<render::model> model)
  25. {
  26. set_model(model);
  27. }
  28. void static_mesh::set_model(std::shared_ptr<render::model> model)
  29. {
  30. m_model = model;
  31. if (m_model)
  32. {
  33. m_operations.resize(m_model->get_groups().size());
  34. for (std::size_t i = 0; i < m_operations.size(); ++i)
  35. {
  36. const auto& group = m_model->get_groups()[i];
  37. auto& operation = m_operations[i];
  38. operation.vertex_array = m_model->get_vertex_array().get();
  39. operation.drawing_mode = group.drawing_mode;
  40. operation.start_index = group.start_index;
  41. operation.index_count = group.index_count;
  42. operation.material = group.material;
  43. }
  44. }
  45. else
  46. {
  47. m_operations.clear();
  48. }
  49. transformed();
  50. }
  51. void static_mesh::set_material(std::size_t index, std::shared_ptr<render::material> material)
  52. {
  53. if (material)
  54. {
  55. m_operations[index].material = material;
  56. }
  57. else
  58. {
  59. m_operations[index].material = m_model->get_groups()[index].material;
  60. }
  61. }
  62. void static_mesh::reset_materials()
  63. {
  64. for (std::size_t i = 0; i < m_operations.size(); ++i)
  65. {
  66. m_operations[i].material = m_model->get_groups()[i].material;
  67. }
  68. }
  69. void static_mesh::update_bounds()
  70. {
  71. if (m_model)
  72. {
  73. // Get model bounds
  74. const auto& model_bounds = m_model->get_bounds();
  75. // Naive algorithm: transform each corner of the model AABB
  76. m_bounds = {math::fvec3::infinity(), -math::fvec3::infinity()};
  77. for (std::size_t i = 0; i < 8; ++i)
  78. {
  79. m_bounds.extend(get_transform() * model_bounds.corner(i));
  80. }
  81. }
  82. else
  83. {
  84. m_bounds = {get_translation(), get_translation()};
  85. }
  86. }
  87. void static_mesh::transformed()
  88. {
  89. update_bounds();
  90. const math::fmat4 transform_matrix = get_transform().matrix();
  91. for (auto& operation: m_operations)
  92. {
  93. operation.transform = transform_matrix;
  94. }
  95. }
  96. void static_mesh::render(render::context& ctx) const
  97. {
  98. const float depth = ctx.camera->get_view_frustum().near().distance(get_translation());
  99. for (auto& operation: m_operations)
  100. {
  101. operation.depth = depth;
  102. operation.layer_mask = get_layer_mask();
  103. ctx.operations.push_back(&operation);
  104. }
  105. }
  106. } // namespace scene