This section describes OpenGL interoperability.
To enable OpenGL support, configure OpenCV using CMake
with WITH_OPENGL=ON
.
Currently OpenGL is supported only with WIN32, GTK and Qt backends on Windows and Linux (MacOS and Android are not supported).
For GTK backend gtkglext-1.0
library is required.
To use OpenGL functionality you should first create OpenGL context (window or frame buffer).
You can do this with namedWindow()
function or with other OpenGL toolkit (GLUT, for example).
Smart pointer for OpenGL buffer object with reference counting.
ogl::
Buffer
¶Buffer Objects are OpenGL objects that store an array of unformatted memory allocated by the OpenGL context. These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.
ogl::Buffer
has interface similar with Mat
interface and represents 2D array memory.
ogl::Buffer
supports memory transfers between host and device and also can be mapped to CUDA memory.
The target defines how you intend to use the buffer object.
enum ogl::Buffer::
Target
¶ARRAY_BUFFER
¶The buffer will be used as a source for vertex data.
ELEMENT_ARRAY_BUFFER
¶The buffer will be used for indices (in glDrawElements
or ogl::render()
, for example).
PIXEL_PACK_BUFFER
¶The buffer will be used for reading from OpenGL textures.
PIXEL_UNPACK_BUFFER
¶The buffer will be used for writing to OpenGL textures.
The constructors.
ogl::Buffer::
Buffer
()¶
ogl::Buffer::
Buffer
(int arows, int acols, int atype, unsigned int abufId, bool autoRelease=false)¶
ogl::Buffer::
Buffer
(Size asize, int atype, unsigned int abufId, bool autoRelease=false)¶
ogl::Buffer::
Buffer
(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)¶
ogl::Buffer::
Buffer
(Size asize, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)¶
ogl::Buffer::
Buffer
(InputArray arr, Target target=ARRAY_BUFFER, bool autoRelease=false)¶Parameters: |
|
---|
Creates empty ogl::Buffer
object, creates ogl::Buffer
object from existed buffer ( abufId
parameter),
allocates memory for ogl::Buffer
object or copies from host/device memory.
Allocates memory for ogl::Buffer
object.
void ogl::Buffer::
create
(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)¶
void ogl::Buffer::
create
(Size asize, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)¶Parameters: |
|
---|
Decrements the reference counter and destroys the buffer object if needed.
void ogl::Buffer::
release
()¶Sets auto release mode.
void ogl::Buffer::
setAutoRelease
(bool flag)¶Parameters: |
|
---|
The lifetime of the OpenGL object is tied to the lifetime of the context.
If OpenGL context was bound to a window it could be released at any time (user can close a window).
If object’s destructor is called after destruction of the context it will cause an error.
Thus ogl::Buffer
doesn’t destroy OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL context).
This function can force ogl::Buffer
destructor to destroy OpenGL object.
Copies from host/device memory to OpenGL buffer.
void ogl::Buffer::
copyFrom
(InputArray arr, Target target=ARRAY_BUFFER, bool autoRelease=false)¶Parameters: |
|
---|
Copies from OpenGL buffer to host/device memory or another OpenGL buffer object.
void ogl::Buffer::
copyTo
(OutputArray arr, Target target=ARRAY_BUFFER, bool autoRelease=false) const
¶Parameters: |
|
---|
Creates a full copy of the buffer object and the underlying data.
Buffer ogl::Buffer::
clone
(Target target=ARRAY_BUFFER, bool autoRelease=false) const
¶Parameters: |
|
---|
Binds OpenGL buffer to the specified buffer binding point.
void ogl::Buffer::
bind
(Target target) const
¶Parameters: |
|
---|
Unbind any buffers from the specified binding point.
static void ogl::Buffer::
unbind
(Target target)¶Parameters: |
|
---|
Maps OpenGL buffer to host memory.
Mat ogl::Buffer::
mapHost
(Access access)¶Parameters: |
|
---|
mapHost
maps to the client’s address space the entire data store of the buffer object.
The data can then be directly read and/or written relative to the returned pointer, depending on the specified access
policy.
A mapped data store must be unmapped with ogl::Buffer::unmapHost()
before its buffer object is used.
This operation can lead to memory transfers between host and device.
Only one buffer object can be mapped at a time.
Maps OpenGL buffer to CUDA device memory.
gpu::GpuMat ogl::Buffer::
mapDevice
()¶This operatation doesn’t copy data. Several buffer objects can be mapped to CUDA memory at a time.
A mapped data store must be unmapped with ogl::Buffer::unmapDevice()
before its buffer object is used.
Smart pointer for OpenGL 2D texture memory with reference counting.
ogl::
Texture2D
¶An Image Format describes the way that the images in Textures store their data.
The constructors.
ogl::Texture2D::
Texture2D
()¶
ogl::Texture2D::
Texture2D
(int arows, int acols, Format aformat, unsigned int atexId, bool autoRelease=false)¶
ogl::Texture2D::
Texture2D
(Size asize, Format aformat, unsigned int atexId, bool autoRelease=false)¶
ogl::Texture2D::
Texture2D
(int arows, int acols, Format aformat, bool autoRelease=false)¶
ogl::Texture2D::
Texture2D
(Size asize, Format aformat, bool autoRelease=false)¶
ogl::Texture2D::
Texture2D
(InputArray arr, bool autoRelease=false)¶Parameters: |
|
---|
Creates empty ogl::Texture2D
object, allocates memory for ogl::Texture2D
object or copies from host/device memory.
Allocates memory for ogl::Texture2D
object.
void ogl::Texture2D::
create
(int arows, int acols, Format aformat, bool autoRelease=false)¶
void ogl::Texture2D::
create
(Size asize, Format aformat, bool autoRelease=false)¶Parameters: |
|
---|
Decrements the reference counter and destroys the texture object if needed.
void ogl::Texture2D::
release
()¶Sets auto release mode.
void ogl::Texture2D::
setAutoRelease
(bool flag)¶Parameters: |
|
---|
The lifetime of the OpenGL object is tied to the lifetime of the context.
If OpenGL context was bound to a window it could be released at any time (user can close a window).
If object’s destructor is called after destruction of the context it will cause an error.
Thus ogl::Texture2D
doesn’t destroy OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL context).
This function can force ogl::Texture2D
destructor to destroy OpenGL object.
Copies from host/device memory to OpenGL texture.
void ogl::Texture2D::
copyFrom
(InputArray arr, bool autoRelease=false)¶Parameters: |
|
---|
Copies from OpenGL texture to host/device memory or another OpenGL texture object.
void ogl::Texture2D::
copyTo
(OutputArray arr, int ddepth=CV_32F, bool autoRelease=false) const
¶Parameters: |
|
---|
Binds texture to current active texture unit for GL_TEXTURE_2D
target.
void ogl::Texture2D::
bind
() const
¶Wrapper for OpenGL Client-Side Vertex arrays.
ogl::
Arrays
¶ogl::Arrays
stores vertex data in ogl::Buffer
objects.
Sets an array of vertex coordinates.
void ogl::Arrays::
setVertexArray
(InputArray vertex)¶Parameters: |
|
---|
Resets vertex coordinates.
void ogl::Arrays::
resetVertexArray
()¶Sets an array of vertex colors.
void ogl::Arrays::
setColorArray
(InputArray color)¶Parameters: |
|
---|
Sets an array of vertex normals.
void ogl::Arrays::
setNormalArray
(InputArray normal)¶Parameters: |
|
---|
Sets an array of vertex texture coordinates.
void ogl::Arrays::
setTexCoordArray
(InputArray texCoord)¶Parameters: |
|
---|
Resets vertex texture coordinates.
void ogl::Arrays::
resetTexCoordArray
()¶Sets auto release mode all inner buffers.
void ogl::Arrays::
setAutoRelease
(bool flag)¶Parameters: |
|
---|
Render OpenGL texture or primitives.
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))¶
void ogl::
render
(const Arrays& arr, int mode=POINTS, Scalar color=Scalar::all(255))¶
void ogl::
render
(const Arrays& arr, InputArray indices, int mode=POINTS, Scalar color=Scalar::all(255))¶Parameters: |
|
---|
Sets a CUDA device and initializes it for the current thread with OpenGL interoperability.
void gpu::
setGlDevice
(int device=0 )¶Parameters: |
|
---|
This function should be explicitly called after OpenGL context creation and before any CUDA calls.