Skip to content

Commit

Permalink
Merge branch 'main' into js-garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
Speykious authored Dec 24, 2023
2 parents 1b57a1d + 626f0d3 commit 84dcd32
Show file tree
Hide file tree
Showing 41 changed files with 4,950 additions and 5,224 deletions.
2 changes: 1 addition & 1 deletion examples/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ edition = "2021"
[dependencies]
glam = "0.24.0"
inox2d = { path = "../../inox2d" }
winit = "0.28.2"
winit = "0.29"
135 changes: 67 additions & 68 deletions examples/common/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,84 @@ use winit::event::{ElementState, MouseScrollDelta, WindowEvent};
use winit::window::Window;

pub struct ExampleSceneController {
// for camera position and mouse interactions
camera_pos: Vec2,
mouse_pos: Vec2,
mouse_pos_held: Vec2,
mouse_state: ElementState,
// for camera position and mouse interactions
camera_pos: Vec2,
mouse_pos: Vec2,
mouse_pos_held: Vec2,
mouse_state: ElementState,

// for smooth scrolling
pub scroll_speed: f32,
hard_scale: Vec2,
// for smooth scrolling
pub scroll_speed: f32,
hard_scale: Vec2,

// for FPS-independent interactions
start: Instant,
prev_elapsed: f32,
current_elapsed: f32,
// for FPS-independent interactions
start: Instant,
prev_elapsed: f32,
current_elapsed: f32,
}

impl ExampleSceneController {
pub fn new(camera: &Camera, scroll_speed: f32) -> Self {
Self {
camera_pos: camera.position,
mouse_pos: Vec2::default(),
mouse_pos_held: Vec2::default(),
mouse_state: ElementState::Released,
scroll_speed,
hard_scale: camera.scale,
start: Instant::now(),
prev_elapsed: 0.0,
current_elapsed: 0.0,
}
}
pub fn new(camera: &Camera, scroll_speed: f32) -> Self {
Self {
camera_pos: camera.position,
mouse_pos: Vec2::default(),
mouse_pos_held: Vec2::default(),
mouse_state: ElementState::Released,
scroll_speed,
hard_scale: camera.scale,
start: Instant::now(),
prev_elapsed: 0.0,
current_elapsed: 0.0,
}
}

pub fn update(&mut self, camera: &mut Camera) {
// Smooth scrolling
let time_delta = self.current_elapsed - self.prev_elapsed;
camera.scale = camera.scale + time_delta.powf(0.6) * (self.hard_scale - camera.scale);
pub fn update(&mut self, camera: &mut Camera) {
// Smooth scrolling
let time_delta = self.current_elapsed - self.prev_elapsed;
camera.scale = camera.scale + time_delta.powf(0.6) * (self.hard_scale - camera.scale);

// Mouse dragging
if self.mouse_state == ElementState::Pressed {
camera.position =
self.camera_pos + (self.mouse_pos - self.mouse_pos_held) / camera.scale;
}
// Mouse dragging
if self.mouse_state == ElementState::Pressed {
camera.position = self.camera_pos + (self.mouse_pos - self.mouse_pos_held) / camera.scale;
}

// Frame interval
self.prev_elapsed = self.current_elapsed;
self.current_elapsed = self.start.elapsed().as_secs_f32();
}
// Frame interval
self.prev_elapsed = self.current_elapsed;
self.current_elapsed = self.start.elapsed().as_secs_f32();
}

pub fn interact(&mut self, window: &Window, event: &WindowEvent, camera: &Camera) {
match event {
WindowEvent::CursorMoved { position, .. } => {
self.mouse_pos = vec2(position.x as f32, position.y as f32);
pub fn interact(&mut self, window: &Window, event: &WindowEvent, camera: &Camera) {
match event {
WindowEvent::CursorMoved { position, .. } => {
self.mouse_pos = vec2(position.x as f32, position.y as f32);

if self.mouse_state == ElementState::Pressed {
window.request_redraw();
}
}
WindowEvent::MouseInput { state, .. } => {
self.mouse_state = *state;
if self.mouse_state == ElementState::Pressed {
self.mouse_pos_held = self.mouse_pos;
self.camera_pos = camera.position;
}
}
WindowEvent::MouseWheel { delta, .. } => {
// Handle mouse wheel (zoom)
let my = match delta {
MouseScrollDelta::LineDelta(_, y) => *y * 12.,
MouseScrollDelta::PixelDelta(pos) => pos.y as f32,
};
if self.mouse_state == ElementState::Pressed {
window.request_redraw();
}
}
WindowEvent::MouseInput { state, .. } => {
self.mouse_state = *state;
if self.mouse_state == ElementState::Pressed {
self.mouse_pos_held = self.mouse_pos;
self.camera_pos = camera.position;
}
}
WindowEvent::MouseWheel { delta, .. } => {
// Handle mouse wheel (zoom)
let my = match delta {
MouseScrollDelta::LineDelta(_, y) => *y * 12.,
MouseScrollDelta::PixelDelta(pos) => pos.y as f32,
};

self.hard_scale *= 2_f32.powf(self.scroll_speed * my * 0.1);
self.hard_scale *= 2_f32.powf(self.scroll_speed * my * 0.1);

window.request_redraw();
}
_ => (),
}
}
window.request_redraw();
}
_ => (),
}
}

pub fn current_elapsed(&self) -> f32 {
self.current_elapsed
}
pub fn current_elapsed(&self) -> f32 {
self.current_elapsed
}
}
6 changes: 3 additions & 3 deletions examples/render-opengl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ inox2d-opengl = { path = "../../inox2d-opengl" }
clap = { version = "4.1.8", features = ["derive"] }
glam = { version = "0.24.0", features = ["bytemuck"] }
glow = { version = "0.12.1" }
glutin = "0.30.6"
glutin-winit = "0.3.0"
glutin = "0.31.2"
glutin-winit = "0.4.2"
raw-window-handle = "0.5.1"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
winit = "0.28.2"
winit = "0.29"
203 changes: 105 additions & 98 deletions examples/render-opengl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,114 +10,121 @@ use glam::Vec2;
use glutin::surface::GlSurface;
use tracing::{debug, info};
use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};

