|
|
|
@ -21,12 +21,14 @@ constexpr bool OFFSCREEN = false; |
|
|
|
|
constexpr const char* OUTPUT_FILENAME = "video-demo.mkv"; |
|
|
|
|
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::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 ibo_cube_elements; |
|
|
|
@ -38,10 +40,7 @@ GLuint init_shader(const char* vShader, const char* fShader, const char* outputA |
|
|
|
|
struct Shader { |
|
|
|
|
GLenum type; |
|
|
|
|
const char* source; |
|
|
|
|
} shaders[2] = { |
|
|
|
|
{ GL_VERTEX_SHADER, vShader }, |
|
|
|
|
{ GL_FRAGMENT_SHADER, fShader } |
|
|
|
|
}; |
|
|
|
|
} shaders[2] = { { GL_VERTEX_SHADER, vShader }, { GL_FRAGMENT_SHADER, fShader } }; |
|
|
|
|
|
|
|
|
|
GLuint program = glCreateProgram(); |
|
|
|
|
|
|
|
|
@ -102,7 +101,9 @@ void load_shader(){ |
|
|
|
|
const string shaderVersion = "300 es"; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
const string vert = " #version " + shaderVersion + R"( |
|
|
|
|
const string vert = |
|
|
|
|
" #version " + shaderVersion |
|
|
|
|
+ R"( |
|
|
|
|
attribute vec3 coord3d; |
|
|
|
|
attribute vec3 v_color; |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
void main(void) { |
|
|
|
@ -130,60 +133,39 @@ void load_shader(){ |
|
|
|
|
|
|
|
|
|
program = init_shader(vert.c_str(), frag.c_str(), "fragColor"); |
|
|
|
|
} |
|
|
|
|
int init_resources() |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
int init_resources() { |
|
|
|
|
GLfloat cube_vertices[] = { |
|
|
|
|
// 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, 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, 1.0, 1.0, -1.0, -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
|
|
|
|
|
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, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, |
|
|
|
|
// back colors
|
|
|
|
|
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, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 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
|
|
|
|
|
0, 1, 2, |
|
|
|
|
2, 3, 0, |
|
|
|
|
0, 1, 2, 2, 3, 0, |
|
|
|
|
// top
|
|
|
|
|
1, 5, 6, |
|
|
|
|
6, 2, 1, |
|
|
|
|
1, 5, 6, 6, 2, 1, |
|
|
|
|
// back
|
|
|
|
|
7, 6, 5, |
|
|
|
|
5, 4, 7, |
|
|
|
|
7, 6, 5, 5, 4, 7, |
|
|
|
|
// bottom
|
|
|
|
|
4, 0, 3, |
|
|
|
|
3, 7, 4, |
|
|
|
|
4, 0, 3, 3, 7, 4, |
|
|
|
|
// left
|
|
|
|
|
4, 5, 1, |
|
|
|
|
1, 0, 4, |
|
|
|
|
4, 5, 1, 1, 0, 4, |
|
|
|
|
// right
|
|
|
|
|
3, 2, 6, |
|
|
|
|
6, 7, 3, |
|
|
|
|
}; |
|
|
|
|
3, 2, 6, 6, 7, 3, }; |
|
|
|
|
glGenBuffers(1, &ibo_cube_elements); |
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements); |
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_elements), cube_elements, GL_STATIC_DRAW); |
|
|
|
@ -232,7 +214,8 @@ void render_scene(const cv::Size& sz) { |
|
|
|
|
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 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::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; |
|
|
|
@ -245,8 +228,7 @@ void render_scene(const cv::Size& sz) { |
|
|
|
|
glEnableVertexAttribArray(attribute_coord3d); |
|
|
|
|
// Describe our vertices array to OpenGL (it can't guess its format automatically)
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices); |
|
|
|
|
glVertexAttribPointer( |
|
|
|
|
attribute_coord3d, // attribute
|
|
|
|
|
glVertexAttribPointer(attribute_coord3d, // attribute
|
|
|
|
|
3, // number of elements per vertex, here (x,y,z)
|
|
|
|
|
GL_FLOAT, // the type of each element
|
|
|
|
|
GL_FALSE, // take our values as-is
|
|
|
|
@ -256,8 +238,7 @@ void render_scene(const cv::Size& sz) { |
|
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(attribute_v_color); |
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors); |
|
|
|
|
glVertexAttribPointer( |
|
|
|
|
attribute_v_color, // attribute
|
|
|
|
|
glVertexAttribPointer(attribute_v_color, // attribute
|
|
|
|
|
3, // number of elements per vertex, here (R,G,B)
|
|
|
|
|
GL_FLOAT, // the type of each element
|
|
|
|
|
GL_FALSE, // take our values as-is
|
|
|
|
@ -267,7 +248,8 @@ void render_scene(const cv::Size& sz) { |
|
|
|
|
|
|
|
|
|
/* Push each element in buffer_vertices to the vertex shader */ |
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements); |
|
|
|
|
int size; glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); |
|
|
|
|
int size; |
|
|
|
|
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); |
|
|
|
|
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0); |
|
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(attribute_coord3d); |
|
|
|
@ -284,7 +266,8 @@ void glow_effect(const cv::UMat &src, cv::UMat &dst, const int ksize) { |
|
|
|
|
//Resize for some extra performance
|
|
|
|
|
cv::resize(dst, resize, cv::Size(), 0.5, 0.5); |
|
|
|
|
//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
|
|
|
|
|
cv::resize(resize, blur, src.size()); |
|
|
|
|
|
|
|
|
@ -338,7 +321,8 @@ int main(int argc, char **argv) { |
|
|
|
|
Source src = makeCaptureSource(argv[1]); |
|
|
|
|
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->gl(init_scene); |
|
|
|
|