-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogrammablePipelineOpenGLWin32.cpp
253 lines (207 loc) · 7.92 KB
/
programmablePipelineOpenGLWin32.cpp
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
// Windows
#include <Windows.h>
// OpenGL
#include <gl/gl.h>
using wglCreateContextAttribsARB_type = HGLRC (*)(HDC hdc, HGLRC hShareContext, const int *attribList);
wglCreateContextAttribsARB_type wglCreateContextAttribsARB;
// See https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt for all values
constexpr auto WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091;
constexpr auto WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092;
constexpr auto WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126;
constexpr auto WGL_CONTEXT_CORE_PROFILE_BIT_ARB = 0x00000001;
using wglChoosePixelFormatARB_type =
BOOL (*)(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
wglChoosePixelFormatARB_type wglChoosePixelFormatARB;
// See https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_pixel_format.txt for all values
constexpr auto WGL_DRAW_TO_WINDOW_ARB = 0x2001;
constexpr auto WGL_ACCELERATION_ARB = 0x2003;
constexpr auto WGL_SUPPORT_OPENGL_ARB = 0x2010;
constexpr auto WGL_DOUBLE_BUFFER_ARB = 0x2011;
constexpr auto WGL_PIXEL_TYPE_ARB = 0x2013;
constexpr auto WGL_COLOR_BITS_ARB = 0x2014;
constexpr auto WGL_DEPTH_BITS_ARB = 0x2022;
constexpr auto WGL_STENCIL_BITS_ARB = 0x2023;
constexpr auto WGL_FULL_ACCELERATION_ARB = 0x2027;
constexpr auto WGL_TYPE_RGBA_ARB = 0x202B;
static LRESULT CALLBACK WindowCallback(HWND window, UINT msg, WPARAM wparam, LPARAM lparam) {
LRESULT result = 0;
switch(msg) {
case WM_CLOSE:
case WM_DESTROY: PostQuitMessage(0); break;
default: result = DefWindowProcA(window, msg, wparam, lparam); break;
}
return result;
}
namespace {
HWND createWindow(HINSTANCE instance) {
WNDCLASSA windowClass = {};
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
windowClass.lpfnWndProc = WindowCallback;
windowClass.hInstance = instance;
windowClass.hCursor = LoadCursor(0, IDC_ARROW);
windowClass.hbrBackground = 0;
windowClass.lpszClassName = "WGL_fdjhsklf";
if(!RegisterClassA(&windowClass)) {
OutputDebugString("Failed to register window.");
return nullptr;
}
RECT rect = {};
rect.right = 1024;
rect.bottom = 576;
DWORD windowStyle = WS_OVERLAPPEDWINDOW;
AdjustWindowRect(&rect, windowStyle, false);
HWND window = CreateWindowExA(
0, windowClass.lpszClassName, "OpenGL", windowStyle, CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, 0, 0, instance, 0);
if(!window) {
OutputDebugString("Failed to create window.");
return nullptr;
}
return window;
}
bool initOpenglExtensions() {
// Before we can load extensions, we need a dummy OpenGL context, created using a dummy window.
// We use a dummy window because you can only set the pixel format for a window once. For the
// real window, we want to use wglChoosePixelFormatARB (so we can potentially specify options
// that aren't available in PIXELFORMATDESCRIPTOR), but we can't load and use that before we
// have a context.
// clang-format off
WNDCLASSA windowClass = {};
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
windowClass.lpfnWndProc = DefWindowProcA;
windowClass.hInstance = GetModuleHandle(0);
windowClass.lpszClassName = "DummyWGL";
// clang-format on
if(!RegisterClassA(&windowClass)) {
OutputDebugString("Failed to register dummy OpenGL window.");
return false;
}
HWND dummy_window = CreateWindowExA(0,
windowClass.lpszClassName,
"Dummy OpenGL Window",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
windowClass.hInstance,
0);
if(!dummy_window) {
OutputDebugString("Failed to create dummy OpenGL window.");
return false;
}
HDC dummy_dc = GetDC(dummy_window);
// clang-format off
PIXELFORMATDESCRIPTOR pfd = {};
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.cColorBits = 32;
pfd.cAlphaBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
// clang-format on
int pixelFormat = ChoosePixelFormat(dummy_dc, &pfd);
if(!pixelFormat) {
OutputDebugString("Failed to find a suitable pixel format.");
return false;
}
if(!SetPixelFormat(dummy_dc, pixelFormat, &pfd)) {
OutputDebugString("Failed to set the pixel format.");
return false;
}
HGLRC dummy_context = wglCreateContext(dummy_dc);
if(!dummy_context) {
OutputDebugString("Failed to create a dummy OpenGL rendering context.");
return false;
}
if(!wglMakeCurrent(dummy_dc, dummy_context)) {
OutputDebugString("Failed to activate dummy OpenGL rendering context.");
return false;
}
wglCreateContextAttribsARB = (wglCreateContextAttribsARB_type)wglGetProcAddress("wglCreateContextAttribsARB");
wglChoosePixelFormatARB = (wglChoosePixelFormatARB_type)wglGetProcAddress("wglChoosePixelFormatARB");
wglMakeCurrent(dummy_dc, 0);
wglDeleteContext(dummy_context);
ReleaseDC(dummy_window, dummy_dc);
DestroyWindow(dummy_window);
return true;
}
HGLRC initializeOpenGL(HDC gldc) {
if (!initOpenglExtensions()) {
return nullptr;
}
// clang-format off
int pixel_format_attribs[] = {
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
0
};
// clang-format on
int pixel_format;
UINT num_formats;
wglChoosePixelFormatARB(gldc, pixel_format_attribs, 0, 1, &pixel_format, &num_formats);
if(!num_formats) {
OutputDebugString("Failed to set the OpenGL 3.3 pixel format.");
return nullptr;
}
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(gldc, pixel_format, sizeof(pfd), &pfd);
if(!SetPixelFormat(gldc, pixel_format, &pfd)) {
OutputDebugString("Failed to set the OpenGL 3.3 pixel format.");
return nullptr;
}
// Specify that we want to create an OpenGL 3.3 core profile context
// clang-format off
int gl33_attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0,
};
// clang-format on
HGLRC gl33_context = wglCreateContextAttribsARB(gldc, 0, gl33_attribs);
if(!gl33_context) {
OutputDebugString("Failed to create OpenGL 3.3 context.");
return nullptr;
}
if(!wglMakeCurrent(gldc, gl33_context)) {
OutputDebugString("Failed to activate OpenGL 3.3 rendering context.");
return nullptr;
}
return gl33_context;
}
} // namespace
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd_line, int show) {
const HWND window = createWindow(inst);
const HDC gldc = GetDC(window);
const HGLRC glrc = initializeOpenGL(gldc);
ShowWindow(window, show);
UpdateWindow(window);
bool running = true;
while(running) {
MSG msg;
while(PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
if(msg.message == WM_QUIT) {
running = false;
} else {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Do OpenGL rendering here
SwapBuffers(gldc);
}
return 0;
}