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
40  \version 3.1.1 $Rev: 1869 $
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  virtual ~cShaderProgram();
101 
103  static cShaderProgramPtr create() { return (std::make_shared<cShaderProgram>()); }
104 
105 
106  //--------------------------------------------------------------------------
107  // PUBLIC METHODS:
108  //--------------------------------------------------------------------------
109 
110 public:
111 
113  void attachShader(cShaderPtr a_shader);
114 
116  bool linkProgram();
117 
119  void use(cGenericObject* a_object, cRenderOptions& a_options);
120 
122  void disable();
123 
125  bool isUsed() { return (m_enabled); }
126 
128  void bindAttributeLocation(const unsigned int a_index, const char *a_name);
129 
131  int getAttributeLocation(const char *a_name);
132 
134  int getUniformLocation(const char *a_name);
135 
137  GLint getId() { return (m_id); }
138 
139 
140  //--------------------------------------------------------------------------
141  // PUBLIC METHODS: (UNIFORMS)
142  //--------------------------------------------------------------------------
143 
144 public:
145 
147  void setUniformi(const char* a_name, const GLint a_value);
148 
150  void setUniformf(const char* a_name, const GLfloat a_value);
151 
153  void setUniform(const char* a_name, cVector3d& a_value);
154 
156  void setUniform(const char* a_name, cMatrix3d& a_value, bool a_transposed);
157 
159  void setUniform(const char* a_name, cTransform& a_value, bool a_transposed);
160 
162  void setUniformiv(const char* a_name, const GLint *a_values, const int a_count);
163 
165  void setUniformfv(const char* a_name, const GLfloat *a_values, const int a_count);
166 
167 
168  //--------------------------------------------------------------------------
169  // PROTECTED MEMBERS:
170  //--------------------------------------------------------------------------
171 
172 protected:
173 
175  GLuint m_id;
176 
178  bool m_linked;
179 
181  bool m_enabled;
182 
185 
187  std::map<std::string, int> m_attributeLocList;
188 
190  std::map<std::string, int> m_uniformLocList;
191 
193  std::vector<cShaderPtr> m_shaders;
194 };
195 
196 //------------------------------------------------------------------------------
197 } // namespace chai3d
198 //------------------------------------------------------------------------------
199 
200 //------------------------------------------------------------------------------
201 #endif
202 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:88
std::vector< cShaderPtr > m_shaders
List of attached shaders.
Definition: CShaderProgram.h:193
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:112
void setUniform(const char *a_name, cVector3d &a_value)
This method sets a cVector3d uniform to a specified value.
Definition: CShaderProgram.cpp:385
void attachShader(cShaderPtr a_shader)
This method attaches a shader to the shader program.
Definition: CShaderProgram.cpp:95
int getUniformLocation(const char *a_name)
This method returns the location of a specified uniform.
Definition: CShaderProgram.cpp:292
void use(cGenericObject *a_object, cRenderOptions &a_options)
This method enables the shader program.
Definition: CShaderProgram.cpp:197
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:525
bool m_linked
If true then shader program was successfully linked, false otherwise.
Definition: CShaderProgram.h:178
GLint getId()
This method returns the program ID.
Definition: CShaderProgram.h:137
This class implements a shader program primitive.
Definition: CShaderProgram.h:88
GLuint m_id
OpenGL handle ID for the shader program.
Definition: CShaderProgram.h:175
std::shared_ptr< cShader > cShaderPtr
Definition: CShader.h:89
bool m_enabled
If true then program shader is currently enabled.
Definition: CShaderProgram.h:181
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:260
Implements a base class for all objects.
std::map< std::string, int > m_uniformLocList
Map of uniforms and their binding locations.
Definition: CShaderProgram.h:190
static cShaderProgramPtr create()
Shared cShaderProgram allocator.
Definition: CShaderProgram.h:103
GLuint m_shaderCount
Number of shaders that are attached to the shader program.
Definition: CShaderProgram.h:184
virtual ~cShaderProgram()
Destructor of cShaderProgram.
Definition: CShaderProgram.cpp:77
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:493
bool isUsed()
This method returns true if this shader is currently in use. false otherwise.
Definition: CShaderProgram.h:125
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:232
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:323
std::map< std::string, int > m_attributeLocList
Map of attributes and their binding locations.
Definition: CShaderProgram.h:187
void setUniformf(const char *a_name, const GLfloat a_value)
This method sets a float uniform to a specified value.
Definition: CShaderProgram.cpp:354
void disable()
This method disables the shader program.
Definition: CShaderProgram.cpp:215