Implement mechanism to push & pop rendering attributes

This commit is contained in:
brunoherbelin
2020-04-05 11:44:01 +02:00
parent 370e680e91
commit 1e70d8f4e2
11 changed files with 139 additions and 100 deletions

View File

@@ -221,6 +221,7 @@ set(VMIX_SRCS
Primitives.cpp
SessionVisitor.cpp
Settings.cpp
Screenshot.cpp
Resource.cpp
FileDialog.cpp
MediaPlayer.cpp

View File

@@ -5,14 +5,18 @@
#include <glad/glad.h>
FrameBuffer::FrameBuffer(uint width, uint height, bool useDepthBuffer) : width_(width), height_(height)
FrameBuffer::FrameBuffer(uint width, uint height, bool useDepthBuffer)
{
attrib_.viewport.x = width;
attrib_.viewport.y = height;
attrib_.clear_color = glm::vec3(0.f);
// create a renderbuffer object to store depth info
GLuint rboId;
if (useDepthBuffer){
glGenRenderbuffers(1, &rboId);
glBindRenderbuffer(GL_RENDERBUFFER, rboId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width_, height_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
@@ -23,7 +27,7 @@ FrameBuffer::FrameBuffer(uint width, uint height, bool useDepthBuffer) : width_(
// generate texture
glGenTextures(1, &textureid_);
glBindTexture(GL_TEXTURE_2D, textureid_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
@@ -38,6 +42,8 @@ FrameBuffer::FrameBuffer(uint width, uint height, bool useDepthBuffer) : width_(
GL_RENDERBUFFER, rboId);
}
checkFramebufferStatus();
FrameBuffer::release();
}
@@ -48,21 +54,30 @@ FrameBuffer::~FrameBuffer()
float FrameBuffer::aspectRatio() const
{
return static_cast<float>(width_) / static_cast<float>(height_);
return static_cast<float>(width()) / static_cast<float>(height());
}
void FrameBuffer::bind()
{
glBindFramebuffer(GL_FRAMEBUFFER, framebufferid_);
}
// handle window resize
glViewport(0, 0, width_, height_);
void FrameBuffer::begin()
{
bind();
Rendering::manager().PushAttrib(attrib_);
// GL Colors+
glClearColor(0.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
}
void FrameBuffer::end()
{
Rendering::manager().PopAttrib();
FrameBuffer::release();
}
void FrameBuffer::release()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
@@ -71,13 +86,13 @@ void FrameBuffer::release()
bool FrameBuffer::blit(FrameBuffer *other)
{
if (width_ != other->width() || height_ != other->height())
if (attrib_.viewport.x != other->width() || attrib_.viewport.y != other->height())
return false;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, other->framebufferid_);
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebufferid_);
// blit to the frame buffer object
glBlitFramebuffer(0, height_, width_, 0, 0, 0,
glBlitFramebuffer(0, attrib_.viewport.y, attrib_.viewport.x, 0, 0, 0,
other->width(), other->height(),
GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

View File

@@ -2,7 +2,7 @@
#define FRAMEBUFFER_H
#include "Scene.h"
#include "RenderingManager.h"
class FrameBuffer {
@@ -12,20 +12,23 @@ public:
// bind the FrameBuffer as current to draw into
void bind();
// releases the framebuffer object
void begin();
void end();
// release any framebuffer object
static void release();
// blit copy to another, returns true on success
bool blit(FrameBuffer *other);
inline uint width() const { return width_; }
inline uint height() const { return height_; }
inline uint width() const { return attrib_.viewport.x; }
inline uint height() const { return attrib_.viewport.y; }
inline uint texture() const { return textureid_; }
float aspectRatio() const;
private:
void checkFramebufferStatus();
uint width_;
uint height_;
RenderingAttrib attrib_;
uint textureid_;
uint framebufferid_;
};

View File

@@ -215,7 +215,6 @@ void LineCircle::init()
}
shader_ = new Shader();
visible_ = true;
initialized_ = true;
}

View File

@@ -62,10 +62,8 @@ static void WindowRefreshCallback( GLFWwindow* window )
Rendering::Rendering()
{
window = nullptr;
render_width = 0;
render_height = 0;
request_screenshot = false;
main_window_ = nullptr;
request_screenshot_ = false;
}
bool Rendering::Init()
@@ -90,8 +88,8 @@ bool Rendering::Init()
// Create window with graphics context
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(winset.w, winset.h, winset.name.c_str(), NULL, NULL);
if (window == NULL){
main_window_ = glfwCreateWindow(winset.w, winset.h, winset.name.c_str(), NULL, NULL);
if (main_window_ == NULL){
Log::Error("Failed to Create GLFW Window.");
return false;
}
@@ -102,14 +100,14 @@ bool Rendering::Init()
if (fp != nullptr) {
GLFWimage icon;
icon.pixels = stbi_load_from_memory( (const stbi_uc*)fp, fpsize, &icon.width, &icon.height, nullptr, 4 );
glfwSetWindowIcon( window, 1, &icon );
glfwSetWindowIcon( main_window_, 1, &icon );
free( icon.pixels );
}
glfwSetWindowPos(window, winset.x, winset.y);
glfwMakeContextCurrent(window);
glfwSetWindowPos(main_window_, winset.x, winset.y);
glfwMakeContextCurrent(main_window_);
glfwSwapInterval(1); // Enable vsync3
glfwSetWindowRefreshCallback( window, WindowRefreshCallback );
glfwSetWindowRefreshCallback( main_window_, WindowRefreshCallback );
// Initialize OpenGL loader
bool err = gladLoadGLLoader((GLADloadproc) glfwGetProcAddress) == 0;
@@ -119,14 +117,15 @@ bool Rendering::Init()
}
// show window
glfwShowWindow(window);
glfwShowWindow(main_window_);
// restore fullscreen
if (winset.fullscreen)
ToggleFullscreen();
// Rendering area (not necessarily same as window)
glfwGetFramebufferSize(window, &render_width, &render_height);
glViewport(0, 0, render_width, render_height);
// Rendering area (here same as window)
glfwGetFramebufferSize(main_window_, &(main_window_attributes_.viewport.x), &(main_window_attributes_.viewport.y));
glViewport(0, 0, main_window_attributes_.viewport.x, main_window_attributes_.viewport.y);
main_window_attributes_.clear_color = glm::vec3(COLOR_BGROUND);
// Gstreamer link to context
g_setenv ("GST_GL_API", "opengl3", FALSE);
@@ -153,10 +152,10 @@ bool Rendering::Init()
global_display = (GstGLDisplay*) gst_gl_display_x11_new_with_display( glfwGetX11Display() );
global_gl_context = gst_gl_context_new_wrapped (global_display,
(guintptr) glfwGetGLXContext(window),
(guintptr) glfwGetGLXContext(main_window_),
GST_GL_PLATFORM_GLX, GST_GL_API_OPENGL);
global_window_handle = (guintptr) glfwGetX11Window(window);
global_window_handle = (guintptr) glfwGetX11Window(main_window_);
#endif
@@ -173,25 +172,25 @@ bool Rendering::Init()
// gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE(vdpaumpegdec), GST_RANK_PRIMARY);
// file drop callback
glfwSetDropCallback(window, Rendering::FileDropped);
glfwSetDropCallback(main_window_, Rendering::FileDropped);
return true;
}
bool Rendering::isActive()
{
return !glfwWindowShouldClose(window);
return !glfwWindowShouldClose(main_window_);
}
void Rendering::PushFrontDrawCallback(RenderingCallback function)
{
drawCallbacks.push_front(function);
draw_callbacks_.push_front(function);
}
void Rendering::PushBackDrawCallback(RenderingCallback function)
{
drawCallbacks.push_back(function);
draw_callbacks_.push_back(function);
}
void Rendering::Draw()
@@ -201,7 +200,7 @@ void Rendering::Draw()
UserInterface::manager().NewFrame();
std::list<Rendering::RenderingCallback>::iterator iter;
for (iter=drawCallbacks.begin(); iter != drawCallbacks.end(); iter++)
for (iter=draw_callbacks_.begin(); iter != draw_callbacks_.end(); iter++)
{
(*iter)();
}
@@ -224,19 +223,20 @@ bool Rendering::Begin()
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();
glfwMakeContextCurrent(window);
if( glfwGetWindowAttrib( window, GLFW_ICONIFIED ) )
glfwMakeContextCurrent(main_window_);
if( glfwGetWindowAttrib( main_window_, GLFW_ICONIFIED ) )
{
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
return false;
}
// handle window resize
glfwGetFramebufferSize(window, &render_width, &render_height);
glViewport(0, 0, render_width, render_height);
glfwGetFramebufferSize(main_window_, &(main_window_attributes_.viewport.x), &(main_window_attributes_.viewport.y));
glViewport(0, 0, main_window_attributes_.viewport.x, main_window_attributes_.viewport.y);
// GL Colors
glClearColor(0.2f, 0.2f, 0.2f, 1.f);
glClearColor(main_window_attributes_.clear_color.r, main_window_attributes_.clear_color.g,
main_window_attributes_.clear_color.b, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
return true;
@@ -244,16 +244,16 @@ bool Rendering::Begin()
void Rendering::End()
{
glfwMakeContextCurrent(window);
glfwMakeContextCurrent(main_window_);
// perform screenshot if requested
if (request_screenshot) {
window_screenshot.CreateFromCaptureGL(0, 0, render_width, render_height);
request_screenshot = false;
if (request_screenshot_) {
screenshot_.CreateFromCaptureGL(0, 0, main_window_attributes_.viewport.x, main_window_attributes_.viewport.y);
request_screenshot_ = false;
}
// swap GL buffers
glfwSwapBuffers(window);
glfwSwapBuffers(main_window_);
}
@@ -262,23 +262,51 @@ void Rendering::Terminate()
// settings
if ( !Settings::application.windows.front().fullscreen) {
int x, y;
glfwGetWindowPos(window, &x, &y);
glfwGetWindowPos(main_window_, &x, &y);
Settings::application.windows.front().x = x;
Settings::application.windows.front().y = y;
glfwGetWindowSize(window,&x, &y);
glfwGetWindowSize(main_window_,&x, &y);
Settings::application.windows.front().w = x;
Settings::application.windows.front().h = y;
}
// close window
glfwDestroyWindow(window);
glfwDestroyWindow(main_window_);
glfwTerminate();
}
void Rendering::Close()
{
glfwSetWindowShouldClose(window, true);
glfwSetWindowShouldClose(main_window_, true);
}
void Rendering::PushAttrib(RenderingAttrib ra)
{
// push it to top of pile
draw_attributes_.push_front(ra);
// apply Changes to OpenGL
glViewport(0, 0, ra.viewport.x, ra.viewport.y);
glClearColor(ra.clear_color.r, ra.clear_color.g, ra.clear_color.b, 1.f);
}
void Rendering::PopAttrib()
{
// pops the top of the pile
if (draw_attributes_.size() > 0)
draw_attributes_.pop_front();
// set attribute element to default
RenderingAttrib ra = main_window_attributes_;
// if there is an element at top, use it
if (draw_attributes_.size() > 0)
ra = draw_attributes_.front();
// apply Changes to OpenGL
glViewport(0, 0, ra.viewport.x, ra.viewport.y);
glClearColor(ra.clear_color.r, ra.clear_color.g, ra.clear_color.b, 1.f);
}
@@ -290,12 +318,15 @@ glm::mat4 Rendering::Projection()
return projection * scale;
}
float Rendering::Width() { return main_window_attributes_.viewport.x; }
float Rendering::Height() { return main_window_attributes_.viewport.y; }
void Rendering::ToggleFullscreen()
{
// if in fullscreen mode
if (glfwGetWindowMonitor(window) != nullptr) {
if (glfwGetWindowMonitor(main_window_) != nullptr) {
// set to window mode
glfwSetWindowMonitor( window, nullptr, Settings::application.windows.front().x,
glfwSetWindowMonitor( main_window_, nullptr, Settings::application.windows.front().x,
Settings::application.windows.front().y,
Settings::application.windows.front().w,
Settings::application.windows.front().h, 0 );
@@ -305,10 +336,10 @@ void Rendering::ToggleFullscreen()
else {
// remember window geometry
int x, y;
glfwGetWindowPos(window, &x, &y);
glfwGetWindowPos(main_window_, &x, &y);
Settings::application.windows.front().x = x;
Settings::application.windows.front().y = y;
glfwGetWindowSize(window,&x, &y);
glfwGetWindowSize(main_window_,&x, &y);
Settings::application.windows.front().w = x;
Settings::application.windows.front().h = y;
@@ -317,7 +348,7 @@ void Rendering::ToggleFullscreen()
const GLFWvidmode * mode = glfwGetVideoMode(monitor);
// set to fullscreen mode
glfwSetWindowMonitor( window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
glfwSetWindowMonitor( main_window_, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
Settings::application.windows.front().fullscreen = true;
}
@@ -325,7 +356,7 @@ void Rendering::ToggleFullscreen()
float Rendering::AspectRatio()
{
return static_cast<float>(render_width) / static_cast<float>(render_height);
return static_cast<float>(main_window_attributes_.viewport.x) / static_cast<float>(main_window_attributes_.viewport.y);
}
void Rendering::FileDropped(GLFWwindow* window, int path_count, const char* paths[])
@@ -339,13 +370,13 @@ void Rendering::FileDropped(GLFWwindow* window, int path_count, const char* path
Screenshot *Rendering::CurrentScreenshot()
{
return &window_screenshot;
return &screenshot_;
}
void Rendering::RequestScreenshot()
{
window_screenshot.Clear();
request_screenshot = true;
screenshot_.Clear();
request_screenshot_ = true;
}

View File

@@ -12,7 +12,7 @@
struct RenderingAttrib
{
RenderingAttrib() {}
glm::ivec4 viewport;
glm::ivec2 viewport;
glm::vec3 clear_color;
};
@@ -22,11 +22,8 @@ class Rendering
friend class UserInterface;
// GLFW integration in OS window management
class GLFWwindow* window;
Screenshot window_screenshot;
class GLFWwindow* main_window_;
std::string glsl_version;
int render_width, render_height;
bool request_screenshot;
// Private Constructor
Rendering();
@@ -59,7 +56,7 @@ public:
void PushBackDrawCallback(RenderingCallback function);
// push and pop rendering attributes
void PushAttrib(glm::ivec4 viewport, glm::vec3 color);
void PushAttrib(RenderingAttrib ra);
void PopAttrib();
// request screenshot
@@ -70,9 +67,9 @@ public:
// request fullscreen
void ToggleFullscreen();
// get width of rendering area
float Width() { return render_width; }
float Width();
// get height of rendering area
float Height() { return render_height; }
float Height();
// get aspect ratio of rendering area
float AspectRatio();
@@ -90,13 +87,17 @@ private:
void End();
// list of rendering attributes
std::list<RenderingAttrib> drawAttributes;
std::list<RenderingAttrib> draw_attributes_;
RenderingAttrib main_window_attributes_;
// list of functions to call at each Draw
std::list<RenderingCallback> drawCallbacks;
std::list<RenderingCallback> draw_callbacks_;
// file drop callback
static void FileDropped(GLFWwindow* window, int path_count, const char* paths[]);
static void FileDropped(GLFWwindow* main_window_, int path_count, const char* paths[]);
Screenshot screenshot_;
bool request_screenshot_;
};

View File

@@ -203,11 +203,3 @@ void Shader::reset()
}
void Shader::setModelview(float x, float y, float angle, float scale, float aspect_ratio)
{
glm::mat4 View = glm::translate(glm::identity<glm::mat4>(), glm::vec3(x, y, 0.f));
View = glm::rotate(View, angle, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 Model = glm::scale(glm::identity<glm::mat4>(), glm::vec3(scale * aspect_ratio, scale, scale));
modelview = View * Model;
}

View File

@@ -59,11 +59,10 @@ public:
} BlendMode;
BlendMode blending;
void setModelview(float x, float y, float angle, float scale, float aspect_ratio);
protected:
ShadingProgram *program_;
};
#endif /* __SHADER_H_ */

View File

@@ -73,7 +73,7 @@ UserInterface::UserInterface()
bool UserInterface::Init()
{
if (Rendering::manager().window == nullptr)
if (Rendering::manager().main_window_ == nullptr)
return false;
// Setup Dear ImGui context
@@ -84,7 +84,7 @@ bool UserInterface::Init()
io.MouseDrawCursor = true;
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(Rendering::manager().window, true);
ImGui_ImplGlfw_InitForOpenGL(Rendering::manager().main_window_, true);
ImGui_ImplOpenGL3_Init(Rendering::manager().glsl_version.c_str());
// Setup Dear ImGui style

View File

@@ -19,6 +19,13 @@
#define SCENE_UNIT 10.0
#define CIRCLE_SQUARE_DIST(x,y) ( (x*x + y*y) / (SCENE_UNIT * SCENE_UNIT * SCENE_UNIT * SCENE_UNIT) )
#define IMGUI_TITLE_MAINWINDOW ICON_FA_CIRCLE_NOTCH " v-mix"
#define IMGUI_TITLE_MEDIAPLAYER ICON_FA_FILM " Media Player"
#define IMGUI_TITLE_SHADEREDITOR ICON_FA_CODE " Shader Editor"
#define COLOR_BGROUND 0.2, 0.2, 0.2
// from glmixer
#define TEXTURE_REQUIRED_MAXIMUM 2048
#define CATALOG_TEXTURE_HEIGHT 96
#define SELECTBUFSIZE 512
@@ -42,7 +49,6 @@
#define COLOR_SOURCE_STATIC 230, 40, 40
#define COLOR_SELECTION 10, 210, 40
#define COLOR_SELECTION_AREA 50, 210, 50
#define COLOR_BGROUND 52, 52, 52
#define COLOR_CIRCLE 210, 30, 210
#define COLOR_CIRCLE_MOVE 230, 30, 230
#define COLOR_DRAWINGS 180, 180, 180
@@ -54,9 +60,6 @@
#define COLOR_FRAME_MOVE 230, 30, 230
#define COLOR_CURSOR 10, 100, 255
#define IMGUI_TITLE_MAINWINDOW ICON_FA_CIRCLE_NOTCH " v-mix"
#define IMGUI_TITLE_MEDIAPLAYER ICON_FA_FILM " Media Player"
#define IMGUI_TITLE_SHADEREDITOR ICON_FA_CODE " Shader Editor"
#endif // VMIX_DEFINES_H

View File

@@ -178,26 +178,21 @@ void drawScene()
last_time = current_time;
// recursive update from root of scene
glm::mat4 MV = glm::identity<glm::mat4>();
glm::mat4 P = glm::scale( glm::ortho(-5.f, 5.f, -5.f, 5.f), glm::vec3(1.f, output->aspectRatio(), 1.f));
// glm::mat4 View = glm::translate(glm::identity<glm::mat4>(), glm::vec3(0, 0, 0.f));
// View = glm::rotate(View, 0.f, glm::vec3(0.0f, 0.0f, 1.0f));
// glm::mat4 Model = glm::scale(glm::identity<glm::mat4>(), glm::vec3(1, 1, 1));
// mv = View * Model;
scene.root_.update( static_cast<float>( GST_TIME_AS_MSECONDS(dt)) * 0.001f );
// draw in output frame buffer
output->bind();
scene.root_.draw(MV, P);
FrameBuffer::release();
glm::mat4 P = glm::scale( glm::ortho(-5.f, 5.f, -5.f, 5.f), glm::vec3(1.f, output->aspectRatio(), 1.f));
output->begin();
scene.root_.draw(glm::identity<glm::mat4>(), P);
output->end();
// draw in main view
scene.root_.draw(glm::identity<glm::mat4>(), Rendering::manager().Projection());
// draw GUI tree scene
ImGui::Begin(IMGUI_TITLE_MAINWINDOW);
static ImGuiVisitor v;
scene.accept(v);
ImGui::End();
}