mod3d_base/
traits.rs

1use crate::{BufferData, BufferDataAccessor, BufferIndexAccessor, Texture, VertexAttr, Vertices};
2use crate::{BufferDescriptor, MaterialAspect, MaterialBaseData, ShortIndex};
3
4//a BufferClient
5//tt BufferClient
6/// Trait supported by a BufferData client
7///
8/// A buffer client is created first by a buffer as 'none'
9///
10/// The data may be created more than once with the same buffer; the client
11/// is responsible for deduplication within the render context if required
12pub trait BufferClient:
13    Sized + std::fmt::Display + std::fmt::Debug + std::default::Default + Clone
14{
15}
16
17//tt AccessorClient
18/// Trait supported by a BufferAccessor client
19///
20/// A buffer client is created first by a buffer as 'none'
21///
22/// Before a view is creataed the data will be created at least once
23///
24/// The data may be created more than once with the same buffer; the client
25/// is responsible for dedupliclation within the render context if required
26pub trait AccessorClient:
27    Sized + std::fmt::Display + std::fmt::Debug + std::default::Default + Clone
28{
29}
30
31//tt DescriptorClient
32/// Trait supported by a BufferDescriptor client
33///
34/// A buffer descriptor client is created first by a buffer as 'none'
35///
36/// Before a descriptor is created the data will be created at least once
37///
38/// The data may be created more than once with the same descriptor; the client
39/// is responsible for dedupliclation within the render context if required
40pub trait DescriptorClient:
41    Sized + std::fmt::Display + std::fmt::Debug + std::default::Default + Clone
42{
43}
44
45//tt TextureClient
46/// The trait that must be supported by a client texture
47///
48/// Default is required as the client is made when a texture is made
49/// Clone is required as the client is textures are cloned
50pub trait TextureClient: Sized + std::fmt::Debug + std::default::Default + Clone {}
51
52//tt MaterialClient
53/// Trait supported by a material client
54///
55/// Default is not required as materials are only created in response
56/// to a crate::Material
57pub trait MaterialClient: Sized + std::fmt::Display + std::fmt::Debug {}
58
59//tt VerticesClient
60/// The trait that must be supported by a client vertices
61///
62/// Clone is required as Vertices can be borrowed by more than one object, and an
63/// instantiable object contains the [VerticesClient] for the Vertices
64///
65pub trait VerticesClient: Sized + std::fmt::Debug + std::default::Default + Clone {}
66
67//tt Renderable
68/// The [Renderable] trait must be implemented by a type that is a
69/// client of the 3D model system. It provides associated types for a
70/// renderable context (this might be a particular shader program
71/// within a OpenGL context, for example), and then its own structures
72/// that are used to hold [BufferData], textures, materials, and sets
73/// of renderable [Vertices].
74pub trait Renderable: Sized {
75    /// The renderer's type that reflects a [BufferData]
76    type Buffer: BufferClient;
77    /// The renderer's type that reflects a [BufferDataAccessor]
78    type DataAccessor: AccessorClient;
79    /// The renderer's type that reflects a [BufferIndexAccessor]
80    type IndexAccessor: AccessorClient;
81    /// The renderer's type that reflects a [BufferDescriptor]
82    type Descriptor: DescriptorClient;
83    /// The renderer's type that represents a texture; this is
84    /// supplied to material creation, and hence is less a product of
85    /// the renderer and more an input to the 3D model library
86    type Texture: TextureClient;
87    /// The renderer's type that reflects a [Material]; this is expected
88    /// to be an extraction of the aspects of a material that the
89    /// renderer pipelines can apply.
90    type Material: MaterialClient;
91    /// The renderer's type that reflects a [BufferAccessor] of indices
92    /// and the associated [BufferAccessor]s of attributes supported by a
93    /// particular pipeline within the renderer
94    type Vertices: VerticesClient;
95    /// Initialize a buffer descriptor client - it will have been created using default()
96    fn init_buffer_desc_client(
97        &mut self,
98        client: &mut Self::Descriptor,
99        buffer_desc: &BufferDescriptor<Self>,
100    );
101    // type Instantiable : ;
102    /// Initialize a buffer data client - it will have been created using default()
103    fn init_buffer_data_client(
104        &mut self,
105        client: &mut Self::Buffer,
106        buffer_data: &BufferData<Self>,
107    );
108    /// Initialize the client of an index accessor of a buffer data
109    fn init_index_accessor_client(
110        &mut self,
111        client: &mut Self::IndexAccessor,
112        buffer_view: &BufferIndexAccessor<Self>,
113    );
114    /// Initialize the client of a data accessor of a buffer data
115    fn init_buffer_view_client(
116        &mut self,
117        client: &mut Self::DataAccessor,
118        buffer_view: &BufferDataAccessor<Self>,
119        attr: VertexAttr,
120    );
121    /// Create a client
122    fn create_vertices_client(&mut self, vertices: &Vertices<Self>) -> Self::Vertices;
123    /// Create a client
124    fn create_texture_client(&mut self, texture: &Texture<Self>) -> Self::Texture;
125    /// Create a client
126    fn create_material_client<M>(
127        &mut self,
128        object: &crate::Object<M, Self>,
129        material: &M,
130    ) -> Self::Material
131    where
132        M: Material;
133
134    /// Create a client for a reason - reason 0 is reserved
135    /// Can we lose this?
136    fn init_material_client<M: Material>(&mut self, client: &mut Self::Material, material: &M);
137    // Destroy a client given a reason - reason 0 implies all
138    // fn drop_material_client(&mut self, material: &dyn Material<Self>, render_context: &mut Self::Context);
139}
140
141//tt Material
142/// A [Material] provides means to access the data for a material, be
143/// it simple of full PBR. A fragment shader may require some aspects
144/// of a material to be provided to it for rendering, and this API
145/// allows that information to be gathered from any kind of material
146pub trait Material: std::fmt::Debug {
147    /// Invoked when an 3D model object is made renderable
148    // fn create_renderable(&self, _render_context: &mut R::Context) {}
149
150    /// Borrow the basic data of a material - color and base
151    /// metallic/roughness, for example
152    fn base_data(&self) -> &MaterialBaseData;
153    /// Get the index into the Textures array for a specific aspect
154    fn texture(&self, _aspect: MaterialAspect) -> ShortIndex {
155        ShortIndex::none()
156    }
157}