-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLHelper.hpp
More file actions
146 lines (139 loc) · 3.91 KB
/
GLHelper.hpp
File metadata and controls
146 lines (139 loc) · 3.91 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
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
#pragma once
#include <glbinding/Binding.h>
#include <glbinding/gl33core/gl.h>
#include <vector>
#include <string>
using namespace gl33core;
class Shader {
GLuint _handle = 0;
public:
Shader(GLenum type, const std::string& src) {
_handle = glCreateShader(type);
const char* srcList[] = {src.c_str()};
glShaderSource(_handle, 1, srcList, nullptr);
glCompileShader(_handle);
GLint compilationOk;
glGetShaderiv(_handle, GL_COMPILE_STATUS, &compilationOk);
if (!compilationOk)
{
GLint errLength;
glGetShaderiv(_handle, GL_INFO_LOG_LENGTH, &errLength);
std::string errLog(errLength, '\0');
glGetShaderInfoLog(_handle, errLength, &errLength, errLog.data());
throw std::runtime_error("shader compilation failed:" + errLog);
}
}
Shader(Shader&& other) noexcept {
_handle = other._handle;
other._handle = 0;
}
virtual ~Shader() {
if (_handle) {
glDeleteShader(_handle);
}
}
GLuint handle() const {return _handle;}
};
class RenderProgram {
GLuint _handle = 0;
public:
RenderProgram(const std::vector<Shader>& shaders)
{
_handle = glCreateProgram();
for(const auto& shader: shaders) {
glAttachShader(_handle, shader.handle());
}
glLinkProgram(_handle);
GLint isLinked = 0;
glGetProgramiv(_handle, GL_LINK_STATUS, &isLinked);
if(!isLinked) {
auto errCode = glGetError();
GLint maxLength;
glGetProgramiv(_handle, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(_handle, maxLength, &maxLength, &infoLog[0]);
throw std::runtime_error(std::string("failed to link program: ") +
std::to_string(static_cast<int>(errCode)) +
reinterpret_cast<const char*>(infoLog.data()));
}
}
RenderProgram(RenderProgram&& other) noexcept {
_handle = other._handle;
other._handle = 0;
}
virtual ~RenderProgram()
{
if (_handle)
glDeleteProgram(_handle);
}
void use() const
{
glUseProgram(_handle);
}
GLuint getUniformLocation(const char* name) const
{
return glGetUniformLocation(_handle, name);
}
};
class VertexArray {
GLuint _handle = 0;
public:
VertexArray() {
glGenVertexArrays(1, &_handle);
}
VertexArray(VertexArray&& other) noexcept {
_handle = other._handle;
other._handle = 0;
}
virtual ~VertexArray() {
if (_handle) {
glDeleteVertexArrays(1, &_handle);
}
}
void bind() {
glBindVertexArray(_handle);
}
};
class RenderBuffer {
GLuint _handle = 0;
friend class FrameBuffer;
public:
RenderBuffer() {
glGenRenderbuffers(1, &_handle);
}
virtual ~RenderBuffer() {
if (_handle)
glDeleteRenderbuffers(1, &_handle);
}
RenderBuffer(RenderBuffer&& other) noexcept {
_handle = other._handle;
other._handle = 0;
}
void bind() {
glBindRenderbuffer(GL_RENDERBUFFER, _handle);
}
};
class FrameBuffer {
GLuint _handle = 0;
public:
FrameBuffer() {
glGenFramebuffers(1, &_handle);
}
virtual ~FrameBuffer() {
if (_handle)
glDeleteFramebuffers(1, &_handle);
}
FrameBuffer(FrameBuffer&& other) noexcept {
_handle = other._handle;
other._handle = 0;
}
void bind() {
glBindFramebuffer(GL_FRAMEBUFFER, _handle);
}
void renderBuffer(const RenderBuffer& rb) {
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb._handle);
}
bool check() {
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
}
};