summaryrefslogtreecommitdiff
path: root/include/mbgl/util/gl_helper.hpp
blob: 7d104fb80ebc9128689a48a0ac82bf52d6f05ecb (plain)
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
#ifndef MBGL_UTIL_GL_HELPER
#define MBGL_UTIL_GL_HELPER

#include <mbgl/platform/gl.hpp>

#include <array>

namespace {

template <typename T, T (*Create)(), void (*Destroy)(const T&)>
class Preserve {
public:
    Preserve() : data(Create()) {}
    ~Preserve() { Destroy(data); }

private:
    const T data;
};

inline bool getBlend() {
    return glIsEnabled(GL_BLEND);
}

inline void setBlend(const bool& enabled) {
    enabled ? MBGL_CHECK_ERROR(glEnable(GL_BLEND)) : MBGL_CHECK_ERROR(glDisable(GL_BLEND));
}

inline std::array<float, 4> getClearColor() {
    std::array<float, 4> color;
    MBGL_CHECK_ERROR(glGetFloatv(GL_COLOR_CLEAR_VALUE, color.data()));
    return color;
}

inline void setClearColor(const std::array<float, 4>& color) {
    MBGL_CHECK_ERROR(glClearColor(color[0], color[1], color[2], color[3]));
}


inline std::array<GLenum, 2> getBlendFunc() {
    GLint func[2];
    glGetIntegerv(GL_BLEND_SRC_ALPHA, &func[0]);
    glGetIntegerv(GL_BLEND_DST_ALPHA, &func[1]);
    return {{ static_cast<GLenum>(func[0]), static_cast<GLenum>(func[1]) }};
}

inline void setBlendFunc(const std::array<GLenum, 2>& func) {
    MBGL_CHECK_ERROR(glBlendFunc(func[0], func[1]));
}

#ifndef GL_ES_VERSION_2_0
inline std::array<double, 2> getPixelZoom() {
    std::array<double, 2> zoom;
    glGetDoublev(GL_ZOOM_X, &zoom[0]);
    glGetDoublev(GL_ZOOM_Y, &zoom[1]);
    return zoom;
}

inline void setPixelZoom(const std::array<double, 2>& func) {
    MBGL_CHECK_ERROR(glPixelZoom(func[0], func[1]));
}


inline std::array<double, 4> getRasterPos() {
    std::array<double, 4> pos;
    MBGL_CHECK_ERROR(glGetDoublev(GL_CURRENT_RASTER_POSITION, pos.data()));
    return pos;
}

inline void setRasterPos(const std::array<double, 4>& pos) {
    MBGL_CHECK_ERROR(glRasterPos4d(pos[0], pos[1], pos[2], pos[3]));
}
#endif
} // end anonymous namespace

namespace mbgl {
namespace gl {

using PreserveBlend = Preserve<bool, getBlend, setBlend>;
using PreserveClearColor = Preserve<std::array<float, 4>, getClearColor, setClearColor>;
using PreserveBlendFunc = Preserve<std::array<GLenum, 2>, getBlendFunc, setBlendFunc>;

#ifndef GL_ES_VERSION_2_0
using PreservePixelZoom = Preserve<std::array<double, 2>, getPixelZoom, setPixelZoom>;
using PreserveRasterPos = Preserve<std::array<double, 4>, getRasterPos, setRasterPos>;
#endif

} // namespace gl
} // namespace mbgl

#endif