diff --git a/RenderingManager.cpp b/RenderingManager.cpp index e658528..6b54290 100644 --- a/RenderingManager.cpp +++ b/RenderingManager.cpp @@ -2,7 +2,6 @@ #include #include #include -#include // Desktop OpenGL function loader #include // Initialized with gladLoadGLLoader() @@ -37,7 +36,6 @@ // standalone image loader #include -#include // vmix #include "defines.h" @@ -234,9 +232,7 @@ bool Rendering::Begin() } // handle window resize - glfwGetFramebufferSize(window, &render_width, &render_height); - - // handle window resize + glfwGetFramebufferSize(window, &render_width, &render_height); glViewport(0, 0, render_width, render_height); // GL Colors @@ -286,7 +282,8 @@ void Rendering::Close() } -glm::mat4 Rendering::Projection() { +glm::mat4 Rendering::Projection() +{ glm::mat4 projection = glm::ortho(-5.0, 5.0, -5.0, 5.0); glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(1.f, AspectRatio(), 1.f)); @@ -351,82 +348,6 @@ void Rendering::RequestScreenshot() request_screenshot = true; } -void Screenshot::CreateEmpty(int w, int h) -{ - Clear(); - Width = w; - Height = h; - Data = (unsigned int*) malloc(Width * Height * 4 * sizeof(unsigned int)); - memset(Data, 0, Width * Height * 4); -} - -void Screenshot::CreateFromCaptureGL(int x, int y, int w, int h) -{ - Clear(); - Width = w; - Height = h; - Data = (unsigned int*) malloc(Width * Height * 4); - - // actual capture of frame buffer - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, Data); - - // make it usable - RemoveAlpha(); - FlipVertical(); -} - -void Screenshot::SaveFile(const char* filename) -{ - if (Data) - stbi_write_png(filename, Width, Height, 4, Data, Width * 4); -} - -void Screenshot::RemoveAlpha() -{ - unsigned int* p = Data; - int n = Width * Height; - while (n-- > 0) - { - *p |= 0xFF000000; - p++; - } -} - -void Screenshot::BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const -{ - const Screenshot* src = this; - assert(dst != src); - assert(dst != NULL); - assert(src_x >= 0 && src_y >= 0); - assert(src_x + w <= src->Width); - assert(src_y + h <= src->Height); - assert(dst_x >= 0 && dst_y >= 0); - assert(dst_x + w <= dst->Width); - assert(dst_y + h <= dst->Height); - for (int y = 0; y < h; y++) - memcpy(dst->Data + dst_x + (dst_y + y) * dst->Width, src->Data + src_x + (src_y + y) * src->Width, w * 4); -} - -void Screenshot::FlipVertical() -{ - int comp = 4; - int stride = Width * comp; - unsigned char* line_tmp = new unsigned char[stride]; - unsigned char* line_a = (unsigned char*)Data; - unsigned char* line_b = (unsigned char*)Data + (stride * (Height - 1)); - while (line_a < line_b) - { - memcpy(line_tmp, line_a, stride); - memcpy(line_a, line_b, stride); - memcpy(line_b, line_tmp, stride); - line_a += stride; - line_b -= stride; - } - delete[] line_tmp; -} - - // // Discarded because not working under OSX - kept in case it would become useful diff --git a/RenderingManager.h b/RenderingManager.h index 0e320c4..f29abc2 100644 --- a/RenderingManager.h +++ b/RenderingManager.h @@ -7,28 +7,16 @@ #include #include -class Screenshot +#include "Screenshot.h" + +struct RenderingAttrib { - int Width, Height; - unsigned int * Data; - -public: - Screenshot() { Width = Height = 0; Data = nullptr; } - ~Screenshot() { Clear(); } - - bool IsFull() { return Data != nullptr; } - void Clear() { if (IsFull()) free(Data); Data = nullptr; } - - void CreateEmpty(int w, int h); - void CreateFromCaptureGL(int x, int y, int w, int h); - - void RemoveAlpha(); - void FlipVertical(); - - void SaveFile(const char* filename); - void BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const; + RenderingAttrib() {} + glm::ivec4 viewport; + glm::vec3 clear_color; }; + class Rendering { friend class UserInterface; @@ -70,10 +58,14 @@ public: void PushFrontDrawCallback(RenderingCallback function); void PushBackDrawCallback(RenderingCallback function); + // push and pop rendering attributes + void PushAttrib(glm::ivec4 viewport, glm::vec3 color); + void PopAttrib(); + // request screenshot void RequestScreenshot(); // get Screenshot - Screenshot *CurrentScreenshot(); + class Screenshot *CurrentScreenshot(); // request fullscreen void ToggleFullscreen(); @@ -97,6 +89,9 @@ private: // loop update end frame void End(); + // list of rendering attributes + std::list drawAttributes; + // list of functions to call at each Draw std::list drawCallbacks; diff --git a/Screenshot.cpp b/Screenshot.cpp new file mode 100644 index 0000000..21c011c --- /dev/null +++ b/Screenshot.cpp @@ -0,0 +1,109 @@ +#include "Screenshot.h" + +#include +#include + +#include + +// standalone image loader +#include +#include + +Screenshot::Screenshot() +{ + Width = Height = 0; + Data = nullptr; +} + +Screenshot::~Screenshot() +{ + Clear(); +} + +bool Screenshot::IsFull() +{ + return Data != nullptr; +} + +void Screenshot::Clear() +{ + if (IsFull()) + free(Data); + Data = nullptr; +} + +void Screenshot::CreateEmpty(int w, int h) +{ + Clear(); + Width = w; + Height = h; + Data = (unsigned int*) malloc(Width * Height * 4 * sizeof(unsigned int)); + memset(Data, 0, Width * Height * 4); +} + +void Screenshot::CreateFromCaptureGL(int x, int y, int w, int h) +{ + Clear(); + Width = w; + Height = h; + Data = (unsigned int*) malloc(Width * Height * 4); + + // actual capture of frame buffer + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, Data); + + // make it usable + RemoveAlpha(); + FlipVertical(); +} + +void Screenshot::SaveFile(const char* filename) +{ + if (Data) + stbi_write_png(filename, Width, Height, 4, Data, Width * 4); +} + +void Screenshot::RemoveAlpha() +{ + unsigned int* p = Data; + int n = Width * Height; + while (n-- > 0) + { + *p |= 0xFF000000; + p++; + } +} + +void Screenshot::BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const +{ + const Screenshot* src = this; + assert(dst != src); + assert(dst != NULL); + assert(src_x >= 0 && src_y >= 0); + assert(src_x + w <= src->Width); + assert(src_y + h <= src->Height); + assert(dst_x >= 0 && dst_y >= 0); + assert(dst_x + w <= dst->Width); + assert(dst_y + h <= dst->Height); + for (int y = 0; y < h; y++) + memcpy(dst->Data + dst_x + (dst_y + y) * dst->Width, src->Data + src_x + (src_y + y) * src->Width, w * 4); +} + +void Screenshot::FlipVertical() +{ + int comp = 4; + int stride = Width * comp; + unsigned char* line_tmp = new unsigned char[stride]; + unsigned char* line_a = (unsigned char*)Data; + unsigned char* line_b = (unsigned char*)Data + (stride * (Height - 1)); + while (line_a < line_b) + { + memcpy(line_tmp, line_a, stride); + memcpy(line_a, line_b, stride); + memcpy(line_b, line_tmp, stride); + line_a += stride; + line_b -= stride; + } + delete[] line_tmp; +} + diff --git a/Screenshot.h b/Screenshot.h new file mode 100644 index 0000000..5e2da51 --- /dev/null +++ b/Screenshot.h @@ -0,0 +1,26 @@ +#ifndef SCREENSHOT_H +#define SCREENSHOT_H + +class Screenshot +{ + int Width, Height; + unsigned int * Data; + +public: + Screenshot(); + ~Screenshot(); + + bool IsFull(); + void Clear(); + + void CreateEmpty(int w, int h); + void CreateFromCaptureGL(int x, int y, int w, int h); + + void RemoveAlpha(); + void FlipVertical(); + + void SaveFile(const char* filename); + void BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const; +}; + +#endif // SCREENSHOT_H