moved OpenGL wrappers to separate header
authorVladislav Vinogradov <no@email>
Wed, 30 Nov 2011 06:20:29 +0000 (06:20 +0000)
committerVladislav Vinogradov <no@email>
Wed, 30 Nov 2011 06:20:29 +0000 (06:20 +0000)
added GlBuffer, GlTexture and GpuMat support to InputArray
replaced addTextOpenGl function by render + GlFont

13 files changed:
modules/core/include/opencv2/core/core.hpp
modules/core/include/opencv2/core/gpumat.hpp
modules/core/include/opencv2/core/internal.hpp
modules/core/include/opencv2/core/opengl_interop.hpp [new file with mode: 0644]
modules/core/src/gpumat.cpp
modules/core/src/matrix.cpp
modules/core/src/opengl_interop.cpp [new file with mode: 0644]
modules/highgui/include/opencv2/highgui/highgui.hpp
modules/highgui/include/opencv2/highgui/highgui_c.h
modules/highgui/src/window.cpp
modules/highgui/src/window_w32.cpp
samples/cpp/point_cloud.cpp
samples/gpu/highgui_gpu.cpp

index d7d98d0..6e7801f 100644 (file)
@@ -90,6 +90,11 @@ class Mat;
 class SparseMat;
 typedef Mat MatND;
 
+class GlBuffer;
+class GlTexture;
+class GlArrays;
+class GlCamera;
+
 namespace gpu {
     class GpuMat;
 }
@@ -1273,10 +1278,19 @@ protected:
 class CV_EXPORTS _InputArray
 {
 public:
-    enum { KIND_SHIFT=16, NONE=0<<KIND_SHIFT, MAT=1<<KIND_SHIFT,
-        MATX=2<<KIND_SHIFT, STD_VECTOR=3<<KIND_SHIFT,
-        STD_VECTOR_VECTOR=4<<KIND_SHIFT,
-        STD_VECTOR_MAT=5<<KIND_SHIFT, EXPR=6<<KIND_SHIFT };
+    enum { 
+        KIND_SHIFT = 16, 
+        NONE              = 0 << KIND_SHIFT, 
+        MAT               = 1 << KIND_SHIFT,
+        MATX              = 2 << KIND_SHIFT, 
+        STD_VECTOR        = 3 << KIND_SHIFT,
+        STD_VECTOR_VECTOR = 4 << KIND_SHIFT,
+        STD_VECTOR_MAT    = 5 << KIND_SHIFT, 
+        EXPR              = 6 << KIND_SHIFT, 
+        OPENGL_BUFFER     = 7 << KIND_SHIFT, 
+        OPENGL_TEXTURE    = 8 << KIND_SHIFT, 
+        GPU_MAT           = 9 << KIND_SHIFT
+    };
     _InputArray();
     _InputArray(const Mat& m);
     _InputArray(const MatExpr& expr);
@@ -1287,8 +1301,16 @@ public:
     template<typename _Tp, int m, int n> _InputArray(const Matx<_Tp, m, n>& matx);
     _InputArray(const Scalar& s);
     _InputArray(const double& val);
+    _InputArray(const GlBuffer& buf);
+    _InputArray(const GlTexture& tex);
+    _InputArray(const gpu::GpuMat& d_mat);
+
     virtual Mat getMat(int i=-1) const;
     virtual void getMatVector(vector<Mat>& mv) const;
+    virtual GlBuffer getGlBuffer() const;
+    virtual GlTexture getGlTexture() const;
+    virtual gpu::GpuMat getGpuMat() const;
+
     virtual int kind() const;
     virtual Size size(int i=-1) const;
     virtual size_t total(int i=-1) const;
index 6682fff..9e12f67 100644 (file)
@@ -50,9 +50,6 @@
 \r
 namespace cv { namespace gpu\r
 {\r
-    ////////////////////////////////////////////////////////////////////////\r
-    // GpuMat\r
-\r
     //! Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat.\r
     class CV_EXPORTS GpuMat\r
     {\r
@@ -218,285 +215,9 @@ namespace cv { namespace gpu
     CV_EXPORTS void ensureSizeIsEnough(Size size, int type, GpuMat& m);\r
 \r
     ////////////////////////////////////////////////////////////////////////\r
-    // OpenGL\r
-\r
-    //! set a CUDA device to use OpenGL interoperability\r
-    CV_EXPORTS void setGlDevice(int device = 0);\r
-\r
-    //! Smart pointer for OpenGL buffer memory with reference counting.\r
-    class CV_EXPORTS GlBuffer\r
-    {\r
-    public:\r
-        enum Usage\r
-        {\r
-            ARRAY_BUFFER = 0x8892,  // buffer will use for OpenGL arrays (vertices, colors, normals, etc)\r
-            TEXTURE_BUFFER = 0x88EC // buffer will ise for OpenGL textures\r
-        };\r
-\r
-        //! create empty buffer\r
-        explicit GlBuffer(Usage usage);\r
-\r
-        //! create buffer\r
-        GlBuffer(int rows, int cols, int type, Usage usage);\r
-        GlBuffer(Size size, int type, Usage usage);\r
-\r
-        //! copy from host/device memory\r
-        GlBuffer(InputArray mat, Usage usage);\r
-        GlBuffer(const GpuMat& d_mat, Usage usage);\r
-\r
-        GlBuffer(const GlBuffer& other);\r
-\r
-        ~GlBuffer();\r
-\r
-        GlBuffer& operator =(const GlBuffer& other);\r
-\r
-        void create(int rows, int cols, int type, Usage usage);\r
-        inline void create(Size size, int type, Usage usage) { create(size.height, size.width, type, usage); }\r
-        inline void create(int rows, int cols, int type) { create(rows, cols, type, usage()); }\r
-        inline void create(Size size, int type) { create(size.height, size.width, type, usage()); }\r
-\r
-        void release();\r
-\r
-        //! copy from host/device memory\r
-        void copyFrom(InputArray mat);\r
-        void copyFrom(const GpuMat& d_mat);\r
-\r
-        void bind() const;\r
-        void unbind() const;\r
-\r
-        //! map to host memory\r
-        Mat mapHost();\r
-        void unmapHost();\r
-\r
-        //! map to device memory\r
-        GpuMat mapDevice();\r
-        void unmapDevice();\r
-        \r
-        int rows;\r
-        int cols;\r
-\r
-        inline Size size() const { return Size(cols, rows); }\r
-        inline bool empty() const { return rows == 0 || cols == 0; }\r
-\r
-        inline int type() const { return type_; }\r
-        inline int depth() const { return CV_MAT_DEPTH(type_); }\r
-        inline int channels() const { return CV_MAT_CN(type_); }\r
-        inline int elemSize() const { return CV_ELEM_SIZE(type_); }\r
-        inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }\r
-\r
-        inline Usage usage() const { return usage_; }\r
-\r
-    private:\r
-        int type_;\r
-        Usage usage_;\r
-\r
-        class Impl;\r
-        Ptr<Impl> impl_;\r
-    };\r
-\r
-    //! Smart pointer for OpenGL 2d texture memory with reference counting.\r
-    class CV_EXPORTS GlTexture\r
-    {\r
-    public:\r
-        //! create empty texture\r
-        GlTexture();\r
-\r
-        //! create texture\r
-        GlTexture(int rows, int cols, int type);\r
-        GlTexture(Size size, int type);\r
-\r
-        //! copy from host/device memory\r
-        explicit GlTexture(InputArray mat, bool bgra = true);\r
-        explicit GlTexture(const GlBuffer& buf, bool bgra = true);\r
-\r
-        GlTexture(const GlTexture& other);\r
-\r
-        ~GlTexture();\r
-\r
-        GlTexture& operator =(const GlTexture& other);\r
-\r
-        void create(int rows, int cols, int type);\r
-        inline void create(Size size, int type) { create(size.height, size.width, type); }\r
-        void release();\r
-\r
-        //! copy from host/device memory\r
-        void copyFrom(InputArray mat, bool bgra = true);\r
-        void copyFrom(const GlBuffer& buf, bool bgra = true);\r
-\r
-        void bind() const;\r
-        void unbind() const;\r
-\r
-        int rows;\r
-        int cols;\r
-\r
-        inline Size size() const { return Size(cols, rows); }\r
-        inline bool empty() const { return rows == 0 || cols == 0; }\r
-\r
-        inline int type() const { return type_; }\r
-        inline int depth() const { return CV_MAT_DEPTH(type_); }\r
-        inline int channels() const { return CV_MAT_CN(type_); }\r
-        inline int elemSize() const { return CV_ELEM_SIZE(type_); }\r
-        inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }\r
-\r
-    private:\r
-        int type_;\r
-\r
-        class Impl;\r
-        Ptr<Impl> impl_;\r
-    };\r
-\r
-    //! OpenGL Arrays\r
-    class CV_EXPORTS GlArrays\r
-    {\r
-    public:\r
-        inline GlArrays() \r
-            : vertex_(GlBuffer::ARRAY_BUFFER), color_(GlBuffer::ARRAY_BUFFER), bgra_(true), normal_(GlBuffer::ARRAY_BUFFER), texCoord_(GlBuffer::ARRAY_BUFFER)\r
-        {\r
-        }\r
-\r
-        void setVertexArray(const GlBuffer& vertex);\r
-        void setVertexArray(const GpuMat& vertex);\r
-        void setVertexArray(InputArray vertex);\r
-        inline void resetVertexArray() { vertex_.release(); }\r
-\r
-        void setColorArray(const GlBuffer& color, bool bgra = true);\r
-        void setColorArray(const GpuMat& color, bool bgra = true);\r
-        void setColorArray(InputArray color, bool bgra = true);\r
-        inline void resetColorArray() { color_.release(); }\r
-        \r
-        void setNormalArray(const GlBuffer& normal);\r
-        void setNormalArray(const GpuMat& normal);\r
-        void setNormalArray(InputArray normal);\r
-        inline void resetNormalArray() { normal_.release(); }\r
-        \r
-        void setTexCoordArray(const GlBuffer& texCoord);\r
-        void setTexCoordArray(const GpuMat& texCoord);\r
-        void setTexCoordArray(InputArray texCoord);\r
-        inline void resetTexCoordArray() { texCoord_.release(); }\r
-\r
-        void bind() const;\r
-        void unbind() const;\r
-\r
-        inline int rows() const { return vertex_.rows; }\r
-        inline int cols() const { return vertex_.cols; }\r
-        inline Size size() const { return vertex_.size(); }\r
-        inline bool empty() const { return vertex_.empty(); }\r
-\r
-    private:\r
-        GlBuffer vertex_;\r
-        GlBuffer color_;\r
-        bool bgra_;\r
-        GlBuffer normal_;\r
-        GlBuffer texCoord_;\r
-    };\r
-\r
-    //! render functions\r
-\r
-    //! render texture rectangle in window\r
-    CV_EXPORTS void render(const GlTexture& tex, \r
-        Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0), \r
-        Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));\r
-\r
-    //! render mode\r
-    namespace RenderMode {\r
-        enum {\r
-            POINTS         = 0x0000,\r
-            LINES          = 0x0001,\r
-            LINE_LOOP      = 0x0002,\r
-            LINE_STRIP     = 0x0003,\r
-            TRIANGLES      = 0x0004,\r
-            TRIANGLE_STRIP = 0x0005,\r
-            TRIANGLE_FAN   = 0x0006,\r
-            QUADS          = 0x0007,\r
-            QUAD_STRIP     = 0x0008,\r
-            POLYGON        = 0x0009\r
-        };\r
-    }\r
-\r
-    //! render OpenGL arrays\r
-    CV_EXPORTS void render(const GlArrays& arr, int mode = RenderMode::POINTS);\r
-\r
-    //! OpenGL camera\r
-    class CV_EXPORTS GlCamera\r
-    {\r
-    public:\r
-        GlCamera();\r
-\r
-        void lookAt(Point3d eye, Point3d center, Point3d up);\r
-        void setCameraPos(Point3d pos, double yaw, double pitch, double roll);\r
-\r
-        void setScale(Point3d scale);\r
-\r
-        void setProjectionMatrix(const Mat& projectionMatrix, bool transpose = true);\r
-        void setPerspectiveProjection(double fov, double aspect, double zNear, double zFar);\r
-        void setOrthoProjection(double left, double right, double bottom, double top, double zNear, double zFar);\r
-\r
-        void setupProjectionMatrix() const;\r
-        void setupModelViewMatrix() const;\r
-\r
-    private:\r
-        Point3d eye_;\r
-        Point3d center_;\r
-        Point3d up_;\r
-\r
-        Point3d pos_;\r
-        double yaw_;\r
-        double pitch_;\r
-        double roll_;\r
-\r
-        bool useLookAtParams_;\r
-\r
-        Point3d scale_;\r
-\r
-        Mat projectionMatrix_;\r
-\r
-        double fov_;\r
-        double aspect_;\r
-\r
-        double left_;\r
-        double right_;\r
-        double bottom_;\r
-        double top_;\r
-\r
-        double zNear_;\r
-        double zFar_;\r
-\r
-        bool perspectiveProjection_;\r
-    };\r
-\r
-    //! OpenGL extension table\r
-    class CV_EXPORTS GlFuncTab\r
-    {\r
-    public:\r
-        virtual ~GlFuncTab();\r
-\r
-        virtual void genBuffers(int n, unsigned int* buffers) const = 0;        \r
-        virtual void deleteBuffers(int n, const unsigned int* buffers) const = 0;\r
-\r
-        virtual void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const = 0;\r
-        virtual void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const = 0;\r
-\r
-        virtual void bindBuffer(unsigned int target, unsigned int buffer) const = 0;\r
-\r
-        virtual void* mapBuffer(unsigned int target, unsigned int access) const = 0;\r
-        virtual void unmapBuffer(unsigned int target) const = 0;\r
-\r
-        virtual bool isGlContextInitialized() const = 0;\r
-    };\r
-\r
-    CV_EXPORTS void setGlFuncTab(const GlFuncTab* tab);\r
-\r
-    ////////////////////////////////////////////////////////////////////////\r
     // Error handling\r
 \r
     CV_EXPORTS void error(const char* error_string, const char* file, const int line, const char* func = "");\r
-    CV_EXPORTS bool checkGlError(const char* file, const int line, const char* func = "");\r
-\r
-    #if defined(__GNUC__)\r
-        #define CV_CheckGlError() CV_DbgAssert( (cv::gpu::checkGlError(__FILE__, __LINE__, __func__)) )\r
-    #else\r
-        #define CV_CheckGlError() CV_DbgAssert( (cv::gpu::checkGlError(__FILE__, __LINE__)) )\r
-    #endif\r
 \r
     ////////////////////////////////////////////////////////////////////////\r
     ////////////////////////////////////////////////////////////////////////\r
index 7c9a900..23bba05 100644 (file)
@@ -708,4 +708,36 @@ CvBigFuncTable;
     (tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG;  \
     (tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG
 
+//! OpenGL extension table
+class CV_EXPORTS CvOpenGlFuncTab
+{
+public:
+    virtual ~CvOpenGlFuncTab();
+
+    virtual void genBuffers(int n, unsigned int* buffers) const = 0;        
+    virtual void deleteBuffers(int n, const unsigned int* buffers) const = 0;
+
+    virtual void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const = 0;
+    virtual void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const = 0;
+
+    virtual void bindBuffer(unsigned int target, unsigned int buffer) const = 0;
+
+    virtual void* mapBuffer(unsigned int target, unsigned int access) const = 0;
+    virtual void unmapBuffer(unsigned int target) const = 0;
+
+    virtual void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const = 0;
+
+    virtual bool isGlContextInitialized() const = 0;
+};
+
+CV_EXPORTS void icvSetOpenGlFuncTab(const CvOpenGlFuncTab* tab);
+
+CV_EXPORTS bool icvCheckGlError(const char* file, const int line, const char* func = "");
+
+#if defined(__GNUC__)
+    #define CV_CheckGlError() CV_DbgAssert( (::icvCheckGlError(__FILE__, __LINE__, __func__)) )
+#else
+    #define CV_CheckGlError() CV_DbgAssert( (::icvCheckGlError(__FILE__, __LINE__)) )
+#endif
+
 #endif
diff --git a/modules/core/include/opencv2/core/opengl_interop.hpp b/modules/core/include/opencv2/core/opengl_interop.hpp
new file mode 100644 (file)
index 0000000..59fcbfc
--- /dev/null
@@ -0,0 +1,327 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                           License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other GpuMaterials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#ifndef __OPENCV_OPENGL_INTEROP_HPP__\r
+#define __OPENCV_OPENGL_INTEROP_HPP__\r
+\r
+#ifdef __cplusplus\r
+\r
+#include "opencv2/core/core.hpp"\r
+\r
+namespace cv \r
+{\r
+    //! Smart pointer for OpenGL buffer memory with reference counting.\r
+    class CV_EXPORTS GlBuffer\r
+    {\r
+    public:\r
+        enum Usage\r
+        {\r
+            ARRAY_BUFFER = 0x8892,  // buffer will use for OpenGL arrays (vertices, colors, normals, etc)\r
+            TEXTURE_BUFFER = 0x88EC // buffer will ise for OpenGL textures\r
+        };\r
+\r
+        //! create empty buffer\r
+        explicit GlBuffer(Usage usage);\r
+\r
+        //! create buffer\r
+        GlBuffer(int rows, int cols, int type, Usage usage);\r
+        GlBuffer(Size size, int type, Usage usage);\r
+\r
+        //! copy from host/device memory\r
+        GlBuffer(InputArray mat, Usage usage);\r
+\r
+        void create(int rows, int cols, int type, Usage usage);\r
+        inline void create(Size size, int type, Usage usage) { create(size.height, size.width, type, usage); }\r
+        inline void create(int rows, int cols, int type) { create(rows, cols, type, usage()); }\r
+        inline void create(Size size, int type) { create(size.height, size.width, type, usage()); }\r
+\r
+        void release();\r
+\r
+        //! copy from host/device memory\r
+        void copyFrom(InputArray mat);\r
+\r
+        void bind() const;\r
+        void unbind() const;\r
+\r
+        //! map to host memory\r
+        Mat mapHost();\r
+        void unmapHost();\r
+\r
+        //! map to device memory\r
+        gpu::GpuMat mapDevice();\r
+        void unmapDevice();\r
+        \r
+        int rows;\r
+        int cols;\r
+\r
+        inline Size size() const { return Size(cols, rows); }\r
+        inline bool empty() const { return rows == 0 || cols == 0; }\r
+\r
+        inline int type() const { return type_; }\r
+        inline int depth() const { return CV_MAT_DEPTH(type_); }\r
+        inline int channels() const { return CV_MAT_CN(type_); }\r
+        inline int elemSize() const { return CV_ELEM_SIZE(type_); }\r
+        inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }\r
+\r
+        inline Usage usage() const { return usage_; }\r
+\r
+    private:\r
+        int type_;\r
+        Usage usage_;\r
+\r
+        class Impl;\r
+        Ptr<Impl> impl_;\r
+    };\r
+\r
+    template <> CV_EXPORTS void Ptr<GlBuffer::Impl>::delete_obj();\r
+\r
+    //! Smart pointer for OpenGL 2d texture memory with reference counting.\r
+    class CV_EXPORTS GlTexture\r
+    {\r
+    public:\r
+        //! create empty texture\r
+        GlTexture();\r
+\r
+        //! create texture\r
+        GlTexture(int rows, int cols, int type);\r
+        GlTexture(Size size, int type);\r
+\r
+        //! copy from host/device memory\r
+        explicit GlTexture(InputArray mat, bool bgra = true);\r
+\r
+        void create(int rows, int cols, int type);\r
+        inline void create(Size size, int type) { create(size.height, size.width, type); }\r
+        void release();\r
+\r
+        //! copy from host/device memory\r
+        void copyFrom(InputArray mat, bool bgra = true);\r
+\r
+        void bind() const;\r
+        void unbind() const;\r
+\r
+        int rows;\r
+        int cols;\r
+\r
+        inline Size size() const { return Size(cols, rows); }\r
+        inline bool empty() const { return rows == 0 || cols == 0; }\r
+\r
+        inline int type() const { return type_; }\r
+        inline int depth() const { return CV_MAT_DEPTH(type_); }\r
+        inline int channels() const { return CV_MAT_CN(type_); }\r
+        inline int elemSize() const { return CV_ELEM_SIZE(type_); }\r
+        inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }\r
+\r
+    private:\r
+        int type_;\r
+\r
+        class Impl;\r
+        Ptr<Impl> impl_;\r
+    };\r
+\r
+    template <> CV_EXPORTS void Ptr<GlTexture::Impl>::delete_obj();\r
+\r
+    //! OpenGL Arrays\r
+    class CV_EXPORTS GlArrays\r
+    {\r
+    public:\r
+        inline GlArrays() \r
+            : vertex_(GlBuffer::ARRAY_BUFFER), color_(GlBuffer::ARRAY_BUFFER), bgra_(true), normal_(GlBuffer::ARRAY_BUFFER), texCoord_(GlBuffer::ARRAY_BUFFER)\r
+        {\r
+        }\r
+\r
+        void setVertexArray(InputArray vertex);\r
+        inline void resetVertexArray() { vertex_.release(); }\r
+\r
+        void setColorArray(InputArray color, bool bgra = true);\r
+        inline void resetColorArray() { color_.release(); }\r
+        \r
+        void setNormalArray(InputArray normal);\r
+        inline void resetNormalArray() { normal_.release(); }\r
+        \r
+        void setTexCoordArray(InputArray texCoord);\r
+        inline void resetTexCoordArray() { texCoord_.release(); }\r
+\r
+        void bind() const;\r
+        void unbind() const;\r
+\r
+        inline int rows() const { return vertex_.rows; }\r
+        inline int cols() const { return vertex_.cols; }\r
+        inline Size size() const { return vertex_.size(); }\r
+        inline bool empty() const { return vertex_.empty(); }\r
+\r
+    private:\r
+        GlBuffer vertex_;\r
+        GlBuffer color_;\r
+        bool bgra_;\r
+        GlBuffer normal_;\r
+        GlBuffer texCoord_;\r
+    };\r
+\r
+    //! OpenGL Font\r
+    class CV_EXPORTS GlFont\r
+    {\r
+    public:\r
+        enum Weight \r
+        {\r
+            WEIGHT_LIGHT    = 300,\r
+            WEIGHT_NORMAL   = 400,\r
+            WEIGHT_SEMIBOLD = 600,\r
+            WEIGHT_BOLD     = 700,\r
+            WEIGHT_BLACK    = 900\r
+        };\r
+\r
+        enum Style \r
+        {  \r
+            STYLE_NORMAL    = 0,\r
+            STYLE_ITALIC    = 1,\r
+            STYLE_UNDERLINE = 2\r
+        };\r
+\r
+        static Ptr<GlFont> get(const std::string& family, int height = 12, Weight weight = WEIGHT_NORMAL, Style style = STYLE_NORMAL);\r
+\r
+        void draw(const char* str, int len) const;\r
+\r
+        inline const std::string& family() const { return family_; }\r
+        inline int height() const { return height_; }\r
+        inline Weight weight() const { return weight_; }\r
+        inline Style style() const { return style_; }\r
+\r
+    private:\r
+        GlFont(const std::string& family, int height, Weight weight, Style style);\r
+\r
+        std::string family_;\r
+        int height_;\r
+        Weight weight_;\r
+        Style style_;\r
+\r
+        unsigned int base_;\r
+\r
+        GlFont(const GlFont&);\r
+        GlFont& operator =(const GlFont&);\r
+    };\r
+\r
+    //! render functions\r
+\r
+    //! render texture rectangle in window\r
+    CV_EXPORTS void render(const GlTexture& tex, \r
+        Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0), \r
+        Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));\r
+\r
+    //! render mode\r
+    namespace RenderMode {\r
+        enum {\r
+            POINTS         = 0x0000,\r
+            LINES          = 0x0001,\r
+            LINE_LOOP      = 0x0002,\r
+            LINE_STRIP     = 0x0003,\r
+            TRIANGLES      = 0x0004,\r
+            TRIANGLE_STRIP = 0x0005,\r
+            TRIANGLE_FAN   = 0x0006,\r
+            QUADS          = 0x0007,\r
+            QUAD_STRIP     = 0x0008,\r
+            POLYGON        = 0x0009\r
+        };\r
+    }\r
+\r
+    //! render OpenGL arrays\r
+    CV_EXPORTS void render(const GlArrays& arr, int mode = RenderMode::POINTS);\r
+\r
+    CV_EXPORTS void render(const std::string& str, const Ptr<GlFont>& font, Scalar color, Point2d pos);\r
+\r
+    //! OpenGL camera\r
+    class CV_EXPORTS GlCamera\r
+    {\r
+    public:\r
+        GlCamera();\r
+\r
+        void lookAt(Point3d eye, Point3d center, Point3d up);\r
+        void setCameraPos(Point3d pos, double yaw, double pitch, double roll);\r
+\r
+        void setScale(Point3d scale);\r
+\r
+        void setProjectionMatrix(const Mat& projectionMatrix, bool transpose = true);\r
+        void setPerspectiveProjection(double fov, double aspect, double zNear, double zFar);\r
+        void setOrthoProjection(double left, double right, double bottom, double top, double zNear, double zFar);\r
+\r
+        void setupProjectionMatrix() const;\r
+        void setupModelViewMatrix() const;\r
+\r
+    private:\r
+        Point3d eye_;\r
+        Point3d center_;\r
+        Point3d up_;\r
+\r
+        Point3d pos_;\r
+        double yaw_;\r
+        double pitch_;\r
+        double roll_;\r
+\r
+        bool useLookAtParams_;\r
+\r
+        Point3d scale_;\r
+\r
+        Mat projectionMatrix_;\r
+\r
+        double fov_;\r
+        double aspect_;\r
+\r
+        double left_;\r
+        double right_;\r
+        double bottom_;\r
+        double top_;\r
+\r
+        double zNear_;\r
+        double zFar_;\r
+\r
+        bool perspectiveProjection_;\r
+    };\r
+\r
+    namespace gpu \r
+    {\r
+        //! set a CUDA device to use OpenGL interoperability\r
+        CV_EXPORTS void setGlDevice(int device = 0);\r
+    }\r
+} // namespace cv\r
+\r
+#endif // __cplusplus\r
+\r
+#endif // __OPENCV_OPENGL_INTEROP_HPP__\r
index f06772c..c591076 100644 (file)
     #include <npp.h>\r
 #endif\r
 \r
-#ifdef HAVE_OPENGL\r
-    #include <GL/gl.h>\r
-    #include <GL/glu.h>\r
-\r
-    #ifdef HAVE_CUDA\r
-        #include <cuda_gl_interop.h>\r
-    #endif\r
-#endif\r
-\r
 using namespace std;\r
 using namespace cv;\r
 using namespace cv::gpu;\r
 \r
-////////////////////////////////////////////////////////////////////////\r
-// GpuMat\r
-\r
 cv::gpu::GpuMat::GpuMat(const GpuMat& m)\r
     : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data), refcount(m.refcount), datastart(m.datastart), dataend(m.dataend)\r
 {\r
@@ -327,28 +315,25 @@ namespace
 \r
 #ifndef HAVE_CUDA\r
 \r
+#define throw_nocuda CV_Error(CV_GpuNotSupported, "The library is compiled without CUDA support")\r
+\r
 namespace\r
 {\r
-    void throw_nogpu()\r
-    {\r
-        CV_Error(CV_GpuNotSupported, "The library is compiled without GPU support");\r
-    }\r
-\r
     class EmptyFuncTable : public GpuFuncTable\r
     {\r
     public:\r
-        void copy(const Mat&, GpuMat&) const { throw_nogpu(); }\r
-        void copy(const GpuMat&, Mat&) const { throw_nogpu(); }\r
-        void copy(const GpuMat&, GpuMat&) const { throw_nogpu(); }\r
+        void copy(const Mat&, GpuMat&) const { throw_nocuda; }\r
+        void copy(const GpuMat&, Mat&) const { throw_nocuda; }\r
+        void copy(const GpuMat&, GpuMat&) const { throw_nocuda; }\r
 \r
-        void copyWithMask(const GpuMat&, GpuMat&, const GpuMat&) const { throw_nogpu(); }\r
+        void copyWithMask(const GpuMat&, GpuMat&, const GpuMat&) const { throw_nocuda; }\r
 \r
-        void convert(const GpuMat&, GpuMat&) const { throw_nogpu(); }\r
-        void convert(const GpuMat&, GpuMat&, double, double) const { throw_nogpu(); }\r
+        void convert(const GpuMat&, GpuMat&) const { throw_nocuda; }\r
+        void convert(const GpuMat&, GpuMat&, double, double) const { throw_nocuda; }\r
 \r
-        void setTo(GpuMat&, Scalar, const GpuMat&) const { throw_nogpu(); }\r
+        void setTo(GpuMat&, Scalar, const GpuMat&) const { throw_nocuda; }\r
 \r
-        void mallocPitch(void**, size_t*, size_t, size_t) const { throw_nogpu(); }\r
+        void mallocPitch(void**, size_t*, size_t, size_t) const { throw_nocuda; }\r
         void free(void*) const {}\r
     };\r
 \r
@@ -944,1405 +929,20 @@ void cv::gpu::GpuMat::release()
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
-// OpenGL\r
-\r
-namespace\r
-{\r
-    void throw_nogl()\r
-    {\r
-    #ifndef HAVE_OPENGL\r
-        CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");\r
-    #else\r
-        CV_Error(CV_OpenGlNotSupported, "OpenGL context doesn't exist");\r
-    #endif\r
-    }\r
-\r
-    class EmptyGlFuncTab : public GlFuncTab\r
-    {\r
-    public:\r
-        void genBuffers(int, unsigned int*) const { throw_nogl(); }\r
-        void deleteBuffers(int, const unsigned int*) const { throw_nogl(); }\r
-\r
-        void bufferData(unsigned int, ptrdiff_t, const void*, unsigned int) const { throw_nogl(); }\r
-        void bufferSubData(unsigned int, ptrdiff_t, ptrdiff_t, const void*) const { throw_nogl(); }\r
-\r
-        void bindBuffer(unsigned int, unsigned int) const { throw_nogl(); }\r
-\r
-        void* mapBuffer(unsigned int, unsigned int) const { throw_nogl(); return 0; }\r
-        void unmapBuffer(unsigned int) const { throw_nogl(); }\r
-\r
-        bool isGlContextInitialized() const { return false; }\r
-    };\r
-\r
-    const GlFuncTab* g_glFuncTab = 0;\r
-\r
-    const GlFuncTab* glFuncTab()\r
-    {\r
-        static EmptyGlFuncTab empty;\r
-        return g_glFuncTab ? g_glFuncTab : &empty;\r
-    }\r
-}\r
-\r
-cv::gpu::GlFuncTab::~GlFuncTab()\r
-{\r
-    if (g_glFuncTab == this)\r
-        g_glFuncTab = 0;\r
-}\r
-\r
-void cv::gpu::setGlFuncTab(const GlFuncTab* tab)\r
-{\r
-    g_glFuncTab = tab;\r
-}\r
-\r
-#ifdef HAVE_OPENGL\r
-    #ifndef GL_DYNAMIC_DRAW\r
-        #define GL_DYNAMIC_DRAW 0x88E8\r
-    #endif\r
-\r
-    #ifndef GL_READ_WRITE\r
-        #define GL_READ_WRITE 0x88BA\r
-    #endif\r
-\r
-    #ifndef GL_BGR\r
-        #define GL_BGR 0x80E0\r
-    #endif\r
-\r
-    #ifndef GL_BGRA\r
-        #define GL_BGRA 0x80E1\r
-    #endif\r
-\r
-    namespace\r
-    {\r
-        const GLenum gl_types[] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE};\r
-\r
-    #ifdef HAVE_CUDA\r
-        bool g_isCudaGlDeviceInitialized = false;\r
-    #endif\r
-    }\r
-#endif // HAVE_OPENGL\r
-\r
-\r
-\r
-void cv::gpu::setGlDevice(int device)\r
-{\r
-#ifndef HAVE_CUDA\r
-    throw_nogpu();\r
-#else\r
-    #ifndef HAVE_OPENGL\r
-        throw_nogl();\r
-    #else\r
-        if (!glFuncTab()->isGlContextInitialized())\r
-            throw_nogl();\r
-\r
-        cudaSafeCall( cudaGLSetGLDevice(device) );\r
-\r
-        g_isCudaGlDeviceInitialized = true;\r
-    #endif\r
-#endif\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////\r
-// CudaGlInterop\r
-\r
-#if defined HAVE_CUDA && defined HAVE_OPENGL\r
-namespace\r
-{\r
-    class CudaGlInterop\r
-    {\r
-    public:\r
-        CudaGlInterop();\r
-        ~CudaGlInterop();\r
-\r
-        void registerBuffer(unsigned int buffer);\r
-\r
-        void copyFrom(const GpuMat& mat, cudaStream_t stream = 0);\r
-\r
-        GpuMat map(int rows, int cols, int type, cudaStream_t stream = 0);\r
-        void unmap(cudaStream_t stream = 0);\r
-\r
-    private:\r
-        cudaGraphicsResource_t resource_;\r
-    };\r
-\r
-    inline CudaGlInterop::CudaGlInterop() : resource_(0)\r
-    {\r
-    }\r
-\r
-    CudaGlInterop::~CudaGlInterop()\r
-    {\r
-        if (resource_)\r
-        {\r
-            cudaGraphicsUnregisterResource(resource_);\r
-            resource_ = 0;\r
-        }\r
-    }\r
-\r
-    void CudaGlInterop::registerBuffer(unsigned int buffer)\r
-    {\r
-        if (!g_isCudaGlDeviceInitialized)\r
-            cvError(CV_GpuApiCallError, "registerBuffer", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
-\r
-        cudaGraphicsResource_t resource;\r
-        cudaSafeCall( cudaGraphicsGLRegisterBuffer(&resource, buffer, cudaGraphicsMapFlagsNone) );\r
-\r
-        resource_ = resource;\r
-    }\r
-\r
-    void CudaGlInterop::copyFrom(const GpuMat& mat, cudaStream_t stream)\r
-    {\r
-        CV_Assert(resource_ != 0);\r
-\r
-        cudaSafeCall( cudaGraphicsMapResources(1, &resource_, stream) );\r
-\r
-        void* dst_ptr;\r
-        size_t num_bytes;\r
-        cudaSafeCall( cudaGraphicsResourceGetMappedPointer(&dst_ptr, &num_bytes, resource_) );\r
-\r
-        const void* src_ptr = mat.ptr();\r
-        size_t widthBytes = mat.cols * mat.elemSize();\r
-\r
-        CV_Assert(widthBytes * mat.rows <= num_bytes);\r
-\r
-        if (stream == 0)\r
-            cudaSafeCall( cudaMemcpy2D(dst_ptr, widthBytes, src_ptr, mat.step, widthBytes, mat.rows, cudaMemcpyDeviceToDevice) );\r
-        else\r
-            cudaSafeCall( cudaMemcpy2DAsync(dst_ptr, widthBytes, src_ptr, mat.step, widthBytes, mat.rows, cudaMemcpyDeviceToDevice, stream) );\r
-\r
-        cudaGraphicsUnmapResources(1, &resource_, stream);\r
-    }\r
-\r
-    GpuMat CudaGlInterop::map(int rows, int cols, int type, cudaStream_t stream)\r
-    {\r
-        CV_Assert(resource_ != 0);\r
-\r
-        cudaSafeCall( cudaGraphicsMapResources(1, &resource_, stream) );\r
-\r
-        void* ptr;\r
-        size_t num_bytes;\r
-        cudaSafeCall( cudaGraphicsResourceGetMappedPointer(&ptr, &num_bytes, resource_) );\r
-\r
-        CV_Assert( static_cast<size_t>(cols) * CV_ELEM_SIZE(type) * rows <= num_bytes );\r
-\r
-        return GpuMat(rows, cols, type, ptr);\r
-    }\r
-\r
-    inline void CudaGlInterop::unmap(cudaStream_t stream)\r
-    {\r
-        cudaGraphicsUnmapResources(1, &resource_, stream);\r
-    }\r
-}\r
-#endif // HAVE_CUDA && HAVE_OPENGL\r
-\r
-////////////////////////////////////////////////////////////////////////\r
-// GlBuffer\r
-\r
-#ifndef HAVE_OPENGL\r
-\r
-class cv::gpu::GlBuffer::Impl\r
-{\r
-};\r
-\r
-#else\r
-\r
-class cv::gpu::GlBuffer::Impl\r
-{\r
-public:\r
-    Impl();\r
-    Impl(int rows, int cols, int type, unsigned int target);\r
-    Impl(const Mat& m, unsigned int target);\r
-    ~Impl();\r
-\r
-    void copyFrom(const Mat& m, unsigned int target);\r
-\r
-#ifdef HAVE_CUDA\r
-    void copyFrom(const GpuMat& mat, cudaStream_t stream = 0);\r
-#endif\r
-\r
-    void bind(unsigned int target) const;\r
-    void unbind(unsigned int target) const;\r
-\r
-    Mat mapHost(int rows, int cols, int type, unsigned int target);\r
-    void unmapHost(unsigned int target);\r
-\r
-#ifdef HAVE_CUDA\r
-    GpuMat mapDevice(int rows, int cols, int type, cudaStream_t stream = 0);\r
-    void unmapDevice(cudaStream_t stream = 0);\r
-#endif\r
-\r
-private:\r
-    unsigned int buffer_;\r
-\r
-#ifdef HAVE_CUDA\r
-    CudaGlInterop cudaGlInterop_;\r
-#endif\r
-};\r
-\r
-inline cv::gpu::GlBuffer::Impl::Impl() : buffer_(0)\r
-{\r
-}\r
-\r
-cv::gpu::GlBuffer::Impl::Impl(int rows, int cols, int type, unsigned int target) : buffer_(0)\r
-{\r
-    if (!glFuncTab()->isGlContextInitialized())\r
-        throw_nogl();\r
-\r
-    CV_DbgAssert(rows > 0 && cols > 0);\r
-    CV_DbgAssert(CV_MAT_DEPTH(type) >= 0 && CV_MAT_DEPTH(type) <= CV_64F);\r
-\r
-    glFuncTab()->genBuffers(1, &buffer_);\r
-    CV_CheckGlError();\r
-    CV_Assert(buffer_ != 0);\r
-\r
-    size_t size = rows * cols * CV_ELEM_SIZE(type);\r
-\r
-    glFuncTab()->bindBuffer(target, buffer_);\r
-    CV_CheckGlError();\r
-\r
-    glFuncTab()->bufferData(target, size, 0, GL_DYNAMIC_DRAW);\r
-    CV_CheckGlError();\r
-\r
-    glFuncTab()->bindBuffer(target, 0);\r
-\r
-#ifdef HAVE_CUDA\r
-    if (g_isCudaGlDeviceInitialized)\r
-        cudaGlInterop_.registerBuffer(buffer_);\r
-#endif\r
-}\r
+// Error handling\r
 \r
-cv::gpu::GlBuffer::Impl::Impl(const Mat& m, unsigned int target) : buffer_(0)\r
+void cv::gpu::error(const char *error_string, const char *file, const int line, const char *func)\r
 {\r
-    if (!glFuncTab()->isGlContextInitialized())\r
-        throw_nogl();\r
-\r
-    CV_DbgAssert(m.rows > 0 && m.cols > 0);\r
-    CV_DbgAssert(m.depth() >= 0 && m.depth() <= CV_64F);\r
-    CV_Assert(m.isContinuous());\r
-\r
-    glFuncTab()->genBuffers(1, &buffer_);\r
-    CV_CheckGlError();\r
-    CV_Assert(buffer_ != 0);\r
-\r
-    size_t size = m.rows * m.cols * m.elemSize();\r
-\r
-    glFuncTab()->bindBuffer(target, buffer_);\r
-    CV_CheckGlError();\r
-\r
-    glFuncTab()->bufferData(target, size, m.data, GL_DYNAMIC_DRAW);\r
-    CV_CheckGlError();\r
-\r
-    glFuncTab()->bindBuffer(target, 0);\r
-\r
-#ifdef HAVE_CUDA\r
-    if (g_isCudaGlDeviceInitialized)\r
-        cudaGlInterop_.registerBuffer(buffer_);\r
-#endif\r
-}\r
+    int code = CV_GpuApiCallError;\r
 \r
-cv::gpu::GlBuffer::Impl::~Impl()\r
-{\r
-    try\r
-    {\r
-        if (buffer_)\r
-            glFuncTab()->deleteBuffers(1, &buffer_);\r
-    }\r
-#ifdef _DEBUG\r
-    catch(const exception& e)\r
-    {\r
-        cerr << e.what() << endl;\r
-    }\r
-#endif\r
-    catch(...)\r
+    if (uncaught_exception())\r
     {\r
-    }\r
-}\r
-\r
-void cv::gpu::GlBuffer::Impl::copyFrom(const Mat& m, unsigned int target)\r
-{\r
-    CV_Assert(buffer_ != 0);\r
-\r
-    CV_Assert(m.isContinuous());\r
-\r
-    bind(target);\r
-\r
-    size_t size = m.rows * m.cols * m.elemSize();\r
-\r
-    glFuncTab()->bufferSubData(target, 0, size, m.data);\r
-    CV_CheckGlError();\r
-\r
-    unbind(target);\r
-}\r
-\r
-#ifdef HAVE_CUDA\r
-\r
-void cv::gpu::GlBuffer::Impl::copyFrom(const GpuMat& mat, cudaStream_t stream)\r
-{\r
-    if (!g_isCudaGlDeviceInitialized)\r
-        cvError(CV_GpuApiCallError, "copyFrom", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
-\r
-    CV_Assert(buffer_ != 0);\r
-\r
-    cudaGlInterop_.copyFrom(mat, stream);\r
-}\r
-\r
-#endif // HAVE_CUDA\r
-\r
-inline void cv::gpu::GlBuffer::Impl::bind(unsigned int target) const\r
-{\r
-    CV_Assert(buffer_ != 0);\r
-\r
-    glFuncTab()->bindBuffer(target, buffer_);\r
-    CV_CheckGlError();\r
-}\r
-\r
-inline void cv::gpu::GlBuffer::Impl::unbind(unsigned int target) const\r
-{\r
-    glFuncTab()->bindBuffer(target, 0);\r
-}\r
-\r
-inline Mat cv::gpu::GlBuffer::Impl::mapHost(int rows, int cols, int type, unsigned int target)\r
-{\r
-    void* ptr = glFuncTab()->mapBuffer(target, GL_READ_WRITE);\r
-    CV_CheckGlError();\r
-\r
-    return Mat(rows, cols, type, ptr);\r
-}\r
-\r
-inline void cv::gpu::GlBuffer::Impl::unmapHost(unsigned int target)\r
-{\r
-    glFuncTab()->unmapBuffer(target);\r
-}\r
-\r
-#ifdef HAVE_CUDA\r
-\r
-inline GpuMat cv::gpu::GlBuffer::Impl::mapDevice(int rows, int cols, int type, cudaStream_t stream)\r
-{\r
-    if (!g_isCudaGlDeviceInitialized)\r
-        cvError(CV_GpuApiCallError, "copyFrom", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
-\r
-    CV_Assert(buffer_ != 0);\r
-\r
-    return cudaGlInterop_.map(rows, cols, type, stream);\r
-}\r
-\r
-inline void cv::gpu::GlBuffer::Impl::unmapDevice(cudaStream_t stream)\r
-{\r
-    if (!g_isCudaGlDeviceInitialized)\r
-        cvError(CV_GpuApiCallError, "copyFrom", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
-\r
-    cudaGlInterop_.unmap(stream);\r
-}\r
-\r
-#endif // HAVE_CUDA\r
-\r
-#endif // HAVE_OPENGL\r
-\r
-cv::gpu::GlBuffer::GlBuffer(Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl;\r
-#endif\r
-}\r
-\r
-cv::gpu::GlBuffer::GlBuffer(int rows_, int cols_, int type, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl(rows_, cols_, type, usage);\r
-    rows = rows_;\r
-    cols = cols_;\r
-    type_ = type;\r
-#endif\r
-}\r
-\r
-cv::gpu::GlBuffer::GlBuffer(Size size, int type, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl(size.height, size.width, type, usage);\r
-    rows = size.height;\r
-    cols = size.width;\r
-    type_ = type;\r
-#endif\r
-}\r
-\r
-cv::gpu::GlBuffer::GlBuffer(InputArray mat_, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    Mat mat = mat_.getMat();\r
-    impl_ = new Impl(mat, usage);\r
-    rows = mat.rows;\r
-    cols = mat.cols;\r
-    type_ = mat.type();\r
-#endif\r
-}\r
-\r
-cv::gpu::GlBuffer::GlBuffer(const GpuMat& d_mat, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    #ifndef HAVE_CUDA\r
-        throw_nogpu();\r
-    #else\r
-        impl_ = new Impl(d_mat.rows, d_mat.cols, d_mat.type(), usage);\r
-        impl_->copyFrom(d_mat);\r
-        rows = d_mat.rows;\r
-        cols = d_mat.cols;\r
-        type_ = d_mat.type();\r
-    #endif\r
-#endif\r
-}\r
-\r
-cv::gpu::GlBuffer::GlBuffer(const GlBuffer& other)\r
-    : rows(other.rows), cols(other.cols), type_(other.type_), usage_(other.usage_), impl_(other.impl_)\r
-{\r
-}\r
-\r
-cv::gpu::GlBuffer::~GlBuffer()\r
-{\r
-}\r
-\r
-GlBuffer& cv::gpu::GlBuffer::operator =(const GlBuffer& other)\r
-{\r
-    rows = other.rows;\r
-    cols = other.cols;\r
-    type_ = other.type_;\r
-    usage_ = other.usage_;\r
-    impl_ = other.impl_;\r
-    return *this;\r
-}\r
+        const char* errorStr = cvErrorStr(code);\r
+        const char* function = func ? func : "unknown function";\r
 \r
-void cv::gpu::GlBuffer::create(int rows_, int cols_, int type, Usage usage)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    if (rows_ != rows || cols_ != cols || type_ != type || usage_ != usage)\r
-    {\r
-        impl_ = new Impl(rows_, cols_, type, usage);\r
-        rows = rows_;\r
-        cols = cols_;\r
-        type_ = type;\r
-        usage_ = usage;\r
-    }\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::release()\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl;\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::copyFrom(InputArray mat_)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    Mat mat = mat_.getMat();\r
-    create(mat.rows, mat.cols, mat.type());\r
-    impl_->copyFrom(mat, usage_);\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::copyFrom(const GpuMat& d_mat)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    #ifndef HAVE_CUDA\r
-        throw_nogpu();\r
-    #else\r
-        create(d_mat.rows, d_mat.cols, d_mat.type());\r
-        impl_->copyFrom(d_mat);\r
-    #endif\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::bind() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_->bind(usage_);\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::unbind() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_->unbind(usage_);\r
-#endif\r
-}\r
-\r
-Mat cv::gpu::GlBuffer::mapHost()\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-    return Mat();\r
-#else\r
-    return impl_->mapHost(rows, cols, type_, usage_);\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::unmapHost()\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_->unmapHost(usage_);\r
-#endif\r
-}\r
-\r
-GpuMat cv::gpu::GlBuffer::mapDevice()\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-    return GpuMat();\r
-#else\r
-    #ifndef HAVE_CUDA\r
-        throw_nogpu();\r
-        return GpuMat();\r
-    #else\r
-        return impl_->mapDevice(rows, cols, type_);\r
-    #endif\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlBuffer::unmapDevice()\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    #ifndef HAVE_CUDA\r
-        throw_nogpu();\r
-    #else\r
-        impl_->unmapDevice();\r
-    #endif\r
-#endif\r
-}\r
-\r
-//////////////////////////////////////////////////////////////////////////////////////////\r
-// GlTexture\r
-\r
-#ifndef HAVE_OPENGL\r
-\r
-class cv::gpu::GlTexture::Impl\r
-{\r
-};\r
-\r
-#else\r
-\r
-class cv::gpu::GlTexture::Impl\r
-{\r
-public:\r
-    Impl();\r
-\r
-    Impl(int rows, int cols, int type);\r
-\r
-    Impl(const Mat& mat, bool bgra);\r
-    Impl(const GlBuffer& buf, bool bgra);\r
-\r
-    ~Impl();\r
-\r
-    void copyFrom(const Mat& mat, bool bgra);\r
-    void copyFrom(const GlBuffer& buf, bool bgra);\r
-\r
-    void bind() const;\r
-    void unbind() const;\r
-\r
-private:\r
-    GLuint tex_;\r
-};\r
-\r
-inline cv::gpu::GlTexture::Impl::Impl() : tex_(0)\r
-{\r
-}\r
-\r
-cv::gpu::GlTexture::Impl::Impl(int rows, int cols, int type) : tex_(0)\r
-{\r
-    if (!glFuncTab()->isGlContextInitialized())\r
-        throw_nogl();\r
-\r
-    int depth = CV_MAT_DEPTH(type);\r
-    int cn = CV_MAT_CN(type);\r
-\r
-    CV_DbgAssert(rows > 0 && cols > 0);\r
-    CV_Assert(cn == 1 || cn == 3 || cn == 4);\r
-    CV_Assert(depth >= 0 && depth <= CV_32F);\r
-\r
-    glGenTextures(1, &tex_);\r
-    CV_CheckGlError();\r
-    CV_Assert(tex_ != 0);\r
-\r
-    glBindTexture(GL_TEXTURE_2D, tex_);\r
-    CV_CheckGlError();\r
-\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
-    CV_CheckGlError();\r
-\r
-    GLenum format = cn == 1 ? GL_LUMINANCE : cn == 3 ? GL_BGR : GL_BGRA;\r
-\r
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
-    CV_CheckGlError();\r
-\r
-    glTexImage2D(GL_TEXTURE_2D, 0, cn, cols, rows, 0, format, gl_types[depth], 0);\r
-    CV_CheckGlError();\r
-}\r
-\r
-cv::gpu::GlTexture::Impl::Impl(const Mat& mat, bool bgra) : tex_(0)\r
-{\r
-    if (!glFuncTab()->isGlContextInitialized())\r
-        throw_nogl();\r
-\r
-    int depth = mat.depth();\r
-    int cn = mat.channels();\r
-\r
-    CV_DbgAssert(mat.rows > 0 && mat.cols > 0);\r
-    CV_Assert(cn == 1 || cn == 3 || cn == 4);\r
-    CV_Assert(depth >= 0 && depth <= CV_32F);\r
-    CV_Assert(mat.isContinuous());\r
-\r
-    glGenTextures(1, &tex_);\r
-    CV_CheckGlError();\r
-    CV_Assert(tex_ != 0);\r
-\r
-    glBindTexture(GL_TEXTURE_2D, tex_);\r
-    CV_CheckGlError();\r
-\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
-    CV_CheckGlError();\r
-\r
-    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
-\r
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
-    CV_CheckGlError();\r
-\r
-    glTexImage2D(GL_TEXTURE_2D, 0, cn, mat.cols, mat.rows, 0, format, gl_types[depth], mat.data);\r
-    CV_CheckGlError();\r
-}\r
-\r
-cv::gpu::GlTexture::Impl::Impl(const GlBuffer& buf, bool bgra) : tex_(0)\r
-{\r
-    if (!glFuncTab()->isGlContextInitialized())\r
-        throw_nogl();\r
-\r
-    int depth = buf.depth();\r
-    int cn = buf.channels();\r
-\r
-    CV_DbgAssert(buf.rows > 0 && buf.cols > 0);\r
-    CV_Assert(cn == 1 || cn == 3 || cn == 4);\r
-    CV_Assert(depth >= 0 && depth <= CV_32F);\r
-    CV_Assert(buf.usage() == GlBuffer::TEXTURE_BUFFER);\r
-\r
-    glGenTextures(1, &tex_);\r
-    CV_CheckGlError();\r
-    CV_Assert(tex_ != 0);\r
-\r
-    glBindTexture(GL_TEXTURE_2D, tex_);\r
-    CV_CheckGlError();\r
-\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
-    CV_CheckGlError();\r
-\r
-    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
-\r
-    buf.bind();\r
-\r
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
-    CV_CheckGlError();\r
-\r
-    glTexImage2D(GL_TEXTURE_2D, 0, cn, buf.cols, buf.rows, 0, format, gl_types[depth], 0);\r
-    CV_CheckGlError();\r
-\r
-    buf.unbind();\r
-}\r
-\r
-inline cv::gpu::GlTexture::Impl::~Impl()\r
-{\r
-    if (tex_)\r
-        glDeleteTextures(1, &tex_);\r
-}\r
-\r
-void cv::gpu::GlTexture::Impl::copyFrom(const Mat& mat, bool bgra)\r
-{\r
-    CV_Assert(tex_ != 0);\r
-\r
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
-    CV_CheckGlError();\r
-\r
-    int cn = mat.channels();\r
-    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
-\r
-    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mat.cols, mat.rows, format, gl_types[mat.depth()], mat.data);\r
-    CV_CheckGlError();\r
-}\r
-\r
-void cv::gpu::GlTexture::Impl::copyFrom(const GlBuffer& buf, bool bgra)\r
-{\r
-    CV_Assert(tex_ != 0);\r
-    CV_Assert(buf.usage() == GlBuffer::TEXTURE_BUFFER);\r
-\r
-    buf.bind();\r
-\r
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
-    CV_CheckGlError();\r
-\r
-    int cn = buf.channels();\r
-    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
-\r
-    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, buf.cols, buf.rows, format, gl_types[buf.depth()], 0);\r
-    CV_CheckGlError();\r
-\r
-    buf.unbind();\r
-}\r
-\r
-inline void cv::gpu::GlTexture::Impl::bind() const\r
-{\r
-    CV_Assert(tex_ != 0);\r
-\r
-    glEnable(GL_TEXTURE_2D);\r
-    CV_CheckGlError();\r
-\r
-    glBindTexture(GL_TEXTURE_2D, tex_);\r
-    CV_CheckGlError();\r
-}\r
-\r
-inline void cv::gpu::GlTexture::Impl::unbind() const\r
-{\r
-    glBindTexture(GL_TEXTURE_2D, 0);\r
-\r
-    glDisable(GL_TEXTURE_2D);\r
-}\r
-\r
-#endif // HAVE_OPENGL\r
-\r
-cv::gpu::GlTexture::GlTexture() : rows(0), cols(0), type_(0)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl;\r
-#endif\r
-}\r
-\r
-cv::gpu::GlTexture::GlTexture(int rows_, int cols_, int type) : rows(0), cols(0), type_(0)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl(rows_, cols_, type);\r
-    rows = rows_;\r
-    cols = cols_;\r
-    type_ = type;\r
-#endif\r
-}\r
-\r
-cv::gpu::GlTexture::GlTexture(Size size, int type) : rows(0), cols(0), type_(0)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl(size.height, size.width, type);\r
-    rows = size.height;\r
-    cols = size.width;\r
-    type_ = type;\r
-#endif\r
-}\r
-\r
-cv::gpu::GlTexture::GlTexture(InputArray mat_, bool bgra) : rows(0), cols(0), type_(0)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    Mat mat = mat_.getMat();\r
-    impl_ = new Impl(mat, bgra);\r
-    rows = mat.rows;\r
-    cols = mat.cols;\r
-    type_ = mat.type();\r
-#endif\r
-}\r
-\r
-cv::gpu::GlTexture::GlTexture(const GlBuffer& buf, bool bgra) : rows(0), cols(0), type_(0)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl(buf, bgra);\r
-    rows = buf.rows;\r
-    cols = buf.cols;\r
-    type_ = buf.type();\r
-#endif\r
-}\r
-\r
-cv::gpu::GlTexture::GlTexture(const GlTexture& other)\r
-    : rows(other.rows), cols(other.cols), type_(other.type_), impl_(other.impl_)\r
-{\r
-}\r
-\r
-cv::gpu::GlTexture::~GlTexture()\r
-{\r
-}\r
-\r
-GlTexture& cv::gpu::GlTexture::operator =(const GlTexture& other)\r
-{\r
-    rows = other.rows;\r
-    cols = other.cols;\r
-    type_ = other.type_;\r
-    impl_ = other.impl_;\r
-    return *this;\r
-}\r
-\r
-void cv::gpu::GlTexture::create(int rows_, int cols_, int type)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    if (rows_ != rows || cols_ != cols || type_ != type)\r
-    {\r
-        impl_ = new Impl(rows_, cols_, type);\r
-        rows = rows_;\r
-        cols = cols_;\r
-        type_ = type;\r
-    }\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlTexture::release()\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_ = new Impl;\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlTexture::copyFrom(InputArray mat_, bool bgra)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    Mat mat = mat_.getMat();\r
-    create(mat.rows, mat.cols, mat.type());\r
-    impl_->copyFrom(mat, bgra);\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlTexture::copyFrom(const GlBuffer& buf, bool bgra)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    create(buf.rows, buf.cols, buf.type());\r
-    impl_->copyFrom(buf, bgra);\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlTexture::bind() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_->bind();\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlTexture::unbind() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    impl_->unbind();\r
-#endif\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////\r
-// GlArrays\r
-\r
-void cv::gpu::GlArrays::setVertexArray(const GlBuffer& vertex)\r
-{\r
-    CV_Assert(vertex.usage() == GlBuffer::ARRAY_BUFFER);\r
-\r
-    int cn = vertex.channels();\r
-    int depth = vertex.depth();\r
-\r
-    CV_Assert(cn == 2 || cn == 3 || cn == 4);\r
-    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    vertex_ = vertex;\r
-}\r
-\r
-void cv::gpu::GlArrays::setVertexArray(const GpuMat& vertex)\r
-{\r
-    int cn = vertex.channels();\r
-    int depth = vertex.depth();\r
-\r
-    CV_Assert(cn == 2 || cn == 3 || cn == 4);\r
-    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    vertex_.copyFrom(vertex);\r
-}\r
-\r
-void cv::gpu::GlArrays::setVertexArray(InputArray vertex)\r
-{\r
-    int cn = vertex.channels();\r
-    int depth = vertex.depth();\r
-\r
-    CV_Assert(cn == 2 || cn == 3 || cn == 4);\r
-    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    vertex_.copyFrom(vertex);\r
-}\r
-\r
-void cv::gpu::GlArrays::setColorArray(const GlBuffer& color, bool bgra)\r
-{\r
-    CV_Assert(color.usage() == GlBuffer::ARRAY_BUFFER);\r
-\r
-    int cn = color.channels();\r
-\r
-    CV_Assert((cn == 3 && !bgra) || cn == 4);\r
-\r
-    color_ = color;\r
-    bgra_ = bgra;\r
-}\r
-\r
-void cv::gpu::GlArrays::setColorArray(const GpuMat& color, bool bgra)\r
-{\r
-    int cn = color.channels();\r
-\r
-    CV_Assert((cn == 3 && !bgra) || cn == 4);\r
-\r
-    color_.copyFrom(color);\r
-    bgra_ = bgra;\r
-}\r
-\r
-void cv::gpu::GlArrays::setColorArray(InputArray color, bool bgra)\r
-{\r
-    int cn = color.channels();\r
-\r
-    CV_Assert((cn == 3 && !bgra) || cn == 4);\r
-\r
-    color_.copyFrom(color);\r
-    bgra_ = bgra;\r
-}\r
-\r
-void cv::gpu::GlArrays::setNormalArray(const GlBuffer& normal)\r
-{\r
-    CV_Assert(normal.usage() == GlBuffer::ARRAY_BUFFER);\r
-\r
-    int cn = normal.channels();\r
-    int depth = normal.depth();\r
-\r
-    CV_Assert(cn == 3);\r
-    CV_Assert(depth == CV_8S || depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    normal_ = normal;\r
-}\r
-\r
-void cv::gpu::GlArrays::setNormalArray(const GpuMat& normal)\r
-{\r
-    int cn = normal.channels();\r
-    int depth = normal.depth();\r
-\r
-    CV_Assert(cn == 3);\r
-    CV_Assert(depth == CV_8S || depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    normal_.copyFrom(normal);\r
-}\r
-\r
-void cv::gpu::GlArrays::setNormalArray(InputArray normal)\r
-{\r
-    int cn = normal.channels();\r
-    int depth = normal.depth();\r
-\r
-    CV_Assert(cn == 3);\r
-    CV_Assert(depth == CV_8S || depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    normal_.copyFrom(normal);\r
-}\r
-\r
-void cv::gpu::GlArrays::setTexCoordArray(const GlBuffer& texCoord)\r
-{\r
-    CV_Assert(texCoord.usage() == GlBuffer::ARRAY_BUFFER);\r
-\r
-    int cn = texCoord.channels();\r
-    int depth = texCoord.depth();\r
-\r
-    CV_Assert(cn >= 1 && cn <= 4);\r
-    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    texCoord_ = texCoord;\r
-}\r
-\r
-void cv::gpu::GlArrays::setTexCoordArray(const GpuMat& texCoord)\r
-{\r
-    int cn = texCoord.channels();\r
-    int depth = texCoord.depth();\r
-\r
-    CV_Assert(cn >= 1 && cn <= 4);\r
-    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    texCoord_.copyFrom(texCoord);\r
-}\r
-\r
-void cv::gpu::GlArrays::setTexCoordArray(InputArray texCoord)\r
-{\r
-    int cn = texCoord.channels();\r
-    int depth = texCoord.depth();\r
-\r
-    CV_Assert(cn >= 1 && cn <= 4);\r
-    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
-\r
-    texCoord_.copyFrom(texCoord);\r
-}\r
-\r
-void cv::gpu::GlArrays::bind() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    CV_DbgAssert(texCoord_.empty() || texCoord_.size().area() == vertex_.size().area());\r
-    CV_DbgAssert(normal_.empty() || normal_.size().area() == vertex_.size().area());\r
-    CV_DbgAssert(color_.empty() || color_.size().area() == vertex_.size().area());\r
-\r
-    if (!texCoord_.empty())\r
-    {\r
-        glEnableClientState(GL_TEXTURE_COORD_ARRAY);\r
-        CV_CheckGlError();\r
-\r
-        texCoord_.bind();\r
-\r
-        glTexCoordPointer(texCoord_.channels(), gl_types[texCoord_.depth()], 0, 0);\r
-        CV_CheckGlError();\r
-\r
-        texCoord_.unbind();\r
-    }\r
-\r
-    if (!normal_.empty())\r
-    {\r
-        glEnableClientState(GL_NORMAL_ARRAY);\r
-        CV_CheckGlError();\r
-\r
-        normal_.bind();\r
-\r
-        glNormalPointer(gl_types[normal_.depth()], 0, 0);\r
-        CV_CheckGlError();\r
-\r
-        normal_.unbind();\r
-    }\r
-\r
-    if (!color_.empty())\r
-    {\r
-        glEnableClientState(GL_COLOR_ARRAY);\r
-        CV_CheckGlError();\r
-\r
-        color_.bind();\r
-\r
-        int cn = color_.channels();\r
-        int format = cn == 3 ? cn : (bgra_ ? GL_BGRA : 4);\r
-\r
-        glColorPointer(format, gl_types[color_.depth()], 0, 0);\r
-        CV_CheckGlError();\r
-\r
-        color_.unbind();\r
-    }\r
-\r
-    if (!vertex_.empty())\r
-    {\r
-        glEnableClientState(GL_VERTEX_ARRAY);\r
-        CV_CheckGlError();\r
-\r
-        vertex_.bind();\r
-\r
-        glVertexPointer(vertex_.channels(), gl_types[vertex_.depth()], 0, 0);\r
-        CV_CheckGlError();\r
-\r
-        vertex_.unbind();\r
-    }\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlArrays::unbind() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    if (!texCoord_.empty())\r
-    {\r
-        glDisableClientState(GL_TEXTURE_COORD_ARRAY);\r
-        CV_CheckGlError();\r
-    }\r
-\r
-    if (!normal_.empty())\r
-    {\r
-        glDisableClientState(GL_NORMAL_ARRAY);\r
-        CV_CheckGlError();\r
-    }\r
-\r
-    if (!color_.empty())\r
-    {\r
-        glDisableClientState(GL_COLOR_ARRAY);\r
-        CV_CheckGlError();\r
-    }\r
-\r
-    if (!vertex_.empty())\r
-    {\r
-        glDisableClientState(GL_VERTEX_ARRAY);\r
-        CV_CheckGlError();\r
-    }\r
-#endif\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////\r
-// Rendering\r
-\r
-void cv::gpu::render(const GlTexture& tex, Rect_<double> wndRect, Rect_<double> texRect)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    if (!tex.empty())\r
-    {\r
-        tex.bind();\r
-\r
-        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);\r
-\r
-        glBegin(GL_QUADS);\r
-            glTexCoord2d(texRect.x, texRect.y);\r
-            glVertex2d(wndRect.x, wndRect.y);\r
-\r
-            glTexCoord2d(texRect.x, texRect.y + texRect.height);\r
-            glVertex2d(wndRect.x, (wndRect.y + wndRect.height));\r
-\r
-            glTexCoord2d(texRect.x + texRect.width, texRect.y + texRect.height);\r
-            glVertex2d(wndRect.x + wndRect.width, (wndRect.y + wndRect.height));\r
-\r
-            glTexCoord2d(texRect.x + texRect.width, texRect.y);\r
-            glVertex2d(wndRect.x + wndRect.width, wndRect.y);\r
-        glEnd();\r
-\r
-        CV_CheckGlError();\r
-\r
-        tex.unbind();\r
-    }\r
-#endif\r
-}\r
-\r
-void cv::gpu::render(const GlArrays& arr, int mode)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    arr.bind();\r
-\r
-    glDrawArrays(mode, 0, arr.size().area());\r
-\r
-    arr.unbind();\r
-#endif\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////\r
-// GlCamera\r
-\r
-cv::gpu::GlCamera::GlCamera() :\r
-    eye_(0.0, 0.0, -5.0), center_(0.0, 0.0, 0.0), up_(0.0, 1.0, 0.0),\r
-    pos_(0.0, 0.0, -5.0), yaw_(0.0), pitch_(0.0), roll_(0.0),\r
-    useLookAtParams_(false),\r
-\r
-    scale_(1.0, 1.0, 1.0),\r
-\r
-    projectionMatrix_(),\r
-    fov_(45.0), aspect_(0.0),\r
-    left_(0.0), right_(1.0), bottom_(1.0), top_(0.0),\r
-    zNear_(-1.0), zFar_(1.0),\r
-    perspectiveProjection_(false)\r
-{\r
-}\r
-\r
-void cv::gpu::GlCamera::lookAt(Point3d eye, Point3d center, Point3d up)\r
-{\r
-    eye_ = eye;\r
-    center_ = center;\r
-    up_ = up;\r
-    useLookAtParams_ = true;\r
-}\r
-\r
-void cv::gpu::GlCamera::setCameraPos(Point3d pos, double yaw, double pitch, double roll)\r
-{\r
-    pos_ = pos;\r
-    yaw_ = yaw;\r
-    pitch_ = pitch;\r
-    roll_ = roll;\r
-    useLookAtParams_ = false;\r
-}\r
-\r
-void cv::gpu::GlCamera::setScale(Point3d scale)\r
-{\r
-    scale_ = scale;\r
-}\r
-\r
-void cv::gpu::GlCamera::setProjectionMatrix(const Mat& projectionMatrix, bool transpose)\r
-{\r
-    CV_Assert(projectionMatrix.type() == CV_32F || projectionMatrix.type() == CV_64F);\r
-    CV_Assert(projectionMatrix.cols == 4 && projectionMatrix.rows == 4);\r
-\r
-    projectionMatrix_ = transpose ? projectionMatrix.t() : projectionMatrix;\r
-}\r
-\r
-void cv::gpu::GlCamera::setPerspectiveProjection(double fov, double aspect, double zNear, double zFar)\r
-{\r
-    fov_ = fov;\r
-    aspect_ = aspect;\r
-    zNear_ = zNear;\r
-    zFar_ = zFar;\r
-\r
-    projectionMatrix_.release();\r
-    perspectiveProjection_ = true;\r
-}\r
-\r
-void cv::gpu::GlCamera::setOrthoProjection(double left, double right, double bottom, double top, double zNear, double zFar)\r
-{\r
-    left_ = left;\r
-    right_ = right;\r
-    bottom_ = bottom;\r
-    top_ = top;\r
-    zNear_ = zNear;\r
-    zFar_ = zFar;\r
-\r
-    projectionMatrix_.release();\r
-    perspectiveProjection_ = false;\r
-}\r
-\r
-void cv::gpu::GlCamera::setupProjectionMatrix() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    glMatrixMode(GL_PROJECTION);\r
-    glLoadIdentity();\r
-\r
-    if (projectionMatrix_.empty())\r
-    {\r
-        if (perspectiveProjection_)\r
-            gluPerspective(fov_, aspect_, zNear_, zFar_);\r
-        else\r
-            glOrtho(left_, right_, bottom_, top_, zNear_, zFar_);\r
-    }\r
-    else\r
-    {\r
-        if (projectionMatrix_.type() == CV_32F)\r
-            glLoadMatrixf(projectionMatrix_.ptr<float>());\r
-        else\r
-            glLoadMatrixd(projectionMatrix_.ptr<double>());\r
-    }\r
-\r
-    CV_CheckGlError();\r
-#endif\r
-}\r
-\r
-void cv::gpu::GlCamera::setupModelViewMatrix() const\r
-{\r
-#ifndef HAVE_OPENGL\r
-    throw_nogl();\r
-#else\r
-    glMatrixMode(GL_MODELVIEW);\r
-    glLoadIdentity();\r
-\r
-    if (useLookAtParams_)\r
-        gluLookAt(eye_.x, eye_.y, eye_.z, center_.x, center_.y, center_.z, up_.x, up_.y, up_.z);\r
-    else\r
-    {\r
-        glRotated(-yaw_, 0.0, 1.0, 0.0);\r
-        glRotated(-pitch_, 1.0, 0.0, 0.0);\r
-        glRotated(-roll_, 0.0, 0.0, 1.0);\r
-        glTranslated(-pos_.x, -pos_.y, -pos_.z);\r
-    }\r
-\r
-    glScaled(scale_.x, scale_.y, scale_.z);\r
-\r
-    CV_CheckGlError();\r
-#endif\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////\r
-// Error handling\r
-\r
-void cv::gpu::error(const char *error_string, const char *file, const int line, const char *func)\r
-{\r
-    int code = CV_GpuApiCallError;\r
-\r
-    if (uncaught_exception())\r
-    {\r
-        const char* errorStr = cvErrorStr(code);\r
-        const char* function = func ? func : "unknown function";\r
-\r
-        cerr << "OpenCV Error: " << errorStr << "(" << error_string << ") in " << function << ", file " << file << ", line " << line;\r
-        cerr.flush();\r
+        cerr << "OpenCV Error: " << errorStr << "(" << error_string << ") in " << function << ", file " << file << ", line " << line;\r
+        cerr.flush();\r
     }\r
     else\r
         cv::error( cv::Exception(code, error_string, func, file, line) );\r
 }\r
-\r
-bool cv::gpu::checkGlError(const char* file, const int line, const char* func)\r
-{\r
-#ifndef HAVE_OPENGL\r
-    return true;\r
-#else\r
-    GLenum err = glGetError();\r
-\r
-    if (err != GL_NO_ERROR)\r
-    {\r
-        const char* msg;\r
-\r
-        switch (err)\r
-        {\r
-        case GL_INVALID_ENUM:\r
-            msg = "An unacceptable value is specified for an enumerated argument";\r
-            break;\r
-        case GL_INVALID_VALUE:\r
-            msg = "A numeric argument is out of range";\r
-            break;\r
-        case GL_INVALID_OPERATION:\r
-            msg = "The specified operation is not allowed in the current state";\r
-            break;\r
-        case GL_STACK_OVERFLOW:\r
-            msg = "This command would cause a stack overflow";\r
-            break;\r
-        case GL_STACK_UNDERFLOW:\r
-            msg = "This command would cause a stack underflow";\r
-            break;\r
-        case GL_OUT_OF_MEMORY:\r
-            msg = "There is not enough memory left to execute the command";\r
-            break;\r
-        default:\r
-            msg = "Unknown error";\r
-        };\r
-\r
-        cvError(CV_OpenGlApiCallError, func, msg, file, line);\r
-\r
-        return false;\r
-    }\r
-\r
-    return true;\r
-#endif\r
-}\r
index 83357af..8188c1b 100644 (file)
@@ -41,6 +41,8 @@
 //M*/
 
 #include "precomp.hpp"
+#include "opencv2/core/gpumat.hpp"
+#include "opencv2/core/opengl_interop.hpp"
 
 /****************************************************************************************\
 *                           [scaled] Identity matrix initialization                      *
@@ -873,6 +875,9 @@ _InputArray::_InputArray(const Mat& m) : flags(MAT), obj((void*)&m) {}
 _InputArray::_InputArray(const vector<Mat>& vec) : flags(STD_VECTOR_MAT), obj((void*)&vec) {}
 _InputArray::_InputArray(const double& val) : flags(MATX+CV_64F), obj((void*)&val), sz(Size(1,1)) {}
 _InputArray::_InputArray(const MatExpr& expr) : flags(EXPR), obj((void*)&expr) {}
+_InputArray::_InputArray(const GlBuffer& buf) : flags(OPENGL_BUFFER), obj((void*)&buf) {}
+_InputArray::_InputArray(const GlTexture& tex) : flags(OPENGL_TEXTURE), obj((void*)&tex) {}
+_InputArray::_InputArray(const gpu::GpuMat& d_mat) : flags(GPU_MAT), obj((void*)&d_mat) {}
  
 Mat _InputArray::getMat(int i) const
 {
@@ -1011,6 +1016,42 @@ void _InputArray::getMatVector(vector<Mat>& mv) const
         return;
     }
 }
+
+GlBuffer _InputArray::getGlBuffer() const
+{
+    int k = kind();
+
+    CV_Assert(k == OPENGL_BUFFER);
+    //if( k == OPENGL_BUFFER )
+    {
+        const GlBuffer* buf = (const GlBuffer*)obj;
+        return *buf;
+    }
+}
+
+GlTexture _InputArray::getGlTexture() const
+{
+    int k = kind();
+
+    CV_Assert(k == OPENGL_TEXTURE);
+    //if( k == OPENGL_TEXTURE )
+    {
+        const GlTexture* tex = (const GlTexture*)obj;
+        return *tex;
+    }
+}
+
+gpu::GpuMat _InputArray::getGpuMat() const
+{
+    int k = kind();
+
+    CV_Assert(k == GPU_MAT);
+    //if( k == GPU_MAT )
+    {
+        const gpu::GpuMat* d_mat = (const gpu::GpuMat*)obj;
+        return *d_mat;
+    }
+}
     
 int _InputArray::kind() const
 {
@@ -1063,8 +1104,7 @@ Size _InputArray::size(int i) const
         return szb == szi ? Size((int)szb, 1) : Size((int)(szb/CV_ELEM_SIZE(flags)), 1);
     }
     
-    CV_Assert( k == STD_VECTOR_MAT );
-    //if( k == STD_VECTOR_MAT )
+    if( k == STD_VECTOR_MAT )
     {
         const vector<Mat>& vv = *(const vector<Mat>*)obj;
         if( i < 0 )
@@ -1073,6 +1113,28 @@ Size _InputArray::size(int i) const
         
         return vv[i].size();
     }
+
+    if( k == OPENGL_BUFFER )
+    {
+        CV_Assert( i < 0 );
+        const GlBuffer* buf = (const GlBuffer*)obj;
+        return buf->size();
+    }
+
+    if( k == OPENGL_TEXTURE )
+    {
+        CV_Assert( i < 0 );
+        const GlTexture* tex = (const GlTexture*)obj;
+        return tex->size();
+    }
+
+    CV_Assert( k == GPU_MAT );
+    //if( k == GPU_MAT )
+    {
+        CV_Assert( i < 0 );
+        const gpu::GpuMat* d_mat = (const gpu::GpuMat*)obj;
+        return d_mat->size();
+    }
 }
 
 size_t _InputArray::total(int i) const
@@ -1096,14 +1158,23 @@ int _InputArray::type(int i) const
     if( k == NONE )
         return -1;
     
-    CV_Assert( k == STD_VECTOR_MAT );
-    //if( k == STD_VECTOR_MAT )
+    if( k == STD_VECTOR_MAT )
     {
         const vector<Mat>& vv = *(const vector<Mat>*)obj;
         CV_Assert( i < (int)vv.size() );
         
         return vv[i >= 0 ? i : 0].type();
     }
+    
+    if( k == OPENGL_BUFFER )
+        return ((const GlBuffer*)obj)->type();
+    
+    if( k == OPENGL_TEXTURE )
+        return ((const GlTexture*)obj)->type();
+    
+    CV_Assert( k == GPU_MAT );
+    //if( k == GPU_MAT )
+        return ((const gpu::GpuMat*)obj)->type();
 }
     
 int _InputArray::depth(int i) const
@@ -1144,12 +1215,21 @@ bool _InputArray::empty() const
         return vv.empty();
     }
     
-    CV_Assert( k == STD_VECTOR_MAT );
-    //if( k == STD_VECTOR_MAT )
+    if( k == STD_VECTOR_MAT )
     {
         const vector<Mat>& vv = *(const vector<Mat>*)obj;
         return vv.empty();
     }
+    
+    if( k == OPENGL_BUFFER )
+        return ((const GlBuffer*)obj)->empty();
+    
+    if( k == OPENGL_TEXTURE )
+        return ((const GlTexture*)obj)->empty();
+    
+    CV_Assert( k == GPU_MAT );
+    //if( k == GPU_MAT )
+        return ((const gpu::GpuMat*)obj)->empty();
 }
     
     
diff --git a/modules/core/src/opengl_interop.cpp b/modules/core/src/opengl_interop.cpp
new file mode 100644 (file)
index 0000000..e6bbddc
--- /dev/null
@@ -0,0 +1,1519 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                           License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "precomp.hpp"\r
+#include <iostream>\r
+#include "opencv2/core/opengl_interop.hpp"\r
+#include "opencv2/core/gpumat.hpp"\r
+\r
+#ifdef HAVE_OPENGL\r
+    #ifdef __APPLE__\r
+        #include <OpenGL/gl.h>\r
+        #include <OpenGL/glu.h>\r
+    #else\r
+        #include <GL/gl.h>\r
+        #include <GL/glu.h>\r
+    #endif\r
+\r
+    #ifdef HAVE_CUDA\r
+        #include <cuda_runtime.h>\r
+        #include <cuda_gl_interop.h>\r
+    #endif\r
+#endif\r
+\r
+using namespace std;\r
+using namespace cv;\r
+using namespace cv::gpu;\r
+\r
+#ifndef HAVE_OPENGL\r
+    #define throw_nogl CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support")\r
+    #define throw_nocuda CV_Error(CV_GpuNotSupported, "The library is compiled without CUDA support")\r
+#else\r
+    #define throw_nogl CV_Error(CV_OpenGlNotSupported, "OpenGL context doesn't exist")\r
+\r
+    #ifndef HAVE_CUDA\r
+        #define throw_nocuda CV_Error(CV_GpuNotSupported, "The library is compiled without CUDA support")\r
+    #else\r
+        #if defined(__GNUC__)\r
+            #define cudaSafeCall(expr)  ___cudaSafeCall(expr, __FILE__, __LINE__, __func__)\r
+        #else /* defined(__CUDACC__) || defined(__MSVC__) */\r
+            #define cudaSafeCall(expr)  ___cudaSafeCall(expr, __FILE__, __LINE__)\r
+        #endif\r
+\r
+        namespace\r
+        {\r
+            inline void ___cudaSafeCall(cudaError_t err, const char *file, const int line, const char *func = "")\r
+            {\r
+                if (cudaSuccess != err)\r
+                    cv::gpu::error(cudaGetErrorString(err), file, line, func);\r
+            }\r
+        }\r
+    #endif // HAVE_CUDA\r
+#endif\r
+\r
+namespace\r
+{\r
+    class EmptyGlFuncTab : public CvOpenGlFuncTab\r
+    {\r
+    public:\r
+        void genBuffers(int, unsigned int*) const { throw_nogl; }\r
+        void deleteBuffers(int, const unsigned int*) const { throw_nogl; }\r
+\r
+        void bufferData(unsigned int, ptrdiff_t, const void*, unsigned int) const { throw_nogl; }\r
+        void bufferSubData(unsigned int, ptrdiff_t, ptrdiff_t, const void*) const { throw_nogl; }\r
+\r
+        void bindBuffer(unsigned int, unsigned int) const { throw_nogl; }\r
+\r
+        void* mapBuffer(unsigned int, unsigned int) const { throw_nogl; return 0; }\r
+        void unmapBuffer(unsigned int) const { throw_nogl; }\r
+\r
+        void generateBitmapFont(const std::string&, int, int, bool, bool, int, int, int) const { throw_nogl; }\r
+\r
+        bool isGlContextInitialized() const { return false; }\r
+    };\r
+\r
+    const CvOpenGlFuncTab* g_glFuncTab = 0;\r
+\r
+    const CvOpenGlFuncTab* glFuncTab()\r
+    {\r
+        static EmptyGlFuncTab empty;\r
+        return g_glFuncTab ? g_glFuncTab : &empty;\r
+    }\r
+}\r
+\r
+CvOpenGlFuncTab::~CvOpenGlFuncTab()\r
+{\r
+    if (g_glFuncTab == this)\r
+        g_glFuncTab = 0;\r
+}\r
+\r
+void icvSetOpenGlFuncTab(const CvOpenGlFuncTab* tab)\r
+{\r
+    g_glFuncTab = tab;\r
+}\r
+\r
+#ifdef HAVE_OPENGL\r
+    #ifndef GL_DYNAMIC_DRAW\r
+        #define GL_DYNAMIC_DRAW 0x88E8\r
+    #endif\r
+\r
+    #ifndef GL_READ_WRITE\r
+        #define GL_READ_WRITE 0x88BA\r
+    #endif\r
+\r
+    #ifndef GL_BGR\r
+        #define GL_BGR 0x80E0\r
+    #endif\r
+\r
+    #ifndef GL_BGRA\r
+        #define GL_BGRA 0x80E1\r
+    #endif\r
+\r
+    namespace\r
+    {\r
+        const GLenum gl_types[] = {GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE};\r
+\r
+    #ifdef HAVE_CUDA\r
+        bool g_isCudaGlDeviceInitialized = false;\r
+    #endif\r
+    }\r
+#endif // HAVE_OPENGL\r
+\r
+void cv::gpu::setGlDevice(int device)\r
+{\r
+#ifndef HAVE_CUDA\r
+    throw_nocuda;\r
+#else\r
+    #ifndef HAVE_OPENGL\r
+        throw_nogl;\r
+    #else\r
+        if (!glFuncTab()->isGlContextInitialized())\r
+            throw_nogl;\r
+\r
+        cudaSafeCall( cudaGLSetGLDevice(device) );\r
+\r
+        g_isCudaGlDeviceInitialized = true;\r
+    #endif\r
+#endif\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// CudaGlInterop\r
+\r
+#if defined HAVE_CUDA && defined HAVE_OPENGL\r
+namespace\r
+{\r
+    class CudaGlInterop\r
+    {\r
+    public:\r
+        CudaGlInterop();\r
+        ~CudaGlInterop();\r
+\r
+        void registerBuffer(unsigned int buffer);\r
+\r
+        void copyFrom(const GpuMat& mat, cudaStream_t stream = 0);\r
+\r
+        GpuMat map(int rows, int cols, int type, cudaStream_t stream = 0);\r
+        void unmap(cudaStream_t stream = 0);\r
+\r
+    private:\r
+        cudaGraphicsResource_t resource_;\r
+    };\r
+\r
+    inline CudaGlInterop::CudaGlInterop() : resource_(0)\r
+    {\r
+    }\r
+\r
+    CudaGlInterop::~CudaGlInterop()\r
+    {\r
+        if (resource_)\r
+        {\r
+            cudaGraphicsUnregisterResource(resource_);\r
+            resource_ = 0;\r
+        }\r
+    }\r
+\r
+    void CudaGlInterop::registerBuffer(unsigned int buffer)\r
+    {\r
+        if (!g_isCudaGlDeviceInitialized)\r
+            cvError(CV_GpuApiCallError, "registerBuffer", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
+\r
+        cudaGraphicsResource_t resource;\r
+        cudaSafeCall( cudaGraphicsGLRegisterBuffer(&resource, buffer, cudaGraphicsMapFlagsNone) );\r
+\r
+        resource_ = resource;\r
+    }\r
+\r
+    void CudaGlInterop::copyFrom(const GpuMat& mat, cudaStream_t stream)\r
+    {\r
+        CV_Assert(resource_ != 0);\r
+\r
+        cudaSafeCall( cudaGraphicsMapResources(1, &resource_, stream) );\r
+\r
+        void* dst_ptr;\r
+        size_t num_bytes;\r
+        cudaSafeCall( cudaGraphicsResourceGetMappedPointer(&dst_ptr, &num_bytes, resource_) );\r
+\r
+        const void* src_ptr = mat.ptr();\r
+        size_t widthBytes = mat.cols * mat.elemSize();\r
+\r
+        CV_Assert(widthBytes * mat.rows <= num_bytes);\r
+\r
+        if (stream == 0)\r
+            cudaSafeCall( cudaMemcpy2D(dst_ptr, widthBytes, src_ptr, mat.step, widthBytes, mat.rows, cudaMemcpyDeviceToDevice) );\r
+        else\r
+            cudaSafeCall( cudaMemcpy2DAsync(dst_ptr, widthBytes, src_ptr, mat.step, widthBytes, mat.rows, cudaMemcpyDeviceToDevice, stream) );\r
+\r
+        cudaGraphicsUnmapResources(1, &resource_, stream);\r
+    }\r
+\r
+    GpuMat CudaGlInterop::map(int rows, int cols, int type, cudaStream_t stream)\r
+    {\r
+        CV_Assert(resource_ != 0);\r
+\r
+        cudaSafeCall( cudaGraphicsMapResources(1, &resource_, stream) );\r
+\r
+        void* ptr;\r
+        size_t num_bytes;\r
+        cudaSafeCall( cudaGraphicsResourceGetMappedPointer(&ptr, &num_bytes, resource_) );\r
+\r
+        CV_Assert( static_cast<size_t>(cols) * CV_ELEM_SIZE(type) * rows <= num_bytes );\r
+\r
+        return GpuMat(rows, cols, type, ptr);\r
+    }\r
+\r
+    inline void CudaGlInterop::unmap(cudaStream_t stream)\r
+    {\r
+        cudaGraphicsUnmapResources(1, &resource_, stream);\r
+    }\r
+}\r
+#endif // HAVE_CUDA && HAVE_OPENGL\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// GlBuffer\r
+\r
+#ifndef HAVE_OPENGL\r
+\r
+class cv::GlBuffer::Impl\r
+{\r
+};\r
+\r
+#else\r
+\r
+class cv::GlBuffer::Impl\r
+{\r
+public:\r
+    Impl();\r
+    Impl(int rows, int cols, int type, unsigned int target);\r
+    Impl(const Mat& m, unsigned int target);\r
+    ~Impl();\r
+\r
+    void copyFrom(const Mat& m, unsigned int target);\r
+\r
+#ifdef HAVE_CUDA\r
+    void copyFrom(const GpuMat& mat, cudaStream_t stream = 0);\r
+#endif\r
+\r
+    void bind(unsigned int target) const;\r
+    void unbind(unsigned int target) const;\r
+\r
+    Mat mapHost(int rows, int cols, int type, unsigned int target);\r
+    void unmapHost(unsigned int target);\r
+\r
+#ifdef HAVE_CUDA\r
+    GpuMat mapDevice(int rows, int cols, int type, cudaStream_t stream = 0);\r
+    void unmapDevice(cudaStream_t stream = 0);\r
+#endif\r
+\r
+private:\r
+    unsigned int buffer_;\r
+\r
+#ifdef HAVE_CUDA\r
+    CudaGlInterop cudaGlInterop_;\r
+#endif\r
+};\r
+\r
+inline cv::GlBuffer::Impl::Impl() : buffer_(0)\r
+{\r
+}\r
+\r
+cv::GlBuffer::Impl::Impl(int rows, int cols, int type, unsigned int target) : buffer_(0)\r
+{\r
+    if (!glFuncTab()->isGlContextInitialized())\r
+        throw_nogl;\r
+\r
+    CV_DbgAssert(rows > 0 && cols > 0);\r
+    CV_DbgAssert(CV_MAT_DEPTH(type) >= 0 && CV_MAT_DEPTH(type) <= CV_64F);\r
+\r
+    glFuncTab()->genBuffers(1, &buffer_);\r
+    CV_CheckGlError();\r
+    CV_Assert(buffer_ != 0);\r
+\r
+    size_t size = rows * cols * CV_ELEM_SIZE(type);\r
+\r
+    glFuncTab()->bindBuffer(target, buffer_);\r
+    CV_CheckGlError();\r
+\r
+    glFuncTab()->bufferData(target, size, 0, GL_DYNAMIC_DRAW);\r
+    CV_CheckGlError();\r
+\r
+    glFuncTab()->bindBuffer(target, 0);\r
+\r
+#ifdef HAVE_CUDA\r
+    if (g_isCudaGlDeviceInitialized)\r
+        cudaGlInterop_.registerBuffer(buffer_);\r
+#endif\r
+}\r
+\r
+cv::GlBuffer::Impl::Impl(const Mat& m, unsigned int target) : buffer_(0)\r
+{\r
+    if (!glFuncTab()->isGlContextInitialized())\r
+        throw_nogl;\r
+\r
+    CV_DbgAssert(m.rows > 0 && m.cols > 0);\r
+    CV_DbgAssert(m.depth() >= 0 && m.depth() <= CV_64F);\r
+    CV_Assert(m.isContinuous());\r
+\r
+    glFuncTab()->genBuffers(1, &buffer_);\r
+    CV_CheckGlError();\r
+    CV_Assert(buffer_ != 0);\r
+\r
+    size_t size = m.rows * m.cols * m.elemSize();\r
+\r
+    glFuncTab()->bindBuffer(target, buffer_);\r
+    CV_CheckGlError();\r
+\r
+    glFuncTab()->bufferData(target, size, m.data, GL_DYNAMIC_DRAW);\r
+    CV_CheckGlError();\r
+\r
+    glFuncTab()->bindBuffer(target, 0);\r
+\r
+#ifdef HAVE_CUDA\r
+    if (g_isCudaGlDeviceInitialized)\r
+        cudaGlInterop_.registerBuffer(buffer_);\r
+#endif\r
+}\r
+\r
+cv::GlBuffer::Impl::~Impl()\r
+{\r
+    try\r
+    {\r
+        if (buffer_)\r
+            glFuncTab()->deleteBuffers(1, &buffer_);\r
+    }\r
+#ifdef _DEBUG\r
+    catch(const exception& e)\r
+    {\r
+        cerr << e.what() << endl;\r
+    }\r
+#endif\r
+    catch(...)\r
+    {\r
+    }\r
+}\r
+\r
+void cv::GlBuffer::Impl::copyFrom(const Mat& m, unsigned int target)\r
+{\r
+    CV_Assert(buffer_ != 0);\r
+\r
+    CV_Assert(m.isContinuous());\r
+\r
+    bind(target);\r
+\r
+    size_t size = m.rows * m.cols * m.elemSize();\r
+\r
+    glFuncTab()->bufferSubData(target, 0, size, m.data);\r
+    CV_CheckGlError();\r
+\r
+    unbind(target);\r
+}\r
+\r
+#ifdef HAVE_CUDA\r
+\r
+void cv::GlBuffer::Impl::copyFrom(const GpuMat& mat, cudaStream_t stream)\r
+{\r
+    if (!g_isCudaGlDeviceInitialized)\r
+        cvError(CV_GpuApiCallError, "copyFrom", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
+\r
+    CV_Assert(buffer_ != 0);\r
+\r
+    cudaGlInterop_.copyFrom(mat, stream);\r
+}\r
+\r
+#endif // HAVE_CUDA\r
+\r
+inline void cv::GlBuffer::Impl::bind(unsigned int target) const\r
+{\r
+    CV_Assert(buffer_ != 0);\r
+\r
+    glFuncTab()->bindBuffer(target, buffer_);\r
+    CV_CheckGlError();\r
+}\r
+\r
+inline void cv::GlBuffer::Impl::unbind(unsigned int target) const\r
+{\r
+    glFuncTab()->bindBuffer(target, 0);\r
+}\r
+\r
+inline Mat cv::GlBuffer::Impl::mapHost(int rows, int cols, int type, unsigned int target)\r
+{\r
+    void* ptr = glFuncTab()->mapBuffer(target, GL_READ_WRITE);\r
+    CV_CheckGlError();\r
+\r
+    return Mat(rows, cols, type, ptr);\r
+}\r
+\r
+inline void cv::GlBuffer::Impl::unmapHost(unsigned int target)\r
+{\r
+    glFuncTab()->unmapBuffer(target);\r
+}\r
+\r
+#ifdef HAVE_CUDA\r
+\r
+inline GpuMat cv::GlBuffer::Impl::mapDevice(int rows, int cols, int type, cudaStream_t stream)\r
+{\r
+    if (!g_isCudaGlDeviceInitialized)\r
+        cvError(CV_GpuApiCallError, "copyFrom", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
+\r
+    CV_Assert(buffer_ != 0);\r
+\r
+    return cudaGlInterop_.map(rows, cols, type, stream);\r
+}\r
+\r
+inline void cv::GlBuffer::Impl::unmapDevice(cudaStream_t stream)\r
+{\r
+    if (!g_isCudaGlDeviceInitialized)\r
+        cvError(CV_GpuApiCallError, "copyFrom", "cuda GL device wasn't initialized, call setGlDevice", __FILE__, __LINE__);\r
+\r
+    cudaGlInterop_.unmap(stream);\r
+}\r
+\r
+#endif // HAVE_CUDA\r
+\r
+#endif // HAVE_OPENGL\r
+\r
+cv::GlBuffer::GlBuffer(Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl;\r
+#endif\r
+}\r
+\r
+cv::GlBuffer::GlBuffer(int rows_, int cols_, int type, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl(rows_, cols_, type, usage);\r
+    rows = rows_;\r
+    cols = cols_;\r
+    type_ = type;\r
+#endif\r
+}\r
+\r
+cv::GlBuffer::GlBuffer(Size size, int type, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl(size.height, size.width, type, usage);\r
+    rows = size.height;\r
+    cols = size.width;\r
+    type_ = type;\r
+#endif\r
+}\r
+\r
+cv::GlBuffer::GlBuffer(InputArray mat_, Usage usage) : rows(0), cols(0), type_(0), usage_(usage)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    int kind = mat_.kind();\r
+    Size size = mat_.size();\r
+    int type = mat_.type();\r
+\r
+    if (kind == _InputArray::GPU_MAT)\r
+    {\r
+        #ifndef HAVE_CUDA\r
+            throw_nocuda;\r
+        #else\r
+            GpuMat d_mat = mat_.getGpuMat();\r
+            impl_ = new Impl(d_mat.rows, d_mat.cols, d_mat.type(), usage);\r
+            impl_->copyFrom(d_mat);\r
+        #endif\r
+    }\r
+    else\r
+    {\r
+        Mat mat = mat_.getMat();\r
+        impl_ = new Impl(mat, usage);\r
+    }\r
+\r
+    rows = size.height;\r
+    cols = size.width;\r
+    type_ = type;\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::create(int rows_, int cols_, int type, Usage usage)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    if (rows_ != rows || cols_ != cols || type_ != type || usage_ != usage)\r
+    {\r
+        impl_ = new Impl(rows_, cols_, type, usage);\r
+        rows = rows_;\r
+        cols = cols_;\r
+        type_ = type;\r
+        usage_ = usage;\r
+    }\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::release()\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl;\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::copyFrom(InputArray mat_)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    int kind = mat_.kind();\r
+    Size size = mat_.size();\r
+    int type = mat_.type();\r
+\r
+    create(size, type);\r
+\r
+    switch (kind)\r
+    {\r
+    case _InputArray::OPENGL_BUFFER:\r
+        {\r
+            GlBuffer buf = mat_.getGlBuffer();\r
+            *this = buf;\r
+            break;\r
+        }\r
+    case _InputArray::GPU_MAT:\r
+        {\r
+            #ifndef HAVE_CUDA\r
+                throw_nocuda;\r
+            #else\r
+                GpuMat d_mat = mat_.getGpuMat();\r
+                impl_->copyFrom(d_mat);\r
+            #endif\r
+\r
+            break;\r
+        }\r
+    default:\r
+        {\r
+            Mat mat = mat_.getMat();\r
+            impl_->copyFrom(mat, usage_);\r
+        }\r
+    }\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::bind() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_->bind(usage_);\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::unbind() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_->unbind(usage_);\r
+#endif\r
+}\r
+\r
+Mat cv::GlBuffer::mapHost()\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+    return Mat();\r
+#else\r
+    return impl_->mapHost(rows, cols, type_, usage_);\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::unmapHost()\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_->unmapHost(usage_);\r
+#endif\r
+}\r
+\r
+GpuMat cv::GlBuffer::mapDevice()\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+    return GpuMat();\r
+#else\r
+    #ifndef HAVE_CUDA\r
+        throw_nocuda;\r
+        return GpuMat();\r
+    #else\r
+        return impl_->mapDevice(rows, cols, type_);\r
+    #endif\r
+#endif\r
+}\r
+\r
+void cv::GlBuffer::unmapDevice()\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    #ifndef HAVE_CUDA\r
+        throw_nocuda;\r
+    #else\r
+        impl_->unmapDevice();\r
+    #endif\r
+#endif\r
+}\r
+\r
+template <> void cv::Ptr<cv::GlBuffer::Impl>::delete_obj()\r
+{\r
+    if (obj) delete obj;\r
+}\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////////\r
+// GlTexture\r
+\r
+#ifndef HAVE_OPENGL\r
+\r
+class cv::GlTexture::Impl\r
+{\r
+};\r
+\r
+#else\r
+\r
+class cv::GlTexture::Impl\r
+{\r
+public:\r
+    Impl();\r
+\r
+    Impl(int rows, int cols, int type);\r
+\r
+    Impl(const Mat& mat, bool bgra);\r
+    Impl(const GlBuffer& buf, bool bgra);\r
+\r
+    ~Impl();\r
+\r
+    void copyFrom(const Mat& mat, bool bgra);\r
+    void copyFrom(const GlBuffer& buf, bool bgra);\r
+\r
+    void bind() const;\r
+    void unbind() const;\r
+\r
+private:\r
+    GLuint tex_;\r
+};\r
+\r
+inline cv::GlTexture::Impl::Impl() : tex_(0)\r
+{\r
+}\r
+\r
+cv::GlTexture::Impl::Impl(int rows, int cols, int type) : tex_(0)\r
+{\r
+    if (!glFuncTab()->isGlContextInitialized())\r
+        throw_nogl;\r
+\r
+    int depth = CV_MAT_DEPTH(type);\r
+    int cn = CV_MAT_CN(type);\r
+\r
+    CV_DbgAssert(rows > 0 && cols > 0);\r
+    CV_Assert(cn == 1 || cn == 3 || cn == 4);\r
+    CV_Assert(depth >= 0 && depth <= CV_32F);\r
+\r
+    glGenTextures(1, &tex_);\r
+    CV_CheckGlError();\r
+    CV_Assert(tex_ != 0);\r
+\r
+    glBindTexture(GL_TEXTURE_2D, tex_);\r
+    CV_CheckGlError();\r
+\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
+    CV_CheckGlError();\r
+\r
+    GLenum format = cn == 1 ? GL_LUMINANCE : cn == 3 ? GL_BGR : GL_BGRA;\r
+\r
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
+    CV_CheckGlError();\r
+\r
+    glTexImage2D(GL_TEXTURE_2D, 0, cn, cols, rows, 0, format, gl_types[depth], 0);\r
+    CV_CheckGlError();\r
+}\r
+\r
+cv::GlTexture::Impl::Impl(const Mat& mat, bool bgra) : tex_(0)\r
+{\r
+    if (!glFuncTab()->isGlContextInitialized())\r
+        throw_nogl;\r
+\r
+    int depth = mat.depth();\r
+    int cn = mat.channels();\r
+\r
+    CV_DbgAssert(mat.rows > 0 && mat.cols > 0);\r
+    CV_Assert(cn == 1 || cn == 3 || cn == 4);\r
+    CV_Assert(depth >= 0 && depth <= CV_32F);\r
+    CV_Assert(mat.isContinuous());\r
+\r
+    glGenTextures(1, &tex_);\r
+    CV_CheckGlError();\r
+    CV_Assert(tex_ != 0);\r
+\r
+    glBindTexture(GL_TEXTURE_2D, tex_);\r
+    CV_CheckGlError();\r
+\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
+    CV_CheckGlError();\r
+\r
+    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
+\r
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
+    CV_CheckGlError();\r
+\r
+    glTexImage2D(GL_TEXTURE_2D, 0, cn, mat.cols, mat.rows, 0, format, gl_types[depth], mat.data);\r
+    CV_CheckGlError();\r
+}\r
+\r
+cv::GlTexture::Impl::Impl(const GlBuffer& buf, bool bgra) : tex_(0)\r
+{\r
+    if (!glFuncTab()->isGlContextInitialized())\r
+        throw_nogl;\r
+\r
+    int depth = buf.depth();\r
+    int cn = buf.channels();\r
+\r
+    CV_DbgAssert(buf.rows > 0 && buf.cols > 0);\r
+    CV_Assert(cn == 1 || cn == 3 || cn == 4);\r
+    CV_Assert(depth >= 0 && depth <= CV_32F);\r
+    CV_Assert(buf.usage() == GlBuffer::TEXTURE_BUFFER);\r
+\r
+    glGenTextures(1, &tex_);\r
+    CV_CheckGlError();\r
+    CV_Assert(tex_ != 0);\r
+\r
+    glBindTexture(GL_TEXTURE_2D, tex_);\r
+    CV_CheckGlError();\r
+\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
+    CV_CheckGlError();\r
+\r
+    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
+\r
+    buf.bind();\r
+\r
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
+    CV_CheckGlError();\r
+\r
+    glTexImage2D(GL_TEXTURE_2D, 0, cn, buf.cols, buf.rows, 0, format, gl_types[depth], 0);\r
+    CV_CheckGlError();\r
+\r
+    buf.unbind();\r
+}\r
+\r
+inline cv::GlTexture::Impl::~Impl()\r
+{\r
+    if (tex_)\r
+        glDeleteTextures(1, &tex_);\r
+}\r
+\r
+void cv::GlTexture::Impl::copyFrom(const Mat& mat, bool bgra)\r
+{\r
+    CV_Assert(tex_ != 0);\r
+\r
+    bind();\r
+\r
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
+    CV_CheckGlError();\r
+\r
+    int cn = mat.channels();\r
+    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
+\r
+    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mat.cols, mat.rows, format, gl_types[mat.depth()], mat.data);\r
+    CV_CheckGlError();\r
+\r
+    unbind();\r
+}\r
+\r
+void cv::GlTexture::Impl::copyFrom(const GlBuffer& buf, bool bgra)\r
+{\r
+    CV_Assert(tex_ != 0);\r
+    CV_Assert(buf.usage() == GlBuffer::TEXTURE_BUFFER);\r
+\r
+    bind();\r
+\r
+    buf.bind();\r
+\r
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
+    CV_CheckGlError();\r
+\r
+    int cn = buf.channels();\r
+    GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA));\r
+\r
+    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, buf.cols, buf.rows, format, gl_types[buf.depth()], 0);\r
+    CV_CheckGlError();\r
+\r
+    buf.unbind();\r
+\r
+    unbind();\r
+}\r
+\r
+inline void cv::GlTexture::Impl::bind() const\r
+{\r
+    CV_Assert(tex_ != 0);\r
+\r
+    glEnable(GL_TEXTURE_2D);\r
+    CV_CheckGlError();\r
+\r
+    glBindTexture(GL_TEXTURE_2D, tex_);\r
+    CV_CheckGlError();\r
+}\r
+\r
+inline void cv::GlTexture::Impl::unbind() const\r
+{\r
+    glBindTexture(GL_TEXTURE_2D, 0);\r
+\r
+    glDisable(GL_TEXTURE_2D);\r
+}\r
+\r
+#endif // HAVE_OPENGL\r
+\r
+cv::GlTexture::GlTexture() : rows(0), cols(0), type_(0)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl;\r
+#endif\r
+}\r
+\r
+cv::GlTexture::GlTexture(int rows_, int cols_, int type) : rows(0), cols(0), type_(0)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl(rows_, cols_, type);\r
+    rows = rows_;\r
+    cols = cols_;\r
+    type_ = type;\r
+#endif\r
+}\r
+\r
+cv::GlTexture::GlTexture(Size size, int type) : rows(0), cols(0), type_(0)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl(size.height, size.width, type);\r
+    rows = size.height;\r
+    cols = size.width;\r
+    type_ = type;\r
+#endif\r
+}\r
+\r
+cv::GlTexture::GlTexture(InputArray mat_, bool bgra) : rows(0), cols(0), type_(0)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else    \r
+    int kind = mat_.kind();\r
+    Size size = mat_.size();\r
+    int type = mat_.type();\r
+\r
+    switch (kind)\r
+    {\r
+    case _InputArray::OPENGL_BUFFER:\r
+        {\r
+            GlBuffer buf = mat_.getGlBuffer();\r
+            impl_ = new Impl(buf, bgra);\r
+            break;\r
+        }\r
+    case _InputArray::GPU_MAT:\r
+        {\r
+            #ifndef HAVE_CUDA\r
+                throw_nocuda;\r
+            #else\r
+                GpuMat d_mat = mat_.getGpuMat();\r
+                GlBuffer buf(d_mat, GlBuffer::TEXTURE_BUFFER);\r
+                impl_ = new Impl(buf, bgra);\r
+            #endif\r
+\r
+            break;\r
+        }\r
+    default:\r
+        {\r
+            Mat mat = mat_.getMat();\r
+            impl_ = new Impl(mat, bgra);\r
+            break;\r
+        }\r
+    }\r
+\r
+    rows = size.height;\r
+    cols = size.width;\r
+    type_ = type;\r
+#endif\r
+}\r
+\r
+void cv::GlTexture::create(int rows_, int cols_, int type)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    if (rows_ != rows || cols_ != cols || type_ != type)\r
+    {\r
+        impl_ = new Impl(rows_, cols_, type);\r
+        rows = rows_;\r
+        cols = cols_;\r
+        type_ = type;\r
+    }\r
+#endif\r
+}\r
+\r
+void cv::GlTexture::release()\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_ = new Impl;\r
+#endif\r
+}\r
+\r
+void cv::GlTexture::copyFrom(InputArray mat_, bool bgra)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    int kind = mat_.kind();\r
+    Size size = mat_.size();\r
+    int type = mat_.type();\r
+\r
+    create(size, type);\r
+\r
+    switch(kind)\r
+    {\r
+    case _InputArray::OPENGL_TEXTURE:\r
+        {\r
+            GlTexture tex = mat_.getGlTexture();\r
+            *this = tex;\r
+            break;\r
+        }\r
+    case _InputArray::OPENGL_BUFFER:\r
+        {\r
+            GlBuffer buf = mat_.getGlBuffer();\r
+            impl_->copyFrom(buf, bgra);\r
+            break;\r
+        }\r
+    case _InputArray::GPU_MAT:\r
+        {\r
+            #ifndef HAVE_CUDA\r
+                throw_nocuda;\r
+            #else\r
+                GpuMat d_mat = mat_.getGpuMat();\r
+                GlBuffer buf(d_mat, GlBuffer::TEXTURE_BUFFER);\r
+                impl_->copyFrom(buf, bgra);\r
+            #endif\r
+\r
+            break;\r
+        }\r
+    default:\r
+        {\r
+            Mat mat = mat_.getMat();\r
+            impl_->copyFrom(mat, bgra);\r
+        }\r
+    }\r
+#endif\r
+}\r
+\r
+void cv::GlTexture::bind() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_->bind();\r
+#endif\r
+}\r
+\r
+void cv::GlTexture::unbind() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    impl_->unbind();\r
+#endif\r
+}\r
+\r
+template <> void cv::Ptr<cv::GlTexture::Impl>::delete_obj()\r
+{\r
+    if (obj) delete obj;\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// GlArrays\r
+\r
+void cv::GlArrays::setVertexArray(InputArray vertex)\r
+{\r
+    int cn = vertex.channels();\r
+    int depth = vertex.depth();\r
+\r
+    CV_Assert(cn == 2 || cn == 3 || cn == 4);\r
+    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
+\r
+    vertex_.copyFrom(vertex);\r
+}\r
+\r
+void cv::GlArrays::setColorArray(InputArray color, bool bgra)\r
+{\r
+    int cn = color.channels();\r
+\r
+    CV_Assert((cn == 3 && !bgra) || cn == 4);\r
+\r
+    color_.copyFrom(color);\r
+    bgra_ = bgra;\r
+}\r
+\r
+void cv::GlArrays::setNormalArray(InputArray normal)\r
+{\r
+    int cn = normal.channels();\r
+    int depth = normal.depth();\r
+\r
+    CV_Assert(cn == 3);\r
+    CV_Assert(depth == CV_8S || depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
+\r
+    normal_.copyFrom(normal);\r
+}\r
+\r
+void cv::GlArrays::setTexCoordArray(InputArray texCoord)\r
+{\r
+    int cn = texCoord.channels();\r
+    int depth = texCoord.depth();\r
+\r
+    CV_Assert(cn >= 1 && cn <= 4);\r
+    CV_Assert(depth == CV_16S || depth == CV_32S || depth == CV_32F || depth == CV_64F);\r
+\r
+    texCoord_.copyFrom(texCoord);\r
+}\r
+\r
+void cv::GlArrays::bind() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    CV_DbgAssert(texCoord_.empty() || texCoord_.size().area() == vertex_.size().area());\r
+    CV_DbgAssert(normal_.empty() || normal_.size().area() == vertex_.size().area());\r
+    CV_DbgAssert(color_.empty() || color_.size().area() == vertex_.size().area());\r
+\r
+    if (!texCoord_.empty())\r
+    {\r
+        glEnableClientState(GL_TEXTURE_COORD_ARRAY);\r
+        CV_CheckGlError();\r
+\r
+        texCoord_.bind();\r
+\r
+        glTexCoordPointer(texCoord_.channels(), gl_types[texCoord_.depth()], 0, 0);\r
+        CV_CheckGlError();\r
+\r
+        texCoord_.unbind();\r
+    }\r
+\r
+    if (!normal_.empty())\r
+    {\r
+        glEnableClientState(GL_NORMAL_ARRAY);\r
+        CV_CheckGlError();\r
+\r
+        normal_.bind();\r
+\r
+        glNormalPointer(gl_types[normal_.depth()], 0, 0);\r
+        CV_CheckGlError();\r
+\r
+        normal_.unbind();\r
+    }\r
+\r
+    if (!color_.empty())\r
+    {\r
+        glEnableClientState(GL_COLOR_ARRAY);\r
+        CV_CheckGlError();\r
+\r
+        color_.bind();\r
+\r
+        int cn = color_.channels();\r
+        int format = cn == 3 ? cn : (bgra_ ? GL_BGRA : 4);\r
+\r
+        glColorPointer(format, gl_types[color_.depth()], 0, 0);\r
+        CV_CheckGlError();\r
+\r
+        color_.unbind();\r
+    }\r
+\r
+    if (!vertex_.empty())\r
+    {\r
+        glEnableClientState(GL_VERTEX_ARRAY);\r
+        CV_CheckGlError();\r
+\r
+        vertex_.bind();\r
+\r
+        glVertexPointer(vertex_.channels(), gl_types[vertex_.depth()], 0, 0);\r
+        CV_CheckGlError();\r
+\r
+        vertex_.unbind();\r
+    }\r
+#endif\r
+}\r
+\r
+void cv::GlArrays::unbind() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    if (!texCoord_.empty())\r
+    {\r
+        glDisableClientState(GL_TEXTURE_COORD_ARRAY);\r
+        CV_CheckGlError();\r
+    }\r
+\r
+    if (!normal_.empty())\r
+    {\r
+        glDisableClientState(GL_NORMAL_ARRAY);\r
+        CV_CheckGlError();\r
+    }\r
+\r
+    if (!color_.empty())\r
+    {\r
+        glDisableClientState(GL_COLOR_ARRAY);\r
+        CV_CheckGlError();\r
+    }\r
+\r
+    if (!vertex_.empty())\r
+    {\r
+        glDisableClientState(GL_VERTEX_ARRAY);\r
+        CV_CheckGlError();\r
+    }\r
+#endif\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// GlFont\r
+\r
+cv::GlFont::GlFont(const string& family, int height, Weight weight, Style style)\r
+    : family_(family), height_(height), weight_(weight), style_(style), base_(0)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    base_ = glGenLists(256);\r
+    CV_CheckGlError();\r
+\r
+    glFuncTab()->generateBitmapFont(family, height, weight, style & STYLE_ITALIC, style & STYLE_UNDERLINE, 0, 256, base_);\r
+#endif\r
+}\r
+\r
+void cv::GlFont::draw(const char* str, int len) const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    if (base_ && len > 0)\r
+    {\r
+        glPushAttrib(GL_LIST_BIT);\r
+        glListBase(base_);\r
+\r
+        glCallLists(len, GL_UNSIGNED_BYTE, str);\r
+\r
+        glPopAttrib();\r
+\r
+        CV_CheckGlError();\r
+    }\r
+#endif\r
+}\r
+\r
+namespace\r
+{\r
+    class FontCompare : public unary_function<Ptr<GlFont>, bool>\r
+    {\r
+    public:\r
+        inline FontCompare(const string& family, int height, GlFont::Weight weight, GlFont::Style style) \r
+            : family_(family), height_(height), weight_(weight), style_(style)\r
+        {\r
+        }\r
+\r
+        bool operator ()(const cv::Ptr<GlFont>& font)\r
+        {\r
+            return font->family() == family_ && font->height() == height_ && font->weight() == weight_ && font->style() == style_;\r
+        }\r
+\r
+    private:\r
+        string family_;\r
+        int height_;\r
+        GlFont::Weight weight_;\r
+        GlFont::Style style_;\r
+    };\r
+}\r
+\r
+Ptr<GlFont> cv::GlFont::get(const std::string& family, int height, Weight weight, Style style)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+    return Ptr<GlFont>();\r
+#else\r
+    static vector< Ptr<GlFont> > fonts;\r
+    fonts.reserve(10);\r
+\r
+    vector< Ptr<GlFont> >::iterator fontIt = find_if(fonts.begin(), fonts.end(), FontCompare(family, height, weight, style));\r
+\r
+    if (fontIt == fonts.end())\r
+    {\r
+        fonts.push_back(new GlFont(family, height, weight, style));\r
+\r
+        fontIt = fonts.end() - 1;\r
+    }\r
+\r
+    return *fontIt;\r
+#endif\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// Rendering\r
+\r
+void cv::render(const GlTexture& tex, Rect_<double> wndRect, Rect_<double> texRect)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    if (!tex.empty())\r
+    {\r
+        tex.bind();\r
+\r
+        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);\r
+\r
+        glBegin(GL_QUADS);\r
+            glTexCoord2d(texRect.x, texRect.y);\r
+            glVertex2d(wndRect.x, wndRect.y);\r
+\r
+            glTexCoord2d(texRect.x, texRect.y + texRect.height);\r
+            glVertex2d(wndRect.x, (wndRect.y + wndRect.height));\r
+\r
+            glTexCoord2d(texRect.x + texRect.width, texRect.y + texRect.height);\r
+            glVertex2d(wndRect.x + wndRect.width, (wndRect.y + wndRect.height));\r
+\r
+            glTexCoord2d(texRect.x + texRect.width, texRect.y);\r
+            glVertex2d(wndRect.x + wndRect.width, wndRect.y);\r
+        glEnd();\r
+\r
+        CV_CheckGlError();\r
+\r
+        tex.unbind();\r
+    }\r
+#endif\r
+}\r
+\r
+void cv::render(const GlArrays& arr, int mode)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    arr.bind();\r
+\r
+    glDrawArrays(mode, 0, arr.size().area());\r
+\r
+    arr.unbind();\r
+#endif\r
+}\r
+\r
+void cv::render(const string& str, const Ptr<GlFont>& font, Scalar color, Point2d pos)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    glPushAttrib(GL_DEPTH_BUFFER_BIT);\r
+\r
+    GLint viewport[4];\r
+    glGetIntegerv(GL_VIEWPORT, viewport);\r
+\r
+    glDisable(GL_DEPTH_TEST);\r
+\r
+    glMatrixMode(GL_PROJECTION);\r
+    glLoadIdentity();\r
+\r
+    glMatrixMode(GL_MODELVIEW);\r
+    glLoadIdentity();\r
+\r
+    glRasterPos2d(2.0 * (viewport[0] + pos.x) / viewport[2] - 1.0, 1.0 - 2.0 * (viewport[1] + pos.y + font->height()) / viewport[3]);\r
+\r
+    glColor4dv(color.val);\r
+    font->draw(str.c_str(), str.length());\r
+\r
+    glPopAttrib();\r
+#endif\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// GlCamera\r
+\r
+cv::GlCamera::GlCamera() :\r
+    eye_(0.0, 0.0, -5.0), center_(0.0, 0.0, 0.0), up_(0.0, 1.0, 0.0),\r
+    pos_(0.0, 0.0, -5.0), yaw_(0.0), pitch_(0.0), roll_(0.0),\r
+    useLookAtParams_(false),\r
+\r
+    scale_(1.0, 1.0, 1.0),\r
+\r
+    projectionMatrix_(),\r
+    fov_(45.0), aspect_(0.0),\r
+    left_(0.0), right_(1.0), bottom_(1.0), top_(0.0),\r
+    zNear_(-1.0), zFar_(1.0),\r
+    perspectiveProjection_(false)\r
+{\r
+}\r
+\r
+void cv::GlCamera::lookAt(Point3d eye, Point3d center, Point3d up)\r
+{\r
+    eye_ = eye;\r
+    center_ = center;\r
+    up_ = up;\r
+    useLookAtParams_ = true;\r
+}\r
+\r
+void cv::GlCamera::setCameraPos(Point3d pos, double yaw, double pitch, double roll)\r
+{\r
+    pos_ = pos;\r
+    yaw_ = yaw;\r
+    pitch_ = pitch;\r
+    roll_ = roll;\r
+    useLookAtParams_ = false;\r
+}\r
+\r
+void cv::GlCamera::setScale(Point3d scale)\r
+{\r
+    scale_ = scale;\r
+}\r
+\r
+void cv::GlCamera::setProjectionMatrix(const Mat& projectionMatrix, bool transpose)\r
+{\r
+    CV_Assert(projectionMatrix.type() == CV_32F || projectionMatrix.type() == CV_64F);\r
+    CV_Assert(projectionMatrix.cols == 4 && projectionMatrix.rows == 4);\r
+\r
+    projectionMatrix_ = transpose ? projectionMatrix.t() : projectionMatrix;\r
+}\r
+\r
+void cv::GlCamera::setPerspectiveProjection(double fov, double aspect, double zNear, double zFar)\r
+{\r
+    fov_ = fov;\r
+    aspect_ = aspect;\r
+    zNear_ = zNear;\r
+    zFar_ = zFar;\r
+\r
+    projectionMatrix_.release();\r
+    perspectiveProjection_ = true;\r
+}\r
+\r
+void cv::GlCamera::setOrthoProjection(double left, double right, double bottom, double top, double zNear, double zFar)\r
+{\r
+    left_ = left;\r
+    right_ = right;\r
+    bottom_ = bottom;\r
+    top_ = top;\r
+    zNear_ = zNear;\r
+    zFar_ = zFar;\r
+\r
+    projectionMatrix_.release();\r
+    perspectiveProjection_ = false;\r
+}\r
+\r
+void cv::GlCamera::setupProjectionMatrix() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    glMatrixMode(GL_PROJECTION);\r
+    glLoadIdentity();\r
+\r
+    if (projectionMatrix_.empty())\r
+    {\r
+        if (perspectiveProjection_)\r
+            gluPerspective(fov_, aspect_, zNear_, zFar_);\r
+        else\r
+            glOrtho(left_, right_, bottom_, top_, zNear_, zFar_);\r
+    }\r
+    else\r
+    {\r
+        if (projectionMatrix_.type() == CV_32F)\r
+            glLoadMatrixf(projectionMatrix_.ptr<float>());\r
+        else\r
+            glLoadMatrixd(projectionMatrix_.ptr<double>());\r
+    }\r
+\r
+    CV_CheckGlError();\r
+#endif\r
+}\r
+\r
+void cv::GlCamera::setupModelViewMatrix() const\r
+{\r
+#ifndef HAVE_OPENGL\r
+    throw_nogl;\r
+#else\r
+    glMatrixMode(GL_MODELVIEW);\r
+    glLoadIdentity();\r
+\r
+    if (useLookAtParams_)\r
+        gluLookAt(eye_.x, eye_.y, eye_.z, center_.x, center_.y, center_.z, up_.x, up_.y, up_.z);\r
+    else\r
+    {\r
+        glRotated(-yaw_, 0.0, 1.0, 0.0);\r
+        glRotated(-pitch_, 1.0, 0.0, 0.0);\r
+        glRotated(-roll_, 0.0, 0.0, 1.0);\r
+        glTranslated(-pos_.x, -pos_.y, -pos_.z);\r
+    }\r
+\r
+    glScaled(scale_.x, scale_.y, scale_.z);\r
+\r
+    CV_CheckGlError();\r
+#endif\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////\r
+// Error handling\r
+\r
+bool icvCheckGlError(const char* file, const int line, const char* func)\r
+{\r
+#ifndef HAVE_OPENGL\r
+    return true;\r
+#else\r
+    GLenum err = glGetError();\r
+\r
+    if (err != GL_NO_ERROR)\r
+    {\r
+        const char* msg;\r
+\r
+        switch (err)\r
+        {\r
+        case GL_INVALID_ENUM:\r
+            msg = "An unacceptable value is specified for an enumerated argument";\r
+            break;\r
+        case GL_INVALID_VALUE:\r
+            msg = "A numeric argument is out of range";\r
+            break;\r
+        case GL_INVALID_OPERATION:\r
+            msg = "The specified operation is not allowed in the current state";\r
+            break;\r
+        case GL_STACK_OVERFLOW:\r
+            msg = "This command would cause a stack overflow";\r
+            break;\r
+        case GL_STACK_UNDERFLOW:\r
+            msg = "This command would cause a stack underflow";\r
+            break;\r
+        case GL_OUT_OF_MEMORY:\r
+            msg = "There is not enough memory left to execute the command";\r
+            break;\r
+        default:\r
+            msg = "Unknown error";\r
+        };\r
+\r
+        cvError(CV_OpenGlApiCallError, func, msg, file, line);\r
+\r
+        return false;\r
+    }\r
+\r
+    return true;\r
+#endif\r
+}\r
index a770f86..9e3d4d4 100644 (file)
@@ -44,7 +44,6 @@
 #define __OPENCV_HIGHGUI_HPP__
 
 #include "opencv2/core/core.hpp"
-#include "opencv2/core/gpumat.hpp"
 #include "opencv2/highgui/highgui_c.h"
 
 #ifdef __cplusplus
@@ -129,7 +128,7 @@ CV_EXPORTS_W void setTrackbarPos(const string& trackbarname, const string& winna
 typedef void (CV_CDECL *OpenGLCallback)(void* userdata);
 CV_EXPORTS void createOpenGLCallback(const string& winname, OpenGLCallback onOpenGlDraw, void* userdata = 0);
 
-typedef void (CV_CDECL *OpenGlDrawCallback)(void* userdata);
+typedef void (*OpenGlDrawCallback)(void* userdata);
 static inline void setOpenGlDrawCallback(const string& winname, OpenGlDrawCallback onOpenGlDraw, void* userdata = 0)
 {
     createOpenGLCallback(winname, onOpenGlDraw, userdata);
@@ -139,22 +138,8 @@ CV_EXPORTS void setOpenGlContext(const string& winname);
 
 CV_EXPORTS void updateWindow(const string& winname);
 
-CV_EXPORTS void imshow(const string& winname, const gpu::GlTexture& tex);
-CV_EXPORTS void imshow(const string& winname, const gpu::GlBuffer& buf);
-CV_EXPORTS void imshow(const string& winname, const gpu::GpuMat& d_mat);
-
-CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlArrays& arr);
-CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlBuffer& points, 
-                               const gpu::GlBuffer& colors = gpu::GlBuffer(gpu::GlBuffer::ARRAY_BUFFER));
-CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GpuMat& points, 
-                               const gpu::GpuMat& colors = gpu::GpuMat());
-CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, InputArray points, 
-                               InputArray colors = noArray());
-
-CV_EXPORTS void addTextOpenGl(const string& winname, const string& text, Point org, Scalar color = Scalar::all(255), 
-                              const string& fontName = "Courier New", int fontHeight = 12, 
-                              int fontWeight = CV_FONT_NORMAL, int fontStyle = CV_STYLE_NORMAL);
-CV_EXPORTS void clearTextOpenGl(const string& winname);
+CV_EXPORTS void pointCloudShow(const string& winname, const GlCamera& camera, const GlArrays& arr);
+CV_EXPORTS void pointCloudShow(const string& winname, const GlCamera& camera, InputArray points, InputArray colors = noArray());
 
 //Only for Qt
 
