wip: engine enhancements and fixes

This commit is contained in:
reo 2025-08-24 18:49:41 +03:00
parent 9816f14f53
commit cc7921a5fe
27 changed files with 347 additions and 32 deletions

View file

@ -1,3 +1,5 @@
use std::any::Any;
use std::cell::RefCell;
use raidillon_assets::{ModelManagerRef, ModelManager};
use crate::model::Model;
use std::path::{Path, PathBuf};
@ -5,10 +7,12 @@ use crate::gltf_loader::load_gltf;
use glium::backend::Facade;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::rc::Rc;
use raidillon_assets::model_manager::ModelID;
/// Glium platform asset manager implementation.
pub struct GliumAssetManager {
models: HashMap<PathBuf, Model>,
pub models: HashMap<PathBuf, Model>,
facade: Box<dyn Facade>,
}
@ -42,4 +46,8 @@ impl ModelManager for GliumAssetManager {
// }
// }
// }
fn get_model(&self, id: &ModelID) -> Option<&dyn Any> {
self.models.get(id).map(|model| model as &dyn Any)
}
}

View file

@ -5,6 +5,6 @@ pub mod gltf_loader;
pub mod system;
mod render;
pub use assets::GliumAssetManager;
pub use assets::{GliumAssetManager};
pub use platform::GliumPlatform;
pub use system::RenderingSystem;

View file

@ -1,3 +1,4 @@
use std::any::{Any, TypeId};
use glium::{IndexBuffer, VertexBuffer, implement_vertex};
use glium::texture::{SrgbTexture2d, Texture2d};
use glium::uniforms::SamplerBehavior;

View file

