-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixedPipelineOpenGLX11.cpp
109 lines (88 loc) · 3.34 KB
/
fixedPipelineOpenGLX11.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
// STL
#include <array>
#include <optional>
#include <cstring>
#include <cstdlib>
#include <iostream>
// X11
#include <X11/Xlib.h>
// OpenGL
#include <GL/glx.h>
static auto parseProgramOptions(int argc, char **argv) -> std::optional<std::pair<uint32_t, uint32_t>> {
if(argc != 3) {
return std::nullopt;
}
return std::make_pair(std::stoi(argv[1]), std::stoi(argv[2]));
}
auto main(int argc, char *argv[]) -> int {
auto pDisplay = XOpenDisplay(nullptr);
if(pDisplay == nullptr) {
fprintf(stderr, "Cannot open display\n");
return EXIT_FAILURE;
}
auto width = 640U;
auto height = 480U;
if(const auto args = parseProgramOptions(argc, argv); args) {
constexpr auto primaryDisplay = 0;
auto screenID = ScreenOfDisplay(pDisplay, primaryDisplay);
width = std::min(args.value().first, static_cast<uint32_t>(screenID->width));
height = std::min(args.value().second, static_cast<uint32_t>(screenID->height));
}
auto screen = DefaultRootWindow(pDisplay);
// clang-format off
GLint defaultFramebufferAttributes[] = {
GLX_RGBA,
GLX_DEPTH_SIZE, 24,
GLX_DOUBLEBUFFER,
None
};
// clang-format on
auto visual = glXChooseVisual(pDisplay, 0, defaultFramebufferAttributes);
if(visual == nullptr) {
std::cerr << "Can not create visual\n";
return EXIT_FAILURE;
}
auto cmap = XCreateColormap(pDisplay, screen, visual->visual, AllocNone);
XSetWindowAttributes windowAttribs;
windowAttribs.border_pixel = BlackPixel(pDisplay, screen);
windowAttribs.background_pixel = WhitePixel(pDisplay, screen);
windowAttribs.override_redirect = True;
windowAttribs.colormap = cmap;
windowAttribs.event_mask = ExposureMask;
auto pWindow =
XCreateWindow(pDisplay, screen, 0, 0, width, height, 0, visual->depth, InputOutput, visual->visual, CWColormap | CWEventMask, &windowAttribs);
XMapWindow(pDisplay, pWindow);
XStoreName(pDisplay, pWindow, "Hello OpenGL X11!");
// Redirect Close
Atom atomWmDeleteWindow = XInternAtom(pDisplay, "WM_DELETE_WINDOW", False);
XSetWMProtocols(pDisplay, pWindow, &atomWmDeleteWindow, 1);
auto context = glXCreateContext(pDisplay, visual, nullptr, GL_TRUE);
glXMakeCurrent(pDisplay, pWindow, context);
std::cout << "GL Renderer: " << glGetString(GL_RENDERER) << "\n";
std::cout << "GL Version: " << glGetString(GL_VERSION) << "\n";
std::cout << "GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n";
glViewport(0, 0, static_cast<int>(width), static_cast<int>(height));
std::array<float, 4> clearColor= {0.1F, 0.1F, 0.15F, 1.F};
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
auto running = true;
while(running) {
if(XPending(pDisplay) > 0) {
XEvent event;
XNextEvent(pDisplay, &event);
if(event.type == ClientMessage) {
if(event.xclient.data.l[0] == static_cast<long>(atomWmDeleteWindow)) {
running = false;
}
} else if(event.type == DestroyNotify) {
running = false;
}
}
glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers(pDisplay, pWindow);
}
glXMakeCurrent(pDisplay, None, nullptr);
glXDestroyContext(pDisplay, context);
XDestroyWindow(pDisplay, pWindow);
XCloseDisplay(pDisplay);
return EXIT_SUCCESS;
}