summaryrefslogtreecommitdiff
path: root/tests/OffscreenContextWGL.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/OffscreenContextWGL.cc')
-rw-r--r--tests/OffscreenContextWGL.cc130
1 files changed, 89 insertions, 41 deletions
diff --git a/tests/OffscreenContextWGL.cc b/tests/OffscreenContextWGL.cc
index 863da31..17c49c6 100644
--- a/tests/OffscreenContextWGL.cc
+++ b/tests/OffscreenContextWGL.cc
@@ -2,6 +2,13 @@
Create an OpenGL context without creating an OpenGL Window. for Windows.
+For more info:
+
+ http://www.nullterminator.net/opengl32.html by Blaine Hodge
+ http://msdn.microsoft.com/en-us/library/ee418815(v=vs.85).aspx
+ http://www.cprogramming.com/tutorial/wgl_wiggle_functions.html by RoD
+ ( which includes robot.cc by Steven Billington )
+ http://blogs.msdn.com/b/oldnewthing/archive/2006/12/04/1205831.aspx by Tom
*/
#include <windows.h>
@@ -35,13 +42,12 @@ void offscreen_context_init(OffscreenContext &ctx, int width, int height)
ctx.fbo = NULL;
}
-
void glewCheck() {
#ifdef DEBUG
cerr << "GLEW version " << glewGetString(GLEW_VERSION) << "\n";
cerr << (const char *)glGetString(GL_RENDERER) << "(" << (const char *)glGetString(GL_VENDOR) << ")\n"
<< "OpenGL version " << (const char *)glGetString(GL_VERSION) << "\n";
- cerr << "Extensions: " << (const char *)glGetString(GL_EXTENSIONS) << "\n";
+ //cerr << "Extensions: " << (const char *)glGetString(GL_EXTENSIONS) << "\n";
if (GLEW_ARB_framebuffer_object) {
cerr << "ARB_FBO supported\n";
@@ -57,46 +63,81 @@ void glewCheck() {
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
- return DefWindowProc( hwnd, message, wparam, lparam );
+ return DefWindowProc( hwnd, message, wparam, lparam );
}
bool create_wgl_dummy_context(OffscreenContext &ctx)
{
- // create window
-
- HINSTANCE inst = GetModuleHandle(0);
- WNDCLASS wc;
- ZeroMemory( &wc, sizeof( wc ) );
- wc.style = CS_OWNDC;
- wc.lpfnWndProc = WndProc;
- wc.hInstance = inst;
- wc.lpszClassName = "OpenSCAD";
- RegisterClass( &wc );
-
- HWND window = CreateWindow( "OpenSCAD", "OpenSCAD",
- WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
- 0, 0, 256, 256, NULL, NULL, inst, NULL );
-
+ // this call alters ctx->window and ctx->openGLContext
+ // and ctx->dev_context if successfull
+ // create window
+
+ HINSTANCE inst = GetModuleHandle(0);
+ WNDCLASS wc;
+ ZeroMemory( &wc, sizeof( wc ) );
+ wc.style = CS_OWNDC;
+ wc.lpfnWndProc = WndProc;
+ wc.hInstance = inst;
+ wc.lpszClassName = "OpenSCAD";
+ RegisterClass( &wc );
- // create WGL context, make current
-
- PIXELFORMATDESCRIPTOR pixformat;
- int chosenformat;
- HDC dev_context = GetDC( window );
- ZeroMemory( &pixformat, sizeof( pixformat ) );
- pixformat.nSize = sizeof( pixformat );
- pixformat.nVersion = 1;
- pixformat.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
- pixformat.iPixelType = PFD_TYPE_RGBA;
- pixformat.cColorBits = 24;
- pixformat.cDepthBits = 16;
- pixformat.iLayerType = PFD_MAIN_PLANE;
- chosenformat = ChoosePixelFormat( dev_context, &pixformat );
- SetPixelFormat( dev_context, chosenformat, &pixformat );
- HGLRC gl_render_context = wglCreateContext( dev_context );
- wglMakeCurrent( dev_context, gl_render_context );
-
- return true;
+ HWND window = CreateWindow( "OpenSCAD", "OpenSCAD",
+ WS_CAPTION | WS_POPUPWINDOW, //| WS_VISIBLE,
+ 0, 0, ctx.width, ctx.height, NULL, NULL, inst, NULL );
+
+ if ( window==NULL ) {
+ cerr << "MS GDI - CreateWindow failed\n";
+ return false;
+ }
+
+ // create WGL context, make current
+
+ PIXELFORMATDESCRIPTOR pixformat;
+ int chosenformat;
+ HDC dev_context = GetDC( window );
+ if ( dev_context == NULL ) {
+ cerr << "MS GDI - GetDC failed\n";
+ return false;
+ }
+
+ ZeroMemory( &pixformat, sizeof( pixformat ) );
+ pixformat.nSize = sizeof( pixformat );
+ pixformat.nVersion = 1;
+ pixformat.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
+ pixformat.iPixelType = PFD_TYPE_RGBA;
+ pixformat.cColorBits = 24;
+ pixformat.cDepthBits = 16;
+ pixformat.iLayerType = PFD_MAIN_PLANE;
+ chosenformat = ChoosePixelFormat( dev_context, &pixformat );
+ if (chosenformat==0) {
+ cerr << "MS GDI - ChoosePixelFormat failed\n";
+ return false;
+ }
+
+ bool spfok = SetPixelFormat( dev_context, chosenformat, &pixformat );
+ if (!spfok) {
+ cerr << "MS GDI - SetPixelFormat failed\n";
+ return false;
+ }
+
+ HGLRC gl_render_context = wglCreateContext( dev_context );
+ if ( gl_render_context == NULL ) {
+ cerr << "MS WGL - wglCreateContext failed\n";
+ ReleaseDC( ctx.window, ctx.dev_context );
+ return false;
+ }
+
+ bool mcok = wglMakeCurrent( dev_context, gl_render_context );
+ if (!mcok) {
+ cerr << "MS GDI - wglMakeCurrent failed\n";
+ return false;
+ }
+
+ ctx.window = window;
+ ctx.dev_context = dev_context;
+ ctx.openGLContext = gl_render_context;
+
+ return true;
}
@@ -105,22 +146,29 @@ OffscreenContext *create_offscreen_context(int w, int h)
OffscreenContext *ctx = new OffscreenContext;
offscreen_context_init( *ctx, w, h );
- // before an FBO can be setup, a WGL context must be created
- // this call alters ctx->window and ctx->openGLContext
+ // Before an FBO can be setup, a WGL context must be created.
+ // This call alters ctx->window and ctx->openGLContext
// and ctx->dev_context if successfull
if (!create_wgl_dummy_context( *ctx )) {
+ cerr << "wgl dummy fail\n";
+ fflush(stderr);
return NULL;
}
- glewInit(); //must come after Context creation and before FBO calls.
+ GLenum err = glewInit(); // must come after Context creation and before FBO calls.
+ if (GLEW_OK != err) {
+ cerr << "Unable to init GLEW: " << glewGetErrorString(err) << "\n";
+ fflush(stderr);
+ return NULL;
+ }
glewCheck();
ctx->fbo = fbo_new();
if (!fbo_init(ctx->fbo, w, h)) {
+ cerr << "fbo didn't init\n";
return NULL;
}
- ctx = NULL;
return ctx;
}
contact: Jan Huwald // Impressum