@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::{Arc, RwLock};
use raidillon_platform::Platform;
use glium::winit::event_loop::EventLoop;
@ -8,12 +9,14 @@ use glium::backend::glutin::Display;
use glium::glutin::surface::WindowSurface;
use glium::backend::glutin::SimpleWindowBuilder;
use glium::Surface;
use crate::system::{RenderingSystemManager, RenderingSystem, RenderingContext};
use crate::system::{RenderingSystemManager, RenderingSystem, RenderingContext, SystemID};
use winit::event::{Event, WindowEvent};
use raidillon_assets::{ModelManager, ModelManagerRef};
use raidillon_core::Engine;
use crate::GliumAssetManager;
use crate::render::BasicRenderingSystem;
use crate::{GliumAssetManager};
use crate::render::BasicMeshRenderingSystem;
pub const MESH_RENDERER: &str = "mesh_renderer";
pub struct GliumPlatform {
event_loop: EventLoop<()>,
@ -36,8 +39,15 @@ impl Platform for GliumPlatform {
.build(&event_loop);
let asset_manager: ModelManagerRef = Rc::new(RefCell::new(Box::new(GliumAssetManager::new(Box::new(display.clone())))));
let rendering_system_manager = RenderingSystemManager::new();
let mut rendering_system_manager = RenderingSystemManager::new();
engine.set_model_manager(asset_manager.clone());
// Install rendering systems
rendering_system_manager.add_system(
MESH_RENDERER,
Box::new(BasicMeshRenderingSystem::initialize(&display))
);
Self {
event_loop,
window,
@ -49,6 +59,7 @@ impl Platform for GliumPlatform {
}
fn run(mut self) {
self.engine.initialize();
let _ = &self.event_loop.run(move |event, el| {
match event {
Event::WindowEvent { event, .. } => match event {

View file

@ -1,11 +1,111 @@
use crate::RenderingSystem;
use std::any::Any;
use glium::{uniform, Display, Program, Surface};
use glium::glutin::surface::WindowSurface;
use glium::texture::{RawImage2d, SrgbTexture2d};
use crate::{GliumAssetManager, RenderingSystem};
use crate::system::RenderingContext;
use raidillon_assets::include_shader;
pub use raidillon_platform::Camera;
use glam::Vec3;
use glium::uniforms::{MagnifySamplerFilter, MinifySamplerFilter, SamplerWrapFunction};
use raidillon_ecs::{Transform, ModelID};
use crate::model::Model;
/// A basic renderer pipeline step.
pub struct BasicRenderingSystem;
pub struct BasicMeshRenderingSystem {
program: Program,
white_tex: SrgbTexture2d,
params: glium::DrawParameters<'static>,
}
impl RenderingSystem for BasicMeshRenderingSystem {
fn initialize(display: &Display<WindowSurface>) -> Self {
const VERT_SRC: &str = include_shader!("gl_textured.vert");
const FRAG_SRC: &str = include_shader!("gl_textured.frag");
let program = Program::from_source(display, VERT_SRC, FRAG_SRC, None).unwrap();
let white_tex = {
let data = vec![255u8, 255u8, 255u8, 255u8];
let raw = RawImage2d::from_raw_rgba(data, (1, 1));
SrgbTexture2d::new(display, raw).unwrap()
};
let params = glium::DrawParameters {
depth: glium::Depth {
test: glium::draw_parameters::DepthTest::IfLess,
write: true,
.. Default::default()
},
.. Default::default()
};
Self {
program, white_tex, params
}
}
impl RenderingSystem for BasicRenderingSystem {
fn render(&mut self, ctx: &mut RenderingContext) {
todo!()
let cam = match ctx.scene.world.query::<&Camera>().iter().next() {
Some((_, cam)) => *cam,
None => {
eprintln!("[renderer] No camera component found. Skipping frame");
return;
}
};
// Direction from the light source (0,+Y) towards the scene.
let light_dir: Vec3 = Vec3::new(0.0, -1.0, 0.0).normalize();
// let asset_manager = ctx.asset_manager.borrow();
// let any_ref: &dyn Any = &**asset_manager;
// if let Some(glium_manager) = any_ref.downcast_ref::<GliumAssetManager>() {
// &glium_manager.models;
// }
let asset_manager = ctx.asset_manager.borrow();
for (_, (tr, mh)) in ctx.scene.world.query::<(&Transform, &ModelID)>().iter() {
let model = match asset_manager.get_model(mh) {
Some(model) => model,
_ => continue,
};
let model = match model.downcast_ref::<Model>() {
Some(model) => model,
None => continue,
};
let mesh = &model.mesh;
let mat = &model.material;
let tex_ref: &SrgbTexture2d = mat.base_color.as_ref().unwrap_or(&self.white_tex);
let mut sampler = tex_ref.sampled();
sampler = sampler.wrap_function(SamplerWrapFunction::Repeat);
sampler = sampler.minify_filter(MinifySamplerFilter::Linear);
sampler = sampler.magnify_filter(MagnifySamplerFilter::Linear);
let c = mat.base_color_factor;
let uniforms = uniform! {
model: tr.matrix().to_cols_array_2d(),
view: cam.view().to_cols_array_2d(),
projection: cam.projection().to_cols_array_2d(),
u_light: [light_dir.x, light_dir.y, light_dir.z],
tex: sampler,
color: [c[0], c[1], c[2]],
uv_offset: [mat.uv_offset.x, mat.uv_offset.y],
uv_scale: [mat.uv_scale.x, mat.uv_scale.y],
};
ctx.target.draw(
&mesh.vbuf,
&mesh.ibuf,
&self.program,
&uniforms,
&self.params,
).unwrap();
}
}
}

View file

@ -1,2 +1,2 @@
mod basic;
pub use basic::BasicRenderingSystem;
pub use basic::BasicMeshRenderingSystem;

View file

@ -1,8 +1,11 @@
use std::cell::RefCell;
use std::rc::Rc;
use raidillon_core::Scene;
use glium::Frame;
use crate::GliumAssetManager;
use glium::{Display, Frame};
use glium::glutin::surface::WindowSurface;
use indexmap::IndexMap;
use raidillon_assets::ModelManagerRef;
use crate::GliumAssetManager;
pub struct RenderingContext<'a> {
pub scene: &'a Scene,
@ -14,10 +17,10 @@ pub struct RenderingContext<'a> {
/// This is unrelated to the main System trait in core.
pub trait RenderingSystem {
fn render(&mut self, ctx: &mut RenderingContext);
fn compile_shaders(&mut self) {}
fn initialize(display: &Display<WindowSurface>) -> Self where Self: Sized;
}
type SystemID = String;
pub type SystemID = &'static str;
pub struct RenderingSystemManager {
pub systems: IndexMap<SystemID, Box<dyn RenderingSystem>>,