8#include <entt/locator/locator.hpp>
11#include <stb_image_write.h>
21#ifdef GALAXY_WIN_PLATFORM
23#pragma warning(disable : 26493)
34 glCreateTextures(GL_TEXTURE_2D, 1, &
m_id);
42 this->m_handle = t.m_handle;
43 this->m_width = t.m_width;
44 this->m_height = t.m_height;
57 this->m_handle = t.m_handle;
58 this->m_width = t.m_width;
59 this->m_height = t.m_height;
75 auto& fs = entt::locator<VirtualFileSystem>::value();
77 auto data = fs.read_binary(file);
87 stbi_set_flip_vertically_on_load(
true);
88 unsigned char* data = stbi_load_from_memory(buffer.data(),
static_cast<int>(buffer.size_bytes()), &
m_width, &
m_height,
nullptr, STBI_rgb_alpha);
100 glMakeTextureHandleResidentARB(
m_handle);
108 stbi_image_free(data);
121 auto& fs = entt::locator<VirtualFileSystem>::value();
123 auto path = std::filesystem::path(file);
124 if (!path.is_absolute())
129 if (!path.has_extension())
131 path.replace_extension(
".png");
134 if (!std::filesystem::exists(path.parent_path()))
136 std::filesystem::create_directories(path.parent_path());
139 std::vector<unsigned int> pixels(
static_cast<unsigned int>(
m_width) *
static_cast<unsigned int>(
m_height) * 4u, 0);
141 glGetTextureImage(
m_id, 0, GL_RGBA, GL_UNSIGNED_BYTE,
static_cast<GLsizei
>(pixels.size()), pixels.data());
143 stbi_flip_vertically_on_write(
true);
147 unsigned char* png = stbi_write_png_to_mem((
const unsigned char*)pixels.data(),
m_width * 4,
m_width,
m_height, 4, &len);
149 if (!fs.write_raw(png, len, path.string()))
159 glBindTexture(GL_TEXTURE_2D,
m_id);
164 glBindTexture(GL_TEXTURE_2D, 0);
171 glMakeTextureHandleNonResidentARB(
m_handle);
177 glDeleteTextures(1, &
m_id);
185 glCreateTextures(GL_TEXTURE_2D, 1, &
m_id);
190 return static_cast<float>(
m_width);
195 return static_cast<float>(
m_height);
212 glGenerateTextureMipmap(
m_id);
217 glTextureParameteri(
m_id, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
218 glTextureParameteri(
m_id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
221 glTextureParameteri(
m_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
222 glTextureParameteri(
m_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 glTextureParameteri(
m_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
226 glTextureParameteri(
m_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
234 glTextureParameteri(
m_id, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
235 glTextureParameteri(
m_id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
239 glTextureParameteri(
m_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
240 glTextureParameteri(
m_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
275 glTextureParameterf(
m_id, GL_TEXTURE_MAX_ANISOTROPY,
static_cast<float>(ansio));
279#ifdef GALAXY_WIN_PLATFORM
#define GALAXY_LOG(level, msg,...)
Bindless OpenGL 2D Texture.
void unbind() const noexcept
Deactivate sampler.
void set_anisotropy() const noexcept
Set ansiotropic filtering level.
Texture & operator=(Texture &&) noexcept
Move assignment operator.
bool load(const std::string &file)
Load a texture from vfs.
bool load_mem(std::span< std::uint8_t > buffer)
Loads texture from memory.
int m_width
Texture width.
void set_filter() const noexcept
Sets filter when texture is (up/down)scaled in OpenGL.
std::uint64_t m_handle
Bindless handle.
unsigned int m_id
OpenGL id.
void save(std::string_view file)
Saves texture to file on disk.
void set_mode() const noexcept
Set texture mode.
void bind() const noexcept
Bind to sampler.
int m_height
Texture height.
void destroy()
Destroy texture.
float height() const noexcept
Get texture height.
unsigned int id() const noexcept
Get OpenGL texture id.
void recreate()
Deletes texture data and configuration in OpenGL.
std::uint64_t handle() const noexcept
Get OpenGL bindless handle.
float width() const noexcept
Get texture width.
@ NEAREST
Nearest-neighbour.
@ CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE.
static auto root_dir() noexcept -> std::filesystem::path
Current root directory of application, unless it has been changed.
static auto ansiotrophy() noexcept -> int
Ansiotropic filtering level.
static auto mipmap() noexcept -> bool
Mipmapping.
static auto texture_filter() noexcept -> GLTextureFilter
Texture filtering type.