CShaderProgram.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti, Sonny Chan
40  \version 3.2.0 $Rev: 2187 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CShaderProgramH
46 #define CShaderProgramH
47 //------------------------------------------------------------------------------
48 #include "shaders/CShader.h"
49 #include "world/CGenericObject.h"
50 //------------------------------------------------------------------------------
51 #include <iostream>
52 #include <map>
53 #include <vector>
54 //------------------------------------------------------------------------------
55 
56 //------------------------------------------------------------------------------
57 namespace chai3d {
58 //------------------------------------------------------------------------------
59 
60 //==============================================================================
67 //==============================================================================
68 
69 //------------------------------------------------------------------------------
71 typedef std::shared_ptr<cShaderProgram> cShaderProgramPtr;
72 //------------------------------------------------------------------------------
73 
74 //==============================================================================
87 //==============================================================================
89 {
90  //--------------------------------------------------------------------------
91  // CONSTRUCTOR & DESTRUCTOR:
92  //--------------------------------------------------------------------------
93 
94 public:
95 
98 
100  cShaderProgram(const std::string& a_vertexShader, const std::string& a_fragmentShader);
101 
103  virtual ~cShaderProgram();
104 
106  static cShaderProgramPtr create() { return (std::make_shared<cShaderProgram>()); }
107 
109  static cShaderProgramPtr create(const std::string& a_vertexShader, const std::string& a_fragmentShader) { return (std::make_shared<cShaderProgram>(a_vertexShader, a_fragmentShader)); }
110 
111 
112  //--------------------------------------------------------------------------
113  // PUBLIC METHODS:
114  //--------------------------------------------------------------------------
115 
116 public:
117 
119  void attachShader(cShaderPtr a_shader);
120 
122  bool linkProgram();
123 
125  void use(cGenericObject* a_object, cRenderOptions& a_options);
126 
128  void disable();
129 
131  bool isUsed() { return (m_enabled); }
132 
134  void bindAttributeLocation(const unsigned int a_index, const char *a_name);
135 
137  int getAttributeLocation(const char *a_name);
138 
140  int getUniformLocation(const char *a_name);
141 
143  GLint getId() { return (m_id); }
144 
146  void setGeometryInputType(GLenum a_type) { m_geometryInputType = a_type; }
147 
149  void setGeometryOutputType(GLenum a_type) { m_geometryOutputType = a_type; }
150 
152  void setGeometryVerticesOut(int a_numVertices) { m_geometryVerticesOut = a_numVertices; }
153 
154 
155  //--------------------------------------------------------------------------
156  // PUBLIC METHODS: (UNIFORMS)
157  //--------------------------------------------------------------------------
158 
159 public:
160 
162  void setUniformi(const char* a_name, const GLint a_value);
163 
165  void setUniformf(const char* a_name, const GLfloat a_value);
166 
168  void setUniform(const char* a_name, cVector3d& a_value);
169 
171  void setUniform(const char* a_name, cMatrix3d& a_value, bool a_transposed);
172 
174  void setUniform(const char* a_name, cTransform& a_value, bool a_transposed);
175 
177  void setUniformiv(const char* a_name, const GLint *a_values, const int a_count);
178 
180  void setUniformfv(const char* a_name, const GLfloat *a_values, const int a_count);
181 
182 
183  //--------------------------------------------------------------------------
184  // PROTECTED MEMBERS:
185  //--------------------------------------------------------------------------
186 
187 protected:
188 
190  GLuint m_id;
191 
193  bool m_linked;
194 
196  bool m_enabled;
197 
200 
202  std::map<std::string, int> m_attributeLocList;
203 
205  std::map<std::string, int> m_uniformLocList;
206 
208  std::vector<cShaderPtr> m_shaders;
209 
212 
215 
218 
221 };
222 
223 //------------------------------------------------------------------------------
224 } // namespace chai3d
225 //------------------------------------------------------------------------------
226 
227 //------------------------------------------------------------------------------
228 #endif
229 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:88
std::vector< cShaderPtr > m_shaders
List of attached shaders.
Definition: CShaderProgram.h:208
GLenum m_geometryOutputType
Geometry shader output primitive type, if applicable.
Definition: CShaderProgram.h:217
This structures provide a containers for storing rendering options that are passed through the sceneg...
Definition: CRenderOptions.h:82
Implements an OpenGL shader primitive.
bool linkProgram()
This method links the shader program and returns the link status.
Definition: CShaderProgram.cpp:166
void setUniform(const char *a_name, cVector3d &a_value)
This method sets a cVector3d uniform to a specified value.
Definition: CShaderProgram.cpp:447
void attachShader(cShaderPtr a_shader)
This method attaches a shader to the shader program.
Definition: CShaderProgram.cpp:145
int getUniformLocation(const char *a_name)
This method returns the location of a specified uniform.
Definition: CShaderProgram.cpp:354
void use(cGenericObject *a_object, cRenderOptions &a_options)
This method enables the shader program.
Definition: CShaderProgram.cpp:259
void setUniformfv(const char *a_name, const GLfloat *a_values, const int a_count)
This method sets a float array uniform to specified values.
Definition: CShaderProgram.cpp:587
bool m_linked
If true then shader program was successfully linked, false otherwise.
Definition: CShaderProgram.h:193
GLint getId()
This method returns the program ID.
Definition: CShaderProgram.h:143
void setGeometryOutputType(GLenum a_type)
Set the geometry shader output primitype type.
Definition: CShaderProgram.h:149
This class implements a shader program primitive.
Definition: CShaderProgram.h:88
GLuint m_id
OpenGL handle ID for the shader program.
Definition: CShaderProgram.h:190
std::shared_ptr< cShader > cShaderPtr
Definition: CShader.h:109
bool m_enabled
If true then program shader is currently enabled.
Definition: CShaderProgram.h:196
static cShaderProgramPtr create(const std::string &a_vertexShader, const std::string &a_fragmentShader)
Shared cShaderProgram allocator.
Definition: CShaderProgram.h:109
GLint m_geometryVerticesOut
Maximum number of vertices generated by the geometry shader.
Definition: CShaderProgram.h:220
This class implements a base class for all 2D or 3D objects in CHAI3D.
Definition: CGenericObject.h:112
This class implements a 3D matrix.
Definition: CMatrix3d.h:97
This class implements a 4D transformation matrix encoded as column-major.
Definition: CTransform.h:75
int getAttributeLocation(const char *a_name)
This method returns the location of a specified attribute.
Definition: CShaderProgram.cpp:322
Implements a base class for all objects.
GLenum m_geometryInputType
Geometry shader input primitive type, if applicable.
Definition: CShaderProgram.h:214
std::map< std::string, int > m_uniformLocList
Map of uniforms and their binding locations.
Definition: CShaderProgram.h:205
static cShaderProgramPtr create()
Shared cShaderProgram allocator.
Definition: CShaderProgram.h:106
GLuint m_shaderCount
Number of shaders that are attached to the shader program.
Definition: CShaderProgram.h:199
virtual ~cShaderProgram()
Destructor of cShaderProgram.
Definition: CShaderProgram.cpp:127
void setUniformiv(const char *a_name, const GLint *a_values, const int a_count)
This method sets an int array uniform to specified values.
Definition: CShaderProgram.cpp:555
bool isUsed()
This method returns true if this shader is currently in use. false otherwise.
Definition: CShaderProgram.h:131
bool m_geometryShaderAttached
Whether or not this program has a geometry shader attached.
Definition: CShaderProgram.h:211
std::shared_ptr< cShaderProgram > cShaderProgramPtr
Definition: CShaderProgram.h:70
cShaderProgram()
Constructor of cShaderProgram.
Definition: CShaderProgram.cpp:61
void bindAttributeLocation(const unsigned int a_index, const char *a_name)
This method binds an attribute to a name.
Definition: CShaderProgram.cpp:294
Definition: CAudioBuffer.cpp:56
void setUniformi(const char *a_name, const GLint a_value)
This method sets an int uniform to a specified value.
Definition: CShaderProgram.cpp:385
std::map< std::string, int > m_attributeLocList
Map of attributes and their binding locations.
Definition: CShaderProgram.h:202
void setGeometryVerticesOut(int a_numVertices)
Set the maximum number of vertices emitted by the gemoetry shader.
Definition: CShaderProgram.h:152
void setUniformf(const char *a_name, const GLfloat a_value)
This method sets a float uniform to a specified value.
Definition: CShaderProgram.cpp:416
void setGeometryInputType(GLenum a_type)
Set the geometry shader input primitype type.
Definition: CShaderProgram.h:146
void disable()
This method disables the shader program.
Definition: CShaderProgram.cpp:277