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

132 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2021 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 "scene/model-instance.hpp"
  20. #include "renderer/model.hpp"
  21. #include "renderer/material.hpp"
  22. namespace scene {
  23. model_instance::model_instance(::model* model):
  24. model(nullptr),
  25. pose(nullptr),
  26. bounds(get_translation(), get_translation()),
  27. instanced(false),
  28. instance_count(0)
  29. {
  30. set_model(model);
  31. update_bounds();
  32. }
  33. model_instance::model_instance():
  34. model_instance(nullptr)
  35. {}
  36. model_instance::model_instance(const model_instance& other)
  37. {
  38. *this = other;
  39. }
  40. model_instance& model_instance::operator=(const model_instance& other)
  41. {
  42. bounds = other.bounds;
  43. model = other.model;
  44. pose = other.pose;
  45. materials = other.materials;
  46. instanced = other.instanced;
  47. instance_count = other.instance_count;
  48. return *this;
  49. }
  50. void model_instance::set_model(::model* model)
  51. {
  52. this->model = model;
  53. this->pose = nullptr;
  54. if (model)
  55. {
  56. materials.resize(model->get_groups()->size());
  57. reset_materials();
  58. }
  59. update_bounds();
  60. }
  61. void model_instance::set_pose(::pose* pose)
  62. {
  63. this->pose = pose;
  64. }
  65. void model_instance::set_material(std::size_t group_index, material* material)
  66. {
  67. materials[group_index] = material;
  68. }
  69. void model_instance::set_instanced(bool instanced, std::size_t instance_count)
  70. {
  71. this->instanced = instanced;
  72. this->instance_count = (instanced) ? instance_count : 0;
  73. }
  74. void model_instance::reset_materials()
  75. {
  76. std::fill(materials.begin(), materials.end(), nullptr);
  77. }
  78. void model_instance::update_bounds()
  79. {
  80. if (model)
  81. bounds = aabb_type::transform(model->get_bounds(), get_transform());
  82. else
  83. bounds = {get_translation(), get_translation()};
  84. }
  85. void model_instance::transformed()
  86. {
  87. update_bounds();
  88. }
  89. void model_instance::update_tweens()
  90. {
  91. object_base::update_tweens();
  92. // Update model material tweens
  93. if (model)
  94. {
  95. for (model_group* group: *model->get_groups())
  96. {
  97. material* material = group->get_material();
  98. if (material)
  99. {
  100. material->update_tweens();
  101. }
  102. }
  103. }
  104. // Update material override tweens
  105. for (::material* material: materials)
  106. {
  107. if (material)
  108. {
  109. material->update_tweens();
  110. }
  111. }
  112. }
  113. } // namespace scene