forked from QianMo/GPU-Gems-Book-Source-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.h
132 lines (104 loc) · 2.88 KB
/
window.h
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
#ifndef WINDOW_H
#define WINDOW_H
#include "GL/glew.h"
#include "GL/wglew.h"
class Window{
public:
Window(){
ispbuffer = false;
pixels = 0;
};
~Window(){
Release();
};
void Release() {
if( ispbuffer ){
//if( wglGetCurrentContext() == rc )
// wglMakeCurrent(0,0);
//wglDeleteContext(rc);
wglReleasePbufferDCARB(pixels, dc);
wglDestroyPbufferARB(pixels);
ispbuffer = false;
}
}
void MakeCurrent(){
HDC curdc = wglGetCurrentDC();
HGLRC currc = wglGetCurrentContext();
if( curdc != dc || currc != rc){
//fprintf(stderr, "\tswitching to %s\n", name);
wglMakeCurrent(dc, rc);
}
//else
//fprintf(stderr, "\tredundant switch to %s\n", name);
};
void SetContext(){
dc = wglGetCurrentDC();
rc = wglGetCurrentContext();
strcpy(name, "colorbuffer");
};
int CreateContext( int w, int h, const char* n ){
ispbuffer = true;
int in_attribs[] = {
WGL_RED_BITS_ARB, 32,
WGL_GREEN_BITS_ARB, 32,
WGL_BLUE_BITS_ARB, 32,
WGL_ALPHA_BITS_ARB, 32,
WGL_COLOR_BITS_ARB, 128,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
WGL_FLOAT_COMPONENTS_NV, GL_TRUE,
WGL_DRAW_TO_PBUFFER_ARB, GL_TRUE,
//WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV, GL_TRUE,
0,0
};
HDC old_dc = wglGetCurrentDC();
int format;
unsigned int n_formats;
HPBUFFERARB pbuffer;
HDC new_dc;
HGLRC new_rc;
//int pbuffer_attribs[] = { WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_FLOAT_RGBA_NV,
// WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_RECTANGLE_NV,
// 0,0
// };
int pbuffer_attribs[] = { 0 };
if (!wglChoosePixelFormatARB(old_dc, in_attribs, NULL, 1, &format, &n_formats))
{
fprintf(stderr, "Failed to choose a suitable pixel format.\n");
return -1;
}
if (!(pbuffer = wglCreatePbufferARB(old_dc, format, w, h, pbuffer_attribs)))
{
fprintf(stderr, "Pbuffer creation failed\n");
return -1;
}
if (!(new_dc = wglGetPbufferDCARB(pbuffer)))
{
fprintf(stderr, "Couldn't get pbuffer device context\n");
return -1;
}
if (!(new_rc = wglCreateContext(new_dc)))
{
fprintf(stderr, "Couldn't create pbuffer rendering context\n");
return -1;
}
if (!wglShareLists(wglGetCurrentContext(), new_rc))
{
fprintf(stderr, "Couldn't share display list (wglShareLists)\n");
return -1;
}
dc = new_dc;
rc = new_rc;
pixels = pbuffer;
strcpy(name, n);
return 1;
};
HPBUFFERARB GetPBuffer() { return pixels; }
private:
HDC dc;
HGLRC rc;
HPBUFFERARB pixels;
bool ispbuffer;
char name[1024];
};
#endif /*WINDOW_H*/