9#include <glm/gtc/matrix_transform.hpp>
10#include <stb_image_write.h>
12#include "galaxy/core/ServiceLocator.hpp"
18#ifdef GALAXY_WIN_PLATFORM
20#pragma warning(disable : 26493)
66 m_proj = glm::ortho(0.0f,
static_cast<float>(
m_width),
static_cast<float>(
m_height), 0.0f, -1.0f, 1.0f);
84 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
86 auto path = std::filesystem::path(file);
87 if (!path.is_absolute())
89 path = GALAXY_ROOT_DIR / path;
92 if (!path.has_extension())
94 path.replace_extension(
".png");
97 if (!std::filesystem::exists(path.parent_path()))
99 std::filesystem::create_directories(path.parent_path());
104 glGetTextureImage(
m_framebuffer.
texture(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
static_cast<GLsizei
>(pixels.size()), pixels.data());
106 stbi_flip_vertically_on_write(
true);
109 unsigned char* png = stbi_write_png_to_mem((
const unsigned char*)pixels.data(),
m_width * 4,
m_width,
m_height, 4, &len);
111 if (!fs.write_raw(png, len, path.string()))
161#ifdef GALAXY_WIN_PLATFORM
#define GALAXY_LOG(level, msg,...)
OpenGL framebuffer to render to.
void add_colour_attachment(const int width, const int height)
Add a colour attachment to the framebuffer.
void add_depth_stencil_renderbuffer()
Adds a depth and stencil supported renderbuffer to the framebuffer.
void reset()
Completely delete objects.
unsigned int texture() const
Get colour attachment texture id.
void create()
Construct framebuffer from attachments and renderbuffers.
void end()
Unbind framebuffer.
void clear()
Clear framebuffer attachments.
void begin(const bool clear=true)
Bind framebuffer for rendering to.
Draw to an opengl texture instead of the screen.
int height() const
Get texture height.
Framebuffer m_framebuffer
OpenGL framebuffer abstraction.
void unbind()
Deactivate context.
const glm::mat4 & get_proj()
Get render texture projection.
void create(const int width, const int height)
Create framebuffer and texture.
void save(std::string_view file)
Saves texture to file on disk.
RenderTexture()
Constructor.
unsigned int texture() const
Gets framebuffer texture.
RenderTexture & operator=(RenderTexture &&)
Move assignment operator.
int m_height
Cached height.
void recreate(const int width=-1, const int height=-1)
Destroy and re-create framebuffer.
glm::mat4 m_proj
Projection.
void clear()
Clear framebuffer attachments.
Framebuffer & fbo()
Get framebuffer.
virtual ~RenderTexture()
Destructor.
void bind(bool clear=true)
Activate context.
int width() const
Get texture width.