2019-01-11 23:18:34 +00:00
|
|
|
pub mod camera;
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Standard
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2019-01-12 13:56:34 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Project
|
|
|
|
use client::{
|
|
|
|
self,
|
|
|
|
Client,
|
|
|
|
};
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// Crate
|
2019-01-12 13:56:34 +00:00
|
|
|
use crate::{
|
2019-01-12 15:57:19 +00:00
|
|
|
Error,
|
2019-01-12 13:56:34 +00:00
|
|
|
render::{
|
|
|
|
Consts,
|
|
|
|
Globals,
|
|
|
|
Model,
|
|
|
|
Renderer,
|
|
|
|
SkyboxPipeline,
|
|
|
|
SkyboxLocals,
|
|
|
|
create_skybox_mesh,
|
|
|
|
},
|
|
|
|
window::Event,
|
2019-01-11 23:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Local
|
|
|
|
use self::camera::Camera;
|
|
|
|
|
|
|
|
struct Skybox {
|
|
|
|
model: Model<SkyboxPipeline>,
|
|
|
|
locals: Consts<SkyboxLocals>,
|
|
|
|
}
|
|
|
|
|
2019-01-12 13:56:34 +00:00
|
|
|
// TODO: Don't hard-code this
|
|
|
|
const CURSOR_PAN_SCALE: f32 = 0.005;
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
pub struct Scene {
|
|
|
|
camera: Camera,
|
|
|
|
globals: Consts<Globals>,
|
|
|
|
skybox: Skybox,
|
2019-01-12 15:57:19 +00:00
|
|
|
|
|
|
|
client: Client,
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scene {
|
|
|
|
/// Create a new `Scene` with default parameters.
|
|
|
|
pub fn new(renderer: &mut Renderer) -> Self {
|
|
|
|
Self {
|
|
|
|
camera: Camera::new(),
|
|
|
|
globals: renderer
|
2019-01-12 01:14:58 +00:00
|
|
|
.create_consts_with(Globals::default())
|
2019-01-11 23:18:34 +00:00
|
|
|
.unwrap(),
|
|
|
|
skybox: Skybox {
|
|
|
|
model: renderer
|
|
|
|
.create_model(&create_skybox_mesh())
|
|
|
|
.unwrap(),
|
|
|
|
locals: renderer
|
2019-01-12 01:14:58 +00:00
|
|
|
.create_consts_with(SkyboxLocals::default())
|
2019-01-11 23:18:34 +00:00
|
|
|
.unwrap(),
|
|
|
|
},
|
2019-01-12 15:57:19 +00:00
|
|
|
|
|
|
|
client: Client::new(),
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
/// Tick the scene (and the client attached to it)
|
|
|
|
pub fn tick(&mut self, dt: Duration) -> Result<(), Error> {
|
|
|
|
self.client.tick(client::Input {}, dt)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-12 13:56:34 +00:00
|
|
|
/// Handle an incoming user input event (i.e: cursor moved, key pressed, window closed, etc.).
|
|
|
|
pub fn handle_input_event(&mut self, event: Event) -> bool {
|
|
|
|
match event {
|
|
|
|
// Panning the cursor makes the camera rotate
|
|
|
|
Event::CursorPan(delta) => {
|
|
|
|
self.camera.rotate_by(Vec3::from(delta) * CURSOR_PAN_SCALE);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
// All other events are unhandled
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maintain and update GPU data such as constant buffers, models, etc.
|
2019-01-12 01:14:58 +00:00
|
|
|
pub fn maintain_gpu_data(&mut self, renderer: &mut Renderer) {
|
|
|
|
// Compute camera matrices
|
|
|
|
let (view_mat, proj_mat, cam_pos) = self.camera.compute_dependents();
|
|
|
|
|
|
|
|
// Update global constants
|
|
|
|
renderer.update_consts(&mut self.globals, Globals::new(
|
|
|
|
view_mat,
|
|
|
|
proj_mat,
|
|
|
|
cam_pos,
|
|
|
|
self.camera.get_focus_pos(),
|
|
|
|
10.0,
|
2019-01-12 15:57:19 +00:00
|
|
|
self.client.state().get_time_of_day(),
|
2019-01-12 01:14:58 +00:00
|
|
|
0.0,
|
|
|
|
))
|
|
|
|
.expect("Failed to update global constants");
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Render the scene using the provided `Renderer`
|
|
|
|
pub fn render_to(&self, renderer: &mut Renderer) {
|
|
|
|
// Render the skybox first (it appears over everything else so must be rendered first)
|
|
|
|
renderer.render_skybox(
|
|
|
|
&self.skybox.model,
|
|
|
|
&self.skybox.locals,
|
|
|
|
&self.globals,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|