Convert indents to 2 spaces

This commit is contained in:
griffi-gh 2023-01-15 18:11:49 +01:00
parent 4e6052957e
commit 5d0802c47a
10 changed files with 235 additions and 235 deletions

View file

@ -1,7 +1,7 @@
use glium::{Surface, uniform}; use glium::{Surface, uniform};
use glium::glutin::{ use glium::glutin::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{EventLoop, ControlFlow}, event_loop::{EventLoop, ControlFlow},
}; };
mod assets; mod assets;
@ -15,61 +15,61 @@ use shaders::{Programs, chunk::Vertex as ChunkVertex};
use camera::Camera; use camera::Camera;
pub fn run() { pub fn run() {
log::info!("starting up"); log::info!("starting up");
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
log::info!("initializing display"); log::info!("initializing display");
let display = init_display(&event_loop); let display = init_display(&event_loop);
log::info!("compiling shaders"); log::info!("compiling shaders");
let programs = Programs::compile_all(&display); let programs = Programs::compile_all(&display);
log::info!("loading assets"); log::info!("loading assets");
let assets = Assets::load_all_sync(&display); let assets = Assets::load_all_sync(&display);
log::info!("init camera"); log::info!("init camera");
let camera = Camera { let camera = Camera {
position: [0., 0., -1.], position: [0., 0., -1.],
direction: [0., 0., 1.], direction: [0., 0., 1.],
..Default::default() ..Default::default()
}; };
log::info!("game loaded"); log::info!("game loaded");
//======================= //=======================
let vertex1 = ChunkVertex { position: [-0.5, -0.5, 1.], uv: [0., 0.], normal: [0., 1., 0.] }; let vertex1 = ChunkVertex { position: [-0.5, -0.5, 1.], uv: [0., 0.], normal: [0., 1., 0.] };
let vertex2 = ChunkVertex { position: [ 0.0, 0.5, 1.], uv: [0., 1.], normal: [0., 1., 0.] }; let vertex2 = ChunkVertex { position: [ 0.0, 0.5, 1.], uv: [0., 1.], normal: [0., 1., 0.] };
let vertex3 = ChunkVertex { position: [ 0.5, -0.25, 1.], uv: [1., 1.], normal: [0., 1., 0.] }; let vertex3 = ChunkVertex { position: [ 0.5, -0.25, 1.], uv: [1., 1.], normal: [0., 1., 0.] };
let shape = vec![vertex1, vertex2, vertex3]; let shape = vec![vertex1, vertex2, vertex3];
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap(); let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
//======================= //=======================
event_loop.run(move |ev, _, control_flow| { event_loop.run(move |ev, _, control_flow| {
#[allow(clippy::single_match, clippy::collapsible_match)] #[allow(clippy::single_match, clippy::collapsible_match)]
match ev { match ev {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
log::info!("exit requested"); log::info!("exit requested");
*control_flow = ControlFlow::Exit; *control_flow = ControlFlow::Exit;
return return
}, },
_ => () _ => ()
}, },
_ => () _ => ()
} }
let mut target = display.draw(); let mut target = display.draw();
let target_dimensions = target.get_dimensions(); let target_dimensions = target.get_dimensions();
let perspective = camera.perspective_matrix(target_dimensions); let perspective = camera.perspective_matrix(target_dimensions);
let view = camera.view_matrix(); let view = camera.view_matrix();
target.clear_color_and_depth((0.5, 0.5, 1., 1.), 1.); target.clear_color_and_depth((0.5, 0.5, 1., 1.), 1.);
target.draw( target.draw(
&vertex_buffer, &vertex_buffer,
glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList), glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
&programs.chunk, &programs.chunk,
&uniform! { &uniform! {
model: [[0.0f32; 4]; 4], model: [[0.0f32; 4]; 4],
view: view, view: view,
perspective: perspective, perspective: perspective,
tex: &assets.textures.block_atlas tex: &assets.textures.block_atlas
}, },
&Default::default() &Default::default()
).unwrap(); ).unwrap();
target.finish().unwrap(); target.finish().unwrap();
}); });
} }

View file

@ -3,13 +3,13 @@ pub mod textures;
use textures::Textures; use textures::Textures;
pub struct Assets { pub struct Assets {
pub textures: Textures pub textures: Textures
} }
impl Assets { impl Assets {
/// Load all assets synchronously /// Load all assets synchronously
pub fn load_all_sync(display: &glium::Display) -> Self { pub fn load_all_sync(display: &glium::Display) -> Self {
Self { Self {
textures: Textures::load_sync(display) textures: Textures::load_sync(display)
}
} }
}
} }

View file

@ -2,37 +2,37 @@ use std::{fs, io};
use glium::texture::{RawImage2d, SrgbTexture2d}; use glium::texture::{RawImage2d, SrgbTexture2d};
fn load_png(file_path: &str, display: &glium::Display) -> SrgbTexture2d { fn load_png(file_path: &str, display: &glium::Display) -> SrgbTexture2d {
log::info!("loading texture {}", file_path); log::info!("loading texture {}", file_path);
//Load file //Load file
let data = fs::read(file_path) let data = fs::read(file_path)
.unwrap_or_else(|_| panic!("Failed to load texture: {}", file_path)); .unwrap_or_else(|_| panic!("Failed to load texture: {}", file_path));
//decode image data //decode image data
let image_data = image::load( let image_data = image::load(
io::Cursor::new(&data), io::Cursor::new(&data),
image::ImageFormat::Png image::ImageFormat::Png
).unwrap().to_rgba8(); ).unwrap().to_rgba8();
//Create raw glium image //Create raw glium image
let image_dimensions = image_data.dimensions(); let image_dimensions = image_data.dimensions();
let raw_image = RawImage2d::from_raw_rgba_reversed( let raw_image = RawImage2d::from_raw_rgba_reversed(
&image_data.into_raw(), &image_data.into_raw(),
image_dimensions image_dimensions
); );
//Create texture //Create texture
SrgbTexture2d::new(display, raw_image).unwrap() SrgbTexture2d::new(display, raw_image).unwrap()
} }
pub struct Textures { pub struct Textures {
pub block_atlas: SrgbTexture2d pub block_atlas: SrgbTexture2d
} }
impl Textures { impl Textures {
/// Load textures synchronously, one by one and upload them to the GPU /// Load textures synchronously, one by one and upload them to the GPU
pub fn load_sync(display: &glium::Display) -> Self { pub fn load_sync(display: &glium::Display) -> Self {
Self { Self {
block_atlas: load_png("assets/spritesheet.png", display) block_atlas: load_png("assets/spritesheet.png", display)
}
} }
}
} }

View file