index 6f6cc0d..4513c07 100644 (file)
@@ -63,8 +63,7 @@ enum {  CV_FONT_LIGHT           = 25,//QFont::Light,
 
 enum {  CV_STYLE_NORMAL         = 0,//QFont::StyleNormal,
         CV_STYLE_ITALIC         = 1,//QFont::StyleItalic,
-        CV_STYLE_OBLIQUE        = 2,//QFont::StyleOblique
-        CV_STYLE_UNDERLINE      = 4
+        CV_STYLE_OBLIQUE        = 2 //QFont::StyleOblique
 };
 /* ---------*/
 
@@ -258,11 +257,6 @@ CVAPI(void) cvCreateOpenGLCallback( const char* window_name, CvOpenGLCallback ca
 CVAPI(void) cvSetOpenGlContext(const char* window_name);
 CVAPI(void) cvUpdateWindow(const char* window_name);
 
-CVAPI(void) cvAddTextOpenGl(const char* winname, const char* text, CvPoint org, CvScalar color CV_DEFAULT(cvScalar(255.0, 255.0, 255.0, 255.0)), 
-                            const char* fontName CV_DEFAULT("Courier New"), int fontHeight CV_DEFAULT(12), 
-                            int fontWeight CV_DEFAULT(CV_FONT_NORMAL), int fontStyle CV_DEFAULT(CV_STYLE_NORMAL));
-
-CVAPI(void) cvClearTextOpenGl(const char* winname);
 
 /****************************************************************************************\
 *                         Working with Video Files and Cameras                           *
index 2ee6d3f..7e49c37 100644 (file)
@@ -40,6 +40,7 @@
 //M*/
 
 #include "precomp.hpp"
+#include "opencv2/core/opengl_interop.hpp"
 
 // in later times, use this file as a dispatcher to implementations like cvcap.cpp
 
@@ -284,7 +285,7 @@ namespace
 
     struct GlObjTex : GlObjBase
     {
-        cv::gpu::GlTexture tex;
+        cv::GlTexture tex;
     };
 
     void CV_CDECL glDrawTextureCallback(void* userdata)
@@ -293,17 +294,17 @@ namespace
 
         CV_DbgAssert(texObj->flag == CV_TEXTURE_MAGIC_VAL);
 
-        static cv::gpu::GlCamera glCamera;
+        static cv::GlCamera glCamera;
 
         glCamera.setupProjectionMatrix();
 
-        cv::gpu::render(texObj->tex);
+        cv::render(texObj->tex);
     }
 
     struct GlObjPointCloud : GlObjBase
     {
-        cv::gpu::GlArrays arr;
-        cv::gpu::GlCamera camera;
+        cv::GlArrays arr;
+        cv::GlCamera camera;
     };
 
     void CV_CDECL glDrawPointCloudCallback(void* userdata)
@@ -315,7 +316,7 @@ namespace
         pointCloudObj->camera.setupProjectionMatrix();
         pointCloudObj->camera.setupModelViewMatrix();
 
-        cv::gpu::render(pointCloudObj->arr);
+        cv::render(pointCloudObj->arr);
     }
 
     void CV_CDECL glCleanCallback(void* userdata)
