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

212 lines
6.8 KiB

  1. /*
  2. * Copyright (C) 2021 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_GL_TEXTURE_HPP
  20. #define ANTKEEPER_GL_TEXTURE_HPP
  21. #include "gl/color-space.hpp"
  22. #include "gl/pixel-format.hpp"
  23. #include "gl/pixel-type.hpp"
  24. #include "gl/texture-filter.hpp"
  25. #include "gl/texture-wrapping.hpp"
  26. #include <array>
  27. #include <cstdint>
  28. #include <tuple>
  29. namespace gl {
  30. class framebuffer;
  31. class shader_input;
  32. /**
  33. * Abstract base class for 1D, 2D, 3D, and cube textures which can be uploaded to shaders via shader inputs.
  34. */
  35. class texture
  36. {
  37. public:
  38. /**
  39. * Constructs a texture.
  40. *
  41. * @param width Texture width, in pixels.
  42. * @param height Texture height, in pixels. For 2D or 3D textures.
  43. * @param depth Texture depth, in pixels. For 3D textures only.
  44. * @param type Pixel component data type.
  45. * @param format Pixel format.
  46. * @param color_space Color space of the pixel data.
  47. * @param data Pointer to pixel data.
  48. *
  49. * @warning If the sRGB color space is specified, pixel data will be stored internally as 8 bits per channel, and automatically converted to linear space before reading.
  50. */
  51. /// @{
  52. texture(std::uint16_t width, std::uint16_t height, std::uint16_t depth, gl::pixel_type type = gl::pixel_type::uint_8, gl::pixel_format format = gl::pixel_format::rgba, gl::color_space color_space = gl::color_space::linear, const void* data = nullptr);
  53. texture(std::uint16_t width, std::uint16_t height, gl::pixel_type type = gl::pixel_type::uint_8, gl::pixel_format format = gl::pixel_format::rgba, gl::color_space color_space = gl::color_space::linear, const void* data = nullptr);
  54. texture(std::uint16_t width, gl::pixel_type type = gl::pixel_type::uint_8, gl::pixel_format format = gl::pixel_format::rgba, gl::color_space color_space = gl::color_space::linear, const void* data = nullptr);
  55. /// @}
  56. /**
  57. * Destructs a texture.
  58. */
  59. virtual ~texture() = 0;
  60. /**
  61. * Sets the texture filtering modes.
  62. *
  63. * @param min_filter Texture minification filter mode.
  64. * @param mag_filter Texture magnification filter mode.
  65. */
  66. void set_filters(texture_min_filter min_filter, texture_mag_filter mag_filter);
  67. /**
  68. * Sets the maximum anisotropy.
  69. *
  70. * @param level Max anisotropy on `[0.0, 1.0]`, with `0.0` indicating normal filtering, and `1.0` indicating maximum anisotropic filtering.
  71. */
  72. void set_max_anisotropy(float anisotropy);
  73. /// Returns the dimensions of the texture, in pixels.
  74. const std::array<std::uint16_t, 3>& get_dimensions() const;
  75. /// Returns the width of the texture, in pixels.
  76. const std::uint16_t& get_width() const;
  77. /// Returns the height of the texture, in pixels.
  78. const std::uint16_t& get_height() const;
  79. /// Returns the depth of the texture, in pixels.
  80. const std::uint16_t& get_depth() const;
  81. /// Returns the pixel type enumeration.
  82. const pixel_type& get_pixel_type() const;
  83. /// Returns the pixel format enumeration.
  84. const pixel_format& get_pixel_format() const;
  85. /// Returns the color space enumeration.
  86. const color_space& get_color_space() const;
  87. /// Returns the wrapping modes of the texture.
  88. const std::array<texture_wrapping, 3>& get_wrapping() const;
  89. /// Returns the filtering modes of the texture.
  90. const std::tuple<texture_min_filter, texture_mag_filter>& get_filters() const;
  91. /// Returns the maximum anisotropy.
  92. float get_max_anisotropy() const;
  93. protected:
  94. /**
  95. * Sets the texture wrapping modes.
  96. *
  97. * @param wrap_s Wrapping mode for s-coordinates.
  98. * @param wrap_t Wrapping mode for t-coordinates.
  99. * @param wrap_r Wrapping mode for r-coordinates.
  100. */
  101. /// @{
  102. virtual void set_wrapping(gl::texture_wrapping wrap_s, gl::texture_wrapping wrap_t, gl::texture_wrapping wrap_r);
  103. virtual void set_wrapping(gl::texture_wrapping wrap_s, gl::texture_wrapping wrap_t);
  104. virtual void set_wrapping(gl::texture_wrapping wrap_s);
  105. /// @}
  106. /**
  107. * Resizes the texture.
  108. *
  109. * @param width Texture width, in pixels.
  110. * @param height Texture height, in pixels. For 2D or 3D textures.
  111. * @param depth Texture depth, in pixels. For 3D textures only.
  112. * @param type Pixel component data type.
  113. * @param format Pixel format.
  114. * @param color_space Color space of the pixel data.
  115. * @param data Pointer to pixel data.
  116. *
  117. * @warning If the sRGB color space is specified, pixel data will be stored internally as 8 bits per channel, and automatically converted to linear space before reading.
  118. */
  119. /// @{
  120. virtual void resize(std::uint16_t width, std::uint16_t height, std::uint16_t depth, gl::pixel_type type, gl::pixel_format format, gl::color_space color_space, const void* data);
  121. virtual void resize(std::uint16_t width, std::uint16_t height, gl::pixel_type type, gl::pixel_format format, gl::color_space color_space, const void* data);
  122. virtual void resize(std::uint16_t width, gl::pixel_type type, gl::pixel_format format, gl::color_space color_space, const void* data);
  123. /// @}
  124. private:
  125. friend class framebuffer;
  126. friend class shader_input;
  127. unsigned int gl_texture_target;
  128. unsigned int gl_texture_id;
  129. std::array<std::uint16_t, 3> dimensions;
  130. gl::pixel_type pixel_type;
  131. gl::pixel_format pixel_format;
  132. gl::color_space color_space;
  133. std::array<texture_wrapping, 3> wrapping;
  134. std::tuple<texture_min_filter, texture_mag_filter> filters;
  135. float max_anisotropy;
  136. };
  137. inline const std::array<std::uint16_t, 3>& texture::get_dimensions() const
  138. {
  139. return dimensions;
  140. }
  141. inline const std::uint16_t& texture::get_width() const
  142. {
  143. return dimensions[0];
  144. }
  145. inline const std::uint16_t& texture::get_height() const
  146. {
  147. return dimensions[1];
  148. }
  149. inline const std::uint16_t& texture::get_depth() const
  150. {
  151. return dimensions[2];
  152. }
  153. inline const pixel_type& texture::get_pixel_type() const
  154. {
  155. return pixel_type;
  156. }
  157. inline const pixel_format& texture::get_pixel_format() const
  158. {
  159. return pixel_format;
  160. }
  161. inline const color_space& texture::get_color_space() const
  162. {
  163. return color_space;
  164. }
  165. inline const std::array<texture_wrapping, 3>& texture::get_wrapping() const
  166. {
  167. return wrapping;
  168. }
  169. inline const std::tuple<texture_min_filter, texture_mag_filter>& texture::get_filters() const
  170. {
  171. return filters;
  172. }
  173. inline float texture::get_max_anisotropy() const
  174. {
  175. return max_anisotropy;
  176. }
  177. } // namespace gl
  178. #endif // ANTKEEPER_GL_TEXTURE_HPP