diff --git a/lib/monet/Cargo.toml b/lib/monet/Cargo.toml index 3c776e36..ef7e426d 100644 --- a/lib/monet/Cargo.toml +++ b/lib/monet/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Anselm Eickhoff "] glium = "*" glium_text = "*" clippy = "*" +fnv = "1.0.3" [dependencies.kay] path = "../kay" diff --git a/lib/monet/src/lib.rs b/lib/monet/src/lib.rs index 8bcb0f6e..36786921 100644 --- a/lib/monet/src/lib.rs +++ b/lib/monet/src/lib.rs @@ -9,23 +9,24 @@ extern crate kay; #[macro_use] extern crate kay_macros; extern crate glium_text; +extern crate fnv; pub use ::descartes::{N, P3, P2, V3, V4, M4, Iso3, Persp3, ToHomogeneous, Norm, Into2d, Into3d, WithUniqueOrthogonal, Inverse, Rotate}; use ::kay::{ID, Recipient, CVec, ActorSystem, Individual, Fate}; -use std::collections::HashMap; +use fnv::FnvHashMap; use glium::{index, Surface}; pub use glium::backend::glutin_backend::GlutinFacade; pub struct Renderer { - pub scenes: HashMap, + pub scenes: Vec, pub render_context: RenderContext } impl Renderer { pub fn new (window: GlutinFacade) -> Renderer { Renderer { - scenes: HashMap::new(), + scenes: Vec::new(), render_context: RenderContext::new(window) } } @@ -45,28 +46,28 @@ pub struct RenderToScene {pub renderer_id: ID, pub scene_id: usize} impl Recipient for Renderer { fn receive(&mut self, msg: &Control) -> Fate {match *msg { Control::Setup => { - for (scene_id, scene) in &self.scenes { + for (scene_id, scene) in self.scenes.iter().enumerate() { for renderable in &scene.renderables { - *renderable << SetupInScene{renderer_id: Self::id(), scene_id: *scene_id}; + *renderable << SetupInScene{renderer_id: Self::id(), scene_id: scene_id}; } } Fate::Live }, Control::Render => { - for (scene_id, mut scene) in &mut self.scenes { + for (scene_id, mut scene) in self.scenes.iter_mut().enumerate() { for batch in (&mut scene).batches.values_mut() { batch.instances.clear(); } for renderable in &scene.renderables { - *renderable << RenderToScene{renderer_id: Self::id(), scene_id: *scene_id}; + *renderable << RenderToScene{renderer_id: Self::id(), scene_id: scene_id}; } } Fate::Live } Control::Submit => { - for scene in self.scenes.values() { + for scene in &self.scenes { self.render_context.submit(scene); } Fate::Live @@ -80,7 +81,7 @@ pub struct MoveEye {pub scene_id: usize, pub delta: V3} impl Recipient for Renderer { fn receive(&mut self, msg: &MoveEye) -> Fate {match *msg{ MoveEye{scene_id, delta} => { - let eye = &mut self.scenes.get_mut(&scene_id).unwrap().eye; + let eye = &mut self.scenes[scene_id].eye; let eye_direction_2d = (eye.target - eye.position).into_2d().normalize(); let absolute_delta = delta.x * eye_direction_2d.into_3d() + delta.y * eye_direction_2d.orthogonal().into_3d() @@ -93,36 +94,36 @@ impl Recipient for Renderer { } #[derive(Compact, Clone)] -pub struct AddBatch {pub scene_id: usize, pub batch_id: usize, pub thing: Thing} +pub struct AddBatch {pub scene_id: usize, pub batch_id: u16, pub thing: Thing} impl Recipient for Renderer { fn receive(&mut self, msg: &AddBatch) -> Fate {match *msg { AddBatch{scene_id, batch_id, ref thing} => { - self.scenes.get_mut(&scene_id).unwrap().batches.insert(batch_id, Batch::new(thing.clone(), Vec::new())); + self.scenes[scene_id].batches.insert(batch_id, Batch::new(thing.clone(), Vec::new())); Fate::Live } }} } #[derive(Compact, Clone)] -pub struct UpdateThing {pub scene_id: usize, pub thing_id: usize, pub thing: Thing, pub instance: Instance} +pub struct UpdateThing {pub scene_id: usize, pub thing_id: u16, pub thing: Thing, pub instance: Instance} impl Recipient for Renderer { fn receive(&mut self, msg: &UpdateThing) -> Fate {match *msg { UpdateThing{scene_id, thing_id, ref thing, instance} => { - self.scenes.get_mut(&scene_id).unwrap().things.insert(thing_id, (thing.clone(), instance)); + self.scenes[scene_id].things.insert(thing_id, (thing.clone(), instance)); Fate::Live } }} } #[derive(Copy, Clone)] -pub struct AddInstance {pub scene_id: usize, pub batch_id: usize, pub position: Instance} +pub struct AddInstance {pub scene_id: usize, pub batch_id: u16, pub position: Instance} impl Recipient for Renderer { fn receive(&mut self, msg: &AddInstance) -> Fate {match *msg { AddInstance{scene_id, batch_id, position} => { - self.scenes.get_mut(&scene_id).unwrap().batches.get_mut(&batch_id).unwrap().instances.push(position); + self.scenes[scene_id].batches.get_mut(&batch_id).unwrap().instances.push(position); Fate::Live } }} @@ -137,7 +138,7 @@ pub struct Projected3d{pub position_3d: P3} impl Recipient for Renderer { fn receive(&mut self, msg: &Project2dTo3d) -> Fate {match *msg{ Project2dTo3d{scene_id, position_2d, requester} => { - let eye = &self.scenes.get_mut(&scene_id).unwrap().eye; + let eye = &self.scenes[scene_id].eye; let frame_size = self.render_context.window.get_framebuffer_dimensions(); // mouse is on the close plane of the frustum @@ -192,8 +193,8 @@ pub fn setup(system: &mut ActorSystem, renderer: Renderer) { pub struct Scene { pub eye: Eye, - pub batches: HashMap, - pub things: HashMap, + pub batches: FnvHashMap, + pub things: FnvHashMap, pub renderables: Vec, pub debug_text: String } @@ -207,8 +208,8 @@ impl Scene { up: V3::new(0.0, 0.0, 1.0), field_of_view: 0.3 * ::std::f32::consts::PI }, - batches: HashMap::new(), - things: HashMap::new(), + batches: FnvHashMap::default(), + things: FnvHashMap::default(), renderables: Vec::new(), debug_text: String::new() } diff --git a/src/core/geometry.rs b/src/core/geometry.rs index 3a1f6ced..0238bae3 100644 --- a/src/core/geometry.rs +++ b/src/core/geometry.rs @@ -112,12 +112,12 @@ pub fn band_to_thing(band: &Band

