Added Terrain type to scene

This commit is contained in:
Joshua Barretto 2019-01-15 15:13:11 +00:00
parent bd561dd1aa
commit 41b6672743
16 changed files with 195 additions and 31 deletions

View File

@ -4,6 +4,7 @@ members = [
"client", "client",
"server", "server",
"voxygen", "voxygen",
"world",
] ]
[profile.dev] [profile.dev]

View File

@ -6,5 +6,7 @@ edition = "2018"
[dependencies] [dependencies]
common = { package = "veloren-common", path = "../common" } common = { package = "veloren-common", path = "../common" }
world = { package = "veloren-world", path = "../world" }
specs = "0.14" specs = "0.14"
vek = "0.9"

View File

@ -1,8 +1,16 @@
// Standard // Standard
use std::time::Duration; use std::time::Duration;
// Internal // Library
use common::state::State; use specs::Entity as EcsEntity;
use vek::*;
// Project
use common::{
state::State,
terrain::TerrainChunk,
};
use world::World;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -17,7 +25,11 @@ pub struct Input {
pub struct Client { pub struct Client {
state: State, state: State,
// TODO: Add "meta" state here player: Option<EcsEntity>,
// Testing
world: World,
pub chunk: Option<TerrainChunk>,
} }
impl Client { impl Client {
@ -25,9 +37,21 @@ impl Client {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
state: State::new(), state: State::new(),
player: None,
// Testing
world: World::new(),
chunk: None,
} }
} }
/// TODO: Get rid of this
pub fn with_test_state(mut self) -> Self {
self.chunk = Some(self.world.generate_chunk(Vec3::zero()));
self
}
/// Get a reference to the client's game state. /// Get a reference to the client's game state.
pub fn state(&self) -> &State { &self.state } pub fn state(&self) -> &State { &self.state }

View File

@ -1,4 +1,5 @@
pub enum BiomeKind { pub enum BiomeKind {
Void,
Grassland, Grassland,
Ocean, Ocean,
Mountain, Mountain,

View File

@ -1,35 +1,47 @@
pub mod block; pub mod block;
pub mod biome; pub mod biome;
// Reexports
pub use self::{
block::Block,
biome::BiomeKind,
};
// Library // Library
use vek::*; use vek::*;
// Crate // Crate
use crate::{ use crate::{
vol::VolSize, vol::VolSize,
volumes::vol_map::VolMap, volumes::{
vol_map::VolMap,
chunk::Chunk,
},
}; };
// Local // TerrainChunkSize
use self::{
block::Block,
biome::BiomeKind,
};
// ChunkSize pub struct TerrainChunkSize;
pub struct ChunkSize; impl VolSize for TerrainChunkSize {
impl VolSize for ChunkSize {
const SIZE: Vec3<u32> = Vec3 { x: 32, y: 32, z: 32 }; const SIZE: Vec3<u32> = Vec3 { x: 32, y: 32, z: 32 };
} }
// ChunkMeta // TerrainChunkMeta
pub struct ChunkMeta { pub struct TerrainChunkMeta {
biome: BiomeKind, biome: BiomeKind,
} }
// TerrainMap impl TerrainChunkMeta {
pub fn void() -> Self {
Self {
biome: BiomeKind::Void,
}
}
}
pub type TerrainMap = VolMap<Block, ChunkSize, ChunkMeta>; // Terrain type aliases
pub type TerrainChunk = Chunk<Block, TerrainChunkSize, TerrainChunkMeta>;
pub type TerrainMap = VolMap<Block, TerrainChunkSize, TerrainChunkMeta>;

View File

@ -105,4 +105,12 @@ impl<V: Vox, S: VolSize, M> VolMap<V, S, M> {
chunks: HashMap::new(), chunks: HashMap::new(),
} }
} }
pub fn insert(&mut self, key: Vec3<i32>, chunk: Chunk<V, S, M>) -> Option<Chunk<V, S, M>> {
self.chunks.insert(key, chunk)
}
pub fn remove(&mut self, key: &Vec3<i32>) -> Option<Chunk<V, S, M>> {
self.chunks.remove(key)
}
} }

View File

@ -6,5 +6,6 @@ edition = "2018"
[dependencies] [dependencies]
common = { package = "veloren-common", path = "../common" } common = { package = "veloren-common", path = "../common" }
world = { package = "veloren-world", path = "../world" }
specs = "0.14" specs = "0.14"

View File

