Antkeeper  0.0.1
material.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
20 #ifndef ANTKEEPER_RENDER_MATERIAL_HPP
21 #define ANTKEEPER_RENDER_MATERIAL_HPP
22 
23 #include <cstdint>
29 #include <unordered_map>
30 
31 namespace render {
32 
36 class material
37 {
38 public:
42  material() = default;
43 
49  material(const material& other);
50 
54  ~material() = default;
55 
62  material& operator=(const material& other);
63 
66 
72  void set_two_sided(bool two_sided) noexcept;
73 
79  void set_blend_mode(material_blend_mode mode) noexcept;
80 
86  void set_shadow_mode(material_shadow_mode mode) noexcept;
87 
93  void set_flags(std::uint32_t flags) noexcept;
94 
96  [[nodiscard]] inline bool is_two_sided() const noexcept
97  {
98  return two_sided;
99  }
100 
102  [[nodiscard]] inline material_blend_mode get_blend_mode() const noexcept
103  {
104  return blend_mode;
105  }
106 
108  [[nodiscard]] inline material_shadow_mode get_shadow_mode() const noexcept
109  {
110  return shadow_mode;
111  }
112 
114  [[nodiscard]] inline std::uint32_t get_flags() const noexcept
115  {
116  return flags;
117  }
118 
120 
123 
129  void set_shader_template(std::shared_ptr<gl::shader_template> shader_template);
130 
134  [[nodiscard]] inline const std::shared_ptr<gl::shader_template>& get_shader_template() const noexcept
135  {
136  return shader_template;
137  }
138 
145  void set_variable(hash::fnv1a32_t key, std::shared_ptr<material_variable_base> value);
146 
154  [[nodiscard]] std::shared_ptr<material_variable_base> get_variable(hash::fnv1a32_t key) const;
155 
161  [[nodiscard]] inline const std::unordered_map<hash::fnv1a32_t, std::shared_ptr<material_variable_base>>& get_variables() const noexcept
162  {
163  return variable_map;
164  }
165 
167 
179  [[nodiscard]] inline std::size_t hash() const noexcept
180  {
181  return m_hash;
182  }
183 
184 private:
188  void rehash() noexcept;
189 
190  bool two_sided{false};
193  std::uint32_t flags{0};
194  std::shared_ptr<gl::shader_template> shader_template;
195  std::unordered_map<hash::fnv1a32_t, std::shared_ptr<material_variable_base>> variable_map;
196  std::size_t m_hash{0};
197 };
198 
199 } // namespace render
200 
201 #endif // ANTKEEPER_RENDER_MATERIAL_HPP
A material is associated with exactly one shader program and contains a set of material properties wh...
Definition: material.hpp:37
const std::shared_ptr< gl::shader_template > & get_shader_template() const noexcept
Returns the shader template with which this material is associated.
Definition: material.hpp:134
void set_variable(hash::fnv1a32_t key, std::shared_ptr< material_variable_base > value)
Sets the value of a material variable with the given name.
Definition: material.cpp:95
material()=default
Constructs a material.
std::uint32_t get_flags() const noexcept
Returns the material flags.
Definition: material.hpp:114
~material()=default
Destroys a material.
material_shadow_mode get_shadow_mode() const noexcept
Returns the material shadow mode.
Definition: material.hpp:108
void set_two_sided(bool two_sided) noexcept
Enables or disables back-face culling of the material surface.
Definition: material.cpp:60
std::size_t hash() const noexcept
Returns a hash of the material state.
Definition: material.hpp:179
std::shared_ptr< material_variable_base > get_variable(hash::fnv1a32_t key) const
Returns a shared pointer to the material variable with the given name, or nullptr if not found.
Definition: material.cpp:100
material & operator=(const material &other)
Makes this material a copy of aother material.
Definition: material.cpp:38
const std::unordered_map< hash::fnv1a32_t, std::shared_ptr< material_variable_base > > & get_variables() const noexcept
Returns all material variables.
Definition: material.hpp:161
void set_flags(std::uint32_t flags) noexcept
Sets the material flags.
Definition: material.cpp:81
bool is_two_sided() const noexcept
Returns true if the material surface is two-sided, and false otherwise.
Definition: material.hpp:96
void set_blend_mode(material_blend_mode mode) noexcept
Sets the material blend mode.
Definition: material.cpp:67
void set_shadow_mode(material_shadow_mode mode) noexcept
Sets the material shadow mode.
Definition: material.cpp:74
material_blend_mode get_blend_mode() const noexcept
Returns the material blend mode.
Definition: material.hpp:102
void set_shader_template(std::shared_ptr< gl::shader_template > shader_template)
Sets the material's shader template.
Definition: material.cpp:88
High-level rendering.
material_blend_mode
Material blend modes.
@ opaque
Material is fully opaque.
material_shadow_mode
Material shadow casting modes.
@ opaque
Material casts fully opaque shadows.
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117