Skip to content

Commit

Permalink
Rendering and updating 160000 cars very stupidly #1
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Jun 23, 2016
1 parent 991f989 commit 4359bfc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion citybound/src/models.rs
Expand Up @@ -140,7 +140,7 @@ impl FutureState for State {
target: Point2::new(0.0, 0.0),
azimuth: PI / 4.0,
inclination: PI / 4.0,
distance: 7.0
distance: 70.0
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion citybound/src/renderer.rs
Expand Up @@ -3,6 +3,7 @@ mod car;

extern crate nalgebra;
use nalgebra::{Vector3, Point3};
use ::monet::WorldPosition;

// coordinate system:
// Z = UP
Expand All @@ -12,7 +13,20 @@ use nalgebra::{Vector3, Point3};
pub fn render (past: &::models::State, future: &::models::State) -> ::monet::Scene {
println!("creating renderer state...");
let mut scene = ::monet::Scene::new();
scene.things.insert("car", car::create());

let n_cars_sqrt = 400;
let mut positions = Vec::<WorldPosition>::with_capacity(n_cars_sqrt * n_cars_sqrt);
for i in 0..(n_cars_sqrt * n_cars_sqrt) {
positions.push(WorldPosition{world_position: [
(i / n_cars_sqrt) as f32 * 5.0,
(i % n_cars_sqrt) as f32 * 5.0,
5.0 * (past.core.header.time as f32 * 0.01).sin() * ((i/n_cars_sqrt) as f32).sin()
]})
}

let car_swarm = ::monet::Swarm::new(car::create(), positions);

scene.swarms.insert("car", car_swarm);
scene.debug_text = format!("Simulation frame: {}", past.core.header.ticks);

let eye = past.ui_state.eye;
Expand Down
38 changes: 36 additions & 2 deletions monet/src/lib.rs
Expand Up @@ -16,6 +16,13 @@ pub struct Vertex {

implement_vertex!(Vertex, position);

#[derive(Copy, Clone)]
pub struct WorldPosition {
pub world_position: [f32; 3]
}

implement_vertex!(WorldPosition, world_position);

pub struct Eye {
pub position: Point3<f32>,
pub target: Point3<f32>,
Expand All @@ -34,9 +41,21 @@ impl Thing {
}
}

pub struct Swarm {
prototype: Thing,
instances: Vec<WorldPosition>
}

impl Swarm {
pub fn new(prototype: Thing, instances: Vec<WorldPosition>) -> Swarm {
Swarm{prototype: prototype, instances: instances}
}
}

pub struct Scene {
pub eye: Eye,
pub things: HashMap<&'static str, Thing>,
pub swarms: HashMap<&'static str, Swarm>,
pub debug_text: String
}

Expand All @@ -50,6 +69,7 @@ impl Scene {
field_of_view: 0.3 * std::f32::consts::PI
},
things: HashMap::with_capacity(3000),
swarms: HashMap::with_capacity(100),
debug_text: String::new()
}
}
Expand All @@ -58,6 +78,7 @@ impl Scene {
pub struct Renderer<'a> {
window: &'a GlutinFacade,
program: glium::Program,
swarm_program: glium::Program,
text_system: glium_text::TextSystem,
font: glium_text::FontTexture
}
Expand All @@ -72,6 +93,12 @@ impl<'a> Renderer<'a> {
fragment: include_str!("shader/solid_140.glslf"),
},
).unwrap(),
swarm_program: program!(window,
140 => {
vertex: include_str!("shader/solid_swarm_140.glslv"),
fragment: include_str!("shader/solid_140.glslf")
}
).unwrap(),
text_system: glium_text::TextSystem::new(window),
font: glium_text::FontTexture::new(
window,
Expand Down Expand Up @@ -119,14 +146,21 @@ impl<'a> Renderer<'a> {
};

// draw a frame
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
target.clear_color_and_depth((1.0, 1.0, 1.0, 1.0), 1.0);

for thing in scene.things.values() {
let vertices = glium::VertexBuffer::new(self.window, thing.vertices.as_slice()).unwrap();
let indices = glium::IndexBuffer::new(self.window, index::PrimitiveType::TrianglesList, thing.indices.as_slice()).unwrap();
target.draw(&vertices, &indices, &self.program, &uniforms, &params).unwrap();
}

for swarm in scene.swarms.values() {
let vertices = glium::VertexBuffer::new(self.window, swarm.prototype.vertices.as_slice()).unwrap();
let indices = glium::IndexBuffer::new(self.window, index::PrimitiveType::TrianglesList, swarm.prototype.indices.as_slice()).unwrap();
let instances = glium::VertexBuffer::dynamic(self.window, swarm.instances.as_slice()).unwrap();
target.draw((&vertices, instances.per_instance().unwrap()), &indices, &self.swarm_program, &uniforms, &params).unwrap();
}

let text = glium_text::TextDisplay::new(&self.text_system, &self.font, scene.debug_text.as_str());
let text_matrix = [
[0.05, 0.0, 0.0, 0.0],
Expand All @@ -135,7 +169,7 @@ impl<'a> Renderer<'a> {
[-0.9, 0.8, 0.0, 1.0f32]
];

glium_text::draw(&text, &self.text_system, &mut target, text_matrix, (1.0, 1.0, 0.0, 1.0));
glium_text::draw(&text, &self.text_system, &mut target, text_matrix, (0.0, 0.0, 0.0, 1.0));

target.finish().unwrap();
}
Expand Down
12 changes: 12 additions & 0 deletions monet/src/shader/solid_swarm_140.glslv
@@ -0,0 +1,12 @@
#version 140
uniform mat4 model;
uniform mat4 view;
uniform mat4 perspective;
in vec3 position;
in vec3 world_position;
out vec3 p;
void main() {
mat4 modelview = view * model;
gl_Position = perspective * modelview * vec4(position + world_position, 1.0);
p = position;
}

0 comments on commit 4359bfc

Please sign in to comment.