|
1 | | -// Copyright 2015-2024 the openage authors. See copying.md for legal info. |
| 1 | +// Copyright 2015-2025 the openage authors. See copying.md for legal info. |
2 | 2 |
|
3 | 3 | #include "texture.h" |
4 | 4 |
|
5 | 5 | #include <epoxy/gl.h> |
6 | 6 |
|
| 7 | +#include <algorithm> |
7 | 8 | #include <tuple> |
8 | 9 |
|
9 | 10 | #include "../../datastructure/constexpr_map.h" |
@@ -86,14 +87,73 @@ GlTexture2d::GlTexture2d(const std::shared_ptr<GlContext> &context, |
86 | 87 | std::get<2>(fmt_in_out), |
87 | 88 | nullptr); |
88 | 89 |
|
89 | | - // TODO these are outdated, use sampler settings |
| 90 | + // TODO: these are outdated, use sampler settings |
90 | 91 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
91 | 92 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
92 | 93 |
|
93 | 94 | log::log(MSG(dbg) << "Created OpenGL texture from info parameters (size: " |
94 | 95 | << size.first << "x" << size.second << ")"); |
95 | 96 | } |
96 | 97 |
|
| 98 | +void GlTexture2d::resize(size_t width, size_t height) { |
| 99 | + auto prev_size = this->info.get_size(); |
| 100 | + if (width == static_cast<size_t>(prev_size.first) |
| 101 | + and height == static_cast<size_t>(prev_size.second)) { |
| 102 | + // size is the same, no need to resize |
| 103 | + log::log(MSG(dbg) << "Texture resize called, but size is unchanged (size: " |
| 104 | + << prev_size.first << "x" << prev_size.second << ")"); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // only allow resizing for internal textures that are not created from |
| 109 | + // image files |
| 110 | + // TODO: maybe allow this for all textures? |
| 111 | + if (this->info.get_image_path().has_value()) { |
| 112 | + throw Error(MSG(err) << "Cannot resize a texture that was created from an image file."); |
| 113 | + } |
| 114 | + if (this->info.get_subtex_count() != 0) { |
| 115 | + throw Error(MSG(err) << "Cannot resize a texture that has subtextures."); |
| 116 | + } |
| 117 | + |
| 118 | + // create new info object |
| 119 | + this->info = resources::Texture2dInfo(width, |
| 120 | + height, |
| 121 | + this->info.get_format(), |
| 122 | + this->info.get_image_path(), |
| 123 | + this->info.get_row_alignment()); |
| 124 | + |
| 125 | + glBindTexture(GL_TEXTURE_2D, *this->handle); |
| 126 | + |
| 127 | + auto fmt_in_out = GL_PIXEL_FORMAT.get(this->info.get_format()); |
| 128 | + auto size = this->info.get_size(); |
| 129 | + |
| 130 | + // redefine the texture with the new size |
| 131 | + glTexImage2D( |
| 132 | + GL_TEXTURE_2D, |
| 133 | + 0, |
| 134 | + std::get<0>(fmt_in_out), |
| 135 | + size.first, |
| 136 | + size.second, |
| 137 | + 0, |
| 138 | + std::get<1>(fmt_in_out), |
| 139 | + std::get<2>(fmt_in_out), |
| 140 | + nullptr); |
| 141 | + |
| 142 | + // copy the old texture data into the new texture |
| 143 | + glCopyTexSubImage2D( |
| 144 | + GL_TEXTURE_2D, |
| 145 | + 0, |
| 146 | + 0, |
| 147 | + 0, |
| 148 | + 0, |
| 149 | + 0, |
| 150 | + std::min(size.first, prev_size.first), // avoid buffer overread with std::min |
| 151 | + std::min(size.second, prev_size.second)); |
| 152 | + |
| 153 | + log::log(MSG(dbg) << "Resized OpenGL texture (size: " |
| 154 | + << width << "x" << height << ")"); |
| 155 | +} |
| 156 | + |
97 | 157 | resources::Texture2dData GlTexture2d::into_data() { |
98 | 158 | auto fmt_in_out = GL_PIXEL_FORMAT.get(this->info.get_format()); |
99 | 159 | std::vector<uint8_t> data(this->info.get_data_size()); |
|
0 commit comments