summaryrefslogtreecommitdiff
path: root/src/GFX
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-07-08 01:20:53 +0200
committerMel <einebeere@gmail.com>2023-07-08 01:20:53 +0200
commit41fbca10f6c6cdd9c1623f1347e7ecb40f5e7f59 (patch)
tree7a41651c7c1eeecfc422395cbdb4b26ac619eaf9 /src/GFX
parentf8c0fb7e5d4cd0139b2b287980149eca688803bd (diff)
downloadmeowcraft-41fbca10f6c6cdd9c1623f1347e7ecb40f5e7f59.tar.zst
meowcraft-41fbca10f6c6cdd9c1623f1347e7ecb40f5e7f59.zip
Add crude texture transparency
Diffstat (limited to 'src/GFX')
-rw-r--r--src/GFX/Image/PPMParser.cpp16
-rw-r--r--src/GFX/Image/PPMParser.hpp2
-rw-r--r--src/GFX/Image/RawImage.cpp4
-rw-r--r--src/GFX/Image/RawImage.hpp2
-rw-r--r--src/GFX/Texture.cpp2
5 files changed, 17 insertions, 9 deletions
diff --git a/src/GFX/Image/PPMParser.cpp b/src/GFX/Image/PPMParser.cpp
index f002db8..1f62871 100644
--- a/src/GFX/Image/PPMParser.cpp
+++ b/src/GFX/Image/PPMParser.cpp
@@ -68,12 +68,18 @@ RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) {
auto map_to_range = [=](uint64_t s) -> uint8_t { return s * 255 / max_color; };
- RawImage::Pixel pixel{};
- pixel.r = map_to_range(r_sample);
- pixel.g = map_to_range(g_sample);
- pixel.b = map_to_range(b_sample);
+ RawImage::Pixel p{
+ map_to_range(r_sample),
+ map_to_range(g_sample),
+ map_to_range(b_sample),
+ 255
+ };
- return pixel;
+ if (p.r == alpha_color.r && p.g == alpha_color.g && p.b == alpha_color.b) {
+ p.a = 0;
+ }
+
+ return p;
}
uint64_t PPMParser::parse_sample() {
diff --git a/src/GFX/Image/PPMParser.hpp b/src/GFX/Image/PPMParser.hpp
index 07ff3c2..a7e9b41 100644
--- a/src/GFX/Image/PPMParser.hpp
+++ b/src/GFX/Image/PPMParser.hpp
@@ -37,6 +37,8 @@ private:
bool is_eof() const;
+ static constexpr RawImage::Pixel alpha_color{192, 0, 255};
+
std::string_view m_source;
uint64_t m_cursor = 0;
};
diff --git a/src/GFX/Image/RawImage.cpp b/src/GFX/Image/RawImage.cpp
index 6cb06b2..0bd2947 100644
--- a/src/GFX/Image/RawImage.cpp
+++ b/src/GFX/Image/RawImage.cpp
@@ -28,9 +28,9 @@ std::string RawImage::string() const {
bool comma = false;
str << "[";
- for (const auto [r, g, b] : m_pixels) {
+ for (const auto [r, g, b, a] : m_pixels) {
if (comma) { str << ", "; }
- str << "{r=" << (uint)r << ", g=" << (uint)g << ", b=" << (uint)r << "}";
+ str << "{r=" << (uint)r << ", g=" << (uint)g << ", b=" << (uint)b << ", a=" << (uint)a << "}";
comma = true;
}
str << "]";
diff --git a/src/GFX/Image/RawImage.hpp b/src/GFX/Image/RawImage.hpp
index be3c83a..809ddd9 100644
--- a/src/GFX/Image/RawImage.hpp
+++ b/src/GFX/Image/RawImage.hpp
@@ -16,7 +16,7 @@ public:
}
struct Pixel {
- uint8_t r, g, b;
+ uint8_t r, g, b, a;
};
void add(Pixel pixel);
diff --git a/src/GFX/Texture.cpp b/src/GFX/Texture.cpp
index 64151a5..16f7dce 100644
--- a/src/GFX/Texture.cpp
+++ b/src/GFX/Texture.cpp
@@ -17,7 +17,7 @@ Texture::Texture(const Image::RawImage& image) {
// will begin at the incorrect byte, causing color artifacts.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.raw());
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.raw());
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);