]> git.tdb.fi Git - libs/gl.git/blob - source/materials/splatmaterial.h
Implement a splat material type
[libs/gl.git] / source / materials / splatmaterial.h
1 #ifndef MSP_GL_SPLATMATERIAL_H_
2 #define MSP_GL_SPLATMATERIAL_H_
3
4 #include "material.h"
5
6 namespace Msp {
7 namespace GL {
8
9 class Texture;
10 class Texture2DArray;
11
12 class SplatMaterial: public Material, public NonCopyable
13 {
14 private:
15         struct MapArray;
16
17 public:
18         class Loader: public DataFile::DerivedObjectLoader<SplatMaterial, PropertyLoader<SplatMaterial>>
19         {
20         private:
21                 static ActionMap shared_actions;
22
23         public:
24                 Loader(SplatMaterial &, Collection &);
25
26         private:
27                 virtual void init_actions();
28                 virtual void finish();
29
30                 void map_storage(MapArray SplatMaterial::*, PixelFormat, unsigned, unsigned);
31                 void sub();
32         };
33
34 private:
35         struct SubMap
36         {
37                 std::string source_fn;
38                 int layer = -1;
39         };
40
41         struct SubMaterial
42         {
43                 class Loader: public DataFile::ObjectLoader<SubMaterial>
44                 {
45                 private:
46                         static ActionMap shared_actions;
47
48                 public:
49                         Loader(SubMaterial &);
50
51                 private:
52                         virtual void init_actions();
53
54                         void base_color(float, float, float);
55                         void emission(float, float, float);
56                         void map(SubMap SubMaterial::*, const std::string &);
57                         void tint(float, float, float, float);
58                 };
59
60                 Color base_color = { 0.8f };
61                 Color tint = { 1.0f };
62                 float metalness = 0.0f;
63                 float roughness = 0.5f;
64                 Color emission = { 0.0f };
65                 SubMap base_color_map;
66                 SubMap normal_map;
67                 SubMap metalness_map;
68                 SubMap roughness_map;
69                 SubMap occlusion_map;
70                 SubMap emission_map;
71         };
72
73         struct MapArray
74         {
75                 Texture2DArray *texture = 0;
76                 PixelFormat format = NO_PIXELFORMAT;
77                 unsigned width = 0;
78                 unsigned height = 0;
79                 unsigned max_layers = 0;
80
81                 void create();
82         };
83
84         std::vector<SubMaterial> sub_materials;
85         MapArray base_color_array;
86         MapArray normal_array;
87         MapArray metalness_array;
88         MapArray roughness_array;
89         MapArray occlusion_array;
90         MapArray emission_array;
91         const Texture2D &fresnel_lookup;
92         const Sampler &fresnel_sampler;
93
94         static const Tag texture_tags[];
95
96 public:
97         SplatMaterial();
98         ~SplatMaterial();
99
100 protected:
101         virtual void fill_program_info(std::string &, std::map<std::string, int> &) const;
102
103 public:
104         virtual const Tag *get_texture_tags() const { return texture_tags; }
105         virtual const Texture *get_texture(Tag) const;
106         virtual const Sampler *get_sampler(Tag) const;
107
108 private:
109         void set_sub_uniforms(unsigned);
110         void upload_sub(DataFile::Collection &, unsigned);
111         void upload_sub_map(DataFile::Collection &, unsigned, SubMap SubMaterial::*, MapArray &, const char *);
112 };
113
114 } // namespace GL
115 } // namespace Msp
116
117 #endif