diff options
| author | hachem <im@hachem.wtf> | 2025-08-12 19:17:50 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-12 19:17:50 +0200 |
| commit | 1ad6bc34323f5b30d463ebe47a58d47ff9d4f328 (patch) | |
| tree | 535774e6db8e03fdb204c27734ca9fa43ab30a61 /src/Platform | |
| parent | 8cf6858a6cd958f31521c4ce680cf2011bfb7cc3 (diff) | |
[add]: Texture and Pixel buffers
Diffstat (limited to 'src/Platform')
| -rw-r--r-- | src/Platform/OpenGL/OpenGLRendererAPI.cpp | 17 | ||||
| -rw-r--r-- | src/Platform/OpenGL/OpenGLRendererAPI.h | 1 | ||||
| -rw-r--r-- | src/Platform/OpenGL/OpenGLTexture.cpp | 66 | ||||
| -rw-r--r-- | src/Platform/OpenGL/OpenGLTexture.h | 33 |
4 files changed, 117 insertions, 0 deletions
diff --git a/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/src/Platform/OpenGL/OpenGLRendererAPI.cpp index 3660555..5ee7d8d 100644 --- a/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -8,6 +8,11 @@ namespace Donut glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CCW); } void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) @@ -30,6 +35,18 @@ namespace Donut glEnable(GL_DEPTH_TEST); } + void OpenGLRendererAPI::SetFaceCulling(bool enabled) + { + if (enabled) + { + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CCW); + } + else + glDisable(GL_CULL_FACE); + } + void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr<VertexArray>& vertexArray, uint32_t indexCount) { uint32_t count = indexCount ? indexCount : vertexArray->GetIndexBuffer()->GetCount(); diff --git a/src/Platform/OpenGL/OpenGLRendererAPI.h b/src/Platform/OpenGL/OpenGLRendererAPI.h index f37cc79..35bd9c3 100644 --- a/src/Platform/OpenGL/OpenGLRendererAPI.h +++ b/src/Platform/OpenGL/OpenGLRendererAPI.h @@ -14,6 +14,7 @@ namespace Donut virtual void SetClearColor(const glm::vec4& color) override; virtual void Clear() override; virtual void EnableDepthTest() override; + virtual void SetFaceCulling(bool enabled) override; virtual void DrawIndexed(const std::shared_ptr<VertexArray>& vertexArray, uint32_t indexCount = 0) override; }; 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 diff --git a/src/Platform/OpenGL/OpenGLTexture.h b/src/Platform/OpenGL/OpenGLTexture.h new file mode 100644 index 0000000..4a95ab4 --- /dev/null +++ b/src/Platform/OpenGL/OpenGLTexture.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Rendering/Texture.h" +#include <glad/glad.h> + +namespace Donut +{ + class OpenGLTexture2D : public Texture2D + { + public: + OpenGLTexture2D(uint32_t width, uint32_t height); + OpenGLTexture2D(const std::string& path); + virtual ~OpenGLTexture2D(); + + virtual uint32_t GetWidth() const override { return m_Width; } + virtual uint32_t GetHeight() const override { return m_Height; } + virtual uint32_t GetRendererID() const override { return m_RendererID; } + + virtual void SetData(void* data, uint32_t size) override; + virtual void Bind(uint32_t slot = 0) const override; + + virtual bool operator==(const Texture& other) const override + { + return m_RendererID == other.GetRendererID(); + } + + private: + std::string m_Path; + uint32_t m_Width, m_Height; + uint32_t m_RendererID; + GLenum m_InternalFormat, m_DataFormat; + }; +} |
