-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGLContext.cpp
More file actions
67 lines (58 loc) · 2.14 KB
/
GLContext.cpp
File metadata and controls
67 lines (58 loc) · 2.14 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
#include "GLContext.h"
#include "EngineWindow.h"
#ifdef _DEBUG
void GLAPIENTRY glMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
GLsizei length, const GLchar* message,
const void* userParam) {
console::out() << "GL: " << message;
// if (type == GL_DEBUG_TYPE_ERROR || severity == GL_DEBUG_SEVERITY_HIGH)
// __debugbreak();
}
#endif
GLContext::GLContext(EngineWindow& window) {
assert(currentContext == nullptr);
currentContext = this;
window.makeContextCurrent();
static const bool glad =
(0 != gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress)));
if (!glad)
throw std::exception("Glad could not initialize OpenGl");
if (!GLAD_GL_VERSION_2_1)
throw std::exception("OpenGL 2.1 is not supported");
if (!GLAD_GL_EXT_texture_filter_anisotropic)
throw std::exception("Missing support for anisotropic textures");
IF_DEBUG(glEnable(GL_DEBUG_OUTPUT));
IF_DEBUG(glDebugMessageCallback(glMessageCallback, 0));
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST);
glEnable(GL_TEXTURE_2D);
glFogi(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 5);
glHint(GL_FOG_HINT, GL_NICEST); // Per-Pixel Fog Calculation
glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD); // Set Fog Based On Vertice Coordinates
}
GLContext::~GLContext() {
if (!wasReset) {
glfwMakeContextCurrent(nullptr);
}
currentContext = nullptr;
}
thread_local GLContext* GLContext::currentContext = nullptr;
void GLContext::checkGraphicsReset() {
if (GLAD_GL_ARB_robustness && currentContext) {
if (glGetGraphicsResetStatusARB() != GL_NO_ERROR) {
currentContext->wasReset = true;
std::chrono::milliseconds sleep_for{10};
std::chrono::milliseconds const sleep_for_max{100};
while (glGetGraphicsResetStatusARB() != GL_NO_ERROR) {
std::this_thread::sleep_for(sleep_for);
sleep_for = std::min(sleep_for * 2, sleep_for_max);
}
throw graphics_reset();
}
}
}