1 OpenGL interoperability
2 =======================
10 This section describes OpenGL interoperability.
12 To enable OpenGL support, configure OpenCV using ``CMake`` with ``WITH_OPENGL=ON`` .
13 Currently OpenGL is supported only with WIN32, GTK and Qt backends on Windows and Linux (MacOS and Android are not supported).
14 For GTK backend ``gtkglext-1.0`` library is required.
16 To use OpenGL functionality you should first create OpenGL context (window or frame buffer).
17 You can do this with :ocv:func:`namedWindow` function or with other OpenGL toolkit (GLUT, for example).
23 Smart pointer for OpenGL buffer object with reference counting.
25 .. ocv:class:: ogl::Buffer
27 Buffer Objects are OpenGL objects that store an array of unformatted memory allocated by the OpenGL context.
28 These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.
30 ``ogl::Buffer`` has interface similar with :ocv:class:`Mat` interface and represents 2D array memory.
32 ``ogl::Buffer`` supports memory transfers between host and device and also can be mapped to CUDA memory.
38 The target defines how you intend to use the buffer object.
40 .. ocv:enum:: ogl::Buffer::Target
42 .. ocv:emember:: ARRAY_BUFFER
44 The buffer will be used as a source for vertex data.
46 .. ocv:emember:: ELEMENT_ARRAY_BUFFER
48 The buffer will be used for indices (in ``glDrawElements`` or :ocv:func:`ogl::render`, for example).
50 .. ocv:emember:: PIXEL_PACK_BUFFER
52 The buffer will be used for reading from OpenGL textures.
54 .. ocv:emember:: PIXEL_UNPACK_BUFFER
56 The buffer will be used for writing to OpenGL textures.
64 .. ocv:function:: ogl::Buffer::Buffer()
66 .. ocv:function:: ogl::Buffer::Buffer(int arows, int acols, int atype, unsigned int abufId, bool autoRelease = false)
68 .. ocv:function:: ogl::Buffer::Buffer(Size asize, int atype, unsigned int abufId, bool autoRelease = false)
70 .. ocv:function:: ogl::Buffer::Buffer(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false)
72 .. ocv:function:: ogl::Buffer::Buffer(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false)
74 .. ocv:function:: ogl::Buffer::Buffer(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false)
76 :param arows: Number of rows in a 2D array.
78 :param acols: Number of columns in a 2D array.
80 :param asize: 2D array size.
82 :param atype: Array type ( ``CV_8UC1, ..., CV_64FC4`` ). See :ocv:class:`Mat` for details.
84 :param abufId: Buffer object name.
86 :param arr: Input array (host or device memory, it can be :ocv:class:`Mat` , :ocv:class:`gpu::GpuMat` or ``std::vector`` ).
88 :param target: Buffer usage. See :ocv:enum:`ogl::Buffer::Target` .
90 :param autoRelease: Auto release mode (if true, release will be called in object's destructor).
92 Creates empty ``ogl::Buffer`` object, creates ``ogl::Buffer`` object from existed buffer ( ``abufId`` parameter),
93 allocates memory for ``ogl::Buffer`` object or copies from host/device memory.
99 Allocates memory for ``ogl::Buffer`` object.
101 .. ocv:function:: void ogl::Buffer::create(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false)
103 .. ocv:function:: void ogl::Buffer::create(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false)
105 :param arows: Number of rows in a 2D array.
107 :param acols: Number of columns in a 2D array.
109 :param asize: 2D array size.
111 :param atype: Array type ( ``CV_8UC1, ..., CV_64FC4`` ). See :ocv:class:`Mat` for details.
113 :param target: Buffer usage. See :ocv:enum:`ogl::Buffer::Target` .
115 :param autoRelease: Auto release mode (if true, release will be called in object's destructor).
121 Decrements the reference counter and destroys the buffer object if needed.
123 .. ocv:function:: void ogl::Buffer::release()
127 ogl::Buffer::setAutoRelease
128 ---------------------------
129 Sets auto release mode.
131 .. ocv:function:: void ogl::Buffer::setAutoRelease(bool flag)
133 :param flag: Auto release mode (if true, release will be called in object's destructor).
135 The lifetime of the OpenGL object is tied to the lifetime of the context.
136 If OpenGL context was bound to a window it could be released at any time (user can close a window).
137 If object's destructor is called after destruction of the context it will cause an error.
138 Thus ``ogl::Buffer`` doesn't destroy OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL context).
139 This function can force ``ogl::Buffer`` destructor to destroy OpenGL object.
143 ogl::Buffer::copyFrom
144 ---------------------
145 Copies from host/device memory to OpenGL buffer.
147 .. ocv:function:: void ogl::Buffer::copyFrom(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false)
149 :param arr: Input array (host or device memory, it can be :ocv:class:`Mat` , :ocv:class:`gpu::GpuMat` or ``std::vector`` ).
151 :param target: Buffer usage. See :ocv:enum:`ogl::Buffer::Target` .
153 :param autoRelease: Auto release mode (if true, release will be called in object's destructor).
159 Copies from OpenGL buffer to host/device memory or another OpenGL buffer object.
161 .. ocv:function:: void ogl::Buffer::copyTo(OutputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false) const
163 :param arr: Destination array (host or device memory, can be :ocv:class:`Mat` , :ocv:class:`gpu::GpuMat` , ``std::vector`` or ``ogl::Buffer`` ).
165 :param target: Buffer usage for destination buffer (if ``arr`` is OpenGL buffer).
167 :param autoRelease: Auto release mode for destination buffer (if ``arr`` is OpenGL buffer).
173 Creates a full copy of the buffer object and the underlying data.
175 .. ocv:function:: Buffer ogl::Buffer::clone(Target target = ARRAY_BUFFER, bool autoRelease = false) const
177 :param target: Buffer usage for destination buffer.
179 :param autoRelease: Auto release mode for destination buffer.
185 Binds OpenGL buffer to the specified buffer binding point.
187 .. ocv:function:: void ogl::Buffer::bind(Target target) const
189 :param target: Binding point. See :ocv:enum:`ogl::Buffer::Target` .
195 Unbind any buffers from the specified binding point.
197 .. ocv:function:: static void ogl::Buffer::unbind(Target target)
199 :param target: Binding point. See :ocv:enum:`ogl::Buffer::Target` .
205 Maps OpenGL buffer to host memory.
207 .. ocv:function:: Mat ogl::Buffer::mapHost(Access access)
209 :param access: Access policy, indicating whether it will be possible to read from, write to, or both read from and write to the buffer object's mapped data store. The symbolic constant must be ``ogl::Buffer::READ_ONLY`` , ``ogl::Buffer::WRITE_ONLY`` or ``ogl::Buffer::READ_WRITE`` .
211 ``mapHost`` maps to the client's address space the entire data store of the buffer object.
212 The data can then be directly read and/or written relative to the returned pointer, depending on the specified ``access`` policy.
214 A mapped data store must be unmapped with :ocv:func:`ogl::Buffer::unmapHost` before its buffer object is used.
216 This operation can lead to memory transfers between host and device.
218 Only one buffer object can be mapped at a time.
222 ogl::Buffer::unmapHost
223 ----------------------
224 Unmaps OpenGL buffer.
226 .. ocv:function:: void ogl::Buffer::unmapHost()
230 ogl::Buffer::mapDevice
231 ----------------------
232 Maps OpenGL buffer to CUDA device memory.
234 .. ocv:function:: gpu::GpuMat ogl::Buffer::mapDevice()
236 This operatation doesn't copy data.
237 Several buffer objects can be mapped to CUDA memory at a time.
239 A mapped data store must be unmapped with :ocv:func:`ogl::Buffer::unmapDevice` before its buffer object is used.
243 ogl::Buffer::unmapDevice
244 ------------------------
245 Unmaps OpenGL buffer.
247 .. ocv:function:: void ogl::Buffer::unmapDevice()
253 Smart pointer for OpenGL 2D texture memory with reference counting.
255 .. ocv:class:: ogl::Texture2D
259 ogl::Texture2D::Format
260 ----------------------
261 An Image Format describes the way that the images in Textures store their data.
263 .. ocv:enum:: ogl::Texture2D::Format
265 .. ocv:emember:: NONE
266 .. ocv:emember:: DEPTH_COMPONENT
268 .. ocv:emember:: RGBA
272 ogl::Texture2D::Texture2D
273 -------------------------
276 .. ocv:function:: ogl::Texture2D::Texture2D()
278 .. ocv:function:: ogl::Texture2D::Texture2D(int arows, int acols, Format aformat, unsigned int atexId, bool autoRelease = false)
280 .. ocv:function:: ogl::Texture2D::Texture2D(Size asize, Format aformat, unsigned int atexId, bool autoRelease = false)
282 .. ocv:function:: ogl::Texture2D::Texture2D(int arows, int acols, Format aformat, bool autoRelease = false)
284 .. ocv:function:: ogl::Texture2D::Texture2D(Size asize, Format aformat, bool autoRelease = false)
286 .. ocv:function:: ogl::Texture2D::Texture2D(InputArray arr, bool autoRelease = false)
288 :param arows: Number of rows.
290 :param acols: Number of columns.
292 :param asize: 2D array size.
294 :param aformat: Image format. See :ocv:enum:`ogl::Texture2D::Format` .
296 :param arr: Input array (host or device memory, it can be :ocv:class:`Mat` , :ocv:class:`gpu::GpuMat` or :ocv:class:`ogl::Buffer` ).
298 :param autoRelease: Auto release mode (if true, release will be called in object's destructor).
300 Creates empty ``ogl::Texture2D`` object, allocates memory for ``ogl::Texture2D`` object or copies from host/device memory.
304 ogl::Texture2D::create
305 ----------------------
306 Allocates memory for ``ogl::Texture2D`` object.
308 .. ocv:function:: void ogl::Texture2D::create(int arows, int acols, Format aformat, bool autoRelease = false)
310 .. ocv:function:: void ogl::Texture2D::create(Size asize, Format aformat, bool autoRelease = false)
312 :param arows: Number of rows.
314 :param acols: Number of columns.
316 :param asize: 2D array size.
318 :param aformat: Image format. See :ocv:enum:`ogl::Texture2D::Format` .
320 :param autoRelease: Auto release mode (if true, release will be called in object's destructor).
324 ogl::Texture2D::release
325 -----------------------
326 Decrements the reference counter and destroys the texture object if needed.
328 .. ocv:function:: void ogl::Texture2D::release()
332 ogl::Texture2D::setAutoRelease
333 ------------------------------
334 Sets auto release mode.
336 .. ocv:function:: void ogl::Texture2D::setAutoRelease(bool flag)
338 :param flag: Auto release mode (if true, release will be called in object's destructor).
340 The lifetime of the OpenGL object is tied to the lifetime of the context.
341 If OpenGL context was bound to a window it could be released at any time (user can close a window).
342 If object's destructor is called after destruction of the context it will cause an error.
343 Thus ``ogl::Texture2D`` doesn't destroy OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL context).
344 This function can force ``ogl::Texture2D`` destructor to destroy OpenGL object.
348 ogl::Texture2D::copyFrom
349 ------------------------
350 Copies from host/device memory to OpenGL texture.
352 .. ocv:function:: void ogl::Texture2D::copyFrom(InputArray arr, bool autoRelease = false)
354 :param arr: Input array (host or device memory, it can be :ocv:class:`Mat` , :ocv:class:`gpu::GpuMat` or :ocv:class:`ogl::Buffer` ).
356 :param autoRelease: Auto release mode (if true, release will be called in object's destructor).
360 ogl::Texture2D::copyTo
361 ----------------------
362 Copies from OpenGL texture to host/device memory or another OpenGL texture object.
364 .. ocv:function:: void ogl::Texture2D::copyTo(OutputArray arr, int ddepth = CV_32F, bool autoRelease = false) const
366 :param arr: Destination array (host or device memory, can be :ocv:class:`Mat` , :ocv:class:`gpu::GpuMat` , :ocv:class:`ogl::Buffer` or ``ogl::Texture2D`` ).
368 :param ddepth: Destination depth.
370 :param autoRelease: Auto release mode for destination buffer (if ``arr`` is OpenGL buffer or texture).
376 Binds texture to current active texture unit for ``GL_TEXTURE_2D`` target.
378 .. ocv:function:: void ogl::Texture2D::bind() const
384 Wrapper for OpenGL Client-Side Vertex arrays.
386 .. ocv:class:: ogl::Arrays
388 ``ogl::Arrays`` stores vertex data in :ocv:class:`ogl::Buffer` objects.
392 ogl::Arrays::setVertexArray
393 ---------------------------
394 Sets an array of vertex coordinates.
396 .. ocv:function:: void ogl::Arrays::setVertexArray(InputArray vertex)
398 :param vertex: array with vertex coordinates, can be both host and device memory.
402 ogl::Arrays::resetVertexArray
403 -----------------------------
404 Resets vertex coordinates.
406 .. ocv:function:: void ogl::Arrays::resetVertexArray()
410 ogl::Arrays::setColorArray
411 --------------------------
412 Sets an array of vertex colors.
414 .. ocv:function:: void ogl::Arrays::setColorArray(InputArray color)
416 :param color: array with vertex colors, can be both host and device memory.
420 ogl::Arrays::resetColorArray
421 ----------------------------
422 Resets vertex colors.
424 .. ocv:function:: void ogl::Arrays::resetColorArray()
428 ogl::Arrays::setNormalArray
429 ---------------------------
430 Sets an array of vertex normals.
432 .. ocv:function:: void ogl::Arrays::setNormalArray(InputArray normal)
434 :param normal: array with vertex normals, can be both host and device memory.
438 ogl::Arrays::resetNormalArray
439 -----------------------------
440 Resets vertex normals.
442 .. ocv:function:: void ogl::Arrays::resetNormalArray()
446 ogl::Arrays::setTexCoordArray
447 -----------------------------
448 Sets an array of vertex texture coordinates.
450 .. ocv:function:: void ogl::Arrays::setTexCoordArray(InputArray texCoord)
452 :param texCoord: array with vertex texture coordinates, can be both host and device memory.
456 ogl::Arrays::resetTexCoordArray
457 -------------------------------
458 Resets vertex texture coordinates.
460 .. ocv:function:: void ogl::Arrays::resetTexCoordArray()
466 Releases all inner buffers.
468 .. ocv:function:: void ogl::Arrays::release()
472 ogl::Arrays::setAutoRelease
473 ---------------------------
474 Sets auto release mode all inner buffers.
476 .. ocv:function:: void ogl::Arrays::setAutoRelease(bool flag)
478 :param flag: Auto release mode.
484 Binds all vertex arrays.
486 .. ocv:function:: void ogl::Arrays::bind() const
492 Returns the vertex count.
494 .. ocv:function:: int ogl::Arrays::size() const
500 Render OpenGL texture or primitives.
502 .. ocv:function:: void ogl::render(const Texture2D& tex, Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0), Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0))
504 .. ocv:function:: void ogl::render(const Arrays& arr, int mode = POINTS, Scalar color = Scalar::all(255))
506 .. ocv:function:: void ogl::render(const Arrays& arr, InputArray indices, int mode = POINTS, Scalar color = Scalar::all(255))
508 :param tex: Texture to draw.
510 :param wndRect: Region of window, where to draw a texture (normalized coordinates).
512 :param texRect: Region of texture to draw (normalized coordinates).
514 :param arr: Array of privitives vertices.
516 :param indices: Array of vertices indices (host or device memory).
518 :param mode: Render mode. Available options:
531 :param color: Color for all vertices. Will be used if ``arr`` doesn't contain color array.
537 Sets a CUDA device and initializes it for the current thread with OpenGL interoperability.
539 .. ocv:function:: void gpu::setGlDevice( int device = 0 )
541 :param device: System index of a GPU device starting with 0.
543 This function should be explicitly called after OpenGL context creation and before any CUDA calls.