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

110 lines
3.0 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. #include <engine/gl/texture-cube.hpp>
  20. #include <cmath>
  21. namespace gl {
  22. cube_map_layout texture_cube::infer_cube_map_layout(std::uint16_t w, std::uint16_t h) noexcept
  23. {
  24. if (h == w * 6)
  25. {
  26. return cube_map_layout::column;
  27. }
  28. else if (w == h * 6)
  29. {
  30. return cube_map_layout::row;
  31. }
  32. else if (w == (h / 4) * 3)
  33. {
  34. return cube_map_layout::vertical_cross;
  35. }
  36. else if (h == (w / 4) * 3)
  37. {
  38. return cube_map_layout::horizontal_cross;
  39. }
  40. else if (w == h * 2)
  41. {
  42. return cube_map_layout::equirectangular;
  43. }
  44. else if (w == h)
  45. {
  46. return cube_map_layout::spherical;
  47. }
  48. return {};
  49. }
  50. std::uint16_t texture_cube::infer_cube_map_face_size(cube_map_layout layout, std::uint16_t w, std::uint16_t h) noexcept
  51. {
  52. switch (layout)
  53. {
  54. case cube_map_layout::column:
  55. case cube_map_layout::spherical:
  56. return w;
  57. case cube_map_layout::row:
  58. return h;
  59. case cube_map_layout::vertical_cross:
  60. return h / 4;
  61. case cube_map_layout::horizontal_cross:
  62. case cube_map_layout::equirectangular:
  63. return w / 4;
  64. default:
  65. return 0;
  66. }
  67. }
  68. texture_cube::texture_cube(std::uint16_t width, std::uint16_t height, gl::pixel_type type, gl::pixel_format format, gl::transfer_function transfer_function, const std::byte* data):
  69. texture(width, height, true, type, format, transfer_function, data)
  70. {
  71. resized();
  72. }
  73. void texture_cube::resize(std::uint16_t width, std::uint16_t height, gl::pixel_type type, gl::pixel_format format, gl::transfer_function transfer_function, const std::byte* data)
  74. {
  75. texture::resize(width, height, type, format, transfer_function, data);
  76. resized();
  77. }
  78. void texture_cube::resize(std::uint16_t width, std::uint16_t height, const std::byte* data)
  79. {
  80. texture::resize(width, height, get_pixel_type(), get_pixel_format(), get_transfer_function(), data);
  81. resized();
  82. }
  83. void texture_cube::set_wrapping(gl::texture_wrapping wrap_s, texture_wrapping wrap_t, texture_wrapping wrap_r)
  84. {
  85. texture::set_wrapping(wrap_s, wrap_t, wrap_r);
  86. }
  87. void texture_cube::resized()
  88. {
  89. const auto w = get_width();
  90. const auto h = get_height();
  91. const auto layout = infer_cube_map_layout(w, h);
  92. m_face_size = infer_cube_map_face_size(layout, w, h);
  93. m_mip_count = 1 + static_cast<std::uint16_t>(std::log2(m_face_size));
  94. }
  95. } // namespace gl