2019-01-11 23:18:34 +00:00
|
|
|
pub mod camera;
|
2019-01-13 20:53:55 +00:00
|
|
|
pub mod figure;
|
2019-01-15 15:13:11 +00:00
|
|
|
pub mod terrain;
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
use self::{camera::Camera, figure::FigureCache, terrain::Terrain};
|
2019-01-12 13:56:34 +00:00
|
|
|
use crate::{
|
2019-04-29 20:37:19 +00:00
|
|
|
anim::{
|
|
|
|
character::{CharacterSkeleton, RunAnimation},
|
|
|
|
Animation,
|
|
|
|
},
|
|
|
|
mesh::Meshable,
|
2019-01-12 13:56:34 +00:00
|
|
|
render::{
|
2019-04-29 20:37:19 +00:00
|
|
|
create_skybox_mesh, Consts, FigureLocals, Globals, Model, Renderer, SkyboxLocals,
|
2019-01-12 13:56:34 +00:00
|
|
|
SkyboxPipeline,
|
|
|
|
},
|
|
|
|
window::Event,
|
2019-01-13 20:53:55 +00:00
|
|
|
};
|
2019-04-29 20:37:19 +00:00
|
|
|
use client::Client;
|
|
|
|
use common::{comp, figure::Segment};
|
|
|
|
use dot_vox;
|
|
|
|
use vek::*;
|
2019-01-13 20:53:55 +00:00
|
|
|
|
|
|
|
// TODO: Don't hard-code this
|
|
|
|
const CURSOR_PAN_SCALE: f32 = 0.005;
|
2019-01-11 23:18:34 +00:00
|
|
|
|
|
|
|
struct Skybox {
|
|
|
|
model: Model<SkyboxPipeline>,
|
|
|
|
locals: Consts<SkyboxLocals>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Scene {
|
|
|
|
globals: Consts<Globals>,
|
2019-01-15 15:13:11 +00:00
|
|
|
camera: Camera,
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
skybox: Skybox,
|
2019-01-15 15:13:11 +00:00
|
|
|
terrain: Terrain,
|
2019-04-19 07:35:23 +00:00
|
|
|
|
|
|
|
figure_cache: FigureCache,
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
impl Scene {
|
|
|
|
/// Create a new `Scene` with default parameters.
|
2019-01-15 15:13:11 +00:00
|
|
|
pub fn new(renderer: &mut Renderer, client: &Client) -> Self {
|
2019-04-14 15:05:51 +00:00
|
|
|
let resolution = renderer.get_resolution().map(|e| e as f32);
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
Self {
|
2019-04-29 20:37:19 +00:00
|
|
|
globals: renderer.create_consts(&[Globals::default()]).unwrap(),
|
2019-04-14 15:05:51 +00:00
|
|
|
camera: Camera::new(resolution.x / resolution.y),
|
2019-01-15 15:13:11 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
skybox: Skybox {
|
2019-04-29 20:37:19 +00:00
|
|
|
model: renderer.create_model(&create_skybox_mesh()).unwrap(),
|
|
|
|
locals: renderer.create_consts(&[SkyboxLocals::default()]).unwrap(),
|
2019-01-11 23:18:34 +00:00
|
|
|
},
|
2019-01-15 15:13:11 +00:00
|
|
|
terrain: Terrain::new(),
|
2019-04-19 07:35:23 +00:00
|
|
|
figure_cache: FigureCache::new(),
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:39:31 +00:00
|
|
|
/// Get a reference to the scene's camera.
|
2019-04-29 20:37:19 +00:00
|
|
|
pub fn camera(&self) -> &Camera {
|
|
|
|
&self.camera
|
|
|
|
}
|
2019-01-23 22:39:31 +00:00
|
|
|
|
|
|
|
/// Get a mutable reference to the scene's camera.
|
2019-04-29 20:37:19 +00:00
|
|
|
pub fn camera_mut(&mut self) -> &mut Camera {
|
|
|
|
&mut self.camera
|
|
|
|
}
|
2019-01-23 22:39:31 +00:00
|
|
|
|
2019-01-12 13:56:34 +00:00
|
|
|
/// Handle an incoming user input event (i.e: cursor moved, key pressed, window closed, etc.).
|
2019-01-30 12:11:34 +00:00
|
|
|
///
|
|
|
|
/// If the event is handled, return true
|
2019-01-12 13:56:34 +00:00
|
|
|
pub fn handle_input_event(&mut self, event: Event) -> bool {
|
|
|
|
match event {
|
2019-01-30 12:11:34 +00:00
|
|
|
// When the window is resized, change the camera's aspect ratio
|
|
|
|
Event::Resize(dims) => {
|
|
|
|
self.camera.set_aspect_ratio(dims.x as f32 / dims.y as f32);
|
|
|
|
true
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-01-12 13:56:34 +00:00
|
|
|
// Panning the cursor makes the camera rotate
|
|
|
|
Event::CursorPan(delta) => {
|
|
|
|
self.camera.rotate_by(Vec3::from(delta) * CURSOR_PAN_SCALE);
|
|
|
|
true
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
// Zoom the camera when a zoom event occurs
|
|
|
|
Event::Zoom(delta) => {
|
2019-04-16 18:01:56 +00:00
|
|
|
self.camera.zoom_by(delta * 0.3);
|
2019-01-30 12:11:34 +00:00
|
|
|
true
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-01-12 13:56:34 +00:00
|
|
|
// All other events are unhandled
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
/// Maintain data such as GPU constant buffers, models, etc. To be called once per tick.
|
2019-04-10 21:51:14 +00:00
|
|
|
pub fn maintain(&mut self, renderer: &mut Renderer, client: &mut Client) {
|
2019-03-05 00:00:11 +00:00
|
|
|
// Get player position
|
2019-03-05 18:39:18 +00:00
|
|
|
let player_pos = client
|
2019-04-14 20:30:27 +00:00
|
|
|
.state()
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::phys::Pos>()
|
2019-04-19 19:32:47 +00:00
|
|
|
.get(client.entity())
|
2019-04-14 20:30:27 +00:00
|
|
|
.map(|pos| pos.0)
|
2019-03-05 18:39:18 +00:00
|
|
|
.unwrap_or(Vec3::zero());
|
|
|
|
|
2019-03-05 00:00:11 +00:00
|
|
|
// Alter camera position to match player
|
2019-05-05 15:17:57 +00:00
|
|
|
self.camera.set_focus_pos(player_pos + Vec3::unit_z() * 3.5);
|
|
|
|
|
|
|
|
// Tick camera for interpolation
|
|
|
|
self.camera.update(client.state().get_time());
|
2019-03-05 00:00:11 +00:00
|
|
|
|
2019-01-12 01:14:58 +00:00
|
|
|
// Compute camera matrices
|
2019-05-05 15:17:57 +00:00
|
|
|
let (view_mat, proj_mat, cam_pos) = self.camera.compute_dependents(client);
|
2019-01-12 01:14:58 +00:00
|
|
|
|
|
|
|
// Update global constants
|
2019-04-29 20:37:19 +00:00
|
|
|
renderer
|
|
|
|
.update_consts(
|
|
|
|
&mut self.globals,
|
|
|
|
&[Globals::new(
|
|
|
|
view_mat,
|
|
|
|
proj_mat,
|
|
|
|
cam_pos,
|
|
|
|
self.camera.get_focus_pos(),
|
|
|
|
10.0,
|
|
|
|
client.state().get_time_of_day(),
|
|
|
|
client.state().get_time(),
|
|
|
|
)],
|
|
|
|
)
|
2019-01-12 01:14:58 +00:00
|
|
|
.expect("Failed to update global constants");
|
2019-01-13 20:53:55 +00:00
|
|
|
|
2019-04-19 07:35:23 +00:00
|
|
|
// Maintain the terrain
|
2019-01-23 20:01:58 +00:00
|
|
|
self.terrain.maintain(renderer, client);
|
2019-04-19 07:35:23 +00:00
|
|
|
|
|
|
|
// Maintain the figures
|
|
|
|
self.figure_cache.maintain(renderer, client);
|
2019-04-23 11:55:48 +00:00
|
|
|
|
|
|
|
// Remove unused figures
|
|
|
|
self.figure_cache.clean(client.get_tick());
|
2019-01-12 01:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Render the scene using the provided `Renderer`
|
2019-04-23 11:55:48 +00:00
|
|
|
pub fn render(&mut self, renderer: &mut Renderer, client: &mut Client) {
|
2019-01-11 23:18:34 +00:00
|
|
|
// Render the skybox first (it appears over everything else so must be rendered first)
|
2019-04-29 20:37:19 +00:00
|
|
|
renderer.render_skybox(&self.skybox.model, &self.globals, &self.skybox.locals);
|
2019-01-13 20:53:55 +00:00
|
|
|
|
2019-04-10 21:51:14 +00:00
|
|
|
// Render terrain and figures
|
2019-01-15 15:13:11 +00:00
|
|
|
self.terrain.render(renderer, &self.globals);
|
2019-04-19 07:35:23 +00:00
|
|
|
self.figure_cache.render(renderer, client, &self.globals);
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|