ShaderProgram.hh 10.1 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6 7
#ifndef ACGL_OPENGL_OBJECTS_SHADERPROGRAM_HH
#define ACGL_OPENGL_OBJECTS_SHADERPROGRAM_HH
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9 10
#include <ACGL/ACGL.hh>

11
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
12 13 14
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Objects/Shader.hh>
#include <ACGL/OpenGL/Objects/Texture.hh>
15
#include <ACGL/Math/Math.hh>
Robert Menzel's avatar
Robert Menzel committed
16 17

#include <vector>
Robert Menzel's avatar
Robert Menzel committed
18
#include <tr1/memory>
Robert Menzel's avatar
Robert Menzel committed
19 20

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
21
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
22 23 24

class ShaderProgram
{
25
    ACGL_NOT_COPYABLE(ShaderProgram)
26

Robert Menzel's avatar
Robert Menzel committed
27 28 29 30
    // ===================================================================================================== \/
    // ============================================================================================ TYPEDEFS \/
    // ===================================================================================================== \/
public:
31
    typedef std::vector< ConstSharedShader > ConstSharedShaderVec;
Robert Menzel's avatar
Robert Menzel committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    ShaderProgram(void)
    :   mContext(0),
        mShaders()
    {
        mContext = glCreateProgram();
    }

    virtual ~ShaderProgram(void)
    {
        // "DeleteProgram will silently ignore the value zero." - GL Spec
        glDeleteProgram(mContext);
    }

    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
54 55
    inline       GLuint                getContext (void) const { return mContext; }
    inline const ConstSharedShaderVec& getShaders (void) const { return mShaders; }
Robert Menzel's avatar
Robert Menzel committed
56 57 58 59 60

    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
public:
61 62 63 64
    inline GLint getUniformLocation      (const std::string& _nameInShader) const { return glGetUniformLocation (mContext, _nameInShader.c_str()); }
    inline GLint getAttributeLocation    (const std::string& _nameInShader) const { return glGetAttribLocation  (mContext, _nameInShader.c_str()); }
    inline GLint getFragmentDataLocation (const std::string& _nameInShader) const { return glGetFragDataLocation(mContext, _nameInShader.c_str()); }

65 66 67
    inline void bindAttributeLocation    (const std::string& _nameInShader, GLuint _location) const { glBindAttribLocation   (mContext, _location, _nameInShader.c_str()); }
    inline void bindFragmentDataLocation (const std::string& _nameInShader, GLuint _location) const { glBindFragDataLocation (mContext, _location, _nameInShader.c_str()); }

68
    inline void use(void) const { glUseProgram(mContext); }
Robert Menzel's avatar
Robert Menzel committed
69

70
    inline void attachShader(const ConstSharedShader& _shader)
Robert Menzel's avatar
Robert Menzel committed
71 72 73 74 75 76 77
    {
        mShaders.push_back(_shader);
        glAttachShader(mContext, _shader->getContext());
    }

    bool link (void) const;

78 79 80
    inline void setUniform (GLint _location, GLint _v)   const { glUniform1i (_location, _v); }
    inline void setUniform (GLint _location, GLfloat _v) const { glUniform1f (_location, _v); }

Robert Menzel's avatar
Robert Menzel committed
81 82 83
    inline void setUniform (GLint _location, const glm::vec2& _v) const { glUniform2fv(_location, 1, glm::value_ptr(_v)); }
    inline void setUniform (GLint _location, const glm::vec3& _v) const { glUniform3fv(_location, 1, glm::value_ptr(_v)); }
    inline void setUniform (GLint _location, const glm::vec4& _v) const { glUniform4fv(_location, 1, glm::value_ptr(_v)); }
84 85 86 87

