libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
gl_cv.cpp
Go to the documentation of this file.
1/**
2 * @file gl_cv.cpp
3 * @brief Implementation of mx::MXCapture (OpenGL + OpenCV variant).
4 */
5#include"config.h"
6#include"gl_cv.hpp"
7#include<filesystem>
8
9namespace mx {
10
11 bool MXCapture::open(const std::string &filename) {
12 return cap.open(filename);
13 }
14
15 bool MXCapture::open(int id, int mode) {
16 if(mode == 0)
17 mode = cv::CAP_V4L2;
18 if(cap.open(id, mode)) {
19 cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));
20 return true;
21 }
22 return false;
23 }
24
26 cap.release();
27 }
28
29 bool MXCapture::createImage(gl::GLWindow *window, size_t width, size_t height, const std::string &vert, const std::string &frag) {
30 shader.reset (new gl::ShaderProgram());
31 if(!shader->loadProgram(vert, frag)) {
32 throw mx::Exception(">> MXCapture: Could not load shader.\n");
33 }
34 this->window = window;
35 sprite.initSize(window->w, window->h);
36 sprite.setShader(shader.get());
37 cv::Mat mat = cv::Mat::zeros(width, height, CV_8UC4);
38 GLuint texture = gl::createTexture(mat.ptr(), width, height);
39 sprite.initWithTexture(shader.get(), texture, 0.0f, 0.0f,
40 static_cast<int>(width), static_cast<int>(height));
41 return true;
42 }
43
44 bool MXCapture::reload(const std::string &vert, const std::string &frag) {
45 shader.reset(new gl::ShaderProgram());
46 if(!shader->loadProgram(vert, frag)) {
47 throw mx::Exception(">> MXCapture: could not load shader: " + vert + "/" + frag);
48 }
49 return false;
50 }
51
52 bool MXCapture::read(cv::Mat &frame) {
53 return cap.read(frame);
54 }
55
57 if(!cap.read(frame)) {
58 return false;
59 }
60 cv::Mat rgba;
61 cv::cvtColor(frame, rgba, cv::COLOR_BGR2RGBA);
62 cv::flip(rgba, rgba, 0);
63 sprite.updateTexture(rgba.ptr(),rgba.cols, rgba.rows);
64 return true;
65 }
66
67 void MXCapture::draw(int x, int y, int width, int height) {
68 if (window) {
69 sprite.initSize(static_cast<float>(window->w), static_cast<float>(window->h));
70 }
71 sprite.draw(x, y, width, height);
72 }
73
74 void MXCapture::draw(int x, int y) {
75 if (window) {
76 sprite.initSize(static_cast<float>(window->w), static_cast<float>(window->h));
77 }
78 sprite.draw(x, y);
79 }
80
81 void MXCapture::set(unsigned int option, double value) {
82 cap.set(option, value);
83 }
84
85 double MXCapture::get(unsigned int option) {
86 return cap.get(option);
87 }
88
89}
SDL2 + OpenGL/ES window with event loop and console support.
Definition gl.hpp:411
GLSL shader program wrapper (shared-ownership).
Definition gl.hpp:57
General-purpose exception class for the mx library.
Definition exception.hpp:20
void close()
Close the video source.
Definition gl_cv.cpp:25
double get(unsigned int option)
Query a VideoCapture property.
Definition gl_cv.cpp:85
void set(unsigned int option, double value)
Set a VideoCapture property.
Definition gl_cv.cpp:81
bool reload(const std::string &vert, const std::string &frag)
Replace the shader pair without reopening the source.
Definition gl_cv.cpp:44
bool open(const std::string &filename)
Open a video file for capture.
Definition gl_cv.cpp:11
cv::Mat frame
Most recent decoded frame.
Definition gl_cv.hpp:133
gl::GLWindow * window
Parent GL window.
Definition gl_cv.hpp:131
void draw(int x, int y, int width, int height)
Draw the most recently captured frame.
Definition gl_cv.cpp:67
cv::VideoCapture cap
OpenCV capture device.
Definition gl_cv.hpp:132
gl::GLSprite sprite
Rendering sprite.
Definition gl_cv.hpp:129
std::unique_ptr< gl::ShaderProgram > shader
Active shader program.
Definition gl_cv.hpp:130
bool read()
Grab and decode the next frame from the source.
Definition gl_cv.cpp:56
bool createImage(gl::GLWindow *window, size_t width, size_t height, const std::string &vert, const std::string &frag)
Create the backing sprite/texture for displaying frames.
Definition gl_cv.cpp:29
unsigned int GLuint
Definition console.hpp:38
OpenCV video-capture integration for the OpenGL backend.
GLuint createTexture(SDL_Surface *surface, bool flip)
Create a new GL texture from an SDL_Surface.
Definition gl.cpp:743
Definition cfg.cpp:11