added OpenGL support to highgui under WIN32

This commit is contained in:
Vladislav Vinogradov
2011-11-21 11:58:52 +00:00
parent 03002fff54
commit c2783af781
22 changed files with 2548 additions and 119 deletions

View File

@@ -54,6 +54,13 @@
#include <stdio.h>
#include <assert.h>
#ifdef HAVE_OPENGL
#include <memory>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/gpumat.hpp"
#include <GL\gl.h>
#endif
static const char* trackbar_text =
" ";
@@ -81,7 +88,7 @@ static const char* trackbar_text =
#endif
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
{
assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
@@ -127,7 +134,6 @@ typedef struct CvTrackbar
}
CvTrackbar;
typedef struct CvWindow
{
int signature;
@@ -155,6 +161,24 @@ typedef struct CvWindow
CvTrackbar* first;
}
toolbar;
int width;
int height;
// OpenGL support
#ifdef HAVE_OPENGL
bool useGl;
HGLRC hGLRC;
CvOpenGLCallback glDrawCallback;
void* glDrawData;
CvOpenGlCleanCallback glCleanCallback;
void* glCleanData;
cv::gpu::GlFuncTab* glFuncTab;
#endif
}
CvWindow;
@@ -464,6 +488,420 @@ void cvSetModeWindow_W32( const char*, double)
}
#endif
double cvGetPropWindowAutoSize_W32(const char* name)
{
double result = -1;
CV_FUNCNAME( "cvSetCloseCallback" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT;
result = window->flags & CV_WINDOW_AUTOSIZE;
__END__;
return result;
}
double cvGetRatioWindow_W32(const char* name)
{
double result = -1;
CV_FUNCNAME( "cvGetRatioWindow_W32" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
CV_ERROR( CV_StsNullPtr, "NULL window" );
result = static_cast<double>(window->width) / window->height;
__END__;
return result;
}
double cvGetOpenGlProp_W32(const char* name)
{
double result = -1;
#ifdef HAVE_OPENGL
CV_FUNCNAME( "cvGetOpenGlProp_W32" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
__CV_EXIT__;
result = window->useGl;
__END__;
#endif
return result;
}
// OpenGL support
#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
#ifndef GL_BGR
#define GL_BGR 0x80E0
#endif
#ifndef GL_BGRA
#define GL_BGRA 0x80E1
#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 cv::gpu::GlFuncTab
{
public:
PFNGLGENBUFFERSPROC glGenBuffersExt;
PFNGLDELETEBUFFERSPROC glDeleteBuffersExt;
PFNGLBUFFERDATAPROC glBufferDataExt;
PFNGLBUFFERSUBDATAPROC glBufferSubDataExt;
PFNGLBINDBUFFERPROC glBindBufferExt;
PFNGLMAPBUFFERPROC glMapBufferExt;
PFNGLUNMAPBUFFERPROC glUnmapBufferExt;
bool initialized;
GlFuncTab_W32()
{
glGenBuffersExt = 0;
glDeleteBuffersExt = 0;
glBufferDataExt = 0;
glBufferSubDataExt = 0;
glBindBufferExt = 0;
glMapBufferExt = 0;
glUnmapBufferExt = 0;
initialized = false;
}
void genBuffers(int n, unsigned int* buffers) const
{
CV_FUNCNAME( "genBuffers" );
__BEGIN__;
if (!glGenBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glGenBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void deleteBuffers(int n, const unsigned int* buffers) const
{
CV_FUNCNAME( "deleteBuffers" );
__BEGIN__;
if (!glDeleteBuffersExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glDeleteBuffersExt(n, buffers);
CV_CheckGlError();
__END__;
}
void bufferData(unsigned int target, ptrdiff_t size, const void* data, unsigned int usage) const
{
CV_FUNCNAME( "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 bufferSubData(unsigned int target, ptrdiff_t offset, ptrdiff_t size, const void* data) const
{
CV_FUNCNAME( "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 bindBuffer(unsigned int target, unsigned int buffer) const
{
CV_FUNCNAME( "bindBuffer" );
__BEGIN__;
if (!glBindBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glBindBufferExt(target, buffer);
CV_CheckGlError();
__END__;
}
void* mapBuffer(unsigned int target, unsigned int access) const
{
CV_FUNCNAME( "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 unmapBuffer(unsigned int target) const
{
CV_FUNCNAME( "unmapBuffer" );
__BEGIN__;
if (!glUnmapBufferExt)
CV_ERROR(CV_OpenGlApiCallError, "Current OpenGL implementation doesn't support required extension");
glUnmapBufferExt(target);
CV_CheckGlError();
__END__;
}
bool isGlContextInitialized() const
{
return initialized;
}
};
void initGl(CvWindow* window)
{
std::auto_ptr<GlFuncTab_W32> glFuncTab(new GlFuncTab_W32);
// 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();
cv::gpu::setGlFuncTab(window->glFuncTab);
}
void createGlContext(HWND hWnd, HDC& hGLDC, HGLRC& hGLRC, bool& useGl)
{
CV_FUNCNAME( "createGlContext" );
__BEGIN__;
useGl = false;
int PixelFormat;
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
hGLDC = GetDC(hWnd);
if (!hGLDC)
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
PixelFormat = ChoosePixelFormat(hGLDC, &pfd);
if (!PixelFormat)
CV_ERROR( CV_OpenGlApiCallError, "Can't Find A Suitable PixelFormat" );
if (!SetPixelFormat(hGLDC, PixelFormat, &pfd))
CV_ERROR( CV_OpenGlApiCallError, "Can't Set The PixelFormat" );
hGLRC = wglCreateContext(hGLDC);
if (!hGLRC)
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Rendering Context" );
if (!wglMakeCurrent(hGLDC, hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
useGl = true;
__END__;
}
void releaseGlContext(CvWindow* window)
{
CV_FUNCNAME( "releaseGlContext" );
__BEGIN__;
if (window->hGLRC)
{
wglDeleteContext(window->hGLRC);
window->hGLRC = NULL;
}
if (window->dc)
{
ReleaseDC(window->hwnd, window->dc);
window->dc = NULL;
}
window->useGl = false;
__END__;
}
void drawGl(CvWindow* window)
{
CV_FUNCNAME( "drawGl" );
__BEGIN__;
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (window->glDrawCallback)
window->glDrawCallback(window->glDrawData);
CV_CheckGlError();
if (!SwapBuffers(window->dc))
CV_ERROR( CV_OpenGlApiCallError, "Can't swap OpenGL buffers" );
__END__;
}
void resizeGl(CvWindow* window)
{
CV_FUNCNAME( "resizeGl" );
__BEGIN__;
if (!wglMakeCurrent(window->dc, window->hGLRC))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
glViewport(0, 0, window->width, window->height);
__END__;
}
}
#endif // HAVE_OPENGL
CV_IMPL int cvNamedWindow( const char* name, int flags )
{
int result = 0;
@@ -483,9 +921,61 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
CV_ERROR( CV_StsNullPtr, "NULL name string" );
// Check the name in the storage
if( icvFindWindowByName( name ) != 0 )
window = icvFindWindowByName( name );
if (window != 0)
{
result = 1;
#ifdef HAVE_OPENGL
if (window->useGl && !(flags & CV_WINDOW_OPENGL))
{
wglMakeCurrent(window->dc, window->hGLRC);
if (window->glCleanCallback)
{
window->glCleanCallback(window->glCleanData);
window->glCleanCallback = 0;
window->glCleanData = 0;
}
releaseGlContext(window);
window->dc = CreateCompatibleDC(0);
window->hGLRC = 0;
window->useGl = false;
}
else if (!window->useGl && (flags & CV_WINDOW_OPENGL))
{
if (window->dc && window->image)
DeleteObject(SelectObject(window->dc, window->image));
if (window->dc)
DeleteDC(window->dc);
bool useGl = false;
HDC hGLDC = 0;
HGLRC hGLRC = 0;
createGlContext(window->hwnd, hGLDC, hGLRC, useGl);
if (!useGl)
{
window->dc = CreateCompatibleDC(0);
window->hGLRC = 0;
window->useGl = false;
result = 0;
}
else
{
window->dc = hGLDC;
window->hGLRC = hGLRC;
window->useGl = true;
initGl(window);
}
}
#endif // HAVE_OPENGL
EXIT;
}
@@ -506,6 +996,18 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
if( !hWnd )
CV_ERROR( CV_StsError, "Frame window can not be created" );
#ifndef HAVE_OPENGL
if (flags & CV_WINDOW_OPENGL)
CV_ERROR( CV_OpenGlNotSupported, "Library was built without OpenGL support" );
#else
bool useGl = false;
HDC hGLDC = 0;
HGLRC hGLRC = 0;
if (flags & CV_WINDOW_OPENGL)
createGlContext(hWnd, hGLDC, hGLRC, useGl);
#endif
ShowWindow(hWnd, SW_SHOW);
len = (int)strlen(name);
@@ -518,7 +1020,32 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
memcpy( window->name, name, len + 1 );
window->flags = flags;
window->image = 0;
#ifndef HAVE_OPENGL
window->dc = CreateCompatibleDC(0);
#else
window->glFuncTab = 0;
if (!useGl)
{
window->dc = CreateCompatibleDC(0);
window->hGLRC = 0;
window->useGl = false;
}
else
{
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;
window->status = CV_WINDOW_NORMAL;//YV
@@ -544,12 +1071,128 @@ CV_IMPL int cvNamedWindow( const char* name, int flags )
return result;
}
#ifdef HAVE_OPENGL
CV_IMPL void cvSetOpenGlContext(const char* name)
{
CV_FUNCNAME( "cvSetOpenGlContext" );
__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" );
cv::gpu::setGlFuncTab(window->glFuncTab);
__END__;
}
CV_IMPL void cvUpdateWindow(const char* name)
{
CV_FUNCNAME( "cvUpdateWindow" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT;
InvalidateRect(window->hwnd, 0, 0);
__END__;
}
CV_IMPL void cvCreateOpenGLCallback(const char* name, CvOpenGLCallback callback, void* userdata, double, double, double)
{
CV_FUNCNAME( "cvCreateOpenGLCallback" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
EXIT;
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window was created without OpenGL context" );
window->glDrawCallback = callback;
window->glDrawData = userdata;
__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 )
{
CvTrackbar* trackbar = NULL;
RECT wrect={0,0,0,0};
#ifdef HAVE_OPENGL
if (window->useGl)
{
delete window->glFuncTab;
cv::gpu::setGlFuncTab(0);
wglMakeCurrent(window->dc, window->hGLRC);
if (window->glCleanCallback)
{
window->glCleanCallback(window->glCleanData);
window->glCleanCallback = 0;
window->glCleanData = 0;
}
releaseGlContext(window);
}
#endif
if( window->frame )
GetWindowRect( window->frame, &wrect );
if( window->name )
@@ -559,7 +1202,7 @@ static void icvRemoveWindow( CvWindow* window )
if( window->hwnd )
icvSetWindowLongPtr( window->hwnd, CV_USERDATA, 0 );
if( window->frame )
icvSetWindowLongPtr( window->frame, CV_USERDATA, 0 );
icvSetWindowLongPtr( window->frame, CV_USERDATA, 0 );
if( window->toolbar.toolbar )
icvSetWindowLongPtr(window->toolbar.toolbar, CV_USERDATA, 0);
@@ -722,7 +1365,6 @@ static void icvUpdateWindowPos( CvWindow* window )
rect.bottom - rect.top + 1, TRUE );
}
CV_IMPL void
cvShowImage( const char* name, const CvArr* arr )
{
@@ -745,7 +1387,12 @@ cvShowImage( const char* name, const CvArr* arr )
window = icvFindWindowByName(name);
if(!window)
{
cvNamedWindow(name, 1);
#ifndef HAVE_OPENGL
cvNamedWindow(name, CV_WINDOW_AUTOSIZE);
#else
cvNamedWindow(name, CV_WINDOW_AUTOSIZE | CV_WINDOW_OPENGL);
#endif
window = icvFindWindowByName(name);
}
@@ -757,6 +1404,15 @@ cvShowImage( const char* name, const CvArr* arr )
CV_CALL( image = cvGetMat( arr, &stub ));
#ifdef HAVE_OPENGL
if (window->useGl)
{
cv::Mat im(image);
cv::imshow(name, im);
return;
}
#endif
if (window->image)
// if there is something wrong with these system calls, we cannot display image...
if (icvGetBitmapData( window, &size, &channels, &dst_ptr ))
@@ -1069,6 +1725,13 @@ static LRESULT CALLBACK HighGUIProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
//DeleteDC(hdc);
EndPaint(hwnd, &paint);
}
#ifdef HAVE_OPENGL
else if(window->useGl)
{
drawGl(window);
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
#endif
else
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
@@ -1094,6 +1757,15 @@ static LRESULT CALLBACK HighGUIProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
case WM_KEYDOWN:
window->last_key = (int)wParam;
return 0;
case WM_SIZE:
window->width = LOWORD(lParam);
window->height = HIWORD(lParam);
#ifdef HAVE_OPENGL
if (window->useGl)
resizeGl(window);
#endif
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);