    inline void setUniform (GLint _location, const glm::mat2& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix2fv(_location, 1, _transpose, glm::value_ptr(_v)); }
    inline void setUniform (GLint _location, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix3fv(_location, 1, _transpose, glm::value_ptr(_v)); }
    inline void setUniform (GLint _location, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { glUniformMatrix4fv(_location, 1, _transpose, glm::value_ptr(_v)); }
Robert Menzel's avatar
Robert Menzel committed
88

89
    inline void setTexture (GLint _location, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glUniform1i(_location, _unit); _texture->bind(_unit); }
Robert Menzel's avatar
Robert Menzel committed
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    //DSA versions:
    inline void setProgramUniform (GLint _location, GLint _v)   const { glProgramUniform1iEXT (mContext, _location, _v); }
    inline void setProgramUniform (GLint _location, GLfloat _v) const { glProgramUniform1fEXT (mContext, _location, _v); }

    inline void setProgramUniform (GLint _location, const glm::vec2& _v) const { glProgramUniform2fvEXT(mContext, _location, 1, glm::value_ptr(_v)); }
    inline void setProgramUniform (GLint _location, const glm::vec3& _v) const { glProgramUniform3fvEXT(mContext, _location, 1, glm::value_ptr(_v)); }
    inline void setProgramUniform (GLint _location, const glm::vec4& _v) const { glProgramUniform4fvEXT(mContext, _location, 1, glm::value_ptr(_v)); }

    inline void setProgramUniform (GLint _location, const glm::mat2& _v, GLboolean _transpose = GL_FALSE) const { glProgramUniformMatrix2fvEXT(mContext, _location, 1, _transpose, glm::value_ptr(_v)); }
    inline void setProgramUniform (GLint _location, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { glProgramUniformMatrix3fvEXT(mContext, _location, 1, _transpose, glm::value_ptr(_v)); }
    inline void setProgramUniform (GLint _location, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { glProgramUniformMatrix4fvEXT(mContext, _location, 1, _transpose, glm::value_ptr(_v)); }

    inline void setTextureDSA (GLint _location, const ConstSharedTexture& _texture, GLenum _unit = 0) const { glProgramUniform1iEXT(mContext, _location, _unit); _texture->bind(_unit); }

Robert Menzel's avatar
Robert Menzel committed
106 107 108 109
    // =================================================================================================== \/
    // ============================================================================== HIGH LEVEL FUNCTIONS \/
    // =================================================================================================== \/

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    inline void setUniform (const std::string& _nameInShader, GLint _v)   const { setUniform( getUniformLocation(_nameInShader), _v); }
    inline void setUniform (const std::string& _nameInShader, GLfloat _v) const { setUniform( getUniformLocation(_nameInShader), _v); }

    inline void setUniform (const std::string& _nameInShader, const glm::vec2& _v) const { setUniform( getUniformLocation(_nameInShader), _v); }
    inline void setUniform (const std::string& _nameInShader, const glm::vec3& _v) const { setUniform( getUniformLocation(_nameInShader), _v); }
    inline void setUniform (const std::string& _nameInShader, const glm::vec4& _v) const { setUniform( getUniformLocation(_nameInShader), _v); }

    inline void setUniform (const std::string& _nameInShader, const glm::mat2& _v, GLboolean _transpose = GL_FALSE) const { setUniform( getUniformLocation(_nameInShader), _v); }
    inline void setUniform (const std::string& _nameInShader, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { setUniform( getUniformLocation(_nameInShader), _v); }
    inline void setUniform (const std::string& _nameInShader, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { setUniform( getUniformLocation(_nameInShader), _v); }

    inline void setTexture (const std::string& _nameInShader, const ConstSharedTexture& _texture, GLenum _unit = 0) const { setUniform( getUniformLocation(_nameInShader), (GLint) _unit); _texture->bind(_unit); }


    //DSA versions:
    inline void setProgramUniform (const std::string& _nameInShader, GLint _v)   const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
    inline void setProgramUniform (const std::string& _nameInShader, GLfloat _v) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }

    inline void setProgramUniform (const std::string& _nameInShader, const glm::vec2& _v) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
    inline void setProgramUniform (const std::string& _nameInShader, const glm::vec3& _v) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
    inline void setProgramUniform (const std::string& _nameInShader, const glm::vec4& _v) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
131

132 133 134
    inline void setProgramUniform (const std::string& _nameInShader, const glm::mat2& _v, GLboolean _transpose = GL_FALSE) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
    inline void setProgramUniform (const std::string& _nameInShader, const glm::mat3& _v, GLboolean _transpose = GL_FALSE) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
    inline void setProgramUniform (const std::string& _nameInShader, const glm::mat4& _v, GLboolean _transpose = GL_FALSE) const { setProgramUniform( getUniformLocation(_nameInShader), _v); }
135

136
    inline void setTextureDSA (const std::string& _nameInShader, const ConstSharedTexture& _texture, GLenum _unit = 0) const { setProgramUniform( getUniformLocation(_nameInShader), (GLint) _unit); _texture->bind(_unit); }
Robert Menzel's avatar
Robert Menzel committed
137 138 139 140 141 142


    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
protected:
143 144
    GLuint               mContext;
    ConstSharedShaderVec mShaders;
Robert Menzel's avatar
Robert Menzel committed
145 146
};

147
ACGL_SHARED_TYPEDEF(ShaderProgram)
Robert Menzel's avatar
Robert Menzel committed
148 149

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
150 151
} // ACGL

152
#endif // ACGL_OPENGL_OBJECTS_SHADERPROGRAM_HH