149 lines
4.4 KiB
Rust
149 lines
4.4 KiB
Rust
#![feature(const_fn_trait_bound)]
|
|
|
|
use std::io::Cursor;
|
|
|
|
#[macro_use]
|
|
extern crate glium;
|
|
|
|
use glium::{
|
|
glutin::{self, event::KeyboardInput},
|
|
Surface,
|
|
};
|
|
pub mod chunk;
|
|
pub mod config;
|
|
pub mod coordinates;
|
|
pub mod textures;
|
|
pub mod voxel;
|
|
fn main() {
|
|
let event_loop = glutin::event_loop::EventLoop::new();
|
|
let wb = glutin::window::WindowBuilder::new()
|
|
.with_title("Voxel Engine")
|
|
.with_resizable(true)
|
|
.with_transparent(true);
|
|
let cb = glutin::ContextBuilder::new();
|
|
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
|
|
|
|
let image = image::load(
|
|
Cursor::new(&include_bytes!("../assets/opengl.png")),
|
|
image::ImageFormat::Png,
|
|
)
|
|
.unwrap()
|
|
.to_rgba8();
|
|
let image_dimensions = image.dimensions();
|
|
let image =
|
|
glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
|
|
let texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
|
|
|
|
#[derive(Copy, Clone)]
|
|
struct Vertex {
|
|
position: [f32; 2],
|
|
tex_coords: [f32; 2],
|
|
}
|
|
|
|
implement_vertex!(Vertex, position, tex_coords);
|
|
|
|
#[rustfmt::skip]
|
|
let vertex1 = Vertex { position: [-0.5, -0.5], tex_coords: [0.0, 0.0] };
|
|
#[rustfmt::skip]
|
|
let vertex2 = Vertex { position: [ 0.0, 0.5], tex_coords: [0.0, 1.0] };
|
|
#[rustfmt::skip]
|
|
let vertex3 = Vertex { position: [ 0.5, -0.25], tex_coords: [1.0, 0.0] };
|
|
|
|
let shape = vec![vertex1, vertex2, vertex3];
|
|
|
|
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
|
|
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
|
|
|
|
let vertex_shader_src = include_str!("../assets/shade.vert");
|
|
let fragment_shader_src = include_str!("../assets/shade.frag");
|
|
|
|
let program =
|
|
glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None)
|
|
.unwrap();
|
|
|
|
let mut t: f32 = -0.5;
|
|
event_loop.run(move |event, _, control_flow| {
|
|
use glutin::event::Event::*;
|
|
match event {
|
|
WindowEvent { event, .. } => match event {
|
|
glutin::event::WindowEvent::CloseRequested => {
|
|
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
|
return;
|
|
}
|
|
_ => return,
|
|
},
|
|
|
|
NewEvents(cause) => {
|
|
use glutin::event::StartCause::*;
|
|
match cause {
|
|
ResumeTimeReached { .. } => (),
|
|
Init => (),
|
|
_ => return,
|
|
}
|
|
}
|
|
DeviceEvent {
|
|
device_id: _,
|
|
event,
|
|
} => match event {
|
|
glutin::event::DeviceEvent::Key(key) => match key {
|
|
KeyboardInput {
|
|
virtual_keycode: Some(glutin::event::VirtualKeyCode::Escape),
|
|
..
|
|
} => {
|
|
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
|
return;
|
|
}
|
|
KeyboardInput {
|
|
state: glutin::event::ElementState::Pressed,
|
|
virtual_keycode,
|
|
..
|
|
} => {
|
|
println!("{:?}", virtual_keycode);
|
|
return;
|
|
}
|
|
_ => return,
|
|
},
|
|
_ => return,
|
|
},
|
|
|
|
_ => {
|
|
return;
|
|
}
|
|
}
|
|
|
|
let next_frame_time =
|
|
std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
|
|
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
|
|
|
|
// we update `t`
|
|
t += 0.0002;
|
|
if t > 0.5 {
|
|
t = -0.5;
|
|
}
|
|
|
|
let mut target = display.draw();
|
|
target.clear_color(0.0, 0.0, 1.0, 0.0);
|
|
|
|
let uniforms = uniform! {
|
|
matrix: [
|
|
[1.0, 0.0, 0.0, 0.0],
|
|
[0.0, 1.0, 0.0, 0.0],
|
|
[0.0, 0.0, 1.0, 0.0],
|
|
[ t , 0.0, 0.0, 1.0f32],
|
|
],
|
|
tex: &texture,
|
|
};
|
|
|
|
target
|
|
.draw(
|
|
&vertex_buffer,
|
|
&indices,
|
|
&program,
|
|
&uniforms,
|
|
&Default::default(),
|
|
)
|
|
.unwrap();
|
|
target.finish().unwrap();
|
|
});
|
|
}
|