Skip to content

Question: manually constructing a Viewport struct #1103

Open
@TooManyBees

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions