Skip to content

Commit

Permalink
Merry holidays to those who celebrate (#75)
Browse files Browse the repository at this point in the history
Merry Christmas :D
  • Loading branch information
Speykious authored Dec 24, 2023
2 parents 24492b0 + f58c88c commit 626f0d3
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 87 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"
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"
31 changes: 19 additions & 12 deletions examples/render-opengl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ 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;

Expand Down Expand Up @@ -64,15 +67,17 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut puppet = model.puppet;

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

control_flow.set_wait();
elwt.set_control_flow(ControlFlow::Wait);

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

Expand All @@ -99,25 +104,27 @@ fn main() -> Result<(), Box<dyn Error>> {
);
window.request_redraw();
}
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
event:
KeyEvent {
//virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Escape),
..
},
..
} => {
info!("There is an Escape D:");
control_flow.set_exit();
elwt.exit();
}
_ => scene_ctrl.interact(&window, event, &renderer.camera),
},
Event::MainEventsCleared => {
Event::AboutToWait => {
window.request_redraw();
}
_ => (),
}
})
})?;
Ok(())
}
10 changes: 5 additions & 5 deletions examples/render-opengl/src/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::ffi::CString;
use std::num::NonZeroU32;

use glow::HasContext;
use glutin::context::{ContextApi, ContextAttributesBuilder, PossiblyCurrentContext, Version};
use glutin::context::{ContextApi, ContextAttributesBuilder, NotCurrentGlContext, PossiblyCurrentContext, Version};
use glutin::display::{Display, GetGlDisplay};
use glutin::prelude::{GlConfig, GlDisplay, NotCurrentGlContextSurfaceAccessor};
use glutin::prelude::{GlConfig, GlDisplay};
use glutin::surface::{Surface, SurfaceAttributesBuilder, WindowSurface};
use glutin_winit::ApiPrefence;
use glutin_winit::ApiPreference;
use raw_window_handle::HasRawWindowHandle;
use tracing::{debug, error, info, warn};
use winit::event_loop::EventLoop;
Expand All @@ -31,7 +31,7 @@ pub fn launch_opengl_window() -> Result<App, Box<dyn Error>> {
}
}

let events = winit::event_loop::EventLoop::new();
let events = winit::event_loop::EventLoop::new().unwrap();

let window_builder = winit::window::WindowBuilder::new()
.with_transparent(true)
Expand All @@ -40,7 +40,7 @@ pub fn launch_opengl_window() -> Result<App, Box<dyn Error>> {
.with_title("Render Inochi2D Puppet (OpenGL)");

let (window, gl_config) = glutin_winit::DisplayBuilder::new()
.with_preference(ApiPrefence::FallbackEgl)
.with_preference(ApiPreference::FallbackEgl)
.with_window_builder(Some(window_builder))
.build(&events, <_>::default(), |configs| {
configs
Expand Down
2 changes: 1 addition & 1 deletion examples/render-webgl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tracing-wasm = "0.2.1"
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.37"
web-time = "0.2.0"
winit = "0.28.2"
winit = "0.29"

[dependencies.web-sys]
version = "0.3.61"
Expand Down
16 changes: 8 additions & 8 deletions examples/render-webgl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn create_window(event: &winit::event_loop::EventLoop<()>) -> Result<winit::wind
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
let canvas = web_sys::Element::from(window.canvas());
let canvas = web_sys::Element::from(window.canvas().unwrap());
canvas.set_id("canvas");
body.append_child(&canvas).ok()
})
Expand Down Expand Up @@ -55,7 +55,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {

use crate::scene::WasmSceneController;

let events = winit::event_loop::EventLoop::new();
let events = winit::event_loop::EventLoop::new().unwrap();
let window = create_window(&events)?;

// Make sure the context has a stencil buffer
Expand Down Expand Up @@ -134,11 +134,10 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
}

// Event loop
events.run(move |event, _, control_flow| {
events.run(move |event, elwt| {
// it needs to be present
let _window = &window;

control_flow.set_wait();
elwt.set_control_flow(winit::event_loop::ControlFlow::Wait);

match event {
Event::WindowEvent { ref event, .. } => match event {
Expand All @@ -147,17 +146,18 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
renderer.borrow_mut().resize(physical_size.width, physical_size.height);
window.request_redraw();
}
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CloseRequested => elwt.exit(),
_ => scene_ctrl
.borrow_mut()
.interact(&window, event, &renderer.borrow().camera),
},
Event::MainEventsCleared => {
Event::AboutToWait => {
window.request_redraw();
}
_ => (),
}
})
})?;
Ok(())
}

#[cfg(target_arch = "wasm32")]
Expand Down
4 changes: 2 additions & 2 deletions examples/render-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pollster = "0.3.0"
raw-window-handle = "0.5.1"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
winit = "0.28.2"
wgpu = "0.17.0"
winit = { version = "0.29", features = ["rwh_05"] }
wgpu = "0.18"
103 changes: 56 additions & 47 deletions examples/render-wgpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use glam::{uvec2, vec2, Vec2};
use wgpu::CompositeAlphaMode;
use winit::{
event::*,
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::{KeyCode, PhysicalKey},
window::WindowBuilder,
};

Expand All @@ -16,7 +17,7 @@ use std::path::PathBuf;
use clap::Parser;

