-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPBuffer.cpp
More file actions
executable file
·337 lines (281 loc) · 8.65 KB
/
PBuffer.cpp
File metadata and controls
executable file
·337 lines (281 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* PBuffer.cpp
*
* Li-Yi Wei
* 3/15/2003
*
*/
#include <GL/glut.h>
#include "PBuffer.hpp"
// Convenience stuff for pbuffer object.
#define GLH_EXT_SINGLE_FILE
#define REQUIRED_EXTENSIONS "WGL_ARB_pbuffer " \
"WGL_ARB_pixel_format " \
"WGL_ARB_render_texture " \
"GL_NV_float_buffer "
#include <glh/glh_extensions.h>
#include <iostream>
PBuffer::PBuffer(const int height, const int width,
const Options options,
const unsigned long mode) throw(Exception) : _mode(mode), _r2t(options & PBuffer::RENDER_TO_TEXTURE), _float(options & PBuffer::FLOAT_COMPONENTS)
{
string result = Init(height, width);
if(result != "")
{
throw Exception(result);
}
}
PBuffer::~PBuffer(void)
{
if(_hpbuffer)
{
// Check if we are currently rendering in the pbuffer
if(wglGetCurrentContext() == _hglrc)
wglMakeCurrent(0, 0);
// delete the pbuffer context
wglDeleteContext(_hglrc);
wglReleasePbufferDCARB(_hpbuffer, _hdc);
wglDestroyPbufferARB(_hpbuffer);
_hpbuffer = 0;
}
}
string PBuffer::Init(const int height, const int width)
{
string result = "";
// set the entry points for the extension.
if(!glh_init_extensions(REQUIRED_EXTENSIONS))
{
result = "pbuffer creation error: Necessary extensions were not supported";
return result;
}
_hpbuffer = 0;
_hdc = 0;
_hglrc = 0;
_height = 0;
_width = 0;
_hdc0 = 0;
_hglrc0 = 0;
_shared = 1;
_binded = 0;
_hdc0 = wglGetCurrentDC();
_hglrc0 = wglGetCurrentContext();
result = LastError();
if(result != "") return result;
// Query for a suitable pixel format based on the specified mode.
const int MAX_ATTRIBS = 256;
const int MAX_PFORMATS = 256;
int format;
int pformat[MAX_PFORMATS];
unsigned int nformats;
int iattributes[2*MAX_ATTRIBS];
float fattributes[2*MAX_ATTRIBS];
int nfattribs = 0;
int niattribs = 0;
// Attribute arrays must be "0" terminated - for simplicity, first
// just zero-out the array entire, then fill from left to right.
memset(iattributes, 0, sizeof(int)*2*MAX_ATTRIBS);
memset(fattributes, 0, sizeof(float)*2*MAX_ATTRIBS);
// Since we are trying to create a pbuffer, the pixel format we
// request (and subsequently use) must be "p-buffer capable".
iattributes[niattribs++] = WGL_DRAW_TO_PBUFFER_ARB;
iattributes[niattribs++] = GL_TRUE;
if(_r2t)
{
// we are asking for a pbuffer that is meant to be bound
// as an RGBA texture - therefore we need a color plane
iattributes[niattribs++] = WGL_BIND_TO_TEXTURE_RGBA_ARB;
iattributes[niattribs++] = GL_TRUE;
}
if(_float)
{
iattributes[niattribs++] = WGL_RED_BITS_ARB;
iattributes[niattribs++] = 32;
iattributes[niattribs++] = WGL_GREEN_BITS_ARB;
iattributes[niattribs++] = 32;
iattributes[niattribs++] = WGL_BLUE_BITS_ARB;
iattributes[niattribs++] = 32;
iattributes[niattribs++] = WGL_ALPHA_BITS_ARB;
iattributes[niattribs++] = 32;
iattributes[niattribs++] = WGL_FLOAT_COMPONENTS_NV;
iattributes[niattribs++] = GL_TRUE;
}
{
if(_mode & GLUT_INDEX)
{
iattributes[niattribs++] = WGL_PIXEL_TYPE_ARB;
iattributes[niattribs++] = WGL_TYPE_COLORINDEX_ARB; // Yikes!
}
else
{
iattributes[niattribs++] = WGL_PIXEL_TYPE_ARB;
iattributes[niattribs++] = WGL_TYPE_RGBA_ARB;
}
if(_mode & GLUT_DOUBLE)
{
iattributes[niattribs++] = WGL_DOUBLE_BUFFER_ARB;
iattributes[niattribs++] = GL_TRUE;
}
if(_mode & GLUT_DEPTH)
{
iattributes[niattribs++] = WGL_DEPTH_BITS_ARB;
iattributes[niattribs++] = 24;
}
if(_mode & GLUT_STENCIL)
{
iattributes[niattribs++] = WGL_STENCIL_BITS_ARB;
iattributes[niattribs++] = 8;
}
if(_mode & GLUT_ACCUM)
{
iattributes[niattribs++] = WGL_ACCUM_BITS_ARB;
iattributes[niattribs++] = 1;
}
}
if ( !wglChoosePixelFormatARB(_hdc0,
iattributes,
fattributes,
MAX_PFORMATS,
pformat,
&nformats ))
{
return "pbuffer creation error: wglChoosePixelFormatARB() failed";
}
result = LastError();
if(result != "") return result;
if(nformats <= 0)
{
return "pbuffer creation error: Couldn't find a suitable pixel format";
}
format = pformat[0];
// Set up the pbuffer attributes
memset(iattributes, 0, sizeof(int)*2*MAX_ATTRIBS);
niattribs = 0;
if(_r2t)
{
// the render texture format is RGBA
iattributes[niattribs++] = WGL_TEXTURE_FORMAT_ARB;
iattributes[niattribs++] = WGL_TEXTURE_RGBA_ARB;
// the render texture target is GL_TEXTURE_2D
iattributes[niattribs++] = WGL_TEXTURE_TARGET_ARB;
iattributes[niattribs++] = WGL_TEXTURE_2D_ARB;
// ask to allocate room for the mipmaps
iattributes[niattribs++] = WGL_MIPMAP_TEXTURE_ARB;
iattributes[niattribs++] = TRUE;
// ask to allocate the largest pbuffer it can, if it is
// unable to allocate for the width and height
iattributes[niattribs++] = WGL_PBUFFER_LARGEST_ARB;
iattributes[niattribs++] = FALSE;
}
// Create the p-buffer.
_hpbuffer = wglCreatePbufferARB(_hdc0, format, width, height, iattributes);
if(_hpbuffer == 0)
{
return "pbuffer creation error: wglCreatePbufferARB() failed";
}
result = LastError();
if(result != "") return result;
// Get the device context.
_hdc = wglGetPbufferDCARB(_hpbuffer);
if(_hdc == 0)
{
return "pbuffer creation error: wglGetPbufferDCARB() failed";
}
result = LastError();
if(result != "") return result;
// Create a gl context for the p-buffer.
_hglrc = wglCreateContext(_hdc);
if(_hglrc == 0)
{
return "pbuffer creation error: wglCreateContext() failed";
}
result = LastError();
if(result != "") return result;
// Determine the actual width and height we were able to create.
wglQueryPbufferARB(_hpbuffer, WGL_PBUFFER_WIDTH_ARB, &_width);
wglQueryPbufferARB(_hpbuffer, WGL_PBUFFER_HEIGHT_ARB, &_height);
// share
if(_shared)
{
if(!wglShareLists(_hglrc0, _hglrc))
{
return "pbuffer creation error: wglShareLists() failed";
}
}
// done
return result;
}
string PBuffer::LastError(void) const
{
DWORD err = GetLastError();
SetLastError(0);
string result = "";
switch(err)
{
case ERROR_INVALID_PIXEL_FORMAT:
result = "Win32 Error: ERROR_INVALID_PIXEL_FORMAT";
break;
case ERROR_NO_SYSTEM_RESOURCES:
result = "Win32 Error: ERROR_NO_SYSTEM_RESOURCES";
break;
case ERROR_INVALID_DATA:
result = "Win32 Error: ERROR_INVALID_DATA";
break;
case ERROR_INVALID_WINDOW_HANDLE:
result = "Win32 Error: ERROR_INVALID_WINDOW_HANDLE";
break;
case ERROR_RESOURCE_TYPE_NOT_FOUND:
result = "Win32 Error: ERROR_RESOURCE_TYPE_NOT_FOUND";
break;
case ERROR_SUCCESS:
// no error
break;
default:
result = "Win32 Error: unknown error";
break;
}
return result;
}
string PBuffer::Begin(void) const
{
string result = "";
// release the pbuffer from the render texture object
if(_binded)
{
if(wglReleaseTexImageARB(_hpbuffer, WGL_FRONT_LEFT_ARB) == FALSE)
{
result = LastError();
}
else
{
_binded = 0;
}
}
// get the GLUT window HDC and HGLRC
_hdc0 = wglGetCurrentDC();
_hglrc0 = wglGetCurrentContext();
if(wglMakeCurrent(_hdc, _hglrc) == FALSE)
{
result = LastError();
}
return result;
}
string PBuffer::End(void) const
{
string result = "";
if(wglMakeCurrent(_hdc0, _hglrc0) == FALSE)
{
result = LastError();
}
if(_r2t && !_binded)
{
if(wglBindTexImageARB(_hpbuffer, WGL_FRONT_LEFT_ARB) == FALSE)
{
result = LastError();
}
else
{
_binded = 1;
}
}
return result;
}