From f105a114a97d2046db5ee16114f53a9ccb23a633 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Sat, 30 May 2020 22:55:47 +0200 Subject: [PATCH] First implementation of Output Rendering Window. --- FrameBuffer.cpp | 9 +++---- FrameBuffer.h | 2 ++ RenderingManager.cpp | 57 ++++++++++++++++++++++++++++++++++++-------- RenderingManager.h | 4 ++-- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/FrameBuffer.cpp b/FrameBuffer.cpp index b22bc75..8dfd690 100644 --- a/FrameBuffer.cpp +++ b/FrameBuffer.cpp @@ -131,17 +131,14 @@ void FrameBuffer::release() bool FrameBuffer::blit(FrameBuffer *other) { - if (!framebufferid_) - return false; - - if (attrib_.viewport.x != other->width() || attrib_.viewport.y != other->height()) + if (!framebufferid_ || !other || !other->id()) return false; glBindFramebuffer(GL_DRAW_FRAMEBUFFER, other->framebufferid_); glBindFramebuffer(GL_READ_FRAMEBUFFER, framebufferid_); // blit to the frame buffer object - glBlitFramebuffer(0, attrib_.viewport.y, attrib_.viewport.x, 0, 0, 0, - other->width(), other->height(), + glBlitFramebuffer(0, 0, attrib_.viewport.x, attrib_.viewport.y, + 0, 0, other->width(), other->height(), GL_COLOR_BUFFER_BIT, GL_NEAREST); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); diff --git a/FrameBuffer.h b/FrameBuffer.h index 093bc4e..bc248ff 100644 --- a/FrameBuffer.h +++ b/FrameBuffer.h @@ -43,6 +43,8 @@ public: // texture index for draw uint texture() const; + inline uint id() const { return framebufferid_; } + private: void init(); void checkFramebufferStatus(); diff --git a/RenderingManager.cpp b/RenderingManager.cpp index e629ab4..d076285 100644 --- a/RenderingManager.cpp +++ b/RenderingManager.cpp @@ -43,6 +43,7 @@ #include "Log.h" #include "Resource.h" #include "Settings.h" +#include "Primitives.h" #include "Mixer.h" #include "SystemToolkit.h" #include "UserInterfaceManager.h" @@ -229,8 +230,6 @@ bool Rendering::init() // output window output.init(main_window_, 1); - glfwMakeContextCurrent(main_window_); - return true; } @@ -301,7 +300,7 @@ void Rendering::draw() g_main_context_iteration(NULL, FALSE); // draw output window - output.draw(); + output.draw( Mixer::manager().session()->frame() ); } bool Rendering::Begin() @@ -522,9 +521,19 @@ bool RenderingWindow::init(GLFWwindow *share, int id) Settings::WindowConfig winset = Settings::application.windows[id_]; + // setup endering area + window_attributes_.viewport.x = winset.w; + window_attributes_.viewport.y = winset.h; + window_attributes_.clear_color = glm::vec4(0.f, 0.f, 0.f, 1.0); + // do not show at creation glfwWindowHint(GLFW_FOCUSED, GLFW_FALSE); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + + glfwWindowHint(GLFW_SAMPLES, 0); + glfwWindowHint(GLFW_DEPTH_BITS, 0); + glfwWindowHint(GLFW_ALPHA_BITS, 0); + window_ = glfwCreateWindow(winset.w, winset.h, winset.name.c_str(), NULL, master_); if (window_ == NULL){ Log::Error("Failed to create GLFW Window %d", id_); @@ -540,11 +549,6 @@ bool RenderingWindow::init(GLFWwindow *share, int id) glfwMakeContextCurrent(window_); glfwSwapInterval(0); // Disable vsync - // setup endering area - window_attributes_.viewport.x = winset.w; - window_attributes_.viewport.y = winset.h; - window_attributes_.clear_color = glm::vec4(0.f, 0.f, 0.f, 1.0); - glfwShowWindow(window_); // give back context ownership @@ -553,7 +557,25 @@ bool RenderingWindow::init(GLFWwindow *share, int id) return true; } -void RenderingWindow::draw() +class WindowSurface : public Primitive { + +public: + WindowSurface(Shader *s = new ImageShader); +}; + +WindowSurface::WindowSurface(Shader *s) : Primitive(s) +{ + points_ = std::vector { glm::vec3( -1.f, -1.f, 0.f ), glm::vec3( -1.f, 1.f, 0.f ), + glm::vec3( 1.f, -1.f, 0.f ), glm::vec3( 1.f, 1.f, 0.f ) }; + colors_ = std::vector { glm::vec4( 1.f, 1.f, 1.f , 1.f ), glm::vec4( 1.f, 1.f, 1.f, 1.f ), + glm::vec4( 1.f, 1.f, 1.f, 1.f ), glm::vec4( 1.f, 1.f, 1.f, 1.f ) }; + texCoords_ = std::vector { glm::vec2( 0.f, 1.f ), glm::vec2( 0.f, 0.f ), + glm::vec2( 1.f, 1.f ), glm::vec2( 1.f, 0.f ) }; + indices_ = std::vector { 0, 1, 2, 3 }; + drawMode_ = GL_TRIANGLE_STRIP; +} + +void RenderingWindow::draw(FrameBuffer *fb) { if (!window_) return; @@ -572,11 +594,26 @@ void RenderingWindow::draw() window_attributes_.clear_color.b, window_attributes_.clear_color.a); glClear(GL_COLOR_BUFFER_BIT); + static glm::mat4 projection = glm::ortho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f); + static WindowSurface *surface = new WindowSurface; + + if (fb) { + float aspectRatio = static_cast(window_attributes_.viewport.x) / static_cast(window_attributes_.viewport.y); + float renderingAspectRatio = fb->aspectRatio(); + glm::vec3 scale; + if (aspectRatio < renderingAspectRatio) + scale = glm::vec3(1.f, aspectRatio / renderingAspectRatio, 1.f); + else + scale = glm::vec3(renderingAspectRatio / aspectRatio, 1.f, 1.f); + + glBindTexture(GL_TEXTURE_2D, fb->texture()); + surface->draw(glm::scale(glm::identity(), scale), projection); + } + // swap buffer glfwSwapBuffers(window_); } - // give back context ownership glfwMakeContextCurrent(master_); } diff --git a/RenderingManager.h b/RenderingManager.h index 251249e..5aff0c2 100644 --- a/RenderingManager.h +++ b/RenderingManager.h @@ -30,10 +30,10 @@ public: RenderingWindow(); ~RenderingWindow(); - void setFrameBuffer(FrameBuffer *fb) { frame_buffer_ = fb; } +// void setFrameBuffer(FrameBuffer *fb) { frame_buffer_ = fb; } bool init(GLFWwindow *share, int id); - void draw(); + void draw(FrameBuffer *fb); };