Commit b70a862b authored by insert's avatar insert

Procedural generation, work on registry.

parent 0b0ab999
{
"version": 0,
"name": "Default Resources"
}
\ No newline at end of file
use crate::registry::{ Registry, block::Block };
use crate::registry::{ GameRegistry, Registry, block::Block };
static NAMESPACE: &str = "global";
......@@ -8,3 +8,7 @@ pub fn init_blocks(registry: &mut Registry<Block>) {
registry.register(Block::new(NAMESPACE, "grass"));
registry.register(Block::new(NAMESPACE, "water"));
}
pub fn init(registry: &mut GameRegistry) {
init_blocks(&mut registry.blocks);
}
mod resources;
mod registry;
mod world;
mod game;
use registry::{ Registry, block::Block };
use registry::GameRegistry;
use world::{ ChunkLocation, World };
use engine::{ native, render, mesh };
......@@ -11,25 +12,29 @@ use std::time::Instant;
fn main() {
// ? GAME CODE
let mut block_registry = Registry::<Block>::new();
let mut registry = GameRegistry::new();
game::init(&mut registry);
// ! REGISTER MOD BLOCKS, ETC
game::init_blocks(&mut block_registry);
//
let mut world = World::new();
world.load_chunks(ChunkLocation ( 0, 0 ), 3);
world.load_chunks(ChunkLocation ( 0, 0 ), 2);
// ? ENGINE CODE
let sdl = native::SDL::init();
let mut window = native::Window::create(&sdl);
let window = native::Window::create(&sdl);
let mut shader = render::shader::ShaderProgram::new()
.lighting()//.textured()
.lighting().textured()
.create().unwrap();
shader.enable();
sdl.lock_mouse(true);
let mut camera = render::camera::Camera::new();
render::texture::Texture::from("packs/default/textures/blocks/grass.png").unwrap();
let mut camera = render::camera::Camera::new(); camera.move_up(20.0);
let voxel = mesh::generator::cube(1.0).create();
let mut pressed: HashSet<native::Keycode> = HashSet::new();
......@@ -63,12 +68,12 @@ fn main() {
for key in &pressed {
match key {
native::Keycode::W => camera.move_forward(delta * 3.0),
native::Keycode::A => camera.move_left(delta * 3.0),
native::Keycode::S => camera.move_backward(delta * 3.0),
native::Keycode::D => camera.move_right(delta * 3.0),
native::Keycode::Space => camera.move_up(delta * 3.0),
native::Keycode::LShift => camera.move_down(delta * 3.0),
native::Keycode::W => camera.move_forward(delta * 12.0),
native::Keycode::A => camera.move_left(delta * 12.0),
native::Keycode::S => camera.move_backward(delta * 12.0),
native::Keycode::D => camera.move_right(delta * 12.0),
native::Keycode::Space => camera.move_up(delta * 12.0),
native::Keycode::LShift => camera.move_down(delta * 12.0),
_ => {}
}
}
......@@ -78,6 +83,8 @@ fn main() {
camera.update(&window.viewport);
shader.upload_vec3("viewPos", &camera.position);
world.load_chunks(ChunkLocation (
(camera.position.x / 16.0).floor() as isize, (camera.position.z / 16.0).floor() as isize ), 2);
world.render(&mut shader, &camera.view_projection);
window.swap();
......
use crate::registry::Registerable;
use crate::resources::atlas::AtlasCoordinate;
pub struct Block {
namespace: String,
id: String,
pub namespace: String,
pub id: String,
pub atlas: Option<AtlasCoordinate>,
}
impl Block {
......@@ -10,6 +12,7 @@ impl Block {
Block {
namespace: namespace.to_owned(),
id: id.to_owned(),
atlas: None,
}
}
}
......
......@@ -21,3 +21,15 @@ impl<T: Registerable> Registry<T> {
self.registered.insert(item.get_fully_qualified_name(), item);
}
}
pub struct GameRegistry {
pub blocks: Registry<block::Block>,
}
impl GameRegistry {
pub fn new() -> GameRegistry {
GameRegistry {
blocks: Registry::new(),
}
}
}
#[derive(Debug)]
pub struct AtlasCoordinate (
pub f32,
pub f32,
pub f32,
pub f32,
);
// loader
\ No newline at end of file
pub mod model;
\ No newline at end of file
pub mod loader;
pub mod atlas;
pub mod pack;
//
\ No newline at end of file
use hashbrown::HashMap;
use engine::{ mesh::{ Mesh, data::{ MeshData, Render } }, render::{ shader::Program, object::Translation }, glm };
use noise::{Fbm, NoiseFn};
use noise::{ Perlin, NoiseFn };
static CHUNK_SIZE: usize = 16;
static CHUNK_HEIGHT: usize = 256;
......@@ -36,17 +36,29 @@ impl Chunk {
}
pub fn generate(&mut self) {
let fbm = Fbm::new();
let perlin = Perlin::new();
let ROW_COUNT = CHUNK_SIZE.pow(2);
// ? heightmap method
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
let h = ((perlin.get([ (x as isize + self.loc.0 * CHUNK_SIZE as isize) as f64 * 0.03, (z as isize + self.loc.1 * CHUNK_SIZE as isize) as f64 * 0.03 ]) + 1.0) * 10.0) as usize;
for y in 0..h {
self.blocks[y * ROW_COUNT + z * CHUNK_SIZE + x] = 1;
}
}
}
/*for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_HEIGHT {
for z in 0..CHUNK_SIZE {
self.blocks[y * ROW_COUNT + z * CHUNK_SIZE + x] =
if fbm.get([x as f64, y as f64, z as f64]) > 0.1 { 1 } else { 0 };
if fbm.get([x as f64, y as f64, z as f64]) > 0.1 {
self.blocks[y * ROW_COUNT + z * CHUNK_SIZE + x] = 1;
}
}
}
}
}*/
self.id_map.push("stone".to_owned());
}
......@@ -62,7 +74,8 @@ impl Chunk {
println!("[world::chunk] generating mesh for {:?}", self.loc);
// ? GENERATE MESH
let mut vertices = Vec::new();
let mut normals = Vec::new();
let mut normal = Vec::new();
let mut uv = Vec::new();
let ROW_COUNT = CHUNK_SIZE.pow(2);
for x in 0..CHUNK_SIZE {
......@@ -115,7 +128,7 @@ impl Chunk {
x as f32 + 1.0, y as f32, z as f32 + 1.0,
]);
normals.append(&mut vec![
normal.append(&mut vec![
// FRONT (Z-)
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
......@@ -170,6 +183,55 @@ impl Chunk {
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
]);
let x0 = 0.0;
let x1 = 1.0;
let y0 = 0.0;
let y1 = 1.0;
uv.append(&mut vec![
x1, y1,
x0, y1,
x0, y0,
x1, y1,
x0, y0,
x1, y0,
x0, y1,
x1, y1,
x1, y0,
x0, y1,
x1, y0,
x0, y0,
x0, y1,
x1, y1,
x0, y0,
x1, y1,
x1, y0,
x0, y0,
x1, y1,
x0, y1,
x1, y0,
x0, y1,
x0, y0,
x1, y0,
x0, y0,
x1, y0,
x1, y1,
x0, y0,
x0, y1,
x1, y1,
x0, y0,
x1, y0,
x1, y1,
x0, y0,
x0, y1,
x1, y1,
]);
}
}
}
......@@ -177,8 +239,8 @@ impl Chunk {
self.mesh = Some(MeshData::new()
.vertex(vertices)
.normal(normals)
.set_rgb(1.0, 1.0, 1.0)
.normal(normal)
.render(Render::UV(uv))
.create());
self.render(shader, view_projection);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment