Basic Image AlgorithmS Library 2.8.0

glfRenderingContext_WGL.cpp

00001 /*
00002  This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004  Copyright (C)  2008, 2009    (see file CONTACT for details)
00005  Multimediale Systeme der Informationsverarbeitung
00006  Institut fuer Informatik
00007  Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010  BIAS is free software; you can redistribute it and/or modify
00011  it under the terms of the GNU Lesser General Public License as published by
00012  the Free Software Foundation; either version 2.1 of the License, or
00013  (at your option) any later version.
00014 
00015  BIAS is distributed in the hope that it will be useful,
00016  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  GNU Lesser General Public License for more details.
00019 
00020  You should have received a copy of the GNU Lesser General Public License
00021  along with BIAS; if not, write to the Free Software
00022  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include <bias_config.h>
00026 #include "glfRenderingContext_WGL.hh"
00027 #include <Utils/GlutInitWrapper.hh>
00028 #include <OpenGLFramework/Base/glfException.hh>
00029 
00030 
00031 using namespace BIAS;
00032 using namespace std;
00033 
00034 glfRenderingContext_WGL::glfRenderingContext_WGL()
00035 {
00036   hDC_ = NULL;
00037   hRC_ = NULL;
00038 }
00039 
00040 glfRenderingContext_WGL::~glfRenderingContext_WGL()
00041 {
00042   // make sure Destroy was called by now.
00043   BIASASSERT(hDC_ == NULL);
00044   BIASASSERT(hRC_ == NULL);
00045 }
00046 
00047 void glfRenderingContext_WGL::Init(const glfRenderingContextConfig& config)
00048 {
00049   Destroy();
00050 
00051 #ifndef BIAS_HAVE_GLEW
00052 #error glfRenderingContext_WGL requires GLEW
00053 #endif
00054 
00055 #ifndef BIAS_HAVE_GLUT
00056 #error glfRenderingContext_WGL requires GLUT
00057 #endif
00058 
00059   // create a hidden glut window, because
00060   // 1) GLEW is needed for ARB_pixel_format
00061   // 2) An existing DC and RC is required.  
00062     BIAS::GlutInitWrapper::GetInstance()->Init(); 
00063     
00064   glutCreateWindow("Hidden window");
00065   glutHideWindow();
00066 
00067   // initialize glew
00068   InitGlew();
00069 
00070   // need WGL_ARB_pixel_format extension
00071   if (!WGLEW_ARB_pixel_format) {
00072     GLF_THROW_EXCEPTION("WGL_ARB_pixel_format extension is unsupported");
00073   }
00074 
00075   // get current OpenGL device and render context
00076   HDC hCurrentDC = wglGetCurrentDC();
00077   if (hCurrentDC == NULL) {
00078     GLF_THROW_EXCEPTION("Failed to get OpenGL device context");
00079   }
00080 
00081   // get pixel formats
00082   int attribs[] = {
00083     WGL_SUPPORT_OPENGL_ARB,         TRUE,
00084     GetWglRenderTargetAttribute(),  TRUE,
00085     0
00086   };
00087   const int MAX_PIXEL_FORMATS = 512;
00088   int pixelFormats[MAX_PIXEL_FORMATS];
00089   UINT numFormats;
00090   if (wglChoosePixelFormatARB(hCurrentDC, attribs, NULL,
00091                               MAX_PIXEL_FORMATS, pixelFormats,
00092                               &numFormats) == FALSE) {
00093     GLF_THROW_EXCEPTION("Failed to choose pixel format");
00094   }
00095   if (numFormats == 0) {
00096     GLF_THROW_EXCEPTION("No matching pixel format found");
00097   }
00098   if (numFormats > MAX_PIXEL_FORMATS) {
00099     numFormats = MAX_PIXEL_FORMATS;
00100   }
00101 
00102   // choose pixel format
00103   pixelFormat_ = -1;
00104   for (int i = 0; i < (int)numFormats; i++) {
00105     int attribList[] = {
00106       WGL_RED_BITS_ARB,     // 0
00107       WGL_GREEN_BITS_ARB,   // 1
00108       WGL_BLUE_BITS_ARB,    // 2
00109       WGL_ALPHA_BITS_ARB,   // 3
00110       WGL_DEPTH_BITS_ARB,   // 4
00111       WGL_STENCIL_BITS_ARB, // 5
00112       WGL_DOUBLE_BUFFER_ARB // 6
00113     };
00114     const int numAttribs = sizeof(attribList)/sizeof(int);
00115     int attribValues[numAttribs];
00116     wglGetPixelFormatAttribivARB(hCurrentDC, pixelFormats[i], 0, numAttribs,
00117                                  attribList, attribValues);
00118     if (attribValues[0] == config.redSize &&
00119         attribValues[1] == config.greenSize &&
00120         attribValues[2] == config.blueSize &&
00121         attribValues[3] == config.alphaSize &&
00122         attribValues[4] == config.depthSize &&
00123         attribValues[5] == config.stencilSize &&
00124         (attribValues[6] == TRUE) == config.doubleBuffer) {
00125       pixelFormat_ = pixelFormats[i];
00126       break;
00127     }
00128   }
00129   if (pixelFormat_ == -1) {
00130     GLF_THROW_EXCEPTION("Requested rendering context configuration is unsupported");
00131   }
00132 
00133   // create device context
00134   hDC_ = CreateDeviceContext(config.width, config.height, pixelFormat_);
00135 
00136   // create render context.
00137   hRC_ = wglCreateContext(hDC_);
00138  // hRC_ = wglCreateContextAttribsARB(hDC_, 0, NULL);
00139   if (hRC_ == NULL) {
00140     GLF_THROW_EXCEPTION("Failed to create WGL rendering context");
00141   }
00142 
00143   MakeCurrent();
00144 }
00145 
00146 void glfRenderingContext_WGL::Destroy()
00147 {
00148   if (hRC_ != NULL) {
00149     wglDeleteContext(hRC_);
00150     hRC_ = NULL;
00151   }
00152 
00153   DestroyDeviceContext();
00154   hDC_ = NULL;
00155 }
00156 
00157 void glfRenderingContext_WGL::MakeCurrent()
00158 {
00159   BIASASSERT(hDC_ != NULL && hRC_ != NULL);
00160 
00161   wglMakeCurrent(hDC_, hRC_);
00162 }
00163 
00164 
00165 void glfRenderingContext_WGL::DoneCurrent()
00166 {
00167   BIASASSERT(hDC_ != NULL && hRC_ != NULL);
00168 
00169   cerr << "Error: glfRenderingContext_WGL::DoneCurrent() is not implemented in BIAS for Windows." << endl;
00170 }
00171 
00172 void glfRenderingContext_WGL::GetConfig(glfRenderingContextConfig& config)
00173 {
00174   BIASASSERT(hDC_ != NULL);
00175 
00176   GetSize(config.width, config.height);
00177 
00178   int attribs[] = {
00179     WGL_RED_BITS_ARB,     // 0
00180     WGL_GREEN_BITS_ARB,   // 1
00181     WGL_BLUE_BITS_ARB,    // 2
00182     WGL_ALPHA_BITS_ARB,   // 3
00183     WGL_DEPTH_BITS_ARB,   // 4
00184     WGL_STENCIL_BITS_ARB, // 5
00185     WGL_DOUBLE_BUFFER_ARB // 6
00186   };
00187   const int numAttribs = sizeof(attribs)/sizeof(int);
00188   int attribValues[numAttribs];
00189 
00190   wglGetPixelFormatAttribivARB(hDC_, pixelFormat_, 0, numAttribs,
00191                                attribs, attribValues);
00192 
00193   config.doubleBuffer = (attribValues[6] == TRUE);
00194   config.redSize      = attribValues[0];
00195   config.greenSize    = attribValues[1];
00196   config.blueSize     = attribValues[2];
00197   config.alphaSize    = attribValues[3];
00198   config.depthSize    = attribValues[4];
00199   config.stencilSize  = attribValues[5];
00200 }
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends