💿🐜 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.1 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 "render/model.hpp"
  21. #include "render/material.hpp"
  22. namespace scene {
  23. model_instance::model_instance(render::model* model):
  24. model(nullptr),
  25. local_bounds{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}},
  26. world_bounds{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}},
  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. local_bounds = other.local_bounds;
  43. world_bounds = other.world_bounds;
  44. model = other.model;
  45. pose = other.pose;
  46. materials = other.materials;
  47. instanced = other.instanced;
  48. instance_count = other.instance_count;
  49. return *this;
  50. }
  51. void model_instance::set_model(render::model* model)
  52. {
  53. this->model = model;
  54. if (model)
  55. {
  56. materials.resize(model->get_groups()->size());
  57. reset_materials();
  58. pose = model->get_skeleton().bind_pose;
  59. ::concatenate(pose, pose);
  60. }
  61. else
  62. {
  63. pose.clear();
  64. }
  65. update_bounds();
  66. }
  67. void model_instance::set_material(std::size_t group_index, render::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 (render::model_group* group: *model->get_groups())
  104. {
  105. render::material* material = group->get_material();
  106. if (material)
  107. {
  108. material->update_tweens();
  109. }
  110. }
  111. }
  112. // Update material override tweens
  113. for (render::material* material: materials)
  114. {
  115. if (material)
  116. {
  117. material->update_tweens();
  118. }
  119. }
  120. }
  121. } // namespace scene