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

172 lines
4.2 KiB

  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. #ifndef ANTKEEPER_RENDER_MODEL_HPP
  20. #define ANTKEEPER_RENDER_MODEL_HPP
  21. #include <engine/animation/skeleton.hpp>
  22. #include <engine/geom/primitives/box.hpp>
  23. #include <engine/gl/primitive-topology.hpp>
  24. #include <engine/gl/vertex-array.hpp>
  25. #include <engine/gl/vertex-buffer.hpp>
  26. #include <engine/render/material.hpp>
  27. #include <engine/utility/hash/fnv1a.hpp>
  28. #include <cstdint>
  29. #include <memory>
  30. #include <string>
  31. #include <vector>
  32. namespace render {
  33. /**
  34. * Part of a model which is associated with exactly one material.
  35. */
  36. struct model_group
  37. {
  38. hash::fnv1a32_t id{};
  39. gl::primitive_topology primitive_topology{gl::primitive_topology::triangle_list};
  40. std::uint32_t first_vertex{};
  41. std::uint32_t vertex_count{};
  42. std::shared_ptr<render::material> material;
  43. };
  44. /**
  45. *
  46. */
  47. class model
  48. {
  49. public:
  50. /// AABB type.
  51. using aabb_type = geom::box<float>;
  52. /**
  53. * Sets the byte offset to the first vertex in the vertex buffer.
  54. *
  55. * @param offset Byte offset into the vertex buffer.
  56. */
  57. inline void set_vertex_offset(std::size_t offset) noexcept
  58. {
  59. m_vertex_offset = offset;
  60. }
  61. /**
  62. * Sets the byte stride between consecutive elements within the vertex buffer.
  63. *
  64. * @param stride Byte stride between consecutive elements within the vertex buffer.
  65. */
  66. inline void set_vertex_stride(std::size_t stride) noexcept
  67. {
  68. m_vertex_stride = stride;
  69. }
  70. /**
  71. * Returns the vertex array associated with this model.
  72. */
  73. /// @{
  74. [[nodiscard]] inline const std::shared_ptr<gl::vertex_array>& get_vertex_array() const noexcept
  75. {
  76. return m_vertex_array;
  77. }
  78. [[nodiscard]] inline std::shared_ptr<gl::vertex_array>& get_vertex_array() noexcept
  79. {
  80. return m_vertex_array;
  81. }
  82. /// @}
  83. /**
  84. * Returns the vertex buffer associated with this model.
  85. */
  86. /// @{
  87. [[nodiscard]] inline const std::shared_ptr<gl::vertex_buffer>& get_vertex_buffer() const noexcept
  88. {
  89. return m_vertex_buffer;
  90. }
  91. [[nodiscard]] inline std::shared_ptr<gl::vertex_buffer>& get_vertex_buffer() noexcept
  92. {
  93. return m_vertex_buffer;
  94. }
  95. /// @}
  96. /// Returns the byte offset to the first vertex in the vertex buffer.
  97. [[nodiscard]] inline constexpr std::size_t get_vertex_offset() const noexcept
  98. {
  99. return m_vertex_offset;
  100. }
  101. /// Returns the byte stride between consecutive elements within the vertex buffer.
  102. [[nodiscard]] inline constexpr std::size_t get_vertex_stride() const noexcept
  103. {
  104. return m_vertex_stride;
  105. }
  106. /**
  107. * Returns the bounds of the model.
  108. */
  109. /// @{
  110. [[nodiscard]] inline const aabb_type& get_bounds() const noexcept
  111. {
  112. return m_bounds;
  113. }
  114. [[nodiscard]] inline aabb_type& get_bounds() noexcept
  115. {
  116. return m_bounds;
  117. }
  118. /// @}
  119. /**
  120. * Returns the model's model groups.
  121. */
  122. /// @{
  123. [[nodiscard]] inline const std::vector<model_group>& get_groups() const noexcept
  124. {
  125. return m_groups;
  126. }
  127. [[nodiscard]] inline std::vector<model_group>& get_groups() noexcept
  128. {
  129. return m_groups;
  130. }
  131. /// @}
  132. /**
  133. * Returns the skeleton associated with this model.
  134. */
  135. /// @{
  136. [[nodiscard]] inline const ::skeleton& get_skeleton() const noexcept
  137. {
  138. return m_skeleton;
  139. }
  140. [[nodiscard]] inline ::skeleton& get_skeleton() noexcept
  141. {
  142. return m_skeleton;
  143. }
  144. /// @}
  145. private:
  146. std::shared_ptr<gl::vertex_array> m_vertex_array;
  147. std::shared_ptr<gl::vertex_buffer> m_vertex_buffer;
  148. std::size_t m_vertex_offset{};
  149. std::size_t m_vertex_stride{};
  150. aabb_type m_bounds{{0, 0, 0}, {0, 0, 0}};
  151. std::vector<model_group> m_groups;
  152. ::skeleton m_skeleton;
  153. };
  154. } // namespace render
  155. #endif // ANTKEEPER_RENDER_MODEL_HPP