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 <math/vector2d.h>
35 
36 #include <string>
37 #include <deque>
38 
39 namespace KIGFX
40 {
41 class OPENGL_GAL;
42 
45 {
46  SHADER_TYPE_VERTEX = GL_VERTEX_SHADER,
47  SHADER_TYPE_FRAGMENT = GL_FRAGMENT_SHADER,
48  SHADER_TYPE_GEOMETRY = GL_GEOMETRY_SHADER
49 };
50 
51 namespace DETAIL {
52 
53 inline const char* translateStringArg( const std::string& str )
54 {
55  return str.c_str();
56 }
57 
58 inline const char* translateStringArg( const char* str )
59 {
60  return str;
61 }
62 
63 }
64 
65 
76 class SHADER
77 {
78 public:
79 
83  SHADER();
84 
88  virtual ~SHADER();
89 
98  template< typename... Args >
99  bool LoadShaderFromStrings( SHADER_TYPE aShaderType, Args&&... aArgs )
100  {
101  const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
102  return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
103  }
104 
112  bool LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName );
113 
119  bool Link();
120 
124  bool IsLinked() const
125  {
126  return isShaderLinked;
127  }
128 
132  inline void Use()
133  {
134  glUseProgram( programNumber );
135  active = true;
136  }
137 
141  inline void Deactivate()
142  {
143  glUseProgram( 0 );
144  active = false;
145  }
146 
152  inline bool IsActive() const
153  {
154  return active;
155  }
156 
164  void ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
165  GLuint geometryOutputType );
166 
177  int AddParameter( const std::string& aParameterName );
178 
185  void SetParameter( int aParameterNumber, float aValue ) const;
186  void SetParameter( int aParameterNumber, int aValue ) const;
187  void SetParameter( int aParameterNumber, const VECTOR2D& aValue ) const;
188  void SetParameter( int aParameterNumber, float f0, float f1, float f2, float f3 ) const;
189 
196  int GetAttribute( const std::string& aAttributeName ) const;
197 
204  static std::string ReadSource( const std::string& aShaderSourceName );
205 
206 private:
207 
211  bool loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray,
212  size_t aSize );
213 
219  void programInfo( GLuint aProgram );
220 
226  void shaderInfo( GLuint aShader );
227 
228  std::deque<GLuint> shaderNumbers;
229  GLuint programNumber;
232  bool active;
234  GLuint geomInputType;
235  GLuint geomOutputType;
236  std::deque<GLint> parameterLocation;
237 };
238 } // namespace KIGFX
239 
240 #endif /* SHADER_H_ */
bool IsLinked() const
Returns true if shaders are linked correctly.
Definition: shader.h:124
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&... aArgs)
Add a shader and compile the shader sources.
Definition: shader.h:99
SHADER_TYPE
Type definition for the shader.
Definition: shader.h:44
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
int GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:167
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:234
void Use()
Use the shader.
Definition: shader.h:132
void SetParameter(int aParameterNumber, float aValue) const
Set a parameter of the shader.
Definition: shader.cpp:138
bool IsActive() const
Returns the current state of the shader.
Definition: shader.h:152
Fragment shader.
Definition: shader.h:47
bool Link()
Link the shaders.
Definition: shader.cpp:97
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:235
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:173
bool LoadShaderFromFile(SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
Loads one of the built-in shaders and compiles it.
Definition: shader.cpp:79
const string & str
Definition: json11.cpp:596
bool isShaderLinked
Is the shader linked?
Definition: shader.h:231
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:233
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:228
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition: shader.cpp:237
int AddParameter(const std::string &aParameterName)
Add a parameter to the parameter queue.
Definition: shader.cpp:125
void shaderInfo(GLuint aShader)
Get the shader information.
Definition: shader.cpp:194
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:141
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:215
Vertex shader.
Definition: shader.h:46
Class SHADER provides the access to the OpenGL shaders.
Definition: shader.h:76
virtual ~SHADER()
Destructor.
Definition: shader.cpp:55
Geometry shader.
Definition: shader.h:48
SHADER()
Constructor.
Definition: shader.cpp:41
bool isProgramCreated
Flag for program creation.
Definition: shader.h:230
GLuint programNumber
Shader program number.
Definition: shader.h:229
bool active
Is any of shaders used?
Definition: shader.h:232
void ConfigureGeometryShader(GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
Configure the geometry shader - has to be done before linking!
Definition: shader.cpp:88
int PGM_BASE * aProgram
Definition: pcbnew.cpp:184
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:236
const char * translateStringArg(const std::string &str)
Definition: shader.h:53