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
40  \version 3.1.1 $Rev: 1869 $
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 #define C_VB_INDEX_BUFFER 0
72 #define C_VB_POSITION 1
73 #define C_VB_NORMAL 2
74 #define C_VB_TEXCOORD 3
75 #define C_VB_COLOR 4
76 #define C_VB_TANGENT 5
77 #define C_VB_BITANGENT 6
78 //------------------------------------------------------------------------------
79 
80 //------------------------------------------------------------------------------
82 {
83  C_VERTEX_SHADER = 0x0001,
85 };
86 //------------------------------------------------------------------------------
87 
88 //------------------------------------------------------------------------------
89 class cShader;
90 typedef std::shared_ptr<cShader> cShaderPtr;
91 //------------------------------------------------------------------------------
92 
93 //==============================================================================
105 //==============================================================================
106 class cShader
107 {
108  //--------------------------------------------------------------------------
109  // CONSTRUCTOR & DESTRUCTOR:
110  //--------------------------------------------------------------------------
111 
112 public:
113 
115  cShader(cShaderType a_type);
116 
118  virtual ~cShader(){};
119 
121  static cShaderPtr create(cShaderType a_type) { return (std::make_shared<cShader>(a_type)); }
122 
123 
124  //--------------------------------------------------------------------------
125  // PUBLIC METHODS:
126  //--------------------------------------------------------------------------
127 
128 public:
129 
131  bool loadSourceCode(const char *a_source);
132 
134  bool loadSourceCode(const std::string& a_source);
135 
137  bool loadSourceFile(const std::string& a_fileName);
138 
140  bool compile();
141 
143  bool isCompiled() const { return (m_compiled); }
144 
146  std::string getLog() const { return (m_log); }
147 
149  GLuint getId() const { return (m_id); }
150 
152  std::string getSource() { return (m_source); }
153 
154 
155  //--------------------------------------------------------------------------
156  // STATIC METHODS:
157  //--------------------------------------------------------------------------
158 
159 public:
160 
162  static bool hasOpenGLShaders(cShaderType a_type);
163 
164 
165  //--------------------------------------------------------------------------
166  // PROTECTED MEMBERS:
167  //--------------------------------------------------------------------------
168 
169 protected:
170 
173 
175  GLuint m_id;
176 
178  std::string m_source;
179 
181  std::string m_log;
182 
185 };
186 
187 //------------------------------------------------------------------------------
188 } // namespace chai3d
189 //------------------------------------------------------------------------------
190 
191 //------------------------------------------------------------------------------
192 #endif
193 //------------------------------------------------------------------------------
Definition: CShader.h:84
cShaderType m_type
Shader type (C_VERTEX_SHADER or C_FRAGMENT_SHADER).
Definition: CShader.h:172
std::string m_source
The shader source code (i.e. the GLSL code itself).
Definition: CShader.h:178
std::shared_ptr< cShader > cShaderPtr
Definition: CShader.h:89
std::string getLog() const
This method returns the log file generated from compilation.
Definition: CShader.h:146
Definition: CShader.h:83
Implements mathematical constants.
Implements option settings for CHAI3D.
std::string m_log
Log message following compilation.
Definition: CShader.h:181
This class implements a shader primitive.
Definition: CShader.h:106
Implements utility functions for manipulating strings.
GLuint getId() const
This method returns the shader OpenGL ID.
Definition: CShader.h:149
bool m_compiled
If true then shader was compiled successfully, false otherwise.
Definition: CShader.h:184
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:121
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:118
Definition: CAudioBuffer.cpp:56
cShaderType
Definition: CShader.h:81
bool isCompiled() const
This method returns true if shader has been compiled successfully, false otherwise.
Definition: CShader.h:143
std::string getSource()
This method returns the source code of the shader.
Definition: CShader.h:152
GLuint m_id
Handle ID for the shader.
Definition: CShader.h:175
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