Antkeeper  0.0.1
material-variable.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_VARIABLE_HPP
21 #define ANTKEEPER_RENDER_MATERIAL_VARIABLE_HPP
22 
23 #include <engine/math/vector.hpp>
24 #include <engine/math/matrix.hpp>
25 #include <engine/gl/texture.hpp>
27 #include <memory>
28 #include <vector>
29 
30 namespace render {
31 
36 {
37 public:
41  virtual ~material_variable_base() = default;
42 
46  [[nodiscard]] virtual constexpr material_variable_type type() const noexcept = 0;
47 
51  [[nodiscard]] virtual std::size_t size() const noexcept = 0;
52 
56  [[nodiscard]] virtual std::unique_ptr<material_variable_base> clone() const = 0;
57 };
58 
64 template <class T>
66 {
67 public:
69  using element_type = T;
70 
77  inline explicit material_variable(std::size_t size, const element_type& value = element_type()):
78  elements(size, value)
79  {}
80 
86  {}
87 
93  inline explicit material_variable(std::initializer_list<element_type> list):
94  elements(list)
95  {}
96 
97  [[nodiscard]] virtual constexpr material_variable_type type() const noexcept override;
98 
99  [[nodiscard]] inline std::size_t size() const noexcept override
100  {
101  return elements.size();
102  }
103 
104  [[nodiscard]] inline std::unique_ptr<material_variable_base> clone() const override
105  {
106  return std::make_unique<material_variable<T>>(*this);
107  }
108 
114  inline void set(const element_type& value)
115  {
116  elements.front() = value;
117  }
118 
125  inline void set(std::size_t index, const element_type& value)
126  {
127  elements[index] = value;
128  }
129 
133  [[nodiscard]] inline const element_type& get() const
134  {
135  return elements.front();
136  }
137 
145  [[nodiscard]] inline const element_type& get(std::size_t index) const
146  {
147  return elements[index];
148  }
149 
153  [[nodiscard]] inline const element_type* data() const noexcept
154  {
155  return elements.data();
156  }
157 
158 private:
159  std::vector<element_type> elements;
160 };
161 
164 
167 
170 
173 
176 
179 
182 
185 
188 
191 
194 
197 
200 
203 
206 
209 
212 
215 
218 
221 
224 
227 
230 
231 template <>
232 inline constexpr material_variable_type matvar_bool::type() const noexcept
233 {
235 }
236 
237 template <>
238 inline constexpr material_variable_type matvar_bvec2::type() const noexcept
239 {
241 }
242 
243 template <>
244 inline constexpr material_variable_type matvar_bvec3::type() const noexcept
245 {
247 }
248 
249 template <>
250 inline constexpr material_variable_type matvar_bvec4::type() const noexcept
251 {
253 }
254 
255 template <>
256 inline constexpr material_variable_type matvar_int::type() const noexcept
257 {
259 }
260 
261 template <>
262 inline constexpr material_variable_type matvar_ivec2::type() const noexcept
263 {
265 }
266 
267 template <>
268 inline constexpr material_variable_type matvar_ivec3::type() const noexcept
269 {
271 }
272 
273 template <>
274 inline constexpr material_variable_type matvar_ivec4::type() const noexcept
275 {
277 }
278 
279 template <>
280 inline constexpr material_variable_type matvar_uint::type() const noexcept
281 {
283 }
284 
285 template <>
286 inline constexpr material_variable_type matvar_uvec2::type() const noexcept
287 {
289 }
290 
291 template <>
292 inline constexpr material_variable_type matvar_uvec3::type() const noexcept
293 {
295 }
296 
297 template <>
298 inline constexpr material_variable_type matvar_uvec4::type() const noexcept
299 {
301 }
302 
303 template <>
304 inline constexpr material_variable_type matvar_float::type() const noexcept
305 {
307 }
308 
309 template <>
310 inline constexpr material_variable_type matvar_fvec2::type() const noexcept
311 {
313 }
314 
315 template <>
316 inline constexpr material_variable_type matvar_fvec3::type() const noexcept
317 {
319 }
320 
321 template <>
322 inline constexpr material_variable_type matvar_fvec4::type() const noexcept
323 {
325 }
326 
327 template <>
328 inline constexpr material_variable_type matvar_fmat2::type() const noexcept
329 {
331 }
332 
333 template <>
334 inline constexpr material_variable_type matvar_fmat3::type() const noexcept
335 {
337 }
338 
339 template <>
340 inline constexpr material_variable_type matvar_fmat4::type() const noexcept
341 {
343 }
344 
345 template <>
346 inline constexpr material_variable_type matvar_texture_1d::type() const noexcept
347 {
349 }
350 
351 template <>
352 inline constexpr material_variable_type matvar_texture_2d::type() const noexcept
353 {
355 }
356 
357 template <>
358 inline constexpr material_variable_type matvar_texture_3d::type() const noexcept
359 {
361 }
362 
363 template <>
364 inline constexpr material_variable_type matvar_texture_cube::type() const noexcept
365 {
367 }
368 
369 } // namespace render
370 
371 #endif // ANTKEEPER_RENDER_MATERIAL_VARIABLE_HPP
Abstract base class for material variables.
virtual constexpr material_variable_type type() const noexcept=0
Returns the material variable data type.
virtual std::size_t size() const noexcept=0
Returns the number of elements in an array variable, or 1 if the variable is not an array.
virtual std::unique_ptr< material_variable_base > clone() const =0
Creates a copy of this material property.
virtual ~material_variable_base()=default
Destructs a material variable base.
T element_type
Material variable element type.
void set(std::size_t index, const element_type &value)
Sets the value of a single element in an array variable.
material_variable(std::size_t size, const element_type &value=element_type())
Constructs a material variable.
const element_type * data() const noexcept
Returns a pointer to the element array.
material_variable(std::initializer_list< element_type > list)
Constructs a material variable from a list of element values.
const element_type & get(std::size_t index) const
Returns a reference to the element at a given index.
material_variable()
Constructs a material variable with a single element.
std::unique_ptr< material_variable_base > clone() const override
Creates a copy of this material property.
const element_type & get() const
Returns a reference to the first element in the array.
virtual constexpr material_variable_type type() const noexcept override
Returns the material variable data type.
void set(const element_type &value)
Sets the value of the the variable, or the value of the first element if the variable is an array.
High-level rendering.
material_variable_type
Material variable data types.