, z: N) -> Thing { Thing::new(vertices, indices) } -static mut LAST_DEBUG_THING: usize = 0; +static mut LAST_DEBUG_THING: u16 = 0; pub fn add_debug_path(path: CPath, color: [f32; 3], z: f32) { Renderer::id() << UpdateThing{ scene_id: 0, - thing_id: 3498539847 + unsafe{LAST_DEBUG_THING}, + thing_id: 4000 + unsafe{LAST_DEBUG_THING}, thing: band_to_thing(&Band::new(path, 0.2), z), instance: Instance::with_color(color) }; @@ -127,7 +127,7 @@ pub fn add_debug_path(path: CPath, color: [f32; 3], z: f32) { pub fn add_debug_point(point: P2, color: [f32; 3], z: f32) { Renderer::id() << UpdateThing{ scene_id: 0, - thing_id: 3498539847 + unsafe{LAST_DEBUG_THING}, + thing_id: 4000 + unsafe{LAST_DEBUG_THING}, thing: Thing::new( vec![ Vertex{position: [point.x + -0.5, point.y + -0.5, z]}, diff --git a/src/game/lanes_and_cars/lane_rendering.rs b/src/game/lanes_and_cars/lane_rendering.rs index e9eabfad..fd78988d 100644 --- a/src/game/lanes_and_cars/lane_rendering.rs +++ b/src/game/lanes_and_cars/lane_rendering.rs @@ -17,7 +17,7 @@ impl RecipientAsSwarm for Lane { SetupInScene{renderer_id, scene_id} => { renderer_id << AddBatch{scene_id: scene_id, batch_id: 0, thing: car::create()}; - renderer_id << AddBatch{scene_id: scene_id, batch_id: 222666222662, thing: Thing::new( + renderer_id << AddBatch{scene_id: scene_id, batch_id: 1333, thing: Thing::new( vec![ Vertex{position: [-1.0, -1.0, 0.0]}, Vertex{position: [1.0, -1.0, 0.0]}, @@ -92,7 +92,7 @@ impl Recipient for Lane { } if ! self.interactions.iter().any(|inter| match inter.kind {InteractionKind::Next{..} => true, _ => false}) { - renderer_id << AddInstance{scene_id: scene_id, batch_id: 222666222662, position: Instance{ + renderer_id << AddInstance{scene_id: scene_id, batch_id: 1333, position: Instance{ instance_position: [self.path.end().x, self.path.end().y, 0.0], instance_direction: [1.0, 0.0], instance_color: [1.0, 0.0, 0.0] @@ -100,7 +100,7 @@ impl Recipient for Lane { } if ! self.interactions.iter().any(|inter| match inter.kind {InteractionKind::Previous{..} => true, _ => false}) { - renderer_id << AddInstance{scene_id: scene_id, batch_id: 222666222662, position: Instance{ + renderer_id << AddInstance{scene_id: scene_id, batch_id: 1333, position: Instance{ instance_position: [self.path.start().x, self.path.start().y, 0.0], instance_direction: [1.0, 0.0], instance_color: [0.0, 1.0, 0.0] @@ -132,7 +132,7 @@ impl Recipient for TransferLane { renderer_id << UpdateThing{ scene_id: scene_id, - thing_id: 200 + self.id().instance_id as usize, + thing_id: 200 + self.id().instance_id as u16, thing: band_to_thing(&Band::new(self.path.clone(), 3.0), 0.1), instance: Instance::with_color([1.0, 1.0, 0.5]) }; diff --git a/src/game/lanes_and_cars/lane_thing_collector.rs b/src/game/lanes_and_cars/lane_thing_collector.rs index 3f9f63ea..cc0dcbc4 100644 --- a/src/game/lanes_and_cars/lane_thing_collector.rs +++ b/src/game/lanes_and_cars/lane_thing_collector.rs @@ -86,7 +86,7 @@ impl Recipient for LaneThingCollector{ renderer_id << UpdateThing{ scene_id: scene_id, - thing_id: 3498547908345, + thing_id: 2000, thing: living_thing, instance: Instance{ instance_position: [0.0, 0.0, -0.1], @@ -101,7 +101,7 @@ impl Recipient for LaneThingCollector{ renderer_id << UpdateThing{ scene_id: scene_id, - thing_id: 9384598345983, + thing_id: 2001, thing: self.cached_frozen_thing.clone(), instance: Instance{ instance_position: [0.0, 0.0, -0.1], diff --git a/src/game/lanes_and_cars/planning/current_plan_rendering.rs b/src/game/lanes_and_cars/planning/current_plan_rendering.rs index 88de6373..6527fb17 100644 --- a/src/game/lanes_and_cars/planning/current_plan_rendering.rs +++ b/src/game/lanes_and_cars/planning/current_plan_rendering.rs @@ -26,7 +26,7 @@ impl Recipient for CurrentPlan { .sum(); renderer_id << UpdateThing{ scene_id: scene_id, - thing_id: 3834747834, + thing_id: 500, thing: thing, instance: Instance::with_color([0.3, 0.3, 0.5]) }; @@ -36,7 +36,7 @@ impl Recipient for CurrentPlan { .sum(); renderer_id << UpdateThing{ scene_id: scene_id, - thing_id: 3834747835, + thing_id: 501, thing: intersections_thing, instance: Instance::with_color([0.0, 0.0, 1.0]) }; @@ -46,7 +46,7 @@ impl Recipient for CurrentPlan { .sum(); renderer_id << UpdateThing{ scene_id: scene_id, - thing_id: 3834747836, + thing_id: 502, thing: connecting_strokes_thing, instance: Instance::with_color([0.5, 0.5, 1.0]) }; diff --git a/src/game/lanes_and_cars/planning/road_stroke_node_interactable.rs b/src/game/lanes_and_cars/planning/road_stroke_node_interactable.rs index 9e42f4e3..8c9b5169 100644 --- a/src/game/lanes_and_cars/planning/road_stroke_node_interactable.rs +++ b/src/game/lanes_and_cars/planning/road_stroke_node_interactable.rs @@ -110,7 +110,7 @@ use monet::AddBatch; impl RecipientAsSwarm for RoadStrokeNodeInteractable { fn receive(_swarm: &mut Swarm, msg: &SetupInScene) -> Fate {match *msg{ SetupInScene{renderer_id, scene_id} => { - renderer_id << AddBatch{scene_id: scene_id, batch_id: 4982939, thing: Thing::new( + renderer_id << AddBatch{scene_id: scene_id, batch_id: 2400, thing: Thing::new( vec![ Vertex{position: [-1.0, -1.0, 0.0]}, Vertex{position: [1.0, -1.0, 0.0]}, @@ -133,7 +133,7 @@ use monet::AddInstance; impl Recipient for RoadStrokeNodeInteractable { fn receive(&mut self, msg: &RenderToScene) -> Fate {match *msg { RenderToScene{renderer_id, scene_id} => { - renderer_id << AddInstance{scene_id: scene_id, batch_id: 4982939, position: Instance{ + renderer_id << AddInstance{scene_id: scene_id, batch_id: 2400, position: Instance{ instance_position: [self.position.x, self.position.y, 0.0], instance_direction: [1.0, 0.0], instance_color: if self.hovered {[1.0, 0.0, 0.0]} else {match self.parent {