include/opencv2/core/opengl_interop.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other GpuMaterials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_OPENGL_INTEROP_HPP__
00044 #define __OPENCV_OPENGL_INTEROP_HPP__
00045 
00046 #ifdef __cplusplus
00047 
00048 #include "opencv2/core/core.hpp"
00049 
00050 namespace cv
00051 {
00053 class CV_EXPORTS GlBuffer
00054 {
00055 public:
00056     enum Usage
00057     {
00058         ARRAY_BUFFER = 0x8892,  // buffer will use for OpenGL arrays (vertices, colors, normals, etc)
00059         TEXTURE_BUFFER = 0x88EC // buffer will ise for OpenGL textures
00060     };
00061 
00063     explicit GlBuffer(Usage usage);
00064 
00066     GlBuffer(int rows, int cols, int type, Usage usage);
00067     GlBuffer(Size size, int type, Usage usage);
00068 
00070     GlBuffer(InputArray mat, Usage usage);
00071 
00072     void create(int rows, int cols, int type, Usage usage);
00073     void create(Size size, int type, Usage usage);
00074     void create(int rows, int cols, int type);
00075     void create(Size size, int type);
00076 
00077     void release();
00078 
00080     void copyFrom(InputArray mat);
00081 
00082     void bind() const;
00083     void unbind() const;
00084 
00086     Mat mapHost();
00087     void unmapHost();
00088 
00090     gpu::GpuMat mapDevice();
00091     void unmapDevice();
00092 
00093     inline int rows() const { return rows_; }
00094     inline int cols() const { return cols_; }
00095     inline Size size() const { return Size(cols_, rows_); }
00096     inline bool empty() const { return rows_ == 0 || cols_ == 0; }
00097 
00098     inline int type() const { return type_; }
00099     inline int depth() const { return CV_MAT_DEPTH(type_); }
00100     inline int channels() const { return CV_MAT_CN(type_); }
00101     inline int elemSize() const { return CV_ELEM_SIZE(type_); }
00102     inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }
00103 
00104     inline Usage usage() const { return usage_; }
00105 
00106     class Impl;
00107 private:
00108     int rows_;
00109     int cols_;
00110     int type_;
00111     Usage usage_;
00112 
00113     Ptr<Impl> impl_;
00114 };
00115 
00116 template <> CV_EXPORTS void Ptr<GlBuffer::Impl>::delete_obj();
00117 
00119 class CV_EXPORTS GlTexture
00120 {
00121 public:
00123     GlTexture();
00124 
00126     GlTexture(int rows, int cols, int type);
00127     GlTexture(Size size, int type);
00128 
00130     explicit GlTexture(InputArray mat, bool bgra = true);
00131 
00132     void create(int rows, int cols, int type);
00133     void create(Size size, int type);
00134     void release();
00135 
00137     void copyFrom(InputArray mat, bool bgra = true);
00138 
00139     void bind() const;
00140     void unbind() const;
00141 
00142     inline int rows() const { return rows_; }
00143     inline int cols() const { return cols_; }
00144     inline Size size() const { return Size(cols_, rows_); }
00145     inline bool empty() const { return rows_ == 0 || cols_ == 0; }
00146 
00147     inline int type() const { return type_; }
00148     inline int depth() const { return CV_MAT_DEPTH(type_); }
00149     inline int channels() const { return CV_MAT_CN(type_); }
00150     inline int elemSize() const { return CV_ELEM_SIZE(type_); }
00151     inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }
00152 
00153     class Impl;
00154 private:
00155     int rows_;
00156     int cols_;
00157     int type_;
00158 
00159     Ptr<Impl> impl_;
00160     GlBuffer buf_;
00161 };
00162 
00163 template <> CV_EXPORTS void Ptr<GlTexture::Impl>::delete_obj();
00164 
00166 class CV_EXPORTS GlArrays
00167 {
00168 public:
00169     inline GlArrays()
00170         : vertex_(GlBuffer::ARRAY_BUFFER), color_(GlBuffer::ARRAY_BUFFER), bgra_(true), normal_(GlBuffer::ARRAY_BUFFER), texCoord_(GlBuffer::ARRAY_BUFFER)
00171     {
00172     }
00173 
00174     void setVertexArray(InputArray vertex);
00175     inline void resetVertexArray() { vertex_.release(); }
00176 
00177     void setColorArray(InputArray color, bool bgra = true);
00178     inline void resetColorArray() { color_.release(); }
00179 
00180     void setNormalArray(InputArray normal);
00181     inline void resetNormalArray() { normal_.release(); }
00182 
00183     void setTexCoordArray(InputArray texCoord);
00184     inline void resetTexCoordArray() { texCoord_.release(); }
00185 
00186     void bind() const;
00187     void unbind() const;
00188 
00189     inline int rows() const { return vertex_.rows(); }
00190     inline int cols() const { return vertex_.cols(); }
00191     inline Size size() const { return vertex_.size(); }
00192     inline bool empty() const { return vertex_.empty(); }
00193 
00194 private:
00195     GlBuffer vertex_;
00196     GlBuffer color_;
00197     bool bgra_;
00198     GlBuffer normal_;
00199     GlBuffer texCoord_;
00200 };
00201 
00203 class CV_EXPORTS GlFont
00204 {
00205 public:
00206     enum Weight
00207     {
00208         WEIGHT_LIGHT    = 300,
00209         WEIGHT_NORMAL   = 400,
00210         WEIGHT_SEMIBOLD = 600,
00211         WEIGHT_BOLD     = 700,
00212         WEIGHT_BLACK    = 900
00213     };
00214 
00215     enum Style
00216     {
00217         STYLE_NORMAL    = 0,
00218         STYLE_ITALIC    = 1,
00219         STYLE_UNDERLINE = 2
00220     };
00221 
00222     static Ptr<GlFont> get(const std::string& family, int height = 12, Weight weight = WEIGHT_NORMAL, Style style = STYLE_NORMAL);
00223 
00224     void draw(const char* str, int len) const;
00225 
00226     inline const std::string& family() const { return family_; }
00227     inline int height() const { return height_; }
00228     inline Weight weight() const { return weight_; }
00229     inline Style style() const { return style_; }
00230 
00231 private:
00232     GlFont(const std::string& family, int height, Weight weight, Style style);
00233 
00234     std::string family_;
00235     int height_;
00236     Weight weight_;
00237     Style style_;
00238 
00239     unsigned int base_;
00240 
00241     GlFont(const GlFont&);
00242     GlFont& operator =(const GlFont&);
00243 };
00244 
00246 
00248 CV_EXPORTS void render(const GlTexture& tex,
00249     Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
00250     Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
00251 
00253 namespace RenderMode {
00254     enum {
00255         POINTS         = 0x0000,
00256         LINES          = 0x0001,
00257         LINE_LOOP      = 0x0002,
00258         LINE_STRIP     = 0x0003,
00259         TRIANGLES      = 0x0004,
00260         TRIANGLE_STRIP = 0x0005,
00261         TRIANGLE_FAN   = 0x0006,
00262         QUADS          = 0x0007,
00263         QUAD_STRIP     = 0x0008,
00264         POLYGON        = 0x0009
00265     };
00266 }
00267 
00269 CV_EXPORTS void render(const GlArrays& arr, int mode = RenderMode::POINTS, Scalar color = Scalar::all(255));
00270 
00271 CV_EXPORTS void render(const std::string& str, const Ptr<GlFont>& font, Scalar color, Point2d pos);
00272 
00274 class CV_EXPORTS GlCamera
00275 {
00276 public:
00277     GlCamera();
00278 
00279     void lookAt(Point3d eye, Point3d center, Point3d up);
00280     void setCameraPos(Point3d pos, double yaw, double pitch, double roll);
00281 
00282     void setScale(Point3d scale);
00283 
00284     void setProjectionMatrix(const Mat& projectionMatrix, bool transpose = true);
00285     void setPerspectiveProjection(double fov, double aspect, double zNear, double zFar);
00286     void setOrthoProjection(double left, double right, double bottom, double top, double zNear, double zFar);
00287 
00288     void setupProjectionMatrix() const;
00289     void setupModelViewMatrix() const;
00290 
00291 private:
00292     Point3d eye_;
00293     Point3d center_;
00294     Point3d up_;
00295 
00296     Point3d pos_;
00297     double yaw_;
00298     double pitch_;
00299     double roll_;
00300 
00301     bool useLookAtParams_;
00302 
00303     Point3d scale_;
00304 
00305     Mat projectionMatrix_;
00306 
00307     double fov_;
00308     double aspect_;
00309 
00310     double left_;
00311     double right_;
00312     double bottom_;
00313     double top_;
00314 
00315     double zNear_;
00316     double zFar_;
00317 
00318     bool perspectiveProjection_;
00319 };
00320 
00321 inline void GlBuffer::create(Size _size, int _type, Usage _usage) { create(_size.height, _size.width, _type, _usage); }
00322 inline void GlBuffer::create(int _rows, int _cols, int _type) { create(_rows, _cols, _type, usage()); }
00323 inline void GlBuffer::create(Size _size, int _type) { create(_size.height, _size.width, _type, usage()); }
00324 inline void GlTexture::create(Size _size, int _type) { create(_size.height, _size.width, _type); }
00325 
00326 namespace gpu
00327 {
00329     CV_EXPORTS void setGlDevice(int device = 0);
00330 }
00331 } // namespace cv
00332 
00333 #endif // __cplusplus
00334 
00335 #endif // __OPENCV_OPENGL_INTEROP_HPP__