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

239 lines
5.4 KiB

  1. /*
  2. * Copyright (C) 2020 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_SCENE_OBJECT_HPP
  20. #define ANTKEEPER_SCENE_OBJECT_HPP
  21. #include <atomic>
  22. #include <cstdlib>
  23. #include <vmq/vmq.hpp>
  24. #include "animation/tween.hpp"
  25. #include "geometry/bounding-volume.hpp"
  26. using namespace vmq::types;
  27. using vmq::quaternion;
  28. using vmq::transform;
  29. /**
  30. * Internal base class for scene objects.
  31. */
  32. class scene_object_base
  33. {
  34. public:
  35. /// Returns the type ID for this scene object type.
  36. virtual const std::size_t get_object_type_id() const = 0;
  37. /**
  38. * Creates a scene object base.
  39. */
  40. scene_object_base();
  41. /**
  42. * Destroys a scene object base.
  43. */
  44. virtual ~scene_object_base() = default;
  45. /**
  46. * Updates all tweens in the scene object.
  47. */
  48. virtual void update_tweens();
  49. /**
  50. * Activates or deactivates the scene object.
  51. */
  52. void set_active(bool active);
  53. /**
  54. *
  55. */
  56. void look_at(const float3& position, const float3& target, const float3& up);
  57. /**
  58. * Sets the scene object's transform.
  59. */
  60. void set_transform(const transform<float>& transform);
  61. /**
  62. * Sets the scene object's translation.
  63. */
  64. void set_translation(const float3& translation);
  65. /**
  66. * Sets the scene object's rotation.
  67. */
  68. void set_rotation(const quaternion<float>& rotation);
  69. /**
  70. * Sets the scene object's scale.
  71. */
  72. void set_scale(const float3& scale);
  73. /**
  74. * Sets a culling mask for the object, which will be used for view-frustum culling instead of the object's bounds.
  75. */
  76. void set_culling_mask(const bounding_volume<float>* culling_mask);
  77. /// Returns whether the scene object is active.
  78. bool is_active() const;
  79. /**
  80. * Returns the transform.
  81. */
  82. const transform<float>& get_transform() const;
  83. /**
  84. * Returns the transform's translation vector.
  85. */
  86. const float3& get_translation() const;
  87. /**
  88. * Returns the transform's rotation quaternion.
  89. */
  90. const quaternion<float>& get_rotation() const;
  91. /**
  92. * Returns the transform's scale vector.
  93. */
  94. const float3& get_scale() const;
  95. /**
  96. * Returns the transform tween.
  97. */
  98. const tween<transform<float>>& get_transform_tween() const;
  99. tween<transform<float>>& get_transform_tween();
  100. /**
  101. * Returns the bounds of the object.
  102. */
  103. virtual const bounding_volume<float>& get_bounds() const = 0;
  104. /**
  105. * Returns the culling mask of the object.
  106. */
  107. const bounding_volume<float>* get_culling_mask() const;
  108. protected:
  109. static std::size_t next_object_type_id();
  110. private:
  111. /**
  112. * Called every time the scene object's tranform is changed.
  113. */
  114. virtual void transformed();
  115. bool active;
  116. tween<transform<float>> transform;
  117. const bounding_volume<float>* culling_mask;
  118. };
  119. inline void scene_object_base::set_active(bool active)
  120. {
  121. this->active = active;
  122. }
  123. inline void scene_object_base::set_transform(const ::transform<float>& transform)
  124. {
  125. this->transform[1] = transform;
  126. transformed();
  127. }
  128. inline void scene_object_base::set_translation(const float3& translation)
  129. {
  130. transform[1].translation = translation;
  131. transformed();
  132. }
  133. inline void scene_object_base::set_rotation(const quaternion<float>& rotation)
  134. {
  135. transform[1].rotation = rotation;
  136. transformed();
  137. }
  138. inline void scene_object_base::set_scale(const float3& scale)
  139. {
  140. transform[1].scale = scale;
  141. transformed();
  142. }
  143. inline bool scene_object_base::is_active() const
  144. {
  145. return active;
  146. }
  147. inline const transform<float>& scene_object_base::get_transform() const
  148. {
  149. return transform[1];
  150. }
  151. inline const float3& scene_object_base::get_translation() const
  152. {
  153. return get_transform().translation;
  154. }
  155. inline const quaternion<float>& scene_object_base::get_rotation() const
  156. {
  157. return get_transform().rotation;
  158. }
  159. inline const float3& scene_object_base::get_scale() const
  160. {
  161. return get_transform().scale;
  162. }
  163. inline const tween<transform<float>>& scene_object_base::get_transform_tween() const
  164. {
  165. return transform;
  166. }
  167. inline tween<transform<float>>& scene_object_base::get_transform_tween()
  168. {
  169. return transform;
  170. }
  171. inline const bounding_volume<float>* scene_object_base::get_culling_mask() const
  172. {
  173. return culling_mask;
  174. }
  175. /**
  176. * Abstract base class for lights, cameras, model instances, and other scene objects.
  177. *
  178. * @tparam T This should be the same class that's inheriting from the scene object, in order to give it a valid type-specific ID.
  179. */
  180. template <class T>
  181. class scene_object: public scene_object_base
  182. {
  183. public:
  184. /// Unique type ID for this scene object type.
  185. static const std::atomic<std::size_t> object_type_id;
  186. virtual const std::size_t get_object_type_id() const final;
  187. };
  188. template <typename T>
  189. const std::atomic<std::size_t> scene_object<T>::object_type_id{scene_object_base::next_object_type_id()};
  190. template <typename T>
  191. inline const std::size_t scene_object<T>::get_object_type_id() const
  192. {
  193. return object_type_id;
  194. }
  195. #endif // ANTKEEPER_SCENE_OBJECT_HPP