#[macro_use]
extern crate glium;
extern crate rand;
use glium::Surface;
use glium::glutin;
mod support;
fn main() {
use glium::DisplayBuild;
println!("This example draws 10,000 instanced teapots. Each teapot gets a random position and \
direction at initialization. Then the CPU updates and uploads the positions of each \
teapot at each frame.");
let display = glutin::WindowBuilder::new()
.with_depth_buffer(24)
.build_glium()
.unwrap();
let vertex_buffer = support::load_wavefront(&display, include_bytes!("support/teapot.obj"));
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
let mut teapots = (0 .. 10000)
.map(|_| {
let pos: (f32, f32, f32) = (rand::random(), rand::random(), rand::random());
let dir: (f32, f32, f32) = (rand::random(), rand::random(), rand::random());
let pos = (pos.0 * 1.5 - 0.75, pos.1 * 1.5 - 0.75, pos.2 * 1.5 - 0.75);
let dir = (dir.0 * 1.5 - 0.75, dir.1 * 1.5 - 0.75, dir.2 * 1.5 - 0.75);
(pos, dir)
})
.collect::<Vec<_>>();
let mut per_instance = {
#[derive(Copy, Clone)]
struct Attr {
world_position: (f32, f32, f32),
}
implement_vertex!(Attr, world_position);
let data = teapots.iter().map(|_| {
Attr {
world_position: (0.0, 0.0, 0.0),
}
}).collect::<Vec<_>>();
glium::vertex::VertexBuffer::dynamic(&display, data)
};
let program = glium::Program::from_source(&display,
"
#version 140
in vec3 position;
in vec3 normal;
in vec3 world_position;
out vec3 v_position;
out vec3 v_normal;
out vec3 v_color;
void main() {
v_position = position;
v_normal = normal;
v_color = vec3(float(gl_InstanceID) / 10000.0, 1.0, 1.0);
gl_Position = vec4(position * 0.0005 + world_position, 1.0);
}
",
"
#version 140
in vec3 v_normal;
in vec3 v_color;
out vec4 f_color;
const vec3 LIGHT = vec3(-0.2, 0.8, 0.1);
void main() {
float lum = max(dot(normalize(v_normal), normalize(LIGHT)), 0.0);
vec3 color = (0.3 + 0.7 * lum) * v_color;
f_color = vec4(color, 1.0);
}
",
None)
.unwrap();
let camera = support::camera::CameraState::new();
support::start_loop(|| {
{
let mut mapping = per_instance.map();
for (src, dest) in teapots.iter_mut().zip(mapping.iter_mut()) {
(src.0).0 += (src.1).0 * 0.001;
(src.0).1 += (src.1).1 * 0.001;
(src.0).2 += (src.1).2 * 0.001;
dest.world_position = src.0;
}
}
let params = glium::DrawParameters {
depth_test: glium::DepthTest::IfLess,
depth_write: true,
.. Default::default()
};
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
target.draw((&vertex_buffer, per_instance.per_instance_if_supported().unwrap()),
&indices, &program, &uniform!{
matrix: camera.get_perspective()
},
¶ms).unwrap();
target.finish().unwrap();
for event in display.poll_events() {
match event {
glutin::Event::Closed => return support::Action::Stop,
_ => ()
}
}
support::Action::Continue
});
}