-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
552dc90
commit c41ed94
Showing
10 changed files
with
190 additions
and
187 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use winit::event_loop::{ControlFlow, EventLoop}; | ||
use winit::window::{Window, WindowBuilder}; | ||
use cgmath::*; | ||
use wgpu::{Device, Queue, Surface, SurfaceConfiguration}; | ||
use winit::dpi::PhysicalSize; | ||
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}; | ||
use winit::platform::unix::EventLoopExtUnix; | ||
use crate::scene::Scene; | ||
|
||
pub const OPENGL_TO_WGPU_MATRIX: Matrix4<f32> = Matrix4::new( | ||
1.0, 0.0, 0.0, 0.0, | ||
0.0, 1.0, 0.0, 0.0, | ||
0.0, 0.0, 0.5, 0.0, | ||
0.0, 0.0, 0.5, 1.0, | ||
); | ||
|
||
pub struct Engine { | ||
pub window: Window, | ||
pub surface: Surface, | ||
pub device: Device, | ||
pub queue: Queue, | ||
pub config: SurfaceConfiguration, | ||
pub size: PhysicalSize<u32>, | ||
} | ||
|
||
|
||
impl Engine { | ||
pub fn new(name: &str, any_thread: bool) -> (Self, EventLoop<()>) { | ||
env_logger::init(); | ||
let event_loop = if any_thread { EventLoop::new_any_thread() } else { EventLoop::new() }; | ||
let window = WindowBuilder::new().build(&event_loop).unwrap(); | ||
window.set_title(name); | ||
|
||
let (surface, device, queue, config, size) = pollster::block_on(init_wgpu(&window)); | ||
|
||
let app = Engine { | ||
window, | ||
surface, | ||
device, | ||
queue, | ||
config, | ||
size, | ||
}; | ||
|
||
(app, event_loop) | ||
} | ||
|
||
pub fn resize(&mut self, new_size: PhysicalSize<u32>) { | ||
if new_size.width > 0 && new_size.height > 0 { | ||
self.size = new_size; | ||
self.config.width = new_size.width; | ||
self.config.height = new_size.height; | ||
self.surface.configure(&self.device, &self.config); | ||
} | ||
} | ||
|
||
pub fn manage_event(&mut self, event: &WindowEvent) { | ||
match event { | ||
WindowEvent::Resized(physical_size) => { | ||
self.resize(*physical_size); | ||
} | ||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { | ||
self.resize(**new_inner_size); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
pub fn start(mut self, mut scene: Scene, event_loop: EventLoop<()>, mut callback: impl FnMut() + 'static) { | ||
event_loop.run(move |event, _, control_flow| match event { | ||
Event::WindowEvent { | ||
ref event, | ||
window_id | ||
} if window_id == self.window.id() => { | ||
self.manage_event(event); | ||
scene.manage_event(event); | ||
|
||
match event { | ||
WindowEvent::CloseRequested | WindowEvent::KeyboardInput { | ||
input: | ||
KeyboardInput { | ||
state: ElementState::Pressed, | ||
virtual_keycode: Some(VirtualKeyCode::Escape), | ||
.. | ||
}, | ||
.. | ||
} => *control_flow = ControlFlow::Exit, | ||
_ => {} | ||
} | ||
} | ||
Event::RedrawRequested(_) => { | ||
scene.update(&mut self); | ||
|
||
callback(); | ||
|
||
match scene.render(&mut self) { | ||
Ok(_) => {} | ||
Err(wgpu::SurfaceError::Lost) => { | ||
scene.resize(self.window.inner_size()); | ||
self.resize(self.window.inner_size()); | ||
} | ||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, | ||
Err(e) => eprintln!("{}", e) | ||
} | ||
} | ||
Event::MainEventsCleared => { | ||
self.window.request_redraw(); | ||
} | ||
_ => {} | ||
}); | ||
} | ||
} | ||
|
||
pub async fn init_wgpu(window: &Window) -> (Surface, Device, Queue, SurfaceConfiguration, PhysicalSize<u32>) { | ||
let size = window.inner_size(); | ||
let instance = wgpu::Instance::new(wgpu::Backends::all()); | ||
//let instance = wgpu::Instance::new(wgpu::Backends::VULKAN); | ||
let surface = unsafe { instance.create_surface(window) }; | ||
let adapter = instance | ||
.request_adapter(&wgpu::RequestAdapterOptions { | ||
power_preference: wgpu::PowerPreference::default(), | ||
compatible_surface: Some(&surface), | ||
force_fallback_adapter: false, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let (device, queue) = adapter | ||
.request_device( | ||
&wgpu::DeviceDescriptor { | ||
label: None, | ||
features: wgpu::Features::POLYGON_MODE_LINE, | ||
limits: wgpu::Limits::default(), | ||
}, | ||
None, // Trace path | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let config = wgpu::SurfaceConfiguration { | ||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT, | ||
format: *surface.get_supported_formats(&adapter).first().unwrap(), | ||
width: size.width, | ||
height: size.height, | ||
present_mode: wgpu::PresentMode::Fifo, | ||
}; | ||
surface.configure(&device, &config); | ||
|
||
(surface, device, queue, config, size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
use winit::platform::unix::EventLoopExtUnix; | ||
|
||
pub mod engine; | ||
pub mod scene; | ||
pub mod camera; | ||
pub mod mesh; | ||
pub mod procedural; | ||
pub mod transform; | ||
pub mod material; | ||
pub mod app; | ||
pub mod engine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
extern crate gilgamesh; | ||
|
||
use gilgamesh::app::App; | ||
use gilgamesh::engine::Engine; | ||
use gilgamesh::mesh::Mesh; | ||
use gilgamesh::scene::Scene; | ||
|
||
fn main() { | ||
let (mut app, event_loop) = App::new(); | ||
let (mut engine, event_loop) = Engine::new("Gilgamesh", false); | ||
|
||
let mut scene = Scene::new(&app); | ||
let mut scene = Scene::new(&engine); | ||
|
||
let sphere = Mesh::new_procedural_sphere(5.0, 32, &|x, y, z| { | ||
f32::powi(f32::sin(60.0 * x * y * z), 2) * 0.5 | ||
}, 0.5, &mut app); | ||
}, 0.5, &mut engine); | ||
|
||
scene.add_mesh(sphere); | ||
|
||
app.start(scene, event_loop); | ||
engine.start(scene, event_loop, move || { | ||
println!("Hello, world!"); | ||
}); | ||
} |
Oops, something went wrong.