@@ -327,20 +328,31 @@ namespace
 }
 #endif // HAVE_OPENGL
 
-#ifdef HAVE_OPENGL
-
-namespace
+void cv::imshow( const string& winname, InputArray _img )
 {
-    template <typename T> void imshowImpl(const std::string& winname, const T& img)
+#ifndef HAVE_OPENGL
+    Mat img = _img.getMat();
+    CvMat c_img = img;
+    cvShowImage(winname.c_str(), &c_img);
+#else
+    double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
+    if (useGl <= 0)
+    {
+        Mat img = _img.getMat();
+        CvMat c_img = img;
+        cvShowImage(winname.c_str(), &c_img);
+    }
+    else
     {
-        using namespace cv;
-
         namedWindow(winname, WINDOW_OPENGL | WINDOW_AUTOSIZE);
 
         double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
 
         if (autoSize > 0)
-            resizeWindow(winname, img.cols, img.rows);
+        {
+            Size size = _img.size();
+            resizeWindow(winname, size.width, size.height);
+        }
 
         setOpenGlContext(winname);
 
@@ -355,12 +367,12 @@ namespace
         if (glObj)
         {
             GlObjTex* texObj = static_cast<GlObjTex*>(glObj);
-            texObj->tex.copyFrom(img);
+            texObj->tex.copyFrom(_img);
         }
         else
         {
             GlObjTex* texObj = new GlObjTex;
-            texObj->tex.copyFrom(img);
+            texObj->tex.copyFrom(_img);
 
             glObj = texObj;
             glObj->flag = CV_TEXTURE_MAGIC_VAL;
@@ -375,68 +387,21 @@ namespace
 
         updateWindow(winname);
     }
-}
-
-#endif // HAVE_OPENGL
-
-void cv::imshow( const string& winname, InputArray _img )
-{
-    Mat img = _img.getMat();
-
-#ifndef HAVE_OPENGL
-    CvMat c_img = img;
-    cvShowImage(winname.c_str(), &c_img);
-#else
-    double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
-    if (useGl <= 0)
-    {
-        CvMat c_img = img;
-        cvShowImage(winname.c_str(), &c_img);
-    }
-    else
-    {
-        imshowImpl(winname, img);
-    }
-#endif
-}
-
-void cv::imshow(const string& winname, const gpu::GlBuffer& buf)
-{
-#ifndef HAVE_OPENGL
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-#else
-    imshowImpl(winname, buf);
 #endif
 }
 
-void cv::imshow(const string& winname, const gpu::GpuMat& d_mat)
+void cv::pointCloudShow(const string& winname, const GlCamera& camera, const GlArrays& arr)
 {
 #ifndef HAVE_OPENGL
     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
 #else
-    setOpenGlContext(winname);
-    gpu::GlBuffer buf(d_mat, gpu::GlBuffer::TEXTURE_BUFFER);
-    imshow(winname, buf);
-#endif
-}
-
-void cv::imshow(const string& winname, const gpu::GlTexture& tex)
-{
-#ifndef HAVE_OPENGL
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-#else
-    namedWindow(winname, WINDOW_OPENGL | WINDOW_AUTOSIZE);
-
-    double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
-
-    if (autoSize > 0)
-        resizeWindow(winname, tex.cols, tex.rows);
+    namedWindow(winname, WINDOW_OPENGL);
 
     setOpenGlContext(winname);
 
     GlObjBase* glObj = findGlObjByName(winname);
 
-    if (glObj && glObj->flag != CV_TEXTURE_MAGIC_VAL)
+    if (glObj && glObj->flag != CV_POINT_CLOUD_MAGIC_VAL)
     {
         icvSetOpenGlCleanCallback(winname.c_str(), 0, 0);
         glObj = 0;
@@ -444,16 +409,18 @@ void cv::imshow(const string& winname, const gpu::GlTexture& tex)
 
     if (glObj)
     {
-        GlObjTex* texObj = static_cast<GlObjTex*>(glObj);
-        texObj->tex = tex;
+        GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(glObj);
+        pointCloudObj->arr = arr;
+        pointCloudObj->camera = camera;
     }
     else
     {
-        GlObjTex* texObj = new GlObjTex;
-        texObj->tex = tex;
+        GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
+        pointCloudObj->arr = arr;
+        pointCloudObj->camera = camera;
 
-        glObj = texObj;
-        glObj->flag = CV_TEXTURE_MAGIC_VAL;
+        glObj = pointCloudObj;
+        glObj->flag = CV_POINT_CLOUD_MAGIC_VAL;
         glObj->winname = winname;
 
         addGlObj(glObj);
@@ -461,13 +428,13 @@ void cv::imshow(const string& winname, const gpu::GlTexture& tex)
         icvSetOpenGlCleanCallback(winname.c_str(), glCleanCallback, glObj);
     }
 
-    setOpenGlDrawCallback(winname, glDrawTextureCallback, glObj);
+    setOpenGlDrawCallback(winname, glDrawPointCloudCallback, glObj);
 
     updateWindow(winname);
 #endif
 }
 
-void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlArrays& arr)
+void cv::pointCloudShow(const std::string& winname, const cv::GlCamera& camera, InputArray points, InputArray colors)
 {
 #ifndef HAVE_OPENGL
     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
@@ -487,13 +454,23 @@ void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, cons
     if (glObj)
     {
         GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(glObj);
-        pointCloudObj->arr = arr;
+
+        pointCloudObj->arr.setVertexArray(points);
+        if (colors.empty())
+            pointCloudObj->arr.resetColorArray();
+        else
+            pointCloudObj->arr.setColorArray(colors);
+
         pointCloudObj->camera = camera;
     }
     else
     {
         GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
-        pointCloudObj->arr = arr;
+
+        pointCloudObj->arr.setVertexArray(points);
+        if (!colors.empty())
+            pointCloudObj->arr.setColorArray(colors);
+
         pointCloudObj->camera = camera;
 
         glObj = pointCloudObj;
@@ -511,104 +488,6 @@ void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, cons
 #endif
 }
 
-#ifdef HAVE_OPENGL
-
-namespace
-{
-    template <typename T> void pointCloudShowImpl(const std::string& winname, const cv::gpu::GlCamera& camera, const T& points, const T& colors)
-    {
-        using namespace cv;
-
-        namedWindow(winname, WINDOW_OPENGL);
-
-        setOpenGlContext(winname);
-
-        GlObjBase* glObj = findGlObjByName(winname);
-
-        if (glObj && glObj->flag != CV_POINT_CLOUD_MAGIC_VAL)
-        {
-            icvSetOpenGlCleanCallback(winname.c_str(), 0, 0);
-            glObj = 0;
-        }
-
-        if (glObj)
-        {
-            GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(glObj);
-
-            pointCloudObj->arr.setVertexArray(points);
-            if (colors.empty())
-                pointCloudObj->arr.resetColorArray();
-            else
-                pointCloudObj->arr.setColorArray(colors);
-
-            pointCloudObj->camera = camera;
-        }
-        else
-        {
-            GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
-
-            pointCloudObj->arr.setVertexArray(points);
-            if (!colors.empty())
-                pointCloudObj->arr.setColorArray(colors);
-
-            pointCloudObj->camera = camera;
-
-            glObj = pointCloudObj;
-            glObj->flag = CV_POINT_CLOUD_MAGIC_VAL;
-            glObj->winname = winname;
-
-            addGlObj(glObj);
-
-            icvSetOpenGlCleanCallback(winname.c_str(), glCleanCallback, glObj);
-        }
-
-        setOpenGlDrawCallback(winname, glDrawPointCloudCallback, glObj);
-
-        updateWindow(winname);
-    }
-}
-
-#endif // HAVE_OPENGL
-
-void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlBuffer& points, const gpu::GlBuffer& colors)
-{
-#ifndef HAVE_OPENGL
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-#else
-    pointCloudShowImpl(winname, camera, points, colors);
-#endif
-}
-
-void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GpuMat& points, const gpu::GpuMat& colors)
-{
-#ifndef HAVE_OPENGL
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-#else
-    pointCloudShowImpl(winname, camera, points, colors);
-#endif
-}
-
-void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, InputArray points, InputArray colors)
-{
-#ifndef HAVE_OPENGL
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-#else
-    pointCloudShowImpl(winname, camera, points, colors);
-#endif
-}
-
-// OpenGL text
-
-void cv::addTextOpenGl(const string& winname, const string& text, Point org, Scalar color, const string& fontName, int fontHeight, int fontWeight, int fontStyle)
-{
-    cvAddTextOpenGl(winname.c_str(), text.c_str(), org, color, fontName.c_str(), fontHeight, fontWeight, fontStyle);
-}
-
-void cv::clearTextOpenGl(const string& winname)
-{
-    cvClearTextOpenGl(winname.c_str());
-}
-
 // Without OpenGL
 
 #ifndef HAVE_OPENGL
