hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/platform/opengl/opengl_texture.cpp
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-08-12 19:17:50 +0200
committerhachem <im@hachem.wtf>2025-08-12 19:17:50 +0200
commit3b02022fd79ad0e130b8c49e7707d221bfc97bb5 (patch)
tree0da9ee9559969a7f40d92611a3b1c234e38ae36f /src/platform/opengl/opengl_texture.cpp
parent7e06a916e6a385c37075abcb83d14b8c1fef7bc0 (diff)
feat: texture and pixel buffers
Diffstat (limited to 'src/platform/opengl/opengl_texture.cpp')
-rw-r--r--src/platform/opengl/opengl_texture.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/platform/opengl/opengl_texture.cpp b/src/platform/opengl/opengl_texture.cpp
new file mode 100644
index 0000000..87e9345
--- /dev/null
+++ b/src/platform/opengl/opengl_texture.cpp
@@ -0,0 +1,65 @@
+#include "opengl_texture.h"
+#include <iostream>
+
+namespace Donut
+{
+ OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height)
+ : m_width(width), m_height(height)
+ {
+ m_internal_format = GL_RGBA8;
+ m_data_format = GL_RGBA;
+
+ glCreateTextures(GL_TEXTURE_2D, 1, &m_renderer_id);
+ glTextureStorage2D(m_renderer_id, 1, m_internal_format, m_width, m_height);
+
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ }
+
+ OpenGLTexture2D::OpenGLTexture2D(const std::string& path)
+ : m_path(path)
+ {
+ m_width = 1;
+ m_height = 1;
+ m_internal_format = GL_RGBA8;
+ m_data_format = GL_RGBA;
+
+ glCreateTextures(GL_TEXTURE_2D, 1, &m_renderer_id);
+ glTextureStorage2D(m_renderer_id, 1, m_internal_format, m_width, m_height);
+
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTextureParameteri(m_renderer_id, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+ uint32_t white_pixel = 0xFFFFFFFF;
+ glTextureSubImage2D(m_renderer_id, 0, 0, 0, m_width, m_height, m_data_format, GL_UNSIGNED_BYTE, &white_pixel);
+
+ std::cout << "Created default texture (stb_image not available for loading: " << path << ")" << std::endl;
+ }
+
+ OpenGLTexture2D::~OpenGLTexture2D()
+ {
+ glDeleteTextures(1, &m_renderer_id);
+ }
+
+ void OpenGLTexture2D::set_data(void* data, uint32_t size)
+ {
+ uint32_t bpp = m_data_format == GL_RGBA ? 4 : 3;
+ if (size != m_width * m_height * bpp)
+ {
+ std::cout << "Data must be entire texture!" << std::endl;
+ return;
+ }
+ glTextureSubImage2D(m_renderer_id, 0, 0, 0, m_width, m_height, m_data_format, GL_UNSIGNED_BYTE, data);
+ }
+
+ void OpenGLTexture2D::bind(uint32_t slot) const
+ {
+ glBindTextureUnit(slot, m_renderer_id);
+ }
+};