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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,14 +4,14 @@ pub mod chunk;
pub mod colored2d;
pub struct Programs {
pub colored_2d: Program,
pub chunk: Program,
pub colored_2d: Program,
pub chunk: Program,
}
impl Programs {
pub fn compile_all(display: &Display) -> Self {
Self {
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(),
}
pub fn compile_all(display: &Display) -> Self {
Self {
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(),
}
}
}

View file

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

View file

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

View file

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

View file

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