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

Offset doesn't apply correctly when set with matrix #1250

Open
aoidenpa opened this issue Sep 23, 2023 · 1 comment
Open

Offset doesn't apply correctly when set with matrix #1250

aoidenpa opened this issue Sep 23, 2023 · 1 comment

Comments

@aoidenpa
Copy link

Describe the bug
Same transform behaves differently depending on if it is set with individual values or as a matrix.

To Reproduce

        //1
        let t = ggez::graphics::Transform::Values { 
            dest: Point2{x: 100.0, y: 100.0}, 
            rotation: 0.0, 
            scale: Vector2{x: 10.0, y: 10.0},
            offset: Point2{x: 0.5, y: 0.5} 
        };
        canvas.draw(&image, DrawParam::new().src(src).transform(t.to_bare_matrix()));

        //2
        let param = DrawParam::new().src(src)
        .dest([100.0, 100.0])
        .rotation(0.0)
        .scale([10.0, 10.0])
        .offset([0.5, 0.5]);
        canvas.draw(&image, param);

Expected behavior
These two draw commands should draw the same thing because they are identical but they don't. When using the matrix form, the offset doesn't center the image. In order to get the same result, I have to set offset to (image.width/2, image.height/2).

I believe the issue is here in /graphics/draw.rs:

        //struct DrawUniforms
        let param = match param.transform {
            Transform::Values { scale, .. } => param.scale(mint::Vector2 {
                x: scale.x * scale_x,
                y: scale.y * scale_y,
            }),
            Transform::Matrix(m) => param.transform(
                glam::Mat4::from(m) * glam::Mat4::from_scale(glam::vec3(scale_x, scale_y, 1.)),
            ),
        };

These 2 arms of the match don't produce the same result.

Screenshots or pasted code
image

Hardware and Software:

  • ggez version: 0.9.3
  • OS: Arch Linux
  • Graphics card: Intel Iris Xe
  • Graphics card drivers: vendor-provided drivers
@jazzfool
Copy link
Contributor

This is because image scaling (where the transform is scaled up by the image size) is applied after the offset calculation (which is based on scale). Basically to bring this up to equivalence without API changes you could disable image scaling in the DrawParam and manually scale the scale values by the image size.

In the API though it would probably be helpful if we exposed a parameter in to_bare_matrix that accepted an image scale parameter so that it's easier to emulate the same behaviour using Transform::Values.

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

2 participants