@ -6,72 +6,72 @@
use std::f32::consts::PI; use std::f32::consts::PI;
pub struct Camera { pub struct Camera {
pub position: [f32; 3], pub position: [f32; 3],
pub direction: [f32; 3], pub direction: [f32; 3],
pub up: [f32; 3], pub up: [f32; 3],
pub fov: f32, pub fov: f32,
pub znear: f32, pub znear: f32,
pub zfar: f32, pub zfar: f32,
} }
impl Camera { impl Camera {
pub fn view_matrix(&self) -> [[f32; 4]; 4] { pub fn view_matrix(&self) -> [[f32; 4]; 4] {
let position = self.position; let position = self.position;
let direction = self.direction; let direction = self.direction;
let up = self.up; let up = self.up;
let f = { let f = {
let f = direction; let f = direction;
let len = f[0] * f[0] + f[1] * f[1] + f[2] * f[2]; let len = f[0] * f[0] + f[1] * f[1] + f[2] * f[2];
let len = len.sqrt(); let len = len.sqrt();
[f[0] / len, f[1] / len, f[2] / len] [f[0] / len, f[1] / len, f[2] / len]
}; };
let s = [up[1] * f[2] - up[2] * f[1], let s = [up[1] * f[2] - up[2] * f[1],
up[2] * f[0] - up[0] * f[2], up[2] * f[0] - up[0] * f[2],
up[0] * f[1] - up[1] * f[0]]; up[0] * f[1] - up[1] * f[0]];
let s_norm = { let s_norm = {
let len = s[0] * s[0] + s[1] * s[1] + s[2] * s[2]; let len = s[0] * s[0] + s[1] * s[1] + s[2] * s[2];
let len = len.sqrt(); let len = len.sqrt();
[s[0] / len, s[1] / len, s[2] / len] [s[0] / len, s[1] / len, s[2] / len]
}; };
let u = [f[1] * s_norm[2] - f[2] * s_norm[1], let u = [f[1] * s_norm[2] - f[2] * s_norm[1],
f[2] * s_norm[0] - f[0] * s_norm[2], f[2] * s_norm[0] - f[0] * s_norm[2],
f[0] * s_norm[1] - f[1] * s_norm[0]]; f[0] * s_norm[1] - f[1] * s_norm[0]];
let p = [-position[0] * s_norm[0] - position[1] * s_norm[1] - position[2] * s_norm[2], let p = [-position[0] * s_norm[0] - position[1] * s_norm[1] - position[2] * s_norm[2],
-position[0] * u[0] - position[1] * u[1] - position[2] * u[2], -position[0] * u[0] - position[1] * u[1] - position[2] * u[2],
-position[0] * f[0] - position[1] * f[1] - position[2] * f[2]]; -position[0] * f[0] - position[1] * f[1] - position[2] * f[2]];
[ [
[s_norm[0], u[0], f[0], 0.0], [s_norm[0], u[0], f[0], 0.0],
[s_norm[1], u[1], f[1], 0.0], [s_norm[1], u[1], f[1], 0.0],
[s_norm[2], u[2], f[2], 0.0], [s_norm[2], u[2], f[2], 0.0],
[p[0], p[1], p[2], 1.0], [p[0], p[1], p[2], 1.0],
] ]
} }
pub fn perspective_matrix(&self, target_dimensions: (u32, u32)) -> [[f32; 4]; 4] { pub fn perspective_matrix(&self, target_dimensions: (u32, u32)) -> [[f32; 4]; 4] {
let znear = self.znear; let znear = self.znear;
let zfar = self.zfar; let zfar = self.zfar;
let fov = self.fov; let fov = self.fov;
let (width, height) = target_dimensions; let (width, height) = target_dimensions;
let aspect_ratio = height as f32 / width as f32; let aspect_ratio = height as f32 / width as f32;
let f = 1.0 / (fov / 2.0).tan(); let f = 1.0 / (fov / 2.0).tan();
[ [
[f * aspect_ratio , 0.0, 0.0 , 0.0], [f * aspect_ratio , 0.0, 0.0 , 0.0],
[ 0.0 , f , 0.0 , 0.0], [ 0.0 , f , 0.0 , 0.0],
[ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0], [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
[ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0], [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
] ]
} }
} }
impl Default for Camera { impl Default for Camera {
fn default() -> Self { fn default() -> Self {
Self { Self {
position: [0., 0., 0.], position: [0., 0., 0.],
direction: [0., 0., 0.], direction: [0., 0., 0.],
up: [0., 1., 0.], up: [0., 1., 0.],
fov: PI / 3., fov: PI / 3.,
zfar: 1024., zfar: 1024.,
znear: 0.1, znear: 0.1,
}
} }
}
} }

View file

@ -7,9 +7,9 @@ use glium::glutin::{
}; };
pub fn init_display(event_loop: &EventLoop<()>) -> Display { pub fn init_display(event_loop: &EventLoop<()>) -> Display {
let wb = WindowBuilder::new(); let wb = WindowBuilder::new();
let cb = ContextBuilder::new() let cb = ContextBuilder::new()
.with_depth_buffer(24) .with_depth_buffer(24)
.with_gl_profile(GlProfile::Core); .with_gl_profile(GlProfile::Core);
Display::new(wb, cb, event_loop).unwrap() Display::new(wb, cb, event_loop).unwrap()
} }

View file

@ -4,14 +4,14 @@ pub mod chunk;
pub mod colored2d; pub mod colored2d;
pub struct Programs { pub struct Programs {
pub colored_2d: Program, pub colored_2d: Program,
pub chunk: Program, pub chunk: Program,
} }
impl Programs { impl Programs {
pub fn compile_all(display: &Display) -> Self { pub fn compile_all(display: &Display) -> Self {
Self { Self {
colored_2d: Program::from_source(display, colored2d::VERTEX_SHADER, colored2d::FRAGMENT_SHADER, None).unwrap(), colored_2d: Program::from_source(display, colored2d::VERTEX_SHADER, colored2d::FRAGMENT_SHADER, None).unwrap(),
chunk: Program::from_source(display, chunk::VERTEX_SHADER, chunk::FRAGMENT_SHADER, None).unwrap(), chunk: Program::from_source(display, chunk::VERTEX_SHADER, chunk::FRAGMENT_SHADER, None).unwrap(),
}
} }
}
} }

View file

@ -2,40 +2,40 @@ use glium::implement_vertex;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Vertex { pub struct Vertex {
pub position: [f32; 3], pub position: [f32; 3],
pub normal: [f32; 3], pub normal: [f32; 3],
pub uv: [f32; 2], pub uv: [f32; 2],
} }
implement_vertex!(Vertex, position, normal, uv); implement_vertex!(Vertex, position, normal, uv);
//TODO store vertex data in a more compact way //TODO store vertex data in a more compact way
pub const VERTEX_SHADER: &str = r#" pub const VERTEX_SHADER: &str = r#"
#version 150 #version 150
in vec3 position; in vec3 position;
in vec3 normal; in vec3 normal;
in vec2 uv; in vec2 uv;
out vec3 v_normal; out vec3 v_normal;
out vec2 v_uv; out vec2 v_uv;
uniform mat4 perspective; uniform mat4 perspective;
uniform mat4 view; uniform mat4 view;
uniform mat4 model; uniform mat4 model;
void main() { void main() {
mat4 modelview = view * model; mat4 modelview = view * model;
v_normal = transpose(inverse(mat3(modelview))) * normal; v_normal = transpose(inverse(mat3(modelview))) * normal;
v_uv = uv; v_uv = uv;
gl_Position = perspective * modelview * vec4(position, 1.0); gl_Position = perspective * modelview * vec4(position, 1.0);
} }
"#; "#;
pub const FRAGMENT_SHADER: &str = r#" pub const FRAGMENT_SHADER: &str = r#"
#version 150 #version 150
in vec2 v_uv; in vec2 v_uv;
out vec4 color; out vec4 color;
uniform sampler2D tex; uniform sampler2D tex;
void main() { void main() {
color = texture(tex, v_uv); color = texture(tex, v_uv);
} }
"#; "#;

View file

@ -2,26 +2,26 @@ use glium::implement_vertex;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Vertex { pub struct Vertex {
pub position: [f32; 2] pub position: [f32; 2]
} }
implement_vertex!(Vertex, position); implement_vertex!(Vertex, position);
pub const VERTEX_SHADER: &str = r#" pub const VERTEX_SHADER: &str = r#"
#version 140 #version 140
in vec2 position; in vec2 position;
void main() { void main() {
gl_Position = vec4(position, 0., 1.); gl_Position = vec4(position, 0., 1.);
} }
"#; "#;
pub const FRAGMENT_SHADER: &str = r#" pub const FRAGMENT_SHADER: &str = r#"
#version 140 #version 140
out vec4 color; out vec4 color;
uniform vec4 u_color; uniform vec4 u_color;
void main() { void main() {
color = u_color; color = u_color;
} }
"#; "#;

View file

@ -5,41 +5,41 @@ use log::Level;
use std::io::Write; use std::io::Write;
pub fn init() { pub fn init() {
let mut env = Env::default(); let mut env = Env::default();
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
env = env.filter_or("RUST_LOG", "info"); env = env.filter_or("RUST_LOG", "info");
} }
Builder::from_env(env) Builder::from_env(env)
.format(|buf, record| { .format(|buf, record| {
let mut level_style = buf.style(); let mut level_style = buf.style();
level_style.set_color(match record.level() { level_style.set_color(match record.level() {
Level::Error => Color::Red, Level::Error => Color::Red,
Level::Warn => Color::Yellow, Level::Warn => Color::Yellow,
_ => Color::Blue _ => Color::Blue
}).set_bold(true); }).set_bold(true);
let mut location_style = buf.style(); let mut location_style = buf.style();
location_style.set_bold(true); location_style.set_bold(true);
location_style.set_dimmed(true); location_style.set_dimmed(true);
let mut location_line_style = buf.style(); let mut location_line_style = buf.style();
location_line_style.set_dimmed(true); location_line_style.set_dimmed(true);
writeln!( writeln!(
buf, buf,
"{} {:<50}\t{}{}{}", "{} {:<50}\t{}{}{}",
level_style.value(match record.level() { level_style.value(match record.level() {
Level::Error => "[e]", Level::Error => "[e]",
Level::Warn => "[w]", Level::Warn => "[w]",
Level::Info => "[i]", Level::Info => "[i]",
Level::Debug => "[d]", Level::Debug => "[d]",
Level::Trace => "[t]", Level::Trace => "[t]",
}), }),
format!("{}", record.args()), format!("{}", record.args()),
location_style.value(record.target()), location_style.value(record.target()),
location_line_style.value(" :"), location_line_style.value(" :"),
location_line_style.value(record.line().unwrap_or(0)) location_line_style.value(record.line().unwrap_or(0))
) )
}) })
.init(); .init();
} }

View file

@ -2,6 +2,6 @@ mod game;
mod logging; mod logging;
fn main() { fn main() {
logging::init(); logging::init();
game::run(); game::run();
} }