CShader.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 CShaderH
46 #define CShaderH
47 //------------------------------------------------------------------------------
48 #include "system/CGlobals.h"
49 #include "system/CString.h"
50 #include "math/CConstants.h"
51 //------------------------------------------------------------------------------
52 #ifdef C_USE_OPENGL
54 #endif
55 //------------------------------------------------------------------------------
56 
57 //------------------------------------------------------------------------------
58 namespace chai3d {
59 //------------------------------------------------------------------------------
60 
61 //==============================================================================
68 //==============================================================================
69 
70 //------------------------------------------------------------------------------
71 // vertex array bindings
72 #define C_VB_INDEX_BUFFER 0
73 #define C_VB_POSITION 1
74 #define C_VB_NORMAL 2
75 #define C_VB_TEXCOORD 3
76 #define C_VB_COLOR 4
77 #define C_VB_TANGENT 5
78 #define C_VB_BITANGENT 6
79 
80 // texture unit bindings
81 #define C_TU_TEXTURE 0
82 #define C_TU_SHADOWMAP 1
83 #define C_TU_NORMALMAP 2
84 #define C_TU_SPECULARMAP 3
85 
86 #define C_TU_IMAGEBUFFER 2
87 #define C_TU_DEPTHBUFFER 3
88 
89 #define C_TU_IBL_ENVIRONMENT 4
90 #define C_TU_IBL_FILTERED 5
91 #define C_TU_IBL_BRDF 6
92 
93 
94 
95 #define C_TU_UNALLOCATED 7
96 
97 //------------------------------------------------------------------------------
98 
99 //------------------------------------------------------------------------------
101 {
102  C_VERTEX_SHADER = 0x0001,
105 };
106 //------------------------------------------------------------------------------
107 
108 //------------------------------------------------------------------------------
109 class cShader;
110 typedef std::shared_ptr<cShader> cShaderPtr;
111 //------------------------------------------------------------------------------
112 
113 //==============================================================================
125 //==============================================================================
126 class cShader
127 {
128  //--------------------------------------------------------------------------
129  // CONSTRUCTOR & DESTRUCTOR:
130  //--------------------------------------------------------------------------
131 
132 public:
133 
135  cShader(cShaderType a_type);
136 
138  virtual ~cShader(){};
139 
141  static cShaderPtr create(cShaderType a_type) { return (std::make_shared<cShader>(a_type)); }
142 
143 
144  //--------------------------------------------------------------------------
145  // PUBLIC METHODS:
146  //--------------------------------------------------------------------------
147 
148 public:
149 
151  bool loadSourceCode(const char *a_source);
152 
154  bool loadSourceCode(const std::string& a_source);
155 
157  bool loadSourceFile(const std::string& a_fileName);
158 
160  bool compile();
161 
163  bool isCompiled() const { return (m_compiled); }
164 
166  std::string getLog() const { return (m_log); }
167 
169  GLuint getId() const { return (m_id); }
170 
172  std::string getSource() { return (m_source); }
173 
175  cShaderType getType() const { return (m_type); }
176 
177 
178  //--------------------------------------------------------------------------
179  // STATIC METHODS:
180  //--------------------------------------------------------------------------
181 
182 public:
183 
185  static bool hasOpenGLShaders(cShaderType a_type);
186 
187 
188  //--------------------------------------------------------------------------
189  // PROTECTED MEMBERS:
190  //--------------------------------------------------------------------------
191 
192 protected:
193 
196 
198  GLuint m_id;
199 
201  std::string m_source;
202 
204  std::string m_log;
205 
208 };
209 
210 //------------------------------------------------------------------------------
211 } // namespace chai3d
212 //------------------------------------------------------------------------------
213 
214 //------------------------------------------------------------------------------
215 #endif
216 //------------------------------------------------------------------------------
Definition: CShader.h:103
cShaderType m_type
Shader type (C_VERTEX_SHADER, C_FRAGMENT_SHADER, or C_GEOMETRY_SHADER).
Definition: CShader.h:195
std::string m_source
The shader source code (i.e. the GLSL code itself).
Definition: CShader.h:201
Definition: CShader.h:104
std::shared_ptr< cShader > cShaderPtr
Definition: CShader.h:109
std::string getLog() const
This method returns the log file generated from compilation.
Definition: CShader.h:166
Definition: CShader.h:102
Implements mathematical constants.
Implements option settings for CHAI3D.
std::string m_log
Log message following compilation.
Definition: CShader.h:204
This class implements a shader primitive.
Definition: CShader.h:126
Implements utility functions for manipulating strings.
GLuint getId() const
This method returns the shader OpenGL ID.
Definition: CShader.h:169
cShaderType getType() const
This method returns the type of the shader.
Definition: CShader.h:175
bool m_compiled
If true then shader was compiled successfully, false otherwise.
Definition: CShader.h:207
static bool hasOpenGLShaders(cShaderType a_type)
This method returns true if shaders are supported on this computer, false otherwise.
bool compile()
This method compiles a shader and display any problems if compilation fails.
Definition: CShader.cpp:157
static cShaderPtr create(cShaderType a_type)
Shared cShader allocator.
Definition: CShader.h:141
bool loadSourceFile(const std::string &a_fileName)
This method loads the shader content from a file.
Definition: CShader.cpp:123
virtual ~cShader()
Destructor of cShader.
Definition: CShader.h:138
Definition: CAudioBuffer.cpp:56
cShaderType
Definition: CShader.h:100
bool isCompiled() const
This method returns true if shader has been compiled successfully, false otherwise.
Definition: CShader.h:163
std::string getSource()
This method returns the source code of the shader.
Definition: CShader.h:172
GLuint m_id
Handle ID for the shader.
Definition: CShader.h:198
bool loadSourceCode(const char *a_source)
This method loads the shader content from a char array.
Definition: CShader.cpp:88
cShader(cShaderType a_type)
Constructor of cShader.
Definition: CShader.cpp:68