@ -3,6 +3,7 @@ use std::time::Duration;
// Internal // Internal
use common::state::State; use common::state::State;
use world::World;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -15,6 +16,7 @@ pub struct Input {
pub struct Server { pub struct Server {
state: State, state: State,
world: World,
// TODO: Add "meta" state here // TODO: Add "meta" state here
} }
@ -24,15 +26,20 @@ impl Server {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
state: State::new(), state: State::new(),
world: World::new(),
} }
} }
/// Get a reference to the client's game state. /// Get a reference to the server's game state.
pub fn state(&self) -> &State { &self.state } pub fn state(&self) -> &State { &self.state }
/// Get a mutable reference to the server's game state.
/// Get a mutable reference to the client's game state.
pub fn state_mut(&mut self) -> &mut State { &mut self.state } pub fn state_mut(&mut self) -> &mut State { &mut self.state }
/// Get a reference to the server's world.
pub fn world(&self) -> &World { &self.world }
/// Get a mutable reference to the server's world.
pub fn world_mut(&mut self) -> &mut World { &mut self.world }
/// Execute a single server tick, handle input and update the game state by the given duration /// Execute a single server tick, handle input and update the game state by the given duration
pub fn tick(&mut self, input: Input, dt: Duration) -> Result<(), Error> { pub fn tick(&mut self, input: Input, dt: Duration) -> Result<(), Error> {
// This tick function is the centre of the Veloren universe. Most server-side things are // This tick function is the centre of the Veloren universe. Most server-side things are

View File

@ -19,6 +19,7 @@ impl Animation for RunAnimation {
) { ) {
let wave = (time as f32 * 12.0).sin(); let wave = (time as f32 * 12.0).sin();
let wave_fast = (time as f32 * 6.0).sin(); let wave_fast = (time as f32 * 6.0).sin();
let wave_dip = (wave_fast.abs() - 0.5).abs();
skeleton.head.offset = Vec3::unit_z() * 13.0; skeleton.head.offset = Vec3::unit_z() * 13.0;
skeleton.head.ori = Quaternion::rotation_z(wave * 0.3); skeleton.head.ori = Quaternion::rotation_z(wave * 0.3);
@ -32,15 +33,15 @@ impl Animation for RunAnimation {
skeleton.shorts.offset = Vec3::unit_z() * 4.0; skeleton.shorts.offset = Vec3::unit_z() * 4.0;
skeleton.shorts.ori = Quaternion::rotation_z(wave * 0.1); skeleton.shorts.ori = Quaternion::rotation_z(wave * 0.1);
skeleton.l_hand.offset = Vec3::new(-7.5, wave * 5.0, 9.0); skeleton.l_hand.offset = Vec3::new(-6.0 - wave_dip * 6.0, wave * 5.0, 11.0 - wave_dip * 6.0);
skeleton.r_hand.offset = Vec3::new(7.5, -wave * 5.0, 9.0); skeleton.r_hand.offset = Vec3::new(6.0 + wave_dip * 6.0, -wave * 5.0, 11.0 - wave_dip * 6.0);
skeleton.l_foot.offset = Vec3::new(-3.5, 2.0 - wave * 8.0, 3.5 - (wave_fast.abs() - 0.5).abs() * 4.0); skeleton.l_foot.offset = Vec3::new(-3.5, 1.0 - wave * 8.0, 3.5 - wave_dip * 4.0);
skeleton.l_foot.ori = Quaternion::rotation_x(-wave + 1.0); skeleton.l_foot.ori = Quaternion::rotation_x(-wave + 1.0);
skeleton.r_foot.offset = Vec3::new(3.5, 2.0 + wave * 8.0, 3.5 - (wave_fast.abs() - 0.5).abs() * 4.0); skeleton.r_foot.offset = Vec3::new(3.5, 1.0 + wave * 8.0, 3.5 - wave_dip * 4.0);
skeleton.r_foot.ori = Quaternion::rotation_x(wave + 1.0); skeleton.r_foot.ori = Quaternion::rotation_x(wave + 1.0);
skeleton.back.offset = Vec3::new(-8.0, 5.0, 16.0); skeleton.back.offset = Vec3::new(-9.0, 5.0, 18.0);
skeleton.back.ori = Quaternion::rotation_y(2.5); skeleton.back.ori = Quaternion::rotation_y(2.5);
} }
} }

View File

