Added basic light

This commit is contained in:
Joshua Barretto 2019-05-31 21:37:11 +01:00
parent 07eb56f287
commit e2182b1a68
8 changed files with 61 additions and 25 deletions

View File

@ -25,6 +25,15 @@ impl Block {
Some(self.color.into()) Some(self.color.into())
} }
} }
pub fn get_opacity(&self) -> f32 {
match self.kind {
0 => 0.0,
1 => 0.3,
2 => 1.0,
_ => unimplemented!(),
}
}
} }
impl Vox for Block { impl Vox for Block {

View File

@ -0,0 +1 @@

View File

@ -5,6 +5,7 @@
in vec3 f_pos; in vec3 f_pos;
in vec3 f_norm; in vec3 f_norm;
in vec3 f_col; in vec3 f_col;
in float f_light;
layout (std140) layout (std140)
uniform u_locals { uniform u_locals {
@ -13,12 +14,23 @@ uniform u_locals {
out vec4 tgt_color; out vec4 tgt_color;
void main() { vec3 point_light() {
float ambient = 0.5; return vec3(1.0, 0.9, 0.4) * 3.0 / pow(length(f_pos - focus_pos.xyz), 2.0);
}
vec3 sun_dir = normalize(vec3(1.3, 1.7, 1.1));
void main() {
float sun_diffuse = dot(sun_dir, f_norm) * 0.5; float glob_ambience = 0.001;
tgt_color = vec4(f_col * (ambient + sun_diffuse), 1.0); float sun_ambience = 0.9;
vec3 sun_dir = normalize(vec3(1.3, 1.7, 2.1));
float sun_diffuse = dot(sun_dir, f_norm);
float sun_light = sun_ambience + sun_diffuse;
float static_light = glob_ambience + min(sun_light, f_light);
vec3 light = point_light() + static_light;
tgt_color = vec4(f_col * light, 1.0);
} }

View File

@ -4,6 +4,7 @@
in uint v_pos; in uint v_pos;
in uint v_col_norm; in uint v_col_norm;
in uint v_light;
layout (std140) layout (std140)
uniform u_locals { uniform u_locals {
@ -13,13 +14,14 @@ uniform u_locals {
out vec3 f_pos; out vec3 f_pos;
out vec3 f_norm; out vec3 f_norm;
out vec3 f_col; out vec3 f_col;
out float f_light;
void main() { void main() {
f_pos = vec3( f_pos = vec3(
float((v_pos >> 0) & 0x00FFu), float((v_pos >> 0) & 0x00FFu),
float((v_pos >> 8) & 0x00FFu), float((v_pos >> 8) & 0x00FFu),
float((v_pos >> 16) & 0xFFFFu) float((v_pos >> 16) & 0xFFFFu)
); ) + model_offs;
f_norm = vec3( f_norm = vec3(
float((v_col_norm >> 0) & 0x3u), float((v_col_norm >> 0) & 0x3u),
@ -33,8 +35,10 @@ void main() {
float((v_col_norm >> 24) & 0xFFu) float((v_col_norm >> 24) & 0xFFu)
) / 255.0; ) / 255.0;
f_light = float(v_light) / 4096.0;
gl_Position = gl_Position =
proj_mat * proj_mat *
view_mat * view_mat *
vec4(f_pos + model_offs, 1); vec4(f_pos, 1);
} }

View File

@ -16,6 +16,7 @@ use crate::{
type TerrainVertex = <TerrainPipeline as render::Pipeline>::Vertex; type TerrainVertex = <TerrainPipeline as render::Pipeline>::Vertex;
/*
impl<M> Meshable for Dyna<Block, M> { impl<M> Meshable for Dyna<Block, M> {
type Pipeline = TerrainPipeline; type Pipeline = TerrainPipeline;
type Supplement = (); type Supplement = ();
@ -43,6 +44,7 @@ impl<M> Meshable for Dyna<Block, M> {
mesh mesh
} }
} }
*/
impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2d<V, S> { impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2d<V, S> {
type Pipeline = TerrainPipeline; type Pipeline = TerrainPipeline;
@ -53,21 +55,22 @@ impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2
for x in range.min.x + 1..range.max.x - 1 { for x in range.min.x + 1..range.max.x - 1 {
for y in range.min.y + 1..range.max.y - 1 { for y in range.min.y + 1..range.max.y - 1 {
let mut neighbour_shade = [[1.0f32; 3]; 3]; let mut neighbour_light = [[1.0f32; 3]; 3];
for z in (range.min.z..range.max.z).rev() { for z in (range.min.z..range.max.z).rev() {
let pos = Vec3::new(x, y, z); let pos = Vec3::new(x, y, z);
// Create mesh polygons // Create mesh polygons
if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) { if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) {
let avg_shade = neighbour_shade let avg_light = neighbour_light
.iter() .iter()
.map(|col| col.iter()) .map(|col| col.iter())
.flatten() .flatten()
.fold(0.0, |a, x| a + x) .fold(0.0, |a, x| a + x)
/ 9.0; / 9.0;
let light = avg_light;
let col = col.map(|e| e as f32 / 255.0) * (0.01 + avg_shade * 0.99); let col = col.map(|e| e as f32 / 255.0);
let offs = (pos - range.min * Vec3::new(1, 1, 0)).map(|e| e as f32) let offs = (pos - range.min * Vec3::new(1, 1, 0)).map(|e| e as f32)
- Vec3::new(1.0, 1.0, 0.0); - Vec3::new(1.0, 1.0, 0.0);
@ -78,7 +81,7 @@ impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2
pos, pos,
offs, offs,
col, col,
TerrainVertex::new, |pos, norm, col| TerrainVertex::new(pos, norm, col, light),
false, false,
); );
} }
@ -86,14 +89,13 @@ impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2
// Accumulate shade under opaque blocks // Accumulate shade under opaque blocks
for i in 0..3 { for i in 0..3 {
for j in 0..3 { for j in 0..3 {
neighbour_shade[i][j] = if self neighbour_light[i][j] = if let Ok(opacity) = self
.get(pos + Vec3::new(i as i32 - 1, j as i32 - 1, 0)) .get(pos + Vec3::new(i as i32 - 1, j as i32 - 1, 0))
.map(|vox| !vox.is_empty()) .map(|vox| vox.get_opacity())
.unwrap_or(false)
{ {
neighbour_shade[i][j] * 0.85 neighbour_light[i][j] * (1.0 - opacity * 0.5)
} else { } else {
(neighbour_shade[i][j] * 1.01).min(1.0) (neighbour_light[i][j] * 1.05).min(1.0)
}; };
} }
} }
@ -104,6 +106,7 @@ impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2
} }
} }
/*
impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap3d<V, S> { impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap3d<V, S> {
type Pipeline = TerrainPipeline; type Pipeline = TerrainPipeline;
type Supplement = Aabb<i32>; type Supplement = Aabb<i32>;
@ -167,3 +170,4 @@ impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap3
mesh mesh
} }
} }
*/

View File

@ -19,6 +19,7 @@ gfx_defines! {
vertex Vertex { vertex Vertex {
pos: u32 = "v_pos", pos: u32 = "v_pos",
col_norm: u32 = "v_col_norm", col_norm: u32 = "v_col_norm",
light: u16 = "v_light",
} }
constant Locals { constant Locals {
@ -37,7 +38,7 @@ gfx_defines! {
} }
impl Vertex { impl Vertex {
pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>) -> Self { pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>, light: f32) -> Self {
Self { Self {
pos: 0 pos: 0
| ((pos.x as u32) & 0x00FF) << 0 | ((pos.x as u32) & 0x00FF) << 0
@ -50,6 +51,7 @@ impl Vertex {
| ((norm.x.add(1.0) as u32) & 0x3) << 0 | ((norm.x.add(1.0) as u32) & 0x3) << 0
| ((norm.y.add(1.0) as u32) & 0x3) << 2 | ((norm.y.add(1.0) as u32) & 0x3) << 2
| ((norm.z.add(1.0) as u32) & 0x3) << 4, | ((norm.z.add(1.0) as u32) & 0x3) << 4,
light: light.mul(4096.0) as u16,
} }
} }
} }

View File

@ -78,9 +78,14 @@ impl Renderer {
env!("CARGO_MANIFEST_DIR"), env!("CARGO_MANIFEST_DIR"),
"/shaders/include/globals.glsl" "/shaders/include/globals.glsl"
)); ));
let sky = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/shaders/include/sky.glsl"
));
let mut include_ctx = IncludeContext::new(); let mut include_ctx = IncludeContext::new();
include_ctx.include("globals.glsl", globals); include_ctx.include("globals.glsl", globals);
include_ctx.include("sky.glsl", globals);
// Construct a pipeline for rendering skyboxes // Construct a pipeline for rendering skyboxes
let skybox_pipeline = create_pipeline( let skybox_pipeline = create_pipeline(

View File

@ -276,11 +276,10 @@ impl<'a> Sampler<'a> {
// Sample blocks // Sample blocks
let air = Block::empty(); let air = Block::empty();
let stone = Block::new(1, Rgb::new(200, 220, 255)); let stone = Block::new(2, Rgb::new(200, 220, 255));
let grass = Block::new(2, Rgb::new(75, 150, 0)); let dirt = Block::new(1, Rgb::new(128, 90, 0));
let dirt = Block::new(3, Rgb::new(128, 90, 0)); let sand = Block::new(1, Rgb::new(180, 150, 50));
let sand = Block::new(4, Rgb::new(180, 150, 50)); let water = Block::new(1, Rgb::new(100, 150, 255));
let water = Block::new(5, Rgb::new(100, 150, 255));
let ground_block = if (wposf.z as f32) < height - 4.0 { let ground_block = if (wposf.z as f32) < height - 4.0 {
// Underground // Underground