pull/3471/head
kallaballa 2 years ago
parent a3c396070d
commit ad4eb8f85b
  1. 270
      modules/v4d/samples/video-demo.cpp

@ -21,12 +21,14 @@ constexpr bool OFFSCREEN = false;
constexpr const char* OUTPUT_FILENAME = "video-demo.mkv"; constexpr const char* OUTPUT_FILENAME = "video-demo.mkv";
constexpr unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT)); constexpr unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT));
constexpr int GLOW_KERNEL_SIZE = std::max(int(DIAG / 138 % 2 == 0 ? DIAG / 138 + 1 : DIAG / 138), 1); constexpr int GLOW_KERNEL_SIZE = std::max(int(DIAG / 138 % 2 == 0 ? DIAG / 138 + 1 : DIAG / 138),
1);
using std::cerr; using std::cerr;
using std::endl; using std::endl;
static cv::Ptr<cv::viz::V4D> v4d = cv::viz::V4D::make(cv::Size(WIDTH, HEIGHT), cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Video Demo"); static cv::Ptr<cv::viz::V4D> v4d = cv::viz::V4D::make(cv::Size(WIDTH, HEIGHT),
cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Video Demo");
GLuint vbo_cube_vertices, vbo_cube_colors; GLuint vbo_cube_vertices, vbo_cube_colors;
GLuint ibo_cube_elements; GLuint ibo_cube_elements;
@ -36,36 +38,33 @@ GLint uniform_mvp;
GLuint init_shader(const char* vShader, const char* fShader, const char* outputAttributeName) { GLuint init_shader(const char* vShader, const char* fShader, const char* outputAttributeName) {
struct Shader { struct Shader {
GLenum type; GLenum type;
const char* source; const char* source;
} shaders[2] = { } shaders[2] = { { GL_VERTEX_SHADER, vShader }, { GL_FRAGMENT_SHADER, fShader } };
{ GL_VERTEX_SHADER, vShader },
{ GL_FRAGMENT_SHADER, fShader }
};
GLuint program = glCreateProgram(); GLuint program = glCreateProgram();
for ( int i = 0; i < 2; ++i ) { for (int i = 0; i < 2; ++i) {
Shader& s = shaders[i]; Shader& s = shaders[i];
GLuint shader = glCreateShader( s.type ); GLuint shader = glCreateShader(s.type);
glShaderSource( shader, 1, (const GLchar**) &s.source, NULL ); glShaderSource(shader, 1, (const GLchar**) &s.source, NULL);
glCompileShader( shader ); glCompileShader(shader);
GLint compiled; GLint compiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled ); glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if ( !compiled ) { if (!compiled) {
std::cerr << " failed to compile:" << std::endl; std::cerr << " failed to compile:" << std::endl;
GLint logSize; GLint logSize;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize ); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize]; char* logMsg = new char[logSize];
glGetShaderInfoLog( shader, logSize, NULL, logMsg ); glGetShaderInfoLog(shader, logSize, NULL, logMsg);
std::cerr << logMsg << std::endl; std::cerr << logMsg << std::endl;
delete [] logMsg; delete[] logMsg;
exit( EXIT_FAILURE ); exit (EXIT_FAILURE);
} }
glAttachShader( program, shader ); glAttachShader(program, shader);
} }
#ifndef OPENCV_V4D_ES_VERSION #ifndef OPENCV_V4D_ES_VERSION
/* Link output */ /* Link output */
@ -74,18 +73,18 @@ GLuint init_shader(const char* vShader, const char* fShader, const char* outputA
/* link and error check */ /* link and error check */
glLinkProgram(program); glLinkProgram(program);
GLint linked; GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked ); glGetProgramiv(program, GL_LINK_STATUS, &linked);
if ( !linked ) { if (!linked) {
std::cerr << "Shader program failed to link" << std::endl; std::cerr << "Shader program failed to link" << std::endl;
GLint logSize; GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize]; char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg ); glGetProgramInfoLog(program, logSize, NULL, logMsg);
std::cerr << logMsg << std::endl; std::cerr << logMsg << std::endl;
delete [] logMsg; delete[] logMsg;
exit( EXIT_FAILURE ); exit (EXIT_FAILURE);
} }
/* use program object */ /* use program object */
@ -95,14 +94,16 @@ GLuint init_shader(const char* vShader, const char* fShader, const char* outputA
} }
//mandelbrot shader code adapted from my own project: https://github.com/kallaballa/FractalDive#after //mandelbrot shader code adapted from my own project: https://github.com/kallaballa/FractalDive#after
void load_shader(){ void load_shader() {
#ifndef OPENCV_V4D_ES_VERSION #ifndef OPENCV_V4D_ES_VERSION
const string shaderVersion = "330"; const string shaderVersion = "330";
#else #else
const string shaderVersion = "300 es"; const string shaderVersion = "300 es";
#endif #endif
const string vert = " #version " + shaderVersion + R"( const string vert =
" #version " + shaderVersion
+ R"(
attribute vec3 coord3d; attribute vec3 coord3d;
attribute vec3 v_color; attribute vec3 v_color;
uniform mat4 mvp; uniform mat4 mvp;
@ -114,7 +115,9 @@ void load_shader(){
} }
)"; )";
const string frag = " #version " + shaderVersion + R"( const string frag =
" #version " + shaderVersion
+ R"(
varying vec3 f_color; varying vec3 f_color;
void main(void) { void main(void) {
@ -128,94 +131,73 @@ void load_shader(){
cerr << "##### Fragment Shader #####" << endl; cerr << "##### Fragment Shader #####" << endl;
cerr << frag << endl; cerr << frag << endl;
program = init_shader(vert.c_str(), frag.c_str(), "fragColor"); program = init_shader(vert.c_str(), frag.c_str(), "fragColor");
} }
int init_resources()
{ int init_resources() {
GLfloat cube_vertices[] = { GLfloat cube_vertices[] = {
// front // front
-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0,
1.0, -1.0, 1.0, // back
1.0, 1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, };
-1.0, 1.0, 1.0, glGenBuffers(1, &vbo_cube_vertices);
// back glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
-1.0, -1.0, -1.0, glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
1.0, -1.0, -1.0,
1.0, 1.0, -1.0, GLfloat cube_colors[] = {
-1.0, 1.0, -1.0,
};
glGenBuffers(1, &vbo_cube_vertices);
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
GLfloat cube_colors[] = {
// front colors // front colors
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0,
0.0, 1.0, 0.0, // back colors
0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, };
1.0, 1.0, 1.0, glGenBuffers(1, &vbo_cube_colors);
// back colors glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors);
1.0, 0.0, 0.0, glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);
0.0, 1.0, 0.0,
0.0, 0.0, 1.0, GLushort cube_elements[] = {
1.0, 1.0, 1.0,
};
glGenBuffers(1, &vbo_cube_colors);
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);
GLushort cube_elements[] = {
// front // front
0, 1, 2, 0, 1, 2, 2, 3, 0,
2, 3, 0, // top
// top 1, 5, 6, 6, 2, 1,
1, 5, 6, // back
6, 2, 1, 7, 6, 5, 5, 4, 7,
// back // bottom
7, 6, 5, 4, 0, 3, 3, 7, 4,
5, 4, 7, // left
// bottom 4, 5, 1, 1, 0, 4,
4, 0, 3, // right
3, 7, 4, 3, 2, 6, 6, 7, 3, };
// left glGenBuffers(1, &ibo_cube_elements);
4, 5, 1, glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements);
1, 0, 4, glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
// right
3, 2, 6, GLint link_ok = GL_FALSE;
6, 7, 3,
}; GLuint vs, fs;
glGenBuffers(1, &ibo_cube_elements);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements); load_shader();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW);
const char* attribute_name;
GLint link_ok = GL_FALSE; attribute_name = "coord3d";
attribute_coord3d = glGetAttribLocation(program, attribute_name);
GLuint vs, fs; if (attribute_coord3d == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
load_shader(); return 0;
}
const char* attribute_name; attribute_name = "v_color";
attribute_name = "coord3d"; attribute_v_color = glGetAttribLocation(program, attribute_name);
attribute_coord3d = glGetAttribLocation(program, attribute_name); if (attribute_v_color == -1) {
if (attribute_coord3d == -1) { fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
fprintf(stderr, "Could not bind attribute %s\n", attribute_name); return 0;
return 0; }
} const char* uniform_name;
attribute_name = "v_color"; uniform_name = "mvp";
attribute_v_color = glGetAttribLocation(program, attribute_name); uniform_mvp = glGetUniformLocation(program, uniform_name);
if (attribute_v_color == -1) { if (uniform_mvp == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name); fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
return 0; return 0;
} }
const char* uniform_name;
uniform_name = "mvp";
uniform_mvp = glGetUniformLocation(program, uniform_name);
if (uniform_mvp == -1) {
fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
return 0;
}
return 1; return 1;
} }
void init_scene(const cv::Size& sz) { void init_scene(const cv::Size& sz) {
@ -232,12 +214,13 @@ void render_scene(const cv::Size& sz) {
glm::mat4 anim = glm::rotate(glm::mat4(1.0f), glm::radians(angle), axis_y); glm::mat4 anim = glm::rotate(glm::mat4(1.0f), glm::radians(angle), axis_y);
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -4.0)); glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -4.0));
glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 0.0, -4.0), glm::vec3(0.0, 1.0, 0.0)); glm::mat4 view = glm::lookAt(glm::vec3(0.0, 2.0, 0.0), glm::vec3(0.0, 0.0, -4.0),
glm::mat4 projection = glm::perspective(45.0f, 1.0f*WIDTH/HEIGHT, 0.1f, 10.0f); glm::vec3(0.0, 1.0, 0.0));
glm::mat4 projection = glm::perspective(45.0f, 1.0f * WIDTH / HEIGHT, 0.1f, 10.0f);
glm::mat4 mvp = projection * view * model * anim; glm::mat4 mvp = projection * view * model * anim;
glClear(GL_DEPTH_BUFFER_BIT); glClear (GL_DEPTH_BUFFER_BIT);
glUseProgram(program); glUseProgram(program);
glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp)); glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
@ -245,36 +228,35 @@ void render_scene(const cv::Size& sz) {
glEnableVertexAttribArray(attribute_coord3d); glEnableVertexAttribArray(attribute_coord3d);
// Describe our vertices array to OpenGL (it can't guess its format automatically) // Describe our vertices array to OpenGL (it can't guess its format automatically)
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices); glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
glVertexAttribPointer( glVertexAttribPointer(attribute_coord3d, // attribute
attribute_coord3d, // attribute 3, // number of elements per vertex, here (x,y,z)
3, // number of elements per vertex, here (x,y,z) GL_FLOAT, // the type of each element
GL_FLOAT, // the type of each element GL_FALSE, // take our values as-is
GL_FALSE, // take our values as-is 0, // no extra data between each position
0, // no extra data between each position 0 // offset of first element
0 // offset of first element );
);
glEnableVertexAttribArray(attribute_v_color); glEnableVertexAttribArray(attribute_v_color);
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors); glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors);
glVertexAttribPointer( glVertexAttribPointer(attribute_v_color, // attribute
attribute_v_color, // attribute 3, // number of elements per vertex, here (R,G,B)
3, // number of elements per vertex, here (R,G,B) GL_FLOAT, // the type of each element
GL_FLOAT, // the type of each element GL_FALSE, // take our values as-is
GL_FALSE, // take our values as-is 0, // no extra data between each position
0, // no extra data between each position 0 // offset of first element
0 // offset of first element );
);
/* Push each element in buffer_vertices to the vertex shader */ /* Push each element in buffer_vertices to the vertex shader */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements);
int size; glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); int size;
glDrawElements(GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0); glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(attribute_coord3d); glDisableVertexAttribArray(attribute_coord3d);
glDisableVertexAttribArray(attribute_v_color); glDisableVertexAttribArray(attribute_v_color);
} }
void glow_effect(const cv::UMat &src, cv::UMat &dst, const int ksize) { void glow_effect(const cv::UMat& src, cv::UMat& dst, const int ksize) {
static cv::UMat resize; static cv::UMat resize;
static cv::UMat blur; static cv::UMat blur;
static cv::UMat dst16; static cv::UMat dst16;
@ -284,7 +266,8 @@ void glow_effect(const cv::UMat &src, cv::UMat &dst, const int ksize) {
//Resize for some extra performance //Resize for some extra performance
cv::resize(dst, resize, cv::Size(), 0.5, 0.5); cv::resize(dst, resize, cv::Size(), 0.5, 0.5);
//Cheap blur //Cheap blur
cv::boxFilter(resize, resize, -1, cv::Size(ksize, ksize), cv::Point(-1,-1), true, cv::BORDER_REPLICATE); cv::boxFilter(resize, resize, -1, cv::Size(ksize, ksize), cv::Point(-1, -1), true,
cv::BORDER_REPLICATE);
//Back to original size //Back to original size
cv::resize(resize, blur, src.size()); cv::resize(resize, blur, src.size());
@ -299,7 +282,7 @@ void glow_effect(const cv::UMat &src, cv::UMat &dst, const int ksize) {
bool iteration() { bool iteration() {
using namespace cv::viz; using namespace cv::viz;
if(!v4d->capture()) if (!v4d->capture())
return false; return false;
//Render using OpenGL //Render using OpenGL
@ -322,23 +305,24 @@ bool iteration() {
return true; return true;
} }
int main(int argc, char **argv) { int main(int argc, char** argv) {
using namespace cv::viz; using namespace cv::viz;
if(argc != 2) { if (argc != 2) {
cerr << "Usage: video-demo <video-file>" << endl; cerr << "Usage: video-demo <video-file>" << endl;
exit(1); exit(1);
} }
printSystemInfo(); printSystemInfo();
if(!v4d->isOffscreen()) if (!v4d->isOffscreen())
v4d->setVisible(true); v4d->setVisible(true);
Source src = makeCaptureSource(argv[1]); Source src = makeCaptureSource(argv[1]);
v4d->setSource(src); v4d->setSource(src);
Sink sink = makeWriterSink(OUTPUT_FILENAME, cv::VideoWriter::fourcc('V', 'P', '9', '0'), src.fps(), cv::Size(WIDTH, HEIGHT)); Sink sink = makeWriterSink(OUTPUT_FILENAME, cv::VideoWriter::fourcc('V', 'P', '9', '0'),
src.fps(), cv::Size(WIDTH, HEIGHT));
v4d->setSink(sink); v4d->setSink(sink);
v4d->gl(init_scene); v4d->gl(init_scene);

Loading…
Cancel
Save