KiCad PCB EDA Suite
shader.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
6  *
7  * Graphics Abstraction Layer (GAL) for OpenGL
8  *
9  * Shader class
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #ifndef SHADER_H_
30 #define SHADER_H_
31 
32 #include <GL/glew.h>
33 
34 #include <string>
35 #include <deque>
36 
37 namespace KIGFX
38 {
39 class OPENGL_GAL;
40 
43 {
44  SHADER_TYPE_VERTEX = GL_VERTEX_SHADER,
45  SHADER_TYPE_FRAGMENT = GL_FRAGMENT_SHADER,
46  SHADER_TYPE_GEOMETRY = GL_GEOMETRY_SHADER
47 };
48 
49 namespace DETAIL {
50 
51 inline const char* translateStringArg( const std::string& str )
52 {
53  return str.c_str();
54 }
55 
56 inline const char* translateStringArg( const char* str )
57 {
58  return str;
59 }
60 
61 }
62 
63 
74 class SHADER
75 {
76 public:
77 
81  SHADER();
82 
86  virtual ~SHADER();
87 
96  template< typename... Args >
97  bool LoadShaderFromStrings( SHADER_TYPE aShaderType, Args&&... aArgs )
98  {
99  const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
100  return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
101  }
102 
110  bool LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName );
111 
117  bool Link();
118 
122  bool IsLinked() const
123  {
124  return isShaderLinked;
125  }
126 
130  inline void Use()
131  {
132  glUseProgram( programNumber );
133  active = true;
134  }
135 
139  inline void Deactivate()
140  {
141  glUseProgram( 0 );
142  active = false;
143  }
144 
150  inline bool IsActive() const
151  {
152  return active;
153  }
154 
162  void ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
163  GLuint geometryOutputType );
164 
175  int AddParameter( const std::string& aParameterName );
176 
183  void SetParameter( int aParameterNumber, float aValue ) const;
184  void SetParameter( int aParameterNumber, int aValue ) const;
185  void SetParameter( int aParameterNumber, float f0, float f1, float f2, float f3 ) const;
186 
193  int GetAttribute( const std::string& aAttributeName ) const;
194 
201  static std::string ReadSource( const std::string& aShaderSourceName );
202 
203 private:
204 
208  bool loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray,
209  size_t aSize );
210 
216  void programInfo( GLuint aProgram );
217 
223  void shaderInfo( GLuint aShader );
224 
225  std::deque<GLuint> shaderNumbers;
226  GLuint programNumber;
229  bool active;
231  GLuint geomInputType;
232  GLuint geomOutputType;
233  std::deque<GLint> parameterLocation;
234 };
235 } // namespace KIGFX
236 
237 #endif /* SHADER_H_ */
bool IsActive() const
Returns the current state of the shader.
Definition: shader.h:150
bool IsLinked() const
Returns true if shaders are linked correctly.
Definition: shader.h:122
SHADER_TYPE
Type definition for the shader.
Definition: shader.h:42
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:231
void Use()
Use the shader.
Definition: shader.h:130
void SetParameter(int aParameterNumber, float aValue) const
Set a parameter of the shader.
Definition: shader.cpp:137
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&...aArgs)
Add a shader and compile the shader sources.
Definition: shader.h:97
Fragment shader.
Definition: shader.h:45
bool Link()
Link the shaders.
Definition: shader.cpp:96
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:232
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:166
bool LoadShaderFromFile(SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
Loads one of the built-in shaders and compiles it.
Definition: shader.cpp:78
bool isShaderLinked
Is the shader linked?
Definition: shader.h:228
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:230
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:225
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition: shader.cpp:230
int AddParameter(const std::string &aParameterName)
Add a parameter to the parameter queue.
Definition: shader.cpp:124
void shaderInfo(GLuint aShader)
Get the shader information.
Definition: shader.cpp:187
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:139
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:208
Vertex shader.
Definition: shader.h:44
Class SHADER provides the access to the OpenGL shaders.
Definition: shader.h:74
int GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:160
virtual ~SHADER()
Destructor.
Definition: shader.cpp:54
Geometry shader.
Definition: shader.h:46
SHADER()
Constructor.
Definition: shader.cpp:40
bool isProgramCreated
Flag for program creation.
Definition: shader.h:227
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool active
Is any of shaders used?
Definition: shader.h:229
void ConfigureGeometryShader(GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
Configure the geometry shader - has to be done before linking!
Definition: shader.cpp:87
int PGM_BASE * aProgram
Definition: pcbnew.cpp:205
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:233
const char * translateStringArg(const std::string &str)
Definition: shader.h:51