refactored opengl functionality

* removed OpenGLFuncTab, now extensions are loaded internally
* renamed GlTexture -> GlTexture2D
* added support of GlBuffer and GlTexture2D to InputArray/OutputArray
* added ELEMENT_ARRAY_BUFFER and PIXEL_PACK_BUFFER targets
* added copyFrom/copyTo method for GlBuffer and GlTexture2D
* removed GlFont
* removed pointCloudShow
* removed OpenGLCleanCallback
This commit is contained in:
Vladislav Vinogradov 2012-11-30 17:35:28 +04:00
parent 2eebd8d939
commit 08fbf667f9
22 changed files with 6062 additions and 2980 deletions

View File

@ -91,7 +91,7 @@ class SparseMat;
typedef Mat MatND;
class GlBuffer;
class GlTexture;
class GlTexture2D;
class GlArrays;
class GlCamera;
@ -1311,7 +1311,7 @@ public:
STD_VECTOR_MAT = 5 << KIND_SHIFT,
EXPR = 6 << KIND_SHIFT,
OPENGL_BUFFER = 7 << KIND_SHIFT,
OPENGL_TEXTURE = 8 << KIND_SHIFT,
OPENGL_TEXTURE2D = 8 << KIND_SHIFT,
GPU_MAT = 9 << KIND_SHIFT
};
_InputArray();
@ -1328,13 +1328,13 @@ public:
_InputArray(const Scalar& s);
_InputArray(const double& val);
_InputArray(const GlBuffer& buf);
_InputArray(const GlTexture& tex);
_InputArray(const GlTexture2D& 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 GlTexture2D getGlTexture2D() const;
virtual gpu::GpuMat getGpuMat() const;
virtual int kind() const;
@ -1385,6 +1385,8 @@ public:
template<typename _Tp, int m, int n> _OutputArray(Matx<_Tp, m, n>& matx);
template<typename _Tp> _OutputArray(_Tp* vec, int n);
_OutputArray(gpu::GpuMat& d_mat);
_OutputArray(GlBuffer& buf);
_OutputArray(GlTexture2D& tex);
_OutputArray(const Mat& m);
template<typename _Tp> _OutputArray(const vector<_Tp>& vec);
@ -1395,12 +1397,16 @@ public:
template<typename _Tp, int m, int n> _OutputArray(const Matx<_Tp, m, n>& matx);
template<typename _Tp> _OutputArray(const _Tp* vec, int n);
_OutputArray(const gpu::GpuMat& d_mat);
_OutputArray(const GlBuffer& buf);
_OutputArray(const GlTexture2D& tex);
virtual bool fixedSize() const;
virtual bool fixedType() const;
virtual bool needed() const;
virtual Mat& getMatRef(int i=-1) const;
virtual gpu::GpuMat& getGpuMatRef() const;
virtual GlBuffer& getGlBufferRef() const;
virtual GlTexture2D& getGlTexture2DRef() const;
virtual void create(Size sz, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
virtual void create(int rows, int cols, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
virtual void create(int dims, const int* size, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;

View File

@ -750,39 +750,4 @@ typedef struct CvBigFuncTable
(tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG; \
(tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG
#ifdef __cplusplus
//! 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 //__cplusplus
#endif // __OPENCV_CORE_INTERNAL_HPP__

View File

@ -47,40 +47,68 @@
#include "opencv2/core/core.hpp"
namespace cv
{
namespace cv {
CV_EXPORTS bool checkGlError(const char* file, const int line, const char* func = "");
#if defined(__GNUC__)
#define CV_CheckGlError() CV_DbgAssert( (cv::checkGlError(__FILE__, __LINE__, __func__)) )
#else
#define CV_CheckGlError() CV_DbgAssert( (cv::checkGlError(__FILE__, __LINE__)) )
#endif
/////////////////// OpenGL Objects ///////////////////
//! Smart pointer for OpenGL buffer memory with reference counting.
class CV_EXPORTS GlBuffer
{
public:
enum Usage
enum Target
{
ARRAY_BUFFER = 0x8892, // buffer will use for OpenGL arrays (vertices, colors, normals, etc)
TEXTURE_BUFFER = 0x88EC // buffer will ise for OpenGL textures
ARRAY_BUFFER = 0x8892, //!< The buffer will be used as a source for vertex data
ELEMENT_ARRAY_BUFFER = 0x8893, //!< The buffer will be used for indices (in glDrawElements, for example)
PIXEL_PACK_BUFFER = 0x88EB, //!< The buffer will be used for reading from OpenGL textures
PIXEL_UNPACK_BUFFER = 0x88EC //!< The buffer will be used for writing to OpenGL textures
};
//! create empty buffer
explicit GlBuffer(Usage usage);
GlBuffer();
//! create buffer from existed buffer id
GlBuffer(int arows, int acols, int atype, unsigned int abufId, bool autoRelease = false);
GlBuffer(Size asize, int atype, unsigned int abufId, bool autoRelease = false);
//! create buffer
GlBuffer(int rows, int cols, int type, Usage usage);
GlBuffer(Size size, int type, Usage usage);
GlBuffer(int arows, int acols, int atype, Target target = ARRAY_BUFFER);
GlBuffer(Size asize, int atype, Target target = ARRAY_BUFFER);
//! copy from host/device memory
GlBuffer(InputArray mat, Usage usage);
explicit GlBuffer(InputArray arr, Target target = ARRAY_BUFFER);
void create(int rows, int cols, int type, Usage usage);
void create(Size size, int type, Usage usage);
void create(int rows, int cols, int type);
void create(Size size, int type);
//! create buffer
void create(int arows, int acols, int atype, Target target = ARRAY_BUFFER);
void create(Size asize, int atype, Target target = ARRAY_BUFFER) { create(asize.height, asize.width, atype, target); }
//! release memory and delete buffer object
void release();
//! copy from host/device memory
void copyFrom(InputArray mat);
//! set auto release mode (if true, release will be called in object's destructor)
void setAutoRelease(bool flag);
void bind() const;
void unbind() const;
//! copy from host/device memory
void copyFrom(InputArray arr, Target target = ARRAY_BUFFER);
//! copy to host/device memory
void copyTo(OutputArray arr, Target target = ARRAY_BUFFER) const;
//! create copy of current buffer
GlBuffer clone(Target target = ARRAY_BUFFER) const;
//! bind buffer for specified target
void bind(Target target) const;
//! unbind any buffers from specified target
static void unbind(Target target);
//! map to host memory
Mat mapHost();
@ -90,162 +118,134 @@ public:
gpu::GpuMat mapDevice();
void unmapDevice();
inline int rows() const { return rows_; }
inline int cols() const { return cols_; }
inline Size size() const { return Size(cols_, rows_); }
inline bool empty() const { return rows_ == 0 || cols_ == 0; }
int rows() const { return rows_; }
int cols() const { return cols_; }
Size size() const { return Size(cols_, rows_); }
bool empty() const { return rows_ == 0 || cols_ == 0; }
inline int type() const { return type_; }
inline int depth() const { return CV_MAT_DEPTH(type_); }
inline int channels() const { return CV_MAT_CN(type_); }
inline int elemSize() const { return CV_ELEM_SIZE(type_); }
inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }
int type() const { return type_; }
int depth() const { return CV_MAT_DEPTH(type_); }
int channels() const { return CV_MAT_CN(type_); }
int elemSize() const { return CV_ELEM_SIZE(type_); }
int elemSize1() const { return CV_ELEM_SIZE1(type_); }
inline Usage usage() const { return usage_; }
unsigned int bufId() const;
class Impl;
private:
Ptr<Impl> impl_;
int rows_;
int cols_;
int type_;
Usage usage_;
Ptr<Impl> impl_;
};
template <> CV_EXPORTS void Ptr<GlBuffer::Impl>::delete_obj();
//! Smart pointer for OpenGL 2d texture memory with reference counting.
class CV_EXPORTS GlTexture
//! Smart pointer for OpenGL 2D texture memory with reference counting.
class CV_EXPORTS GlTexture2D
{
public:
enum Format
{
NONE = 0,
DEPTH_COMPONENT = 0x1902, //!< Depth
RGB = 0x1907, //!< Red, Green, Blue
RGBA = 0x1908 //!< Red, Green, Blue, Alpha
};
//! create empty texture
GlTexture();
GlTexture2D();
//! create texture from existed texture id
GlTexture2D(int arows, int acols, Format aformat, unsigned int atexId, bool autoRelease = false);
GlTexture2D(Size asize, Format aformat, unsigned int atexId, bool autoRelease = false);
//! create texture
GlTexture(int rows, int cols, int type);
GlTexture(Size size, int type);
GlTexture2D(int arows, int acols, Format aformat);
GlTexture2D(Size asize, Format aformat);
//! copy from host/device memory
explicit GlTexture(InputArray mat, bool bgra = true);
explicit GlTexture2D(InputArray arr);
void create(int rows, int cols, int type);
void create(Size size, int type);
//! create texture
void create(int arows, int acols, Format aformat);
void create(Size asize, Format aformat) { create(asize.height, asize.width, aformat); }
//! release memory and delete texture object
void release();
//! set auto release mode (if true, release will be called in object's destructor)
void setAutoRelease(bool flag);
//! copy from host/device memory
void copyFrom(InputArray mat, bool bgra = true);
void copyFrom(InputArray arr);
//! copy to host/device memory
void copyTo(OutputArray arr, int ddepth = CV_32F) const;
//! bind texture to current active texture unit for GL_TEXTURE_2D target
void bind() const;
void unbind() const;
inline int rows() const { return rows_; }
inline int cols() const { return cols_; }
inline Size size() const { return Size(cols_, rows_); }
inline bool empty() const { return rows_ == 0 || cols_ == 0; }
int rows() const { return rows_; }
int cols() const { return cols_; }
Size size() const { return Size(cols_, rows_); }
bool empty() const { return rows_ == 0 || cols_ == 0; }
inline int type() const { return type_; }
inline int depth() const { return CV_MAT_DEPTH(type_); }
inline int channels() const { return CV_MAT_CN(type_); }
inline int elemSize() const { return CV_ELEM_SIZE(type_); }
inline int elemSize1() const { return CV_ELEM_SIZE1(type_); }
Format format() const { return format_; }
unsigned int texId() const;
class Impl;
private:
Ptr<Impl> impl_;
int rows_;
int cols_;
int type_;
Ptr<Impl> impl_;
GlBuffer buf_;
Format format_;
};
template <> CV_EXPORTS void Ptr<GlTexture::Impl>::delete_obj();
template <> CV_EXPORTS void Ptr<GlTexture2D::Impl>::delete_obj();
//! OpenGL Arrays
class CV_EXPORTS GlArrays
{
public:
inline GlArrays()
: vertex_(GlBuffer::ARRAY_BUFFER), color_(GlBuffer::ARRAY_BUFFER), bgra_(true), normal_(GlBuffer::ARRAY_BUFFER), texCoord_(GlBuffer::ARRAY_BUFFER)
{
}
GlArrays();
void setVertexArray(InputArray vertex);
inline void resetVertexArray() { vertex_.release(); }
void resetVertexArray();
void setColorArray(InputArray color, bool bgra = true);
inline void resetColorArray() { color_.release(); }
void setColorArray(InputArray color);
void resetColorArray();
void setNormalArray(InputArray normal);
inline void resetNormalArray() { normal_.release(); }
void resetNormalArray();
void setTexCoordArray(InputArray texCoord);
inline void resetTexCoordArray() { texCoord_.release(); }
void resetTexCoordArray();
void release();
void setAutoRelease(bool flag);
void bind() const;
void unbind() const;
inline int rows() const { return vertex_.rows(); }
inline int cols() const { return vertex_.cols(); }
inline Size size() const { return vertex_.size(); }
inline bool empty() const { return vertex_.empty(); }
int size() const { return size_; }
bool empty() const { return size_ == 0; }
private:
int size_;
GlBuffer vertex_;
GlBuffer color_;
bool bgra_;
GlBuffer normal_;
GlBuffer texCoord_;
};
//! OpenGL Font
class CV_EXPORTS GlFont
{
public:
enum Weight
{
WEIGHT_LIGHT = 300,
WEIGHT_NORMAL = 400,
WEIGHT_SEMIBOLD = 600,
WEIGHT_BOLD = 700,
WEIGHT_BLACK = 900
};
enum Style
{
STYLE_NORMAL = 0,
STYLE_ITALIC = 1,
STYLE_UNDERLINE = 2
};
static Ptr<GlFont> get(const std::string& family, int height = 12, Weight weight = WEIGHT_NORMAL, Style style = STYLE_NORMAL);
void draw(const char* str, size_t len) const;
inline const std::string& family() const { return family_; }
inline int height() const { return height_; }
inline Weight weight() const { return weight_; }
inline Style style() const { return style_; }
private:
GlFont(const std::string& family, int height, Weight weight, Style style);
std::string family_;
int height_;
Weight weight_;
Style style_;
unsigned int base_;
GlFont(const GlFont&);
GlFont& operator =(const GlFont&);
};
//! render functions
/////////////////// Render Functions ///////////////////
//! render texture rectangle in window
CV_EXPORTS void render(const GlTexture& tex,
CV_EXPORTS void render(const GlTexture2D& tex,
Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
@ -267,67 +267,13 @@ namespace RenderMode {
//! render OpenGL arrays
CV_EXPORTS void render(const GlArrays& arr, int mode = RenderMode::POINTS, Scalar color = Scalar::all(255));
CV_EXPORTS void render(const GlArrays& arr, InputArray indices, int mode = RenderMode::POINTS, Scalar color = Scalar::all(255));
CV_EXPORTS void render(const std::string& str, const Ptr<GlFont>& font, Scalar color, Point2d pos);
//! OpenGL camera
class CV_EXPORTS GlCamera
{
public:
GlCamera();
void lookAt(Point3d eye, Point3d center, Point3d up);
void setCameraPos(Point3d pos, double yaw, double pitch, double roll);
void setScale(Point3d scale);
void setProjectionMatrix(const Mat& projectionMatrix, bool transpose = true);
void setPerspectiveProjection(double fov, double aspect, double zNear, double zFar);
void setOrthoProjection(double left, double right, double bottom, double top, double zNear, double zFar);
void setupProjectionMatrix() const;
void setupModelViewMatrix() const;
private:
Point3d eye_;
Point3d center_;
Point3d up_;
Point3d pos_;
double yaw_;
double pitch_;
double roll_;
bool useLookAtParams_;
Point3d scale_;
Mat projectionMatrix_;
double fov_;
double aspect_;
double left_;
double right_;
double bottom_;
double top_;
double zNear_;
double zFar_;
bool perspectiveProjection_;
};
inline void GlBuffer::create(Size _size, int _type, Usage _usage) { create(_size.height, _size.width, _type, _usage); }
inline void GlBuffer::create(int _rows, int _cols, int _type) { create(_rows, _cols, _type, usage()); }
inline void GlBuffer::create(Size _size, int _type) { create(_size.height, _size.width, _type, usage()); }
inline void GlTexture::create(Size _size, int _type) { create(_size.height, _size.width, _type); }
namespace gpu
{
namespace gpu {
//! set a CUDA device to use OpenGL interoperability
CV_EXPORTS void setGlDevice(int device = 0);
}
} // namespace cv
#endif // __cplusplus

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -922,8 +922,8 @@ _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(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F), obj((void*)&val), sz(Size(1,1)) {}
_InputArray::_InputArray(const MatExpr& expr) : flags(FIXED_TYPE + FIXED_SIZE + EXPR), obj((void*)&expr) {}
_InputArray::_InputArray(const GlBuffer& buf) : flags(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER), obj((void*)&buf) {}
_InputArray::_InputArray(const GlTexture& tex) : flags(FIXED_TYPE + FIXED_SIZE + OPENGL_TEXTURE), obj((void*)&tex) {}
_InputArray::_InputArray(const GlBuffer& buf) : flags(OPENGL_BUFFER), obj((void*)&buf) {}
_InputArray::_InputArray(const GlTexture2D &tex) : flags(OPENGL_TEXTURE2D), obj((void*)&tex) {}
_InputArray::_InputArray(const gpu::GpuMat& d_mat) : flags(GPU_MAT), obj((void*)&d_mat) {}
Mat _InputArray::getMat(int i) const
@ -1076,14 +1076,14 @@ GlBuffer _InputArray::getGlBuffer() const
}
}
GlTexture _InputArray::getGlTexture() const
GlTexture2D _InputArray::getGlTexture2D() const
{
int k = kind();
CV_Assert(k == OPENGL_TEXTURE);
CV_Assert(k == OPENGL_TEXTURE2D);
//if( k == OPENGL_TEXTURE )
{
const GlTexture* tex = (const GlTexture*)obj;
const GlTexture2D* tex = (const GlTexture2D*)obj;
return *tex;
}
}
@ -1168,10 +1168,10 @@ Size _InputArray::size(int i) const
return buf->size();
}
if( k == OPENGL_TEXTURE )
if( k == OPENGL_TEXTURE2D )
{
CV_Assert( i < 0 );
const GlTexture* tex = (const GlTexture*)obj;
const GlTexture2D* tex = (const GlTexture2D*)obj;
return tex->size();
}
@ -1216,9 +1216,6 @@ int _InputArray::type(int i) const
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();
@ -1271,8 +1268,8 @@ bool _InputArray::empty() const
if( k == OPENGL_BUFFER )
return ((const GlBuffer*)obj)->empty();
if( k == OPENGL_TEXTURE )
return ((const GlTexture*)obj)->empty();
if( k == OPENGL_TEXTURE2D )
return ((const GlTexture2D*)obj)->empty();
CV_Assert( k == GPU_MAT );
//if( k == GPU_MAT )
@ -1285,10 +1282,14 @@ _OutputArray::~_OutputArray() {}
_OutputArray::_OutputArray(Mat& m) : _InputArray(m) {}
_OutputArray::_OutputArray(vector<Mat>& vec) : _InputArray(vec) {}
_OutputArray::_OutputArray(gpu::GpuMat& d_mat) : _InputArray(d_mat) {}
_OutputArray::_OutputArray(GlBuffer& buf) : _InputArray(buf) {}
_OutputArray::_OutputArray(GlTexture2D& tex) : _InputArray(tex) {}
_OutputArray::_OutputArray(const Mat& m) : _InputArray(m) {flags |= FIXED_SIZE|FIXED_TYPE;}
_OutputArray::_OutputArray(const vector<Mat>& vec) : _InputArray(vec) {flags |= FIXED_SIZE;}
_OutputArray::_OutputArray(const gpu::GpuMat& d_mat) : _InputArray(d_mat) {flags |= FIXED_SIZE|FIXED_TYPE;}
_OutputArray::_OutputArray(const GlBuffer& buf) : _InputArray(buf) {flags |= FIXED_SIZE|FIXED_TYPE;}
_OutputArray::_OutputArray(const GlTexture2D& tex) : _InputArray(tex) {flags |= FIXED_SIZE|FIXED_TYPE;}
bool _OutputArray::fixedSize() const
@ -1318,6 +1319,13 @@ void _OutputArray::create(Size _sz, int mtype, int i, bool allowTransposed, int
((gpu::GpuMat*)obj)->create(_sz, mtype);
return;
}
if( k == OPENGL_BUFFER && i < 0 && !allowTransposed && fixedDepthMask == 0 )
{
CV_Assert(!fixedSize() || ((GlBuffer*)obj)->size() == _sz);
CV_Assert(!fixedType() || ((GlBuffer*)obj)->type() == mtype);
((GlBuffer*)obj)->create(_sz, mtype);
return;
}
int sizes[] = {_sz.height, _sz.width};
create(2, sizes, mtype, i, allowTransposed, fixedDepthMask);
}
@ -1339,6 +1347,13 @@ void _OutputArray::create(int rows, int cols, int mtype, int i, bool allowTransp
((gpu::GpuMat*)obj)->create(rows, cols, mtype);
return;
}
if( k == OPENGL_BUFFER && i < 0 && !allowTransposed && fixedDepthMask == 0 )
{
CV_Assert(!fixedSize() || ((GlBuffer*)obj)->size() == Size(cols, rows));
CV_Assert(!fixedType() || ((GlBuffer*)obj)->type() == mtype);
((GlBuffer*)obj)->create(rows, cols, mtype);
return;
}
int sizes[] = {rows, cols};
create(2, sizes, mtype, i, allowTransposed, fixedDepthMask);
}
@ -1558,6 +1573,18 @@ void _OutputArray::release() const
return;
}
if( k == OPENGL_BUFFER )
{
((GlBuffer*)obj)->release();
return;
}
if( k == OPENGL_TEXTURE2D )
{
((GlTexture2D*)obj)->release();
return;
}
if( k == NONE )
return;
@ -1623,6 +1650,20 @@ gpu::GpuMat& _OutputArray::getGpuMatRef() const
return *(gpu::GpuMat*)obj;
}
GlBuffer& _OutputArray::getGlBufferRef() const
{
int k = kind();
CV_Assert( k == OPENGL_BUFFER );
return *(GlBuffer*)obj;
}
GlTexture2D& _OutputArray::getGlTexture2DRef() const
{
int k = kind();
CV_Assert( k == OPENGL_TEXTURE2D );
return *(GlTexture2D*)obj;
}
static _OutputArray _none;
OutputArray noArray() { return _none; }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,625 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "test_precomp.hpp"
#if defined(HAVE_CUDA) && defined(HAVE_OPENGL)
/////////////////////////////////////////////
// GlBuffer
PARAM_TEST_CASE(GlBuffer, cv::Size, MatType)
{
cv::Size size;
int type;
virtual void SetUp()
{
size = GET_PARAM(0);
type = GET_PARAM(1);
}
};
TEST_P(GlBuffer, Constructor1)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlBuffer buf(size.height, size.width, type);
EXPECT_EQ(size.height, buf.rows());
EXPECT_EQ(size.width, buf.cols());
EXPECT_EQ(type, buf.type());
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, Constructor2)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlBuffer buf(size, type);
EXPECT_EQ(size.height, buf.rows());
EXPECT_EQ(size.width, buf.cols());
EXPECT_EQ(type, buf.type());
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, ConstructorFromMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf(gold);
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, ConstructorFromGpuMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::gpu::GpuMat d_gold(gold);
cv::GlBuffer buf(d_gold);
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
buf.release();
d_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, ConstructorFromGlBuffer)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlBuffer buf_gold(size, type);
cv::GlBuffer buf(buf_gold);
EXPECT_EQ(buf_gold.bufId(), buf.bufId());
EXPECT_EQ(buf_gold.rows(), buf.rows());
EXPECT_EQ(buf_gold.cols(), buf.cols());
EXPECT_EQ(buf_gold.type(), buf.type());
buf.release();
buf_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, ConstructorFromGlTexture2D)
{
const int depth = CV_MAT_DEPTH(type);
const int cn = CV_MAT_CN(type);
if (depth != CV_32F || cn == 2)
return;
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, 1.0);
cv::GlTexture2D tex_gold(gold);
cv::GlBuffer buf(tex_gold);
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 1e-2);
buf.release();
tex_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, Create)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlBuffer buf;
buf.create(size.height, size.width, type);
EXPECT_EQ(size.height, buf.rows());
EXPECT_EQ(size.width, buf.cols());
EXPECT_EQ(type, buf.type());
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyFromMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf;
buf.copyFrom(gold);
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyFromGpuMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::gpu::GpuMat d_gold(gold);
cv::GlBuffer buf;
buf.copyFrom(d_gold);
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
buf.release();
d_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyFromGlBuffer)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf_gold(gold);
cv::GlBuffer buf;
buf.copyFrom(buf_gold);
EXPECT_NE(buf_gold.bufId(), buf.bufId());
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
buf.release();
buf_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyFromGlTexture2D)
{
const int depth = CV_MAT_DEPTH(type);
const int cn = CV_MAT_CN(type);
if (depth != CV_32F || cn == 2)
return;
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, 1.0);
cv::GlTexture2D tex_gold(gold);
cv::GlBuffer buf;
buf.copyFrom(tex_gold);
cv::Mat bufData;
buf.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 1e-2);
buf.release();
tex_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyToGpuMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf(gold);
cv::gpu::GpuMat dst;
buf.copyTo(dst);
EXPECT_MAT_NEAR(gold, dst, 0);
dst.release();
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyToGlBuffer)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf(gold);
cv::GlBuffer dst;
buf.copyTo(dst);
EXPECT_NE(buf.bufId(), dst.bufId());
cv::Mat bufData;
dst.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
dst.release();
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, CopyToGlTexture2D)
{
const int depth = CV_MAT_DEPTH(type);
const int cn = CV_MAT_CN(type);
if (depth != CV_32F || cn == 2)
return;
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, 1.0);
cv::GlBuffer buf(gold);
cv::GlTexture2D tex;
buf.copyTo(tex);
cv::Mat texData;
tex.copyTo(texData);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, Clone)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf(gold);
cv::GlBuffer dst = buf.clone();
EXPECT_NE(buf.bufId(), dst.bufId());
cv::Mat bufData;
dst.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 0);
dst.release();
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, MapHost)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf(gold);
cv::Mat dst = buf.mapHost();
EXPECT_MAT_NEAR(gold, dst, 0);
buf.unmapHost();
buf.release();
cv::destroyAllWindows();
}
TEST_P(GlBuffer, MapDevice)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type);
cv::GlBuffer buf(gold);
cv::gpu::GpuMat dst = buf.mapDevice();
EXPECT_MAT_NEAR(gold, dst, 0);
buf.unmapDevice();
buf.release();
cv::destroyAllWindows();
}
INSTANTIATE_TEST_CASE_P(OpenGL, GlBuffer, testing::Combine(DIFFERENT_SIZES, ALL_TYPES));
/////////////////////////////////////////////
// GlTexture2D
PARAM_TEST_CASE(GlTexture2D, cv::Size, MatType)
{
cv::Size size;
int type;
int depth;
int cn;
cv::GlTexture2D::Format format;
virtual void SetUp()
{
size = GET_PARAM(0);
type = GET_PARAM(1);
depth = CV_MAT_DEPTH(type);
cn = CV_MAT_CN(type);
format = cn == 1 ? cv::GlTexture2D::DEPTH_COMPONENT : cn == 3 ? cv::GlTexture2D::RGB : cn == 4 ? cv::GlTexture2D::RGBA : cv::GlTexture2D::NONE;
}
};
TEST_P(GlTexture2D, Constructor1)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlTexture2D tex(size.height, size.width, format);
EXPECT_EQ(size.height, tex.rows());
EXPECT_EQ(size.width, tex.cols());
EXPECT_EQ(format, tex.format());
tex.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, Constructor2)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlTexture2D tex(size, format);
EXPECT_EQ(size.height, tex.rows());
EXPECT_EQ(size.width, tex.cols());
EXPECT_EQ(format, tex.format());
tex.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, ConstructorFromMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::GlTexture2D tex(gold);
cv::Mat texData;
tex.copyTo(texData, depth);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, ConstructorFromGpuMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::gpu::GpuMat d_gold(gold);
cv::GlTexture2D tex(d_gold);
cv::Mat texData;
tex.copyTo(texData, depth);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
d_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, ConstructorFromGlBuffer)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::GlBuffer buf_gold(gold);
cv::GlTexture2D tex(buf_gold);
cv::Mat texData;
tex.copyTo(texData, depth);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
buf_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, ConstructorFromGlTexture2D)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlTexture2D tex_gold(size, format);
cv::GlTexture2D tex(tex_gold);
EXPECT_EQ(tex_gold.texId(), tex.texId());
EXPECT_EQ(tex_gold.rows(), tex.rows());
EXPECT_EQ(tex_gold.cols(), tex.cols());
EXPECT_EQ(tex_gold.format(), tex.format());
tex.release();
tex_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, Create)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::GlTexture2D tex;
tex.create(size.height, size.width, format);
EXPECT_EQ(size.height, tex.rows());
EXPECT_EQ(size.width, tex.cols());
EXPECT_EQ(format, tex.format());
tex.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, CopyFromMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::GlTexture2D tex;
tex.copyFrom(gold);
cv::Mat texData;
tex.copyTo(texData, depth);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, CopyFromGpuMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::gpu::GpuMat d_gold(gold);
cv::GlTexture2D tex;
tex.copyFrom(d_gold);
cv::Mat texData;
tex.copyTo(texData, depth);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
d_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, CopyFromGlBuffer)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::GlBuffer buf_gold(gold);
cv::GlTexture2D tex;
tex.copyFrom(buf_gold);
cv::Mat texData;
tex.copyTo(texData, depth);
EXPECT_MAT_NEAR(gold, texData, 1e-2);
tex.release();
buf_gold.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, CopyToGpuMat)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::GlTexture2D tex(gold);
cv::gpu::GpuMat dst;
tex.copyTo(dst, depth);
EXPECT_MAT_NEAR(gold, dst, 1e-2);
dst.release();
tex.release();
cv::destroyAllWindows();
}
TEST_P(GlTexture2D, CopyToGlBuffer)
{
cv::namedWindow("test", cv::WINDOW_OPENGL);
cv::Mat gold = randomMat(size, type, 0, depth == CV_8U ? 255 : 1);
cv::GlTexture2D tex(gold);
cv::GlBuffer dst;
tex.copyTo(dst, depth);
cv::Mat bufData;
dst.copyTo(bufData);
EXPECT_MAT_NEAR(gold, bufData, 1e-2);
dst.release();
tex.release();
cv::destroyAllWindows();
}
INSTANTIATE_TEST_CASE_P(OpenGL, GlTexture2D, testing::Combine(DIFFERENT_SIZES, testing::Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4)));
#endif

