SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLProgram.h
Go to the documentation of this file.
1 /**
2  * \file SLGLProgram.h
3  * \authors Marcus Hudritsch, Martin Christens, http://www.clockworkcoders.com
4  * \date July 2014
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLGLPROGRAM_H
11 #define SLGLPROGRAM_H
12 
13 #include <map>
14 
15 #include <SLGLState.h>
16 #include <SLVec4.h>
17 #include <SLGLUniform.h>
18 #include <SLObject.h>
19 #include <SLLight.h>
20 
21 class SLGLShader;
22 class SLScene;
23 class SLMaterial;
24 class SLGLState;
25 class SLAssetManager;
26 class SLCamera;
27 class SLSkybox;
28 
29 //-----------------------------------------------------------------------------
30 //! STL vector type for SLGLShader pointers
31 typedef vector<SLGLShader*> SLVGLShader;
32 
33 #if defined(TARGET_OS_IOS)
34 // The TR1 unordered_map or the hash_map is not yet available on iOS
35 typedef std::map<string, int> SLLocMap;
36 #else
37 // typedef unordered_map<const char*, int, hash<const char*>, eqstr> SLLocMap;
38 typedef std::map<string, int> SLLocMap;
39 #endif
40 
41 //-----------------------------------------------------------------------------
42 //! Encapsulation of an OpenGL shader program object
43 /*!
44 The SLGLProgram base class represents a shader program object of the OpenGL
45 Shading Language (GLSL). Multiple SLGLShader objects can be attached and linked
46 at run time. An SLGLProgram object can then be attached to an SLMaterial
47 node for execution. An SLGLProgram object can hold an vector of uniform
48 variable that can transfer variables from the CPU program to the GPU program.
49 For more details on GLSL please refer to official GLSL documentation and to
50 SLGLShader.<br>
51 All shader files are located in the directory data/shaders. For OSX, iOS and
52 Android applications they are copied to the appropriate file system locations.
53 */
54 //-----------------------------------------------------------------------------
55 class SLGLProgram : public SLObject
56 {
57 public:
59  const string& vertShaderFile,
60  const string& fragShaderFile,
61  const string& geomShaderFile = "",
62  const string& programName = "");
63 
64  ~SLGLProgram() override;
65 
66  void deleteDataGpu();
67  void addShader(SLGLShader* shader);
68  void init(SLVLight* lights);
69  void initTF(const char* writeBackAttrib[], int size);
70 
71  virtual void beginShader(SLCamera* cam,
72  SLMaterial* mat,
73  SLVLight* lights) = 0; //!< starter for derived classes
74  virtual void endShader() = 0;
75 
76  void beginUse(SLCamera* cam,
77  SLMaterial* mat,
78  SLVLight* lights);
80  SLuint nextTexUnit) const;
81  void endUse();
82  void useProgram();
83 
84  void addUniform1f(SLGLUniform1f* u); //!< add float uniform
85  void addUniform1i(SLGLUniform1i* u); //!< add int uniform
86 
87  // Getters
88  SLuint progID() const { return _progID; }
89  SLVGLShader& shaders() { return _shaders; }
90 
91  // Variable location getters
92  SLint getUniformLocation(const SLchar* name) const;
93 
94  // Send uniform variables to program
95  SLint uniform1f(const SLchar* name, SLfloat v0) const;
96  SLint uniform2f(const SLchar* name, SLfloat v0, SLfloat v1) const;
97  SLint uniform3f(const SLchar* name, SLfloat v0, SLfloat v1, SLfloat v2) const;
98  SLint uniform4f(const SLchar* name, SLfloat v0, SLfloat v1, SLfloat v2, SLfloat v3) const;
99 
100  SLint uniform1i(const SLchar* name, SLint v0) const;
101  SLint uniform2i(const SLchar* name, SLint v0, SLint v1) const;
102  SLint uniform3i(const SLchar* name, SLint v0, SLint v1, SLint v2) const;
103  SLint uniform4i(const SLchar* name, SLint v0, SLint v1, SLint v2, SLint v3) const;
104 
105  SLint uniform1fv(const SLchar* name, SLsizei count, const SLfloat* value) const;
106  SLint uniform2fv(const SLchar* name, SLsizei count, const SLfloat* value) const;
107  SLint uniform3fv(const SLchar* name, SLsizei count, const SLfloat* value) const;
108  SLint uniform4fv(const SLchar* name, SLsizei count, const SLfloat* value) const;
109 
110  SLint uniform1iv(const SLchar* name, SLsizei count, const SLint* value) const;
111  SLint uniform2iv(const SLchar* name, SLsizei count, const SLint* value) const;
112  SLint uniform3iv(const SLchar* name, SLsizei count, const SLint* value) const;
113  SLint uniform4iv(const SLchar* name, GLsizei count, const SLint* value) const;
114 
116  SLsizei count,
117  const SLfloat* value,
118  GLboolean transpose = false) const;
119  void uniformMatrix2fv(SLint loc,
120  SLsizei count,
121  const SLfloat* value,
122  GLboolean transpose = false) const;
124  SLsizei count,
125  const SLfloat* value,
126  GLboolean transpose = false) const;
127  void uniformMatrix3fv(SLint loc,
128  SLsizei count,
129  const SLfloat* value,
130  GLboolean transpose = false) const;
132  SLsizei count,
133  const SLfloat* value,
134  GLboolean transpose = false) const;
135  void uniformMatrix4fv(SLint loc,
136  SLsizei count,
137  const SLfloat* value,
138  GLboolean transpose = false) const;
139 
140 protected:
141  SLuint _progID; //!< OpenGL shader program object ID
142  SLbool _isLinked; //!< Flag if program is linked
143  SLVGLShader _shaders; //!< Vector of all shader objects
144  SLVUniform1f _uniforms1f; //!< Vector of uniform1f variables
145  SLVUniform1i _uniforms1i; //!< Vector of uniform1i variables
146 };
147 //-----------------------------------------------------------------------------
148 //! STL vector of SLGLProgram pointers
149 typedef vector<SLGLProgram*> SLVGLProgram;
150 //-----------------------------------------------------------------------------
151 #endif // SLSHADERPROGRAM_H
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
char SLchar
Definition: SL.h:162
bool SLbool
Definition: SL.h:175
int SLsizei
Definition: SL.h:172
int SLint
Definition: SL.h:170
std::map< string, int > SLLocMap
Definition: SLGLProgram.h:38
vector< SLGLShader * > SLVGLShader
STL vector type for SLGLShader pointers.
Definition: SLGLProgram.h:27
vector< SLGLProgram * > SLVGLProgram
STL vector of SLGLProgram pointers.
Definition: SLGLProgram.h:149
Singleton class for global render state.
vector< SLGLUniform1f * > SLVUniform1f
STL vector of SLGLShaderUniform1f pointers.
Definition: SLGLUniform.h:167
vector< SLGLUniform1i * > SLVUniform1i
Definition: SLGLUniform.h:168
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
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
SLint uniformMatrix4fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 4x4 float matrix values py pointer to the uniform variable "name".
SLint uniform4fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 4 float values py pointer to the uniform variable "name".
void deleteDataGpu()
Delete all Gpu data.
Definition: SLGLProgram.cpp:96
SLint uniform4f(const SLchar *name, SLfloat v0, SLfloat v1, SLfloat v2, SLfloat v3) const
Passes the float values v0,v1,v2 & v3 to the uniform variable "name".
SLint uniform3fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 3 float values py pointer to the uniform variable "name".
~SLGLProgram() override
Definition: SLGLProgram.cpp:66
virtual void beginShader(SLCamera *cam, SLMaterial *mat, SLVLight *lights)=0
starter for derived classes
SLint uniform3i(const SLchar *name, SLint v0, SLint v1, SLint v2) const
Passes the int values v0, v1 & v2 to the uniform variable "name".
SLint uniform2iv(const SLchar *name, SLsizei count, const SLint *value) const
Passes 2 int values py pointer to the uniform variable "name".
SLint uniform3iv(const SLchar *name, SLsizei count, const SLint *value) const
Passes 3 int values py pointer to the uniform variable "name".
SLint uniformMatrix3fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 3x3 float matrix values py pointer to the uniform variable "name".
SLint uniform2fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 2 float values py pointer to the uniform variable "name".
SLint uniform1f(const SLchar *name, SLfloat v0) const
Passes the float value v0 to the uniform variable "name".
void initTF(const char *writeBackAttrib[], int size)
void beginUse(SLCamera *cam, SLMaterial *mat, SLVLight *lights)
void addShader(SLGLShader *shader)
SLGLProgram::addShader adds a shader to the shader list.
SLint uniform2f(const SLchar *name, SLfloat v0, SLfloat v1) const
Passes the float values v0 & v1 to the uniform variable "name".
void addUniform1i(SLGLUniform1i *u)
add int uniform
SLuint _progID
OpenGL shader program object ID.
Definition: SLGLProgram.h:141
SLint uniform1fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 1 float value py pointer to the uniform variable "name".
SLint getUniformLocation(const SLchar *name) const
SLVUniform1f _uniforms1f
Vector of uniform1f variables.
Definition: SLGLProgram.h:144
SLVGLShader & shaders()
Definition: SLGLProgram.h:89
void addUniform1f(SLGLUniform1f *u)
add float uniform
SLint uniform3f(const SLchar *name, SLfloat v0, SLfloat v1, SLfloat v2) const
Passes the float values v0, v1 & v2 to the uniform variable "name".
SLint uniformMatrix2fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 2x2 float matrix values py pointer to the uniform variable "name".
void useProgram()
SLVGLShader _shaders
Vector of all shader objects.
Definition: SLGLProgram.h:143
void init(SLVLight *lights)
SLint passLightsToUniforms(SLVLight *lights, SLuint nextTexUnit) const
SLint uniform1iv(const SLchar *name, SLsizei count, const SLint *value) const
Passes 1 int value py pointer to the uniform variable "name".
SLVUniform1i _uniforms1i
Vector of uniform1i variables.
Definition: SLGLProgram.h:145
SLint uniform1i(const SLchar *name, SLint v0) const
Passes the int values v0 to the uniform variable "name".
virtual void endShader()=0
SLuint progID() const
Definition: SLGLProgram.h:88
SLint uniform2i(const SLchar *name, SLint v0, SLint v1) const
Passes the int values v0 & v1 to the uniform variable "name".
SLint uniform4iv(const SLchar *name, GLsizei count, const SLint *value) const
Passes 4 int values py pointer to the uniform variable "name".
SLbool _isLinked
Flag if program is linked.
Definition: SLGLProgram.h:142
SLGLProgram(SLAssetManager *am, const string &vertShaderFile, const string &fragShaderFile, const string &geomShaderFile="", const string &programName="")
Ctor with a vertex and a fragment shader filename.
Definition: SLGLProgram.cpp:39
void endUse()
SLGLProgram::endUse stops the shader program.
SLint uniform4i(const SLchar *name, SLint v0, SLint v1, SLint v2, SLint v3) const
Passes the int values v0, v1, v2 & v3 to the uniform variable "name".
Encapsulation of an OpenGL shader object.
Definition: SLGLShader.h:25
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
Template for a single GLSL uniform variable.
Definition: SLGLUniform.h:24
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
Base class for all other classes.
Definition: SLObject.h:23
const SLstring & name() const
Definition: SLObject.h:38
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
Skybox node class with a SLBox mesh.
Definition: SLSkybox.h:29
V3 v3(float x, float y, float z)
Definition: WAIMath.h:38