opengl_interop.hpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_OPENGL_INTEROP_HPP__
44 #define __OPENCV_OPENGL_INTEROP_HPP__
45 
46 #ifdef __cplusplus
47 
48 #include "opencv2/core/core.hpp"
50 
51 namespace cv { namespace ogl {
52 
54 
56 class CV_EXPORTS Buffer
57 {
58 public:
59  enum Target
60  {
61  ARRAY_BUFFER = 0x8892,
62  ELEMENT_ARRAY_BUFFER = 0x8893,
63  PIXEL_PACK_BUFFER = 0x88EB,
64  PIXEL_UNPACK_BUFFER = 0x88EC
65  };
66 
67  enum Access
68  {
69  READ_ONLY = 0x88B8,
70  WRITE_ONLY = 0x88B9,
71  READ_WRITE = 0x88BA
72  };
73 
75  Buffer();
76 
78  Buffer(int arows, int acols, int atype, unsigned int abufId, bool autoRelease = false);
79  Buffer(Size asize, int atype, unsigned int abufId, bool autoRelease = false);
80 
82  Buffer(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
83  Buffer(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
84 
86  explicit Buffer(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false);
87 
89  void create(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
90  void create(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false) { create(asize.height, asize.width, atype, target, autoRelease); }
91 
93  void release();
94 
96  void setAutoRelease(bool flag);
97 
99  void copyFrom(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false);
100 
102  void copyTo(OutputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false) const;
103 
105  Buffer clone(Target target = ARRAY_BUFFER, bool autoRelease = false) const;
106 
108  void bind(Target target) const;
109 
111  static void unbind(Target target);
112 
114  Mat mapHost(Access access);
115  void unmapHost();
116 
118  gpu::GpuMat mapDevice();
119  void unmapDevice();
120 
121  int rows() const { return rows_; }
122  int cols() const { return cols_; }
123  Size size() const { return Size(cols_, rows_); }
124  bool empty() const { return rows_ == 0 || cols_ == 0; }
125 
126  int type() const { return type_; }
127  int depth() const { return CV_MAT_DEPTH(type_); }
128  int channels() const { return CV_MAT_CN(type_); }
129  int elemSize() const { return CV_ELEM_SIZE(type_); }
130  int elemSize1() const { return CV_ELEM_SIZE1(type_); }
131 
132  unsigned int bufId() const;
133 
134  class Impl;
135 
136 private:
137  Ptr<Impl> impl_;
138  int rows_;
139  int cols_;
140  int type_;
141 };
142 
144 class CV_EXPORTS Texture2D
145 {
146 public:
147  enum Format
148  {
149  NONE = 0,
150  DEPTH_COMPONENT = 0x1902,
151  RGB = 0x1907,
152  RGBA = 0x1908
153  };
154 
156  Texture2D();
157 
159  Texture2D(int arows, int acols, Format aformat, unsigned int atexId, bool autoRelease = false);
160  Texture2D(Size asize, Format aformat, unsigned int atexId, bool autoRelease = false);
161 
163  Texture2D(int arows, int acols, Format aformat, bool autoRelease = false);
164  Texture2D(Size asize, Format aformat, bool autoRelease = false);
165 
167  explicit Texture2D(InputArray arr, bool autoRelease = false);
168 
170  void create(int arows, int acols, Format aformat, bool autoRelease = false);
171  void create(Size asize, Format aformat, bool autoRelease = false) { create(asize.height, asize.width, aformat, autoRelease); }
172 
174  void release();
175 
177  void setAutoRelease(bool flag);
178 
180  void copyFrom(InputArray arr, bool autoRelease = false);
181 
183  void copyTo(OutputArray arr, int ddepth = CV_32F, bool autoRelease = false) const;
184 
186  void bind() const;
187 
188  int rows() const { return rows_; }
189  int cols() const { return cols_; }
190  Size size() const { return Size(cols_, rows_); }
191  bool empty() const { return rows_ == 0 || cols_ == 0; }
192 
193  Format format() const { return format_; }
194 
195  unsigned int texId() const;
196 
197  class Impl;
198 
199 private:
200  Ptr<Impl> impl_;
201  int rows_;
202  int cols_;
203  Format format_;
204 };
205 
207 class CV_EXPORTS Arrays
208 {
209 public:
210  Arrays();
211 
212  void setVertexArray(InputArray vertex);
213  void resetVertexArray();
214 
215  void setColorArray(InputArray color);
216  void resetColorArray();
217 
218  void setNormalArray(InputArray normal);
219  void resetNormalArray();
220 
221  void setTexCoordArray(InputArray texCoord);
222  void resetTexCoordArray();
223 
224  void release();
225 
226  void setAutoRelease(bool flag);
227 
228  void bind() const;
229 
230  int size() const { return size_; }
231  bool empty() const { return size_ == 0; }
232 
233 private:
234  int size_;
235  Buffer vertex_;
236  Buffer color_;
237  Buffer normal_;
238  Buffer texCoord_;
239 };
240 
242 
244 CV_EXPORTS void render(const Texture2D& tex,
245  Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
246  Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
247 
249 enum {
250  POINTS = 0x0000,
251  LINES = 0x0001,
252  LINE_LOOP = 0x0002,
253  LINE_STRIP = 0x0003,
254  TRIANGLES = 0x0004,
255  TRIANGLE_STRIP = 0x0005,
256  TRIANGLE_FAN = 0x0006,
257  QUADS = 0x0007,
258  QUAD_STRIP = 0x0008,
259  POLYGON = 0x0009
260 };
261 
263 CV_EXPORTS void render(const Arrays& arr, int mode = POINTS, Scalar color = Scalar::all(255));
264 CV_EXPORTS void render(const Arrays& arr, InputArray indices, int mode = POINTS, Scalar color = Scalar::all(255));
265 
266 }} // namespace cv::gl
267 
268 namespace cv { namespace gpu {
269 
271 CV_EXPORTS void setGlDevice(int device = 0);
272 
273 }}
274 
275 namespace cv {
276 
277 template <> CV_EXPORTS void Ptr<cv::ogl::Buffer::Impl>::delete_obj();
278 template <> CV_EXPORTS void Ptr<cv::ogl::Texture2D::Impl>::delete_obj();
279 
280 }
281 
282 #endif // __cplusplus
283 
284 #endif // __OPENCV_OPENGL_INTEROP_HPP__
Definition: opengl_interop.hpp:253
GLenum mode
Access
Definition: opengl_interop.hpp:67
int size() const
Definition: opengl_interop.hpp:230
Target
Definition: opengl_interop.hpp:59
void delete_obj()
deletes the object. Override if needed
Definition: operations.hpp:2612
Size2i Size
Definition: core.hpp:896
Definition: opengl_interop.hpp:251
Smart pointer for OpenGL 2D texture memory with reference counting.
Definition: opengl_interop.hpp:144
bool empty() const
Definition: opengl_interop.hpp:191
OpenGL Arrays.
Definition: opengl_interop.hpp:207
Definition: opengl_interop.hpp:250
int elemSize() const
Definition: opengl_interop.hpp:129
GLenum target
int elemSize1() const
Definition: opengl_interop.hpp:130
Definition: opengl_interop.hpp:258
void normal(float x, float y, float z)
CV_EXPORTS void 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))
render texture rectangle in window
Proxy datatype for passing Mat's and vector<>'s as input parameters.
Definition: core.hpp:1312
const CvPoint2D32f vertex[4]
Definition: legacy.hpp:1070
The 2D size class.
Definition: core.hpp:81
bool empty() const
Definition: opengl_interop.hpp:124
int cols() const
Definition: opengl_interop.hpp:122
int depth() const
Definition: opengl_interop.hpp:127
bool empty() const
Definition: opengl_interop.hpp:231
Definition: opengl_interop.hpp:254
GLuint GLuint GLsizei GLenum const GLvoid * indices
Definition: legacy.hpp:3084
int channels() const
Definition: opengl_interop.hpp:128
Definition: opengl_interop.hpp:257
Definition: opengl_interop.hpp:252
GLintptr GLsizeiptr GLbitfield access
Smart pointer for OpenGL buffer memory with reference counting.
Definition: opengl_interop.hpp:56
Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat...
Definition: gpumat.hpp:154
int rows() const
Definition: opengl_interop.hpp:121
void create(Size asize, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
Definition: opengl_interop.hpp:90
Definition: opengl_interop.hpp:256
int CvSeq float CvSize2D32f int flag
Definition: legacy.hpp:237
int rows() const
Definition: opengl_interop.hpp:188
Definition: opengl_interop.hpp:255
The Core Functionality.
The n-dimensional matrix class.
Definition: core.hpp:1688
Format format() const
Definition: opengl_interop.hpp:193
static Scalar_< double > all(doublev0)
returns a scalar with all elements set to v0
int type() const
Definition: opengl_interop.hpp:126
_Tp width
Definition: core.hpp:840
The 2D up-right rectangle class.
Definition: core.hpp:83
CvArr * arr
Definition: core_c.h:649
void create(Size asize, Format aformat, bool autoRelease=false)
Definition: opengl_interop.hpp:171
Size size() const
Definition: opengl_interop.hpp:190
Format
Definition: opengl_interop.hpp:147
Definition: opengl_interop.hpp:259
int cols() const
Definition: opengl_interop.hpp:189
_Tp height
Definition: core.hpp:840
CV_EXPORTS void setGlDevice(int device=0)
set a CUDA device to use OpenGL interoperability
void texCoord(float x, float y)
Proxy datatype for passing Mat's and vector<>'s as input parameters.
Definition: core.hpp:1400
Size size() const
Definition: opengl_interop.hpp:123
GLuint color
Definition: core_c.h:1276