pub async fn run(model: Model) {
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_inner_size(winit::dpi::PhysicalSize::new(800, 800))
.with_resizable(true)
Expand Down Expand Up @@ -79,53 +80,61 @@ pub async fn run(model: Model) {
let mut scene_ctrl = ExampleSceneController::new(&renderer.camera, 0.5);
let mut puppet = model.puppet;

event_loop.run(move |event, _, control_flow| match event {
Event::RedrawRequested(_) => {
scene_ctrl.update(&mut renderer.camera);

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

let output = surface.get_current_texture().unwrap();
let view = (output.texture).create_view(&wgpu::TextureViewDescriptor::default());

renderer.render(&queue, &device, &puppet, &view);
output.present();
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => *control_flow = ControlFlow::Exit,
WindowEvent::Resized(size) => {
// Reconfigure the surface with the new size
config.width = size.width;
config.height = size.height;
surface.configure(&device, &config);

// Update the renderer's internal viewport
renderer.resize(uvec2(size.width, size.height));

// On macos the window needs to be redrawn manually after resizing
event_loop
.run(move |event, elwt| match event {
Event::WindowEvent {
window_id: _,
event: WindowEvent::RedrawRequested,
} => {
scene_ctrl.update(&mut renderer.camera);

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

let output = surface.get_current_texture().unwrap();
let view = (output.texture).create_view(&wgpu::TextureViewDescriptor::default());

renderer.render(&queue, &device, &puppet, &view);
output.present();
}
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
//virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Escape),
..
},
..
} => {
elwt.exit();
}
WindowEvent::Resized(size) => {
// Reconfigure the surface with the new size
config.width = size.width;
config.height = size.height;
surface.configure(&device, &config);

// Update the renderer's internal viewport
renderer.resize(uvec2(size.width, size.height));

// On macos the window needs to be redrawn manually after resizing
window.request_redraw();
}
_ => scene_ctrl.interact(&window, event, &renderer.camera),
},
Event::AboutToWait => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
}
_ => scene_ctrl.interact(&window, event, &renderer.camera),
},
Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
}
_ => {}
});
_ => {}
})
.expect("Error in event loop")
}

#[derive(Parser, Debug)]
Expand Down
5 changes: 2 additions & 3 deletions inox2d-opengl/src/gl_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use inox2d::render::RenderCtx;

use super::OpenglRendererError;

unsafe fn upload_array_to_gl<T>(gl: &glow::Context, array: &Vec<T>, target: u32, usage: u32) {
let bytes: &[u8] =
core::slice::from_raw_parts(array.as_ptr() as *const u8, array.len() * core::mem::size_of::<T>());
unsafe fn upload_array_to_gl<T>(gl: &glow::Context, array: &[T], target: u32, usage: u32) {
let bytes: &[u8] = core::slice::from_raw_parts(array.as_ptr() as *const u8, std::mem::size_of_val(array));
let buffer = gl.create_buffer().unwrap();
gl.bind_buffer(target, Some(buffer));
gl.buffer_data_u8_slice(target, bytes, usage);
Expand Down
2 changes: 1 addition & 1 deletion inox2d-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ encase = { version = "0.6.1", features = ["glam"] }
glam = { version = "0.24.0", features = ["bytemuck"] }
thiserror = "1.0.39"
tracing = "0.1.37"
wgpu = { version = "0.17.0", features = ["webgl"] }
wgpu = { version = "0.18", features = ["webgl"] }
16 changes: 13 additions & 3 deletions inox2d-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,21 @@ impl Renderer {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations { load: op, store: true },
ops: wgpu::Operations {
load: op,
store: StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: mask_view,
depth_ops: None,
stencil_ops: Some(Operations {
load: wgpu::LoadOp::Clear(u32::from(!masks.is_empty())),
store: true,
store: StoreOp::Store,
}),
}),
timestamp_writes: None, // todo!(),
occlusion_query_set: None, // todo!(),
});

render_pass.set_vertex_buffer(0, self.buffers.vertex_buffer.slice(..));
Expand Down Expand Up @@ -236,13 +241,18 @@ impl Renderer {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
resolve_target: None,
ops: wgpu::Operations { load: op, store: true },
ops: wgpu::Operations {
load: op,
store: StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: mask_view,
depth_ops: None,
stencil_ops: None,
}),
timestamp_writes: None, // todo!(),
occlusion_query_set: None, //todo!(),
});

render_pass.set_vertex_buffer(0, self.buffers.vertex_buffer.slice(..));
Expand Down
2 changes: 1 addition & 1 deletion inox2d/src/formats/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn deserialize_part(obj: &JsonObject) -> InoxParseResult<Part> {
let (tex_albedo, tex_emissive, tex_bumpmap) = {
let textures = obj.get_list("textures")?;

let tex_albedo = match textures.get(0).ok_or(InoxParseError::NoAlbedoTexture)?.as_number() {
let tex_albedo = match textures.first().ok_or(InoxParseError::NoAlbedoTexture)?.as_number() {
Some(val) => val
.try_into()
.map_err(|_| InoxParseError::JsonError(JsonError::ParseIntError("0".to_owned()).nested("textures")))?,
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
max_width = 120
hard_tabs = true
edition = "2021"

0 comments on commit 626f0d3

Please sign in to comment.