提交 bd4239be 编写于 作者: V Vladislav Vinogradov

moved OpenGL wrappers to separate header

added GlBuffer, GlTexture and GpuMat support to InputArray
replaced addTextOpenGl function by render + GlFont
上级 59ea0d8e
......@@ -90,6 +90,11 @@ class Mat;
class SparseMat;
typedef Mat MatND;
class GlBuffer;
class GlTexture;
class GlArrays;
class GlCamera;
namespace gpu {
class GpuMat;
}
......@@ -1273,10 +1278,19 @@ protected:
class CV_EXPORTS _InputArray
{
public:
enum { KIND_SHIFT=16, NONE=0<<KIND_SHIFT, MAT=1<<KIND_SHIFT,
MATX=2<<KIND_SHIFT, STD_VECTOR=3<<KIND_SHIFT,
STD_VECTOR_VECTOR=4<<KIND_SHIFT,
STD_VECTOR_MAT=5<<KIND_SHIFT, EXPR=6<<KIND_SHIFT };
enum {
KIND_SHIFT = 16,
NONE = 0 << KIND_SHIFT,
MAT = 1 << KIND_SHIFT,
MATX = 2 << KIND_SHIFT,
STD_VECTOR = 3 << KIND_SHIFT,
STD_VECTOR_VECTOR = 4 << KIND_SHIFT,
STD_VECTOR_MAT = 5 << KIND_SHIFT,
EXPR = 6 << KIND_SHIFT,
OPENGL_BUFFER = 7 << KIND_SHIFT,
OPENGL_TEXTURE = 8 << KIND_SHIFT,
GPU_MAT = 9 << KIND_SHIFT
};
_InputArray();
_InputArray(const Mat& m);
_InputArray(const MatExpr& expr);
......@@ -1287,8 +1301,16 @@ public:
template<typename _Tp, int m, int n> _InputArray(const Matx<_Tp, m, n>& matx);
_InputArray(const Scalar& s);
_InputArray(const double& val);
_InputArray(const GlBuffer& buf);
_InputArray(const GlTexture& tex);
_InputArray(const gpu::GpuMat& d_mat);
virtual Mat getMat(int i=-1) const;
virtual void getMatVector(vector<Mat>& mv) const;
virtual GlBuffer getGlBuffer() const;
virtual GlTexture getGlTexture() const;
virtual gpu::GpuMat getGpuMat() const;
virtual int kind() const;
virtual Size size(int i=-1) const;
virtual size_t total(int i=-1) const;
......
......@@ -50,9 +50,6 @@
namespace cv { namespace gpu
{
////////////////////////////////////////////////////////////////////////
// GpuMat
//! Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat.
class CV_EXPORTS GpuMat
{
......@@ -217,286 +214,10 @@ namespace cv { namespace gpu
CV_EXPORTS void ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m);
CV_EXPORTS void ensureSizeIsEnough(Size size, int type, GpuMat& m);
////////////////////////////////////////////////////////////////////////
// OpenGL
//! set a CUDA device to use OpenGL interoperability
CV_EXPORTS void setGlDevice(int device = 0);
//! Smart pointer for OpenGL buffer memory with reference counting.
class CV_EXPORTS GlBuffer
{
public:
enum Usage
{
ARRAY_BUFFER = 0x8892, // buffer will use for OpenGL arrays (vertices, colors, normals, etc)
TEXTURE_BUFFER = 0x88EC // buffer will ise for OpenGL textures
};
//! create empty buffer
explicit GlBuffer(Usage usage);
//! create buffer
GlBuffer(int rows, int cols, int type, Usage usage);
GlBuffer(Size size, int type, Usage usage);
//! copy from host/device memory
GlBuffer(InputArray mat, Usage usage);
GlBuffer(const GpuMat& d_mat, Usage usage);
GlBuffer(const GlBuffer& other);
~GlBuffer();
GlBuffer& operator =(const GlBuffer& other);
void create(int rows, int cols, int type, Usage usage);
inline void create(Size size, int type, Usage usage) { create(size.height, size.width, type, usage); }
inline void create(int rows, int cols, int type) { create(rows, cols, type, usage()); }
inline void create(Size size, int type) { create(size.height, size.width, type, usage()); }
void release();
//! copy from host/device memory
void copyFrom(InputArray mat);
void copyFrom(const GpuMat& d_mat);
void bind() const;
void unbind() const;
//! map to host memory
Mat mapHost();
void unmapHost();
//! map to device memory
GpuMat mapDevice();
void unmapDevice();
int rows;
int cols;
inline Size size() const { return Size(cols, rows); }
inline 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_); }
inline Usage usage() const { return usage_; }
private:
int type_;
Usage usage_;
class Impl;
Ptr<Impl> impl_;
};
//! Smart pointer for OpenGL 2d texture memory with reference counting.
class CV_EXPORTS GlTexture
{
public:
//! create empty texture
GlTexture();
//! create texture
GlTexture(int rows, int cols, int type);
GlTexture(Size size, int type);
//! copy from host/device memory
explicit GlTexture(InputArray mat, bool bgra = true);
explicit GlTexture(const GlBuffer& buf, bool bgra = true);
GlTexture(const GlTexture& other);
~GlTexture();
GlTexture& operator =(const GlTexture& other);
void create(int rows, int cols, int type);
inline void create(Size size, int type) { create(size.height, size.width, type); }
void release();
//! copy from host/device memory
void copyFrom(InputArray mat, bool bgra = true);
void copyFrom(const GlBuffer& buf, bool bgra = true);
void bind() const;
void unbind() const;
int rows;
int cols;
inline Size size() const { return Size(cols, rows); }
inline 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_); }
private:
int type_;
class Impl;
Ptr<Impl> impl_;
};
//! 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)
{
}
void setVertexArray(const GlBuffer& vertex);
void setVertexArray(const GpuMat& vertex);
void setVertexArray(InputArray vertex);
inline void resetVertexArray() { vertex_.release(); }
void setColorArray(const GlBuffer& color, bool bgra = true);
void setColorArray(const GpuMat& color, bool bgra = true);
void setColorArray(InputArray color, bool bgra = true);
inline void resetColorArray() { color_.release(); }
void setNormalArray(const GlBuffer& normal);
void setNormalArray(const GpuMat& normal);
void setNormalArray(InputArray normal);
inline void resetNormalArray() { normal_.release(); }
void setTexCoordArray(const GlBuffer& texCoord);
void setTexCoordArray(const GpuMat& texCoord);
void setTexCoordArray(InputArray texCoord);
inline void resetTexCoordArray() { texCoord_.release(); }
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(); }
private:
GlBuffer vertex_;
GlBuffer color_;
bool bgra_;
GlBuffer normal_;
GlBuffer texCoord_;
};
//! render functions
//! render texture rectangle in window
CV_EXPORTS void render(const GlTexture& tex,
Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
//! render mode
namespace RenderMode {
enum {
POINTS = 0x0000,
LINES = 0x0001,
LINE_LOOP = 0x0002,
LINE_STRIP = 0x0003,
TRIANGLES = 0x0004,
TRIANGLE_STRIP = 0x0005,
TRIANGLE_FAN = 0x0006,
QUADS = 0x0007,
QUAD_STRIP = 0x0008,
POLYGON = 0x0009
};
}
//! render OpenGL arrays
CV_EXPORTS void render(const GlArrays& arr, int mode = RenderMode::POINTS);
//! 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_;
};
//! OpenGL extension table
class CV_EXPORTS GlFuncTab
{
public:
virtual ~GlFuncTab();
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 bool isGlContextInitialized() const = 0;
};
CV_EXPORTS void setGlFuncTab(const GlFuncTab* tab);
////////////////////////////////////////////////////////////////////////
// Error handling
CV_EXPORTS void error(const char* error_string, const char* file, const int line, const char* func = "");
CV_EXPORTS bool checkGlError(const char* file, const int line, const char* func = "");
#if defined(__GNUC__)
#define CV_CheckGlError() CV_DbgAssert( (cv::gpu::checkGlError(__FILE__, __LINE__, __func__)) )
#else
#define CV_CheckGlError() CV_DbgAssert( (cv::gpu::checkGlError(__FILE__, __LINE__)) )
#endif
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
......
......@@ -708,4 +708,36 @@ CvBigFuncTable;
(tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG; \
(tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG
//! OpenGL extension table
class CV_EXPORTS CvOpenGlFuncTab
{
public:
virtual ~CvOpenGlFuncTab();
virtual void genBuffers(int n, unsigned int* buffers) const = 0;
virtual void deleteBuffers(int n, const unsigned int* buffers) const = 0;
virtual void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const = 0;
virtual void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const = 0;
virtual void bindBuffer(unsigned int target, unsigned int buffer) const = 0;
virtual void* mapBuffer(unsigned int target, unsigned int access) const = 0;
virtual void unmapBuffer(unsigned int target) const = 0;
virtual void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const = 0;
virtual bool isGlContextInitialized() const = 0;
};
CV_EXPORTS void icvSetOpenGlFuncTab(const CvOpenGlFuncTab* tab);
CV_EXPORTS bool icvCheckGlError(const char* file, const int line, const char* func = "");
#if defined(__GNUC__)
#define CV_CheckGlError() CV_DbgAssert( (::icvCheckGlError(__FILE__, __LINE__, __func__)) )
#else
#define CV_CheckGlError() CV_DbgAssert( (::icvCheckGlError(__FILE__, __LINE__)) )
#endif
#endif
/*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.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., 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 GpuMaterials provided with the distribution.
//
// * The name of the copyright holders 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*/
#ifndef __OPENCV_OPENGL_INTEROP_HPP__
#define __OPENCV_OPENGL_INTEROP_HPP__
#ifdef __cplusplus
#include "opencv2/core/core.hpp"
namespace cv
{
//! Smart pointer for OpenGL buffer memory with reference counting.
class CV_EXPORTS GlBuffer
{
public:
enum Usage
{
ARRAY_BUFFER = 0x8892, // buffer will use for OpenGL arrays (vertices, colors, normals, etc)
TEXTURE_BUFFER = 0x88EC // buffer will ise for OpenGL textures
};
//! create empty buffer
explicit GlBuffer(Usage usage);
//! create buffer
GlBuffer(int rows, int cols, int type, Usage usage);
GlBuffer(Size size, int type, Usage usage);
//! copy from host/device memory
GlBuffer(InputArray mat, Usage usage);
void create(int rows, int cols, int type, Usage usage);
inline void create(Size size, int type, Usage usage) { create(size.height, size.width, type, usage); }
inline void create(int rows, int cols, int type) { create(rows, cols, type, usage()); }
inline void create(Size size, int type) { create(size.height, size.width, type, usage()); }
void release();
//! copy from host/device memory
void copyFrom(InputArray mat);
void bind() const;
void unbind() const;
//! map to host memory
Mat mapHost();
void unmapHost();
//! map to device memory
gpu::GpuMat mapDevice();
void unmapDevice();
int rows;
int cols;
inline Size size() const { return Size(cols, rows); }
inline 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_); }
inline Usage usage() const { return usage_; }
private:
int type_;
Usage usage_;
class Impl;
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
{
public:
//! create empty texture
GlTexture();
//! create texture
GlTexture(int rows, int cols, int type);
GlTexture(Size size, int type);
//! copy from host/device memory
explicit GlTexture(InputArray mat, bool bgra = true);
void create(int rows, int cols, int type);
inline void create(Size size, int type) { create(size.height, size.width, type); }
void release();
//! copy from host/device memory
void copyFrom(InputArray mat, bool bgra = true);
void bind() const;
void unbind() const;
int rows;
int cols;
inline Size size() const { return Size(cols, rows); }
inline 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_); }
private:
int type_;
class Impl;
Ptr<Impl> impl_;
};
template <> CV_EXPORTS void Ptr<GlTexture::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)
{
}
void setVertexArray(InputArray vertex);
inline void resetVertexArray() { vertex_.release(); }
void setColorArray(InputArray color, bool bgra = true);
inline void resetColorArray() { color_.release(); }
void setNormalArray(InputArray normal);
inline void resetNormalArray() { normal_.release(); }
void setTexCoordArray(InputArray texCoord);
inline void resetTexCoordArray() { texCoord_.release(); }
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(); }
private:
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, int 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 texture rectangle in window
CV_EXPORTS void render(const GlTexture& tex,
Rect_<double> wndRect = Rect_<double>(0.0, 0.0, 1.0, 1.0),
Rect_<double> texRect = Rect_<double>(0.0, 0.0, 1.0, 1.0));
//! render mode
namespace RenderMode {
enum {
POINTS = 0x0000,
LINES = 0x0001,
LINE_LOOP = 0x0002,
LINE_STRIP = 0x0003,
TRIANGLES = 0x0004,
TRIANGLE_STRIP = 0x0005,
TRIANGLE_FAN = 0x0006,
QUADS = 0x0007,
QUAD_STRIP = 0x0008,
POLYGON = 0x0009
};
}
//! render OpenGL arrays
CV_EXPORTS void render(const GlArrays& arr, int mode = RenderMode::POINTS);
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_;
};
namespace gpu
{
//! set a CUDA device to use OpenGL interoperability
CV_EXPORTS void setGlDevice(int device = 0);
}
} // namespace cv
#endif // __cplusplus
#endif // __OPENCV_OPENGL_INTEROP_HPP__
此差异已折叠。
......@@ -41,6 +41,8 @@
//M*/
#include "precomp.hpp"
#include "opencv2/core/gpumat.hpp"
#include "opencv2/core/opengl_interop.hpp"
/****************************************************************************************\
* [scaled] Identity matrix initialization *
......@@ -873,6 +875,9 @@ _InputArray::_InputArray(const Mat& m) : flags(MAT), obj((void*)&m) {}
_InputArray::_InputArray(const vector<Mat>& vec) : flags(STD_VECTOR_MAT), obj((void*)&vec) {}
_InputArray::_InputArray(const double& val) : flags(MATX+CV_64F), obj((void*)&val), sz(Size(1,1)) {}
_InputArray::_InputArray(const MatExpr& expr) : flags(EXPR), obj((void*)&expr) {}
_InputArray::_InputArray(const GlBuffer& buf) : flags(OPENGL_BUFFER), obj((void*)&buf) {}
_InputArray::_InputArray(const GlTexture& tex) : flags(OPENGL_TEXTURE), obj((void*)&tex) {}
_InputArray::_InputArray(const gpu::GpuMat& d_mat) : flags(GPU_MAT), obj((void*)&d_mat) {}
Mat _InputArray::getMat(int i) const
{
......@@ -1011,6 +1016,42 @@ void _InputArray::getMatVector(vector<Mat>& mv) const
return;
}
}
GlBuffer _InputArray::getGlBuffer() const
{
int k = kind();
CV_Assert(k == OPENGL_BUFFER);
//if( k == OPENGL_BUFFER )
{
const GlBuffer* buf = (const GlBuffer*)obj;
return *buf;
}
}
GlTexture _InputArray::getGlTexture() const
{
int k = kind();
CV_Assert(k == OPENGL_TEXTURE);
//if( k == OPENGL_TEXTURE )
{
const GlTexture* tex = (const GlTexture*)obj;
return *tex;
}
}
gpu::GpuMat _InputArray::getGpuMat() const
{
int k = kind();
CV_Assert(k == GPU_MAT);
//if( k == GPU_MAT )
{
const gpu::GpuMat* d_mat = (const gpu::GpuMat*)obj;
return *d_mat;
}
}
int _InputArray::kind() const
{
......@@ -1063,8 +1104,7 @@ Size _InputArray::size(int i) const
return szb == szi ? Size((int)szb, 1) : Size((int)(szb/CV_ELEM_SIZE(flags)), 1);
}
CV_Assert( k == STD_VECTOR_MAT );
//if( k == STD_VECTOR_MAT )
if( k == STD_VECTOR_MAT )
{
const vector<Mat>& vv = *(const vector<Mat>*)obj;
if( i < 0 )
......@@ -1073,6 +1113,28 @@ Size _InputArray::size(int i) const
return vv[i].size();
}
if( k == OPENGL_BUFFER )
{
CV_Assert( i < 0 );
const GlBuffer* buf = (const GlBuffer*)obj;
return buf->size();
}
if( k == OPENGL_TEXTURE )
{
CV_Assert( i < 0 );
const GlTexture* tex = (const GlTexture*)obj;
return tex->size();
}
CV_Assert( k == GPU_MAT );
//if( k == GPU_MAT )
{
CV_Assert( i < 0 );
const gpu::GpuMat* d_mat = (const gpu::GpuMat*)obj;
return d_mat->size();
}
}
size_t _InputArray::total(int i) const
......@@ -1096,14 +1158,23 @@ int _InputArray::type(int i) const
if( k == NONE )
return -1;
CV_Assert( k == STD_VECTOR_MAT );
//if( k == STD_VECTOR_MAT )
if( k == STD_VECTOR_MAT )
{
const vector<Mat>& vv = *(const vector<Mat>*)obj;
CV_Assert( i < (int)vv.size() );
return vv[i >= 0 ? i : 0].type();
}
if( k == OPENGL_BUFFER )
return ((const GlBuffer*)obj)->type();
if( k == OPENGL_TEXTURE )
return ((const GlTexture*)obj)->type();
CV_Assert( k == GPU_MAT );
//if( k == GPU_MAT )
return ((const gpu::GpuMat*)obj)->type();
}
int _InputArray::depth(int i) const
......@@ -1144,12 +1215,21 @@ bool _InputArray::empty() const
return vv.empty();
}
CV_Assert( k == STD_VECTOR_MAT );
//if( k == STD_VECTOR_MAT )
if( k == STD_VECTOR_MAT )
{
const vector<Mat>& vv = *(const vector<Mat>*)obj;
return vv.empty();
}
if( k == OPENGL_BUFFER )
return ((const GlBuffer*)obj)->empty();
if( k == OPENGL_TEXTURE )
return ((const GlTexture*)obj)->empty();
CV_Assert( k == GPU_MAT );
//if( k == GPU_MAT )
return ((const gpu::GpuMat*)obj)->empty();
}
......
此差异已折叠。
......@@ -44,7 +44,6 @@
#define __OPENCV_HIGHGUI_HPP__
#include "opencv2/core/core.hpp"
#include "opencv2/core/gpumat.hpp"
#include "opencv2/highgui/highgui_c.h"
#ifdef __cplusplus
......@@ -129,7 +128,7 @@ CV_EXPORTS_W void setTrackbarPos(const string& trackbarname, const string& winna
typedef void (CV_CDECL *OpenGLCallback)(void* userdata);
CV_EXPORTS void createOpenGLCallback(const string& winname, OpenGLCallback onOpenGlDraw, void* userdata = 0);
typedef void (CV_CDECL *OpenGlDrawCallback)(void* userdata);
typedef void (*OpenGlDrawCallback)(void* userdata);
static inline void setOpenGlDrawCallback(const string& winname, OpenGlDrawCallback onOpenGlDraw, void* userdata = 0)
{
createOpenGLCallback(winname, onOpenGlDraw, userdata);
......@@ -139,22 +138,8 @@ CV_EXPORTS void setOpenGlContext(const string& winname);
CV_EXPORTS void updateWindow(const string& winname);
CV_EXPORTS void imshow(const string& winname, const gpu::GlTexture& tex);
CV_EXPORTS void imshow(const string& winname, const gpu::GlBuffer& buf);
CV_EXPORTS void imshow(const string& winname, const gpu::GpuMat& d_mat);
CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlArrays& arr);
CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlBuffer& points,
const gpu::GlBuffer& colors = gpu::GlBuffer(gpu::GlBuffer::ARRAY_BUFFER));
CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GpuMat& points,
const gpu::GpuMat& colors = gpu::GpuMat());
CV_EXPORTS void pointCloudShow(const string& winname, const gpu::GlCamera& camera, InputArray points,
InputArray colors = noArray());
CV_EXPORTS void addTextOpenGl(const string& winname, const string& text, Point org, Scalar color = Scalar::all(255),
const string& fontName = "Courier New", int fontHeight = 12,
int fontWeight = CV_FONT_NORMAL, int fontStyle = CV_STYLE_NORMAL);
CV_EXPORTS void clearTextOpenGl(const string& winname);
CV_EXPORTS void pointCloudShow(const string& winname, const GlCamera& camera, const GlArrays& arr);
CV_EXPORTS void pointCloudShow(const string& winname, const GlCamera& camera, InputArray points, InputArray colors = noArray());
//Only for Qt
......
......@@ -63,8 +63,7 @@ enum { CV_FONT_LIGHT = 25,//QFont::Light,
enum { CV_STYLE_NORMAL = 0,//QFont::StyleNormal,
CV_STYLE_ITALIC = 1,//QFont::StyleItalic,
CV_STYLE_OBLIQUE = 2,//QFont::StyleOblique
CV_STYLE_UNDERLINE = 4
CV_STYLE_OBLIQUE = 2 //QFont::StyleOblique
};
/* ---------*/
......@@ -258,11 +257,6 @@ CVAPI(void) cvCreateOpenGLCallback( const char* window_name, CvOpenGLCallback ca
CVAPI(void) cvSetOpenGlContext(const char* window_name);
CVAPI(void) cvUpdateWindow(const char* window_name);
CVAPI(void) cvAddTextOpenGl(const char* winname, const char* text, CvPoint org, CvScalar color CV_DEFAULT(cvScalar(255.0, 255.0, 255.0, 255.0)),
const char* fontName CV_DEFAULT("Courier New"), int fontHeight CV_DEFAULT(12),
int fontWeight CV_DEFAULT(CV_FONT_NORMAL), int fontStyle CV_DEFAULT(CV_STYLE_NORMAL));
CVAPI(void) cvClearTextOpenGl(const char* winname);
/****************************************************************************************\
* Working with Video Files and Cameras *
......
......@@ -40,6 +40,7 @@
//M*/
#include "precomp.hpp"
#include "opencv2/core/opengl_interop.hpp"
// in later times, use this file as a dispatcher to implementations like cvcap.cpp
......@@ -284,7 +285,7 @@ namespace
struct GlObjTex : GlObjBase
{
cv::gpu::GlTexture tex;
cv::GlTexture tex;
};
void CV_CDECL glDrawTextureCallback(void* userdata)
......@@ -293,17 +294,17 @@ namespace
CV_DbgAssert(texObj->flag == CV_TEXTURE_MAGIC_VAL);
static cv::gpu::GlCamera glCamera;
static cv::GlCamera glCamera;
glCamera.setupProjectionMatrix();
cv::gpu::render(texObj->tex);
cv::render(texObj->tex);
}
struct GlObjPointCloud : GlObjBase
{
cv::gpu::GlArrays arr;
cv::gpu::GlCamera camera;
cv::GlArrays arr;
cv::GlCamera camera;
};
void CV_CDECL glDrawPointCloudCallback(void* userdata)
......@@ -315,7 +316,7 @@ namespace
pointCloudObj->camera.setupProjectionMatrix();
pointCloudObj->camera.setupModelViewMatrix();
cv::gpu::render(pointCloudObj->arr);
cv::render(pointCloudObj->arr);
}
void CV_CDECL glCleanCallback(void* userdata)
......@@ -327,20 +328,31 @@ namespace
}
#endif // HAVE_OPENGL
#ifdef HAVE_OPENGL
namespace
void cv::imshow( const string& winname, InputArray _img )
{
template <typename T> void imshowImpl(const std::string& winname, const T& img)
#ifndef HAVE_OPENGL
Mat img = _img.getMat();
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
#else
double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
if (useGl <= 0)
{
Mat img = _img.getMat();
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
}
else
{
using namespace cv;
namedWindow(winname, WINDOW_OPENGL | WINDOW_AUTOSIZE);
double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
if (autoSize > 0)
resizeWindow(winname, img.cols, img.rows);
{
Size size = _img.size();
resizeWindow(winname, size.width, size.height);
}
setOpenGlContext(winname);
......@@ -355,12 +367,12 @@ namespace
if (glObj)
{
GlObjTex* texObj = static_cast<GlObjTex*>(glObj);
texObj->tex.copyFrom(img);
texObj->tex.copyFrom(_img);
}
else
{
GlObjTex* texObj = new GlObjTex;
texObj->tex.copyFrom(img);
texObj->tex.copyFrom(_img);
glObj = texObj;
glObj->flag = CV_TEXTURE_MAGIC_VAL;
......@@ -375,68 +387,21 @@ namespace
updateWindow(winname);
}
}
#endif // HAVE_OPENGL
void cv::imshow( const string& winname, InputArray _img )
{
Mat img = _img.getMat();
#ifndef HAVE_OPENGL
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
#else
double useGl = getWindowProperty(winname, WND_PROP_OPENGL);
if (useGl <= 0)
{
CvMat c_img = img;
cvShowImage(winname.c_str(), &c_img);
}
else
{
imshowImpl(winname, img);
}
#endif
}
void cv::imshow(const string& winname, const gpu::GlBuffer& buf)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
imshowImpl(winname, buf);
#endif
}
void cv::imshow(const string& winname, const gpu::GpuMat& d_mat)
void cv::pointCloudShow(const string& winname, const GlCamera& camera, const GlArrays& arr)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
setOpenGlContext(winname);
gpu::GlBuffer buf(d_mat, gpu::GlBuffer::TEXTURE_BUFFER);
imshow(winname, buf);
#endif
}
void cv::imshow(const string& winname, const gpu::GlTexture& tex)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
namedWindow(winname, WINDOW_OPENGL | WINDOW_AUTOSIZE);
double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE);
if (autoSize > 0)
resizeWindow(winname, tex.cols, tex.rows);
namedWindow(winname, WINDOW_OPENGL);
setOpenGlContext(winname);
GlObjBase* glObj = findGlObjByName(winname);
if (glObj && glObj->flag != CV_TEXTURE_MAGIC_VAL)
if (glObj && glObj->flag != CV_POINT_CLOUD_MAGIC_VAL)
{
icvSetOpenGlCleanCallback(winname.c_str(), 0, 0);
glObj = 0;
......@@ -444,16 +409,18 @@ void cv::imshow(const string& winname, const gpu::GlTexture& tex)
if (glObj)
{
GlObjTex* texObj = static_cast<GlObjTex*>(glObj);
texObj->tex = tex;
GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(glObj);
pointCloudObj->arr = arr;
pointCloudObj->camera = camera;
}
else
{
GlObjTex* texObj = new GlObjTex;
texObj->tex = tex;
GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
pointCloudObj->arr = arr;
pointCloudObj->camera = camera;
glObj = texObj;
glObj->flag = CV_TEXTURE_MAGIC_VAL;
glObj = pointCloudObj;
glObj->flag = CV_POINT_CLOUD_MAGIC_VAL;
glObj->winname = winname;
addGlObj(glObj);
......@@ -461,13 +428,13 @@ void cv::imshow(const string& winname, const gpu::GlTexture& tex)
icvSetOpenGlCleanCallback(winname.c_str(), glCleanCallback, glObj);
}
setOpenGlDrawCallback(winname, glDrawTextureCallback, glObj);
setOpenGlDrawCallback(winname, glDrawPointCloudCallback, glObj);
updateWindow(winname);
#endif
}
void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlArrays& arr)
void cv::pointCloudShow(const std::string& winname, const cv::GlCamera& camera, InputArray points, InputArray colors)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
......@@ -487,13 +454,23 @@ void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, cons
if (glObj)
{
GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(glObj);
pointCloudObj->arr = arr;
pointCloudObj->arr.setVertexArray(points);
if (colors.empty())
pointCloudObj->arr.resetColorArray();
else
pointCloudObj->arr.setColorArray(colors);
pointCloudObj->camera = camera;
}
else
{
GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
pointCloudObj->arr = arr;
pointCloudObj->arr.setVertexArray(points);
if (!colors.empty())
pointCloudObj->arr.setColorArray(colors);
pointCloudObj->camera = camera;
glObj = pointCloudObj;
......@@ -511,104 +488,6 @@ void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, cons
#endif
}
#ifdef HAVE_OPENGL
namespace
{
template <typename T> void pointCloudShowImpl(const std::string& winname, const cv::gpu::GlCamera& camera, const T& points, const T& colors)
{
using namespace cv;
namedWindow(winname, WINDOW_OPENGL);
setOpenGlContext(winname);
GlObjBase* glObj = findGlObjByName(winname);
if (glObj && glObj->flag != CV_POINT_CLOUD_MAGIC_VAL)
{
icvSetOpenGlCleanCallback(winname.c_str(), 0, 0);
glObj = 0;
}
if (glObj)
{
GlObjPointCloud* pointCloudObj = static_cast<GlObjPointCloud*>(glObj);
pointCloudObj->arr.setVertexArray(points);
if (colors.empty())
pointCloudObj->arr.resetColorArray();
else
pointCloudObj->arr.setColorArray(colors);
pointCloudObj->camera = camera;
}
else
{
GlObjPointCloud* pointCloudObj = new GlObjPointCloud;
pointCloudObj->arr.setVertexArray(points);
if (!colors.empty())
pointCloudObj->arr.setColorArray(colors);
pointCloudObj->camera = camera;
glObj = pointCloudObj;
glObj->flag = CV_POINT_CLOUD_MAGIC_VAL;
glObj->winname = winname;
addGlObj(glObj);
icvSetOpenGlCleanCallback(winname.c_str(), glCleanCallback, glObj);
}
setOpenGlDrawCallback(winname, glDrawPointCloudCallback, glObj);
updateWindow(winname);
}
}
#endif // HAVE_OPENGL
void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GlBuffer& points, const gpu::GlBuffer& colors)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
pointCloudShowImpl(winname, camera, points, colors);
#endif
}
void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, const gpu::GpuMat& points, const gpu::GpuMat& colors)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
pointCloudShowImpl(winname, camera, points, colors);
#endif
}
void cv::pointCloudShow(const string& winname, const gpu::GlCamera& camera, InputArray points, InputArray colors)
{
#ifndef HAVE_OPENGL
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
#else
pointCloudShowImpl(winname, camera, points, colors);
#endif
}
// OpenGL text
void cv::addTextOpenGl(const string& winname, const string& text, Point org, Scalar color, const string& fontName, int fontHeight, int fontWeight, int fontStyle)
{
cvAddTextOpenGl(winname.c_str(), text.c_str(), org, color, fontName.c_str(), fontHeight, fontWeight, fontStyle);
}
void cv::clearTextOpenGl(const string& winname)
{
cvClearTextOpenGl(winname.c_str());
}
// Without OpenGL
#ifndef HAVE_OPENGL
......@@ -630,16 +509,6 @@ CV_IMPL void cvUpdateWindow(const char*)
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
}
CV_IMPL void cvAddTextOpenGl(const char*, const char*, CvPoint, CvScalar, const char*, int, int, int)
{
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
}
CV_IMPL void cvClearTextOpenGl(const char*)
{
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
}
void icvSetOpenGlCleanCallback(const char*, CvOpenGlCleanCallback, void*)
{
CV_Error(CV_OpenGlNotSupported, "The library is compiled without OpenGL support");
......
......@@ -60,7 +60,6 @@
#include <vector>
#include <functional>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/gpumat.hpp"
#include <GL\gl.h>
#endif
......@@ -137,216 +136,6 @@ typedef struct CvTrackbar
}
CvTrackbar;
// OpenGL support
#ifdef HAVE_OPENGL
namespace
{
class OpenGlFont
{
public:
OpenGlFont(HDC hDC, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle);
~OpenGlFont();
void draw(const char* str, int len, CvPoint org, CvScalar color, int width, int height) const;
inline const std::string& fontName() const { return fontName_; }
inline int fontHeight() const { return fontHeight_; }
inline int fontWeight() const { return fontWeight_; }
inline int fontStyle() const { return fontStyle_; }
private:
std::string fontName_;
int fontHeight_;
int fontWeight_;
int fontStyle_;
GLuint base_;
OpenGlFont(const OpenGlFont&);
OpenGlFont& operator =(const OpenGlFont&);
};
int getFontWidthW32(int fontWeight)
{
int weight = 0;
switch(fontWeight)
{
case CV_FONT_LIGHT:
weight = 200;
break;
case CV_FONT_NORMAL:
weight = FW_NORMAL;
break;
case CV_FONT_DEMIBOLD:
weight = 550;
break;
case CV_FONT_BOLD:
weight = FW_BOLD;
break;
case CV_FONT_BLACK:
weight = FW_BLACK;
break;
default:
cvError(CV_StsBadArg, "getFontWidthW32", "Unsopported font width", __FILE__, __LINE__);
};
return weight;
}
OpenGlFont::OpenGlFont(HDC hDC, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle)
: fontName_(), fontHeight_(0), fontWeight_(0), fontStyle_(0), base_(0)
{
base_ = glGenLists(96);
HFONT font = CreateFont
(
-fontHeight, // height
0, // cell width
0, // Angle of Escapement
0, // Orientation Angle
getFontWidthW32(fontWeight), // font weight
fontStyle & CV_STYLE_ITALIC ? TRUE : FALSE, // Italic
fontStyle & CV_STYLE_UNDERLINE ? TRUE : FALSE, // Underline
FALSE, // StrikeOut
ANSI_CHARSET, // CharSet
OUT_TT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
ANTIALIASED_QUALITY, // Quality
FF_DONTCARE | DEFAULT_PITCH, // PitchAndFamily
fontName.c_str() // FaceName
);
SelectObject(hDC, font);
if (!wglUseFontBitmaps(hDC, 32, 96, base_))
cvError(CV_OpenGlApiCallError, "OpenGlFont", "Can't create font", __FILE__, __LINE__);
fontName_ = fontName;
fontHeight_ = fontHeight;
fontWeight_ = fontWeight;
fontStyle_ = fontStyle;
}
OpenGlFont::~OpenGlFont()
{
if (base_)
glDeleteLists(base_, 96);
}
void OpenGlFont::draw(const char* str, int len, CvPoint org, CvScalar color, int width, int height) const
{
if (base_)
{
glPushAttrib(GL_LIST_BIT);
glListBase(base_ - 32);
glColor4dv(color.val);
glRasterPos2f(static_cast<float>(org.x) / width, static_cast<float>((org.y + fontHeight_)) / height);
glCallLists(len, GL_UNSIGNED_BYTE, str);
glPopAttrib();
CV_CheckGlError();
}
}
class OpenGlText
{
public:
OpenGlText(HDC hDC);
void add(const std::string& text, CvPoint org, CvScalar color, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle);
inline void clear() { text_.clear(); }
void draw(int width, int height) const;
private:
struct Text
{
std::string str;
CvPoint org;
CvScalar color;
cv::Ptr<OpenGlFont> font;
};
HDC hDC_;
std::vector< cv::Ptr<OpenGlFont> > fonts_;
std::vector<Text> text_;
};
OpenGlText::OpenGlText(HDC hDC) : hDC_(hDC)
{
fonts_.reserve(5);
text_.reserve(5);
}
class FontCompare : public std::unary_function<cv::Ptr<OpenGlFont>, bool>
{
public:
inline FontCompare(const std::string& fontName, int fontHeight, int fontWeight, int fontStyle)
: fontName_(fontName), fontHeight_(fontHeight), fontWeight_(fontWeight), fontStyle_(fontStyle)
{
}
bool operator ()(const cv::Ptr<OpenGlFont>& font)
{
return font->fontName() == fontName_ && font->fontHeight() == fontHeight_ && font->fontWeight() == fontWeight_ && font->fontStyle() == fontStyle_;
}
private:
std::string fontName_;
int fontHeight_;
int fontWeight_;
int fontStyle_;
};
void OpenGlText::add(const std::string& str, CvPoint org, CvScalar color, const std::string& fontName, int fontHeight, int fontWeight, int fontStyle)
{
std::vector< cv::Ptr<OpenGlFont> >::iterator fontIt =
std::find_if(fonts_.begin(), fonts_.end(), FontCompare(fontName, fontHeight, fontWeight, fontStyle));
if (fontIt == fonts_.end())
{
fonts_.push_back(new OpenGlFont(hDC_, fontName, fontHeight, fontWeight, fontStyle));
fontIt = fonts_.end() - 1;
}
Text text;
text.str = str;
text.org = org;
text.color = color;
text.font = *fontIt;
text_.push_back(text);
}
void OpenGlText::draw(int width, int height) const
{
glDisable(GL_DEPTH_TEST);
static cv::gpu::GlCamera glCamera;
glCamera.setupProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for (size_t i = 0, size = text_.size(); i < size; ++i)
{
const Text& text = text_[i];
text.font->draw(text.str.c_str(), text.str.length(), text.org, text.color, width, height);
}
}
}
#endif // HAVE_OPENGL
typedef struct CvWindow
{
......@@ -391,9 +180,7 @@ typedef struct CvWindow
CvOpenGlCleanCallback glCleanCallback;
void* glCleanData;
cv::gpu::GlFuncTab* glFuncTab;
OpenGlText* glText;
CvOpenGlFuncTab* glFuncTab;
#endif
}
CvWindow;
......@@ -809,9 +596,26 @@ namespace
typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERPROC) (GLenum target, GLenum access);
typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERPROC) (GLenum target);
class GlFuncTab_W32 : public cv::gpu::GlFuncTab
class GlFuncTab_W32 : public CvOpenGlFuncTab
{
public:
GlFuncTab_W32(HDC hDC);
void genBuffers(int n, unsigned int* buffers) const;
void deleteBuffers(int n, const unsigned int* buffers) const;
void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const;
void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const;
void bindBuffer(unsigned int target, unsigned int buffer) const;
void* mapBuffer(unsigned int target, unsigned int access) const;
void unmapBuffer(unsigned int target) const;
void generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const;
bool isGlContextInitialized() const;
PFNGLGENBUFFERSPROC glGenBuffersExt;
PFNGLDELETEBUFFERSPROC glDeleteBuffersExt;
......@@ -825,142 +629,180 @@ namespace
bool initialized;
GlFuncTab_W32()
{
glGenBuffersExt = 0;
glDeleteBuffersExt = 0;
HDC hDC;
};
glBufferDataExt = 0;
glBufferSubDataExt = 0;
GlFuncTab_W32::GlFuncTab_W32(HDC hDC_)
{
glGenBuffersExt = 0;
glDeleteBuffersExt = 0;
glBindBufferExt = 0;
glBufferDataExt = 0;
glBufferSubDataExt = 0;
glMapBufferExt = 0;
glUnmapBufferExt = 0;
glBindBufferExt = 0;
initialized = false;
}
glMapBufferExt = 0;
glUnmapBufferExt = 0;
void genBuffers(int n, unsigned int* buffers) const
{
CV_FUNCNAME( "genBuffers" );
initialized = false;
__BEGIN__;
hDC = hDC_;
}
if (!glGenBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void GlFuncTab_W32::genBuffers(int n, unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_W32::genBuffers" );
glGenBuffersExt(n, buffers);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glGenBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void deleteBuffers(int n, const unsigned int* buffers) const
{
CV_FUNCNAME( "deleteBuffers" );
glGenBuffersExt(n, buffers);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glDeleteBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void GlFuncTab_W32::deleteBuffers(int n, const unsigned int* buffers) const
{
CV_FUNCNAME( "GlFuncTab_W32::deleteBuffers" );
glDeleteBuffersExt(n, buffers);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glDeleteBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
{
CV_FUNCNAME( "bufferData" );
glDeleteBuffersExt(n, buffers);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glBufferDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void GlFuncTab_W32::bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
{
CV_FUNCNAME( "GlFuncTab_W32::bufferData" );
glBufferDataExt(target, size, data, usage);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glBufferDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
{
CV_FUNCNAME( "bufferSubData" );
glBufferDataExt(target, size, data, usage);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glBufferSubDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void GlFuncTab_W32::bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
{
CV_FUNCNAME( "GlFuncTab_W32::bufferSubData" );
glBufferSubDataExt(target, offset, size, data);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glBufferSubDataExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void bindBuffer(unsigned int target, unsigned int buffer) const
{
CV_FUNCNAME( "bindBuffer" );
glBufferSubDataExt(target, offset, size, data);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glBindBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void GlFuncTab_W32::bindBuffer(unsigned int target, unsigned int buffer) const
{
CV_FUNCNAME( "GlFuncTab_W32::bindBuffer" );
glBindBufferExt(target, buffer);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glBindBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void* mapBuffer(unsigned int target, unsigned int access) const
{
CV_FUNCNAME( "mapBuffer" );
glBindBufferExt(target, buffer);
CV_CheckGlError();
void* res = 0;
__END__;
}
__BEGIN__;
void* GlFuncTab_W32::mapBuffer(unsigned int target, unsigned int access) const
{
CV_FUNCNAME( "GlFuncTab_W32::mapBuffer" );
if (!glMapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void* res = 0;
res = glMapBufferExt(target, access);
CV_CheckGlError();
__BEGIN__;
__END__;
if (!glMapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
return res;
}
res = glMapBufferExt(target, access);
CV_CheckGlError();
void unmapBuffer(unsigned int target) const
{
CV_FUNCNAME( "unmapBuffer" );
__END__;
__BEGIN__;
return res;
}
if (!glUnmapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
void GlFuncTab_W32::unmapBuffer(unsigned int target) const
{
CV_FUNCNAME( "GlFuncTab_W32::unmapBuffer" );
glUnmapBufferExt(target);
CV_CheckGlError();
__BEGIN__;
__END__;
}
if (!glUnmapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
bool isGlContextInitialized() const
{
return initialized;
}
};
glUnmapBufferExt(target);
CV_CheckGlError();
__END__;
}
void GlFuncTab_W32::generateBitmapFont(const std::string& family, int height, int weight, bool italic, bool underline, int start, int count, int base) const
{
HFONT font;
CV_FUNCNAME( "GlFuncTab_W32::generateBitmapFont" );
__BEGIN__;
font = CreateFont
(
-height, // height
0, // cell width
0, // Angle of Escapement
0, // Orientation Angle
weight, // font weight
italic ? TRUE : FALSE, // Italic
underline ? TRUE : FALSE, // Underline
FALSE, // StrikeOut
ANSI_CHARSET, // CharSet
OUT_TT_PRECIS, // OutPrecision
CLIP_DEFAULT_PRECIS, // ClipPrecision
ANTIALIASED_QUALITY, // Quality
FF_DONTCARE | DEFAULT_PITCH, // PitchAndFamily
family.c_str() // FaceName
);
SelectObject(hDC, font);
if (!wglUseFontBitmaps(hDC, start, count, base))
CV_ERROR(CV_OpenGlApiCallError, "Can't create font");
__END__;
}
bool GlFuncTab_W32::isGlContextInitialized() const
{
return initialized;
}
void initGl(CvWindow* window)
{
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
std::auto_ptr<GlFuncTab_W32> glFuncTab(new GlFuncTab_W32);
std::auto_ptr<GlFuncTab_W32> glFuncTab(new GlFuncTab_W32(window->dc));
// Load extensions
PROC func;
......@@ -990,7 +832,7 @@ namespace
window->glFuncTab = glFuncTab.release();
cv::gpu::setGlFuncTab(window->glFuncTab);
icvSetOpenGlFuncTab(window->glFuncTab);
}
void createGlContext(HWND hWnd, HDC& hGLDC, HGLRC& hGLRC, bool& useGl)
......@@ -1089,11 +931,6 @@ namespace
CV_CheckGlError();
if (window->glText)
window->glText->draw(window->width, window->height);
CV_CheckGlError();
if (!SwapBuffers(window->dc))
CV_ERROR( CV_OpenGlApiCallError, "Can't swap OpenGL buffers" );
......@@ -1209,8 +1046,6 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
window->glCleanCallback = 0;
window->glCleanData = 0;
window->glText = 0;
#endif
window->last_key = 0;
......@@ -1261,68 +1096,7 @@ CV_IMPL void cvSetOpenGlContext(const char* name)
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
cv::gpu::setGlFuncTab(window->glFuncTab);
__END__;
}
CV_IMPL void cvAddTextOpenGl(const char* name, const char* text, CvPoint org, CvScalar color, const char* fontName, int fontHeight, int fontWeight, int fontStyle)
{
CV_FUNCNAME( "cvAddTextOpenGl" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
CV_ERROR( CV_StsNullPtr, "NULL window" );
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
if (!window->glText)
window->glText = new OpenGlText(window->dc);
window->glText->add(text, org, color, fontName, fontHeight, fontWeight, fontStyle);
InvalidateRect(window->hwnd, 0, 0);
__END__;
}
CV_IMPL void cvClearTextOpenGl(const char* name)
{
CV_FUNCNAME( "cvClearTextOpenGl" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
CV_ERROR( CV_StsNullPtr, "NULL window" );
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
if (window->glText)
{
window->glText->clear();
InvalidateRect(window->hwnd, 0, 0);
}
icvSetOpenGlFuncTab(window->glFuncTab);
__END__;
}
......@@ -1406,9 +1180,6 @@ static void icvRemoveWindow( CvWindow* window )
if (window->useGl)
{
wglMakeCurrent(window->dc, window->hGLRC);
if (window->glText)
delete window->glText;
if (window->glCleanCallback)
{
......
此差异已折叠。
......@@ -3,6 +3,7 @@
#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"
......@@ -43,11 +44,17 @@ int main(int argc, char* argv[])
{
bool haveCuda = getCudaEnabledDeviceCount() > 0;
namedWindow("OpenGL Mat", WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow("OpenGL GlBuffer", WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow("OpenGL GlTexture", WINDOW_OPENGL | WINDOW_AUTOSIZE);
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("OpenGL GpuMat", WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow(openGlGpuMatWnd, WINDOW_OPENGL | WINDOW_AUTOSIZE);
namedWindow("Mat", WINDOW_AUTOSIZE);
Mat img = imread(argv[1]);
......@@ -55,10 +62,10 @@ int main(int argc, char* argv[])
if (haveCuda)
setGlDevice();
setOpenGlContext("OpenGL GlBuffer");
setOpenGlContext(openGlBufferWnd);
GlBuffer buf(img, GlBuffer::TEXTURE_BUFFER);
setOpenGlContext("OpenGL GlTexture");
setOpenGlContext(openGlTextureWnd);
GlTexture tex(img);
GpuMat d_img;
......@@ -69,48 +76,50 @@ int main(int argc, char* argv[])
{
Timer t("OpenGL Mat ");
imshow("OpenGL Mat", img);
imshow(openGlMatWnd, img);
}
{
Timer t("OpenGL GlBuffer ");
imshow("OpenGL GlBuffer", buf);
imshow(openGlBufferWnd, buf);
}
{
Timer t("OpenGL GlTexture");
imshow("OpenGL GlTexture", tex);
imshow(openGlTextureWnd, tex);
}
if (haveCuda)
{
Timer t("OpenGL GpuMat ");
imshow("OpenGL GpuMat", d_img);
imshow(openGlGpuMatWnd, d_img);
}
{
Timer t("Mat ");
imshow("Mat", img);
imshow(matWnd, img);
}
waitKey();
cout << "\n=== Second call\n\n";
{
Timer t("OpenGL Mat ");
imshow("OpenGL Mat", img);
imshow(openGlMatWnd, img);
}
{
Timer t("OpenGL GlBuffer ");
imshow("OpenGL GlBuffer", buf);
imshow(openGlBufferWnd, buf);
}
{
Timer t("OpenGL GlTexture");
imshow("OpenGL GlTexture", tex);
imshow(openGlTextureWnd, tex);
}
if (haveCuda)
{
Timer t("OpenGL GpuMat ");
imshow("OpenGL GpuMat", d_img);
imshow(openGlGpuMatWnd, d_img);
}
{
Timer t("Mat ");
imshow("Mat", img);
imshow(matWnd, img);
}
cout << "\n";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册