Skip to content

Commit

Permalink
Support dynamically changed terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 1, 2022
1 parent 28bb3e2 commit 7332ccb
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 61 deletions.
4 changes: 2 additions & 2 deletions bin/convert/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ impl LevelLayers {

LevelData {
size: (self.size.0 as i32, self.size.1 as i32),
meta,
height,
meta: meta.into_boxed_slice(),
height: height.into_boxed_slice(),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions bin/level/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum Input {

pub struct LevelView {
render: Render,
_level: level::Level,
level: level::Level,
cam: space::Camera,
input: Input,

Expand Down Expand Up @@ -123,7 +123,7 @@ impl LevelView {

LevelView {
render,
_level: level,
level,
cam: space::Camera {
loc: cgmath::vec3(0.0, 0.0, 400.0),
rot: cgmath::Quaternion::new(1.0, 0.0, 0.0, 0.0),
Expand Down Expand Up @@ -372,6 +372,7 @@ impl Application for LevelView {
self.render.draw_world(
&mut encoder,
&mut Batcher::new(),
&self.level,
&self.cam,
targets,
device,
Expand Down
10 changes: 8 additions & 2 deletions bin/road/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,14 @@ impl Application for Game {
label: Some("Draw"),
});

self.render
.draw_world(&mut encoder, &mut self.batcher, &self.cam, targets, device);
self.render.draw_world(
&mut encoder,
&mut self.batcher,
&self.level,
&self.cam,
targets,
device,
);

/*
self.render.debug.draw_lines(
Expand Down
39 changes: 31 additions & 8 deletions lib/ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Rusty Vangers FFI bindings.
//! Matches "lib/renderer/src/renderer/scene/rust/vange_rs.h"
//! See https://github.com/KranX/Vangers/pull/517

use futures::executor::LocalPool;
use std::ptr;
Expand Down Expand Up @@ -46,6 +47,7 @@ pub struct CameraDescription {
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct MapDescription {
width: i32,
height: i32,
Expand All @@ -61,8 +63,14 @@ struct Camera {
transform: Transform,
}

struct LevelContext {
desc: MapDescription,
render: vangers::render::Render,
level: vangers::level::Level,
}

pub struct Context {
renderer: Option<vangers::render::Render>,
level: Option<LevelContext>,
render_config: vangers::config::settings::Render,
color_format: wgpu::TextureFormat,
queue: wgpu::Queue,
Expand All @@ -72,6 +80,7 @@ pub struct Context {
camera: Camera,
}

#[no_mangle]
pub extern "C" fn rv_init() -> Option<ptr::NonNull<Context>> {
use vangers::config::settings as st;

Expand All @@ -95,7 +104,7 @@ pub extern "C" fn rv_init() -> Option<ptr::NonNull<Context>> {
.ok()?;

let ctx = Context {
renderer: None,
level: None,
render_config: st::Render {
wgpu_trace_path: String::new(),
light: st::Light {
Expand Down Expand Up @@ -124,18 +133,22 @@ pub extern "C" fn rv_init() -> Option<ptr::NonNull<Context>> {
ptr::NonNull::new(ptr)
}

#[no_mangle]
pub unsafe extern "C" fn rv_exit(ctx: *mut Context) {
let _ctx = Box::from_raw(ctx);
}

#[no_mangle]
pub extern "C" fn rv_camera_init(ctx: &mut Context, desc: CameraDescription) {
ctx.camera.desc = desc;
}

#[no_mangle]
pub extern "C" fn rv_camera_set_transform(ctx: &mut Context, transform: Transform) {
ctx.camera.transform = transform;
}

#[no_mangle]
pub extern "C" fn rv_map_init(ctx: &mut Context, desc: MapDescription) {
let terrains = (0..desc.material_count)
.map(|i| unsafe {
Expand All @@ -149,10 +162,10 @@ pub extern "C" fn rv_map_init(ctx: &mut Context, desc: MapDescription) {
let total = (desc.width * desc.height) as usize;
let level = vangers::level::Level {
size: (desc.width, desc.height),
flood_map: vec![],
flood_map: vec![].into_boxed_slice(),
flood_section_power: 0, //TODO
height: vec![0; total],
meta: vec![0; total],
height: vec![0; total].into_boxed_slice(),
meta: vec![0; total].into_boxed_slice(),
palette: [[0; 4]; 0x100], //TODO
terrains,
};
Expand All @@ -162,7 +175,7 @@ pub extern "C" fn rv_map_init(ctx: &mut Context, desc: MapDescription) {
&ctx.queue,
&ctx.downlevel_caps,
&level,
&[], //TODO: objects palette
&[[0; 4]; 0x100], //TODO: objects palette
&ctx.render_config,
ctx.color_format,
// extent only matters for "scatter" style rendering
Expand All @@ -172,9 +185,19 @@ pub extern "C" fn rv_map_init(ctx: &mut Context, desc: MapDescription) {
depth_or_array_layers: 0,
},
);
ctx.renderer = Some(render);
ctx.level = Some(LevelContext {
desc,
render,
level,
});
}

#[no_mangle]
pub extern "C" fn rv_map_exit(ctx: &mut Context) {
ctx.renderer = None;
ctx.level = None;
}

#[no_mangle]
pub extern "C" fn rv_map_request_update(ctx: &mut Context, region: Rect) {
//TODO
}
32 changes: 16 additions & 16 deletions src/level/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub const HEIGHT_SCALE: u32 = 128;

pub struct Level {
pub size: (i32, i32),
pub flood_map: Vec<u8>,
pub flood_map: Box<[u8]>,
pub flood_section_power: usize,
pub height: Vec<u8>,
pub meta: Vec<u8>,
pub height: Box<[u8]>,
pub meta: Box<[u8]>,
pub palette: [[u8; 4]; 0x100],
pub terrains: Box<[TerrainConfig]>,
}
Expand Down Expand Up @@ -91,10 +91,10 @@ impl Level {
};
Level {
size: (2, 1),
flood_map: vec![0],
flood_map: vec![0].into_boxed_slice(),
flood_section_power: 0,
height: vec![0, 0],
meta: vec![0, 0],
height: vec![0, 0].into_boxed_slice(),
meta: vec![0, 0].into_boxed_slice(),
palette: [[0xFF; 4]; 0x100],
terrains: (0..8).map(|_| tc.clone()).collect(),
}
Expand Down Expand Up @@ -206,14 +206,14 @@ pub fn read_palette(input: File, config: Option<&[TerrainConfig]>) -> [[u8; 4];
data
}

pub fn load_flood(config: &LevelConfig) -> Vec<u8> {
pub fn load_flood(config: &LevelConfig) -> Box<[u8]> {
profiling::scope!("Flood Map");
let size = (config.size.0.as_value(), config.size.1.as_value());
let flood_size = size.1 >> config.section.as_power();

let vpr_file = match File::open(&config.path_data.with_extension("vpr")) {
Ok(file) => file,
Err(_) => return vec![0; flood_size as usize],
Err(_) => return vec![0; flood_size as usize].into_boxed_slice(),
};

info!("Loading flood map...");
Expand All @@ -232,8 +232,8 @@ pub fn load_flood(config: &LevelConfig) -> Vec<u8> {
}

pub struct LevelData {
pub height: Vec<u8>,
pub meta: Vec<u8>,
pub height: Box<[u8]>,
pub meta: Box<[u8]>,
pub size: (i32, i32),
}

Expand Down Expand Up @@ -289,8 +289,8 @@ impl LevelData {
let total = (size.0 * size.1) as usize;
assert_eq!(data.len(), total * 4);
let mut level = LevelData {
height: vec![0u8; total],
meta: vec![0u8; total],
height: vec![0u8; total].into_boxed_slice(),
meta: vec![0u8; total].into_boxed_slice(),
size,
};

Expand Down Expand Up @@ -330,8 +330,8 @@ pub fn load_vmc(path: &Path, size: (i32, i32)) -> LevelData {
info!("Loading height map...");
let total = (size.0 * size.1) as usize;
let mut level = LevelData {
height: vec![0u8; total],
meta: vec![0u8; total],
height: vec![0u8; total].into_boxed_slice(),
meta: vec![0u8; total].into_boxed_slice(),
size,
};

Expand Down Expand Up @@ -381,8 +381,8 @@ pub fn load_vmc(path: &Path, size: (i32, i32)) -> LevelData {
pub fn load_vmp(path: &Path, size: (i32, i32)) -> LevelData {
let total = (size.0 * size.1) as usize;
let mut level = LevelData {
height: vec![0u8; total],
meta: vec![0u8; total],
height: vec![0u8; total].into_boxed_slice(),
meta: vec![0u8; total].into_boxed_slice(),
size,
};

Expand Down
2 changes: 1 addition & 1 deletion src/render/mipmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl MaxMipper {

pub fn update(
&self,
rects: &[Rect],
encoder: &mut wgpu::CommandEncoder,
rects: &[Rect],
device: &wgpu::Device,
) {
let mut vertex_data = Vec::with_capacity(rects.len() * 6);
Expand Down
7 changes: 4 additions & 3 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,15 @@ impl Render {
&mut self,
encoder: &mut wgpu::CommandEncoder,
batcher: &mut Batcher,
level: &level::Level,
cam: &Camera,
targets: ScreenTargets<'_>,
device: &wgpu::Device,
) {
profiling::scope!("draw_world");
batcher.prepare(device);
self.terrain.update_dirty(encoder, level, device);

//TODO: common routine for draw passes
//TODO: use `write_buffer`

Expand All @@ -610,11 +613,9 @@ impl Render {
mem::size_of::<global::Constants>() as wgpu::BufferAddress,
);

self.terrain.prepare(
self.terrain.prepare_shadow(
encoder,
device,
&self.global,
&self.fog_config,
cam,
wgpu::Extent3d {
width: shadow.size,
Expand Down
Loading

0 comments on commit 7332ccb

Please sign in to comment.