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

185 lines
4.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. #ifndef ANTKEEPER_MATERIAL_HPP
  20. #define ANTKEEPER_MATERIAL_HPP
  21. #include "renderer/material-property.hpp"
  22. #include "gl/shader-program.hpp"
  23. #include <cstdint>
  24. #include <cstddef>
  25. #include <list>
  26. #include <map>
  27. #include <string>
  28. /**
  29. * A material is associated with exactly one shader program and contains a set of material properties which can be uploaded to that shader program via shader inputs.
  30. */
  31. class material
  32. {
  33. public:
  34. /**
  35. * Creates a material.
  36. *
  37. * @param program Shader program with which to associate this material.
  38. */
  39. explicit material(gl::shader_program* program);
  40. /**
  41. * Creates a material.
  42. */
  43. material();
  44. /**
  45. * Creates a copy of another material.
  46. *
  47. * @param other Material to copy.
  48. */
  49. material(const material& other);
  50. /**
  51. * Destroys a material.
  52. */
  53. ~material();
  54. /**
  55. * Makes this material a copy of aother material.
  56. *
  57. * @param other Material to copy.
  58. * @return Reference to this material.
  59. */
  60. material& operator=(const material& other);
  61. /**
  62. * Sets state 0 = state 1 for each material property tween.
  63. */
  64. void update_tweens();
  65. /**
  66. * Uploads each material property to the material's shader program.
  67. *
  68. * @param a Interpolation factor. Should be on `[0.0, 1.0]`.
  69. * @return Number of material property uploads which failed.
  70. */
  71. std::size_t upload(double a) const;
  72. /**
  73. * Sets the material's shader program and reconnects all shader properties to their corresponding shader inputs.
  74. *
  75. * @param program Shader program with which to associate the material.
  76. */
  77. void set_shader_program(gl::shader_program* program);
  78. /**
  79. * Sets the material flags.
  80. *
  81. * @param flags Material flags.
  82. */
  83. void set_flags(std::uint32_t flags);
  84. /**
  85. * Adds a material array property to the material.
  86. *
  87. * @param name Name of the material array property.
  88. * @param element_count Number of elements in the array.
  89. * @return Pointer to the added material property.
  90. */
  91. template <typename T>
  92. material_property<T>* add_property(const std::string& name, std::size_t element_count = 1);
  93. /**
  94. * Returns the shader program with which this material is associated.
  95. */
  96. gl::shader_program* get_shader_program() const;
  97. /**
  98. * Returns the material flags.
  99. */
  100. std::uint32_t get_flags() const;
  101. /**
  102. * Returns the material property with the specified name, or `nullptr` if the material could not be found.
  103. */
  104. material_property_base* get_property(const std::string& name) const;
  105. /**
  106. * Returns a list of all material properties in the material.
  107. */
  108. const std::list<material_property_base*>* get_properties() const;
  109. private:
  110. /**
  111. * Attempts to reconnect all material properties to their corresponding shader inputs.
  112. *
  113. * @return Number of disconnected properties.
  114. */
  115. std::size_t reconnect_properties();
  116. gl::shader_program* program;
  117. std::uint32_t flags;
  118. std::list<material_property_base*> properties;
  119. std::map<std::string, material_property_base*> property_map;
  120. };
  121. template <typename T>
  122. material_property<T>* material::add_property(const std::string& name, std::size_t element_count)
  123. {
  124. // Allocate property
  125. material_property<T>* property = new material_property<T>(element_count);
  126. // Add to property list and map
  127. properties.push_back(property);
  128. property_map[name] = property;
  129. // Attempt to connect property to its corresponding shader input
  130. if (program)
  131. {
  132. property->connect(program->get_input(name));
  133. }
  134. return property;
  135. }
  136. inline gl::shader_program* material::get_shader_program() const
  137. {
  138. return program;
  139. }
  140. inline std::uint32_t material::get_flags() const
  141. {
  142. return flags;
  143. }
  144. inline material_property_base* material::get_property(const std::string& name) const
  145. {
  146. if (auto it = property_map.find(name); it != property_map.end())
  147. {
  148. return it->second;
  149. }
  150. return nullptr;
  151. }
  152. inline const std::list<material_property_base*>* material::get_properties() const
  153. {
  154. return &properties;
  155. }
  156. #endif // ANTKEEPER_MATERIAL_HPP