1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#ifndef __OPENCV_CORE_OPENGL_HPP__
44
#define __OPENCV_CORE_OPENGL_HPP__
47
# error opengl.hpp header must be compiled as C++
50
#include "opencv2/core.hpp"
53
namespace cv { namespace ogl {
55
/** @addtogroup core_opengl
56
This section describes OpenGL interoperability.
58
To enable OpenGL support, configure OpenCV using CMake with WITH_OPENGL=ON . Currently OpenGL is
59
supported only with WIN32, GTK and Qt backends on Windows and Linux (MacOS and Android are not
60
supported). For GTK backend gtkglext-1.0 library is required.
62
To use OpenGL functionality you should first create OpenGL context (window or frame buffer). You can
63
do this with namedWindow function or with other OpenGL toolkit (GLUT, for example).
67
/////////////////// OpenGL Objects ///////////////////
69
/** @brief Smart pointer for OpenGL buffer object with reference counting.
71
Buffer Objects are OpenGL objects that store an array of unformatted memory allocated by the OpenGL
72
context. These can be used to store vertex data, pixel data retrieved from images or the
73
framebuffer, and a variety of other things.
75
ogl::Buffer has interface similar with Mat interface and represents 2D array memory.
77
ogl::Buffer supports memory transfers between host and device and also can be mapped to CUDA memory.
79
class CV_EXPORTS Buffer
82
/** @brief The target defines how you intend to use the buffer object.
86
ARRAY_BUFFER = 0x8892, //!< The buffer will be used as a source for vertex data
87
ELEMENT_ARRAY_BUFFER = 0x8893, //!< The buffer will be used for indices (in glDrawElements, for example)
88
PIXEL_PACK_BUFFER = 0x88EB, //!< The buffer will be used for reading from OpenGL textures
89
PIXEL_UNPACK_BUFFER = 0x88EC //!< The buffer will be used for writing to OpenGL textures
99
/** @brief The constructors.
101
Creates empty ogl::Buffer object, creates ogl::Buffer object from existed buffer ( abufId
102
parameter), allocates memory for ogl::Buffer object or copies from host/device memory.
107
@param arows Number of rows in a 2D array.
108
@param acols Number of columns in a 2D array.
109
@param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
110
@param abufId Buffer object name.
111
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
113
Buffer(int arows, int acols, int atype, unsigned int abufId, bool autoRelease = false);
116
@param asize 2D array size.
117
@param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
118
@param abufId Buffer object name.
119
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
121
Buffer(Size asize, int atype, unsigned int abufId, bool autoRelease = false);
124
@param arows Number of rows in a 2D array.
125
@param acols Number of columns in a 2D array.
126
@param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
127
@param target Buffer usage. See cv::ogl::Buffer::Target .
128
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
130
Buffer(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
133
@param asize 2D array size.
134
@param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
135
@param target Buffer usage. See cv::ogl::Buffer::Target .
136
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
138
Buffer(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
141
@param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or std::vector ).
142
@param target Buffer usage. See cv::ogl::Buffer::Target .
143
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
145
explicit Buffer(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false);
147
/** @brief Allocates memory for ogl::Buffer object.
149
@param arows Number of rows in a 2D array.
150
@param acols Number of columns in a 2D array.
151
@param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
152
@param target Buffer usage. See cv::ogl::Buffer::Target .
153
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
155
void create(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
158
@param asize 2D array size.
159
@param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
160
@param target Buffer usage. See cv::ogl::Buffer::Target .
161
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
163
void create(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
165
/** @brief Decrements the reference counter and destroys the buffer object if needed.
167
The function will call setAutoRelease(true) .
171
/** @brief Sets auto release mode.
173
The lifetime of the OpenGL object is tied to the lifetime of the context. If OpenGL context was
174
bound to a window it could be released at any time (user can close a window). If object's destructor
175
is called after destruction of the context it will cause an error. Thus ogl::Buffer doesn't destroy
176
OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL context).
177
This function can force ogl::Buffer destructor to destroy OpenGL object.
178
@param flag Auto release mode (if true, release will be called in object's destructor).
180
void setAutoRelease(bool flag);
182
/** @brief Copies from host/device memory to OpenGL buffer.
183
@param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or std::vector ).
184
@param target Buffer usage. See cv::ogl::Buffer::Target .
185
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
187
void copyFrom(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false);
190
void copyFrom(InputArray arr, cuda::Stream& stream, Target target = ARRAY_BUFFER, bool autoRelease = false);
192
/** @brief Copies from OpenGL buffer to host/device memory or another OpenGL buffer object.
194
@param arr Destination array (host or device memory, can be Mat , cuda::GpuMat , std::vector or
197
void copyTo(OutputArray arr) const;
200
void copyTo(OutputArray arr, cuda::Stream& stream) const;
202
/** @brief Creates a full copy of the buffer object and the underlying data.
204
@param target Buffer usage for destination buffer.
205
@param autoRelease Auto release mode for destination buffer.
207
Buffer clone(Target target = ARRAY_BUFFER, bool autoRelease = false) const;
209
/** @brief Binds OpenGL buffer to the specified buffer binding point.
211
@param target Binding point. See cv::ogl::Buffer::Target .
213
void bind(Target target) const;
215
/** @brief Unbind any buffers from the specified binding point.
217
@param target Binding point. See cv::ogl::Buffer::Target .
219
static void unbind(Target target);
221
/** @brief Maps OpenGL buffer to host memory.
223
mapHost maps to the client's address space the entire data store of the buffer object. The data can
224
then be directly read and/or written relative to the returned pointer, depending on the specified
227
A mapped data store must be unmapped with ogl::Buffer::unmapHost before its buffer object is used.
229
This operation can lead to memory transfers between host and device.
231
Only one buffer object can be mapped at a time.
232
@param access Access policy, indicating whether it will be possible to read from, write to, or both
233
read from and write to the buffer object's mapped data store. The symbolic constant must be
234
ogl::Buffer::READ_ONLY , ogl::Buffer::WRITE_ONLY or ogl::Buffer::READ_WRITE .
236
Mat mapHost(Access access);
238
/** @brief Unmaps OpenGL buffer.
242
//! map to device memory (blocking)
243
cuda::GpuMat mapDevice();
246
/** @brief Maps OpenGL buffer to CUDA device memory.
248
This operatation doesn't copy data. Several buffer objects can be mapped to CUDA memory at a time.
250
A mapped data store must be unmapped with ogl::Buffer::unmapDevice before its buffer object is used.
252
cuda::GpuMat mapDevice(cuda::Stream& stream);
254
/** @brief Unmaps OpenGL buffer.
256
void unmapDevice(cuda::Stream& stream);
265
int channels() const;
266
int elemSize() const;
267
int elemSize1() const;
269
//! get OpenGL opject id
270
unsigned int bufId() const;
281
/** @brief Smart pointer for OpenGL 2D texture memory with reference counting.
283
class CV_EXPORTS Texture2D
286
/** @brief An Image Format describes the way that the images in Textures store their data.
291
DEPTH_COMPONENT = 0x1902, //!< Depth
292
RGB = 0x1907, //!< Red, Green, Blue
293
RGBA = 0x1908 //!< Red, Green, Blue, Alpha
296
/** @brief The constructors.
298
Creates empty ogl::Texture2D object, allocates memory for ogl::Texture2D object or copies from
304
Texture2D(int arows, int acols, Format aformat, unsigned int atexId, bool autoRelease = false);
307
Texture2D(Size asize, Format aformat, unsigned int atexId, bool autoRelease = false);
310
@param arows Number of rows.
311
@param acols Number of columns.
312
@param aformat Image format. See cv::ogl::Texture2D::Format .
313
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
315
Texture2D(int arows, int acols, Format aformat, bool autoRelease = false);
318
@param asize 2D array size.
319
@param aformat Image format. See cv::ogl::Texture2D::Format .
320
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
322
Texture2D(Size asize, Format aformat, bool autoRelease = false);
325
@param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or ogl::Buffer ).
326
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
328
explicit Texture2D(InputArray arr, bool autoRelease = false);
330
/** @brief Allocates memory for ogl::Texture2D object.
332
@param arows Number of rows.
333
@param acols Number of columns.
334
@param aformat Image format. See cv::ogl::Texture2D::Format .
335
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
337
void create(int arows, int acols, Format aformat, bool autoRelease = false);
339
@param asize 2D array size.
340
@param aformat Image format. See cv::ogl::Texture2D::Format .
341
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
343
void create(Size asize, Format aformat, bool autoRelease = false);
345
/** @brief Decrements the reference counter and destroys the texture object if needed.
347
The function will call setAutoRelease(true) .
351
/** @brief Sets auto release mode.
353
@param flag Auto release mode (if true, release will be called in object's destructor).
355
The lifetime of the OpenGL object is tied to the lifetime of the context. If OpenGL context was
356
bound to a window it could be released at any time (user can close a window). If object's destructor
357
is called after destruction of the context it will cause an error. Thus ogl::Texture2D doesn't
358
destroy OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL
359
context). This function can force ogl::Texture2D destructor to destroy OpenGL object.
361
void setAutoRelease(bool flag);
363
/** @brief Copies from host/device memory to OpenGL texture.
365
@param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or ogl::Buffer ).
366
@param autoRelease Auto release mode (if true, release will be called in object's destructor).
368
void copyFrom(InputArray arr, bool autoRelease = false);
370
/** @brief Copies from OpenGL texture to host/device memory or another OpenGL texture object.
372
@param arr Destination array (host or device memory, can be Mat , cuda::GpuMat , ogl::Buffer or
374
@param ddepth Destination depth.
375
@param autoRelease Auto release mode for destination buffer (if arr is OpenGL buffer or texture).
377
void copyTo(OutputArray arr, int ddepth = CV_32F, bool autoRelease = false) const;
379
/** @brief Binds texture to current active texture unit for GL_TEXTURE_2D target.
388
Format format() const;
390
//! get OpenGL opject id
391
unsigned int texId() const;
402
/** @brief Wrapper for OpenGL Client-Side Vertex arrays.
404
ogl::Arrays stores vertex data in ogl::Buffer objects.
406
class CV_EXPORTS Arrays
409
/** @brief Default constructor
413
/** @brief Sets an array of vertex coordinates.
414
@param vertex array with vertex coordinates, can be both host and device memory.
416
void setVertexArray(InputArray vertex);
418
/** @brief Resets vertex coordinates.
420
void resetVertexArray();
422
/** @brief Sets an array of vertex colors.
423
@param color array with vertex colors, can be both host and device memory.
425
void setColorArray(InputArray color);
427
/** @brief Resets vertex colors.
429
void resetColorArray();
431
/** @brief Sets an array of vertex normals.
432
@param normal array with vertex normals, can be both host and device memory.
434
void setNormalArray(InputArray normal);
436
/** @brief Resets vertex normals.
438
void resetNormalArray();
440
/** @brief Sets an array of vertex texture coordinates.
441
@param texCoord array with vertex texture coordinates, can be both host and device memory.
443
void setTexCoordArray(InputArray texCoord);
445
/** @brief Resets vertex texture coordinates.
447
void resetTexCoordArray();
449
/** @brief Releases all inner buffers.
453
/** @brief Sets auto release mode all inner buffers.
454
@param flag Auto release mode.
456
void setAutoRelease(bool flag);
458
/** @brief Binds all vertex arrays.
462
/** @brief Returns the vertex count.
475
/////////////////// Render Functions ///////////////////
484
TRIANGLE_STRIP = 0x0005,
485
TRIANGLE_FAN = 0x0006,
491
/** @brief Render OpenGL texture or primitives.
492
@param tex Texture to draw.
493
@param wndRect Region of window, where to draw a texture (normalized coordinates).
494
@param texRect Region of texture to draw (normalized coordinates).
496
CV_EXPORTS void render(const Texture2D& tex,
497
Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
498
Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
501
@param arr Array of privitives vertices.
502
@param mode Render mode. One of cv::ogl::RenderModes
503
@param color Color for all vertices. Will be used if arr doesn't contain color array.
505
CV_EXPORTS void render(const Arrays& arr, int mode = POINTS, Scalar color = Scalar::all(255));
508
@param arr Array of privitives vertices.
509
@param indices Array of vertices indices (host or device memory).
510
@param mode Render mode. One of cv::ogl::RenderModes
511
@param color Color for all vertices. Will be used if arr doesn't contain color array.
513
CV_EXPORTS void render(const Arrays& arr, InputArray indices, int mode = POINTS, Scalar color = Scalar::all(255));
515
/////////////////// CL-GL Interoperability Functions ///////////////////
518
using namespace cv::ocl;
520
// TODO static functions in the Context class
521
/** @brief Creates OpenCL context from GL.
522
@return Returns reference to OpenCL Context
524
CV_EXPORTS Context& initializeContextFromGL();
526
} // namespace cv::ogl::ocl
528
/** @brief Converts InputArray to Texture2D object.
529
@param src - source InputArray.
530
@param texture - destination Texture2D object.
532
CV_EXPORTS void convertToGLTexture2D(InputArray src, Texture2D& texture);
534
/** @brief Converts Texture2D object to OutputArray.
535
@param texture - source Texture2D object.
536
@param dst - destination OutputArray.
538
CV_EXPORTS void convertFromGLTexture2D(const Texture2D& texture, OutputArray dst);
540
/** @brief Maps Buffer object to process on CL side (convert to UMat).
542
Function creates CL buffer from GL one, and then constructs UMat that can be used
543
to process buffer data with OpenCV functions. Note that in current implementation
544
UMat constructed this way doesn't own corresponding GL buffer object, so it is
545
the user responsibility to close down CL/GL buffers relationships by explicitly
546
calling unmapGLBuffer() function.
547
@param buffer - source Buffer object.
548
@param accessFlags - data access flags (ACCESS_READ|ACCESS_WRITE).
549
@return Returns UMat object
551
CV_EXPORTS UMat mapGLBuffer(const Buffer& buffer, int accessFlags = ACCESS_READ|ACCESS_WRITE);
553
/** @brief Unmaps Buffer object (releases UMat, previously mapped from Buffer).
555
Function must be called explicitly by the user for each UMat previously constructed
556
by the call to mapGLBuffer() function.
557
@param u - source UMat, created by mapGLBuffer().
559
CV_EXPORTS void unmapGLBuffer(UMat& u);
561
}} // namespace cv::ogl
563
namespace cv { namespace cuda {
568
/** @brief Sets a CUDA device and initializes it for the current thread with OpenGL interoperability.
570
This function should be explicitly called after OpenGL context creation and before any CUDA calls.
571
@param device System index of a CUDA device starting with 0.
574
CV_EXPORTS void setGlDevice(int device = 0);
582
////////////////////////////////////////////////////////////////////////
583
////////////////////////////////////////////////////////////////////////
584
////////////////////////////////////////////////////////////////////////
587
cv::ogl::Buffer::Buffer(int arows, int acols, int atype, Target target, bool autoRelease) : rows_(0), cols_(0), type_(0)
589
create(arows, acols, atype, target, autoRelease);
593
cv::ogl::Buffer::Buffer(Size asize, int atype, Target target, bool autoRelease) : rows_(0), cols_(0), type_(0)
595
create(asize, atype, target, autoRelease);
599
void cv::ogl::Buffer::create(Size asize, int atype, Target target, bool autoRelease)
601
create(asize.height, asize.width, atype, target, autoRelease);
605
int cv::ogl::Buffer::rows() const
611
int cv::ogl::Buffer::cols() const
617
cv::Size cv::ogl::Buffer::size() const
619
return Size(cols_, rows_);
623
bool cv::ogl::Buffer::empty() const
625
return rows_ == 0 || cols_ == 0;
629
int cv::ogl::Buffer::type() const
635
int cv::ogl::Buffer::depth() const
637
return CV_MAT_DEPTH(type_);
641
int cv::ogl::Buffer::channels() const
643
return CV_MAT_CN(type_);
647
int cv::ogl::Buffer::elemSize() const
649
return CV_ELEM_SIZE(type_);
653
int cv::ogl::Buffer::elemSize1() const
655
return CV_ELEM_SIZE1(type_);
661
cv::ogl::Texture2D::Texture2D(int arows, int acols, Format aformat, bool autoRelease) : rows_(0), cols_(0), format_(NONE)
663
create(arows, acols, aformat, autoRelease);
667
cv::ogl::Texture2D::Texture2D(Size asize, Format aformat, bool autoRelease) : rows_(0), cols_(0), format_(NONE)
669
create(asize, aformat, autoRelease);
673
void cv::ogl::Texture2D::create(Size asize, Format aformat, bool autoRelease)
675
create(asize.height, asize.width, aformat, autoRelease);
679
int cv::ogl::Texture2D::rows() const
685
int cv::ogl::Texture2D::cols() const
691
cv::Size cv::ogl::Texture2D::size() const
693
return Size(cols_, rows_);
697
bool cv::ogl::Texture2D::empty() const
699
return rows_ == 0 || cols_ == 0;
703
cv::ogl::Texture2D::Format cv::ogl::Texture2D::format() const
711
cv::ogl::Arrays::Arrays() : size_(0)
716
int cv::ogl::Arrays::size() const
722
bool cv::ogl::Arrays::empty() const
729
#endif /* __OPENCV_CORE_OPENGL_HPP__ */