diff --git a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp index 87198b8c2..d6bc34dee 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp @@ -16,13 +16,27 @@ namespace Nz class NAZARA_OPENGLRENDERER_API OpenGLFramebuffer : public Framebuffer { public: + enum class Type + { + FBO, + Window + }; + + inline OpenGLFramebuffer(Type type); OpenGLFramebuffer() = default; OpenGLFramebuffer(const OpenGLFramebuffer&) = delete; OpenGLFramebuffer(OpenGLFramebuffer&&) noexcept = default; ~OpenGLFramebuffer() = default; + virtual void Activate() const = 0; + + inline Type GetType() const; + OpenGLFramebuffer& operator=(const OpenGLFramebuffer&) = delete; OpenGLFramebuffer& operator=(OpenGLFramebuffer&&) noexcept = default; + + private: + Type m_type; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl index c629eb72a..df37fb3dd 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl @@ -7,6 +7,15 @@ namespace Nz { + inline OpenGLFramebuffer::OpenGLFramebuffer(Type type) : + m_type(type) + { + } + + inline auto OpenGLFramebuffer::GetType() const -> Type + { + return m_type; + } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp b/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp index f8673cd44..fde356958 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -43,8 +43,8 @@ namespace Nz std::shared_ptr m_device; std::vector m_renderImage; std::unique_ptr m_context; - OpenGLFramebuffer m_framebuffer; OpenGLRenderPass m_renderPass; + OpenGLWindowFramebuffer m_framebuffer; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.hpp b/include/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.hpp new file mode 100644 index 000000000..b1a08cb26 --- /dev/null +++ b/include/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.hpp @@ -0,0 +1,37 @@ +// Copyright (C) 2020 Jérôme Leclercq +// This file is part of the "Nazara Engine - Renderer module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_OPENGLRENDERER_OPENGLWINDOWFRAMEBUFFER_HPP +#define NAZARA_OPENGLRENDERER_OPENGLWINDOWFRAMEBUFFER_HPP + +#include +#include + +namespace Nz +{ + class OpenGLRenderWindow; + + class NAZARA_OPENGLRENDERER_API OpenGLWindowFramebuffer : public OpenGLFramebuffer + { + public: + inline OpenGLWindowFramebuffer(OpenGLRenderWindow& renderWindow); + OpenGLWindowFramebuffer(const OpenGLWindowFramebuffer&) = delete; + OpenGLWindowFramebuffer(OpenGLWindowFramebuffer&&) noexcept = default; + ~OpenGLWindowFramebuffer() = default; + + void Activate() const override; + + OpenGLWindowFramebuffer& operator=(const OpenGLWindowFramebuffer&) = delete; + OpenGLWindowFramebuffer& operator=(OpenGLWindowFramebuffer&&) noexcept = default; + + private: + OpenGLRenderWindow& m_renderWindow; + }; +} + +#include + +#endif // NAZARA_OPENGLRENDERER_OpenGLWindowFramebuffer_HPP diff --git a/include/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.inl b/include/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.inl new file mode 100644 index 000000000..9549a0467 --- /dev/null +++ b/include/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.inl @@ -0,0 +1,17 @@ +// Copyright (C) 2020 Jérôme Leclercq +// This file is part of the "Nazara Engine - OpenGL Renderer" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +namespace Nz +{ + inline OpenGLWindowFramebuffer::OpenGLWindowFramebuffer(OpenGLRenderWindow& renderWindow) : + OpenGLFramebuffer(OpenGLFramebuffer::Type::Window), + m_renderWindow(renderWindow) + { + } +} + +#include diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp b/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp index 96378fa22..ae0fd627f 100644 --- a/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp +++ b/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp @@ -58,6 +58,12 @@ namespace Nz::GL KHR }; + enum class FramebufferTarget + { + Draw, + Read + }; + enum class TextureTarget { Cubemap, @@ -89,6 +95,8 @@ namespace Nz::GL virtual ~Context(); void BindBuffer(BufferTarget target, GLuint buffer) const; + void BindFramebuffer(GLuint fbo) const; + void BindFramebuffer(FramebufferTarget target, GLuint fbo) const; void BindSampler(UInt32 textureUnit, GLuint sampler) const; void BindTexture(TextureTarget target, GLuint texture) const; void BindTexture(UInt32 textureUnit, TextureTarget target, GLuint texture) const; @@ -147,7 +155,10 @@ namespace Nz::GL std::array bufferTargets = { 0 }; std::vector textureUnits; GLuint boundProgram = 0; + GLuint boundDrawFBO = 0; + GLuint boundReadFBO = 0; UInt32 currentTextureUnit = 0; + RenderStates renderStates; }; std::array m_extensionStatus; diff --git a/src/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.cpp b/src/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.cpp new file mode 100644 index 000000000..48b900a5c --- /dev/null +++ b/src/Nazara/OpenGLRenderer/OpenGLWindowFramebuffer.cpp @@ -0,0 +1,20 @@ +// Copyright (C) 2020 Jérôme Leclercq +// This file is part of the "Nazara Engine - OpenGL Renderer" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +namespace Nz +{ + void OpenGLWindowFramebuffer::Activate() const + { + GL::Context& context = m_renderWindow.GetContext(); + if (!GL::Context::SetCurrentContext(&context)) + throw std::runtime_error("failed to bind window context"); + + context.BindFramebuffer(GL::FramebufferTarget::Draw, 0); + } +} diff --git a/src/Nazara/OpenGLRenderer/Wrapper/Context.cpp b/src/Nazara/OpenGLRenderer/Wrapper/Context.cpp index 3d7417f6d..e4344b3be 100644 --- a/src/Nazara/OpenGLRenderer/Wrapper/Context.cpp +++ b/src/Nazara/OpenGLRenderer/Wrapper/Context.cpp @@ -36,6 +36,19 @@ namespace Nz::GL } } + void Context::BindFramebuffer(GLuint fbo) const + { + if (m_state.boundDrawFBO != fbo || m_state.boundReadFBO != fbo) + { + if (!SetCurrentContext(this)) + throw std::runtime_error("failed to activate context"); + + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + m_state.boundDrawFBO = fbo; + m_state.boundReadFBO = fbo; + } + } + void Context::BindSampler(UInt32 textureUnit, GLuint sampler) const { if (textureUnit >= m_state.textureUnits.size())