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_CORE_OPENGL_HPP__
44 #define __OPENCV_CORE_OPENGL_HPP__
45 
46 #ifndef __cplusplus
47 #  error opengl.hpp header must be compiled as C++
48 #endif
49 
50 #include "opencv2/core.hpp"
51 
52 namespace cv { namespace ogl {
53 
54 /** @addtogroup core_opengl
55 This section describes OpenGL interoperability.
56 
57 To enable OpenGL support, configure OpenCV using CMake with WITH_OPENGL=ON . Currently OpenGL is
58 supported only with WIN32, GTK and Qt backends on Windows and Linux (MacOS and Android are not
59 supported). For GTK backend gtkglext-1.0 library is required.
60 
61 To use OpenGL functionality you should first create OpenGL context (window or frame buffer). You can
62 do this with namedWindow function or with other OpenGL toolkit (GLUT, for example).
63 */
64 //! @{
65 
66 /////////////////// OpenGL Objects ///////////////////
67 
68 /** @brief Smart pointer for OpenGL buffer object with reference counting.
69 
70 Buffer Objects are OpenGL objects that store an array of unformatted memory allocated by the OpenGL
71 context. These can be used to store vertex data, pixel data retrieved from images or the
72 framebuffer, and a variety of other things.
73 
74 ogl::Buffer has interface similar with Mat interface and represents 2D array memory.
75 
76 ogl::Buffer supports memory transfers between host and device and also can be mapped to CUDA memory.
77  */
78 class CV_EXPORTS Buffer
79 {
80 public:
81     /** @brief The target defines how you intend to use the buffer object.
82     */
83     enum Target
84     {
85         ARRAY_BUFFER         = 0x8892, //!< The buffer will be used as a source for vertex data
86         ELEMENT_ARRAY_BUFFER = 0x8893, //!< The buffer will be used for indices (in glDrawElements, for example)
87         PIXEL_PACK_BUFFER    = 0x88EB, //!< The buffer will be used for reading from OpenGL textures
88         PIXEL_UNPACK_BUFFER  = 0x88EC  //!< The buffer will be used for writing to OpenGL textures
89     };
90 
91     enum Access
92     {
93         READ_ONLY  = 0x88B8,
94         WRITE_ONLY = 0x88B9,
95         READ_WRITE = 0x88BA
96     };
97 
98     /** @brief The constructors.
99 
100     Creates empty ogl::Buffer object, creates ogl::Buffer object from existed buffer ( abufId
101     parameter), allocates memory for ogl::Buffer object or copies from host/device memory.
102      */
103     Buffer();
104 
105     /** @overload
106     @param arows Number of rows in a 2D array.
107     @param acols Number of columns in a 2D array.
108     @param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
109     @param abufId Buffer object name.
110     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
111     */
112     Buffer(int arows, int acols, int atype, unsigned int abufId, bool autoRelease = false);
113 
114     /** @overload
115     @param asize 2D array size.
116     @param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
117     @param abufId Buffer object name.
118     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
119     */
120     Buffer(Size asize, int atype, unsigned int abufId, bool autoRelease = false);
121 
122     /** @overload
123     @param arows Number of rows in a 2D array.
124     @param acols Number of columns in a 2D array.
125     @param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
126     @param target Buffer usage. See cv::ogl::Buffer::Target .
127     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
128     */
129     Buffer(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
130 
131     /** @overload
132     @param asize 2D array size.
133     @param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
134     @param target Buffer usage. See cv::ogl::Buffer::Target .
135     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
136     */
137     Buffer(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
138 
139     /** @overload
140     @param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or std::vector ).
141     @param target Buffer usage. See cv::ogl::Buffer::Target .
142     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
143     */
144     explicit Buffer(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false);
145 
146     /** @brief Allocates memory for ogl::Buffer object.
147 
148     @param arows Number of rows in a 2D array.
149     @param acols Number of columns in a 2D array.
150     @param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
151     @param target Buffer usage. See cv::ogl::Buffer::Target .
152     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
153      */
154     void create(int arows, int acols, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
155 
156     /** @overload
157     @param asize 2D array size.
158     @param atype Array type ( CV_8UC1, ..., CV_64FC4 ). See Mat for details.
159     @param target Buffer usage. See cv::ogl::Buffer::Target .
160     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
161     */
162     void create(Size asize, int atype, Target target = ARRAY_BUFFER, bool autoRelease = false);
163 
164     /** @brief Decrements the reference counter and destroys the buffer object if needed.
165 
166     The function will call setAutoRelease(true) .
167      */
168     void release();
169 
170     /** @brief Sets auto release mode.
171 
172     The lifetime of the OpenGL object is tied to the lifetime of the context. If OpenGL context was
173     bound to a window it could be released at any time (user can close a window). If object's destructor
174     is called after destruction of the context it will cause an error. Thus ogl::Buffer doesn't destroy
175     OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL context).
176     This function can force ogl::Buffer destructor to destroy OpenGL object.
177     @param flag Auto release mode (if true, release will be called in object's destructor).
178      */
179     void setAutoRelease(bool flag);
180 
181     /** @brief Copies from host/device memory to OpenGL buffer.
182     @param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or std::vector ).
183     @param target Buffer usage. See cv::ogl::Buffer::Target .
184     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
185      */
186     void copyFrom(InputArray arr, Target target = ARRAY_BUFFER, bool autoRelease = false);
187 
188     /** @overload */
189     void copyFrom(InputArray arr, cuda::Stream& stream, Target target = ARRAY_BUFFER, bool autoRelease = false);
190 
191     /** @brief Copies from OpenGL buffer to host/device memory or another OpenGL buffer object.
192 
193     @param arr Destination array (host or device memory, can be Mat , cuda::GpuMat , std::vector or
194     ogl::Buffer ).
195      */
196     void copyTo(OutputArray arr) const;
197 
198     /** @overload */
199     void copyTo(OutputArray arr, cuda::Stream& stream) const;
200 
201     /** @brief Creates a full copy of the buffer object and the underlying data.
202 
203     @param target Buffer usage for destination buffer.
204     @param autoRelease Auto release mode for destination buffer.
205      */
206     Buffer clone(Target target = ARRAY_BUFFER, bool autoRelease = false) const;
207 
208     /** @brief Binds OpenGL buffer to the specified buffer binding point.
209 
210     @param target Binding point. See cv::ogl::Buffer::Target .
211      */
212     void bind(Target target) const;
213 
214     /** @brief Unbind any buffers from the specified binding point.
215 
216     @param target Binding point. See cv::ogl::Buffer::Target .
217      */
218     static void unbind(Target target);
219 
220     /** @brief Maps OpenGL buffer to host memory.
221 
222     mapHost maps to the client's address space the entire data store of the buffer object. The data can
223     then be directly read and/or written relative to the returned pointer, depending on the specified
224     access policy.
225 
226     A mapped data store must be unmapped with ogl::Buffer::unmapHost before its buffer object is used.
227 
228     This operation can lead to memory transfers between host and device.
229 
230     Only one buffer object can be mapped at a time.
231     @param access Access policy, indicating whether it will be possible to read from, write to, or both
232     read from and write to the buffer object's mapped data store. The symbolic constant must be
233     ogl::Buffer::READ_ONLY , ogl::Buffer::WRITE_ONLY or ogl::Buffer::READ_WRITE .
234      */
235     Mat mapHost(Access access);
236 
237     /** @brief Unmaps OpenGL buffer.
238     */
239     void unmapHost();
240 
241     //! map to device memory (blocking)
242     cuda::GpuMat mapDevice();
243     void unmapDevice();
244 
245     /** @brief Maps OpenGL buffer to CUDA device memory.
246 
247     This operatation doesn't copy data. Several buffer objects can be mapped to CUDA memory at a time.
248 
249     A mapped data store must be unmapped with ogl::Buffer::unmapDevice before its buffer object is used.
250      */
251     cuda::GpuMat mapDevice(cuda::Stream& stream);
252 
253     /** @brief Unmaps OpenGL buffer.
254     */
255     void unmapDevice(cuda::Stream& stream);
256 
257     int rows() const;
258     int cols() const;
259     Size size() const;
260     bool empty() const;
261 
262     int type() const;
263     int depth() const;
264     int channels() const;
265     int elemSize() const;
266     int elemSize1() const;
267 
268     //! get OpenGL opject id
269     unsigned int bufId() const;
270 
271     class Impl;
272 
273 private:
274     Ptr<Impl> impl_;
275     int rows_;
276     int cols_;
277     int type_;
278 };
279 
280 /** @brief Smart pointer for OpenGL 2D texture memory with reference counting.
281  */
282 class CV_EXPORTS Texture2D
283 {
284 public:
285     /** @brief An Image Format describes the way that the images in Textures store their data.
286     */
287     enum Format
288     {
289         NONE            = 0,
290         DEPTH_COMPONENT = 0x1902, //!< Depth
291         RGB             = 0x1907, //!< Red, Green, Blue
292         RGBA            = 0x1908  //!< Red, Green, Blue, Alpha
293     };
294 
295     /** @brief The constructors.
296 
297     Creates empty ogl::Texture2D object, allocates memory for ogl::Texture2D object or copies from
298     host/device memory.
299      */
300     Texture2D();
301 
302     /** @overload */
303     Texture2D(int arows, int acols, Format aformat, unsigned int atexId, bool autoRelease = false);
304 
305     /** @overload */
306     Texture2D(Size asize, Format aformat, unsigned int atexId, bool autoRelease = false);
307 
308     /** @overload
309     @param arows Number of rows.
310     @param acols Number of columns.
311     @param aformat Image format. See cv::ogl::Texture2D::Format .
312     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
313     */
314     Texture2D(int arows, int acols, Format aformat, bool autoRelease = false);
315 
316     /** @overload
317     @param asize 2D array size.
318     @param aformat Image format. See cv::ogl::Texture2D::Format .
319     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
320     */
321     Texture2D(Size asize, Format aformat, bool autoRelease = false);
322 
323     /** @overload
324     @param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or ogl::Buffer ).
325     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
326     */
327     explicit Texture2D(InputArray arr, bool autoRelease = false);
328 
329     /** @brief Allocates memory for ogl::Texture2D object.
330 
331     @param arows Number of rows.
332     @param acols Number of columns.
333     @param aformat Image format. See cv::ogl::Texture2D::Format .
334     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
335      */
336     void create(int arows, int acols, Format aformat, bool autoRelease = false);
337     /** @overload
338     @param asize 2D array size.
339     @param aformat Image format. See cv::ogl::Texture2D::Format .
340     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
341     */
342     void create(Size asize, Format aformat, bool autoRelease = false);
343 
344     /** @brief Decrements the reference counter and destroys the texture object if needed.
345 
346     The function will call setAutoRelease(true) .
347      */
348     void release();
349 
350     /** @brief Sets auto release mode.
351 
352     @param flag Auto release mode (if true, release will be called in object's destructor).
353 
354     The lifetime of the OpenGL object is tied to the lifetime of the context. If OpenGL context was
355     bound to a window it could be released at any time (user can close a window). If object's destructor
356     is called after destruction of the context it will cause an error. Thus ogl::Texture2D doesn't
357     destroy OpenGL object in destructor by default (all OpenGL resources will be released with OpenGL
358     context). This function can force ogl::Texture2D destructor to destroy OpenGL object.
359      */
360     void setAutoRelease(bool flag);
361 
362     /** @brief Copies from host/device memory to OpenGL texture.
363 
364     @param arr Input array (host or device memory, it can be Mat , cuda::GpuMat or ogl::Buffer ).
365     @param autoRelease Auto release mode (if true, release will be called in object's destructor).
366      */
367     void copyFrom(InputArray arr, bool autoRelease = false);
368 
369     /** @brief Copies from OpenGL texture to host/device memory or another OpenGL texture object.
370 
371     @param arr Destination array (host or device memory, can be Mat , cuda::GpuMat , ogl::Buffer or
372     ogl::Texture2D ).
373     @param ddepth Destination depth.
374     @param autoRelease Auto release mode for destination buffer (if arr is OpenGL buffer or texture).
375      */
376     void copyTo(OutputArray arr, int ddepth = CV_32F, bool autoRelease = false) const;
377 
378     /** @brief Binds texture to current active texture unit for GL_TEXTURE_2D target.
379     */
380     void bind() const;
381 
382     int rows() const;
383     int cols() const;
384     Size size() const;
385     bool empty() const;
386 
387     Format format() const;
388 
389     //! get OpenGL opject id
390     unsigned int texId() const;
391 
392     class Impl;
393 
394 private:
395     Ptr<Impl> impl_;
396     int rows_;
397     int cols_;
398     Format format_;
399 };
400 
401 /** @brief Wrapper for OpenGL Client-Side Vertex arrays.
402 
403 ogl::Arrays stores vertex data in ogl::Buffer objects.
404  */
405 class CV_EXPORTS Arrays
406 {
407 public:
408     /** @brief Default constructor
409      */
410     Arrays();
411 
412     /** @brief Sets an array of vertex coordinates.
413     @param vertex array with vertex coordinates, can be both host and device memory.
414     */
415     void setVertexArray(InputArray vertex);
416 
417     /** @brief Resets vertex coordinates.
418     */
419     void resetVertexArray();
420 
421     /** @brief Sets an array of vertex colors.
422     @param color array with vertex colors, can be both host and device memory.
423      */
424     void setColorArray(InputArray color);
425 
426     /** @brief Resets vertex colors.
427     */
428     void resetColorArray();
429 
430     /** @brief Sets an array of vertex normals.
431     @param normal array with vertex normals, can be both host and device memory.
432      */
433     void setNormalArray(InputArray normal);
434 
435     /** @brief Resets vertex normals.
436     */
437     void resetNormalArray();
438 
439     /** @brief Sets an array of vertex texture coordinates.
440     @param texCoord array with vertex texture coordinates, can be both host and device memory.
441      */
442     void setTexCoordArray(InputArray texCoord);
443 
444     /** @brief Resets vertex texture coordinates.
445     */
446     void resetTexCoordArray();
447 
448     /** @brief Releases all inner buffers.
449     */
450     void release();
451 
452     /** @brief Sets auto release mode all inner buffers.
453     @param flag Auto release mode.
454      */
455     void setAutoRelease(bool flag);
456 
457     /** @brief Binds all vertex arrays.
458     */
459     void bind() const;
460 
461     /** @brief Returns the vertex count.
462     */
463     int size() const;
464     bool empty() const;
465 
466 private:
467     int size_;
468     Buffer vertex_;
469     Buffer color_;
470     Buffer normal_;
471     Buffer texCoord_;
472 };
473 
474 /////////////////// Render Functions ///////////////////
475 
476 //! render mode
477 enum RenderModes {
478     POINTS         = 0x0000,
479     LINES          = 0x0001,
480     LINE_LOOP      = 0x0002,
481     LINE_STRIP     = 0x0003,
482     TRIANGLES      = 0x0004,
483     TRIANGLE_STRIP = 0x0005,
484     TRIANGLE_FAN   = 0x0006,
485     QUADS          = 0x0007,
486     QUAD_STRIP     = 0x0008,
487     POLYGON        = 0x0009
488 };
489 
490 /** @brief Render OpenGL texture or primitives.
491 @param tex Texture to draw.
492 @param wndRect Region of window, where to draw a texture (normalized coordinates).
493 @param texRect Region of texture to draw (normalized coordinates).
494  */
495 CV_EXPORTS void render(const Texture2D& tex,
496     Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
497     Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
498 
499 /** @overload
500 @param arr Array of privitives vertices.
501 @param mode Render mode. One of cv::ogl::RenderModes
502 @param color Color for all vertices. Will be used if arr doesn't contain color array.
503 */
504 CV_EXPORTS void render(const Arrays& arr, int mode = POINTS, Scalar color = Scalar::all(255));
505 
506 /** @overload
507 @param arr Array of privitives vertices.
508 @param indices Array of vertices indices (host or device memory).
509 @param mode Render mode. One of cv::ogl::RenderModes
510 @param color Color for all vertices. Will be used if arr doesn't contain color array.
511 */
512 CV_EXPORTS void render(const Arrays& arr, InputArray indices, int mode = POINTS, Scalar color = Scalar::all(255));
513 
514 //! @} core_opengl
515 
516 }} // namespace cv::ogl
517 
518 namespace cv { namespace cuda {
519 
520 //! @addtogroup cuda
521 //! @{
522 
523 /** @brief Sets a CUDA device and initializes it for the current thread with OpenGL interoperability.
524 
525 This function should be explicitly called after OpenGL context creation and before any CUDA calls.
526 @param device System index of a CUDA device starting with 0.
527 @ingroup core_opengl
528  */
529 CV_EXPORTS void setGlDevice(int device = 0);
530 
531 //! @}
532 
533 }}
534 
535 //! @cond IGNORED
536 
537 ////////////////////////////////////////////////////////////////////////
538 ////////////////////////////////////////////////////////////////////////
539 ////////////////////////////////////////////////////////////////////////
540 
541 inline
Buffer(int arows,int acols,int atype,Target target,bool autoRelease)542 cv::ogl::Buffer::Buffer(int arows, int acols, int atype, Target target, bool autoRelease) : rows_(0), cols_(0), type_(0)
543 {
544     create(arows, acols, atype, target, autoRelease);
545 }
546 
547 inline
Buffer(Size asize,int atype,Target target,bool autoRelease)548 cv::ogl::Buffer::Buffer(Size asize, int atype, Target target, bool autoRelease) : rows_(0), cols_(0), type_(0)
549 {
550     create(asize, atype, target, autoRelease);
551 }
552 
553 inline
create(Size asize,int atype,Target target,bool autoRelease)554 void cv::ogl::Buffer::create(Size asize, int atype, Target target, bool autoRelease)
555 {
556     create(asize.height, asize.width, atype, target, autoRelease);
557 }
558 
559 inline
rows() const560 int cv::ogl::Buffer::rows() const
561 {
562     return rows_;
563 }
564 
565 inline
cols() const566 int cv::ogl::Buffer::cols() const
567 {
568     return cols_;
569 }
570 
571 inline
size() const572 cv::Size cv::ogl::Buffer::size() const
573 {
574     return Size(cols_, rows_);
575 }
576 
577 inline
empty() const578 bool cv::ogl::Buffer::empty() const
579 {
580     return rows_ == 0 || cols_ == 0;
581 }
582 
583 inline
type() const584 int cv::ogl::Buffer::type() const
585 {
586     return type_;
587 }
588 
589 inline
depth() const590 int cv::ogl::Buffer::depth() const
591 {
592     return CV_MAT_DEPTH(type_);
593 }
594 
595 inline
channels() const596 int cv::ogl::Buffer::channels() const
597 {
598     return CV_MAT_CN(type_);
599 }
600 
601 inline
elemSize() const602 int cv::ogl::Buffer::elemSize() const
603 {
604     return CV_ELEM_SIZE(type_);
605 }
606 
607 inline
elemSize1() const608 int cv::ogl::Buffer::elemSize1() const
609 {
610     return CV_ELEM_SIZE1(type_);
611 }
612 
613 ///////
614 
615 inline
Texture2D(int arows,int acols,Format aformat,bool autoRelease)616 cv::ogl::Texture2D::Texture2D(int arows, int acols, Format aformat, bool autoRelease) : rows_(0), cols_(0), format_(NONE)
617 {
618     create(arows, acols, aformat, autoRelease);
619 }
620 
621 inline
Texture2D(Size asize,Format aformat,bool autoRelease)622 cv::ogl::Texture2D::Texture2D(Size asize, Format aformat, bool autoRelease) : rows_(0), cols_(0), format_(NONE)
623 {
624     create(asize, aformat, autoRelease);
625 }
626 
627 inline
create(Size asize,Format aformat,bool autoRelease)628 void cv::ogl::Texture2D::create(Size asize, Format aformat, bool autoRelease)
629 {
630     create(asize.height, asize.width, aformat, autoRelease);
631 }
632 
633 inline
rows() const634 int cv::ogl::Texture2D::rows() const
635 {
636     return rows_;
637 }
638 
639 inline
cols() const640 int cv::ogl::Texture2D::cols() const
641 {
642     return cols_;
643 }
644 
645 inline
size() const646 cv::Size cv::ogl::Texture2D::size() const
647 {
648     return Size(cols_, rows_);
649 }
650 
651 inline
empty() const652 bool cv::ogl::Texture2D::empty() const
653 {
654     return rows_ == 0 || cols_ == 0;
655 }
656 
657 inline
format() const658 cv::ogl::Texture2D::Format cv::ogl::Texture2D::format() const
659 {
660     return format_;
661 }
662 
663 ///////
664 
665 inline
Arrays()666 cv::ogl::Arrays::Arrays() : size_(0)
667 {
668 }
669 
670 inline
size() const671 int cv::ogl::Arrays::size() const
672 {
673     return size_;
674 }
675 
676 inline
empty() const677 bool cv::ogl::Arrays::empty() const
678 {
679     return size_ == 0;
680 }
681 
682 //! @endcond
683 
684 #endif /* __OPENCV_CORE_OPENGL_HPP__ */
685