globject_rs/
meshset.rs

1
2use crate::prelude::*;
3use std::{
4	collections::BTreeMap,
5	rc::Rc,
6};
7
8/// Mesh set, each mesh has its name and material.
9#[derive(Debug, Clone)]
10pub struct Meshset {
11	pub subsets: BTreeMap<String, Rc<dyn GenericMeshWithMaterial>>,
12}
13
14/// Pipeline set, converted from the mesh set, for batch drawing.
15#[derive(Debug, Clone)]
16pub struct Pipelineset<V: VertexType, I: VertexType> {
17	pub subsets: BTreeMap<String, Vec<Rc<Pipeline<V, I>>>>,
18}
19
20impl<V: VertexType, I: VertexType> Pipelineset<V, I> {
21	/// Create a pipeline set from the mesh set with shaders.
22	pub fn from_meshset(glcore: Rc<GLCore>, meshset: Meshset, shaders: &[Rc<Shader>]) -> Result<Self, PipelineError> {
23		let mut subsets = BTreeMap::new();
24		for (name, mesh) in meshset.subsets.iter() {
25			let mut v = Vec::with_capacity(shaders.len());
26			for shader in shaders.iter() {
27				v.push(Rc::new(Pipeline::new(glcore.clone(), mesh.clone(), shader.clone())?));
28			}
29			subsets.insert(name.clone(), v);
30		}
31		Ok(Self {
32			subsets
33		})
34	}
35
36	/// Draw the pipeline set to a framebuffer
37	pub fn draw(&self, fbo: Option<&Framebuffer>) -> Result<(), PipelineError> {
38		for (_name, pipelines) in self.subsets.iter() {
39			for pipeline in pipelines.iter() {
40				let bind = pipeline.bind()?;
41				bind.draw(fbo)?;
42				bind.unbind();
43			}
44		}
45		Ok(())
46	}
47}