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
RenderTexture.cpp
Go to the documentation of this file.
1
7
8#include <glad/glad.h>
9#include <glm/gtc/matrix_transform.hpp>
10#include <stb_image_write.h>
11
12#include "galaxy/core/ServiceLocator.hpp"
15
16#include "RenderTexture.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 : m_width {0}
29 , m_height {0}
30 {
31 }
32
34 {
35 this->m_width = rt.m_width;
36 this->m_height = rt.m_height;
37 this->m_framebuffer = std::move(rt.m_framebuffer);
38 }
39
41 {
42 if (this != &rt)
43 {
44 this->m_width = rt.m_width;
45 this->m_height = rt.m_height;
46 this->m_framebuffer = std::move(rt.m_framebuffer);
47 }
48
49 return *this;
50 }
51
55
56 void RenderTexture::create(const int width, const int height)
57 {
58 m_width = std::max(1, width);
59 m_height = std::max(1, height);
60
61 // m_projection = glm::ortho(0.0f, static_cast<float>(width), static_cast<float>(height), 0.0f, -1.0f, 1.0f);
62
66 m_proj = glm::ortho(0.0f, static_cast<float>(m_width), static_cast<float>(m_height), 0.0f, -1.0f, 1.0f);
67 }
68
69 void RenderTexture::recreate(const int width, const int height)
70 {
72
73 if (width > 0 || height > 0)
74 {
75 m_width = width;
77 }
78
80 }
81
82 void RenderTexture::save(std::string_view file)
83 {
84 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
85
86 auto path = std::filesystem::path(file);
87 if (!path.is_absolute())
88 {
89 path = GALAXY_ROOT_DIR / path;
90 }
91
92 if (!path.has_extension())
93 {
94 path.replace_extension(".png");
95 }
96
97 if (!std::filesystem::exists(path.parent_path()))
98 {
99 std::filesystem::create_directories(path.parent_path());
100 }
101
102 meta::vector<unsigned int> pixels(m_width * m_height * 4, 0);
103
104 glGetTextureImage(m_framebuffer.texture(), 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLsizei>(pixels.size()), pixels.data());
105
106 stbi_flip_vertically_on_write(true);
107
108 int len = 0;
109 unsigned char* png = stbi_write_png_to_mem((const unsigned char*)pixels.data(), m_width * 4, m_width, m_height, 4, &len);
110
111 if (!fs.write_raw(png, len, path.string()))
112 {
113 GALAXY_LOG(GALAXY_ERROR, "Failed to write '{0}' to disk.", path.string());
114 }
115
116 mi_free(png);
117 }
118
119 void RenderTexture::bind(bool clear)
120 {
122 }
123
125 {
127 }
128
130 {
132 }
133
135 {
136 return m_width;
137 }
138
140 {
141 return m_height;
142 }
143
144 unsigned int RenderTexture::texture() const
145 {
146 return m_framebuffer.texture();
147 }
148
149 const glm::mat4& RenderTexture::get_proj()
150 {
151 return m_proj;
152 }
153
155 {
156 return m_framebuffer;
157 }
158 } // namespace graphics
159} // namespace galaxy
160
161#ifdef GALAXY_WIN_PLATFORM
162#pragma warning(pop)
163#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
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.
unsigned int texture() const
Gets framebuffer texture.
RenderTexture & operator=(RenderTexture &&)
Move assignment operator.
void recreate(const int width=-1, const int height=-1)
Destroy and re-create framebuffer.
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.
Animated.cpp galaxy.
Definition Animated.cpp:16