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

128 lines
2.8 KiB

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