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

160 lines
4.5 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_2D_HPP
  20. #define ANTKEEPER_GL_TEXTURE_2D_HPP
  21. #include <array>
  22. #include <tuple>
  23. #include "gl/color-space.hpp"
  24. #include "gl/pixel-format.hpp"
  25. #include "gl/pixel-type.hpp"
  26. namespace gl {
  27. class framebuffer;
  28. class shader_input;
  29. enum class texture_mag_filter;
  30. enum class texture_min_filter;
  31. enum class texture_wrapping;
  32. /**
  33. * A 2D texture which can be uploaded to shaders via shader inputs.
  34. */
  35. class texture_2d
  36. {
  37. public:
  38. /**
  39. * Creates a 2D texture.
  40. *
  41. * @param color_space Specifies the color space of the pixel data.
  42. *
  43. * @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.
  44. */
  45. texture_2d(int width, int 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);
  46. /**
  47. * Destroys a 2D texture.
  48. */
  49. ~texture_2d();
  50. /**
  51. * Resizes the texture.
  52. *
  53. * @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.
  54. */
  55. void resize(int width, int height, gl::pixel_type type, gl::pixel_format format, gl::color_space color_space, const void* data);
  56. /**
  57. * Sets the texture wrapping modes.
  58. *
  59. * @param wrap_s Wrapping mode for s-coordinates.
  60. * @param wrap_t Wrapping mode for t-coordinates.
  61. */
  62. void set_wrapping(gl::texture_wrapping wrap_s, texture_wrapping wrap_t);
  63. /**
  64. * Sets the texture filtering modes.
  65. *
  66. * @param min_filter Texture minification filter.
  67. * @param mag_filter Texture magnification filter.
  68. */
  69. void set_filters(texture_min_filter min_filter, texture_mag_filter mag_filter);
  70. /**
  71. * Sets the maximum anisotropy.
  72. *
  73. * @param level Max anisotropy on `[0.0, 1.0]`, with `0.0` indicating normal filtering, and `1.0` indicating maximum anisotropic filtering.
  74. */
  75. void set_max_anisotropy(float anisotropy);
  76. /// Returns the dimensions of the texture, in pixels.
  77. const std::array<int, 2>& get_dimensions() const;
  78. /// Returns the pixel type enumeration.
  79. const pixel_type& get_pixel_type() const;
  80. /// Returns the pixel format enumeration.
  81. const pixel_format& get_pixel_format() const;
  82. /// Returns the color space enumeration.
  83. const color_space& get_color_space() const;
  84. /// Returns the wrapping modes of the texture.
  85. const std::tuple<texture_wrapping, texture_wrapping> get_wrapping() const;
  86. /// Returns the filtering modes of the texture.
  87. const std::tuple<texture_min_filter, texture_mag_filter> get_filters() const;
  88. /// Returns the maximum anisotropy.
  89. float get_max_anisotropy() const;
  90. private:
  91. friend class framebuffer;
  92. friend class shader_input;
  93. unsigned int gl_texture_id;
  94. std::array<int, 2> dimensions;
  95. gl::pixel_type pixel_type;
  96. gl::pixel_format pixel_format;
  97. gl::color_space color_space;
  98. std::tuple<texture_wrapping, texture_wrapping> wrapping;
  99. std::tuple<texture_min_filter, texture_mag_filter> filters;
  100. float max_anisotropy;
  101. };
  102. inline const std::array<int, 2>& texture_2d::get_dimensions() const
  103. {
  104. return dimensions;
  105. }
  106. inline const pixel_type& texture_2d::get_pixel_type() const
  107. {
  108. return pixel_type;
  109. }
  110. inline const pixel_format& texture_2d::get_pixel_format() const
  111. {
  112. return pixel_format;
  113. }
  114. inline const color_space& texture_2d::get_color_space() const
  115. {
  116. return color_space;
  117. }
  118. inline const std::tuple<texture_wrapping, texture_wrapping> texture_2d::get_wrapping() const
  119. {
  120. return wrapping;
  121. }
  122. inline const std::tuple<texture_min_filter, texture_mag_filter> texture_2d::get_filters() const
  123. {
  124. return filters;
  125. }
  126. inline float texture_2d::get_max_anisotropy() const
  127. {
  128. return max_anisotropy;
  129. }
  130. } // namespace gl
  131. #endif // ANTKEEPER_GL_TEXTURE_2D_HPP