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

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