Skip to content

Commit

Permalink
Merge pull request #640 from hannobraun/window
Browse files Browse the repository at this point in the history
Extract `fj-window` from `fj-viewer`
  • Loading branch information
hannobraun authored May 26, 2022
2 parents e9282eb + 2968580 commit 10dd253
Show file tree
Hide file tree
Showing 18 changed files with 343 additions and 182 deletions.
17 changes: 14 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"crates/fj-math",
"crates/fj-operations",
"crates/fj-viewer",
"crates/fj-window",

"models/cuboid",
"models/spacer",
Expand All @@ -28,4 +29,5 @@ default-members = [
"crates/fj-math",
"crates/fj-operations",
"crates/fj-viewer",
"crates/fj-window",
]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Here's an overview over all of the crates, with a short description of what they
- `fj-export`: Exports Fornjot models to external data formats.
- `fj-host`: Loads Fornjot models and watches them for changes.
- `fj-viewer`: Displays Fornjot models.
- `fj-window`: Embed `fj-viewer` in a Winit-based window.
- `fj-app`: The Fornjot CAD application.
- `fj`: End-user API for defining Fornjot models.

Expand Down
4 changes: 4 additions & 0 deletions crates/fj-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ path = "../fj-operations"
version = "0.6.0"
path = "../fj-viewer"

[dependencies.fj-window]
version = "0.6.0"
path = "../fj-window"

[dependencies.serde]
version = "1.0.137"
features = ["derive"]
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::{anyhow, Context as _};
use fj_export::export;
use fj_host::{Model, Parameters};
use fj_operations::shape_processor::ShapeProcessor;
use fj_viewer::run::run;
use fj_window::run::run;
use tracing_subscriber::fmt::format;
use tracing_subscriber::EnvFilter;

Expand Down
11 changes: 1 addition & 10 deletions crates/fj-viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ categories = ["encoding", "mathematics", "rendering"]

[dependencies]
bytemuck = "1.9.1"
futures = "0.3.21"
raw-window-handle = "0.4.3"
thiserror = "1.0.31"
tracing = "0.1.34"
wgpu = "0.12.0"
wgpu_glyph = "0.16.0"
winit = "0.26.1"

[dependencies.fj-host]
version = "0.6.0"
path = "../fj-host"

[dependencies.fj-interop]
version = "0.6.0"
Expand All @@ -32,7 +27,3 @@ path = "../fj-interop"
[dependencies.fj-math]
version = "0.6.0"
path = "../fj-math"

[dependencies.fj-operations]
version = "0.6.0"
path = "../fj-operations"
16 changes: 7 additions & 9 deletions crates/fj-viewer/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::f64::consts::FRAC_PI_2;

use fj_interop::mesh::Mesh;
use fj_math::{Aabb, Point, Scalar, Transform, Triangle, Vector};
use winit::dpi::PhysicalPosition;

use crate::window::Window;
use crate::screen::{Position, Size};

/// The camera abstraction
///
Expand Down Expand Up @@ -108,11 +107,10 @@ impl Camera {
/// Transform the position of the cursor on the near plane to model space.
pub fn cursor_to_model_space(
&self,
cursor: PhysicalPosition<f64>,
window: &Window,
cursor: Position,
size: Size,
) -> Point<3> {
let width = window.width() as f64;
let height = window.height() as f64;
let [width, height] = size.as_f64();
let aspect_ratio = width / height;

// Cursor position in normalized coordinates (-1 to +1) with
Expand All @@ -131,8 +129,8 @@ impl Camera {
/// Compute the point on the model, that the cursor currently points to.
pub fn focus_point(
&self,
window: &Window,
cursor: Option<PhysicalPosition<f64>>,
size: Size,
cursor: Option<Position>,
mesh: &Mesh<fj_math::Point<3>>,
) -> FocusPoint {
let cursor = match cursor {
Expand All @@ -142,7 +140,7 @@ impl Camera {

// Transform camera and cursor positions to model space.
let origin = self.position();
let cursor = self.cursor_to_model_space(cursor, window);
let cursor = self.cursor_to_model_space(cursor, size);
let dir = (cursor - origin).normalize();

let mut min_t = None;
Expand Down
32 changes: 10 additions & 22 deletions crates/fj-viewer/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use thiserror::Error;
use tracing::debug;
use wgpu::util::DeviceExt as _;
use wgpu_glyph::ab_glyph::InvalidFont;
use winit::dpi::PhysicalSize;

use crate::{camera::Camera, window::Window};
use crate::{
camera::Camera,
screen::{Screen, Size},
};

use super::{
config_ui::ConfigUi, draw_config::DrawConfig, drawables::Drawables,
Expand Down Expand Up @@ -36,26 +38,11 @@ pub struct Renderer {

impl Renderer {
/// Returns a new `Renderer`.
///
/// # Arguments
/// - `window` - a `crate::window::Window` with a surface to render onto.
///
/// # Examples
/// ```rust no_run
/// use fj_viewer::{graphics, window};
///
/// // Create window
/// let event_loop = winit::event_loop::EventLoop::new();
/// let window = window::Window::new(&event_loop).unwrap();
///
/// // Attach renderer to the window
/// let mut renderer = graphics::Renderer::new(&window);
/// ```
pub async fn new(window: &Window) -> Result<Self, InitError> {
pub async fn new(screen: &impl Screen) -> Result<Self, InitError> {
let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY);

// This is sound, as `window` is an object to create a surface upon.
let surface = unsafe { instance.create_surface(window.inner()) };
let surface = unsafe { instance.create_surface(screen.window()) };

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand Down Expand Up @@ -87,11 +74,12 @@ impl Renderer {
.get_preferred_format(&adapter)
.expect("Error determining preferred color format");

let Size { width, height } = screen.size();
let surface_config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: color_format,
width: window.width(),
height: window.height(),
width,
height,
present_mode: wgpu::PresentMode::Mailbox,
};
surface.configure(&device, &surface_config);
Expand Down Expand Up @@ -182,7 +170,7 @@ impl Renderer {
///
/// # Arguments
/// - `size`: The target size for the render surface.
pub fn handle_resize(&mut self, size: PhysicalSize<u32>) {
pub fn handle_resize(&mut self, size: Size) {
self.surface_config.width = size.width;
self.surface_config.height = size.height;

Expand Down
43 changes: 43 additions & 0 deletions crates/fj-viewer/src/input/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::screen::Position;

/// An input event
pub enum Event {
/// The cursor has moved to another position
CursorMoved(Position),

/// A key has been pressed or released
Key(Key, KeyState),

/// The user scrolled
Scroll(f64),
}

/// A keyboard or mouse key
pub enum Key {
/// The escape key
Escape,

/// The numerical key `1`
Key1,

/// The numerical key `2`
Key2,

/// The numerical key `3`
Key3,

/// The left mouse key
MouseLeft,

/// The right mouse key
MouseRight,
}

/// Defines the meaning of a key event
pub enum KeyState {
/// A key was pressed
Pressed,

/// A key was released
Released,
}
Loading

0 comments on commit 10dd253

Please sign in to comment.