summaryrefslogtreecommitdiff
path: root/include/gal/opengl/shader.h
blob: a83de846e333486fd2f10656880457e82954ae8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
 * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
 *
 * Graphics Abstraction Layer (GAL) for OpenGL
 *
 * Shader class
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef SHADER_H_
#define SHADER_H_

#include <GL/glew.h>

#include <string>
#include <deque>

namespace KIGFX
{
class OPENGL_GAL;

/// Type definition for the shader
enum SHADER_TYPE
{
    SHADER_TYPE_VERTEX	 = GL_VERTEX_SHADER,    ///< Vertex shader
    SHADER_TYPE_FRAGMENT = GL_FRAGMENT_SHADER,  ///< Fragment shader
    SHADER_TYPE_GEOMETRY = GL_GEOMETRY_SHADER   ///< Geometry shader
};

/**
 * @brief Class SHADER provides the access to the OpenGL shaders.
 *
 * The purpose of this class is advanced drawing with OpenGL. One example is using the pixel
 * shader for drawing exact circles or for anti-aliasing. This class supports vertex, geometry
 * and fragment shaders.
 * <br>
 * Make sure that the hardware supports these features. This can be identified with the "GLEW"
 * library.
 */
class SHADER
{
public:

    /**
     * @brief Constructor
     */
    SHADER();

    /**
     * @brief Destructor
     */
    virtual ~SHADER();

    /**
     * @brief Loads one of the built-in shaders and compiles it.
     *
     * @param aShaderNumber is the shader number (indexing from 0).
     * @param aShaderType is the type of the shader.
     * @return True in case of success, false otherwise.
     */
    bool LoadBuiltinShader( unsigned int aShaderNumber, SHADER_TYPE aShaderType );

    /**
     * @brief Loads one of the built-in shaders and compiles it.
     *
     * @param aShaderSourceName is the shader source file name.
     * @param aShaderType is the type of the shader.
     * @return True in case of success, false otherwise.
     */
    bool LoadShaderFromFile( const std::string& aShaderSourceName, SHADER_TYPE aShaderType );

    /**
     * @brief Link the shaders.
     *
     * @return true in case of success, false otherwise.
     */
    bool Link();

    /**
     * @brief Use the shader.
     */
    inline void Use()
    {
        glUseProgram( programNumber );
        active = true;
    }

    /**
     * @brief Deactivate the shader and use the default OpenGL program.
     */
    inline void Deactivate()
    {
        glUseProgram( 0 );
        active = false;
    }

    /**
     * @brief Returns the current state of the shader.
     *
     * @return True if any of shaders is enabled.
     */
    inline bool IsActive() const
    {
        return active;
    }

    /**
     * @brief Configure the geometry shader - has to be done before linking!
     *
     * @param maxVertices is the maximum of vertices to be generated.
     * @param geometryInputType is the input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]
     * @param geometryOutputType is the output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]
     */
    void ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
                                  GLuint geometryOutputType );

    /**
     * @brief Add a parameter to the parameter queue.
     *
     * To communicate with the shader use this function to set up the names for the uniform
     * variables. These are queued in a list and can be assigned with the SetParameter(..)
     * method using the queue position.
     *
     * @param aParameterName is the name of the parameter.
     * @return the added parameter location.
     */
    int AddParameter( const std::string& aParameterName );

    /**
     * @brief Set a parameter of the shader.
     *
     * @param aParameterNumber is the number of the parameter.
     * @param aValue is the value of the parameter.
     */
    void SetParameter( int aParameterNumber, float aValue ) const;
    void SetParameter( int aParameterNumber, int aValue ) const;

    /**
     * @brief Gets an attribute location.
     *
     * @param aAttributeName is the name of the attribute.
     * @return the location.
     */
    int GetAttribute( std::string aAttributeName ) const;

private:

    /**
     * @brief Get the shader program information.
     *
     * @param aProgram is the program number.
     */
    void programInfo( GLuint aProgram );

    /**
     * @brief Get the shader information.
     *
     * @param aShader is the shader number.
     */
    void shaderInfo( GLuint aShader );

    /**
     * @brief Read the shader source file
     *
     * @param aShaderSourceName is the shader source file name.
     * @return the source as string
     */
    std::string readSource( std::string aShaderSourceName );

    /**
     * @brief Add a shader and compile the shader sources.
     *
     * @param aShaderSource is the shader source content.
     * @param aShaderType is the type of the shader.
     * @return True in case of success, false otherwise.
     */
    bool addSource( const std::string& aShaderSource, SHADER_TYPE aShaderType );

    std::deque<GLuint>  shaderNumbers;      ///< Shader number list
    GLuint              programNumber;      ///< Shader program number
    bool                isProgramCreated;   ///< Flag for program creation
    bool                isShaderLinked;     ///< Is the shader linked?
    bool                active;             ///< Is any of shaders used?
    GLuint              maximumVertices;    ///< The maximum of vertices to be generated
    GLuint              geomInputType;      ///< Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]
    GLuint              geomOutputType;     ///< Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]
    std::deque<GLint>   parameterLocation;  ///< Location of the parameter
};
} // namespace KIGFX

#endif /* SHADER_H_ */