shader.h 6.5 KB
Newer Older
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
/*
 * 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>

37
namespace KIGFX
38 39 40 41
{
class OPENGL_GAL;

/// Type definition for the shader
Maciej Suminski's avatar
Maciej Suminski committed
42
enum SHADER_TYPE
43
{
44
    SHADER_TYPE_VERTEX	 = GL_VERTEX_SHADER,    ///< Vertex shader
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
    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();

    /**
74 75 76 77 78 79
     * @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.
     */
Maciej Suminski's avatar
Maciej Suminski committed
80
    bool LoadBuiltinShader( unsigned int aShaderNumber, SHADER_TYPE aShaderType );
81 82 83

    /**
     * @brief Loads one of the built-in shaders and compiles it.
84 85 86
     *
     * @param aShaderSourceName is the shader source file name.
     * @param aShaderType is the type of the shader.
87
     * @return True in case of success, false otherwise.
88
     */
Maciej Suminski's avatar
Maciej Suminski committed
89
    bool LoadShaderFromFile( const std::string& aShaderSourceName, SHADER_TYPE aShaderType );
90 91

    /**
92 93
     * @brief Link the shaders.
     *
94
     * @return true in case of success, false otherwise.
95
     */
96
    bool Link();
97 98

    /**
99
     * @brief Use the shader.
100
     */
101 102 103 104 105
    inline void Use()
    {
        glUseProgram( programNumber );
        active = true;
    }
106 107 108 109

    /**
     * @brief Deactivate the shader and use the default OpenGL program.
     */
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    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;
    }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    /**
     * @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.
144
     * @return the added parameter location.
145
     */
146
    int AddParameter( const std::string& aParameterName );
147 148 149 150 151 152 153

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

157 158 159 160 161 162
    /**
     * @brief Gets an attribute location.
     *
     * @param aAttributeName is the name of the attribute.
     * @return the location.
     */
163
    int GetAttribute( std::string aAttributeName ) const;
164

165 166 167 168 169 170 171
private:

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

174
    /**
175 176 177 178 179
     * @brief Get the shader information.
     *
     * @param aShader is the shader number.
     */
    void shaderInfo( GLuint aShader );
180

181 182 183 184 185 186
    /**
     * @brief Read the shader source file
     *
     * @param aShaderSourceName is the shader source file name.
     * @return the source as string
     */
187 188 189 190 191 192 193 194 195
    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.
     */
Maciej Suminski's avatar
Maciej Suminski committed
196
    bool addSource( const std::string& aShaderSource, SHADER_TYPE aShaderType );
197 198 199 200 201

    std::deque<GLuint>  shaderNumbers;      ///< Shader number list
    GLuint              programNumber;      ///< Shader program number
    bool                isProgramCreated;   ///< Flag for program creation
    bool                isShaderLinked;     ///< Is the shader linked?
202
    bool                active;             ///< Is any of shaders used?
203 204 205 206 207
    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
};
208
} // namespace KIGFX
209 210

#endif /* SHADER_H_ */