Loading...
Searching...
No Matches
Shader-Demo
Prev Tutorial: Nanovg-Demo
Next Tutorial: Font-Demo
Original author | Amir Hassan (kallaballa) amir@.nosp@m.viel.nosp@m.-zu.o.nosp@m.rg |
Compatibility | OpenCV >= 4.7 |
Renders a mandelbrot fractal zoom. Uses shaders, OpenCV and video editing together.
Downloading...
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Copyright Amir Hassan (kallaballa) <amir@viel-zu.org>
#include <opencv2/v4d/v4d.hpp>
using std::cerr;
using std::endl;
/* Demo parameters */
#ifndef __EMSCRIPTEN__
constexpr long unsigned int WIDTH = 1280;
constexpr long unsigned int HEIGHT = 720;
#else
constexpr long unsigned int WIDTH = 960;
constexpr long unsigned int HEIGHT = 960;
#endif
constexpr bool OFFSCREEN = false;
const unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT));
#ifndef __EMSCRIPTEN__
constexpr const char* OUTPUT_FILENAME = "shader-demo.mkv";
#endif
// vertex position, color
static const float vertices[] = {
// x y z
-1.0f, -1.0f, -0.0f, 1.0f, 1.0f, -0.0f, -1.0f, 1.0f, -0.0f, 1.0f, -1.0f, -0.0f };
static const unsigned int indices[] = {
// 2---,1
// | .' |
// 0'---3
0, 1, 2, 0, 3, 1 };
//easing function for the bungee zoom
static float easeInOutQuint(float x) {
return x < 0.5f ? 16.0f * x * x * x * x * x : 1.0f - std::pow(-2.0f * x + 2.0f, 5.0f) / 2.0f;
}
using namespace cv::v4d;
struct Params {
/* Mandelbrot control parameters */
int glowKernelSize_ = std::max(int(DIAG / 200 % 2 == 0 ? DIAG / 200 + 1 : DIAG / 200), 1);
// Red, green, blue and alpha. All from 0.0f to 1.0f
float baseColorVal_[4] = {0.2, 0.6, 1.0, 1.0};
//contrast boost
int contrastBoost_ = 50; //0.0-255
//max fractal iterations
int maxIterations_ = 1000;
//center x coordinate
float centerX_ = -0.119609;
//center y coordinate
float centerY_ = 0.13262;
float zoomFactor_ = 1.0;
float currentZoom_ = 1.0;
float zoomIncr_ = 0.99;
bool manualNavigation_ = false;
} params_;
struct Handles {
/* GL uniform handles */
GLint baseColorHdl_;
GLint contrastBoostHdl_;
GLint maxIterationsHdl_;
GLint centerXHdl_;
GLint centerYHdl_;
GLint currentZoomHdl_;
GLint resolutionHdl_;
/* Shader program handle */
GLuint shaderHdl_;
/* Object handles */
GLuint vao_;
GLuint vbo_, ebo_;
} handles_;
struct Cache {
cv::UMat down;
cv::UMat up;
cv::UMat dst16;
} cache_;
cv::Size sz_;
#ifndef __EMSCRIPTEN__
cv::bitwise_not(src, dst);
cv::resize(dst, cache.down, cv::Size(), 0.5, 0.5);
cv::resize(cache.blur, cache.up, src.size());
cv::multiply(dst, cache.up, cache.dst16, 1, CV_16U);
cv::bitwise_not(dst, dst);
}
#endif
//Load objects and buffers
static void load_buffers(Handles& handles) {
GL_CHECK(glGenVertexArrays(1, &handles.vao_));
GL_CHECK(glBindVertexArray(handles.vao_));
GL_CHECK(glGenBuffers(1, &handles.vbo_));
GL_CHECK(glGenBuffers(1, &handles.ebo_));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, handles.vbo_));
GL_CHECK(glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW));
GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handles.ebo_));
GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
GL_CHECK(glEnableVertexAttribArray(0));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
GL_CHECK(glBindVertexArray(0));
}
//mandelbrot shader code adapted from my own project: https://github.com/kallaballa/FractalDive#after
static GLuint load_shader() {
#if !defined(__EMSCRIPTEN__) && !defined(OPENCV_V4D_USE_ES3)
const string shaderVersion = "330";
#else
const string shaderVersion = "300 es";
#endif
const string vert =
" #version " + shaderVersion
+ R"(
in vec4 position;
void main()
{
gl_Position = vec4(position.xyz, 1.0);
})";
const string frag =
" #version " + shaderVersion
+ R"(
precision lowp float;
out vec4 outColor;
uniform vec4 base_color;
uniform int contrast_boost;
uniform int max_iterations;
uniform float current_zoom;
uniform float center_y;
uniform float center_x;
uniform vec2 resolution;
int get_iterations()
{
float pointr = (((gl_FragCoord.x / resolution[1]) - 0.5f) * current_zoom + center_x) * 5.0f;
float pointi = (((gl_FragCoord.y / resolution[1]) - 0.5f) * current_zoom + center_y) * 5.0f;
const float four = 4.0f;
int iterations = 0;
float zi = 0.0f;
float zr = 0.0f;
float zrsqr = 0.0f;
float zisqr = 0.0f;
while (iterations < max_iterations && zrsqr + zisqr < four) {
//equals following line as a consequence of binomial expansion: zi = (((zr + zi)*(zr + zi)) - zrsqr) - zisqr
zi = (zr + zr) * zi;
zi += pointi;
zr = (zrsqr - zisqr) + pointr;
zrsqr = zr * zr;
zisqr = zi * zi;
++iterations;
}
return iterations;
}
void determine_color()
{
int iter = get_iterations();
if (iter < max_iterations) {
float iterations = float(iter) / float(max_iterations);
float cb = float(contrast_boost);
outColor = vec4(base_color[0] * iterations * cb, base_color[1] * iterations * cb, base_color[2] * iterations * cb, base_color[3]);
} else {
outColor = vec4(0,0,0,0);
}
}
void main()
{
determine_color();
})";
}
//Initialize shaders, objects, buffers and uniforms
GL_CHECK(glEnable(GL_BLEND));
GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
handles.shaderHdl_ = load_shader();
load_buffers(handles);
handles.baseColorHdl_ = glGetUniformLocation(handles.shaderHdl_, "base_color");
handles.contrastBoostHdl_ = glGetUniformLocation(handles.shaderHdl_, "contrast_boost");
handles.maxIterationsHdl_ = glGetUniformLocation(handles.shaderHdl_, "max_iterations");
handles.currentZoomHdl_ = glGetUniformLocation(handles.shaderHdl_, "current_zoom");
handles.centerXHdl_ = glGetUniformLocation(handles.shaderHdl_, "center_x");
handles.centerYHdl_ = glGetUniformLocation(handles.shaderHdl_, "center_y");
handles.resolutionHdl_ = glGetUniformLocation(handles.shaderHdl_, "resolution");
}
//Free OpenGL resources
static void destroy_scene(Handles& handles) {
glDeleteShader(handles.shaderHdl_);
glDeleteBuffers(1, &handles.vbo_);
glDeleteBuffers(1, &handles.ebo_);
glDeleteVertexArrays(1, &handles.vao_);
}
//Render the mandelbrot fractal on top of a video
//bungee zoom
if (params.currentZoom_ >= 1) {
params.zoomIncr_ = -0.01;
params.zoomIncr_ = +0.01;
}
GL_CHECK(glUseProgram(handles.shaderHdl_));
GL_CHECK(glUniform4f(handles.baseColorHdl_, params.baseColorVal_[0], params.baseColorVal_[1], params.baseColorVal_[2], params.baseColorVal_[3]));
if (!params.manualNavigation_) {
} else {
}
GL_CHECK(glUniform2fv(handles.resolutionHdl_, 1, res));
GL_CHECK(glBindVertexArray(handles.vao_));
GL_CHECK(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0));
}
public:
window->imgui([](cv::Ptr<V4D> win, ImGuiContext* ctx, Params& params) {
CV_UNUSED(win);
using namespace ImGui;
SetCurrentContext(ctx);
Begin("Fractal");
Text("Navigation");
SliderInt("Iterations", ¶ms.maxIterations_, 3, 50000);
params.manualNavigation_ = true;
params.manualNavigation_ = true;
params.manualNavigation_ = true;
#ifndef __EMSCRIPTEN__
Text("Glow");
SliderInt("Kernel Size", ¶ms.glowKernelSize_, 1, 127);
#endif
Text("Color");
ColorPicker4("Color", params.baseColorVal_);
SliderInt("Contrast boost", ¶ms.contrastBoost_, 1, 255);
End();
}, params_);
}
sz_ = window->fbSize();
window->gl([](const cv::Size &sz, Handles& handles) {
init_scene(sz, handles);
}, sz_, handles_);
}
window->capture();
window->gl([](const cv::Size &sz, Params& params, Handles& handles) {
render_scene(sz, params, handles);
}, sz_, params_, handles_);
#ifndef __EMSCRIPTEN__
window->fb([](cv::UMat& framebuffer, const Params& params, Cache& cache) {
glow_effect(framebuffer, framebuffer, params.glowKernelSize_, cache);
}, params_, cache_);
#endif
window->write();
}
window->gl([](Handles& handles) {
destroy_scene(handles);
}, handles_);
}
};
int main(int argc, char** argv) {
#ifndef __EMSCRIPTEN__
if (argc != 2) {
cerr << "Usage: shader-demo <video-file>" << endl;
exit(1);
}
#else
CV_UNUSED(args);
CV_UNUSED(argv);
#endif
try {
cv::Ptr<V4D> window = V4D::make(WIDTH, HEIGHT, "Mandelbrot Shader Demo", IMGUI, OFFSCREEN);
#ifndef __EMSCRIPTEN__
auto src = makeCaptureSource(window, argv[1]);
window->setSource(src);
window->setSink(sink);
#else
auto src = makeCaptureSource(window);
window->setSource(src);
#endif
window->run<ShaderDemoPlan>(0);
} catch (std::exception& ex) {
cerr << "Exception: " << ex.what() << endl;
}
return 0;
}
static Scalar_< double > all(double v0)
returns a scalar with all elements set to v0
Definition: mat.hpp:2432
MatSize size
dimensional size of the matrix; accessible in various formats
Definition: mat.hpp:2643
Definition: v4d.hpp:68
void bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray())
Inverts every bit of an array.
void divide(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
Performs per-element division of two arrays or a scalar by an array.
void multiply(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
Calculates the per-element scaled product of two arrays.
void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT)
Blurs an image using the normalized box filter.
void boxFilter(InputArray src, OutputArray dst, int ddepth, Size ksize, Point anchor=Point(-1,-1), bool normalize=true, int borderType=BORDER_DEFAULT)
Blurs an image using the box filter.
void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
Resizes an image.
PyParams params(const std::string &tag, const std::string &model, const std::string &weights, const std::string &device)
Net::Result infer(cv::GOpaque< cv::Rect > roi, T in)
Calculates response for the specified network (template parameter) for the specified region in the so...
Definition: infer.hpp:474
Definition: backend.hpp:15
cv::Ptr< Sink > makeWriterSink(cv::Ptr< V4D > window, const string &outputFilename, const float fps, const cv::Size &frameSize)
cv::Ptr< Source > makeCaptureSource(cv::Ptr< V4D > window, const string &inputFilename)
unsigned int initShader(const char *vShader, const char *fShader, const char *outputAttributeName)