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

127 lines
2.6 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/material.hpp"
  20. #include "gl/shader-program.hpp"
  21. material::material(gl::shader_program* program):
  22. program(program),
  23. flags(0)
  24. {}
  25. material::material():
  26. material(nullptr)
  27. {}
  28. material::material(const material& other)
  29. {
  30. *this = other;
  31. }
  32. material::~material()
  33. {
  34. for (material_property_base* property: properties)
  35. {
  36. delete property;
  37. }
  38. }
  39. material& material::operator=(const material& other)
  40. {
  41. // Remove all properties
  42. for (material_property_base* property: properties)
  43. {
  44. delete property;
  45. }
  46. properties.clear();
  47. property_map.clear();
  48. this->program = other.program;
  49. this->flags = other.flags;
  50. for (auto it = other.property_map.begin(); it != other.property_map.end(); ++it)
  51. {
  52. material_property_base* property = it->second->clone();
  53. properties.push_back(property);
  54. property_map[it->first] = property;
  55. }
  56. return *this;
  57. }
  58. void material::update_tweens()
  59. {
  60. for (material_property_base* property: properties)
  61. {
  62. property->update_tweens();
  63. }
  64. }
  65. std::size_t material::upload(double a) const
  66. {
  67. if (!program)
  68. {
  69. return false;
  70. }
  71. std::size_t failed_upload_count = 0;
  72. for (material_property_base* property: properties)
  73. {
  74. if (!property->upload(a))
  75. {
  76. ++failed_upload_count;
  77. }
  78. }
  79. return failed_upload_count;
  80. }
  81. void material::set_shader_program(gl::shader_program* program)
  82. {
  83. this->program = program;
  84. reconnect_properties();
  85. }
  86. void material::set_flags(std::uint32_t flags)
  87. {
  88. this->flags = flags;
  89. }
  90. std::size_t material::reconnect_properties()
  91. {
  92. std::size_t disconnected_property_count = properties.size();
  93. for (auto it = property_map.begin(); it != property_map.end(); ++it)
  94. {
  95. material_property_base* property = it->second;
  96. property->disconnect();
  97. if (program != nullptr)
  98. {
  99. if (property->connect(program->get_input(it->first)))
  100. {
  101. --disconnected_property_count;
  102. }
  103. }
  104. }
  105. return disconnected_property_count;
  106. }