Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfRenderingContext_WGL.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <bias_config.h>
26 #include "glfRenderingContext_WGL.hh"
27 #include <Utils/GlutInitWrapper.hh>
28 #include <OpenGLFramework/Base/glfException.hh>
29 
30 
31 using namespace BIAS;
32 using namespace std;
33 
35 {
36  hDC_ = NULL;
37  hRC_ = NULL;
38 }
39 
41 {
42  // make sure Destroy was called by now.
43  BIASASSERT(hDC_ == NULL);
44  BIASASSERT(hRC_ == NULL);
45 }
46 
48 {
49  Destroy();
50 
51 #ifndef BIAS_HAVE_GLEW
52 #error glfRenderingContext_WGL requires GLEW
53 #endif
54 
55 #ifndef BIAS_HAVE_GLUT
56 #error glfRenderingContext_WGL requires GLUT
57 #endif
58 
59  // create a hidden glut window, because
60  // 1) GLEW is needed for ARB_pixel_format
61  // 2) An existing DC and RC is required.
63 
64  glutCreateWindow("Hidden window");
65  glutHideWindow();
66 
67  // initialize glew
68  InitGlew();
69 
70  // need WGL_ARB_pixel_format extension
71  if (!WGLEW_ARB_pixel_format) {
72  GLF_THROW_EXCEPTION("WGL_ARB_pixel_format extension is unsupported");
73  }
74 
75  // get current OpenGL device and render context
76  HDC hCurrentDC = wglGetCurrentDC();
77  if (hCurrentDC == NULL) {
78  GLF_THROW_EXCEPTION("Failed to get OpenGL device context");
79  }
80 
81  // get pixel formats
82  int attribs[] = {
83  WGL_SUPPORT_OPENGL_ARB, TRUE,
84  GetWglRenderTargetAttribute(), TRUE,
85  0
86  };
87  const int MAX_PIXEL_FORMATS = 512;
88  int pixelFormats[MAX_PIXEL_FORMATS];
89  UINT numFormats;
90  if (wglChoosePixelFormatARB(hCurrentDC, attribs, NULL,
91  MAX_PIXEL_FORMATS, pixelFormats,
92  &numFormats) == FALSE) {
93  GLF_THROW_EXCEPTION("Failed to choose pixel format");
94  }
95  if (numFormats == 0) {
96  GLF_THROW_EXCEPTION("No matching pixel format found");
97  }
98  if (numFormats > MAX_PIXEL_FORMATS) {
99  numFormats = MAX_PIXEL_FORMATS;
100  }
101 
102  // choose pixel format
103  pixelFormat_ = -1;
104  for (int i = 0; i < (int)numFormats; i++) {
105  int attribList[] = {
106  WGL_RED_BITS_ARB, // 0
107  WGL_GREEN_BITS_ARB, // 1
108  WGL_BLUE_BITS_ARB, // 2
109  WGL_ALPHA_BITS_ARB, // 3
110  WGL_DEPTH_BITS_ARB, // 4
111  WGL_STENCIL_BITS_ARB, // 5
112  WGL_DOUBLE_BUFFER_ARB // 6
113  };
114  const int numAttribs = sizeof(attribList)/sizeof(int);
115  int attribValues[numAttribs];
116  wglGetPixelFormatAttribivARB(hCurrentDC, pixelFormats[i], 0, numAttribs,
117  attribList, attribValues);
118  if (attribValues[0] == config.redSize &&
119  attribValues[1] == config.greenSize &&
120  attribValues[2] == config.blueSize &&
121  attribValues[3] == config.alphaSize &&
122  attribValues[4] == config.depthSize &&
123  attribValues[5] == config.stencilSize &&
124  (attribValues[6] == TRUE) == config.doubleBuffer) {
125  pixelFormat_ = pixelFormats[i];
126  break;
127  }
128  }
129  if (pixelFormat_ == -1) {
130  GLF_THROW_EXCEPTION("Requested rendering context configuration is unsupported");
131  }
132 
133  // create device context
134  hDC_ = CreateDeviceContext(config.width, config.height, pixelFormat_);
135 
136  // create render context.
137  hRC_ = wglCreateContext(hDC_);
138  // hRC_ = wglCreateContextAttribsARB(hDC_, 0, NULL);
139  if (hRC_ == NULL) {
140  GLF_THROW_EXCEPTION("Failed to create WGL rendering context");
141  }
142 
143  MakeCurrent();
144 }
145 
147 {
148  if (hRC_ != NULL) {
149  wglDeleteContext(hRC_);
150  hRC_ = NULL;
151  }
152 
153  DestroyDeviceContext();
154  hDC_ = NULL;
155 }
156 
158 {
159  BIASASSERT(hDC_ != NULL && hRC_ != NULL);
160 
161  wglMakeCurrent(hDC_, hRC_);
162 }
163 
164 
166 {
167  BIASASSERT(hDC_ != NULL && hRC_ != NULL);
168 
169  cerr << "Error: glfRenderingContext_WGL::DoneCurrent() is not implemented in BIAS for Windows." << endl;
170 }
171 
173 {
174  BIASASSERT(hDC_ != NULL);
175 
176  GetSize(config.width, config.height);
177 
178  int attribs[] = {
179  WGL_RED_BITS_ARB, // 0
180  WGL_GREEN_BITS_ARB, // 1
181  WGL_BLUE_BITS_ARB, // 2
182  WGL_ALPHA_BITS_ARB, // 3
183  WGL_DEPTH_BITS_ARB, // 4
184  WGL_STENCIL_BITS_ARB, // 5
185  WGL_DOUBLE_BUFFER_ARB // 6
186  };
187  const int numAttribs = sizeof(attribs)/sizeof(int);
188  int attribValues[numAttribs];
189 
190  wglGetPixelFormatAttribivARB(hDC_, pixelFormat_, 0, numAttribs,
191  attribs, attribValues);
192 
193  config.doubleBuffer = (attribValues[6] == TRUE);
194  config.redSize = attribValues[0];
195  config.greenSize = attribValues[1];
196  config.blueSize = attribValues[2];
197  config.alphaSize = attribValues[3];
198  config.depthSize = attribValues[4];
199  config.stencilSize = attribValues[5];
200 }
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
virtual void Destroy()
Uninitializes the rendering context.
virtual void DoneCurrent()
Removes the current state of this context.
static GlutInitWrapper * GetInstance()
virtual void MakeCurrent()
Makes this context the current target for OpenGL calls.
virtual void GetConfig(glfRenderingContextConfig &config)
Gets the currently used config of the rendering context.
Configuration for a rendering context.