Antkeeper  0.0.1
object.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_SCENE_OBJECT_HPP
21 #define ANTKEEPER_SCENE_OBJECT_HPP
22 
24 #include <engine/math/vector.hpp>
28 #include <atomic>
29 #include <cstdint>
30 
31 namespace scene {
32 
37 {
38 public:
43 
45  [[nodiscard]] virtual const std::size_t get_object_type_id() const noexcept = 0;
46 
52  inline virtual void render(render::context& ctx) const {}
53 
57  void look_at(const vector_type& position, const vector_type& target, const vector_type& up);
58 
64  inline constexpr void set_layer_mask(std::uint32_t mask) noexcept
65  {
66  m_layer_mask = mask;
67  }
68 
74  inline void set_transform(const transform_type& transform)
75  {
76  m_transform = transform;
77  transformed();
78  }
79 
85  inline void set_translation(const vector_type& translation)
86  {
87  m_transform.translation = translation;
88  transformed();
89  }
90 
97  {
99  transformed();
100  }
101 
108  inline void set_scale(const vector_type& scale)
109  {
111  transformed();
112  }
113  inline void set_scale(float scale)
114  {
116  transformed();
117  }
119 
121  [[nodiscard]] inline constexpr std::uint32_t get_layer_mask() const noexcept
122  {
123  return m_layer_mask;
124  }
125 
127  [[nodiscard]] inline constexpr const transform_type& get_transform() const noexcept
128  {
129  return m_transform;
130  }
131 
133  [[nodiscard]] inline constexpr const vector_type& get_translation() const noexcept
134  {
135  return m_transform.translation;
136  }
137 
139  [[nodiscard]] inline constexpr const quaternion_type& get_rotation() const noexcept
140  {
141  return m_transform.rotation;
142  }
143 
145  [[nodiscard]] inline constexpr const vector_type& get_scale() const noexcept
146  {
147  return m_transform.scale;
148  }
149 
151  [[nodiscard]] virtual const aabb_type& get_bounds() const noexcept = 0;
152 
153 protected:
154  static std::size_t next_object_type_id();
155 
159  inline virtual void transformed() {}
160 
161  std::uint32_t m_layer_mask{1};
163 };
164 
170 template <class T>
171 class object: public object_base
172 {
173 public:
175  static const std::atomic<std::size_t> object_type_id;
176 
177  [[nodiscard]] inline const std::size_t get_object_type_id() const noexcept final
178  {
179  return object_type_id;
180  }
181 };
182 
183 template <typename T>
184 const std::atomic<std::size_t> object<T>::object_type_id{object_base::next_object_type_id()};
185 
186 } // namespace scene
187 
188 #endif // ANTKEEPER_SCENE_OBJECT_HPP
Abstract base class for scene objects.
Definition: object.hpp:37
void set_transform(const transform_type &transform)
Sets the transform of the object.
Definition: object.hpp:74
virtual const aabb_type & get_bounds() const noexcept=0
Returns the bounds of the object.
static std::size_t next_object_type_id()
Definition: object.cpp:24
transform_type m_transform
Definition: object.hpp:162
math::fvec3 vector_type
Definition: object.hpp:39
constexpr std::uint32_t get_layer_mask() const noexcept
Returns the layer mask of the object.
Definition: object.hpp:121
virtual const std::size_t get_object_type_id() const noexcept=0
Returns the type ID for this scene object type.
constexpr const vector_type & get_translation() const noexcept
Returns the translation of the object.
Definition: object.hpp:133
void set_scale(const vector_type &scale)
Sets the scale of the object.
Definition: object.hpp:108
constexpr const vector_type & get_scale() const noexcept
Returns the scale of the object.
Definition: object.hpp:145
virtual void transformed()
Called every time the scene object's tranform is changed.
Definition: object.hpp:159
std::uint32_t m_layer_mask
Definition: object.hpp:161
constexpr void set_layer_mask(std::uint32_t mask) noexcept
Sets the layer mask of the object.
Definition: object.hpp:64
void set_rotation(const quaternion_type &rotation)
Sets the rotation of the object.
Definition: object.hpp:96
void set_scale(float scale)
Sets the scale of the object.
Definition: object.hpp:113
constexpr const quaternion_type & get_rotation() const noexcept
Returns the rotation of the object.
Definition: object.hpp:139
void look_at(const vector_type &position, const vector_type &target, const vector_type &up)
Definition: object.cpp:30
void set_translation(const vector_type &translation)
Sets the translation of the object.
Definition: object.hpp:85
constexpr const transform_type & get_transform() const noexcept
Returns the transform of the object.
Definition: object.hpp:127
Abstract base class for lights, cameras, model instances, and other scene objects.
Definition: object.hpp:172
static const std::atomic< std::size_t > object_type_id
Unique type ID for this scene object type.
Definition: object.hpp:175
const std::size_t get_object_type_id() const noexcept final
Returns the type ID for this scene object type.
Definition: object.hpp:177
quat< float > fquat
Quaternion with single-precision floating-point scalars.
Definition: quaternion.hpp:218
quaternion< T > rotation(const vec3< T > &from, const vec3< T > &to, T tolerance=T{1e-6})
Constructs a quaternion representing the minimum rotation from one direction to another direction.
Definition: quaternion.hpp:691
constexpr mat4< T > scale(const vec3< T > &v)
Constructs a scale matrix.
High-level rendering.
3D scene.
Definition: context.hpp:28
n-dimensional axis-aligned rectangle.
Quaternion composed of a real scalar part and imaginary vector part.
Definition: quaternion.hpp:39
vector_type scale
Scale vector.
Definition: transform.hpp:56
static constexpr transform identity() noexcept
Returns an identity transform.
Definition: transform.hpp:107
vector_type translation
Translation vector.
Definition: transform.hpp:50
quaternion_type rotation
Rotation quaternion.
Definition: transform.hpp:53
n-dimensional vector.
Definition: vector.hpp:44