diff options
Diffstat (limited to 'src/Util/ImageViewer.cpp')
| -rw-r--r-- | src/Util/ImageViewer.cpp | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/Util/ImageViewer.cpp b/src/Util/ImageViewer.cpp index b4d8c6e..7fd5867 100644 --- a/src/Util/ImageViewer.cpp +++ b/src/Util/ImageViewer.cpp @@ -5,13 +5,14 @@ namespace MC::Util { ImageViewer::ImageViewer( - GFX::Image::RawImage& image + GFX::Image::RawImage& image, + float window_aspect ) : 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_mesh(GFX::Binder::load(create_mesh(window_aspect, image.width(), image.height()))) { m_program.bind(); auto model_uniform = m_program.uniform("model_matrix"); auto view_uniform = m_program.uniform("view_matrix"); @@ -19,7 +20,7 @@ ImageViewer::ImageViewer( 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)); + projection_uniform.set(Math::MVP::orthographic_projection(1000 * window_aspect, 1000, 0.0f, 100.0f)); m_program.unbind(); } @@ -34,19 +35,27 @@ void ImageViewer::render() { m_program.unbind(); } -GFX::Mesh ImageViewer::create_default_mesh() { +GFX::Mesh ImageViewer::create_mesh(float window_aspect, uint32_t image_width, uint32_t image_height) { + auto aspect = (float)image_width / image_height; + float max_size = 500.0f; + float width = max_size * std::min(1.0f, aspect); + float height = max_size * std::min(1.0f, 1/aspect); + + float x = max_size * window_aspect - width / 2.0f; + float y = max_size - height / 2.0f; + return {{ std::vector<Vector<3>>{ - {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 + {x, y, 0.0f}, // top left + {x, y + height, 0.0f}, // bottom left + {x + width, y + height, 0.0f}, // bottom right + {x + width, y, 0.0f} // top right }, std::vector<Vector<2>>{ - {1.0f, 1.0f}, - {1.0f, 0.0f,}, {0.0f, 0.0f}, {0.0f, 1.0f}, + {1.0f, 1.0f}, + {1.0f, 0.0f}, }, }, {0, 1, 2, 0, 2, 3}}; } |
