SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLProgramGenerated.h
Go to the documentation of this file.
1 /**
2  * \file SLGLProgramGenerated.h
3  * \authors Marcus Hudritsch
4  * \details Defines a generated shader program that just starts and stops the
5  * shaders that are hold in the base class SLGLProgram.
6  * \date December 2020
7  * \copyright http://opensource.org/licenses/GPL-3.0
8  * \remarks Please use clangformat to format the code. See more code style on
9  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
10 */
11 
12 #ifndef SLGLPROGRAMMGENERATED_H
13 #define SLGLPROGRAMMGENERATED_H
14 
15 #include <SLGLProgram.h>
16 
17 class SLMaterial;
18 class SLAssetManager;
19 
20 //-----------------------------------------------------------------------------
21 //! Generated Shader Program class inherited from SLGLProgram
22 /*! An instance of this class generates the shader code on the fly at
23  construction time based on the information of the passed material and lights
24  vector. The generated program depends on the following parameters:
25  - mat->reflectionModel (Blinn-Phong or Cook-Torrance)
26  - mat->textures
27  - light->createsShadows
28  - active camera for the fog and projection parameters
29 
30  The shader program gets a unique name with the following pattern:
31 <pre>
32  genCook-D00-N00-E00-O01-RM00-Sky-C4s
33  | | | | | | | |
34  | | | | | | | + Directional light w. 4 shadow cascades
35  | | | | | | + Ambient light from skybox
36  | | | | | + Roughness-metallic map with index 0 and uv 0
37  | | | | + Ambient Occlusion map with index 0 and uv 1
38  | | | + Emissive Map with index 0 and uv 0
39  | | + Normal Map with index 0 and uv 0
40  | + Diffuse Texture Mapping with index 0 and uv 0
41  + Cook-Torrance or Blinn-Phong lighting model
42 </pre>
43  The above example is for a material with 5 textures and a scene with one light.
44  The shader program is constructed when a material is for the first time
45  activated (SLMaterial::activate) and it's program pointer is null. The old
46  system of custom written GLSL shader program is still valid.
47  At the end of SLMaterial::activate the generated vertex and fragment shader
48  get compiled, linked and activated with the OpenGL functions in SLGLShader
49  and SLGLProgram.
50  After successful compilation the shader get exported into the applications
51  config directory if they not yet exist there.
52 */
54 {
55 public:
56  ~SLGLProgramGenerated() override = default;
57 
58  //! ctor for generated shader program
60  const string& programName,
61  SLMaterial* mat,
62  SLVLight* lights,
63  SLbool supportGPUSkinning)
64  : SLGLProgram(am,
65  "",
66  "",
67  "",
68  programName)
69  {
70  buildProgramCode(mat, lights, supportGPUSkinning);
71  }
72 
73  //! ctor for generated shader program PS
75  const string& programName,
76  SLMaterial* mat,
77  bool isDrawProg,
78  SLstring geomShader = "")
79  : SLGLProgram(am,
80  "",
81  "",
82  geomShader,
83  programName)
84  {
85 
86  if (geomShader != "")
87  {
88  buildProgramCodePS(mat, isDrawProg, false);
89  }
90  else
91  {
92  buildProgramCodePS(mat, isDrawProg, true);
93  }
94  }
95 
96  static bool lightsDoShadowMapping(SLVLight* lights);
97  static void buildProgramName(SLMaterial* mat,
98  SLVLight* lights,
99  SLbool supportGPUSkinning,
100  string& programName);
101  static void buildProgramNamePS(SLMaterial* mat,
102  string& programName,
103  bool isDrawProg,
104  bool drawInstanced);
105 
106  void buildProgramCodePS(SLMaterial* mat,
107  bool isDrawProg,
108  bool drawInstanced = false);
109  void buildProgramCode(SLMaterial* mat,
110  SLVLight* lights,
111  SLbool supportGPUSkinning);
113  SLMaterial* mat,
114  SLVLight* lights) override { beginUse(cam, mat, lights); }
115  void endShader() override { endUse(); }
116 
117 private:
118  void buildPerPixCook(SLMaterial* mat, SLVLight* lights, SLbool supportGPUSkinning);
119  void buildPerPixBlinn(SLMaterial* mat, SLVLight* lights, SLbool supportGPUSkinning);
120  void buildPerPixParticle(SLMaterial* mat);
123 
124  // Video background shader builder functions
125  void buildPerPixVideoBkgdSm(SLVLight* lights);
126 
127  // Helpers
128  static string fragInput_u_lightSm(SLVLight* lights);
129  static string fragInput_u_shadowMaps(SLVLight* lights);
130  static string fragFunctionShadowTest(SLVLight* lights);
131  static string shaderHeader(int numLights);
132  static string shaderHeader();
133  static void addCodeToShader(SLGLShader* shader,
134  const string& code,
135  const string& name);
136  static void setVariable(std::string& code,
137  const std::string& name,
138  const std::string& value);
139 
140  static string generatedShaderPath; //! Path to write out generated shaders
141 };
142 //-----------------------------------------------------------------------------
143 #endif
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
vector< SLLight * > SLVLight
STL vector of light pointers.
Definition: SLLight.h:232
Toplevel holder of the assets meshes, materials, textures and shaders.
Active or visible camera node class.
Definition: SLCamera.h:54
Generated Shader Program class inherited from SLGLProgram.
static string fragInput_u_lightSm(SLVLight *lights)
void buildPerPixParticleUpdate(SLMaterial *mat)
static bool lightsDoShadowMapping(SLVLight *lights)
Returns true if at least one of the light does shadow mapping.
static void addCodeToShader(SLGLShader *shader, const string &code, const string &name)
Add vertex shader code to the SLGLShader instance.
~SLGLProgramGenerated() override=default
SLGLProgramGenerated(SLAssetManager *am, const string &programName, SLMaterial *mat, bool isDrawProg, SLstring geomShader="")
ctor for generated shader program PS
static void setVariable(std::string &code, const std::string &name, const std::string &value)
Sets a variable in the shader code.
void buildPerPixCook(SLMaterial *mat, SLVLight *lights, SLbool supportGPUSkinning)
void buildPerPixBlinn(SLMaterial *mat, SLVLight *lights, SLbool supportGPUSkinning)
SLGLProgramGenerated(SLAssetManager *am, const string &programName, SLMaterial *mat, SLVLight *lights, SLbool supportGPUSkinning)
ctor for generated shader program
static string generatedShaderPath
static string fragFunctionShadowTest(SLVLight *lights)
Adds the core shadow mapping test routine depending on the lights.
static string shaderHeader()
Adds shader header code.
void buildProgramCode(SLMaterial *mat, SLVLight *lights, SLbool supportGPUSkinning)
static void buildProgramNamePS(SLMaterial *mat, string &programName, bool isDrawProg, bool drawInstanced)
static void buildProgramName(SLMaterial *mat, SLVLight *lights, SLbool supportGPUSkinning, string &programName)
Builds unique program name that identifies shader program.
void beginShader(SLCamera *cam, SLMaterial *mat, SLVLight *lights) override
starter for derived classes
void buildProgramCodePS(SLMaterial *mat, bool isDrawProg, bool drawInstanced=false)
void buildPerPixParticle(SLMaterial *mat)
void buildPerPixParticleInstanced(SLMaterial *mat)
static string fragInput_u_shadowMaps(SLVLight *lights)
void buildPerPixVideoBkgdSm(SLVLight *lights)
Assemble shaders for video on background.
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
void beginUse(SLCamera *cam, SLMaterial *mat, SLVLight *lights)
void endUse()
SLGLProgram::endUse stops the shader program.
Encapsulation of an OpenGL shader object.
Definition: SLGLShader.h:25
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
const SLstring & name() const
Definition: SLObject.h:38