tossed together a very minimal graphics toy
This commit is contained in:
parent
c96ff044f0
commit
455e231b22
1458
Cargo.lock
generated
1458
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -6,8 +6,5 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
bgfx-rs = "0.7.0"
|
||||
|
||||
glfw = "0.44.0"
|
||||
raw-window-handle = "0.4.2"
|
||||
glium = "0.31"
|
||||
image = "0.24.1"
|
BIN
assets/opengl.png
Normal file
BIN
assets/opengl.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 71 KiB |
10
assets/shade.frag
Normal file
10
assets/shade.frag
Normal file
|
@ -0,0 +1,10 @@
|
|||
#version 140
|
||||
|
||||
in vec2 v_tex_coords;
|
||||
out vec4 color;
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
color = texture(tex, v_tex_coords);
|
||||
}
|
12
assets/shade.vert
Normal file
12
assets/shade.vert
Normal file
|
@ -0,0 +1,12 @@
|
|||
#version 140
|
||||
|
||||
in vec2 position;
|
||||
in vec2 tex_coords;
|
||||
out vec2 v_tex_coords;
|
||||
|
||||
uniform mat4 matrix;
|
||||
|
||||
void main() {
|
||||
v_tex_coords = tex_coords;
|
||||
gl_Position = matrix * vec4(position, 0.0, 1.0);
|
||||
}
|
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
|
@ -0,0 +1 @@
|
|||
nightly
|
5
src/config.rs
Normal file
5
src/config.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
//! A few constants to make standardizing sizes easier.
|
||||
|
||||
pub const CHUNK_RADIUS: u8 = 3;
|
||||
pub const CHUNK_HEIGHT: u8 = 4;
|
||||
pub const PLAYER_HEIGHT: f64 = 1.7;
|
211
src/main.rs
211
src/main.rs
|
@ -1,99 +1,148 @@
|
|||
#![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 platform;
|
||||
pub mod textures;
|
||||
pub mod voxel;
|
||||
|
||||
use bgfx::*;
|
||||
use bgfx_rs::bgfx;
|
||||
use glfw::{Action, Key, Window};
|
||||
use platform::{get_platform_data, get_render_type};
|
||||
|
||||
const WIDTH: usize = 1280;
|
||||
const HEIGHT: usize = 720;
|
||||
|
||||
fn main() {
|
||||
let hex_coord_1 = coordinates::Hex {
|
||||
q: 0.0,
|
||||
r: 0.0,
|
||||
s: 0.0,
|
||||
y: 0.0,
|
||||
};
|
||||
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 hex_coord_2 = coordinates::Hex {
|
||||
q: 1.0,
|
||||
r: 1.0,
|
||||
s: 1.1,
|
||||
y: 0.0,
|
||||
};
|
||||
hex_coord_2.round();
|
||||
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();
|
||||
|
||||
println!("{}", hex_coord_1.distance_to(hex_coord_2));
|
||||
|
||||
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
|
||||
glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi));
|
||||
|
||||
let (mut window, events) = glfw
|
||||
.create_window(
|
||||
WIDTH as _,
|
||||
HEIGHT as _,
|
||||
"Vox - ESC to close",
|
||||
glfw::WindowMode::Windowed,
|
||||
)
|
||||
.expect("Failed to create GLFW window.");
|
||||
|
||||
window.set_key_polling(true);
|
||||
|
||||
let init = init(&window);
|
||||
if !bgfx::init(&init) {
|
||||
panic!("failed to init bgfx");
|
||||
#[derive(Copy, Clone)]
|
||||
struct Vertex {
|
||||
position: [f32; 2],
|
||||
tex_coords: [f32; 2],
|
||||
}
|
||||
|
||||
bgfx::set_view_clear(
|
||||
0,
|
||||
ClearFlags::COLOR.bits() | ClearFlags::DEPTH.bits(),
|
||||
SetViewClearArgs {
|
||||
rgba: 0x00000000,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
implement_vertex!(Vertex, position, tex_coords);
|
||||
|
||||
let mut old_size = (0, 0);
|
||||
#[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] };
|
||||
|
||||
while !window.should_close() {
|
||||
glfw.poll_events();
|
||||
for (_, event) in glfw::flush_messages(&events) {
|
||||
if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event {
|
||||
window.set_should_close(true)
|
||||
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 size = window.get_framebuffer_size();
|
||||
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);
|
||||
|
||||
if old_size != size {
|
||||
bgfx::reset(size.0 as _, size.1 as _, ResetArgs::default());
|
||||
old_size = size;
|
||||
// we update `t`
|
||||
t += 0.0002;
|
||||
if t > 0.5 {
|
||||
t = -0.5;
|
||||
}
|
||||
|
||||
bgfx::set_view_rect(0, 0, 0, size.0 as _, size.1 as _);
|
||||
bgfx::touch(0);
|
||||
let mut target = display.draw();
|
||||
target.clear_color(0.0, 0.0, 1.0, 0.0);
|
||||
|
||||
bgfx::frame(false);
|
||||
}
|
||||
shutdown_handler();
|
||||
}
|
||||
pub fn shutdown_handler() {
|
||||
bgfx::shutdown();
|
||||
|
||||
println!("shutdown");
|
||||
}
|
||||
|
||||
pub fn init(window: &Window) -> Init {
|
||||
let mut init = Init::new();
|
||||
|
||||
init.type_r = get_render_type();
|
||||
init.resolution.width = WIDTH as u32;
|
||||
init.resolution.height = HEIGHT as u32;
|
||||
init.resolution.reset = ResetFlags::VSYNC.bits();
|
||||
init.platform_data = get_platform_data(&window);
|
||||
init
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
use bgfx_rs::static_lib::{PlatformData, RendererType};
|
||||
use glfw::Window;
|
||||
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
|
||||
|
||||
pub fn get_platform_data(window: &Window) -> PlatformData {
|
||||
let mut pd = PlatformData::new();
|
||||
|
||||
match window.raw_window_handle() {
|
||||
#[cfg(any(
|
||||
target_os = "linux",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd"
|
||||
))]
|
||||
RawWindowHandle::Xlib(data) => {
|
||||
pd.nwh = data.window as *mut _;
|
||||
pd.ndt = data.display as *mut _;
|
||||
}
|
||||
#[cfg(any(
|
||||
target_os = "linux",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd"
|
||||
))]
|
||||
RawWindowHandle::Wayland(data) => {
|
||||
pd.ndt = data.surface; // same as window, on wayland there ins't a concept of windows
|
||||
pd.nwh = data.display;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
RawWindowHandle::MacOS(data) => {
|
||||
pd.nwh = data.ns_window;
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
RawWindowHandle::Win32(data) => {
|
||||
pd.nwh = data.hwnd;
|
||||
}
|
||||
_ => panic!("Unsupported Window Manager"),
|
||||
}
|
||||
|
||||
return pd;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn get_render_type() -> RendererType {
|
||||
RendererType::OpenGL
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn get_render_type() -> RendererType {
|
||||
RendererType::Count
|
||||
}
|
1
src/textures.rs
Normal file
1
src/textures.rs
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -1,3 +1,12 @@
|
|||
#[repr(C)]
|
||||
pub struct Light {
|
||||
pub red: u8,
|
||||
pub green: u8,
|
||||
pub blue: u8,
|
||||
pub sun: u8,
|
||||
}
|
||||
|
||||
pub struct VoxelData {
|
||||
pub id: u32,
|
||||
pub light_level: Light,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue