-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpenGLHelper.cpp
126 lines (111 loc) · 4.11 KB
/
OpenGLHelper.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
#include "OpenGLHelper.h"
#include <iostream>
#include <string>
#include "imgui.h"
#include "imgui_impl_opengl3.h"
#include <SDL.h>
// below because "The declaration of a static data member in its class definition is not a definition"
OpenGLHelper* OpenGLHelper::s_instance;
//////////////////////////////////////////////////////////////////////////
// Basic singleton methods
//////////////////////////////////////////////////////////////////////////
void OpenGLHelper::Initialize()
{
for (size_t i = 0; i < _SDHR_MAX_TEXTURES; i++)
{
v_texture_ids.push_back(UINT_MAX);
}
}
OpenGLHelper::~OpenGLHelper()
{
for (auto texid : v_texture_ids)
{
glDeleteTextures(1, &texid);
}
}
//////////////////////////////////////////////////////////////////////////
// Methods
//////////////////////////////////////////////////////////////////////////
// Sets the correct gl version and returns the glsl version string
void OpenGLHelper::set_gl_version()
{
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 3.0 + GLSL 300 es
// ImGui only supports 3.0, not 3.1
glsl_version = "#version 300 es";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__)
// GL 4.1 Core + GLSL 410
glsl_version = "#version 410";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
#else
// GL 4.1 Core + GLSL 410
glsl_version = "#version 410";
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0) != 0)
std::cerr << "SDL Error: " << SDL_GetError() << std::endl;
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0)
std::cerr << "SDL Error: " << SDL_GetError() << std::endl;
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4) != 0)
std::cerr << "SDL Error: " << SDL_GetError() << std::endl;
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1) != 0)
std::cerr << "SDL Error: " << SDL_GetError() << std::endl;
#endif
}
const std::string* OpenGLHelper::get_glsl_version()
{
return &glsl_version;
}
// This method loads the texture data into the texture specified at textureID
void OpenGLHelper::load_texture(unsigned char* data, int width, int height, int nrComponents, GLuint textureID)
{
GLenum glerr;
GLenum format = GL_RGBA;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGBA;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL load_texture glBindTexture error: " << glerr << std::endl;
}
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL load_texture glTexImage2D error: " << glerr << std::endl;
}
// NOTE: May need to generate mipmaps in case we want to allow zooming in-out
// But then we need to change the GL_TEXTURE_MIN_FILTER to GL_XXX_MIPMAP_XXX
//glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Note: Could also use GL_LINEAR, need to test
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL load_texture glTexParameteri error: " << glerr << std::endl;
}
}
GLuint OpenGLHelper::get_texture_id_at_slot(int slot)
{
if (slot >= v_texture_ids.size())
{
#ifdef DEBUG
std::cerr << "ERROR: Requesting a texture slot above _SDHR_MAX_TEXTURES!\n";
#endif
return UINT_MAX;
}
GLuint texid = v_texture_ids.at(slot);
if (texid == UINT_MAX)
{
glGenTextures(1, &texid);
v_texture_ids.at(slot) = texid;
}
return texid;
}