Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Y-axis is Inverted #22

Open
chrisburnor opened this issue Apr 23, 2020 · 4 comments
Open

Y-axis is Inverted #22

chrisburnor opened this issue Apr 23, 2020 · 4 comments

Comments

@chrisburnor
Copy link

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.

@skyne98
Copy link

skyne98 commented Apr 23, 2020

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

@chrisburnor
Copy link
Author

chrisburnor commented Apr 25, 2020

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.

@cloudhead
Copy link
Owner

cloudhead commented Apr 26, 2020

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
Copy link
Owner

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),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants