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

Question: manually constructing a Viewport struct #1103

Open
TooManyBees opened this issue Apr 17, 2018 · 0 comments
Open

Question: manually constructing a Viewport struct #1103

TooManyBees opened this issue Apr 17, 2018 · 0 comments

Comments

@TooManyBees
Copy link

I have a weird use case for piston2d-graphics, where I'm making draw calls without a game loop, because I'm generating a batch of one-off images in a headless OpenGL context. The problem is that, given a glutin::HeadlessContext with dimensions widthxheight, I don't know how to construct a graphics::Viewport that covers exactly that rectangle.

For clarity, here's where my code diverges from regular code. Where a lot of sample code suggests something like

let mut glgraphics = GlGraphics::new(opengl);
let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
if let Some(args) = e.render_args() {
    glgraphics.draw(e.viewport(), |c, g| {
        something.draw(c.transform, g);
    });
}

I instead use code like this, because I don't have a window or event loop:

let mut glgraphics = GlGraphics::new(opengl);
let viewport = Viewport {
    rect: [0, 0, width, height], // pretty sure this is wrong!
    draw_size: [width as u32, height as u32],
    window_size: [width as u32, height as u32],
};
for _ in some_iterator() {
    glgraphics.draw(&viewport, |c, g| {
        /* what I'd like to write is
           graphics::image(&some_texture, c.transform, g);
           but to make sure my viewport coordinates are sensible I currently write */
        graphics::Rectangle::new([1.0, 1.0, 1.0, 1.0])
            .draw([0.0, 0.0, 1000.0, 1000.0], &c.draw_state, c.transform, g);
        // and, spoiler, they're not
    });
}

The resulting image shows the square stretched out of shape, and partially off the horizontal edge it should be aligned to, and I'm pretty sure it's the result of my initial viewport dimensions being wrong.

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

1 participant