@@ -630,16 +509,6 @@ CV_IMPL void cvUpdateWindow(const char*)
     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
 }
 
-CV_IMPL void cvAddTextOpenGl(const char*, const char*, CvPoint, CvScalar, const char*, int, int, int)
-{
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-}
-
-CV_IMPL void cvClearTextOpenGl(const char*)
-{
-    CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
-}
-
 void icvSetOpenGlCleanCallback(const char*, CvOpenGlCleanCallback, void*)
 {
     CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
index 0572dc5..3bbed9e 100644 (file)
@@ -60,7 +60,6 @@
 #include <vector>
 #include <functional>
 #include "opencv2/highgui/highgui.hpp"
-#include "opencv2/core/gpumat.hpp"
 #include <GL\gl.h>
 #endif
 
@@ -137,216 +136,6 @@ typedef struct CvTrackbar
 }
 CvTrackbar;
 
-// OpenGL support
-
-#ifdef HAVE_OPENGL
-
-namespace
-{
-    class OpenGlFont
-    {
-    public:
-        OpenGlFont(HDC hDC, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle);
-        ~OpenGlFont();
-
-        void draw(const char* str, int len, CvPoint org, CvScalar color, int width, int height) const;
-
-        inline const std::string& fontName() const { return fontName_; }
-        inline int fontHeight() const { return fontHeight_; }
-        inline int fontWeight() const { return fontWeight_; }
-        inline int fontStyle() const { return fontStyle_; }
-
-    private:
-        std::string fontName_;
-        int fontHeight_;
-        int fontWeight_;
-        int fontStyle_;
-
-        GLuint base_;
-
-        OpenGlFont(const OpenGlFont&);
-        OpenGlFont& operator =(const OpenGlFont&);
-    };
-
-    int getFontWidthW32(int fontWeight)
-    {
-        int weight = 0;
-
-        switch(fontWeight)
-        {
-        case CV_FONT_LIGHT: 
-            weight = 200;
-            break;
-        case CV_FONT_NORMAL: 
-            weight = FW_NORMAL;
-            break;
-        case CV_FONT_DEMIBOLD: 
-            weight = 550;
-            break;
-        case CV_FONT_BOLD: 
-            weight = FW_BOLD;
-            break;
-        case CV_FONT_BLACK: 
-            weight = FW_BLACK;
-            break;
-        default:
-            cvError(CV_StsBadArg, "getFontWidthW32", "Unsopported font width", __FILE__, __LINE__);
-        };
-
-        return weight;
-    }
-
-    OpenGlFont::OpenGlFont(HDC hDC, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle)
-        : fontName_(), fontHeight_(0), fontWeight_(0), fontStyle_(0), base_(0)
-    {
-        base_ = glGenLists(96);
-
-        HFONT font = CreateFont
-        (
-            -fontHeight,                                   // height
-            0,                                             // cell width
-            0,                                             // Angle of Escapement
-            0,                                             // Orientation Angle
-            getFontWidthW32(fontWeight),                   // font weight
-            fontStyle & CV_STYLE_ITALIC ? TRUE : FALSE,    // Italic
-            fontStyle & CV_STYLE_UNDERLINE ? TRUE : FALSE, // Underline
-            FALSE,                                         // StrikeOut  
-            ANSI_CHARSET,                                  // CharSet  
-            OUT_TT_PRECIS,                                 // OutPrecision
-            CLIP_DEFAULT_PRECIS,                           // ClipPrecision
-            ANTIALIASED_QUALITY,                           // Quality
-            FF_DONTCARE | DEFAULT_PITCH,                   // PitchAndFamily
-            fontName.c_str()                               // FaceName
-        );
-
-        SelectObject(hDC, font);
-
-        if (!wglUseFontBitmaps(hDC, 32, 96, base_))
-            cvError(CV_OpenGlApiCallError, "OpenGlFont", "Can't create font", __FILE__, __LINE__);
-
-        fontName_ = fontName;
-        fontHeight_ = fontHeight;
-        fontWeight_ = fontWeight;
-        fontStyle_ = fontStyle;
-    }
-
-    OpenGlFont::~OpenGlFont()
-    {    
-        if (base_)
-            glDeleteLists(base_, 96);
-    }
-
-    void OpenGlFont::draw(const char* str, int len, CvPoint org, CvScalar color, int width, int height) const
-    {
-        if (base_)
-        {
-            glPushAttrib(GL_LIST_BIT);
-            glListBase(base_ - 32);
-
-            glColor4dv(color.val);
-            glRasterPos2f(static_cast<float>(org.x) / width, static_cast<float>((org.y + fontHeight_)) / height);
-            glCallLists(len, GL_UNSIGNED_BYTE, str);
-
-            glPopAttrib();
-
-            CV_CheckGlError();
-        }
-    }
-
-    class OpenGlText
-    {
-    public:
-        OpenGlText(HDC hDC);
-
-        void add(const std::string& text, CvPoint org, CvScalar color, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle);
-        inline void clear() { text_.clear(); }
-
-        void draw(int width, int height) const;
-
-    private:
-        struct Text
-        {
-            std::string str;
-
-            CvPoint org;
-            CvScalar color;
-
-            cv::Ptr<OpenGlFont> font;
-        };
-
-        HDC hDC_;
-
-        std::vector< cv::Ptr<OpenGlFont> > fonts_;
-
-        std::vector<Text> text_;
-    };
-
-    OpenGlText::OpenGlText(HDC hDC) : hDC_(hDC)
-    {
-        fonts_.reserve(5);
-        text_.reserve(5);
-    }
-
-    class FontCompare : public std::unary_function<cv::Ptr<OpenGlFont>, bool>
-    {
-    public:
-        inline FontCompare(const std::string& fontName, int fontHeight, int fontWeight, int fontStyle) 
-            : fontName_(fontName), fontHeight_(fontHeight), fontWeight_(fontWeight), fontStyle_(fontStyle)
-        {
-        }
-
-        bool operator ()(const cv::Ptr<OpenGlFont>& font)
-        {
-            return font->fontName() == fontName_ && font->fontHeight() == fontHeight_ && font->fontWeight() == fontWeight_ && font->fontStyle() == fontStyle_;
-        }
-
-    private:
-        std::string fontName_;
-        int fontHeight_;
-        int fontWeight_;
-        int fontStyle_;
-    };
-
-    void OpenGlText::add(const std::string& str, CvPoint org, CvScalar color, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle)
-    {
-        std::vector< cv::Ptr<OpenGlFont> >::iterator fontIt = 
-            std::find_if(fonts_.begin(), fonts_.end(), FontCompare(fontName, fontHeight, fontWeight, fontStyle));
-
-        if (fontIt == fonts_.end())
-        {
-            fonts_.push_back(new OpenGlFont(hDC_, fontName, fontHeight, fontWeight, fontStyle));
-            fontIt = fonts_.end() - 1;
-        }
-
-        Text text;
-        text.str = str;
-        text.org = org;
-        text.color = color;
-        text.font = *fontIt;
-
-        text_.push_back(text);
-    }
-
-    void OpenGlText::draw(int width, int height) const
-    {        
-        glDisable(GL_DEPTH_TEST);
-
-        static cv::gpu::GlCamera glCamera;
-        glCamera.setupProjectionMatrix();
-
-        glMatrixMode(GL_MODELVIEW);
-        glLoadIdentity();
-
-        for (size_t i = 0, size = text_.size(); i < size; ++i)
-        {
-            const Text& text = text_[i];
-            text.font->draw(text.str.c_str(), text.str.length(), text.org, text.color, width, height);
-        }
-    }
-}
-
-#endif // HAVE_OPENGL
-
 
 typedef struct CvWindow
 {
@@ -391,9 +180,7 @@ typedef struct CvWindow
     CvOpenGlCleanCallback glCleanCallback;
     void* glCleanData;
 
-    cv::gpu::GlFuncTab* glFuncTab;
-
-    OpenGlText* glText;
+    CvOpenGlFuncTab* glFuncTab;
 #endif
 }
 CvWindow;
