rgx.legacy icon indicating copy to clipboard operation
rgx.legacy copied to clipboard

Y-axis is Inverted

Open chrisburnor opened this issue 4 years ago • 4 comments

I'm trying to understand the coordinate system in RGX and think that there is an inverted y-axis somewhere. I've modified the sprite example to only create one instance of the sprite in the batch as follows:

                let rect =
                    Rect::new(0.0, 0.0, sw, sh);
                batch.add(
                    anim.val(),
                    rect,
                    ZDepth::default(),
                    Rgba::new(0.5, 0.5, 0.5, 0.5),
                    1.0,
                    Repeat::default(),
                );
                batch.offset(mx, my);

then the pipeline is set to:

                r.update_pipeline(
                    &pip,
                    kit::ortho(out.width, out.height, Origin::BottomLeft),
                    &mut frame,
                );

However, the sprite then appears in the upper left (and is upside down). Similarly, if I change the origin in kit::ortho to Origin::TopLeft, the the sprite is in the BottomLeft.

This is on metal backend.

chrisburnor avatar Apr 23 '20 03:04 chrisburnor

Maybe it's related to the way WebGPU's coordinate system is defined? https://gpuweb.github.io/gpuweb/#coordinate-systems

skyne98 avatar Apr 23 '20 22:04 skyne98

I'm still not sure the root cause, but that coord system link did lead me to a fix. The fact that the wgpu NDC is lower left origin, but the texture is upper left origin, and that both were inverted got me looking at the ortho method in kit/mod.rs:

@@ -187,8 +187,8 @@ impl<T> Animation<T> {

pub fn ortho(w: u32, h: u32, origin: Origin) -> Matrix4<f32> {
    let (top, bottom) = match origin {
-        Origin::BottomLeft => (h as f32, 0.),
-        Origin::TopLeft => (0., h as f32),
+        Origin::BottomLeft => (0., h as f32),
+        Origin::TopLeft => (-1.0 * h as f32, 0.),
    };
    Ortho::<f32> {
        left: 0.0,
        right: w as f32,
        bottom,
        top,
        near: -1.0,
        far: 1.0,
    }
    .into()
}

Fixes it for both BottomLeft and TopLeft. The sprite is oriented 'up' in both cases, but the origin is right.

For the final fix, it might be more correct to make the change in impl<S: Float> From<Ortho<S>> for Matrix4<S>, but that might have broader implications and the assumptions of the rgx::math module aren't totally clear to me.

With a little guidance, I can probably put together a pull request though for this fix.

chrisburnor avatar Apr 25 '20 05:04 chrisburnor

Hmmm yeah TopLeft and BottomLeft appear to be swapped. If you use a top-left origin, I think it's expected to have the sprite flipped, since your rectangle's height is downwards pointing?

cloudhead avatar Apr 26 '20 12:04 cloudhead

Basically this should fix it, but I need to check whether the error is perhaps in the conversion to Matrix4, as you suggested.

diff --git a/src/kit/mod.rs b/src/kit/mod.rs
index 9116518..594ea65 100644
--- a/src/kit/mod.rs
+++ b/src/kit/mod.rs
@@ -186,15 +186,15 @@ impl<T> Animation<T> {
 ///////////////////////////////////////////////////////////////////////////////

 pub fn ortho(w: u32, h: u32, origin: Origin) -> Matrix4<f32> {
-    let (top, bottom) = match origin {
+    let (bottom, top) = match origin {
         Origin::BottomLeft => (h as f32, 0.),
         Origin::TopLeft => (0., h as f32),

cloudhead avatar Apr 26 '20 12:04 cloudhead