Antkeeper  0.0.1
texture-cube.cpp
Go to the documentation of this file.
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 
21 #include <cmath>
22 
23 namespace gl {
24 
25 cube_map_layout texture_cube::infer_cube_map_layout(std::uint16_t w, std::uint16_t h) noexcept
26 {
27  if (h == w * 6)
28  {
30  }
31  else if (w == h * 6)
32  {
33  return cube_map_layout::row;
34  }
35  else if (w == (h / 4) * 3)
36  {
38  }
39  else if (h == (w / 4) * 3)
40  {
42  }
43  else if (w == h * 2)
44  {
46  }
47  else if (w == h)
48  {
50  }
51 
52  return {};
53 }
54 
55 std::uint16_t texture_cube::infer_cube_map_face_size(cube_map_layout layout, std::uint16_t w, std::uint16_t h) noexcept
56 {
57  switch (layout)
58  {
61  return w;
62 
64  return h;
65 
67  return h / 4;
68 
71  return w / 4;
72 
73  default:
74  return 0;
75  }
76 }
77 
78 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):
79  texture(width, height, true, type, format, transfer_function, data)
80 {
81  resized();
82 }
83 
84 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)
85 {
86  texture::resize(width, height, type, format, transfer_function, data);
87  resized();
88 }
89 
90 void texture_cube::resize(std::uint16_t width, std::uint16_t height, const std::byte* data)
91 {
93  resized();
94 }
95 
97 {
98  texture::set_wrapping(wrap_s, wrap_t, wrap_r);
99 }
100 
101 void texture_cube::resized()
102 {
103  const auto w = get_width();
104  const auto h = get_height();
105  const auto layout = infer_cube_map_layout(w, h);
106  m_face_size = infer_cube_map_face_size(layout, w, h);
107  m_mip_count = 1 + static_cast<std::uint16_t>(std::log2(m_face_size));
108 }
109 
110 } // namespace gl
texture_cube(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::transfer_function transfer_function=gl::transfer_function::linear, const std::byte *data=nullptr)
Constructs a cube texture.
virtual void set_wrapping(gl::texture_wrapping wrap_s, texture_wrapping wrap_t, texture_wrapping wrap_r)
Sets the texture wrapping modes.
static cube_map_layout infer_cube_map_layout(std::uint16_t w, std::uint16_t h) noexcept
Infers the layout of a cube map from its aspect ratio.
void 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) override
Resizes the texture.
static std::uint16_t infer_cube_map_face_size(cube_map_layout layout, std::uint16_t w, std::uint16_t h) noexcept
Infers the edge length of a cube map face from its layout and resolution.
Abstract base class for 1D, 2D, 3D, and cube textures which can be uploaded to shaders via shader inp...
Definition: texture.hpp:47
virtual void resize(std::uint16_t width, std::uint16_t height, std::uint16_t depth, gl::pixel_type type, gl::pixel_format format, gl::transfer_function transfer_function, const std::byte *data)
Resizes the texture.
Definition: texture.cpp:267
std::uint16_t get_height() const noexcept
Returns the height of the texture, in pixels.
Definition: texture.hpp:131
virtual void set_wrapping(gl::texture_wrapping wrap_s, gl::texture_wrapping wrap_t, gl::texture_wrapping wrap_r)
Sets the texture wrapping modes.
Definition: texture.cpp:230
transfer_function get_transfer_function() const noexcept
Returns the transfer function.
Definition: texture.hpp:155
std::uint16_t m_mip_count
Definition: texture.hpp:270
std::uint16_t get_width() const noexcept
Returns the width of the texture, in pixels.
Definition: texture.hpp:125
pixel_format get_pixel_format() const noexcept
Returns the pixel format enumeration.
Definition: texture.hpp:149
pixel_type get_pixel_type() const noexcept
Returns the pixel type enumeration.
Definition: texture.hpp:143
Graphics library interface.
transfer_function
Texture sampling transfer function.
pixel_format
pixel_type
Definition: pixel-type.hpp:28
cube_map_layout
Cube map layout types.
@ column
Faces are stored consecutively in a vertical column.
@ spherical
Faces are stored in a spherical projection.
@ horizontal_cross
Faces are stored in a horizontal cross.
@ equirectangular
Faces are stored in an equirectangular projection.
@ vertical_cross
Faces are stored in a vertical cross.
@ row
Faces are stored consecutively in a horizontal row.
Text and typography.
Definition: bitmap-font.cpp:24