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

119 lines
2.4 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 "renderer/model.hpp"
  20. #include "rasterizer/drawing-mode.hpp"
  21. model::model():
  22. bounds({0, 0, 0}, {0, 0, 0})
  23. {}
  24. model::~model()
  25. {
  26. for (model_group* group: groups)
  27. {
  28. delete group;
  29. }
  30. }
  31. model_group* model::add_group(const std::string& name)
  32. {
  33. if (!name.empty())
  34. {
  35. if (auto it = group_map.find(name); it != group_map.end())
  36. {
  37. return it->second;
  38. }
  39. }
  40. model_group* group = new model_group();
  41. group->index = groups.size();
  42. group->name = name;
  43. group->material = nullptr;
  44. group->drawing_mode = drawing_mode::triangles;
  45. group->start_index = 0;
  46. group->index_count = 0;
  47. groups.push_back(group);
  48. if (!name.empty())
  49. {
  50. group_map[name] = group;
  51. }
  52. return group;
  53. }
  54. bool model::remove_group(const std::string& name)
  55. {
  56. if (auto it = group_map.find(name); it != group_map.end())
  57. {
  58. return remove_group(it->second);
  59. }
  60. return false;
  61. }
  62. bool model::remove_group(model_group* group)
  63. {
  64. // Remove from group map
  65. if (!group->name.empty())
  66. {
  67. if (auto it = group_map.find(group->name); it != group_map.end())
  68. {
  69. group_map.erase(it);
  70. }
  71. }
  72. // Adjust indices of groups after this group
  73. for (std::size_t i = group->index + 1; i < groups.size(); ++i)
  74. {
  75. --groups[i]->index;
  76. }
  77. // Remove from groups
  78. groups.erase(groups.begin() + group->index);
  79. // Deallocate group
  80. delete group;
  81. return true;
  82. }
  83. const model_group* model::get_group(const std::string& name) const
  84. {
  85. if (auto it = group_map.find(name); it != group_map.end())
  86. {
  87. return it->second;
  88. }
  89. return nullptr;
  90. }
  91. model_group* model::get_group(const std::string& name)
  92. {
  93. if (auto it = group_map.find(name); it != group_map.end())
  94. {
  95. return it->second;
  96. }
  97. return nullptr;
  98. }