💿🐜 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.

100 lines
2.3 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/model-instance.hpp>
  20. #include <engine/render/model.hpp>
  21. #include <engine/render/material.hpp>
  22. namespace scene {
  23. model_instance::model_instance(std::shared_ptr<render::model> model)
  24. {
  25. set_model(model);
  26. }
  27. void model_instance::set_model(std::shared_ptr<render::model> model)
  28. {
  29. this->model = model;
  30. if (model)
  31. {
  32. materials.resize(model->get_groups().size());
  33. reset_materials();
  34. pose = model->get_skeleton().bind_pose;
  35. ::concatenate(pose, pose);
  36. }
  37. else
  38. {
  39. pose.clear();
  40. }
  41. update_bounds();
  42. }
  43. void model_instance::set_material(std::size_t group_index, std::shared_ptr<render::material> material)
  44. {
  45. materials[group_index] = material;
  46. }
  47. void model_instance::set_instanced(bool instanced, std::size_t instance_count)
  48. {
  49. this->instanced = instanced;
  50. this->instance_count = (instanced) ? instance_count : 0;
  51. }
  52. void model_instance::reset_materials()
  53. {
  54. std::fill(materials.begin(), materials.end(), nullptr);
  55. }
  56. void model_instance::update_bounds()
  57. {
  58. if (model)
  59. {
  60. local_bounds = aabb_type::transform(model->get_bounds(), get_transform());
  61. transformed();
  62. }
  63. else
  64. {
  65. local_bounds = {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
  66. world_bounds = {get_translation(), get_translation()};
  67. }
  68. }
  69. void model_instance::transformed()
  70. {
  71. world_bounds = aabb_type::transform(local_bounds, get_transform());
  72. }
  73. void model_instance::update_tweens()
  74. {
  75. object_base::update_tweens();
  76. // Update material override tweens
  77. for (auto& material: materials)
  78. {
  79. if (material)
  80. {
  81. //material->update_tweens();
  82. }
  83. }
  84. }
  85. } // namespace scene