mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-12 02:40:00 +01:00
Separate Screenshot class header
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
// Desktop OpenGL function loader
|
// Desktop OpenGL function loader
|
||||||
#include <glad/glad.h> // Initialized with gladLoadGLLoader()
|
#include <glad/glad.h> // Initialized with gladLoadGLLoader()
|
||||||
@@ -37,7 +36,6 @@
|
|||||||
|
|
||||||
// standalone image loader
|
// standalone image loader
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
#include <stb_image_write.h>
|
|
||||||
|
|
||||||
// vmix
|
// vmix
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
@@ -234,9 +232,7 @@ bool Rendering::Begin()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle window resize
|
// handle window resize
|
||||||
glfwGetFramebufferSize(window, &render_width, &render_height);
|
glfwGetFramebufferSize(window, &render_width, &render_height);
|
||||||
|
|
||||||
// handle window resize
|
|
||||||
glViewport(0, 0, render_width, render_height);
|
glViewport(0, 0, render_width, render_height);
|
||||||
|
|
||||||
// GL Colors
|
// 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 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));
|
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;
|
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
|
// Discarded because not working under OSX - kept in case it would become useful
|
||||||
|
|||||||
@@ -7,28 +7,16 @@
|
|||||||
#include <gst/gl/gl.h>
|
#include <gst/gl/gl.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Screenshot
|
#include "Screenshot.h"
|
||||||
|
|
||||||
|
struct RenderingAttrib
|
||||||
{
|
{
|
||||||
int Width, Height;
|
RenderingAttrib() {}
|
||||||
unsigned int * Data;
|
glm::ivec4 viewport;
|
||||||
|
glm::vec3 clear_color;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Rendering
|
class Rendering
|
||||||
{
|
{
|
||||||
friend class UserInterface;
|
friend class UserInterface;
|
||||||
@@ -70,10 +58,14 @@ public:
|
|||||||
void PushFrontDrawCallback(RenderingCallback function);
|
void PushFrontDrawCallback(RenderingCallback function);
|
||||||
void PushBackDrawCallback(RenderingCallback function);
|
void PushBackDrawCallback(RenderingCallback function);
|
||||||
|
|
||||||
|
// push and pop rendering attributes
|
||||||
|
void PushAttrib(glm::ivec4 viewport, glm::vec3 color);
|
||||||
|
void PopAttrib();
|
||||||
|
|
||||||
// request screenshot
|
// request screenshot
|
||||||
void RequestScreenshot();
|
void RequestScreenshot();
|
||||||
// get Screenshot
|
// get Screenshot
|
||||||
Screenshot *CurrentScreenshot();
|
class Screenshot *CurrentScreenshot();
|
||||||
|
|
||||||
// request fullscreen
|
// request fullscreen
|
||||||
void ToggleFullscreen();
|
void ToggleFullscreen();
|
||||||
@@ -97,6 +89,9 @@ private:
|
|||||||
// loop update end frame
|
// loop update end frame
|
||||||
void End();
|
void End();
|
||||||
|
|
||||||
|
// list of rendering attributes
|
||||||
|
std::list<RenderingAttrib> drawAttributes;
|
||||||
|
|
||||||
// list of functions to call at each Draw
|
// list of functions to call at each Draw
|
||||||
std::list<RenderingCallback> drawCallbacks;
|
std::list<RenderingCallback> drawCallbacks;
|
||||||
|
|
||||||
|
|||||||
109
Screenshot.cpp
Normal file
109
Screenshot.cpp
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
#include "Screenshot.h"
|
||||||
|
|
||||||
|
#include <memory.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
// standalone image loader
|
||||||
|
#include <stb_image.h>
|
||||||
|
#include <stb_image_write.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
26
Screenshot.h
Normal file
26
Screenshot.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user