diff options
Diffstat (limited to 'src/renderer/texture.cpp')
-rw-r--r-- | src/renderer/texture.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp new file mode 100644 index 0000000..2728665 --- /dev/null +++ b/src/renderer/texture.cpp | |||
@@ -0,0 +1,124 @@ | |||
1 | #include "texture.h" | ||
2 | #include <stdexcept> | ||
3 | #include "renderer.h" | ||
4 | #include "util.h" | ||
5 | |||
6 | // include stb_image | ||
7 | #define STBI_ONLY_PNG | ||
8 | #define STBI_ONLY_BMP | ||
9 | #include "stb_image.h" | ||
10 | |||
11 | Texture::Texture( | ||
12 | int width, | ||
13 | int height) : | ||
14 | width_(width), | ||
15 | height_(height) | ||
16 | { | ||
17 | if (!Renderer::isSingletonInitialized()) | ||
18 | { | ||
19 | throw std::logic_error("Renderer needs to be initialized"); | ||
20 | } | ||
21 | |||
22 | glBindTexture(GL_TEXTURE_2D, texture_.getId()); | ||
23 | glTexImage2D( | ||
24 | GL_TEXTURE_2D, | ||
25 | 0, | ||
26 | GL_RGBA, | ||
27 | width_, | ||
28 | height_, | ||
29 | 0, | ||
30 | GL_RGBA, | ||
31 | GL_UNSIGNED_BYTE, | ||
32 | 0); | ||
33 | |||
34 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
35 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
36 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
37 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
38 | } | ||
39 | |||
40 | Texture::Texture(const char* filename) | ||
41 | { | ||
42 | if (!Renderer::isSingletonInitialized()) | ||
43 | { | ||
44 | throw std::logic_error("Renderer needs to be initialized"); | ||
45 | } | ||
46 | |||
47 | glBindTexture(GL_TEXTURE_2D, texture_.getId()); | ||
48 | unsigned char* data = stbi_load(filename, &width_, &height_, 0, 4); | ||
49 | flipImageData(data, width_, height_, 4); | ||
50 | glTexImage2D( | ||
51 | GL_TEXTURE_2D, | ||
52 | 0, | ||
53 | GL_RGBA, | ||
54 | width_, | ||
55 | height_, | ||
56 | 0, | ||
57 | GL_RGBA, | ||
58 | GL_UNSIGNED_BYTE, | ||
59 | data); | ||
60 | |||
61 | stbi_image_free(data); | ||
62 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
63 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
64 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
65 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
66 | } | ||
67 | |||
68 | Texture::Texture( | ||
69 | const Texture& tex) : | ||
70 | width_(tex.width_), | ||
71 | height_(tex.height_) | ||
72 | { | ||
73 | if (!Renderer::isSingletonInitialized()) | ||
74 | { | ||
75 | throw std::logic_error("Renderer needs to be initialized"); | ||
76 | } | ||
77 | |||
78 | unsigned char* data = new unsigned char[4 * width_ * height_]; | ||
79 | glBindTexture(GL_TEXTURE_2D, tex.getId()); | ||
80 | glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); | ||
81 | |||
82 | glBindTexture(GL_TEXTURE_2D, texture_.getId()); | ||
83 | glTexImage2D( | ||
84 | GL_TEXTURE_2D, | ||
85 | 0, | ||
86 | GL_RGBA, | ||
87 | width_, | ||
88 | height_, | ||
89 | 0, | ||
90 | GL_RGBA, | ||
91 | GL_UNSIGNED_BYTE, | ||
92 | data); | ||
93 | |||
94 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
95 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
96 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
97 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
98 | |||
99 | delete[] data; | ||
100 | } | ||
101 | |||
102 | Texture::Texture(Texture&& tex) : Texture(0, 0) | ||
103 | { | ||
104 | swap(*this, tex); | ||
105 | } | ||
106 | |||
107 | Texture& Texture::operator= (Texture tex) | ||
108 | { | ||
109 | swap(*this, tex); | ||
110 | |||
111 | return *this; | ||
112 | } | ||
113 | |||
114 | void swap(Texture& tex1, Texture& tex2) | ||
115 | { | ||
116 | std::swap(tex1.width_, tex2.width_); | ||
117 | std::swap(tex1.height_, tex2.height_); | ||
118 | std::swap(tex1.texture_, tex2.texture_); | ||
119 | } | ||
120 | |||
121 | Rectangle Texture::entirety() const | ||
122 | { | ||
123 | return {0, 0, width_, height_}; | ||
124 | } | ||