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

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