diff options
Diffstat (limited to 'src/platform/opengl')
| -rw-r--r-- | src/platform/opengl/opengl_renderer_api.cpp | 17 | ||||
| -rw-r--r-- | src/platform/opengl/opengl_renderer_api.h | 1 | ||||
| -rw-r--r-- | src/platform/opengl/opengl_texture.cpp | 65 | ||||
| -rw-r--r-- | src/platform/opengl/opengl_texture.h | 33 |
4 files changed, 116 insertions, 0 deletions
diff --git a/src/platform/opengl/opengl_renderer_api.cpp b/src/platform/opengl/opengl_renderer_api.cpp index 49b4e53..3a147b9 100644 --- a/src/platform/opengl/opengl_renderer_api.cpp +++ b/src/platform/opengl/opengl_renderer_api.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::set_viewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) @@ -30,6 +35,18 @@ namespace Donut glEnable(GL_DEPTH_TEST); } + void OpenGLRendererAPI::set_face_culling(bool enabled) + { + if (enabled) + { + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CCW); + } + else + glDisable(GL_CULL_FACE); + } + void OpenGLRendererAPI::draw_indexed(const std::shared_ptr<VertexArray>& vertex_array, uint32_t index_count) { uint32_t count = index_count ? index_count : vertex_array->get_index_buffer()->get_count(); diff --git a/src/platform/opengl/opengl_renderer_api.h b/src/platform/opengl/opengl_renderer_api.h index 30be426..f28567c 100644 --- a/src/platform/opengl/opengl_renderer_api.h +++ b/src/platform/opengl/opengl_renderer_api.h @@ -14,6 +14,7 @@ namespace Donut virtual void set_clear_color(const glm::vec4& color) override; virtual void clear() override; virtual void enable_depth_test() override; + virtual void set_face_culling(bool enabled) override; virtual void draw_indexed(const std::shared_ptr<VertexArray>& vertex_array, uint32_t index_count = 0) override; }; 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); + } +}; diff --git a/src/platform/opengl/opengl_texture.h b/src/platform/opengl/opengl_texture.h new file mode 100644 index 0000000..0782e8b --- /dev/null +++ b/src/platform/opengl/opengl_texture.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 get_width() const override { return m_width; } + virtual uint32_t get_height() const override { return m_height; } + virtual uint32_t get_renderer_id() const override { return m_renderer_id; } + + virtual void set_data(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_renderer_id == other.get_renderer_id(); + } + + private: + std::string m_path; + uint32_t m_width, m_height; + uint32_t m_renderer_id; + GLenum m_internal_format, m_data_format; + }; +} |
