Skip to content

Commit

Permalink
Send several instances at once to monet #14
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Nov 20, 2016
1 parent a9ddee9 commit b253254
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
14 changes: 14 additions & 0 deletions lib/monet/src/lib.rs
Expand Up @@ -129,6 +129,18 @@ impl Recipient<AddInstance> for Renderer {
}}
}

#[derive(Compact, Clone)]
pub struct AddSeveralInstances {pub scene_id: usize, pub batch_id: u16, pub positions: CVec<Instance>}

impl Recipient<AddSeveralInstances> for Renderer {
fn receive(&mut self, msg: &AddSeveralInstances) -> Fate {match *msg {
AddSeveralInstances{scene_id, batch_id, ref positions} => {
self.scenes[scene_id].batches.get_mut(&batch_id).unwrap().instances.extend_from_slice(positions);
Fate::Live
}
}}
}

#[derive(Copy, Clone)]
pub struct Project2dTo3d {pub scene_id: usize, pub position_2d: P2, pub requester: ID}

Expand Down Expand Up @@ -184,6 +196,7 @@ pub fn setup(system: &mut ActorSystem, renderer: Renderer) {
system.add_unclearable_inbox::<Control, Renderer>();
system.add_unclearable_inbox::<AddBatch, Renderer>();
system.add_unclearable_inbox::<AddInstance, Renderer>();
system.add_unclearable_inbox::<AddSeveralInstances, Renderer>();
system.add_unclearable_inbox::<UpdateThing, Renderer>();
system.add_unclearable_inbox::<MoveEye, Renderer>();
system.add_unclearable_inbox::<Project2dTo3d, Renderer>();
Expand Down Expand Up @@ -402,6 +415,7 @@ impl RenderContext {
let vertices = glium::VertexBuffer::new(&self.window, &batch.prototype.vertices).unwrap();
let indices = glium::IndexBuffer::new(&self.window, index::PrimitiveType::TrianglesList, &batch.prototype.indices).unwrap();
let instances = glium::VertexBuffer::dynamic(&self.window, batch.instances.as_slice()).unwrap();
println!("rendering batch with {} instances", instances.len());
target.draw((&vertices, instances.per_instance().unwrap()), &indices, &self.batch_program, &uniforms, &params).unwrap();
}

Expand Down
27 changes: 15 additions & 12 deletions src/game/lanes_and_cars/lane_rendering.rs
@@ -1,5 +1,5 @@
use descartes::{Band, FiniteCurve, WithUniqueOrthogonal, Norm};
use kay::{Actor, Individual, Recipient, RecipientAsSwarm, ActorSystem, Swarm, Fate};
use kay::{Actor, CVec, Individual, Recipient, RecipientAsSwarm, ActorSystem, Swarm, Fate};
use monet::{Instance, Thing, Vertex};
use core::geometry::band_to_thing;
use super::{Lane, TransferLane, InteractionKind};
Expand Down Expand Up @@ -72,24 +72,27 @@ impl Recipient<RenderToCollector> for Lane {

use ::monet::RenderToScene;
use ::monet::AddInstance;
use ::monet::AddSeveralInstances;
use ::monet::UpdateThing;

impl Recipient<RenderToScene> for Lane {
fn receive(&mut self, msg: &RenderToScene) -> Fate {match *msg {
RenderToScene{renderer_id, scene_id} => {
for car in &self.cars {
let car_instances = self.cars.iter().map(|car| {
let position2d = self.path.along(*car.position);
let direction = self.path.direction_along(*car.position);
renderer_id << AddInstance{
scene_id: scene_id,
batch_id: 0,
position: Instance{
instance_position: [position2d.x, position2d.y, 0.0],
instance_direction: [direction.x, direction.y],
instance_color: [0.5, 0.5, 0.5]
}
};
}
Instance{
instance_position: [position2d.x, position2d.y, 0.0],
instance_direction: [direction.x, direction.y],
instance_color: [0.5, 0.5, 0.5]
}
}).collect::<CVec<_>>();

renderer_id << AddSeveralInstances{
scene_id: scene_id,
batch_id: 0,
positions: car_instances
};

if ! self.interactions.iter().any(|inter| match inter.kind {InteractionKind::Next{..} => true, _ => false}) {
renderer_id << AddInstance{scene_id: scene_id, batch_id: 1333, position: Instance{
Expand Down
2 changes: 1 addition & 1 deletion src/game/lanes_and_cars/mod.rs
Expand Up @@ -113,7 +113,7 @@ const TRAFFIC_LOGIC_THROTTLING : usize = 60;
impl Recipient<Tick> for Lane {
fn receive(&mut self, msg: &Tick) -> Fate {match *msg{
Tick{dt, current_tick} => {
self.in_construction += dt * 40.0;
self.in_construction += dt * 400.0;

let do_traffic = current_tick % TRAFFIC_LOGIC_THROTTLING == self.id().instance_id as usize % TRAFFIC_LOGIC_THROTTLING;

Expand Down

0 comments on commit b253254

Please sign in to comment.