View File

@ -69,6 +69,7 @@
#include <cuda_runtime.h>
#include "opencv2/core/core.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"

View File

@ -125,16 +125,13 @@ CV_EXPORTS_W void setTrackbarPos(const string& trackbarname, const string& winna
// OpenGL support
typedef void (*OpenGlDrawCallback)(void* userdata);
typedef void (CV_CDECL *OpenGlDrawCallback)(void* userdata);
CV_EXPORTS void setOpenGlDrawCallback(const string& winname, OpenGlDrawCallback onOpenGlDraw, void* userdata = 0);
CV_EXPORTS void setOpenGlContext(const string& winname);
CV_EXPORTS void updateWindow(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
CV_EXPORTS CvFont fontQt(const string& nameFont, int pointSize=-1,

View File

@ -206,9 +206,6 @@ void cvSetRatioWindow_QT(const char* name,double prop_value);
double cvGetOpenGlProp_QT(const char* name);
#endif
// OpenGL
typedef void (CV_CDECL *CvOpenGlCleanCallback)(void* userdata);
void icvSetOpenGlCleanCallback(const char* window_name, CvOpenGlCleanCallback callback, void* userdata);
/*namespace cv

View File

@ -40,6 +40,7 @@
//M*/
#include "precomp.hpp"
#include <map>
#include "opencv2/core/opengl_interop.hpp"
// in later times, use this file as a dispatcher to implementations like cvcap.cpp
@ -240,94 +241,15 @@ void cv::updateWindow(const string& windowName)
#ifdef HAVE_OPENGL
namespace
{
const int CV_TEXTURE_MAGIC_VAL = 0x00287653;
const int CV_POINT_CLOUD_MAGIC_VAL = 0x00287654;
struct GlObjBase
{
int flag;
GlObjBase* next;
GlObjBase* prev;
std::string winname;
virtual ~GlObjBase() {}
};
GlObjBase* g_glObjs = 0;
GlObjBase* findGlObjByName(const std::string& winname)
{
GlObjBase* obj = g_glObjs;
while(obj && obj->winname != winname)
obj = obj->next;
return obj;
}
void addGlObj(GlObjBase* glObj)
{
glObj->next = g_glObjs;
glObj->prev = 0;
if (g_glObjs)
g_glObjs->prev = glObj;
g_glObjs = glObj;
}
void removeGlObj(GlObjBase* glObj)
{
if (glObj->prev)
glObj->prev->next = glObj->next;
else
g_glObjs = glObj->next;
if (glObj->next)
glObj->next->prev = glObj->prev;
delete glObj;
}
struct GlObjTex : GlObjBase
{
cv::GlTexture tex;
};
std::map<std::string, cv::GlTexture2D> wndTexs;
std::map<std::string, cv::GlTexture2D> ownWndTexs;
std::map<std::string, cv::GlBuffer> ownWndBufs;
void CV_CDECL glDrawTextureCallback(void* userdata)
{
GlObjTex* texObj = static_cast<GlObjTex*>(userdata);
cv::GlTexture2D* texObj = static_cast<cv::GlTexture2D*>(userdata);
CV_DbgAssert(texObj->flag == CV_TEXTURE_MAGIC_VAL);
static cv::GlCamera glCamera;
glCamera.setupProjectionMatrix();
cv::render(texObj->tex);
}
struct GlObjPointCloud : GlObjBase
{
cv::GlArrays arr;
cv::GlCamera camera;
};
void CV_CDECL glDrawPointCloudCallback(void* userdata)
{
GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(userdata);
CV_DbgAssert(pointCloudObj->flag == CV_POINT_CLOUD_MAGIC_VAL);
pointCloudObj->camera.setupProjectionMatrix();
pointCloudObj->camera.setupModelViewMatrix();
cv::render(pointCloudObj->arr);
}
void CV_CDECL glCleanCallback(void* userdata)
{
GlObjBase* glObj = static_cast<GlObjBase*>(userdata);
removeGlObj(glObj);
cv::render(*texObj);
}
}
#endif // HAVE_OPENGL
@ -339,7 +261,8 @@ void cv::imshow( const string& winname, InputArray _img )
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
#else
double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
const double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
if (useGl <= 0)
{
Mat img = _img.getMat();
@ -348,7 +271,7 @@ void cv::imshow( const string& winname, InputArray _img )
}
else
{
double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
if (autoSize > 0)
{
@ -358,145 +281,44 @@ void cv::imshow( const string& winname, InputArray _img )
setOpenGlContext(winname);
GlObjBase* glObj = findGlObjByName(winname);
if (glObj && glObj->flag != CV_TEXTURE_MAGIC_VAL)
if (_img.kind() == _InputArray::OPENGL_TEXTURE2D)
{
icvSetOpenGlCleanCallback(winname.c_str(), 0, 0);
glObj = 0;
}
cv::GlTexture2D& tex = wndTexs[winname];
if (glObj)
{
GlObjTex* texObj = static_cast<GlObjTex*>(glObj);
texObj->tex.copyFrom(_img);
tex = _img.getGlTexture2D();
tex.setAutoRelease(false);
setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
}
else
{
GlObjTex* texObj = new GlObjTex;
texObj->tex.copyFrom(_img);
cv::GlTexture2D& tex = ownWndTexs[winname];
glObj = texObj;
glObj->flag = CV_TEXTURE_MAGIC_VAL;
glObj->winname = winname;
if (_img.kind() == _InputArray::GPU_MAT)
{
cv::GlBuffer& buf = ownWndBufs[winname];
buf.copyFrom(_img);
buf.setAutoRelease(false);
addGlObj(glObj);
tex.copyFrom(buf);
tex.setAutoRelease(false);
}
else
{
tex.copyFrom(_img);
}
icvSetOpenGlCleanCallback(winname.c_str(), glCleanCallback, glObj);
tex.setAutoRelease(false);
setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex);
}
setOpenGlDrawCallback(winname, glDrawTextureCallback, glObj);
updateWindow(winname);
}
#endif
}
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");
(void)winname;
(void)camera;
(void)arr;
#else
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 = arr;
pointCloudObj->camera = camera;
}
else
{
GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
pointCloudObj->arr = arr;
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
}
void cv::pointCloudShow(const std::string& winname, const cv::GlCamera& camera, InputArray points, InputArray colors)
{
#ifndef HAVE_OPENGL
(void)winname;
(void)camera;
(void)points;
(void)colors;
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
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
}
// Without OpenGL
#ifndef HAVE_OPENGL
@ -516,11 +338,6 @@ CV_IMPL void cvUpdateWindow(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");
}
#endif // !HAVE_OPENGL
#if defined (HAVE_QT)

View File

@ -685,20 +685,6 @@ CV_IMPL void cvSetOpenGlDrawCallback(const char* window_name, CvOpenGlDrawCallba
}
void icvSetOpenGlCleanCallback(const char* window_name, CvOpenGlCleanCallback callback, void* userdata)
{
if (!guiMainThread)
CV_Error( CV_StsNullPtr, "NULL guiReceiver (please create a window)" );
QMetaObject::invokeMethod(guiMainThread,
"setOpenGlCleanCallback",
Qt::AutoConnection,
Q_ARG(QString, QString(window_name)),
Q_ARG(void*, (void*)callback),
Q_ARG(void*, userdata));
}
CV_IMPL void cvSetOpenGlContext(const char* window_name)
{
if (!guiMainThread)
@ -1175,14 +1161,6 @@ void GuiReceiver::setOpenGlDrawCallback(QString name, void* callback, void* user
w->setOpenGlDrawCallback((CvOpenGlDrawCallback) callback, userdata);
}
void GuiReceiver::setOpenGlCleanCallback(QString name, void* callback, void* userdata)
{
QPointer<CvWindow> w = icvFindWindowByName(name);
if (w)
w->setOpenGlCleanCallback((CvOpenGlCleanCallback) callback, userdata);
}
void GuiReceiver::setOpenGlContext(QString name)
{
QPointer<CvWindow> w = icvFindWindowByName(name);
@ -1828,12 +1806,6 @@ void CvWindow::setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userda
}
void CvWindow::setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata)
{
myView->setOpenGlCleanCallback(callback, userdata);
}
void CvWindow::makeCurrentOpenGlContext()
{
myView->makeCurrentOpenGlContext();
@ -2420,12 +2392,6 @@ void DefaultViewPort::setOpenGlDrawCallback(CvOpenGlDrawCallback /*callback*/, v
}
void DefaultViewPort::setOpenGlCleanCallback(CvOpenGlCleanCallback /*callback*/, void* /*userdata*/)
{
CV_Error(CV_OpenGlNotSupported, "Window doesn't support OpenGL");
}
void DefaultViewPort::makeCurrentOpenGlContext()
{
CV_Error(CV_OpenGlNotSupported, "Window doesn't support OpenGL");
@ -3074,19 +3040,10 @@ OpenGlViewPort::OpenGlViewPort(QWidget* _parent) : QGLWidget(_parent), size(-1,
glDrawCallback = 0;
glDrawData = 0;
glCleanCallback = 0;
glCleanData = 0;
glFuncTab = 0;
}
OpenGlViewPort::~OpenGlViewPort()
{
if (glFuncTab)
delete glFuncTab;
setOpenGlCleanCallback(0, 0);
}
QWidget* OpenGlViewPort::getWidget()
@ -3131,21 +3088,9 @@ void OpenGlViewPort::setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void*
glDrawData = userdata;
}
void OpenGlViewPort::setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata)
{
makeCurrentOpenGlContext();
if (glCleanCallback)
glCleanCallback(glCleanData);
glCleanCallback = callback;
glCleanData = userdata;
}
void OpenGlViewPort::makeCurrentOpenGlContext()
{
makeCurrent();
icvSetOpenGlFuncTab(glFuncTab);
}
void OpenGlViewPort::updateGl()
@ -3153,255 +3098,9 @@ void OpenGlViewPort::updateGl()
QGLWidget::updateGL();
}
#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GL_VERSION_1_5
/* GL types for handling large vertex buffer objects */
typedef ptrdiff_t GLintptr;
typedef ptrdiff_t GLsizeiptr;
#endif
typedef void (APIENTRYP PFNGLGENBUFFERSPROC ) (GLsizei n, GLuint *buffers);
typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRYP PFNGLBUFFERDATAPROC ) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
typedef void (APIENTRYP PFNGLBUFFERSUBDATAPROC) (GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data);
typedef void (APIENTRYP PFNGLBINDBUFFERPROC ) (GLenum target, GLuint buffer);
typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERPROC) (GLenum target, GLenum access);
typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERPROC) (GLenum target);
class GlFuncTab_QT : public CvOpenGlFuncTab
{
public:
#ifdef Q_WS_WIN
GlFuncTab_QT(HDC hDC);
#else
GlFuncTab_QT();
#endif
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;
PFNGLBUFFERDATAPROC glBufferDataExt;
PFNGLBUFFERSUBDATAPROC glBufferSubDataExt;
PFNGLBINDBUFFERPROC glBindBufferExt;
PFNGLMAPBUFFERPROC glMapBufferExt;
PFNGLUNMAPBUFFERPROC glUnmapBufferExt;
bool initialized;
#ifdef Q_WS_WIN
HDC hDC;
#endif
};
#ifdef Q_WS_WIN
GlFuncTab_QT::GlFuncTab_QT(HDC hDC_) : hDC(hDC_)
#else
GlFuncTab_QT::GlFuncTab_QT()
#endif
{
glGenBuffersExt = 0;
glDeleteBuffersExt = 0;
glBufferDataExt = 0;
glBufferSubDataExt = 0;
glBindBufferExt = 0;
glMapBufferExt = 0;
glUnmapBufferExt = 0;
initialized = false;
}
void GlFuncTab_QT::genBuffers(int n, unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_QT::genBuffers" );
__BEGIN__;
if (!glGenBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glGenBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void GlFuncTab_QT::deleteBuffers(int n, const unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_QT::deleteBuffers" );
__BEGIN__;
if (!glDeleteBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glDeleteBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void GlFuncTab_QT::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
{
CV_FUNCNAME( "GlFuncTab_QT::bufferData" );
__BEGIN__;
if (!glBufferDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBufferDataExt(target, size, data, usage);
CV_CheckGlError();
__END__;
}
void GlFuncTab_QT::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
{
CV_FUNCNAME( "GlFuncTab_QT::bufferSubData" );
__BEGIN__;
if (!glBufferSubDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBufferSubDataExt(target, offset, size, data);
CV_CheckGlError();
__END__;
}
void GlFuncTab_QT::bindBuffer(unsigned int target, unsigned int buffer) const
{
CV_FUNCNAME( "GlFuncTab_QT::bindBuffer" );
__BEGIN__;
if (!glBindBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBindBufferExt(target, buffer);
CV_CheckGlError();
__END__;
}
void* GlFuncTab_QT::mapBuffer(unsigned int target, unsigned int access) const
{
CV_FUNCNAME( "GlFuncTab_QT::mapBuffer" );
void* res = 0;
__BEGIN__;
if (!glMapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
res = glMapBufferExt(target, access);
CV_CheckGlError();
__END__;
return res;
}
void GlFuncTab_QT::unmapBuffer(unsigned int target) const
{
CV_FUNCNAME( "GlFuncTab_QT::unmapBuffer" );
__BEGIN__;
if (!glUnmapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glUnmapBufferExt(target);
CV_CheckGlError();
__END__;
}
void GlFuncTab_QT::generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool /*underline*/, int start, int count, int base) const
{
#ifdef Q_WS_WIN
CV_FUNCNAME( "GlFuncTab_QT::generateBitmapFont" );
#endif
QFont font(QString(family.c_str()), height, weight, italic);
__BEGIN__;
#ifndef Q_WS_WIN
font.setStyleStrategy(QFont::OpenGLCompatible);
if (font.handle())
glXUseXFont(font.handle(), start, count, base);
#else
SelectObject(hDC, font.handle());
if (!wglUseFontBitmaps(hDC, start, count, base))
CV_ERROR(CV_OpenGlApiCallError, "Can't create font");
#endif
__END__;
}
bool GlFuncTab_QT::isGlContextInitialized() const
{
return initialized;
}
void OpenGlViewPort::initializeGL()
{
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
#ifdef Q_WS_WIN
std::auto_ptr<GlFuncTab_QT> qglFuncTab(new GlFuncTab_QT(getDC()));
#else
std::auto_ptr<GlFuncTab_QT> qglFuncTab(new GlFuncTab_QT);
#endif
// Load extensions
qglFuncTab->glGenBuffersExt = (PFNGLGENBUFFERSPROC)context()->getProcAddress("glGenBuffers");
qglFuncTab->glDeleteBuffersExt = (PFNGLDELETEBUFFERSPROC)context()->getProcAddress("glDeleteBuffers");
qglFuncTab->glBufferDataExt = (PFNGLBUFFERDATAPROC)context()->getProcAddress("glBufferData");
qglFuncTab->glBufferSubDataExt = (PFNGLBUFFERSUBDATAPROC)context()->getProcAddress("glBufferSubData");
qglFuncTab->glBindBufferExt = (PFNGLBINDBUFFERPROC)context()->getProcAddress("glBindBuffer");
qglFuncTab->glMapBufferExt = (PFNGLMAPBUFFERPROC)context()->getProcAddress("glMapBuffer");
qglFuncTab->glUnmapBufferExt = (PFNGLUNMAPBUFFERPROC)context()->getProcAddress("glUnmapBuffer");
qglFuncTab->initialized = true;
glFuncTab = qglFuncTab.release();
icvSetOpenGlFuncTab(glFuncTab);
}
void OpenGlViewPort::resizeGL(int w, int h)
@ -3411,14 +3110,10 @@ void OpenGlViewPort::resizeGL(int w, int h)
void OpenGlViewPort::paintGL()
{
icvSetOpenGlFuncTab(glFuncTab);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (glDrawCallback)
glDrawCallback(glDrawData);
CV_CheckGlError();
}
void OpenGlViewPort::mousePressEvent(QMouseEvent* evnt)

View File

@ -141,7 +141,6 @@ public slots:
void enablePropertiesButtonEachWindow();
void setOpenGlDrawCallback(QString name, void* callback, void* userdata);
void setOpenGlCleanCallback(QString name, void* callback, void* userdata);
void setOpenGlContext(QString name);
void updateWindow(QString name);
double isOpenGl(QString name);
@ -312,7 +311,6 @@ public:
static void addSlider2(CvWindow* w, QString name, int* value, int count, CvTrackbarCallback2 on_change CV_DEFAULT(NULL), void* userdata CV_DEFAULT(0));
void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata);
void makeCurrentOpenGlContext();
void updateGl();
bool isOpenGl();
@ -397,7 +395,6 @@ public:
virtual void startDisplayInfo(QString text, int delayms) = 0;
virtual void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata) = 0;
virtual void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata) = 0;
virtual void makeCurrentOpenGlContext() = 0;
virtual void updateGl() = 0;
@ -429,7 +426,6 @@ public:
void startDisplayInfo(QString text, int delayms);
void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata);
void makeCurrentOpenGlContext();
void updateGl();
@ -456,11 +452,6 @@ private:
CvOpenGlDrawCallback glDrawCallback;
void* glDrawData;
CvOpenGlCleanCallback glCleanCallback;
void* glCleanData;
CvOpenGlFuncTab* glFuncTab;
void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags);
void icvmouseProcessing(QPointF pt, int cv_event, int flags);
};
@ -491,7 +482,6 @@ public:
void startDisplayInfo(QString text, int delayms);
void setOpenGlDrawCallback(CvOpenGlDrawCallback callback, void* userdata);
void setOpenGlCleanCallback(CvOpenGlCleanCallback callback, void* userdata);
void makeCurrentOpenGlContext();
void updateGl();

View File

@ -417,9 +417,6 @@ typedef struct CvWindow
CvOpenGlDrawCallback glDrawCallback;
void* glDrawData;
CvOpenGlCleanCallback glCleanCallback;
void* glCleanData;
#endif
}
CvWindow;
@ -692,238 +689,6 @@ double cvGetOpenGlProp_GTK(const char* name)
namespace
{
class GlFuncTab_GTK : public CvOpenGlFuncTab
{
public:
GlFuncTab_GTK();
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;
PFNGLBUFFERDATAPROC glBufferDataExt;
PFNGLBUFFERSUBDATAPROC glBufferSubDataExt;
PFNGLBINDBUFFERPROC glBindBufferExt;
PFNGLMAPBUFFERPROC glMapBufferExt;
PFNGLUNMAPBUFFERPROC glUnmapBufferExt;
bool initialized;
};
GlFuncTab_GTK::GlFuncTab_GTK()
{
glGenBuffersExt = 0;
glDeleteBuffersExt = 0;
glBufferDataExt = 0;
glBufferSubDataExt = 0;
glBindBufferExt = 0;
glMapBufferExt = 0;
glUnmapBufferExt = 0;
initialized = false;
}
void GlFuncTab_GTK::genBuffers(int n, unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_GTK::genBuffers" );
__BEGIN__;
if (!glGenBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glGenBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void GlFuncTab_GTK::deleteBuffers(int n, const unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_GTK::deleteBuffers" );
__BEGIN__;
if (!glDeleteBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glDeleteBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void GlFuncTab_GTK::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
{
CV_FUNCNAME( "GlFuncTab_GTK::bufferData" );
__BEGIN__;
if (!glBufferDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBufferDataExt(target, size, data, usage);
CV_CheckGlError();
__END__;
}
void GlFuncTab_GTK::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
{
CV_FUNCNAME( "GlFuncTab_GTK::bufferSubData" );
__BEGIN__;
if (!glBufferSubDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBufferSubDataExt(target, offset, size, data);
CV_CheckGlError();
__END__;
}
void GlFuncTab_GTK::bindBuffer(unsigned int target, unsigned int buffer) const
{
CV_FUNCNAME( "GlFuncTab_GTK::bindBuffer" );
__BEGIN__;
if (!glBindBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBindBufferExt(target, buffer);
CV_CheckGlError();
__END__;
}
void* GlFuncTab_GTK::mapBuffer(unsigned int target, unsigned int access) const
{
CV_FUNCNAME( "GlFuncTab_GTK::mapBuffer" );
void* res = 0;
__BEGIN__;
if (!glMapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
res = glMapBufferExt(target, access);
CV_CheckGlError();
__END__;
return res;
}
void GlFuncTab_GTK::unmapBuffer(unsigned int target) const
{
CV_FUNCNAME( "GlFuncTab_GTK::unmapBuffer" );
__BEGIN__;
if (!glUnmapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glUnmapBufferExt(target);
CV_CheckGlError();
__END__;
}
void GlFuncTab_GTK::generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool /*underline*/, int start, int count, int base) const
{
PangoFontDescription* fontDecr;
PangoFont* pangoFont;
CV_FUNCNAME( "GlFuncTab_GTK::generateBitmapFont" );
__BEGIN__;
fontDecr = pango_font_description_new();
pango_font_description_set_size(fontDecr, height);
pango_font_description_set_family_static(fontDecr, family.c_str());
pango_font_description_set_weight(fontDecr, static_cast<PangoWeight>(weight));
pango_font_description_set_style(fontDecr, italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
pangoFont = gdk_gl_font_use_pango_font(fontDecr, start, count, base);
pango_font_description_free(fontDecr);
if (!pangoFont)
CV_ERROR(CV_OpenGlApiCallError, "Can't create font");
__END__;
}
bool GlFuncTab_GTK::isGlContextInitialized() const
{
return initialized;
}
void initGl()
{
static GlFuncTab_GTK glFuncTab;
static bool first = true;
if (first)
{
// Load extensions
GdkGLProc func;
func = gdk_gl_get_proc_address("glGenBuffers");
glFuncTab.glGenBuffersExt = (PFNGLGENBUFFERSPROC)func;
func = gdk_gl_get_proc_address("glDeleteBuffers");
glFuncTab.glDeleteBuffersExt = (PFNGLDELETEBUFFERSPROC)func;
func = gdk_gl_get_proc_address("glBufferData");
glFuncTab.glBufferDataExt = (PFNGLBUFFERDATAPROC)func;
func = gdk_gl_get_proc_address("glBufferSubData");
glFuncTab.glBufferSubDataExt = (PFNGLBUFFERSUBDATAPROC)func;
func = gdk_gl_get_proc_address("glBindBuffer");
glFuncTab.glBindBufferExt = (PFNGLBINDBUFFERPROC)func;
func = gdk_gl_get_proc_address("glMapBuffer");
glFuncTab.glMapBufferExt = (PFNGLMAPBUFFERPROC)func;
func = gdk_gl_get_proc_address("glUnmapBuffer");
glFuncTab.glUnmapBufferExt = (PFNGLUNMAPBUFFERPROC)func;
glFuncTab.initialized = true;
icvSetOpenGlFuncTab(&glFuncTab);
first = false;
}
}
void createGlContext(CvWindow* window)
{
GdkGLConfig* glconfig;
@ -932,8 +697,6 @@ namespace
__BEGIN__;
window->useGl = false;
// Try double-buffered visual
glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE));
if (!glconfig)
@ -943,24 +706,11 @@ namespace
if (!gtk_widget_set_gl_capability(window->widget, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
initGl();
window->useGl = true;
__END__;
}
void releaseGlContext(CvWindow* window)
{
//CV_FUNCNAME( "releaseGlContext" );
//__BEGIN__;
window->useGl = false;
//__END__;
}
void drawGl(CvWindow* window)
{
CV_FUNCNAME( "drawGl" );
@ -980,8 +730,6 @@ namespace
if (window->glDrawCallback)
window->glDrawCallback(window->glDrawData);
CV_CheckGlError();
if (gdk_gl_drawable_is_double_buffered (gldrawable))
gdk_gl_drawable_swap_buffers(gldrawable);
else
@ -1101,9 +849,6 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
window->glDrawCallback = 0;
window->glDrawData = 0;
window->glCleanCallback = 0;
window->glCleanData = 0;
#endif
//
@ -1233,40 +978,6 @@ CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback call
__END__;
}
void icvSetOpenGlCleanCallback(const char* name, CvOpenGlCleanCallback callback, void* userdata)
{
CvWindow* window;
GdkGLContext* glcontext;
GdkGLDrawable* gldrawable;
CV_FUNCNAME( "icvSetOpenGlCleanCallback" );
__BEGIN__;
if (!name)
CV_ERROR(CV_StsNullPtr, "NULL name string");
window = icvFindWindowByName(name);
if (!window)
EXIT;
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
glcontext = gtk_widget_get_gl_context(window->widget);
gldrawable = gtk_widget_get_gl_drawable(window->widget);
gdk_gl_drawable_make_current(gldrawable, glcontext);
if (window->glCleanCallback)
window->glCleanCallback(window->glCleanData);
window->glCleanCallback = callback;
window->glCleanData = userdata;
__END__;
}
#endif // HAVE_OPENGL
@ -1276,25 +987,6 @@ static void icvDeleteWindow( CvWindow* window )
{
CvTrackbar* trackbar;
#ifdef HAVE_OPENGL
if (window->useGl)
{
GdkGLContext* glcontext = gtk_widget_get_gl_context(window->widget);
GdkGLDrawable* gldrawable = gtk_widget_get_gl_drawable(window->widget);
gdk_gl_drawable_make_current(gldrawable, glcontext);
if (window->glCleanCallback)
{
window->glCleanCallback(window->glCleanData);
window->glCleanCallback = 0;
window->glCleanData = 0;
}
releaseGlContext(window);
}
#endif
if( window->prev )
window->prev->next = window->next;
else

View File

@ -192,11 +192,6 @@ typedef struct CvWindow
CvOpenGlDrawCallback glDrawCallback;
void* glDrawData;
CvOpenGlCleanCallback glCleanCallback;
void* glCleanData;
CvOpenGlFuncTab* glFuncTab;
#endif
}
CvWindow;
@ -580,272 +575,8 @@ double cvGetOpenGlProp_W32(const char* name)
#ifdef HAVE_OPENGL
#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GL_VERSION_1_5
/* GL types for handling large vertex buffer objects */
typedef ptrdiff_t GLintptr;
typedef ptrdiff_t GLsizeiptr;
#endif
namespace
{
typedef void (APIENTRYP PFNGLGENBUFFERSPROC ) (GLsizei n, GLuint *buffers);
typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRYP PFNGLBUFFERDATAPROC ) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
typedef void (APIENTRYP PFNGLBUFFERSUBDATAPROC) (GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid* data);
typedef void (APIENTRYP PFNGLBINDBUFFERPROC ) (GLenum target, GLuint buffer);
typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERPROC) (GLenum target, GLenum access);
typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERPROC) (GLenum target);
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;
PFNGLBUFFERDATAPROC glBufferDataExt;
PFNGLBUFFERSUBDATAPROC glBufferSubDataExt;
PFNGLBINDBUFFERPROC glBindBufferExt;
PFNGLMAPBUFFERPROC glMapBufferExt;
PFNGLUNMAPBUFFERPROC glUnmapBufferExt;
bool initialized;
HDC hDC;
};
GlFuncTab_W32::GlFuncTab_W32(HDC hDC_)
{
glGenBuffersExt = 0;
glDeleteBuffersExt = 0;
glBufferDataExt = 0;
glBufferSubDataExt = 0;
glBindBufferExt = 0;
glMapBufferExt = 0;
glUnmapBufferExt = 0;
initialized = false;
hDC = hDC_;
}
void GlFuncTab_W32::genBuffers(int n, unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_W32::genBuffers" );
__BEGIN__;
if (!glGenBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glGenBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void GlFuncTab_W32::deleteBuffers(int n, const unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_W32::deleteBuffers" );
__BEGIN__;
if (!glDeleteBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glDeleteBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void GlFuncTab_W32::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
{
CV_FUNCNAME( "GlFuncTab_W32::bufferData" );
__BEGIN__;
if (!glBufferDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBufferDataExt(target, size, data, usage);
CV_CheckGlError();
__END__;
}
void GlFuncTab_W32::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
{
CV_FUNCNAME( "GlFuncTab_W32::bufferSubData" );
__BEGIN__;
if (!glBufferSubDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBufferSubDataExt(target, offset, size, data);
CV_CheckGlError();
__END__;
}
void GlFuncTab_W32::bindBuffer(unsigned int target, unsigned int buffer) const
{
CV_FUNCNAME( "GlFuncTab_W32::bindBuffer" );
__BEGIN__;
if (!glBindBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBindBufferExt(target, buffer);
CV_CheckGlError();
__END__;
}
void* GlFuncTab_W32::mapBuffer(unsigned int target, unsigned int access) const
{
CV_FUNCNAME( "GlFuncTab_W32::mapBuffer" );
void* res = 0;
__BEGIN__;
if (!glMapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
res = glMapBufferExt(target, access);
CV_CheckGlError();
__END__;
return res;
}
void GlFuncTab_W32::unmapBuffer(unsigned int target) const
{
CV_FUNCNAME( "GlFuncTab_W32::unmapBuffer" );
__BEGIN__;
if (!glUnmapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
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(window->dc));
// Load extensions
PROC func;
func = wglGetProcAddress("glGenBuffers");
glFuncTab->glGenBuffersExt = (PFNGLGENBUFFERSPROC)func;
func = wglGetProcAddress("glDeleteBuffers");
glFuncTab->glDeleteBuffersExt = (PFNGLDELETEBUFFERSPROC)func;
func = wglGetProcAddress("glBufferData");
glFuncTab->glBufferDataExt = (PFNGLBUFFERDATAPROC)func;
func = wglGetProcAddress("glBufferSubData");
glFuncTab->glBufferSubDataExt = (PFNGLBUFFERSUBDATAPROC)func;
func = wglGetProcAddress("glBindBuffer");
glFuncTab->glBindBufferExt = (PFNGLBINDBUFFERPROC)func;
func = wglGetProcAddress("glMapBuffer");
glFuncTab->glMapBufferExt = (PFNGLMAPBUFFERPROC)func;
func = wglGetProcAddress("glUnmapBuffer");
glFuncTab->glUnmapBufferExt = (PFNGLUNMAPBUFFERPROC)func;
glFuncTab->initialized = true;
window->glFuncTab = glFuncTab.release();
icvSetOpenGlFuncTab(window->glFuncTab);
}
void createGlContext(HWND hWnd, HDC& hGLDC, HGLRC& hGLRC, bool& useGl)
{
CV_FUNCNAME( "createGlContext" );
@ -907,8 +638,6 @@ namespace
__BEGIN__;
delete window->glFuncTab;
if (window->hGLRC)
{
wglDeleteContext(window->hGLRC);
@ -940,8 +669,6 @@ namespace
if (window->glDrawCallback)
window->glDrawCallback(window->glDrawData);
CV_CheckGlError();
if (!SwapBuffers(window->dc))
CV_ERROR( CV_OpenGlApiCallError, "Can't swap OpenGL buffers" );
@ -1042,7 +769,6 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
#ifndef HAVE_OPENGL
window->dc = CreateCompatibleDC(0);
#else
window->glFuncTab = 0;
if (!useGl)
{
window->dc = CreateCompatibleDC(0);
@ -1054,14 +780,10 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
window->dc = hGLDC;
window->hGLRC = hGLRC;
window->useGl = true;
initGl(window);
}
window->glDrawCallback = 0;
window->glDrawData = 0;
window->glCleanCallback = 0;
window->glCleanData = 0;
#endif
window->last_key = 0;
@ -1112,8 +834,6 @@ CV_IMPL void cvSetOpenGlContext(const char* name)
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
icvSetOpenGlFuncTab(window->glFuncTab);
__END__;
}
@ -1161,30 +881,6 @@ CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback call
__END__;
}
void icvSetOpenGlCleanCallback(const char* name, CvOpenGlCleanCallback callback, void* userdata)
{
CV_FUNCNAME( "icvSetOpenGlCleanCallback" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR(CV_StsNullPtr, "NULL name string");
window = icvFindWindowByName(name);
if (!window)
EXIT;
if (window->glCleanCallback)
window->glCleanCallback(window->glCleanData);
window->glCleanCallback = callback;
window->glCleanData = userdata;
__END__;
}
#endif // HAVE_OPENGL
static void icvRemoveWindow( CvWindow* window )
@ -1194,18 +890,7 @@ static void icvRemoveWindow( CvWindow* window )
#ifdef HAVE_OPENGL
if (window->useGl)
{
wglMakeCurrent(window->dc, window->hGLRC);
if (window->glCleanCallback)
{
window->glCleanCallback(window->glCleanData);
window->glCleanCallback = 0;
window->glCleanData = 0;
}
releaseGlContext(window);
}
#endif
if( window->frame )

View File

@ -1,357 +0,0 @@
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include "opencv2/core/core.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/contrib/contrib.hpp"
using namespace std;
using namespace cv;
using namespace cv::gpu;
class PointCloudRenderer
{
public:
PointCloudRenderer(const Mat& points, const Mat& img, double scale);
void onMouseEvent(int event, int x, int y, int flags);
void draw();
void update(int key, double aspect);
int fov_;
private:
int mouse_dx_;
int mouse_dy_;
double yaw_;
double pitch_;
Point3d pos_;
TickMeter tm_;
static const int step_;
int frame_;
GlCamera camera_;
GlArrays pointCloud_;
string fps_;
};
bool stop = false;
static void mouseCallback(int event, int x, int y, int flags, void* userdata)
{
if (stop)
return;
PointCloudRenderer* renderer = static_cast<PointCloudRenderer*>(userdata);
renderer->onMouseEvent(event, x, y, flags);
}
static void openGlDrawCallback(void* userdata)
{
if (stop)
return;
PointCloudRenderer* renderer = static_cast<PointCloudRenderer*>(userdata);
renderer->draw();
}
int main(int argc, const char* argv[])
{
const char* keys =
"{ l left | | left image file name }"
"{ r right | | right image file name }"
"{ i intrinsic | | intrinsic camera parameters file name }"
"{ e extrinsic | | extrinsic camera parameters file name }"
"{ d ndisp | 256 | number of disparities }"
"{ s scale | 1.0 | scale factor for point cloud }"
"{ h help | | print help message }";
CommandLineParser cmd(argc, argv, keys);
if (cmd.has("help"))
{
cmd.printMessage();
return 0;
}
string left = cmd.get<string>("left");
string right = cmd.get<string>("right");
string intrinsic = cmd.get<string>("intrinsic");
string extrinsic = cmd.get<string>("extrinsic");
int ndisp = cmd.get<int>("ndisp");
double scale = cmd.get<double>("scale");
if (!cmd.check())
{
cmd.printErrors();
return 0;
}
if (left.empty() || right.empty())
{
cout << "Missed input images" << endl;
cout << "Avaible options:" << endl;
cmd.printMessage();
return 0;
}
if (intrinsic.empty() ^ extrinsic.empty())
{
cout << "Boss camera parameters must be specified" << endl;
cout << "Avaible options:" << endl;
cmd.printMessage();
return 0;
}
Mat imgLeftColor = imread(left, IMREAD_COLOR);
Mat imgRightColor = imread(right, IMREAD_COLOR);
if (imgLeftColor.empty())
{
cout << "Can't load image " << left << endl;
return -1;
}
if (imgRightColor.empty())
{
cout << "Can't load image " << right << endl;
return -1;
}
Mat Q = Mat::eye(4, 4, CV_32F);
if (!intrinsic.empty() && !extrinsic.empty())
{
FileStorage fs;
// reading intrinsic parameters
fs.open(intrinsic, CV_STORAGE_READ);
if (!fs.isOpened())
{
cout << "Failed to open file " << intrinsic << endl;
return -1;
}
Mat M1, D1, M2, D2;
fs["M1"] >> M1;
fs["D1"] >> D1;
fs["M2"] >> M2;
fs["D2"] >> D2;
// reading extrinsic parameters
fs.open(extrinsic, CV_STORAGE_READ);
if (!fs.isOpened())
{
cout << "Failed to open file " << extrinsic << endl;
return -1;
}
Mat R, T, R1, P1, R2, P2;
fs["R"] >> R;
fs["T"] >> T;
Size img_size = imgLeftColor.size();
Rect roi1, roi2;
stereoRectify(M1, D1, M2, D2, img_size, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, -1, img_size, &roi1, &roi2);
Mat map11, map12, map21, map22;
initUndistortRectifyMap(M1, D1, R1, P1, img_size, CV_16SC2, map11, map12);
initUndistortRectifyMap(M2, D2, R2, P2, img_size, CV_16SC2, map21, map22);
Mat img1r, img2r;
remap(imgLeftColor, img1r, map11, map12, INTER_LINEAR);
remap(imgRightColor, img2r, map21, map22, INTER_LINEAR);
imgLeftColor = img1r(roi1);
imgRightColor = img2r(roi2);
}
Mat imgLeftGray, imgRightGray;
cvtColor(imgLeftColor, imgLeftGray, COLOR_BGR2GRAY);
cvtColor(imgRightColor, imgRightGray, COLOR_BGR2GRAY);
cvtColor(imgLeftColor, imgLeftColor, COLOR_BGR2RGB);
Mat disp, points;
StereoBM bm(0, ndisp);
bm(imgLeftGray, imgRightGray, disp);
disp.convertTo(disp, CV_8U, 1.0 / 16.0);
disp = disp(Range(21, disp.rows - 21), Range(ndisp, disp.cols - 21)).clone();
imgLeftColor = imgLeftColor(Range(21, imgLeftColor.rows - 21), Range(ndisp, imgLeftColor.cols - 21)).clone();
reprojectImageTo3D(disp, points, Q);
const string windowName = "OpenGL Sample";
namedWindow(windowName, WINDOW_OPENGL);
resizeWindow(windowName, 400, 400);
PointCloudRenderer renderer(points, imgLeftColor, scale);
createTrackbar("Fov", windowName, &renderer.fov_, 100);
setMouseCallback(windowName, mouseCallback, &renderer);
setOpenGlDrawCallback(windowName, openGlDrawCallback, &renderer);
for(;;)
{
int key = waitKey(10);
if (key >= 0)
key = key & 0xff;
if (key == 27)
{
stop = true;
break;
}
double aspect = getWindowProperty(windowName, WND_PROP_ASPECT_RATIO);
key = tolower(key);
renderer.update(key, aspect);
updateWindow(windowName);
}
return 0;
}
const int PointCloudRenderer::step_ = 20;
PointCloudRenderer::PointCloudRenderer(const Mat& points, const Mat& img, double scale)
{
mouse_dx_ = 0;
mouse_dy_ = 0;
fov_ = 0;
yaw_ = 0.0;
pitch_ = 0.0;
frame_ = 0;
camera_.setScale(Point3d(scale, scale, scale));
pointCloud_.setVertexArray(points);
pointCloud_.setColorArray(img, false);
tm_.start();
}
inline int clamp(int val, int minVal, int maxVal)
{
return max(min(val, maxVal), minVal);
}
void PointCloudRenderer::onMouseEvent(int event, int x, int y, int /*flags*/)
{
static int oldx = x;
static int oldy = y;
static bool moving = false;
if (event == EVENT_LBUTTONDOWN)
{
oldx = x;
oldy = y;
moving = true;
}
else if (event == EVENT_LBUTTONUP)
{
moving = false;
}
if (moving)
{
mouse_dx_ = oldx - x;
mouse_dy_ = oldy - y;
}
else
{
mouse_dx_ = 0;
mouse_dy_ = 0;
}
const int mouseClamp = 300;
mouse_dx_ = clamp(mouse_dx_, -mouseClamp, mouseClamp);
mouse_dy_ = clamp(mouse_dy_, -mouseClamp, mouseClamp);
}
static Point3d rotate(Point3d v, double yaw, double pitch)
{
Point3d t1;
t1.x = v.x * cos(-yaw / 180.0 * CV_PI) - v.z * sin(-yaw / 180.0 * CV_PI);
t1.y = v.y;
t1.z = v.x * sin(-yaw / 180.0 * CV_PI) + v.z * cos(-yaw / 180.0 * CV_PI);
Point3d t2;
t2.x = t1.x;
t2.y = t1.y * cos(pitch / 180.0 * CV_PI) - t1.z * sin(pitch / 180.0 * CV_PI);
t2.z = t1.y * sin(pitch / 180.0 * CV_PI) + t1.z * cos(pitch / 180.0 * CV_PI);
return t2;
}
void PointCloudRenderer::update(int key, double aspect)
{
const Point3d dirVec(0.0, 0.0, -1.0);
const Point3d upVec(0.0, 1.0, 0.0);
const Point3d leftVec(-1.0, 0.0, 0.0);
const double posStep = 0.1;
const double mouseStep = 0.001;
camera_.setPerspectiveProjection(30.0 + fov_ / 100.0 * 40.0, aspect, 0.1, 1000.0);
yaw_ += mouse_dx_ * mouseStep;
pitch_ += mouse_dy_ * mouseStep;
if (key == 'w')
pos_ += posStep * rotate(dirVec, yaw_, pitch_);
else if (key == 's')
pos_ -= posStep * rotate(dirVec, yaw_, pitch_);
else if (key == 'a')
pos_ += posStep * rotate(leftVec, yaw_, pitch_);
else if (key == 'd')
pos_ -= posStep * rotate(leftVec, yaw_, pitch_);
else if (key == 'q')
pos_ += posStep * rotate(upVec, yaw_, pitch_);
else if (key == 'e')
pos_ -= posStep * rotate(upVec, yaw_, pitch_);
camera_.setCameraPos(pos_, yaw_, pitch_, 0.0);
tm_.stop();
if (frame_++ >= step_)
{
ostringstream ostr;
ostr << "FPS: " << step_ / tm_.getTimeSec();
fps_ = ostr.str();
frame_ = 0;
tm_.reset();
}
tm_.start();
}
void PointCloudRenderer::draw()
{
camera_.setupProjectionMatrix();
camera_.setupModelViewMatrix();
render(pointCloud_);
render(fps_, GlFont::get("Courier New", 16), Scalar::all(255), Point2d(3.0, 0.0));
}

View File

@ -4,7 +4,6 @@
#include "cvconfig.h"
#include "opencv2/core/core.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
@ -14,12 +13,6 @@ using namespace cv::gpu;
void getFlowField(const Mat& u, const Mat& v, Mat& flowField);
#ifdef HAVE_OPENGL
void needleMapDraw(void* userdata);
#endif
int main(int argc, const char* argv[])
{
try
@ -79,12 +72,8 @@ int main(int argc, const char* argv[])
namedWindow("Forward flow");
namedWindow("Backward flow");
namedWindow("Needle Map", WINDOW_OPENGL);
namedWindow("Interpolated frame");
setGlDevice();
cout << "Press:" << endl;
cout << "\tESC to quit" << endl;
cout << "\t'a' to move to the previous frame" << endl;
@ -123,14 +112,6 @@ int main(int argc, const char* argv[])
Mat flowFieldBackward;
getFlowField(Mat(d_bu), Mat(d_bv), flowFieldBackward);
#ifdef HAVE_OPENGL
cout << "Create Optical Flow Needle Map..." << endl;
GpuMat d_vertex, d_colors;
createOpticalFlowNeedleMap(d_fu, d_fv, d_vertex, d_colors);
#endif
cout << "Interpolating..." << endl;
// first frame color components
@ -195,14 +176,6 @@ int main(int argc, const char* argv[])
imshow("Forward flow", flowFieldForward);
imshow("Backward flow", flowFieldBackward);
#ifdef HAVE_OPENGL
GlArrays arr;
arr.setVertexArray(d_vertex);
arr.setColorArray(d_colors, false);
setOpenGlDrawCallback("Needle Map", needleMapDraw, &arr);
#endif
int currentFrame = 0;
imshow("Interpolated frame", frames[currentFrame]);
@ -292,21 +265,3 @@ void getFlowField(const Mat& u, const Mat& v, Mat& flowField)
}
}
}
#ifdef HAVE_OPENGL
void needleMapDraw(void* userdata)
{
const GlArrays* arr = static_cast<const GlArrays*>(userdata);
GlCamera camera;
camera.setOrthoProjection(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
camera.lookAt(Point3d(0.0, 0.0, 1.0), Point3d(0.0, 0.0, 0.0), Point3d(0.0, 1.0, 0.0));
camera.setupProjectionMatrix();
camera.setupModelViewMatrix();
render(*arr, RenderMode::TRIANGLES);
}
#endif

View File

@ -1,135 +0,0 @@
#include <iostream>
#include <string>
#include "opencv2/core/core.hpp"
#include "opencv2/core/gpumat.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
using namespace std;
using namespace cv;
using namespace cv::gpu;
struct Timer
{
Timer(const string& msg_)
{
msg = msg_;
tm.reset();
tm.start();
}
~Timer()
{
tm.stop();
cout << msg << " " << tm.getTimeMilli() << " ms\n";
}
string msg;
TickMeter tm;
};
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " image" << endl;
return -1;
}
try
{
bool haveCuda = getCudaEnabledDeviceCount() > 0;
const string openGlMatWnd = "OpenGL Mat";
const string openGlBufferWnd = "OpenGL GlBuffer";
const string openGlTextureWnd = "OpenGL GlTexture";
const string openGlGpuMatWnd = "OpenGL GpuMat";
const string matWnd = "Mat";
namedWindow(openGlMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow(openGlBufferWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow(openGlTextureWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
if (haveCuda)
namedWindow(openGlGpuMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow("Mat", WINDOW_AUTOSIZE);
Mat img = imread(argv[1]);
if (haveCuda)
setGlDevice();
setOpenGlContext(openGlBufferWnd);
GlBuffer buf(img, GlBuffer::TEXTURE_BUFFER);
setOpenGlContext(openGlTextureWnd);
GlTexture tex(img);
GpuMat d_img;
if (haveCuda)
d_img.upload(img);
cout << "=== First call\n\n";
{
Timer t("OpenGL Mat ");
imshow(openGlMatWnd, img);
}
{
Timer t("OpenGL GlBuffer ");
imshow(openGlBufferWnd, buf);
}
{
Timer t("OpenGL GlTexture");
imshow(openGlTextureWnd, tex);
}
if (haveCuda)
{
Timer t("OpenGL GpuMat ");
imshow(openGlGpuMatWnd, d_img);
}
{
Timer t("Mat ");
imshow(matWnd, img);
}
waitKey();
cout << "\n=== Second call\n\n";
{
Timer t("OpenGL Mat ");
imshow(openGlMatWnd, img);
}
{
Timer t("OpenGL GlBuffer ");
imshow(openGlBufferWnd, buf);
}
{
Timer t("OpenGL GlTexture");
imshow(openGlTextureWnd, tex);
}
if (haveCuda)
{
Timer t("OpenGL GpuMat ");
imshow(openGlGpuMatWnd, d_img);
}
{
Timer t("Mat ");
imshow(matWnd, img);
}
cout << "\n";
waitKey();
}
catch(const exception& e)
{
cout << e.what() << endl;
}
return 0;
}

128
samples/gpu/opengl.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <iostream>
#include "cvconfig.h"
#ifndef HAVE_OPENGL
int main()
{
std::cerr << "Library was built without OpenGL support" << std::endl;
return -1;
}
#else
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX 1
#include <windows.h>
#endif
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "opencv2/core/core.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/core/gpumat.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
using namespace cv::gpu;
const int win_width = 800;
const int win_height = 640;
struct DrawData
{
GlArrays arr;
GlTexture2D tex;
GlBuffer indices;
};
void CV_CDECL draw(void* userdata);
void CV_CDECL draw(void* userdata)
{
static double angle = 0.0;
DrawData* data = static_cast<DrawData*>(userdata);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)win_width / win_height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 4, 0, 0, 0, 0, 1, 0);
glRotated(angle, 0, 1, 0);
glEnable(GL_TEXTURE_2D);
data->tex.bind();
glDisable(GL_CULL_FACE);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
render(data->arr, data->indices, RenderMode::TRIANGLES);
angle += 0.3;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " image" << endl;
return -1;
}
Mat img = imread(argv[1]);
if (img.empty())
{
cerr << "Can't open image " << argv[1] << endl;
return -1;
}
namedWindow("OpenGL", WINDOW_OPENGL);
resizeWindow("OpenGL", win_width, win_height);
Mat_<Vec2f> vertex(1, 4);
vertex << Vec2f(-1, 1), Vec2f(-1, -1), Vec2f(1, -1), Vec2f(1, 1);
Mat_<Vec2f> texCoords(1, 4);
texCoords << Vec2f(0, 0), Vec2f(0, 1), Vec2f(1, 1), Vec2f(1, 0);
Mat_<int> indices(1, 6);
indices << 0, 1, 2, 2, 3, 0;
DrawData data;
data.arr.setVertexArray(vertex);
data.arr.setTexCoordArray(texCoords);
data.arr.setAutoRelease(false);
data.indices.copyFrom(indices);
data.indices.setAutoRelease(false);
data.tex.copyFrom(img);
data.tex.setAutoRelease(false);
setOpenGlDrawCallback("OpenGL", draw, &data);
for (;;)
{
updateWindow("OpenGL");
int key = waitKey(10);
if ((key & 0xff) == 27)
break;
}
setOpenGlDrawCallback("OpenGL", 0, 0);
destroyAllWindows();
return 0;
}
#endif

View File

@ -3,7 +3,6 @@
#include "cvconfig.h"
#include "opencv2/core/core.hpp"
#include "opencv2/core/opengl_interop.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
@ -66,40 +65,6 @@ static void drawArrows(Mat& frame, const vector<Point2f>& prevPts, const vector<
}
}
#ifdef HAVE_OPENGL
struct DrawData
{
GlTexture tex;
GlArrays arr;
};
static void drawCallback(void* userdata)
{
DrawData* data = static_cast<DrawData*>(userdata);
if (data->tex.empty() || data->arr.empty())
return;
static GlCamera camera;
static bool init_camera = true;
if (init_camera)
{
camera.setOrthoProjection(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
camera.lookAt(Point3d(0.0, 0.0, 1.0), Point3d(0.0, 0.0, 0.0), Point3d(0.0, 1.0, 0.0));
init_camera = false;
}
camera.setupProjectionMatrix();
camera.setupModelViewMatrix();
render(data->tex);
render(data->arr, RenderMode::TRIANGLES);
}
#endif
template <typename T> inline T clamp (T x, T a, T b)
{
return ((x) > (a) ? ((x) < (b) ? (x) : (b)) : (a));
@ -199,12 +164,6 @@ int main(int argc, const char* argv[])
namedWindow("PyrLK [Sparse]", WINDOW_NORMAL);
namedWindow("PyrLK [Dense] Flow Field", WINDOW_NORMAL);
#ifdef HAVE_OPENGL
namedWindow("PyrLK [Dense]", WINDOW_OPENGL);
setGlDevice();
#endif
cout << "Image size : " << frame0.cols << " x " << frame0.rows << endl;
cout << "Points count : " << points << endl;
@ -270,21 +229,6 @@ int main(int argc, const char* argv[])
imshow("PyrLK [Dense] Flow Field", flowField);
#ifdef HAVE_OPENGL
setOpenGlContext("PyrLK [Dense]");
GpuMat d_vertex, d_colors;
createOpticalFlowNeedleMap(d_u, d_v, d_vertex, d_colors);
DrawData drawData;
drawData.tex.copyFrom(d_frame0Gray);
drawData.arr.setVertexArray(d_vertex);
drawData.arr.setColorArray(d_colors, false);
setOpenGlDrawCallback("PyrLK [Dense]", drawCallback, &drawData);
#endif
waitKey();
return 0;

View File

@ -47,7 +47,7 @@ GpuMat d_result[2];
// CPU result
Mat result;
void printHelp()
static void printHelp()
{
std::cout << "Usage: stereo_multi_gpu --left <image> --right <image>\n";
}