summaryrefslogtreecommitdiff
path: root/src/GFX
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-06-12 17:09:55 +0200
committerMel <einebeere@gmail.com>2023-06-12 17:14:03 +0200
commitd0de60dc33df75fbcacb53a09568b14d0fd48cb9 (patch)
tree7aefdbb81f114552881834bd5b0d842bc2bdb691 /src/GFX
parent23b0bc4d1ddc9fad3c32e8257497ddd13ac6a155 (diff)
downloadmeowcraft-d0de60dc33df75fbcacb53a09568b14d0fd48cb9.tar.zst
meowcraft-d0de60dc33df75fbcacb53a09568b14d0fd48cb9.zip
Multithreaded world generation with Perlin
Diffstat (limited to 'src/GFX')
-rw-r--r--src/GFX/Binder.cpp2
-rw-r--r--src/GFX/Binder.hpp2
-rw-r--r--src/GFX/Camera.hpp3
-rw-r--r--src/GFX/Image/PPMParser.cpp2
-rw-r--r--src/GFX/Image/RawImage.cpp28
-rw-r--r--src/GFX/Image/RawImage.hpp14
-rw-r--r--src/GFX/Mesh.hpp2
-rw-r--r--src/GFX/Shading/Program.hpp2
-rw-r--r--src/GFX/Shading/Uniform.hpp2
-rw-r--r--src/GFX/Texture.cpp4
10 files changed, 49 insertions, 12 deletions
diff --git a/src/GFX/Binder.cpp b/src/GFX/Binder.cpp
index 0cc844d..0dce06d 100644
--- a/src/GFX/Binder.cpp
+++ b/src/GFX/Binder.cpp
@@ -4,7 +4,7 @@
namespace MC::GFX {
-BindableMesh Binder::load(Mesh& mesh) {
+BindableMesh Binder::load(Mesh mesh) {
auto vao = create_vao();
if (!mesh.indices().empty()) {
store_indices(mesh.indices().data(), mesh.indices().size());
diff --git a/src/GFX/Binder.hpp b/src/GFX/Binder.hpp
index 3d3949b..d8a2be9 100644
--- a/src/GFX/Binder.hpp
+++ b/src/GFX/Binder.hpp
@@ -35,7 +35,7 @@ class Binder {
public:
Binder() = default;;
- static BindableMesh load(Mesh& mesh);
+ static BindableMesh load(Mesh mesh);
private:
static uint32_t create_vao();
diff --git a/src/GFX/Camera.hpp b/src/GFX/Camera.hpp
index f03f009..77a210f 100644
--- a/src/GFX/Camera.hpp
+++ b/src/GFX/Camera.hpp
@@ -1,7 +1,6 @@
#pragma once
-#include "../Math/Math.hpp"
-#include "../Math/Rotation.hpp"
+#include "../Math/Common.hpp"
namespace MC::GFX {
diff --git a/src/GFX/Image/PPMParser.cpp b/src/GFX/Image/PPMParser.cpp
index 31be0ab..cf1bf77 100644
--- a/src/GFX/Image/PPMParser.cpp
+++ b/src/GFX/Image/PPMParser.cpp
@@ -17,7 +17,7 @@ RawImage PPMParser::parse() {
auto pixel_count = header.width * header.height;
- RawImage image(header.width, header.height, 3);
+ RawImage image(header.width, header.height);
for (uint64_t pixel_index = 0; pixel_index < pixel_count; pixel_index++) {
RawImage::Pixel pixel = parse_pixel(header.max_color);
image.add(pixel);
diff --git a/src/GFX/Image/RawImage.cpp b/src/GFX/Image/RawImage.cpp
index aca8fbc..1222fab 100644
--- a/src/GFX/Image/RawImage.cpp
+++ b/src/GFX/Image/RawImage.cpp
@@ -2,6 +2,19 @@
namespace MC::GFX::Image {
+RawImage::RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height)
+ : m_pixels(), m_width(width), m_height(height)
+{
+ m_pixels.reserve(width * height);
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ auto result = sampler.sample({(float)x, (float)y});
+ auto intensity = (uint8_t)(result * 255);
+ add({intensity, intensity, intensity});
+ }
+ }
+}
+
void RawImage::add(RawImage::Pixel pixel) {
m_pixels.push_back(pixel);
}
@@ -26,4 +39,19 @@ uint8_t RawImage::channels() const {
return m_channels;
}
+std::string RawImage::string() const {
+ std::stringstream str{};
+
+ bool comma = false;
+ str << "[";
+ for (const auto& pixel : m_pixels) {
+ if (comma) { str << ", "; }
+ str << "{r=" << (uint)pixel.r << ", g=" << (uint)pixel.g << ", b=" << (uint)pixel.r << "}";
+ comma = true;
+ }
+ str << "]";
+
+ return str.str();
+}
+
} \ No newline at end of file
diff --git a/src/GFX/Image/RawImage.hpp b/src/GFX/Image/RawImage.hpp
index 2b10cb0..8a837e6 100644
--- a/src/GFX/Image/RawImage.hpp
+++ b/src/GFX/Image/RawImage.hpp
@@ -2,19 +2,23 @@
#include <cstdint>
#include <cstddef>
+#include <string>
#include <vector>
+#include "../../Util/Sampler.hpp"
namespace MC::GFX::Image {
class RawImage {
public:
- RawImage() : m_pixels(), m_width(0), m_height(0), m_channels(0) {};
+ RawImage() : m_pixels(), m_width(0), m_height(0) {};
- explicit RawImage(uint32_t width, uint32_t height, uint8_t channels)
- : m_pixels(), m_width(width), m_height(height), m_channels(channels) {
+ RawImage(uint32_t width, uint32_t height)
+ : m_pixels(), m_width(width), m_height(height) {
m_pixels.reserve(width * height);
}
+ RawImage(Util::Sampler<2, float>& sampler, uint32_t width, uint32_t height);
+
struct Pixel {
uint8_t r, g, b;
};
@@ -27,11 +31,13 @@ public:
uint32_t width() const;
uint32_t height() const;
uint8_t channels() const;
+
+ std::string string() const;
private:
std::vector<Pixel> m_pixels;
uint32_t m_width, m_height;
- uint8_t m_channels;
+ uint8_t m_channels = 3;
};
}
diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp
index 36a7dac..bfe8eab 100644
--- a/src/GFX/Mesh.hpp
+++ b/src/GFX/Mesh.hpp
@@ -3,7 +3,7 @@
#include <utility>
#include <vector>
#include <cstdint>
-#include "../Math/Math.hpp"
+#include "../Math/Common.hpp"
namespace MC::GFX {
diff --git a/src/GFX/Shading/Program.hpp b/src/GFX/Shading/Program.hpp
index b7db953..6b28de0 100644
--- a/src/GFX/Shading/Program.hpp
+++ b/src/GFX/Shading/Program.hpp
@@ -3,7 +3,7 @@
#include <string>
#include <vector>
#include "Shader.hpp"
-#include "../../Math/Math.hpp"
+#include "../../Math/Common.hpp"
#include "Uniform.hpp"
namespace MC::GFX::Shading {
diff --git a/src/GFX/Shading/Uniform.hpp b/src/GFX/Shading/Uniform.hpp
index 8035dfe..3c14315 100644
--- a/src/GFX/Shading/Uniform.hpp
+++ b/src/GFX/Shading/Uniform.hpp
@@ -3,7 +3,7 @@
#include <cstdint>
#include <string>
#include <utility>
-#include "../../Math/Math.hpp"
+#include "../../Math/Common.hpp"
namespace MC::GFX::Shading {
diff --git a/src/GFX/Texture.cpp b/src/GFX/Texture.cpp
index f5474df..ef48631 100644
--- a/src/GFX/Texture.cpp
+++ b/src/GFX/Texture.cpp
@@ -13,6 +13,10 @@ Texture::Texture(const Image::RawImage& image) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ // If this is not here, odd-numbered rows (like those in i.e. 5x5 textures)
+ // 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());
glGenerateMipmap(GL_TEXTURE_2D);