Shader.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2018 Laurent Gomila (laurent@sfml-dev.org)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef SFML_SHADER_HPP
26 #define SFML_SHADER_HPP
27 
29 // Headers
31 #include <SFML/Graphics/Export.hpp>
32 #include <SFML/Graphics/Glsl.hpp>
33 #include <SFML/Window/GlResource.hpp>
34 #include <SFML/System/NonCopyable.hpp>
35 #include <SFML/System/Vector2.hpp>
36 #include <SFML/System/Vector3.hpp>
37 #include <map>
38 #include <string>
39 
40 
41 namespace sf
42 {
43 class Color;
44 class InputStream;
45 class Texture;
46 class Transform;
47 
52 class SFML_GRAPHICS_API Shader : GlResource, NonCopyable
53 {
54 public:
55 
60  enum Type
61  {
64  Fragment
65  };
66 
74  struct CurrentTextureType {};
75 
83 
84 public:
85 
92  Shader();
93 
98  ~Shader();
99 
119  bool loadFromFile(const std::string& filename, Type type);
120 
140  bool loadFromFile(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename);
141 
162  bool loadFromFile(const std::string& vertexShaderFilename, const std::string& geometryShaderFilename, const std::string& fragmentShaderFilename);
163 
182  bool loadFromMemory(const std::string& shader, Type type);
183 
203  bool loadFromMemory(const std::string& vertexShader, const std::string& fragmentShader);
204 
225  bool loadFromMemory(const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader);
226 
245  bool loadFromStream(InputStream& stream, Type type);
246 
266  bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream);
267 
288  bool loadFromStream(InputStream& vertexShaderStream, InputStream& geometryShaderStream, InputStream& fragmentShaderStream);
289 
297  void setUniform(const std::string& name, float x);
298 
306  void setUniform(const std::string& name, const Glsl::Vec2& vector);
307 
315  void setUniform(const std::string& name, const Glsl::Vec3& vector);
316 
333  void setUniform(const std::string& name, const Glsl::Vec4& vector);
334 
342  void setUniform(const std::string& name, int x);
343 
351  void setUniform(const std::string& name, const Glsl::Ivec2& vector);
352 
360  void setUniform(const std::string& name, const Glsl::Ivec3& vector);
361 
377  void setUniform(const std::string& name, const Glsl::Ivec4& vector);
378 
386  void setUniform(const std::string& name, bool x);
387 
395  void setUniform(const std::string& name, const Glsl::Bvec2& vector);
396 
404  void setUniform(const std::string& name, const Glsl::Bvec3& vector);
405 
413  void setUniform(const std::string& name, const Glsl::Bvec4& vector);
414 
422  void setUniform(const std::string& name, const Glsl::Mat3& matrix);
423 
431  void setUniform(const std::string& name, const Glsl::Mat4& matrix);
432 
463  void setUniform(const std::string& name, const Texture& texture);
464 
486  void setUniform(const std::string& name, CurrentTextureType);
487 
496  void setUniformArray(const std::string& name, const float* scalarArray, std::size_t length);
497 
506  void setUniformArray(const std::string& name, const Glsl::Vec2* vectorArray, std::size_t length);
507 
516  void setUniformArray(const std::string& name, const Glsl::Vec3* vectorArray, std::size_t length);
517 
526  void setUniformArray(const std::string& name, const Glsl::Vec4* vectorArray, std::size_t length);
527 
536  void setUniformArray(const std::string& name, const Glsl::Mat3* matrixArray, std::size_t length);
537 
546  void setUniformArray(const std::string& name, const Glsl::Mat4* matrixArray, std::size_t length);
547 
554  SFML_DEPRECATED void setParameter(const std::string& name, float x);
555 
562  SFML_DEPRECATED void setParameter(const std::string& name, float x, float y);
563 
570  SFML_DEPRECATED void setParameter(const std::string& name, float x, float y, float z);
571 
578  SFML_DEPRECATED void setParameter(const std::string& name, float x, float y, float z, float w);
579 
586  SFML_DEPRECATED void setParameter(const std::string& name, const Vector2f& vector);
587 
594  SFML_DEPRECATED void setParameter(const std::string& name, const Vector3f& vector);
595 
602  SFML_DEPRECATED void setParameter(const std::string& name, const Color& color);
603 
610  SFML_DEPRECATED void setParameter(const std::string& name, const Transform& transform);
611 
618  SFML_DEPRECATED void setParameter(const std::string& name, const Texture& texture);
619 
626  SFML_DEPRECATED void setParameter(const std::string& name, CurrentTextureType);
627 
638  unsigned int getNativeHandle() const;
639 
661  static void bind(const Shader* shader);
662 
673  static bool isAvailable();
674 
692  static bool isGeometryAvailable();
693 
694 private:
695 
709  bool compile(const char* vertexShaderCode, const char* geometryShaderCode, const char* fragmentShaderCode);
710 
718  void bindTextures() const;
719 
728  int getUniformLocation(const std::string& name);
729 
737  struct UniformBinder;
738 
740  // Types
742  typedef std::map<int, const Texture*> TextureTable;
743  typedef std::map<std::string, int> UniformTable;
744 
746  // Member data
748  unsigned int m_shaderProgram;
749  int m_currentTexture;
750  TextureTable m_textures;
751  UniformTable m_uniforms;
752 };
753 
754 } // namespace sf
755 
756 
757 #endif // SFML_SHADER_HPP
758 
759 
implementation defined Bvec4
4D bool vector (bvec4 in GLSL)
Definition: Glsl.hpp:130
Abstract class for custom file input streams.
Definition: InputStream.hpp:41
Image living on the graphics card that can be used for drawing.
Definition: Texture.hpp:48
implementation defined Ivec4
4D int vector (ivec4 in GLSL)
Definition: Glsl.hpp:124
implementation defined Mat4
4x4 float matrix (mat4 in GLSL)
Definition: Glsl.hpp:181
Utility class for manipulating RGBA colors.
Definition: Color.hpp:40
Utility template class for manipulating 3-dimensional vectors.
Definition: Vector3.hpp:37
implementation defined Vec4
4D float vector (vec4 in GLSL)
Definition: Glsl.hpp:110
implementation defined Mat3
3x3 float matrix (mat3 in GLSL)
Definition: Glsl.hpp:155
Define a 3x3 transform matrix.
Definition: Transform.hpp:42
Special type that can be passed to setUniform(), and that represents the texture of the object being ...
Definition: Shader.hpp:74
Geometry shader.
Definition: Shader.hpp:63
Utility class that makes any derived class non-copyable.
Definition: NonCopyable.hpp:41
static CurrentTextureType CurrentTexture
Represents the texture of the object being drawn.
Definition: Shader.hpp:82
Vertex shader
Definition: Shader.hpp:62
Shader class (vertex, geometry and fragment)
Definition: Shader.hpp:52
Base class for classes that require an OpenGL context.
Definition: GlResource.hpp:46
Type
Types of shaders.
Definition: Shader.hpp:60