@ -27,7 +27,8 @@ impl Camera {
} }
} }
/// Compute the transformation matrices (view matrix and projection matrix) for the camera. /// Compute the transformation matrices (view matrix and projection matrix) and position of the
/// camera.
pub fn compute_dependents(&self) -> (Mat4<f32>, Mat4<f32>, Vec3<f32>) { pub fn compute_dependents(&self) -> (Mat4<f32>, Mat4<f32>, Vec3<f32>) {
let view_mat = Mat4::<f32>::identity() let view_mat = Mat4::<f32>::identity()
* Mat4::translation_3d(-Vec3::unit_z() * self.dist) * Mat4::translation_3d(-Vec3::unit_z() * self.dist)

View File

@ -1,5 +1,6 @@
pub mod camera; pub mod camera;
pub mod figure; pub mod figure;
pub mod terrain;
// Library // Library
use vek::*; use vek::*;
@ -33,6 +34,7 @@ use crate::{
use self::{ use self::{
camera::Camera, camera::Camera,
figure::Figure, figure::Figure,
terrain::Terrain,
}; };
// TODO: Don't hard-code this // TODO: Don't hard-code this
@ -44,9 +46,11 @@ struct Skybox {
} }
pub struct Scene { pub struct Scene {
camera: Camera,
globals: Consts<Globals>, globals: Consts<Globals>,
camera: Camera,
skybox: Skybox, skybox: Skybox,
terrain: Terrain,
test_figure: Figure<CharacterSkeleton>, test_figure: Figure<CharacterSkeleton>,
} }
@ -58,12 +62,13 @@ fn load_segment(filename: &'static str) -> Segment {
impl Scene { impl Scene {
/// Create a new `Scene` with default parameters. /// Create a new `Scene` with default parameters.
pub fn new(renderer: &mut Renderer) -> Self { pub fn new(renderer: &mut Renderer, client: &Client) -> Self {
Self { Self {
camera: Camera::new(),
globals: renderer globals: renderer
.create_consts(&[Globals::default()]) .create_consts(&[Globals::default()])
.unwrap(), .unwrap(),
camera: Camera::new(),
skybox: Skybox { skybox: Skybox {
model: renderer model: renderer
.create_model(&create_skybox_mesh()) .create_model(&create_skybox_mesh())
@ -72,6 +77,7 @@ impl Scene {
.create_consts(&[SkyboxLocals::default()]) .create_consts(&[SkyboxLocals::default()])
.unwrap(), .unwrap(),
}, },
terrain: Terrain::new(),
test_figure: Figure::new( test_figure: Figure::new(
renderer, renderer,
@ -129,6 +135,9 @@ impl Scene {
)]) )])
.expect("Failed to update global constants"); .expect("Failed to update global constants");
// Maintain terrain GPU data
self.terrain.maintain_gpu_data(renderer);
// TODO: Don't do this here // TODO: Don't do this here
RunAnimation::update_skeleton( RunAnimation::update_skeleton(
&mut self.test_figure.skeleton, &mut self.test_figure.skeleton,
@ -147,6 +156,9 @@ impl Scene {
&self.skybox.locals, &self.skybox.locals,
); );
// Render terrain
self.terrain.render(renderer, &self.globals);
// Render the test figure // Render the test figure
self.test_figure.render(renderer, &self.globals); self.test_figure.render(renderer, &self.globals);
} }

View File

@ -0,0 +1,52 @@
// Standard
use std::collections::HashMap;
// Library
use vek::*;
// Crate
use crate::{
Error,
render::{
Consts,
Globals,
Mesh,
Model,
Renderer,
TerrainPipeline,
TerrainLocals,
},
};
struct TerrainChunk {
// GPU data
model: Model<TerrainPipeline>,
locals: Consts<TerrainLocals>,
}
pub struct Terrain {
chunks: HashMap<Vec3<i32>, TerrainChunk>,
}
impl Terrain {
pub fn new() -> Self {
Self {
chunks: HashMap::new(),
}
}
pub fn maintain_gpu_data(&mut self, renderer: &mut Renderer) {
// TODO
}
pub fn render(&self, renderer: &mut Renderer, globals: &Consts<Globals>) {
/*
renderer.render_terrain_chunk(
&self.model,
globals,
&self.locals,
&self.bone_consts,
);
*/
}
}

View File

@ -33,10 +33,11 @@ pub struct SessionState {
impl SessionState { impl SessionState {
/// Create a new `SessionState` /// Create a new `SessionState`
pub fn new(renderer: &mut Renderer) -> Self { pub fn new(renderer: &mut Renderer) -> Self {
let client = Client::new().with_test_state(); // <--- TODO: Remove this
Self { Self {
// Create a scene for this session. The scene handles visible elements of the game world // Create a scene for this session. The scene handles visible elements of the game world
scene: Scene::new(renderer), scene: Scene::new(renderer, &client),
client: Client::new(), client,
} }
} }
} }

3
world/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

9
world/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "veloren-world"
version = "0.1.0"
authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>"]
edition = "2018"
[dependencies]
common = { package = "veloren-common", path = "../common" }
vek = "0.9"

29
world/src/lib.rs Normal file
View File

@ -0,0 +1,29 @@
// Library
use vek::*;
// Project
use common::{
vol::Vox,
terrain::{
Block,
TerrainChunk,
TerrainChunkMeta,
},
};
#[derive(Debug)]
pub enum Error {
Other(String),
}
pub struct World;
impl World {
pub fn new() -> Self {
Self
}
pub fn generate_chunk(&self, pos: Vec3<i32>) -> TerrainChunk {
TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void())
}
}