tossed together a very minimal graphics toy

This commit is contained in:
Able 2022-03-26 06:34:58 -05:00
parent c96ff044f0
commit 455e231b22
11 changed files with 1595 additions and 173 deletions

1458
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,8 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
glium = "0.31"
bgfx-rs = "0.7.0" image = "0.24.1"
glfw = "0.44.0"
raw-window-handle = "0.4.2"

BIN
assets/opengl.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

10
assets/shade.frag Normal file
View 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
View 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
View file

@ -0,0 +1 @@
nightly

5
src/config.rs Normal file
View 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;

View file

@ -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 chunk;
pub mod config;
pub mod coordinates; pub mod coordinates;
pub mod platform; pub mod textures;
pub mod voxel; 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() { fn main() {
let hex_coord_1 = coordinates::Hex { let event_loop = glutin::event_loop::EventLoop::new();
q: 0.0, let wb = glutin::window::WindowBuilder::new()
r: 0.0, .with_title("Voxel Engine")
s: 0.0, .with_resizable(true)
y: 0.0, .with_transparent(true);
}; let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
let hex_coord_2 = coordinates::Hex { let image = image::load(
q: 1.0, Cursor::new(&include_bytes!("../assets/opengl.png")),
r: 1.0, image::ImageFormat::Png,
s: 1.1, )
y: 0.0, .unwrap()
}; .to_rgba8();
hex_coord_2.round(); 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)); #[derive(Copy, Clone)]
struct Vertex {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); position: [f32; 2],
glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi)); tex_coords: [f32; 2],
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");
} }
bgfx::set_view_clear( implement_vertex!(Vertex, position, tex_coords);
0,
ClearFlags::COLOR.bits() | ClearFlags::DEPTH.bits(),
SetViewClearArgs {
rgba: 0x00000000,
..Default::default()
},
);
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() { let shape = vec![vertex1, vertex2, vertex3];
glfw.poll_events();
for (_, event) in glfw::flush_messages(&events) { let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event { let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
window.set_should_close(true)
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 { // we update `t`
bgfx::reset(size.0 as _, size.1 as _, ResetArgs::default()); t += 0.0002;
old_size = size; if t > 0.5 {
t = -0.5;
} }
bgfx::set_view_rect(0, 0, 0, size.0 as _, size.1 as _); let mut target = display.draw();
bgfx::touch(0); target.clear_color(0.0, 0.0, 1.0, 0.0);
bgfx::frame(false); let uniforms = uniform! {
} matrix: [
shutdown_handler(); [1.0, 0.0, 0.0, 0.0],
} [0.0, 1.0, 0.0, 0.0],
pub fn shutdown_handler() { [0.0, 0.0, 1.0, 0.0],
bgfx::shutdown(); [ t , 0.0, 0.0, 1.0f32],
],
println!("shutdown"); tex: &texture,
} };
pub fn init(window: &Window) -> Init { target
let mut init = Init::new(); .draw(
&vertex_buffer,
init.type_r = get_render_type(); &indices,
init.resolution.width = WIDTH as u32; &program,
init.resolution.height = HEIGHT as u32; &uniforms,
init.resolution.reset = ResetFlags::VSYNC.bits(); &Default::default(),
init.platform_data = get_platform_data(&window); )
init .unwrap();
target.finish().unwrap();
});
} }

View file

@ -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
View file

@ -0,0 +1 @@

View file

@ -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 struct VoxelData {
pub id: u32, pub id: u32,
pub light_level: Light,
} }