use winit::event::{ElementState, Event, KeyEvent, WindowEvent};

use common::scene::ExampleSceneController;
use opengl::{launch_opengl_window, App};
use winit::event_loop::ControlFlow;
use winit::keyboard::{KeyCode, PhysicalKey};

mod opengl;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(help = "Path to the .inp file. .inx files don't work!")]
inp_path: PathBuf,
#[arg(help = "Path to the .inp file. .inx files don't work!")]
inp_path: PathBuf,
}

fn main() -> Result<(), Box<dyn Error>> {
let cli = Cli::parse();

tracing_subscriber::registry()
.with(fmt::layer())
.with(LevelFilter::INFO)
.init();

info!("Parsing puppet");

let data = fs::read(cli.inp_path).unwrap();
let model = parse_inp(data.as_slice()).unwrap();
info!(
"Successfully parsed puppet: {}",
(model.puppet.meta.name.as_deref()).unwrap_or("<no puppet name specified in file>")
);

info!("Setting up windowing and OpenGL");
let App {
gl,
gl_ctx,
gl_surface,
gl_display,
events,
window,
} = launch_opengl_window()?;

info!("Initializing Inox2D renderer");
let window_size = window.inner_size();

let mut renderer = OpenglRenderer::new(gl)?;
renderer.prepare(&model)?;
renderer.resize(window_size.width, window_size.height);
renderer.camera.scale = Vec2::splat(0.15);
info!("Inox2D renderer initialized");

let mut scene_ctrl = ExampleSceneController::new(&renderer.camera, 0.5);
let mut puppet = model.puppet;

// Event loop
events.run(move |event, _, control_flow| {
// They need to be present
let _gl_display = &gl_display;
let _window = &window;

control_flow.set_wait();

match event {
Event::RedrawRequested(_) => {
debug!("Redrawing");
scene_ctrl.update(&mut renderer.camera);

renderer.clear();

puppet.begin_set_params();
let t = scene_ctrl.current_elapsed();
puppet.set_param("Head:: Yaw-Pitch", Vec2::new(t.cos(), t.sin()));
puppet.end_set_params();

renderer.render(&puppet);

gl_surface.swap_buffers(&gl_ctx).unwrap();
window.request_redraw();
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(physical_size) => {
// Handle window resizing
renderer.resize(physical_size.width, physical_size.height);
gl_surface.resize(
&gl_ctx,
NonZeroU32::new(physical_size.width).unwrap(),
NonZeroU32::new(physical_size.height).unwrap(),
);
window.request_redraw();
}
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
..
},
..
} => {
info!("There is an Escape D:");
control_flow.set_exit();
}
_ => scene_ctrl.interact(&window, event, &renderer.camera),
},
Event::MainEventsCleared => {
window.request_redraw();
}
_ => (),
}
})
let cli = Cli::parse();

tracing_subscriber::registry()
.with(fmt::layer())
.with(LevelFilter::INFO)
.init();

info!("Parsing puppet");

let data = fs::read(cli.inp_path).unwrap();
let model = parse_inp(data.as_slice()).unwrap();
info!(
"Successfully parsed puppet: {}",
(model.puppet.meta.name.as_deref()).unwrap_or("<no puppet name specified in file>")
);

info!("Setting up windowing and OpenGL");
let App {
gl,
gl_ctx,
gl_surface,
gl_display,
events,
window,
} = launch_opengl_window()?;

info!("Initializing Inox2D renderer");
let window_size = window.inner_size();

let mut renderer = OpenglRenderer::new(gl)?;
renderer.prepare(&model)?;
renderer.resize(window_size.width, window_size.height);
renderer.camera.scale = Vec2::splat(0.15);
info!("Inox2D renderer initialized");

let mut scene_ctrl = ExampleSceneController::new(&renderer.camera, 0.5);
let mut puppet = model.puppet;

// Event loop
events.run(move |event, elwt| {
// They need to be present
let _gl_display = &gl_display;
let _window = &window;
elwt.set_control_flow(ControlFlow::Wait);

match event {
Event::WindowEvent {
window_id: _,
event: winit::event::WindowEvent::RedrawRequested,
} => {
debug!("Redrawing");
scene_ctrl.update(&mut renderer.camera);

renderer.clear();

puppet.begin_set_params();
let t = scene_ctrl.current_elapsed();
puppet.set_param("Head:: Yaw-Pitch", Vec2::new(t.cos(), t.sin()));
puppet.end_set_params();

renderer.render(&puppet);

gl_surface.swap_buffers(&gl_ctx).unwrap();
window.request_redraw();
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(physical_size) => {
// Handle window resizing
renderer.resize(physical_size.width, physical_size.height);
gl_surface.resize(
&gl_ctx,
NonZeroU32::new(physical_size.width).unwrap(),
NonZeroU32::new(physical_size.height).unwrap(),
);
window.request_redraw();
}
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
//virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Escape),
..
},
..
} => {
info!("There is an Escape D:");
elwt.exit();
}
_ => scene_ctrl.interact(&window, event, &renderer.camera),
},
Event::AboutToWait => {
window.request_redraw();
}
_ => (),
}
})?;
Ok(())
}
Loading

0 comments on commit 84dcd32

Please sign in to comment.