Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfShaderProgram.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef __glfShaderProgram_hh__
26 #define __glfShaderProgram_hh__
27 
28 #include <Base/Math/Vector.hh>
29 #include <Base/Math/Matrix.hh>
30 #include "glfShader.hh"
31 #include "glfMatrix.hh"
32 
33 namespace BIAS {
34 
35  /**
36  * \brief A shader program composed of several shaders.
37  * \ingroup g_openglframework
38  * \author jkollmann
39  */
40  class BIASOpenGLFramework_EXPORT glfShaderProgram {
41  public:
44 
45  /**
46  * Creates the shader program. After this, you can attach shaders.
47  */
48  void Create();
49 
50  /**
51  * Attaches a shader to the program. Must be called after glfShaderProgram::Create
52  * and before glfShaderProgram::Link.
53  */
54  void AttachShader(const glfShader& shader);
55 
56  /**
57  * Attaches a shader to the program, without the need to create an
58  * instance of the <code>glfShader</code> class.
59  * \param type Either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
60  * \param sourceCode The GLSL source code.
61  */
62  void AttachShaderFromSource(GLenum type, const std::string& sourceCode);
63 
64  /**
65  * Attaches a shader to the program, without the need to create an
66  * instance of the <code>glfShader</code> class. The source code of the
67  * shader is loaded from a file.
68  * \param type Either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
69  * \param fileName Name of file to load the GLSL source from.
70  */
71  void AttachShaderFromFile(GLenum type, const std::string& fileName);
72 
73  /**
74  * Links the attached shaders. After this, the shader program can be used
75  * for rendering.
76  * \attention clears all set uniforms!
77  */
78  void Link();
79 
80  /**
81  * Returns the info log containing information about the linking
82  * of the shader program.
83  * \note If linking the shader programs fails, the info log is also
84  * contained in the message of the thrown exception.
85  */
86  std::string GetInfoLog() const;
87 
88  /** \name Methods to set uniform parameters.
89  \note All double parameters are converted to floats, because OpenGL
90  supports only float uniforms.
91  \attention Program has to be linked successfully.
92  */
93  /** @{ */
94  void SetUniform(const std::string& varName, float value);
95  void SetUniform(const std::string& varName, double value);
96  void SetUniform(const std::string& varName, int value);
97  void SetUniform(const std::string& varName, bool value);
98  void SetUniform(const std::string& varName, int count, int *value);
99  void SetUniform(const std::string& varName, int count, float *value);
100  void SetUniform(const std::string& varName, const BIAS::Vector<float>& v);
101  void SetUniform(const std::string& varName, const BIAS::Vector<double>& v);
102  void SetUniform(const std::string& varName, const BIAS::Vector<int>& v);
103  void SetUniform(const std::string& varName, const BIAS::Matrix<float>& m);
104  void SetUniform(const std::string& varName, const BIAS::Matrix<double>& m);
105  void SetUniform(const std::string& varName, const glfMatrix& m);
106 
107  /** Finds all sampler uniforms and automatically assigns them texture unit indices.
108  **/
109  void AutoSetSamplerUniforms();
110  /** @} */
111 
112 //#ifdef BIAS_HAVE_OPENGL3
113 #ifdef GL_VERSION_3_0
114  /** Binds vertex shader variable to vertex attrib slot.
115  \attention The settings will only be used when programm is (re)linked afterwards!
116  \param indexSlot id for the vertex attribute slot
117  \param VertexAttributeName name of varying variable in vertex shader source
118  **/
119  void BindAttribLocation(GLuint indexSlot, const std::string& VertexAttributeName);
120 #endif
121 
122  /**
123  * Binds the shader program.
124  */
125  void Bind() const;
126 
127  static int GetMaxCombinedTextureUnits();
128 
129  GLuint GetProgramId() {return id_;}
130  GLint GetUniformLocation(const std::string& varName);
131 
132  private:
133  bool IsSamplerType_(GLenum type);
134  GLint GetUniformLocation_(const std::string& varName);
135 
136  GLuint id_;
137 
138  /** An intuitive assignment operator should generate a copy, as long as nobody is willing to implement
139  * the generation of a copy in gl controlled memory this operator cannot be used.
140  */
141  glfShaderProgram& operator=(const glfShaderProgram& b);
142 
143  /** An intuitive copy constructor should generate a copy, as long as nobody is willing to implement
144  * the generation of a copy in gl controlled memory this constructor cannot be used.
145  */
147 
148 
149  std::vector<glfShader*> shaders_attached_from_source_;
150  std::vector<GLuint> shaders_attached_as_object_;
151 
152  };
153 
154 } // namespace BIAS
155 
156 #endif // __glfShaderProgram_hh__
A shader program composed of several shaders.
A GLSL vertex shader or fragment shader, which must be linked in a shader program.
Definition: glfShader.hh:39
matrix class with arbitrary size, indexing is row major.
A 4x4 matrix in native OpenGL format.
Definition: glfMatrix.hh:41