Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in fj-viewer #633

Merged
merged 6 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions crates/fj-math/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Transform {
))
}

/// Project transform according to camera specfication, return data as an array.
/// Project transform according to camera specification, return data as an array.
/// Used primarily for graphics code.
pub fn project_to_array(
&self,
Expand All @@ -110,14 +110,13 @@ impl Transform {
zfar: f64,
) -> [Scalar; 16] {
let projection = Perspective3::new(aspect_ratio, fovy, znear, zfar);
(projection.to_projective() * self.0)
.matrix()
.as_slice()
.iter()
.map(|f| Scalar::from(*f))
.collect::<Vec<Scalar>>()
.try_into()
.unwrap()

let mut array = [0.; 16];
array.copy_from_slice(
(projection.to_projective() * self.0).matrix().as_slice(),
);

array.map(Scalar::from)
}

/// Transform the given axis-aligned bounding box
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-viewer/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Renderer {
///
/// // Create window
/// let event_loop = winit::event_loop::EventLoop::new();
/// let window = window::Window::new(&event_loop);
/// let window = window::Window::new(&event_loop).unwrap();
///
/// // Attach renderer to the window
/// let mut renderer = graphics::Renderer::new(&window);
Expand Down Expand Up @@ -347,7 +347,7 @@ pub enum InitError {

/// Graphics rendering error
///
/// Describes errors related to non intialization graphics errors.
/// Describes errors related to non initialization graphics errors.
#[derive(Error, Debug)]
pub enum DrawError {
#[error("Error acquiring output surface: {0}")]
Expand Down
44 changes: 30 additions & 14 deletions crates/fj-viewer/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ use crate::{
camera::Camera,
graphics::{self, DrawConfig, Renderer},
input,
window::Window,
window::{self, Window},
};

/// Initializes a model viewer for a given model and enters its process loop.
pub fn run(
watcher: Watcher,
shape_processor: ShapeProcessor,
) -> Result<(), graphics::InitError> {
) -> Result<(), Error> {
let event_loop = EventLoop::new();
let window = Window::new(&event_loop);
let window = Window::new(&event_loop)?;

let mut previous_time = Instant::now();

Expand All @@ -47,18 +47,22 @@ pub fn run(
let now = Instant::now();

if let Some(new_shape) = watcher.receive() {
let new_shape = shape_processor.process(&new_shape).unwrap();
renderer.update_geometry(
(&new_shape.mesh).into(),
(&new_shape.debug_info).into(),
new_shape.aabb,
);

if camera.is_none() {
camera = Some(Camera::new(&new_shape.aabb));
}
match shape_processor.process(&new_shape) {
Ok(new_shape) => {
renderer.update_geometry(
(&new_shape.mesh).into(),
(&new_shape.debug_info).into(),
new_shape.aabb,
);

shape = Some(new_shape);
if camera.is_none() {
camera = Some(Camera::new(&new_shape.aabb));
}

shape = Some(new_shape);
}
Err(err) => println!("Shape processing error: {}", err),
}
}

match event {
Expand Down Expand Up @@ -155,3 +159,15 @@ pub fn run(
}
});
}

/// Error in main loop
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Error initializing window
#[error("Error initializing window")]
WindowInit(#[from] window::Error),

/// Error initializing graphics
#[error("Error initializing graphics")]
GraphicsInit(#[from] graphics::InitError),
}
12 changes: 8 additions & 4 deletions crates/fj-viewer/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ impl Window {
/// let event_loop = winit::event_loop::EventLoop::new();
/// let window = fj_viewer::window::Window::new(&event_loop);
/// ```
pub fn new(event_loop: &EventLoop<()>) -> Self {
pub fn new(event_loop: &EventLoop<()>) -> Result<Self, Error> {
let window = WindowBuilder::new()
.with_title("Fornjot")
.with_maximized(true)
.with_decorations(true)
.with_transparent(false)
.build(event_loop)
.unwrap();
.build(event_loop)?;

Self(window)
Ok(Self(window))
}

/// Returns a shared reference to the wrapped window
Expand All @@ -40,3 +39,8 @@ impl Window {
self.0.inner_size().height
}
}

/// Error initializing window
#[derive(Debug, thiserror::Error)]
#[error("Error initializing window")]
pub struct Error(#[from] pub winit::error::OsError);