libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
gl_cv.hpp
Go to the documentation of this file.
1/**
2 * @file gl_cv.hpp
3 * @brief OpenCV video-capture integration for the OpenGL backend.
4 *
5 * MXCapture wraps cv::VideoCapture and renders each captured frame into
6 * a gl::GLSprite using a user-supplied GLSL shader pair.
7 */
8#ifndef __GL_OPENCV__H_
9#define __GL_OPENCV__H_
10
11#include<opencv2/opencv.hpp>
12#include"gl.hpp"
13#include<memory>
14
15namespace mx {
16
17/**
18 * @class MXCapture
19 * @ingroup mxgl_cv_module
20 * @brief OpenCV video capture source that renders via an OpenGL sprite.
21 *
22 * Opens a video file or camera device, reads frames frame-by-frame, and
23 * uploads each frame to a gl::GLSprite texture for display inside a
24 * gl::GLWindow with a custom GLSL shader.
25 */
26 class MXCapture {
27 public:
28 /** @brief Default constructor. */
29 MXCapture() = default;
30 /** @brief Destructor. */
31 ~MXCapture() = default;
32 MXCapture &operator=(const MXCapture &) = delete;
34 MXCapture(const MXCapture &) = delete;
35 MXCapture(MXCapture &&) = delete;
36
37 /**
38 * @brief Open a video file for capture.
39 * @param filename Path to the video file.
40 * @return @c true on success.
41 */
42 bool open(const std::string &filename);
43
44 /**
45 * @brief Open a camera device for capture.
46 * @param id Camera device index.
47 * @param mode Optional backend hint (0 = auto).
48 * @return @c true on success.
49 */
50 bool open(int id, int mode=0);
51
52 /** @brief Close the video source. */
53 void close();
54
55 /**
56 * @brief Check whether the capture source is open.
57 * @return @c true if the VideoCapture is opened.
58 */
59 bool is_open() const { return cap.isOpened(); }
60
61 /**
62 * @brief Create the backing sprite/texture for displaying frames.
63 * @param window OpenGL window context.
64 * @param width Frame display width.
65 * @param height Frame display height.
66 * @param vert Path to the vertex shader GLSL file.
67 * @param frag Path to the fragment shader GLSL file.
68 * @return @c true on success.
69 */
70 bool createImage(gl::GLWindow *window, size_t width, size_t height, const std::string &vert, const std::string &frag);
71
72 /**
73 * @brief Access the internal sprite.
74 * @return Pointer to the backing gl::GLSprite.
75 */
77
78 /**
79 * @brief Draw the most recently captured frame.
80 * @param x Destination X position.
81 * @param y Destination Y position.
82 * @param width Display width.
83 * @param height Display height.
84 */
85 void draw(int x, int y, int width, int height);
86
87 /**
88 * @brief Draw using the sprite's native dimensions.
89 * @param x Destination X position.
90 * @param y Destination Y position.
91 */
92 void draw(int x, int y);
93
94 /**
95 * @brief Replace the shader pair without reopening the source.
96 * @param vert New vertex shader path.
97 * @param frag New fragment shader path.
98 * @return @c true on success.
99 */
100 bool reload(const std::string &vert, const std::string &frag);
101
102 /**
103 * @brief Grab and decode the next frame from the source.
104 * @return @c true if a new frame was available.
105 */
106 bool read();
107
108 /**
109 * @brief Grab and decode the next frame into a cv::Mat.
110 * @param frame Output matrix.
111 * @return @c true if a new frame was available.
112 */
113 bool read(cv::Mat &frame);
114
115 /**
116 * @brief Set a VideoCapture property.
117 * @param option cv::VideoCaptureProperties constant.
118 * @param value Desired value.
119 */
120 void set(unsigned int option, double value);
121
122 /**
123 * @brief Query a VideoCapture property.
124 * @param option cv::VideoCaptureProperties constant.
125 * @return Current property value.
126 */
127 double get(unsigned int option);
128 private:
129 gl::GLSprite sprite; ///< Rendering sprite.
130 std::unique_ptr<gl::ShaderProgram> shader; ///< Active shader program.
131 gl::GLWindow *window = nullptr; ///< Parent GL window.
132 cv::VideoCapture cap; ///< OpenCV capture device.
133 cv::Mat frame; ///< Most recent decoded frame.
134 };
135}
136
137#endif
A textured screen-space quad drawn with an OpenGL shader.
Definition gl.hpp:255
SDL2 + OpenGL/ES window with event loop and console support.
Definition gl.hpp:411
void close()
Close the video source.
Definition gl_cv.cpp:25
MXCapture()=default
Default constructor.
MXCapture & operator=(const MXCapture &)=delete
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
MXCapture & operator=(MXCapture &&)=delete
MXCapture(const MXCapture &)=delete
~MXCapture()=default
Destructor.
bool open(const std::string &filename)
Open a video file for capture.
Definition gl_cv.cpp:11
gl::GLSprite * getSprite()
Access the internal sprite.
Definition gl_cv.hpp:76
MXCapture(MXCapture &&)=delete
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
bool is_open() const
Check whether the capture source is open.
Definition gl_cv.hpp:59
OpenGL window, shader program, sprite, and text rendering classes.
Definition cfg.cpp:11