Browse code

Add implementation

Robert Cranston authored on 29/12/2021 16:30:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,124 @@
1
+#ifndef GLTEXTURE2D_HPP_
2
+#define GLTEXTURE2D_HPP_
3
+
4
+
5
+#include <string>
6
+#include <utility>
7
+
8
+#include <globject.hpp>
9
+#include <gltexturend.hpp>
10
+
11
+
12
+/// Class
13
+
14
+class GLTexture2D : public GLTextureND<2>
15
+{
16
+public:
17
+
18
+    /// Special member functions
19
+
20
+    explicit GLTexture2D(
21
+        std::string object_label,
22
+        Size        size,
23
+        GLenum      internal_format = GL_RGBA8,
24
+        GLenum      wrap            = GL_REPEAT,
25
+        GLenum      min_filter      = GL_LINEAR_MIPMAP_LINEAR,
26
+        GLenum      mag_filter      = GL_LINEAR
27
+    );
28
+
29
+    /// Core
30
+
31
+    GLTexture2D static const & empty();
32
+
33
+    /// TGA
34
+
35
+    GLTexture2D static tga_read(
36
+        Path const & path,
37
+        GLenum       format          = GL_BGRA,
38
+        GLenum       internal_format = GL_SRGB8_ALPHA8,
39
+        GLenum       wrap            = GL_REPEAT,
40
+        GLenum       min_filter      = GL_LINEAR_MIPMAP_LINEAR,
41
+        GLenum       mag_filter      = GL_LINEAR
42
+    );
43
+
44
+    void tga_write(
45
+        Path const & path,
46
+        GLenum       format = GL_BGRA
47
+    ) const;
48
+
49
+private:
50
+
51
+    /// Core
52
+
53
+    void virtual data_(
54
+        void const * data,
55
+        GLenum       target,
56
+        GLenum       format,
57
+        GLenum       type
58
+    ) override;
59
+};
60
+
61
+
62
+/// Special member functions
63
+
64
+inline GLTexture2D::GLTexture2D(
65
+    std::string object_label,
66
+    Size        size,
67
+    GLenum      internal_format,
68
+    GLenum      wrap,
69
+    GLenum      min_filter,
70
+    GLenum      mag_filter
71
+)
72
+:
73
+    GLTextureND(
74
+        std::move(object_label),
75
+        GL_TEXTURE_2D,
76
+        GL_TEXTURE_BINDING_2D,
77
+        GL_MAX_TEXTURE_SIZE,
78
+        size,
79
+        internal_format,
80
+        wrap,
81
+        min_filter,
82
+        mag_filter
83
+    )
84
+{
85
+    try
86
+    {
87
+        glTexImage2D(
88
+            target_, 0,
89
+            (GLint)internal_format,
90
+            size[0], size[1], 0,
91
+            GL_RED, GL_UNSIGNED_BYTE, nullptr
92
+        );
93
+        check_error_(glGetError());
94
+    }
95
+    catch (...)
96
+    {
97
+        fail_action_("create");
98
+    }
99
+}
100
+
101
+/// Core
102
+
103
+inline GLTexture2D const & GLTexture2D::empty()
104
+{
105
+    return empty_<GLTexture2D>();
106
+}
107
+
108
+inline void GLTexture2D::data_(
109
+    void const * data,
110
+    GLenum       target,
111
+    GLenum       format,
112
+    GLenum       type
113
+)
114
+{
115
+    glTexSubImage2D(
116
+        target, 0,
117
+        0, 0,
118
+        size()[0], size()[1],
119
+        format, type, data
120
+    );
121
+}
122
+
123
+
124
+#endif // GLTEXTURE2D_HPP_