galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Texture2D.cpp
Go to the documentation of this file.
1
7
8#include <stb_image.h>
9#include <stb_image_write.h>
10
12#include "galaxy/core/ServiceLocator.hpp"
14#include "galaxy/utils/Globals.hpp"
15
16#include "Texture2D.hpp"
17
18#ifdef GALAXY_WIN_PLATFORM
19#pragma warning(push)
20#pragma warning(disable : 26493)
21#endif
22
23namespace galaxy
24{
25 namespace graphics
26 {
28 : Texture {}
29 {
30 glCreateTextures(GL_TEXTURE_2D, 1, &m_id);
31 }
32
34 : Texture {std::move(t)}
35 {
36 }
37
39 {
40 if (this != &t)
41 {
42 Texture::operator=(std::move(t));
43 }
44
45 return *this;
46 }
47
51
52 bool Texture2D::load(const std::string& file)
53 {
54 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
55
56 auto data = fs.read_binary(file);
57 if (!data.empty())
58 {
59 return load_mem(data);
60 }
61 else
62 {
63 GALAXY_LOG(GALAXY_ERROR, "Failed to read '{0}' from vfs.", file);
64 }
65
66 return false;
67 }
68
69 bool Texture2D::load_mem(std::span<std::uint8_t> buffer)
70 {
71 auto result = false;
72
73 auto& config = core::ServiceLocator<core::Config>::ref();
74
75 stbi_set_flip_vertically_on_load(true);
76 unsigned char* data = stbi_load_from_memory(buffer.data(), static_cast<int>(buffer.size_bytes()), &m_width, &m_height, nullptr, STBI_rgb_alpha);
77
78 if (data)
79 {
80 glTextureStorage2D(m_id, 1, GL_RGBA8, m_width, m_height);
81 glTextureSubImage2D(m_id, 0, 0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, data);
82 glGenerateTextureMipmap(m_id);
83
84 if (config.get<int>("trilinear_filtering", "graphics"))
85 {
87 }
88 else
89 {
91 }
92
94 anisotropy(config.get<int>("ansiotrophic_filtering", "graphics"));
95
96 result = true;
97 }
98 else
99 {
100 GALAXY_LOG(GALAXY_ERROR, "Failed to load texture from memory because '{0}'.", stbi_failure_reason());
101 }
102
103 stbi_image_free(data);
104 return result;
105 }
106
107 void Texture2D::save(std::string_view file)
108 {
109 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
110
111 auto path = std::filesystem::path(file);
112 if (!path.is_absolute())
113 {
114 path = GALAXY_ROOT_DIR / path;
115 }
116
117 if (!path.has_extension())
118 {
119 path.replace_extension(".png");
120 }
121
122 if (!std::filesystem::exists(path.parent_path()))
123 {
124 std::filesystem::create_directories(path.parent_path());
125 }
126
127 meta::vector<unsigned int> pixels(static_cast<unsigned int>(m_width) * static_cast<unsigned int>(m_height) * 4u, 0);
128
129 glGetTextureImage(m_id, 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLsizei>(pixels.size()), pixels.data());
130
131 stbi_flip_vertically_on_write(true);
132
133 int len = 0;
134 unsigned char* png = stbi_write_png_to_mem((const unsigned char*)pixels.data(), m_width * 4, m_width, m_height, 4, &len);
135
136 if (!fs.write_raw(png, len, path.string()))
137 {
138 GALAXY_LOG(GALAXY_ERROR, "Failed to write '{0}' to disk.", path.string());
139 }
140
141 mi_free(png);
142 }
143
145 {
146 if (m_id != 0)
147 {
148 glDeleteTextures(1, &m_id);
149 m_id = 0;
150 }
151
152 glCreateTextures(GL_TEXTURE_2D, 1, &m_id);
153 }
154
156 {
157 glBindTexture(GL_TEXTURE_2D, m_id);
158 }
159
161 {
162 glBindTexture(GL_TEXTURE_2D, 0);
163 }
164 } // namespace graphics
165} // namespace galaxy
166
167#ifdef GALAXY_WIN_PLATFORM
168#pragma warning(pop)
169#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
Standard opengl texture.
Definition Texture2D.hpp:21
bool load(const std::string &file)
Load a texture from vfs.
Definition Texture2D.cpp:52
Texture2D & operator=(Texture2D &&)
Move assignment operator.
Definition Texture2D.cpp:38
void unbind() override
Deactivate context.
virtual ~Texture2D()
Destructor.
Definition Texture2D.cpp:48
bool load_mem(std::span< std::uint8_t > buffer)
Loads texture from memory.
Definition Texture2D.cpp:69
void save(std::string_view file)
Saves texture to file on disk.
void bind() override
Activate context.
void recreate()
Deletes texture data and configuration in OpenGL and creates new texture handle.
OpenGL 2D Texture.
Definition Texture.hpp:26
int m_height
Cached height.
Definition Texture.hpp:171
int anisotropy() const
Get current anisotropy level.
Definition Texture.cpp:152
unsigned int m_id
OpenGL id.
Definition Texture.hpp:161
Texture & operator=(Texture &&)
Move assignment operator.
Definition Texture.cpp:49
int m_width
Cached width.
Definition Texture.hpp:166
TextureMode mode() const
Get texture mode.
Definition Texture.cpp:97
TextureFilter filter() const
Get texture filter.
Definition Texture.cpp:120
@ NEAREST
Nearest-neighbour.
@ CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE.
Animated.cpp galaxy.
Definition Animated.cpp:16
STL namespace.