@@ -809,9 +596,26 @@ namespace
     typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERPROC) (GLenum target, GLenum access);
     typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERPROC) (GLenum target);
 
-    class GlFuncTab_W32 : public cv::gpu::GlFuncTab
+    class GlFuncTab_W32 : public CvOpenGlFuncTab
     {
     public:
+        GlFuncTab_W32(HDC hDC);
+
+        void genBuffers(int n, unsigned int* buffers) const;
+        void deleteBuffers(int n, const unsigned int* buffers) const;
+
+        void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const;
+        void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const;
+
+        void bindBuffer(unsigned int target, unsigned int buffer) const;
+
+        void* mapBuffer(unsigned int target, unsigned int access) const;
+        void unmapBuffer(unsigned int target) const;
+
+        void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const;
+
+        bool isGlContextInitialized() const;
+            
         PFNGLGENBUFFERSPROC    glGenBuffersExt;
         PFNGLDELETEBUFFERSPROC glDeleteBuffersExt;
 
@@ -825,142 +629,180 @@ namespace
 
         bool initialized;
 
-        GlFuncTab_W32()
-        {
-            glGenBuffersExt    = 0;
-            glDeleteBuffersExt = 0;
+        HDC hDC;
+    };
 
-            glBufferDataExt    = 0;
-            glBufferSubDataExt = 0;
+    GlFuncTab_W32::GlFuncTab_W32(HDC hDC_)
+    {
+        glGenBuffersExt    = 0;
+        glDeleteBuffersExt = 0;
 
-            glBindBufferExt    = 0;
+        glBufferDataExt    = 0;
+        glBufferSubDataExt = 0;
 
-            glMapBufferExt     = 0;
-            glUnmapBufferExt   = 0;
+        glBindBufferExt    = 0;
 
-            initialized = false;
-        }
+        glMapBufferExt     = 0;
+        glUnmapBufferExt   = 0;
 
-        void genBuffers(int n, unsigned int* buffers) const
-        {
-            CV_FUNCNAME( "genBuffers" );
+        initialized = false;
 
-            __BEGIN__;
+        hDC = hDC_;
+    }
 
-            if (!glGenBuffersExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+    void GlFuncTab_W32::genBuffers(int n, unsigned int* buffers) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::genBuffers" );
 
-            glGenBuffersExt(n, buffers);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
-        }
+        if (!glGenBuffersExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-        void deleteBuffers(int n, const unsigned int* buffers) const
-        {
-            CV_FUNCNAME( "deleteBuffers" );
+        glGenBuffersExt(n, buffers);
+        CV_CheckGlError();
 
-            __BEGIN__;
+        __END__;
+    }
 
-            if (!glDeleteBuffersExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+    void GlFuncTab_W32::deleteBuffers(int n, const unsigned int* buffers) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::deleteBuffers" );
 
-            glDeleteBuffersExt(n, buffers);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
-        }
+        if (!glDeleteBuffersExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-        void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
-        {
-            CV_FUNCNAME( "bufferData" );
+        glDeleteBuffersExt(n, buffers);
+        CV_CheckGlError();
 
-            __BEGIN__;
+        __END__;
+    }
 
-            if (!glBufferDataExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+    void GlFuncTab_W32::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::bufferData" );
 
-            glBufferDataExt(target, size, data, usage);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
-        }
+        if (!glBufferDataExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-        void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
-        {
-            CV_FUNCNAME( "bufferSubData" );
+        glBufferDataExt(target, size, data, usage);
+        CV_CheckGlError();
 
-            __BEGIN__;
+        __END__;
+    }
 
-            if (!glBufferSubDataExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+    void GlFuncTab_W32::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::bufferSubData" );
 
-            glBufferSubDataExt(target, offset, size, data);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
-        }
+        if (!glBufferSubDataExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-        void bindBuffer(unsigned int target, unsigned int buffer) const
-        {
-            CV_FUNCNAME( "bindBuffer" );
+        glBufferSubDataExt(target, offset, size, data);
+        CV_CheckGlError();
 
-            __BEGIN__;
+        __END__;
+    }
 
-            if (!glBindBufferExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+    void GlFuncTab_W32::bindBuffer(unsigned int target, unsigned int buffer) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::bindBuffer" );
 
-            glBindBufferExt(target, buffer);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
-        }
+        if (!glBindBufferExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-        void* mapBuffer(unsigned int target, unsigned int access) const
-        {
-            CV_FUNCNAME( "mapBuffer" );
+        glBindBufferExt(target, buffer);
+        CV_CheckGlError();
 
-            void* res = 0;
+        __END__;
+    }
 
-            __BEGIN__;
+    void* GlFuncTab_W32::mapBuffer(unsigned int target, unsigned int access) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::mapBuffer" );
 
-            if (!glMapBufferExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+        void* res = 0;
 
-            res = glMapBufferExt(target, access);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
+        if (!glMapBufferExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-            return res;
-        }
+        res = glMapBufferExt(target, access);
+        CV_CheckGlError();
 
-        void unmapBuffer(unsigned int target) const
-        {
-            CV_FUNCNAME( "unmapBuffer" );
+        __END__;
 
-            __BEGIN__;
+        return res;
+    }
 
-            if (!glUnmapBufferExt)
-                CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
+    void GlFuncTab_W32::unmapBuffer(unsigned int target) const
+    {
+        CV_FUNCNAME( "GlFuncTab_W32::unmapBuffer" );
 
-            glUnmapBufferExt(target);
-            CV_CheckGlError();
+        __BEGIN__;
 
-            __END__;
-        }
+        if (!glUnmapBufferExt)
+            CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
 
-        bool isGlContextInitialized() const
-        {
-            return initialized;
-        }
-    };
+        glUnmapBufferExt(target);
+        CV_CheckGlError();
+
+        __END__;
+    }
+
+    void GlFuncTab_W32::generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const
+    {
+        HFONT font;
+
+        CV_FUNCNAME( "GlFuncTab_W32::generateBitmapFont" );
+
+        __BEGIN__;
+
+        font = CreateFont
+        (
+            -height,                     // height
+            0,                           // cell width
+            0,                           // Angle of Escapement
+            0,                           // Orientation Angle
+            weight,                      // font weight
+            italic ? TRUE : FALSE,       // Italic
+            underline ? TRUE : FALSE,    // Underline
+            FALSE,                       // StrikeOut  
+            ANSI_CHARSET,                // CharSet  
+            OUT_TT_PRECIS,               // OutPrecision
+            CLIP_DEFAULT_PRECIS,         // ClipPrecision
+            ANTIALIASED_QUALITY,         // Quality
+            FF_DONTCARE | DEFAULT_PITCH, // PitchAndFamily
+            family.c_str()               // FaceName
+        );
+
+        SelectObject(hDC, font);
+
+        if (!wglUseFontBitmaps(hDC, start, count, base))
+            CV_ERROR(CV_OpenGlApiCallError, "Can't create font");
+
+        __END__;
+    }
+
+    bool GlFuncTab_W32::isGlContextInitialized() const
+    {
+        return initialized;
+    }
 
     void initGl(CvWindow* window)
     {
         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 
-        std::auto_ptr<GlFuncTab_W32> glFuncTab(new GlFuncTab_W32);
+        std::auto_ptr<GlFuncTab_W32> glFuncTab(new GlFuncTab_W32(window->dc));
 
         // Load extensions
         PROC func;
@@ -990,7 +832,7 @@ namespace
 
         window->glFuncTab = glFuncTab.release();
 
-        cv::gpu::setGlFuncTab(window->glFuncTab);
+        icvSetOpenGlFuncTab(window->glFuncTab);
     }
 
     void createGlContext(HWND hWnd, HDC& hGLDC, HGLRC& hGLRC, bool& useGl)
@@ -1089,11 +931,6 @@ namespace
 
         CV_CheckGlError();
 
-        if (window->glText)
-            window->glText->draw(window->width, window->height);
-
-        CV_CheckGlError();
-
         if (!SwapBuffers(window->dc))
             CV_ERROR( CV_OpenGlApiCallError, "Can't swap OpenGL buffers" );
 
@@ -1209,8 +1046,6 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
 
     window->glCleanCallback = 0;
     window->glCleanData = 0;
-
-    window->glText = 0;
 #endif
 
     window->last_key = 0;
@@ -1261,68 +1096,7 @@ CV_IMPL void cvSetOpenGlContext(const char* name)
     if (!wglMakeCurrent(window->dc, window->hGLRC))
         CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
 
-    cv::gpu::setGlFuncTab(window->glFuncTab);
-
-    __END__;
-}
-
-CV_IMPL void cvAddTextOpenGl(const char* name, const char* text, CvPoint org, CvScalar color, const char* fontName, int fontHeight, int fontWeight, int fontStyle)
-{
-    CV_FUNCNAME( "cvAddTextOpenGl" );
-
-    __BEGIN__;
-
-    CvWindow* window;
-
-    if(!name)
-        CV_ERROR( CV_StsNullPtr, "NULL name string" );
-
-    window = icvFindWindowByName( name );
-    if (!window)
-        CV_ERROR( CV_StsNullPtr, "NULL window" );
-
-    if (!window->useGl)
-        CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
-
-    if (!wglMakeCurrent(window->dc, window->hGLRC))
-        CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
-
-    if (!window->glText)
-        window->glText = new OpenGlText(window->dc);
-
-    window->glText->add(text, org, color, fontName, fontHeight, fontWeight, fontStyle);
-
-    InvalidateRect(window->hwnd, 0, 0);
-
-    __END__;
-}
-
-CV_IMPL void cvClearTextOpenGl(const char* name)
-{
-    CV_FUNCNAME( "cvClearTextOpenGl" );
-
-    __BEGIN__;
-
-    CvWindow* window;
-
-    if(!name)
-        CV_ERROR( CV_StsNullPtr, "NULL name string" );
-
-    window = icvFindWindowByName( name );
-    if (!window)
-        CV_ERROR( CV_StsNullPtr, "NULL window" );
-
-    if (!window->useGl)
-        CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
-
-    if (!wglMakeCurrent(window->dc, window->hGLRC))
-        CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
-
-    if (window->glText)
-    {
-        window->glText->clear();
-        InvalidateRect(window->hwnd, 0, 0);
-    }
+    icvSetOpenGlFuncTab(window->glFuncTab);
 
     __END__;
 }
@@ -1406,9 +1180,6 @@ static void icvRemoveWindow( CvWindow* window )
     if (window->useGl)
     {
         wglMakeCurrent(window->dc, window->hGLRC);
-        
-        if (window->glText)
-            delete window->glText;
 
         if (window->glCleanCallback)
         {
index d668da6..e1fddf1 100644 (file)
@@ -3,7 +3,7 @@
 #include <iostream>\r
 #include <sstream>\r
 #include "opencv2/core/core.hpp"\r
-#include "opencv2/core/gpumat.hpp"\r
+#include "opencv2/core/opengl_interop.hpp"\r
 #include "opencv2/highgui/highgui.hpp"\r
 #include "opencv2/imgproc/imgproc.hpp"\r
 #include "opencv2/calib3d/calib3d.hpp"\r
@@ -13,56 +13,52 @@ using namespace std;
 using namespace cv;\r
 using namespace cv::gpu;\r
 \r
-void mouseCallback(int event, int x, int y, int flags, void* userdata)\r
+class PointCloudRenderer\r
 {\r
-    int* dx = static_cast<int*>(userdata);\r
-    int* dy = dx + 1;\r
-\r
-    static int oldx = x;\r
-    static int oldy = y;\r
-    static bool moving = false;\r
+public:\r
+    PointCloudRenderer(const Mat& points, const Mat& img, double scale);\r
+\r
+    void onMouseEvent(int event, int x, int y, int flags);\r
+    void draw();\r
+    void update(int key, double aspect);\r
+\r
+    int fov_;\r
+\r
+private:\r
+    int mouse_dx_;\r
+    int mouse_dy_;\r
+    \r
+    double yaw_;\r
+    double pitch_;\r
+    Point3d pos_;\r
+\r
+    TickMeter tm_;\r
+    static const int step_;\r
+    int frame_;\r
+    \r
+    GlCamera camera_;\r
+    GlArrays pointCloud_;\r
+    string fps_;\r
+};\r
+\r
+bool stop = false;\r
 \r
-    if (event == EVENT_LBUTTONDOWN)\r
-    {\r
-        oldx = x;\r
-        oldy = y;\r
-        moving = true;\r
-    }\r
-    else if (event == EVENT_LBUTTONUP)\r
-    {\r
-        moving = false;\r
-    }\r
-\r
-    if (moving)\r
-    {\r
-        *dx = oldx - x;\r
-        *dy = oldy - y;\r
-    }\r
-    else\r
-    {\r
-        *dx = 0;\r
-        *dy = 0;\r
-    }\r
-}\r
-\r
-inline int clamp(int val, int minVal, int maxVal)\r
+void mouseCallback(int event, int x, int y, int flags, void* userdata)\r
 {\r
-    return max(min(val, maxVal), minVal);\r
+    if (stop)\r
+        return;\r
+\r
+    PointCloudRenderer* renderer = static_cast<PointCloudRenderer*>(userdata);\r
+    renderer->onMouseEvent(event, x, y, flags);\r
 }\r
 \r
-Point3d rotate(Point3d v, double yaw, double pitch)\r
+void openGlDrawCallback(void* userdata)\r
 {\r
-    Point3d t1;\r
-    t1.x = v.x * cos(-yaw / 180.0 * CV_PI) - v.z * sin(-yaw / 180.0 * CV_PI);\r
-    t1.y = v.y;\r
-    t1.z = v.x * sin(-yaw / 180.0 * CV_PI) + v.z * cos(-yaw / 180.0 * CV_PI);\r
+    if (stop)\r
+        return;\r
 \r
-    Point3d t2;\r
-    t2.x = t1.x;\r
-    t2.y = t1.y * cos(pitch / 180.0 * CV_PI) - t1.z * sin(pitch / 180.0 * CV_PI);\r
-    t2.z = t1.y * sin(pitch / 180.0 * CV_PI) + t1.z * cos(pitch / 180.0 * CV_PI);\r
-\r
-    return t2;\r
+    PointCloudRenderer* renderer = static_cast<PointCloudRenderer*>(userdata);\r
+    renderer->draw();\r
 }\r
 \r
 int main(int argc, const char* argv[])\r
@@ -192,96 +188,168 @@ int main(int argc, const char* argv[])
     const string windowName = "OpenGL Sample";\r
 \r
     namedWindow(windowName, WINDOW_OPENGL);\r
+    resizeWindow(windowName, 400, 400);\r
+    \r
+    PointCloudRenderer renderer(points, imgLeftColor, scale);\r
 \r
-    int fov = 0;\r
-    createTrackbar("Fov", windowName, &fov, 100);\r
+    createTrackbar("Fov", windowName, &renderer.fov_, 100);\r
+    setMouseCallback(windowName, mouseCallback, &renderer);\r
+    setOpenGlDrawCallback(windowName, openGlDrawCallback, &renderer);\r
 \r
-    int mouse[2] = {0, 0};\r
-    setMouseCallback(windowName, mouseCallback, mouse);\r
+    while (true)\r
+    {\r
+        int key = waitKey(1);\r
 \r
-    GlArrays pointCloud;\r
+        if (key >= 0)\r
+            key = key & 0xff;\r
 \r
-    pointCloud.setVertexArray(points);\r
-    pointCloud.setColorArray(imgLeftColor, false);\r
+        if (key == 27)\r
+        {\r
+            stop = true;\r
+            break;\r
+        }\r
 \r
-    GlCamera camera;\r
-    camera.setScale(Point3d(scale, scale, scale));\r
+        double aspect = getWindowProperty(windowName, WND_PROP_ASPECT_RATIO);\r
 \r
-    double yaw = 0.0;\r
-    double pitch = 0.0;\r
+        key = tolower(key);\r
 \r
-    const Point3d dirVec(0.0, 0.0, -1.0);\r
-    const Point3d upVec(0.0, 1.0, 0.0);\r
-    const Point3d leftVec(-1.0, 0.0, 0.0);\r
-    Point3d pos;\r
+        renderer.update(key, aspect);\r
+\r
+        updateWindow(windowName);\r
+    }\r
 \r
-    TickMeter tm;\r
-    const int step = 20;\r
-    int frame = 0;\r
+    return 0;\r
+}\r
 \r
-    while (true)\r
+const int PointCloudRenderer::step_ = 20;\r
+\r
+PointCloudRenderer::PointCloudRenderer(const Mat& points, const Mat& img, double scale)\r
+{\r
+    mouse_dx_ = 0;\r
+    mouse_dy_ = 0;\r
+\r
+    fov_ = 0;\r
+    yaw_ = 0.0;\r
+    pitch_ = 0.0;\r
+\r
+    frame_ = 0;\r
+\r
+    camera_.setScale(Point3d(scale, scale, scale));\r
+\r
+    pointCloud_.setVertexArray(points);\r
+    pointCloud_.setColorArray(img, false);\r
+\r
+    tm_.start();\r
+}\r
+\r
+inline int clamp(int val, int minVal, int maxVal)\r
+{\r
+    return max(min(val, maxVal), minVal);\r
+}\r
+\r
+void PointCloudRenderer::onMouseEvent(int event, int x, int y, int flags)\r
+{\r
+    static int oldx = x;\r
+    static int oldy = y;\r
+    static bool moving = false;\r
+\r
+    if (event == EVENT_LBUTTONDOWN)\r
     {\r
-        tm.start();\r
+        oldx = x;\r
+        oldy = y;\r
+        moving = true;\r
+    }\r
+    else if (event == EVENT_LBUTTONUP)\r
+    {\r
+        moving = false;\r
+    }\r
 \r
-        int key = waitKey(1);\r
-        if (key >= 0)\r
-            key = key & 0xff;\r
+    if (moving)\r
+    {\r
+        mouse_dx_ = oldx - x;\r
+        mouse_dy_ = oldy - y;\r
+    }\r
+    else\r
+    {\r
+        mouse_dx_ = 0;\r
+        mouse_dy_ = 0;\r
+    }\r
 \r
-        if (key == 27)\r
-            break;\r
+    const int mouseClamp = 300;\r
+    mouse_dx_ = clamp(mouse_dx_, -mouseClamp, mouseClamp);\r
+    mouse_dy_ = clamp(mouse_dy_, -mouseClamp, mouseClamp);\r
+}\r
 \r
-        double aspect = getWindowProperty(windowName, WND_PROP_ASPECT_RATIO);\r
+Point3d rotate(Point3d v, double yaw, double pitch)\r
+{\r
+    Point3d t1;\r
+    t1.x = v.x * cos(-yaw / 180.0 * CV_PI) - v.z * sin(-yaw / 180.0 * CV_PI);\r
+    t1.y = v.y;\r
+    t1.z = v.x * sin(-yaw / 180.0 * CV_PI) + v.z * cos(-yaw / 180.0 * CV_PI);\r
 \r
-        const double posStep = 0.1;\r
-        \r
-        #ifdef _WIN32\r
+    Point3d t2;\r
+    t2.x = t1.x;\r
+    t2.y = t1.y * cos(pitch / 180.0 * CV_PI) - t1.z * sin(pitch / 180.0 * CV_PI);\r
+    t2.z = t1.y * sin(pitch / 180.0 * CV_PI) + t1.z * cos(pitch / 180.0 * CV_PI);\r
+\r
+    return t2;\r
+}\r
+\r
+void PointCloudRenderer::update(int key, double aspect)\r
+{\r
+    const Point3d dirVec(0.0, 0.0, -1.0);\r
+    const Point3d upVec(0.0, 1.0, 0.0);\r
+    const Point3d leftVec(-1.0, 0.0, 0.0);\r
+\r
+    const double posStep = 0.1;\r
+    \r
+    #ifdef _WIN32\r
         const double mouseStep = 0.001;\r
-        #else\r
+    #else\r
         const double mouseStep = 0.000001;\r
-        #endif\r
+    #endif\r
         \r
-        const int mouseClamp = 300;\r
+    camera_.setPerspectiveProjection(30.0 + fov_ / 100.0 * 40.0, aspect, 0.1, 1000.0);\r
 \r
-        camera.setPerspectiveProjection(30.0 + fov / 100.0 * 40.0, aspect, 0.1, 1000.0);\r
+    yaw_ += mouse_dx_ * mouseStep;\r
+    pitch_ += mouse_dy_ * mouseStep;\r
 \r
-        int mouse_dx = clamp(mouse[0], -mouseClamp, mouseClamp);\r
-        int mouse_dy = clamp(mouse[1], -mouseClamp, mouseClamp);\r
+    if (key == 'w')\r
+        pos_ += posStep * rotate(dirVec, yaw_, pitch_);\r
+    else if (key == 's')\r
+        pos_ -= posStep * rotate(dirVec, yaw_, pitch_);\r
+    else if (key == 'a')\r
+        pos_ += posStep * rotate(leftVec, yaw_, pitch_);\r
+    else if (key == 'd')\r
+        pos_ -= posStep * rotate(leftVec, yaw_, pitch_);\r
+    else if (key == 'q')\r
+        pos_ += posStep * rotate(upVec, yaw_, pitch_);\r
+    else if (key == 'e')\r
+        pos_ -= posStep * rotate(upVec, yaw_, pitch_);\r
 \r
-        yaw += mouse_dx * mouseStep;\r
-        pitch += mouse_dy * mouseStep;\r
+    camera_.setCameraPos(pos_, yaw_, pitch_, 0.0);\r
 \r
-        key = tolower(key);\r
-        if (key == 'w')\r
-            pos += posStep * rotate(dirVec, yaw, pitch);\r
-        else if (key == 's')\r
-            pos -= posStep * rotate(dirVec, yaw, pitch);\r
-        else if (key == 'a')\r
-            pos += posStep * rotate(leftVec, yaw, pitch);\r
-        else if (key == 'd')\r
-            pos -= posStep * rotate(leftVec, yaw, pitch);\r
-        else if (key == 'q')\r
-            pos += posStep * rotate(upVec, yaw, pitch);\r
-        else if (key == 'e')\r
-            pos -= posStep * rotate(upVec, yaw, pitch);\r
-\r
-        camera.setCameraPos(pos, yaw, pitch, 0.0);\r
-\r
-        pointCloudShow(windowName, camera, pointCloud);\r
-\r
-        tm.stop();\r
-\r
-        if (frame++ >= step)\r
-        {\r
-            ostringstream fps;\r
-            fps << "FPS: " << step / tm.getTimeSec();\r
-            \r
-            clearTextOpenGl(windowName);\r
-            addTextOpenGl(windowName, fps.str(), Point(0, 0), Scalar::all(255), "Courier New", 16);\r
-\r
-            frame = 0;\r
-            tm.reset();\r
-        }\r
+    tm_.stop();\r
+\r
+    if (frame_++ >= step_)\r
+    {\r
+        ostringstream ostr;\r
+        ostr << "FPS: " << step_ / tm_.getTimeSec();\r
+        fps_ = ostr.str();\r
+        \r
+        frame_ = 0;\r
+        tm_.reset();\r
     }\r
 \r
-    return 0;\r
+    tm_.start();\r
+}\r
+\r
+void PointCloudRenderer::draw()\r
+{\r
+    camera_.setupProjectionMatrix();\r
+    camera_.setupModelViewMatrix();\r
+\r
+    render(pointCloud_);\r
+\r
+    render(fps_, GlFont::get("Courier New", 16), Scalar::all(255), Point2d(3.0, 0.0));\r
 }\r
index bf00743..821c70f 100644 (file)
@@ -3,6 +3,7 @@
 \r
 #include "opencv2/core/core.hpp"\r
 #include "opencv2/core/gpumat.hpp"\r
+#include "opencv2/core/opengl_interop.hpp"\r
 #include "opencv2/gpu/gpu.hpp"\r
 #include "opencv2/highgui/highgui.hpp"\r
 #include "opencv2/contrib/contrib.hpp"\r
@@ -43,11 +44,17 @@ int main(int argc, char* argv[])
     {\r
         bool haveCuda = getCudaEnabledDeviceCount() > 0;\r
 \r
-        namedWindow("OpenGL Mat", WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
-        namedWindow("OpenGL GlBuffer", WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
-        namedWindow("OpenGL GlTexture", WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
+        const string openGlMatWnd = "OpenGL Mat";\r
+        const string openGlBufferWnd = "OpenGL GlBuffer";\r
+        const string openGlTextureWnd = "OpenGL GlTexture";\r
+        const string openGlGpuMatWnd = "OpenGL GpuMat";\r
+        const string matWnd = "Mat";\r
+\r
+        namedWindow(openGlMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
+        namedWindow(openGlBufferWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
+        namedWindow(openGlTextureWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
         if (haveCuda)\r
-            namedWindow("OpenGL GpuMat", WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
+            namedWindow(openGlGpuMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);\r
         namedWindow("Mat", WINDOW_AUTOSIZE);\r
 \r
         Mat img = imread(argv[1]);\r
@@ -55,10 +62,10 @@ int main(int argc, char* argv[])
         if (haveCuda)\r
             setGlDevice();\r
 \r
-        setOpenGlContext("OpenGL GlBuffer");\r
+        setOpenGlContext(openGlBufferWnd);\r
         GlBuffer buf(img, GlBuffer::TEXTURE_BUFFER);\r
 \r
-        setOpenGlContext("OpenGL GlTexture");\r
+        setOpenGlContext(openGlTextureWnd);\r
         GlTexture tex(img);\r
         \r
         GpuMat d_img;\r
@@ -69,48 +76,50 @@ int main(int argc, char* argv[])
 \r
         {\r
             Timer t("OpenGL Mat      ");\r
-            imshow("OpenGL Mat", img);\r
+            imshow(openGlMatWnd, img);\r
         }\r
         {\r
             Timer t("OpenGL GlBuffer ");\r
-            imshow("OpenGL GlBuffer", buf);\r
+            imshow(openGlBufferWnd, buf);\r
         }\r
         {\r
             Timer t("OpenGL GlTexture");\r
-            imshow("OpenGL GlTexture", tex);\r
+            imshow(openGlTextureWnd, tex);\r
         }\r
         if (haveCuda)\r
         {\r
             Timer t("OpenGL GpuMat   ");\r
-            imshow("OpenGL GpuMat", d_img);\r
+            imshow(openGlGpuMatWnd, d_img);\r
         }\r
         {\r
             Timer t("Mat             ");\r
-            imshow("Mat", img);\r
+            imshow(matWnd, img);\r
         }\r
 \r
+        waitKey();\r
+\r
         cout << "\n=== Second call\n\n";   \r
 \r
         {\r
             Timer t("OpenGL Mat      ");\r
-            imshow("OpenGL Mat", img);\r
+            imshow(openGlMatWnd, img);\r
         }\r
         {\r
             Timer t("OpenGL GlBuffer ");\r
-            imshow("OpenGL GlBuffer", buf);\r
+            imshow(openGlBufferWnd, buf);\r
         }\r
         {\r
             Timer t("OpenGL GlTexture");\r
-            imshow("OpenGL GlTexture", tex);\r
+            imshow(openGlTextureWnd, tex);\r
         }\r
         if (haveCuda)\r
         {\r
             Timer t("OpenGL GpuMat   ");\r
-            imshow("OpenGL GpuMat", d_img);\r
+            imshow(openGlGpuMatWnd, d_img);\r
         }\r
         {\r
             Timer t("Mat             ");\r
-            imshow("Mat", img);\r
+            imshow(matWnd, img);\r
         }\r
 \r
         cout << "\n";\r