My Project
Loading...
Searching...
No Matches
CShaderProgram.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, 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.3.0
41*/
42//==============================================================================
43
44//------------------------------------------------------------------------------
45#ifndef CShaderProgramH
46#define CShaderProgramH
47//------------------------------------------------------------------------------
48#include "shaders/CShader.h"
50//------------------------------------------------------------------------------
51#include <iostream>
52#include <map>
53#include <vector>
54//------------------------------------------------------------------------------
55
56//------------------------------------------------------------------------------
57namespace chai3d {
58//------------------------------------------------------------------------------
59
60//==============================================================================
67//==============================================================================
68
69//------------------------------------------------------------------------------
70class cShaderProgram;
71typedef std::shared_ptr<cShaderProgram> cShaderProgramPtr;
72//------------------------------------------------------------------------------
73
74//==============================================================================
87//==============================================================================
89{
90 //--------------------------------------------------------------------------
91 // CONSTRUCTOR & DESTRUCTOR:
92 //--------------------------------------------------------------------------
93
94public:
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
116public:
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
159public:
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
187protected:
188
190 GLuint m_id;
191
194
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//------------------------------------------------------------------------------
Implements a base class for all objects.
Implements an OpenGL shader primitive.
This class implements a base class for all 2D or 3D objects in CHAI3D.
Definition CGenericObject.h:113
This class implements a shader program primitive.
Definition CShaderProgram.h:89
virtual ~cShaderProgram()
Destructor of cShaderProgram.
Definition CShaderProgram.cpp:127
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
void disable()
This method disables the shader program.
Definition CShaderProgram.cpp:277
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
int getUniformLocation(const char *a_name)
This method returns the location of a specified uniform.
Definition CShaderProgram.cpp:354
std::map< std::string, int > m_uniformLocList
Map of uniforms and their binding locations.
Definition CShaderProgram.h:205
void setGeometryVerticesOut(int a_numVertices)
Set the maximum number of vertices emitted by the gemoetry shader.
Definition CShaderProgram.h:152
void setGeometryInputType(GLenum a_type)
Set the geometry shader input primitype type.
Definition CShaderProgram.h:146
int getAttributeLocation(const char *a_name)
This method returns the location of a specified attribute.
Definition CShaderProgram.cpp:322
GLint m_geometryVerticesOut
Maximum number of vertices generated by the geometry shader.
Definition CShaderProgram.h:220
GLuint m_id
OpenGL handle ID for the shader program.
Definition CShaderProgram.h:190
void setUniformi(const char *a_name, const GLint a_value)
This method sets an int uniform to a specified value.
Definition CShaderProgram.cpp:385
void setUniform(const char *a_name, cVector3d &a_value)
This method sets a cVector3d uniform to a specified value.
Definition CShaderProgram.cpp:447
GLuint m_shaderCount
Number of shaders that are attached to the shader program.
Definition CShaderProgram.h:199
cShaderProgram()
Constructor of cShaderProgram.
Definition CShaderProgram.cpp:61
bool m_geometryShaderAttached
Whether or not this program has a geometry shader attached.
Definition CShaderProgram.h:211
void bindAttributeLocation(const unsigned int a_index, const char *a_name)
This method binds an attribute to a name.
Definition CShaderProgram.cpp:294
GLenum m_geometryInputType
Geometry shader input primitive type, if applicable.
Definition CShaderProgram.h:214
void setGeometryOutputType(GLenum a_type)
Set the geometry shader output primitype type.
Definition CShaderProgram.h:149
bool isUsed()
This method returns true if this shader is currently in use. false otherwise.
Definition CShaderProgram.h:131
static cShaderProgramPtr create()
Shared cShaderProgram allocator.
Definition CShaderProgram.h:106
void use(cGenericObject *a_object, cRenderOptions &a_options)
This method enables the shader program.
Definition CShaderProgram.cpp:259
static cShaderProgramPtr create(const std::string &a_vertexShader, const std::string &a_fragmentShader)
Shared cShaderProgram allocator.
Definition CShaderProgram.h:109
GLint getId()
This method returns the program ID.
Definition CShaderProgram.h:143
bool linkProgram()
This method links the shader program and returns the link status.
Definition CShaderProgram.cpp:166
void attachShader(cShaderPtr a_shader)
This method attaches a shader to the shader program.
Definition CShaderProgram.cpp:145
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
std::map< std::string, int > m_attributeLocList
Map of attributes and their binding locations.
Definition CShaderProgram.h:202
bool m_linked
If true then shader program was successfully linked, false otherwise.
Definition CShaderProgram.h:193
void setUniformf(const char *a_name, const GLfloat a_value)
This method sets a float uniform to a specified value.
Definition CShaderProgram.cpp:416
bool m_enabled
If true then program shader is currently enabled.
Definition CShaderProgram.h:196
Definition CAudioBuffer.cpp:57
std::shared_ptr< cShader > cShaderPtr
Definition CShader.h:110
std::shared_ptr< cShaderProgram > cShaderProgramPtr
Definition CShaderProgram.h:71
This class implements a 3D matrix.
Definition CMatrix3d.h:98
This structures provide a containers for storing rendering options that are passed through the sceneg...
Definition CRenderOptions.h:83
This class implements a 4D transformation matrix encoded as column-major.
Definition CTransform.h:76
This class implements a 3D vector.
Definition CVector3d.h:89