Skip to content

Commit

Permalink
InoxRenderer trait. (Inochi2D#57)
Browse files Browse the repository at this point in the history
GL renderer reimplemented using this trait.

The major part of this refactor is to finally resolve the mess of
part/masks/composites being weaved together. The source of the mess is
that drawables have the distinctions of `drawOne` and `drawSelf` (using
ref impl terms) and "used or not, as a mask". The previous impl just
blindly models the OOP pattern in the ref impl with two `bool`s being
passed around. However the proper way is to factor out the commonalities
instead of functions calling each other back and forth.

Problems automatically emerged after this refactor:

0. recursive compositing just cannot happen. all checks are removed
1. find that the previous impl doesn't support simple mesh masks
2. find that the previous impl doesn't support composite node as mask
3. find that rendering to texture is not strictly enforced. in the
framework there is easy way to customize the render flow
4. the data structure can be reworked a bit. after the refactor one will
notice that some now separate `struct`s almost always appear together
  • Loading branch information
Speykious authored Aug 7, 2023
2 parents 71b0ab2 + f54dddb commit 8bd2de1
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 299 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- name: Setup aarch64
if: matrix.config.target == 'aarch64-unknown-linux-gnu'
run: |
sudo apt update
sudo apt install gcc-aarch64-linux-gnu
echo "[target.aarch64-unknown-linux-gnu]" >> ~/.cargo/config
echo "linker = \"aarch64-linux-gnu-gcc\"" >> ~/.cargo/config
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"no-inline-html": false,
"first-line-heading": false
},
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
// "rust-analyzer.cargo.target": "wasm32-unknown-unknown"
}
15 changes: 8 additions & 7 deletions examples/render-opengl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::path::PathBuf;
use std::{error::Error, fs, num::NonZeroU32};

use inox2d::formats::inp::parse_inp;
use inox2d::render::InoxRenderer;
use inox2d_opengl::OpenglRenderer;

use clap::Parser;
use glam::{uvec2, Vec2};
use glam::Vec2;
use glutin::surface::GlSurface;
use tracing::{debug, info};
use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*};
Expand Down Expand Up @@ -35,10 +36,9 @@ fn main() -> Result<(), Box<dyn Error>> {

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

info!("Setting up windowing and OpenGL");
Expand All @@ -53,14 +53,15 @@ fn main() -> Result<(), Box<dyn Error>> {

info!("Initializing Inox2D renderer");
let window_size = window.inner_size();
let viewport = uvec2(window_size.width, window_size.height);
let mut renderer = OpenglRenderer::new(gl, viewport, &puppet)?;
renderer.upload_model_textures(&model.textures)?;

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 = puppet;
let mut puppet = model.puppet;

// Event loop
events.run(move |event, _, control_flow| {
Expand Down
15 changes: 7 additions & 8 deletions examples/render-webgl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
use std::cell::RefCell;
use std::rc::Rc;

use inox2d::formats::inp::parse_inp;
use inox2d::{formats::inp::parse_inp, render::InoxRenderer};
use inox2d_opengl::OpenglRenderer;

use glam::{uvec2, Vec2};
use glam::Vec2;
use tracing::info;
use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -90,15 +90,14 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {

let model_bytes = res.bytes().await?;
let model = parse_inp(model_bytes.as_ref())?;
let puppet = model.puppet;

info!("Initializing Inox2D renderer");
let window_size = window.inner_size();
let viewport = uvec2(window_size.width, window_size.height);
let mut renderer = OpenglRenderer::new(gl, viewport, &puppet)?;
let mut renderer = OpenglRenderer::new(gl)?;

info!("Uploading model textures");
renderer.upload_model_textures(&model.textures)?;
info!("Creating buffers and uploading model textures");
renderer.prepare(&model)?;
renderer.resize(window_size.width, window_size.height);
renderer.camera.scale = Vec2::splat(0.15);
info!("Inox2D renderer initialized");

Expand All @@ -108,7 +107,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
// Winit won't help us :(
let scene_ctrl = Rc::new(RefCell::new(scene_ctrl));
let renderer = Rc::new(RefCell::new(renderer));
let puppet = Rc::new(RefCell::new(puppet));
let puppet = Rc::new(RefCell::new(model.puppet));

// Setup continuous animation loop
{
Expand Down
1 change: 0 additions & 1 deletion examples/render-webgl/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use glam::{vec2, Vec2};
use inox2d::math::camera::Camera;
use tracing::info;
use web_time::Instant;
use winit::event::{ElementState, MouseScrollDelta, WindowEvent};
use winit::window::Window;
Expand Down
7 changes: 3 additions & 4 deletions inox2d-opengl/src/gl_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub trait RenderCtxOpenglExt {
unsafe fn setup_gl_buffers(
&self,
gl: &glow::Context,
vao: glow::VertexArray,
) -> Result<glow::VertexArray, OpenglRendererError>;
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context);
}
Expand All @@ -44,14 +45,12 @@ impl RenderCtxOpenglExt for RenderCtx {
///
/// # Safety
///
/// Only call this function once (probably).
/// Only call this function once when loading a new puppet.
unsafe fn setup_gl_buffers(
&self,
gl: &glow::Context,
vao: glow::VertexArray,
) -> Result<glow::VertexArray, OpenglRendererError> {
let vao = gl
.create_vertex_array()
.map_err(OpenglRendererError::Opengl)?;
gl.bind_vertex_array(Some(vao));

upload_array_to_gl(
Expand Down
Loading

0 comments on commit 8bd2de1

Please sign in to comment.