From 9dba30e23f0f14792674020869803674cc783fb6 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Sun, 29 Nov 2026 22:21:45 +0200 Subject: [PATCH 1/4] document the top-left origin window coordinate convention The window coordinate system used by the renderers (to_window_coords, get/set_viewport, get/set_scissor) is always top-left origin (x right, y down). Document this in the context's API docs so the renderers have a single canonical convention to implement. --- src/ruis/render/context.hpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/ruis/render/context.hpp b/src/ruis/render/context.hpp index 83a11936d..8b1ac5f0f 100644 --- a/src/ruis/render/context.hpp +++ b/src/ruis/render/context.hpp @@ -204,10 +204,7 @@ class context : public std::enable_shared_from_this * @brief Get window coordinates of a point in renderer's clipping coordinates. * Renderer's clipping coordinates of a point are coordinates after all matrix transformations * and perspective division. - * The window coordinate system is renderer-specific. It can be top-bottom or bottom-top. - * For example, OpenGL/ES renderer has bottom-top coordinate system, i.e. origin is - * at the bottom left corner of the window and y-axis goes up. So, the resulting position of a point - * should be interpreted in context of the specific renderer. + * The window coordinate system is always top-left origin (x right, y down). * @return Window coordinates of the point. */ virtual r4::vector2 to_window_coords(const ruis::vec2& point) const = 0; @@ -228,8 +225,7 @@ class context : public std::enable_shared_from_this /** * @brief Get scissor rectangle. * Get scissor rectangle in application window coordinates. - * TODO: - * + * The window coordinate system is always top-left origin (x right, y down). * @return Current scissor rectangle. */ virtual r4::rectangle get_scissor() const = 0; @@ -237,7 +233,7 @@ class context : public std::enable_shared_from_this /** * @brief Set scissor rectangle. * Set scissor rectangle in application window coordinates. - * TODO: + * The window coordinate system is always top-left origin (x right, y down). * @param r - new scissor rectangle. */ virtual void set_scissor(const r4::rectangle& r) = 0; @@ -245,10 +241,7 @@ class context : public std::enable_shared_from_this /** * @brief Get current rendering viewport within application window. * Get the rendering viewport rectangle in application window coordinates. - * The window coordinate system is renderer-specific. It can be top-bottom or bottom-top. - * For example, OpenGL/ES renderer has bottom-top coordinate system, i.e. origin is - * at the bottom left corner of the window and y-axis goes up. So, the viewport's position - * should be interpreted in context of the specific renderer. + * The window coordinate system is always top-left origin (x right, y down). * The viewport's dimensions are renderer-agnostic. * @return Current rendering viewport rectangle. */ @@ -257,11 +250,10 @@ class context : public std::enable_shared_from_this /** * @brief Set rendering viewport within application window. - * Since application window's coordinate system is renderer-specific - * (see description of get_viewport() for details), the meaning of the viewport rectangle's - * position is also renderer-specific. Though, the position of (0, 0) in combination with - * framebuffer's dimensions can safely be used regardless of the renderer to set the viewport - * covering the whole framebuffer. + * The viewport rectangle is specified in top-left origin window coordinates + * (x right, y down). The position of (0, 0) in combination with the + * framebuffer's dimensions can safely be used regardless of the renderer + * to set the viewport covering the whole framebuffer. * @param r - new viewport rectangle. */ virtual void set_viewport(const r4::rectangle& r) = 0; From eea9704131b0f898d4d061087c683b0f0b94c44b Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Mon, 30 Nov 2026 20:17:19 +0200 Subject: [PATCH 2/4] document the NDC convention for the initial matrix The framework convention for the initial matrix: NDC (normalized device coordinates) spans x, y, z from [0, 1] with x-axis right, y-axis down and z-axis away from the viewer. With the initial_matrix: x = 0 maps to the left viewport edge, x = 1 to the right viewport edge; y = 0 maps to the top viewport edge, y = 1 to the bottom viewport edge; z = 0 maps to the near clipping plane, z = 1 to the far clipping plane. --- src/ruis/render/context.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ruis/render/context.hpp b/src/ruis/render/context.hpp index 8b1ac5f0f..94cc1daf6 100644 --- a/src/ruis/render/context.hpp +++ b/src/ruis/render/context.hpp @@ -92,7 +92,12 @@ class context : public std::enable_shared_from_this /** * @brief Initial matrix to use for rendering. * The initial coordinate system is right-handed. - * The initial matrix makes viewport edges to be: left = 0, right = 1, top = 0, bottom = 1. + * + * Convention: NDC (normalized device coordinates) spans x, y, z from [0, 1], + * x-axis right, y-axis down, z-axis away from the viewer. With the initial_matrix: + * x = 0 maps to the left viewport edge, x = 1 to the right viewport edge; + * y = 0 maps to the top viewport edge, y = 1 to the bottom viewport edge; + * z = 0 maps to the near clipping plane, z = 1 to the far clipping plane. */ const r4::matrix4 initial_matrix; From 82b263424a3508a413b9ab929281e1984fd23d22 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Mon, 30 Nov 2026 21:33:51 +0200 Subject: [PATCH 3/4] app1 cube_widget: remove z flip from widget matrix chain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The framework convention maps framework z=0 to the near clipping plane and z=1 to the far plane (z away), and the frustum (r4) now maps near to NDC z=0 and far to NDC z=1. The previous z flip in the widget chain (scale(1, -1, -1)) made framework z=0 map to NDC z=+1, which is the far plane — clipping out-of-range z values. The y flip remains (framework y=0 is top, OpenGL viewport is y-up). --- tests/app1/src/cube_widget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app1/src/cube_widget.cpp b/tests/app1/src/cube_widget.cpp index 6f9c76f6f..20d9e1db3 100644 --- a/tests/app1/src/cube_widget.cpp +++ b/tests/app1/src/cube_widget.cpp @@ -81,7 +81,7 @@ void cube_widget::render(const ruis::mat4& matrix) const ruis::mat4 matr(matrix); matr.scale(this->rect().d / 2); matr.translate(1, 1); - matr.scale(1, -1, -1); // y down, z away + matr.scale(1, -1, 1); // y down // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) matr.frustum(-2, 2, -1.5, 1.5, 2, 100); matr.translate(0, 0, -4); From 43d399ce12e2dceac8168d48474c4285f3f5299a Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Mon, 30 Nov 2026 22:47:37 +0200 Subject: [PATCH 4/4] app1 cube_widget: scale by full window rect --- tests/app1/src/cube_widget.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/app1/src/cube_widget.cpp b/tests/app1/src/cube_widget.cpp index 20d9e1db3..f825765f3 100644 --- a/tests/app1/src/cube_widget.cpp +++ b/tests/app1/src/cube_widget.cpp @@ -79,9 +79,7 @@ void cube_widget::update(uint32_t dt) void cube_widget::render(const ruis::mat4& matrix) const { ruis::mat4 matr(matrix); - matr.scale(this->rect().d / 2); - matr.translate(1, 1); - matr.scale(1, -1, 1); // y down + matr.scale(this->rect().d); // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) matr.frustum(-2, 2, -1.5, 1.5, 2, 100); matr.translate(0, 0, -4);