diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/render-bsp/src/main.rs | 56 | ||||
-rw-r--r-- | examples/render-quad/Cargo.toml | 12 | ||||
-rw-r--r-- | examples/render-quad/README.md | 3 | ||||
-rw-r--r-- | examples/render-quad/data/test1.png | bin | 486777 -> 0 bytes | |||
-rw-r--r-- | examples/render-quad/data/test2.png | bin | 375052 -> 0 bytes | |||
-rw-r--r-- | examples/render-quad/src/main.rs | 130 | ||||
-rw-r--r-- | stockton-render/src/draw/camera.rs | 15 | ||||
-rw-r--r-- | stockton-render/src/draw/context.rs | 4 |
10 files changed, 40 insertions, 184 deletions
@@ -54,3 +54,6 @@ GitHub.sublime-settings .history # End of https://www.gitignore.io/api/rust,sublimetext,visualstudiocode + +# Game data. Used for testing, but not sure about license yet +textures/ @@ -3,6 +3,5 @@ members = [ "stockton-types", "stockton-render", "stockton-levels", - "examples/render-quad", "examples/render-bsp" ]
\ No newline at end of file diff --git a/examples/render-bsp/src/main.rs b/examples/render-bsp/src/main.rs index 0d81759..37807a9 100644 --- a/examples/render-bsp/src/main.rs +++ b/examples/render-bsp/src/main.rs @@ -21,16 +21,9 @@ extern crate stockton_render; extern crate winit; extern crate log; extern crate simple_logger; -extern crate image; use std::time::SystemTime; use std::f32::consts::PI; -use image::load_from_memory; - -use stockton_levels::prelude::*; -use stockton_levels::q3::Q3BSPFile; -use stockton_types::{World, Vector3, Vector2}; -use stockton_render::Renderer; use winit::{ event::{Event, WindowEvent, ElementState}, @@ -38,12 +31,21 @@ use winit::{ window::WindowBuilder }; +use stockton_levels::{ + prelude::*, + q3::Q3BSPFile +}; +use stockton_types::{World, Vector3, Vector2}; +use stockton_render::Renderer; + + /// Movement speed, world units per second const SPEED: f32 = 100.0; /// Pixels required to rotate 90 degrees const PIXELS_PER_90D: f32 = 100.0; +/// Sensitivity, derived from above const SENSITIVITY: f32 = PI / (2.0 * PIXELS_PER_90D); #[derive(Debug)] @@ -68,6 +70,7 @@ impl KeyState { } } + /// Helper function to get our movement request as a normalized vector pub fn as_vector(&self) -> Vector3 { let mut vec = Vector3::new(0.0, 0.0, 0.0); @@ -89,14 +92,16 @@ impl KeyState { vec.z = -1.0; } + vec.normalize_mut(); vec } } fn main() { + // Initialise logger simple_logger::init_with_level(log::Level::Debug).unwrap(); - // Load the world and renderer + // Make a window let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); @@ -105,25 +110,17 @@ fn main() { } window.set_cursor_visible(false); + // Parse the map file and swizzle the co-ords let data = include_bytes!("../data/test.bsp").to_vec().into_boxed_slice(); let bsp: Result<Q3BSPFile<Q3System>, stockton_levels::types::ParseError> = Q3BSPFile::parse_file(&data); let bsp: Q3BSPFile<Q3System> = bsp.unwrap(); let bsp: Q3BSPFile<VulkanSystem> = bsp.swizzle_to(); + // Load into a world and create the new renderer let world = World::new(bsp); let mut renderer = Renderer::new(world, &window).unwrap(); - // { - // renderer.context.add_texture( - // load_from_memory(include_bytes!("../../render-quad/data/test1.png")) - // .expect("Couldn't load test texture 1") - // .into_rgba()).unwrap(); - - // renderer.context.add_texture( - // load_from_memory(include_bytes!("../../render-quad/data/test2.png")) - // .expect("Couldn't load test texture 2") - // .into_rgba()).unwrap(); - // } + // Done loading - This is our main loop. let mut last_update = SystemTime::now(); let mut key_state = KeyState::new(); @@ -137,9 +134,12 @@ fn main() { event, .. } => match event { + // Close when requested WindowEvent::CloseRequested => { *flow = ControlFlow::Exit }, + + // Update our keystates WindowEvent::KeyboardInput {input, ..} => match input.scancode { // A 30 => key_state.left = input.state == ElementState::Pressed, @@ -155,41 +155,53 @@ fn main() { 42 => key_state.down = input.state == ElementState::Pressed, _ => () }, + + // Look around with mouse WindowEvent::CursorMoved { position, .. } => { - // Special case: First frame + // Don't do anything on the first frame if last_cursor_pos.x != 0.0 || last_cursor_pos.y == 0.0 { + // Figure out how much to rotate by let x_offset = (position.x as f32 - last_cursor_pos.x) * SENSITIVITY; let y_offset = (position.y as f32 - last_cursor_pos.y) * SENSITIVITY; + // Rotate renderer.context.rotate(Vector3::new( -y_offset, x_offset, 0.0 )); } - + + // For tracking how much the mouse has moved last_cursor_pos.x = position.x as f32; last_cursor_pos.y = position.y as f32; } _ => () }, + // Nothing new happened Event::MainEventsCleared => { + // Draw as many frames as we can + // This isn't ideal, but it'll do for now. window.request_redraw() }, - Event::RedrawRequested(_) => { + // Redraw - either from above or resizing, etc + Event::RedrawRequested(_) => { + // Time since last frame drawn. Again, not ideal. let delta = last_update.elapsed().unwrap().as_secs_f32(); last_update = SystemTime::now(); + // Move our camera if needed let delta_pos = key_state.as_vector() * delta * SPEED; if delta_pos.x != 0.0 || delta_pos.y != 0.0 || delta_pos.z != 0.0 { renderer.context.move_camera_relative(delta_pos); } + // Render the frame renderer.render_frame().unwrap() } _ => () diff --git a/examples/render-quad/Cargo.toml b/examples/render-quad/Cargo.toml deleted file mode 100644 index d376605..0000000 --- a/examples/render-quad/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "render-quad" -version = "0.1.0" -authors = ["Oscar <oscar.shrimpton.personal@gmail.com>"] - -[dependencies] -stockton-render = { path = "../../stockton-render", features = ["vulkan"] } -stockton-types = { path = "../../stockton-types" } -stockton-bsp = { git = "https://github.com/tcmal/rust-bsp.git" } -winit = "^0.21" -simple_logger = "1.0" -image = "0.23.2" diff --git a/examples/render-quad/README.md b/examples/render-quad/README.md deleted file mode 100644 index c129ff6..0000000 --- a/examples/render-quad/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# render-quad - -Renders a rainbow quad. The top-left corner follows your mouse.
\ No newline at end of file diff --git a/examples/render-quad/data/test1.png b/examples/render-quad/data/test1.png Binary files differdeleted file mode 100644 index 3d37758..0000000 --- a/examples/render-quad/data/test1.png +++ /dev/null diff --git a/examples/render-quad/data/test2.png b/examples/render-quad/data/test2.png Binary files differdeleted file mode 100644 index f33cc3e..0000000 --- a/examples/render-quad/data/test2.png +++ /dev/null diff --git a/examples/render-quad/src/main.rs b/examples/render-quad/src/main.rs deleted file mode 100644 index ea22b18..0000000 --- a/examples/render-quad/src/main.rs +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright (C) Oscar Shrimpton 2019 - -// This program is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free -// Software Foundation, either version 3 of the License, or (at your option) -// any later version. - -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -// more details. - -// You should have received a copy of the GNU General Public License along -// with this program. If not, see <http://www.gnu.org/licenses/>. - -//! Renders ./example.bsp - -extern crate stockton_types; -extern crate stockton_bsp; -extern crate stockton_render; -extern crate winit; -extern crate simple_logger; -extern crate image; - -use stockton_render::draw::{RenderingContext, UVPoint}; -use stockton_types::{Vector2, Vector3}; -use image::load_from_memory; - -use winit::{ - event::{Event, WindowEvent}, - event_loop::{ControlFlow, EventLoop}, - window::WindowBuilder -}; - -fn main() { - let cube_points: [UVPoint; 24] = [ - // Face 1 (front) - UVPoint(Vector3::new(0.0, 0.0, 0.0), Vector2::new(0.0, 1.0), 0), /* bottom left */ - UVPoint(Vector3::new(0.0, 1.0, 0.0), Vector2::new(0.0, 0.0), 0), /* top left */ - UVPoint(Vector3::new(1.0, 0.0, 0.0), Vector2::new(1.0, 1.0), 0), /* bottom right */ - UVPoint(Vector3::new(1.0, 1.0, 0.0), Vector2::new(1.0, 0.0), 0), /* top right */ - // Face 2 (top) - UVPoint(Vector3::new(0.0, 1.0, 0.0), Vector2::new(0.0, 1.0), 0), /* bottom left */ - UVPoint(Vector3::new(0.0, 1.0, 1.0), Vector2::new(0.0, 0.0), 0), /* top left */ - UVPoint(Vector3::new(1.0, 1.0, 0.0), Vector2::new(1.0, 1.0), 0), /* bottom right */ - UVPoint(Vector3::new(1.0, 1.0, 1.0), Vector2::new(1.0, 0.0), 0), /* top right */ - // Face 3 (back) - UVPoint(Vector3::new(0.0, 0.0, 1.0), Vector2::new(0.0, 1.0), 1), /* bottom left */ - UVPoint(Vector3::new(0.0, 1.0, 1.0), Vector2::new(0.0, 0.0), 1), /* top left */ - UVPoint(Vector3::new(1.0, 0.0, 1.0), Vector2::new(1.0, 1.0), 1), /* bottom right */ - UVPoint(Vector3::new(1.0, 1.0, 1.0), Vector2::new(1.0, 0.0), 1), /* top right */ - // Face 4 (bottom) - UVPoint(Vector3::new(0.0, 0.0, 0.0), Vector2::new(0.0, 1.0), 1), /* bottom left */ - UVPoint(Vector3::new(0.0, 0.0, 1.0), Vector2::new(0.0, 0.0), 1), /* top left */ - UVPoint(Vector3::new(1.0, 0.0, 0.0), Vector2::new(1.0, 1.0), 1), /* bottom right */ - UVPoint(Vector3::new(1.0, 0.0, 1.0), Vector2::new(1.0, 0.0), 1), /* top right */ - // Face 5 (left) - UVPoint(Vector3::new(0.0, 0.0, 1.0), Vector2::new(0.0, 1.0), 1), /* bottom left */ - UVPoint(Vector3::new(0.0, 1.0, 1.0), Vector2::new(0.0, 0.0), 1), /* top left */ - UVPoint(Vector3::new(0.0, 0.0, 0.0), Vector2::new(1.0, 1.0), 1), /* bottom right */ - UVPoint(Vector3::new(0.0, 1.0, 0.0), Vector2::new(1.0, 0.0), 1), /* top right */ - // Face 6 (right) - UVPoint(Vector3::new(1.0, 0.0, 0.0), Vector2::new(0.0, 1.0), 0), /* bottom left */ - UVPoint(Vector3::new(1.0, 1.0, 0.0), Vector2::new(0.0, 0.0), 0), /* top left */ - UVPoint(Vector3::new(1.0, 0.0, 1.0), Vector2::new(1.0, 1.0), 0), /* bottom right */ - UVPoint(Vector3::new(1.0, 1.0, 1.0), Vector2::new(1.0, 0.0), 0), /* top right */ - ]; - let cube_indices: [(u16, u16, u16); 12] = [ - (0, 1, 2), (2, 1, 3), // front - (4, 5, 6), (7, 6, 5), // top - (10, 9, 8), (9, 10, 11), // back - (12, 14, 13), (15, 13, 14), // bottom - (16, 17, 18), (19, 18, 17), // left - (20, 21, 22), (23, 22, 21), // right - ]; - - simple_logger::init().unwrap(); - - // Create the renderer. - let event_loop = EventLoop::new(); - let window = WindowBuilder::new().build(&event_loop).unwrap(); - let mut ctx = RenderingContext::new(&window).unwrap(); - - // Load 2 test textures - ctx.add_texture( - load_from_memory(include_bytes!("../data/test1.png")) - .expect("Couldn't load test texture 1") - .into_rgba()) - .unwrap(); - ctx.add_texture( - load_from_memory(include_bytes!("../data/test2.png")) - .expect("Couldn't load test texture 2") - .into_rgba()) - .unwrap(); - - // First quad with test1 - for (index, point) in cube_points.iter().enumerate() { - ctx.vert_buffer[index] = *point; - } - - for (index, value) in cube_indices.iter().enumerate() { - ctx.index_buffer[index] = *value; - } - - event_loop.run(move |event, _, flow| { - *flow = ControlFlow::Poll; - - match event { - Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => { - *flow = ControlFlow::Exit - } - - Event::MainEventsCleared => { - window.request_redraw() - }, - Event::RedrawRequested(_) => { - if let Err(_) = ctx.draw_vertices() { - unsafe {ctx.handle_surface_change().unwrap()}; - - // If it fails twice, then panic - ctx.draw_vertices().unwrap(); - } - } - _ => () - } - }); -} diff --git a/stockton-render/src/draw/camera.rs b/stockton-render/src/draw/camera.rs index 1a16803..0e9011a 100644 --- a/stockton-render/src/draw/camera.rs +++ b/stockton-render/src/draw/camera.rs @@ -15,19 +15,10 @@ //! Things related to converting 3D world space to 2D screen space -use std::iter::once; -use std::f32::consts::PI; -use hal::prelude::*; -use hal::buffer::Usage; -use na::{look_at_lh, perspective_lh_zo}; - -use core::mem::ManuallyDrop; - -use crate::error; -use crate::types::*; -use super::buffer::{StagedBuffer, ModifiableBuffer}; use stockton_types::{Vector3, Matrix4}; -use na::{Mat4, Vec4}; + +use std::f32::consts::PI; +use na::{look_at_lh, perspective_lh_zo, Mat4, Vec4}; /// 90 degrees in radians const R89: f32 = (PI / 180.0) * 89.0; diff --git a/stockton-render/src/draw/context.rs b/stockton-render/src/draw/context.rs index d4b1f84..b76bed7 100644 --- a/stockton-render/src/draw/context.rs +++ b/stockton-render/src/draw/context.rs @@ -25,7 +25,6 @@ use std::{ }; use winit::window::Window; use arrayvec::ArrayVec; -use image::RgbaImage; use hal::{ prelude::*, @@ -56,9 +55,6 @@ const COLOR_RANGE: hal::image::SubresourceRange = hal::image::SubresourceRange { layers: 0..1, }; -/// Size of texturestore. This needs to sync up with the array size in the fragment shader -const INITIAL_TEX_SIZE: usize = 2; - /// Initial size of vertex buffer. TODO: Way of overriding this const INITIAL_VERT_SIZE: u64 = 3 * 3000; |