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

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