-
Notifications
You must be signed in to change notification settings - Fork 172
/
glObjects.cpp
97 lines (81 loc) · 2.61 KB
/
glObjects.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
#include "glObjects.h"
#include <graphics/opengl.h>
#include <type_traits>
#include <cassert>
static_assert(std::is_same<uint32_t, GLuint>::value, "GLuint and uint32_t are *NOT* the same: troubles!");
#define GL_CHECK(statement) statement
namespace gl
{
namespace details
{
void createBuffers(size_t count, uint32_t* buffers)
{
GL_CHECK(glGenBuffers(count, buffers));
}
void deleteBuffers(size_t count, const uint32_t* buffers)
{
GL_CHECK(glDeleteBuffers(count, buffers));
}
void createTextures(size_t count, uint32_t* textures)
{
GL_CHECK(glGenTextures(count, textures));
}
void deleteTextures(size_t count, const uint32_t* textures)
{
GL_CHECK(glDeleteTextures(count, textures));
}
}
ScopedBufferBinding::ScopedBufferBinding(uint32_t target, uint32_t buffer)
:target{target}, buffer{buffer}
{
GL_CHECK(glBindBuffer(target, buffer));
}
ScopedBufferBinding::~ScopedBufferBinding()
{
GL_CHECK(glBindBuffer(target, GL_NONE));
}
ScopedVertexAttribArray::ScopedVertexAttribArray(int32_t attrib)
:attrib{ attrib }
{
if (attrib != -1)
GL_CHECK(glEnableVertexAttribArray(attrib));
}
ScopedVertexAttribArray::~ScopedVertexAttribArray()
{
if (attrib != -1)
GL_CHECK(glDisableVertexAttribArray(attrib));
}
ScopedVertexAttribArray::ScopedVertexAttribArray(ScopedVertexAttribArray&& other) noexcept
:attrib{other.attrib}
{
other.attrib = -1;
}
ScopedVertexAttribArray& ScopedVertexAttribArray::operator=(ScopedVertexAttribArray&& other) noexcept
{
if (this != &other)
{
if (attrib != -1 && attrib != other.attrib)
GL_CHECK(glDisableVertexAttribArray(attrib));
attrib = other.attrib;
other.attrib = -1;
}
return *this;
}
ScopedTexture::ScopedTexture(uint32_t target, uint32_t texture)
:target{ target }, texture{ texture }
{
// ES2 only supports 2D textures and cubemaps.
GL_CHECK(glGetIntegerv(target == GL_TEXTURE_2D ? GL_TEXTURE_BINDING_2D : GL_TEXTURE_BINDING_CUBE_MAP, (int32_t*)&previously_bound));
if (previously_bound != texture)
GL_CHECK(glBindTexture(target, texture));
}
ScopedTexture::~ScopedTexture()
{
if (previously_bound != texture)
GL_CHECK(glBindTexture(target, previously_bound));
}
bool isAvailable()
{
return true;
}
} // namespace gl