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,90 @@
1
+#include <gltexturecubemap.hpp>
2
+
3
+#include <ostream>
4
+
5
+#include <globject.hpp>
6
+#include <gltexturend.hpp>
7
+
8
+// NOLINTNEXTLINE
9
+#define STR_EXCEPTION GLObject<>::Exception
10
+#include <str.hpp>
11
+
12
+
13
+/// TGA
14
+
15
+
16
+GLTextureCubemap GLTextureCubemap::tga_read(
17
+    Paths const & paths,
18
+    GLenum        format,
19
+    GLenum        internal_format,
20
+    GLenum        wrap,
21
+    GLenum        min_filter,
22
+    GLenum        mag_filter
23
+)
24
+try
25
+{
26
+    check_cubemap_paths_(paths);
27
+    auto texture = GLTextureCubemap(
28
+        str_paths_(paths),
29
+        TGA::read(paths[0]).size(),
30
+        internal_format,
31
+        wrap,
32
+        min_filter,
33
+        mag_filter
34
+    );
35
+    for (auto face = GLuint{0}; face < faces_; ++face)
36
+        texture.data(
37
+            TGA::read(path_prefix_(paths[face], prefix())).data(),
38
+            GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
39
+            format
40
+        );
41
+    return texture;
42
+}
43
+catch (...)
44
+{
45
+    STR_THROW_NESTED(
46
+        "Failed to read GLTextureCubemap TGA " << str_paths_(paths) << ":"
47
+    );
48
+}
49
+
50
+
51
+void GLTextureCubemap::tga_write(
52
+    Paths const & paths,
53
+    GLenum        format
54
+) const
55
+try
56
+{
57
+    check_cubemap_paths_(paths);
58
+    for (auto face = GLuint{0}; face < faces_; ++face)
59
+        TGA(size(), data(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, format))
60
+            .write(path_prefix_(paths[face], prefix()));
61
+}
62
+catch (...)
63
+{
64
+    STR_THROW_NESTED(
65
+        "Failed to write GLTextureCubemap TGA " << str_paths_(paths) << ":"
66
+    );
67
+}
68
+
69
+
70
+/// Check
71
+
72
+
73
+void GLTextureCubemap::check_cubemap_paths_(Paths const & paths)
74
+{
75
+    if (paths.size() != faces_)
76
+        STR_THROW(
77
+            "Expected number of paths " << GLuint{faces_} << ", " <<
78
+            "got "                      << paths.size()   << "."
79
+        );
80
+}
81
+
82
+
83
+void GLTextureCubemap::check_cubemap_size_() const
84
+{
85
+    if (size()[0] != size()[1])
86
+        STR_THROW(
87
+            "Expected size " << "to be square"    << ", " <<
88
+            "got "           << str_size_(size()) << "."
89
+        );
90
+}