diff options
| -rw-r--r-- | src/GFX/Mesh.hpp | 22 | ||||
| -rw-r--r-- | src/Math/MVP.cpp | 20 | ||||
| -rw-r--r-- | src/Math/MVP.hpp | 3 | ||||
| -rw-r--r-- | src/Util/ImageViewer.cpp | 38 | ||||
| -rw-r--r-- | src/Util/ImageViewer.hpp | 9 | ||||
| -rw-r--r-- | src/main.cpp | 13 |
6 files changed, 83 insertions, 22 deletions
diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp index 9d640b6..36a7dac 100644 --- a/src/GFX/Mesh.hpp +++ b/src/GFX/Mesh.hpp @@ -12,11 +12,25 @@ public: struct Attribute { template<size_t S = 3, typename T = float> Attribute( - std::vector<Vector<S, T>> data - ) : data((T*) data.data()), - data_size(data.size()), + std::vector<Vector<S, T>> v + ) : data_size(v.size()), attribute_size(S), - type_size(sizeof(T)) {}; + type_size(sizeof(T)) { + data = copy(v.data(), v.size() * S * type_size); + }; + + Attribute( + const Attribute& other + ) : data(copy(other.data, other.data_size * other.attribute_size * other.type_size)), + data_size(other.data_size), + attribute_size(other.attribute_size), + type_size(other.type_size) {}; + + static void* copy(void* ptr, uint32_t size) { + auto* buffer = new uint8_t[size]; + std::copy((uint8_t*)ptr, (uint8_t*)ptr + size, buffer); + return buffer; + } void* data; long data_size; diff --git a/src/Math/MVP.cpp b/src/Math/MVP.cpp index de9f2ee..754c656 100644 --- a/src/Math/MVP.cpp +++ b/src/Math/MVP.cpp @@ -18,7 +18,7 @@ Matrix<4, 4> view(Vector<3> position, Rotation angles) { return rotation * transformation; } -Matrix<4, 4> projection(float aspect, float fov, float near, float far) { +Matrix<4, 4> perspective_projection(float aspect, float fov, float near, float far) { auto fov_radians = Math::radians(fov); float x_scale = 1.0f / (tan(fov_radians / 2.0f) * aspect); @@ -38,4 +38,22 @@ Matrix<4, 4> projection(float aspect, float fov, float near, float far) { return projection; } +Matrix<4, 4> orthographic_projection(float width, float height, float near, float far) { + float x_scale = 2.0f / width; + float y_scale = 2.0f / -height; + + float frustum_length = far - near; + float z_near = -(far + near) / frustum_length; + float z_far = -2 / frustum_length; + + Matrix<4, 4> projection{ + x_scale, 0.0f, 0.0f, -1.0f, + 0.0f, y_scale, 0.0f, 1.0f, + 0.0f, 0.0f, z_near, z_far, + 0.0f, 0.0f, 0.0f, 1.0f, + }; + + return projection; +} + } diff --git a/src/Math/MVP.hpp b/src/Math/MVP.hpp index c3fb64f..0abad66 100644 --- a/src/Math/MVP.hpp +++ b/src/Math/MVP.hpp @@ -6,6 +6,7 @@ namespace Math::MVP { Matrix<4, 4> model(Vector<3> position, Rotation angles); Matrix<4, 4> view(Vector<3> position, Rotation angles); -Matrix<4, 4> projection(float aspect, float fov, float near, float far); +Matrix<4, 4> perspective_projection(float aspect, float fov, float near, float far); +Matrix<4, 4> orthographic_projection(float width, float height, float near, float far); } diff --git a/src/Util/ImageViewer.cpp b/src/Util/ImageViewer.cpp index 76faa3e..b4d8c6e 100644 --- a/src/Util/ImageViewer.cpp +++ b/src/Util/ImageViewer.cpp @@ -1,8 +1,29 @@ #include <GL/glew.h> #include "ImageViewer.hpp" +#include "../Math/MVP.hpp" namespace MC::Util { +ImageViewer::ImageViewer( + GFX::Image::RawImage& image +) : m_texture(image), + m_program( + {GFX::Shading::Shader::Type::Vertex, ImageViewer::vertex}, + {GFX::Shading::Shader::Type::Fragment, ImageViewer::fragment} + ), + m_mesh(GFX::Binder::load(default_mesh)) { + m_program.bind(); + auto model_uniform = m_program.uniform("model_matrix"); + auto view_uniform = m_program.uniform("view_matrix"); + auto projection_uniform = m_program.uniform("projection_matrix"); + + model_uniform.set(Math::MVP::model({}, {})); + view_uniform.set(Math::MVP::view({}, {})); + projection_uniform.set(Math::MVP::orthographic_projection(1000, 800, 0.0f, 100.0f)); + + m_program.unbind(); +} + void ImageViewer::render() { m_program.bind(); m_texture.bind(); @@ -16,10 +37,10 @@ void ImageViewer::render() { GFX::Mesh ImageViewer::create_default_mesh() { return {{ std::vector<Vector<3>>{ - {0.0f, 1.0f, 0.0f}, // top left - {0.0f, 0.0f, 0.0f}, // bottom left - {1.0f, 0.0f, 0.0f}, // bottom right - {1.0f, 0.0f, 0.0f } // top right + {300.0f, 200.0f, 0.0f}, // top left + {300.0f, 600.0f, 0.0f}, // bottom left + {700.0f, 600.0f, 0.0f}, // bottom right + {700.0f, 200.0f, 0.0f } // top right }, std::vector<Vector<2>>{ {1.0f, 1.0f}, @@ -33,13 +54,17 @@ GFX::Mesh ImageViewer::create_default_mesh() { const char* ImageViewer::vertex = R"v( #version 330 core +uniform mat4 model_matrix; +uniform mat4 view_matrix; +uniform mat4 projection_matrix; + layout (location = 0) in vec3 position; layout (location = 1) in vec2 tex_coord; out vec2 frag_tex_coord; void main() { - gl_Position = vec4(position, 1.0); + gl_Position = projection_matrix * view_matrix * model_matrix * vec4(position, 1.0); frag_tex_coord = tex_coord; })v"; @@ -52,8 +77,7 @@ in vec2 frag_tex_coord; out vec4 color; void main() { - //color = texture(image, frag_tex_coord); - color = vec4(0.3, 0.5, 1.0, 1.0); + color = texture(image, frag_tex_coord); })f"; } \ No newline at end of file diff --git a/src/Util/ImageViewer.hpp b/src/Util/ImageViewer.hpp index d583aa8..cb2dda0 100644 --- a/src/Util/ImageViewer.hpp +++ b/src/Util/ImageViewer.hpp @@ -9,14 +9,7 @@ namespace MC::Util { class ImageViewer { public: - explicit ImageViewer( - GFX::Image::RawImage& image - ) : m_texture(image), - m_program( - {GFX::Shading::Shader::Type::Vertex, ImageViewer::vertex}, - {GFX::Shading::Shader::Type::Fragment, ImageViewer::fragment} - ), - m_mesh(GFX::Binder::load(default_mesh)) {}; + explicit ImageViewer(GFX::Image::RawImage& image); void render(); private: diff --git a/src/main.cpp b/src/main.cpp index eafba6e..2589555 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "GFX/Texture.hpp" #include "GFX/Image/PPMParser.hpp" #include "World/World.hpp" +#include "Util/ImageViewer.hpp" #define APP_NAME "Meowcraft" @@ -49,6 +50,14 @@ void run() { glViewport(0, 0, w, h); }); + auto example_image = MC::GFX::Image::RawImage(10, 10, 3); + for (uint8_t x = 0; x < 10; x++) { + for (uint8_t y = 0; y < 10; y++) { + example_image.add({static_cast<uint8_t>(x * 20), 0, static_cast<uint8_t>(y * 20)}); + } + } + auto viewer = MC::Util::ImageViewer(example_image); + auto image = MC::GFX::Image::PPMParser(MC::Assets::Images::atlas).parse(); auto texture = MC::GFX::Texture(image); @@ -69,7 +78,7 @@ void run() { auto sun_direction_uniform = program.uniform("sun_direction"); program.bind(); - auto projection = Math::MVP::projection(ASPECT, FOV, 0.1f, 1000.0f); + auto projection = Math::MVP::perspective_projection(ASPECT, FOV, 0.1f, 1000.0f); projection_uniform.set(projection); Vector<3> sun_direction{1.0f, -1.0f, 0.0f}; @@ -105,6 +114,8 @@ void run() { render(chunk.mesh.value(), texture); } + viewer.render(); + time++; } } |
