2019-01-11 23:18:34 +00:00
|
|
|
pub mod camera;
|
2019-01-13 20:53:55 +00:00
|
|
|
pub mod figure;
|
2019-01-11 23:18:34 +00:00
|
|
|
|
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-13 20:53:55 +00:00
|
|
|
use dot_vox;
|
2019-01-12 13:56:34 +00:00
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
// Project
|
|
|
|
use client::{
|
|
|
|
self,
|
|
|
|
Client,
|
|
|
|
};
|
2019-01-13 20:53:55 +00:00
|
|
|
use common::figure::Segment;
|
2019-01-12 15:57:19 +00:00
|
|
|
|
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,
|
2019-01-13 20:53:55 +00:00
|
|
|
FigureLocals,
|
2019-01-12 13:56:34 +00:00
|
|
|
create_skybox_mesh,
|
|
|
|
},
|
|
|
|
window::Event,
|
2019-01-13 20:53:55 +00:00
|
|
|
mesh::Meshable,
|
2019-01-14 14:18:58 +00:00
|
|
|
anim::{
|
|
|
|
Animation,
|
|
|
|
CharacterSkeleton,
|
|
|
|
RunAnimation,
|
|
|
|
},
|
2019-01-11 23:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Local
|
2019-01-13 20:53:55 +00:00
|
|
|
use self::{
|
|
|
|
camera::Camera,
|
|
|
|
figure::Figure,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
camera: Camera,
|
|
|
|
globals: Consts<Globals>,
|
|
|
|
skybox: Skybox,
|
2019-01-12 15:57:19 +00:00
|
|
|
|
2019-01-14 14:18:58 +00:00
|
|
|
test_figure: Figure<CharacterSkeleton>,
|
2019-01-13 20:53:55 +00:00
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
client: Client,
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:53:55 +00:00
|
|
|
// TODO: Make a proper asset loading system
|
|
|
|
fn load_segment(filename: &'static str) -> Segment {
|
|
|
|
Segment::from(dot_vox::load(&(concat!(env!("CARGO_MANIFEST_DIR"), "/test_assets/").to_string() + filename)).unwrap())
|
|
|
|
}
|
|
|
|
|
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-13 20:53:55 +00:00
|
|
|
.create_consts(&[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-13 20:53:55 +00:00
|
|
|
.create_consts(&[SkyboxLocals::default()])
|
2019-01-11 23:18:34 +00:00
|
|
|
.unwrap(),
|
|
|
|
},
|
2019-01-12 15:57:19 +00:00
|
|
|
|
2019-01-13 20:53:55 +00:00
|
|
|
test_figure: Figure::new(
|
|
|
|
renderer,
|
|
|
|
[
|
|
|
|
Some(load_segment("head.vox").generate_mesh_with_offset(Vec3::new(-7.0, -5.5, -1.0))),
|
|
|
|
Some(load_segment("chest.vox").generate_mesh_with_offset(Vec3::new(-6.0, -3.0, 0.0))),
|
|
|
|
Some(load_segment("belt.vox").generate_mesh_with_offset(Vec3::new(-5.0, -3.0, 0.0))),
|
|
|
|
Some(load_segment("pants.vox").generate_mesh_with_offset(Vec3::new(-5.0, -3.0, 0.0))),
|
|
|
|
Some(load_segment("hand.vox").generate_mesh_with_offset(Vec3::new(-2.0, -2.0, -1.0))),
|
|
|
|
Some(load_segment("hand.vox").generate_mesh_with_offset(Vec3::new(-2.0, -2.0, -1.0))),
|
2019-01-14 14:18:58 +00:00
|
|
|
Some(load_segment("foot.vox").generate_mesh_with_offset(Vec3::new(-2.5, -3.0, 0.0))),
|
|
|
|
Some(load_segment("foot.vox").generate_mesh_with_offset(Vec3::new(-2.5, -3.0, 0.0))),
|
2019-01-13 20:53:55 +00:00
|
|
|
Some(load_segment("sword.vox").generate_mesh_with_offset(Vec3::new(-6.5, -1.0, 0.0))),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
],
|
2019-01-14 14:18:58 +00:00
|
|
|
CharacterSkeleton::new(),
|
2019-01-13 20:53:55 +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
|
2019-01-13 20:53:55 +00:00
|
|
|
renderer.update_consts(&mut self.globals, &[Globals::new(
|
2019-01-12 01:14:58 +00:00
|
|
|
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,
|
2019-01-13 20:53:55 +00:00
|
|
|
)])
|
2019-01-12 01:14:58 +00:00
|
|
|
.expect("Failed to update global constants");
|
2019-01-13 20:53:55 +00:00
|
|
|
|
|
|
|
// TODO: Don't do this here
|
2019-01-14 14:18:58 +00:00
|
|
|
RunAnimation::update_skeleton(
|
|
|
|
&mut self.test_figure.skeleton,
|
|
|
|
self.client.state().get_tick(),
|
|
|
|
);
|
2019-01-13 20:53:55 +00:00
|
|
|
self.test_figure.update_locals(renderer, FigureLocals::default());
|
|
|
|
self.test_figure.update_skeleton(renderer);
|
2019-01-12 01:14:58 +00:00
|
|
|
}
|
|
|
|
|
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.globals,
|
2019-01-13 20:53:55 +00:00
|
|
|
&self.skybox.locals,
|
2019-01-11 23:18:34 +00:00
|
|
|
);
|
2019-01-13 20:53:55 +00:00
|
|
|
|
|
|
|
// Render the test figure
|
|
|
|
self.test_figure.render(renderer, &self.globals);
|
2019-01-11 23:18:34 +00:00
|
|
|
}
|
|
|
|
}
|