aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/OpenGL/OpenGLTexture.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Platform/OpenGL/OpenGLTexture.cpp')
-rw-r--r--src/Platform/OpenGL/OpenGLTexture.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/Platform/OpenGL/OpenGLTexture.cpp b/src/Platform/OpenGL/OpenGLTexture.cpp
new file mode 100644
index 0000000..02252d1
--- /dev/null
+++ b/src/Platform/OpenGL/OpenGLTexture.cpp
@@ -0,0 +1,66 @@
+#include "OpenGLTexture.h"
+#include <iostream>
+
+namespace Donut
+{
+ OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height)
+ : m_Width(width), m_Height(height)
+ {
+ m_InternalFormat = GL_RGBA8;
+ m_DataFormat = GL_RGBA;
+
+ glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
+ glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height);
+
+ glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ }
+
+ // TODO: Texture Loading
+ OpenGLTexture2D::OpenGLTexture2D(const std::string& path)
+ : m_Path(path)
+ {
+ m_Width = 1;
+ m_Height = 1;
+ m_InternalFormat = GL_RGBA8;
+ m_DataFormat = GL_RGBA;
+
+ glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID);
+ glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height);
+
+ glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+ uint32_t whitePixel = 0xFFFFFFFF;
+ glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, &whitePixel);
+
+ std::cout << "Created default texture (stb_image not available for loading: " << path << ")" << std::endl;
+ }
+
+ OpenGLTexture2D::~OpenGLTexture2D()
+ {
+ glDeleteTextures(1, &m_RendererID);
+ }
+
+ void OpenGLTexture2D::SetData(void* data, uint32_t size)
+ {
+ uint32_t bpp = m_DataFormat == GL_RGBA ? 4 : 3;
+ if (size != m_Width * m_Height * bpp)
+ {
+ std::cout << "Data must be entire texture!" << std::endl;
+ return;
+ }
+ glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, data);
+ }
+
+ void OpenGLTexture2D::Bind(uint32_t slot) const
+ {
+ glBindTextureUnit(slot, m_RendererID);
+ }
+}; \ No newline at end of file