Fixed capture
This commit is contained in:
parent
da4e6cbf6b
commit
92e1f1d0ec
|
@ -85,6 +85,7 @@ fn main() {
|
|||
|
||||
let mut events_loop = EventsLoop::new();
|
||||
let window = WindowBuilder::new().with_visibility(false);
|
||||
|
||||
let context = glutin::ContextBuilder::new().with_depth_buffer(24);
|
||||
let display = Display::new(window, context, &events_loop).unwrap();
|
||||
let mut renderer = Renderer::new(display);
|
||||
|
@ -142,6 +143,8 @@ fn main() {
|
|||
|
||||
while !closed {
|
||||
|
||||
let mut should_screenshot = false;
|
||||
|
||||
events_loop.poll_events(|ev| {
|
||||
|
||||
controls.manage_event(&ev, &mut camera, &renderer);
|
||||
|
@ -191,11 +194,9 @@ fn main() {
|
|||
|
||||
} => {
|
||||
|
||||
// Make a screenshot
|
||||
let size = renderer.gl_window().get_inner_size().unwrap();
|
||||
let cap = renderer.capture(size.into());
|
||||
cap.save(format!("capture-{}.png", capture_count)).unwrap();
|
||||
capture_count += 1;
|
||||
should_screenshot = true;
|
||||
|
||||
|
||||
|
||||
},
|
||||
|
||||
|
@ -205,6 +206,14 @@ fn main() {
|
|||
|
||||
controls.update(&mut camera, &renderer);
|
||||
renderer.render(&scene, &camera);
|
||||
|
||||
if should_screenshot {
|
||||
// Make a screenshot
|
||||
let cap = renderer.capture();
|
||||
cap.save(format!("capture-{}.png", capture_count)).unwrap();
|
||||
capture_count += 1;
|
||||
}
|
||||
|
||||
let elapsed = as_millis(Instant::now().duration_since(before));
|
||||
|
||||
if elapsed < 20 {
|
||||
|
|
|
@ -4,13 +4,10 @@ use std::cell::Ref;
|
|||
use std::borrow::Cow;
|
||||
|
||||
use image;
|
||||
use image::{ImageBuffer, Rgba};
|
||||
use image::{ImageBuffer, Rgba, DynamicImage};
|
||||
|
||||
use glium::texture::{RawImage2d, SrgbTexture2d, Texture2dDataSink};
|
||||
use glium::{Frame, Display, Surface, Program, DrawParameters, Depth, VertexBuffer};
|
||||
use glium::{Rect, BlitTarget};
|
||||
use glium::framebuffer::SimpleFrameBuffer;
|
||||
use glium::uniforms::MagnifySamplerFilter;
|
||||
use glium::draw_parameters::{DepthTest, Blend};
|
||||
use glium::index::{NoIndices, PrimitiveType};
|
||||
use glium::glutin::GlWindow;
|
||||
|
@ -74,12 +71,17 @@ impl Renderer {
|
|||
let image = RawImage2d::from_raw_rgba(vec![1.0, 1.0, 1.0, 1.0], (1, 1));
|
||||
let texture = SrgbTexture2d::new(&display, image).ok().unwrap();
|
||||
|
||||
Renderer {
|
||||
let renderer = Renderer {
|
||||
display: display,
|
||||
program: program,
|
||||
clear_color: (0.0, 0.0, 0.0, 1.0),
|
||||
default_texture: texture,
|
||||
}
|
||||
};
|
||||
|
||||
// Force capture, otherwise, first capture fails.
|
||||
renderer.capture();
|
||||
|
||||
renderer
|
||||
|
||||
}
|
||||
|
||||
|
@ -215,45 +217,11 @@ impl Renderer {
|
|||
}
|
||||
|
||||
/// Returns a DynamicImage of the corresponding frame.
|
||||
pub fn capture(&self, dimensions: (u32, u32)) -> image::DynamicImage {
|
||||
rgba_image_data_to_image(self.capture_rgba_image_data(dimensions))
|
||||
}
|
||||
|
||||
/// Returns a RgbaImageData of the corresponding frame.
|
||||
pub fn capture_rgba_image_data(&self, dimensions: (u32, u32)) -> RgbaImageData {
|
||||
|
||||
let rect = Rect {
|
||||
left: 0,
|
||||
bottom: 0,
|
||||
width: dimensions.0,
|
||||
height: dimensions.1,
|
||||
};
|
||||
|
||||
let blit_target = BlitTarget {
|
||||
left: 0,
|
||||
bottom: 0,
|
||||
width: dimensions.0 as i32,
|
||||
height: dimensions.1 as i32,
|
||||
};
|
||||
|
||||
pub fn capture(&self) -> DynamicImage {
|
||||
// Create temporary texture and blit the front buffer to it
|
||||
let texture = SrgbTexture2d::empty(
|
||||
&self.display, dimensions.0, dimensions.1)
|
||||
.unwrap();
|
||||
let framebuffer = SimpleFrameBuffer::new(&self.display, &texture)
|
||||
.unwrap();
|
||||
|
||||
use glium::Surface;
|
||||
framebuffer.blit_from_frame(
|
||||
&rect,
|
||||
&blit_target,
|
||||
MagnifySamplerFilter::Nearest
|
||||
);
|
||||
|
||||
// Read the texture into new pixel buffer
|
||||
let pixel_buffer = texture.read_to_pixel_buffer();
|
||||
pixel_buffer.read_as_texture_2d().unwrap()
|
||||
|
||||
let image: RawImage2d<u8> = self.display.read_front_buffer();
|
||||
let image = ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
|
||||
DynamicImage::ImageRgba8(image).flipv()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue