SFML 3.1.0
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
sf::Shader Class Reference
Graphics module

Shader class (vertex, geometry and fragment). More...

`#include <SFML/Graphics/Shader.hpp>`

Inheritance diagram for sf::Shader:

## Classes

struct | CurrentTextureType
| Special type that can be passed to setUniform(), and that represents the texture of the object being drawn. More...

## Public Types

enum class | Type { Vertex , Geometry , Fragment }
| Types of shaders. More...

## Public Member Functions

| Shader ()=default
| Default constructor.
| ~Shader ()
| Destructor.
| Shader (const Shader &)=delete
| Deleted copy constructor.
Shader & | operator= (const Shader &)=delete
| Deleted copy assignment.
| Shader (Shader &&source) noexcept
| Move constructor.
Shader & | operator= (Shader &&right) noexcept
| Move assignment.
| Shader (const std::filesystem::path &filename, Type type)
| Construct from a shader file.
| Shader (const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &fragmentShaderFilename)
| Construct from vertex and fragment shader files.
| Shader (const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &geometryShaderFilename, const std::filesystem::path &fragmentShaderFilename)
| Construct from vertex, geometry and fragment shader files.
| Shader (std::string_view shader, Type type)
| Construct from shader in memory.
| Shader (std::string_view vertexShader, std::string_view fragmentShader)
| Construct from vertex and fragment shaders in memory.
| Shader (std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader)
| Construct from vertex, geometry and fragment shaders in memory.
| Shader (InputStream &stream, Type type)
| Construct from a shader stream.
| Shader (InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
| Construct from vertex and fragment shader streams.
| Shader (InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
| Construct from vertex, geometry and fragment shader streams.
bool | loadFromFile (const std::filesystem::path &filename, Type type)
| Load the vertex, geometry or fragment shader from a file.
bool | loadFromFile (const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &fragmentShaderFilename)
| Load both the vertex and fragment shaders from files.
bool | loadFromFile (const std::filesystem::path &vertexShaderFilename, const std::filesystem::path &geometryShaderFilename, const std::filesystem::path &fragmentShaderFilename)
| Load the vertex, geometry and fragment shaders from files.
bool | loadFromMemory (std::string_view shader, Type type)
| Load the vertex, geometry or fragment shader from a source code in memory.
bool | loadFromMemory (std::string_view vertexShader, std::string_view fragmentShader)
| Load both the vertex and fragment shaders from source codes in memory.
bool | loadFromMemory (std::string_view vertexShader, std::string_view geometryShader, std::string_view fragmentShader)
| Load the vertex, geometry and fragment shaders from source codes in memory.
bool | loadFromStream (InputStream &stream, Type type)
| Load the vertex, geometry or fragment shader from a custom stream.
bool | loadFromStream (InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
| Load both the vertex and fragment shaders from custom streams.
bool | loadFromStream (InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
| Load the vertex, geometry and fragment shaders from custom streams.
void | setUniform (const std::string &name, float x)
| Specify value for `float` uniform.
void | setUniform (const std::string &name, Glsl::Vec2 vector)
| Specify value for `vec2` uniform.
void | setUniform (const std::string &name, const Glsl::Vec3 &vector)
| Specify value for `vec3` uniform.
void | setUniform (const std::string &name, const Glsl::Vec4 &vector)
| Specify value for `vec4` uniform.
void | setUniform (const std::string &name, int x)
| Specify value for `int` uniform.
void | setUniform (const std::string &name, Glsl::Ivec2 vector)
| Specify value for `ivec2` uniform.
void | setUniform (const std::string &name, const Glsl::Ivec3 &vector)
| Specify value for `ivec3` uniform.
void | setUniform (const std::string &name, const Glsl::Ivec4 &vector)
| Specify value for `ivec4` uniform.
void | setUniform (const std::string &name, bool x)
| Specify value for `bool` uniform.
void | setUniform (const std::string &name, Glsl::Bvec2 vector)
| Specify value for `bvec2` uniform.
void | setUniform (const std::string &name, const Glsl::Bvec3 &vector)
| Specify value for `bvec3` uniform.
void | setUniform (const std::string &name, const Glsl::Bvec4 &vector)
| Specify value for `bvec4` uniform.
void | setUniform (const std::string &name, const Glsl::Mat3 &matrix)
| Specify value for `mat3` matrix.
void | setUniform (const std::string &name, const Glsl::Mat4 &matrix)
| Specify value for `mat4` matrix.
void | setUniform (const std::string &name, const Texture &texture)
| Specify a texture as `sampler2D` uniform.
void | setUniform (const std::string &name, const Texture &&texture)=delete
| Disallow setting from a temporary texture.
void | setUniform (const std::string &name, CurrentTextureType)
| Specify current texture as `sampler2D` uniform.
void | setUniformArray (const std::string &name, const float *scalarArray, std::size_t length)
| Specify values for `float`[] array uniform.
void | setUniformArray (const std::string &name, const Glsl::Vec2 *vectorArray, std::size_t length)
| Specify values for `vec2`[] array uniform.
void | setUniformArray (const std::string &name, const Glsl::Vec3 *vectorArray, std::size_t length)
| Specify values for `vec3`[] array uniform.
void | setUniformArray (const std::string &name, const Glsl::Vec4 *vectorArray, std::size_t length)
| Specify values for `vec4`[] array uniform.
void | setUniformArray (const std::string &name, const Glsl::Mat3 *matrixArray, std::size_t length)
| Specify values for `mat3`[] array uniform.
void | setUniformArray (const std::string &name, const Glsl::Mat4 *matrixArray, std::size_t length)
| Specify values for `mat4`[] array uniform.
unsigned int | getNativeHandle () const
| Get the underlying OpenGL handle of the shader.

## Static Public Member Functions

static void | bind (const Shader *shader)
| Bind a shader for rendering.
static bool | isAvailable ()
| Tell whether or not the system supports shaders.
static bool | isGeometryAvailable ()
| Tell whether or not the system supports geometry shaders.

## Static Public Attributes

static CurrentTextureType | CurrentTexture
| Represents the texture of the object being drawn.

## Detailed Description

Shader class (vertex, geometry and fragment).

Shaders are programs written using a specific language, executed directly by the graphics card and allowing to apply real-time operations to the rendered entities.

There are three kinds of shaders:

- Vertex shaders, that process vertices
- Geometry shaders, that process primitives
- Fragment (pixel) shaders, that process pixels

A sf::Shader can be composed of either a vertex shader alone, a geometry shader alone, a fragment shader alone, or any combination of them. (see the variants of the load functions).

Shaders are written in GLSL, which is a C-like language dedicated to OpenGL shaders. You'll probably need to learn its basics before writing your own shaders for SFML.

Like any C/C++ program, a GLSL shader has its own variables called uniforms that you can set from your C++ application. sf::Shader handles different types of uniforms:

- scalars: `float`, `int`, `bool`
- vectors (2, 3 or 4 components)
- matrices (3x3 or 4x4)
- samplers (textures)

Some SFML-specific types can be converted:

- sf::Color as a 4D vector (`vec4`)
- sf::Transform as matrices (`mat3` or `mat4`)

Every uniform variable in a shader can be set through one of the setUniform() or setUniformArray() overloads. For example, if you have a shader with the following uniforms:

uniform float offset;
uniform vec3 point;
uniform vec4 color;
uniform mat4 matrix;
uniform sampler2D overlay;
uniform sampler2D current;

You can set their values from C++ code as follows, using the types defined in the sf::Glsl namespace:

shader.setUniform("offset", 2.f);
shader.setUniform("point", sf::Vector3f(0.5f, 0.8f, 0.3f));
shader.setUniform("color", sf::Glsl::Vec4(color)); // color is a sf::Color
shader.setUniform("matrix", sf::Glsl::Mat4(transform)); // transform is a sf::Transform
shader.setUniform("overlay", texture); // texture is a sf::Texture
shader.setUniform("current", sf::Shader::CurrentTexture);
sf::Shader::CurrentTexture
static CurrentTextureType CurrentTexture
Represents the texture of the object being drawn.
Definition Shader.hpp:85
sf::Glsl::Vec4
ImplementationDefined Vec4
4D float vector (vec4 in GLSL)
Definition Glsl.hpp:107
sf::Glsl::Mat4
ImplementationDefined Mat4
4x4 float matrix (mat4 in GLSL)
Definition Glsl.hpp:178
sf::Vector3f
Vector3< float > Vector3f
Definition Vector3.hpp:309

The special Shader::CurrentTexture argument maps the given `sampler2D` uniform to the current texture of the object being drawn (which cannot be known in advance).

To apply a shader to a drawable, you must pass it as an additional parameter to the RenderWindow::draw function:

window.draw(sprite, &shader);

... which is in fact just a shortcut for this:

sf::RenderStates states;
states.shader = &shader;
window.draw(sprite, states);
sf::RenderStates
Define the states used for drawing to a RenderTarget.
Definition RenderStates.hpp:48
sf::RenderStates::shader
const Shader * shader
Shader.
Definition RenderStates.hpp:136

In the code above we pass a pointer to the shader, because it may be null (which means "no shader").

Shaders can be used on any drawable, but some combinations are not interesting. For example, using a vertex shader on a sf::Sprite is limited because there are only 4 vertices, the sprite would have to be subdivided in order to apply wave effects. Another bad example is a fragment shader with sf::Text: the texture of the text is not the actual text that you see on screen, it is a big texture containing all the characters of the font in an arbitrary order; thus, texture lookups on pixels other than the current one may not give you the expected result.

Shaders can also be used to apply global post-effects to the current contents of the target. This can be done in two different ways:

- draw everything to a sf::RenderTexture, then draw it to the main target using the shader
- draw everything directly to the main target, then use sf::Texture::update(Window&) to copy its contents to a texture and draw it to the main target using the shader

The first technique is more optimized because it doesn't involve retrieving the target's pixels to system memory, but the second one doesn't impact the rendering process and can be easily inserted anywhere without impacting all the code.

Like sf::Texture that can be used as a raw OpenGL texture, sf::Shader can also be used directly as a raw shader for custom OpenGL geometry.

sf::Shader::bind(&shader);
... render OpenGL geometry ...
sf::Shader::bind(nullptr);
sf::Shader::bind
static void bind(const Shader *shader)
Bind a shader for rendering.

See also
  sf::Glsl

Definition at line 53 of file Shader.hpp.

## Member Enumeration Documentation

## ◆ Type

enum class sf::Shader::Type

strong

Types of shaders.

Enumerator
Vertex | Vertex shader
Geometry | Geometry shader.
Fragment | Fragment (pixel) shader.

Definition at line 60 of file Shader.hpp.

## Constructor & Destructor Documentation

## ◆ Shader() [1/12]

sf::Shader::Shader | ( |  | ) |

default

Default constructor.

This constructor creates an empty shader.

Binding an empty shader has the same effect as not binding any shader.

## ◆ ~Shader()

sf::Shader::~Shader | ( |  | ) |

Destructor.

## ◆ Shader() [2/12]

sf::Shader::Shader | ( | const Shader & |  | ) |

delete

Deleted copy constructor.

## ◆ Shader() [3/12]

sf::Shader::Shader | ( | Shader && | source | ) |

noexcept

Move constructor.

## ◆ Shader() [4/12]

sf::Shader::Shader | ( | const std::filesystem::path & | filename,
|  | Type | type )

Construct from a shader file.

This constructor loads a single shader, vertex, geometry or fragment, identified by the second argument. The source must be a text file containing a valid shader in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  filename | Path of the vertex, geometry or fragment shader file to load
  type | Type of shader (vertex, geometry or fragment)

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [5/12]

sf::Shader::Shader | ( | const std::filesystem::path & | vertexShaderFilename,
|  | const std::filesystem::path & | fragmentShaderFilename )

Construct from vertex and fragment shader files.

This constructor loads both the vertex and the fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be text files containing valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderFilename | Path of the vertex shader file to load
  fragmentShaderFilename | Path of the fragment shader file to load

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [6/12]

sf::Shader::Shader | ( | const std::filesystem::path & | vertexShaderFilename,
|  | const std::filesystem::path & | geometryShaderFilename,
|  | const std::filesystem::path & | fragmentShaderFilename )

Construct from vertex, geometry and fragment shader files.

This constructor loads the vertex, geometry and fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be text files containing valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderFilename | Path of the vertex shader file to load
  geometryShaderFilename | Path of the geometry shader file to load
  fragmentShaderFilename | Path of the fragment shader file to load

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [7/12]

sf::Shader::Shader | ( | std::string_view | shader,
|  | Type | type )

Construct from shader in memory.

This constructor loads a single shader, vertex, geometry or fragment, identified by the second argument. The source code must be a valid shader in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  shader | String containing the source code of the shader
  type | Type of shader (vertex, geometry or fragment)

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [8/12]

sf::Shader::Shader | ( | std::string_view | vertexShader,
|  | std::string_view | fragmentShader )

Construct from vertex and fragment shaders in memory.

This constructor loads both the vertex and the fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShader | String containing the source code of the vertex shader
  fragmentShader | String containing the source code of the fragment shader

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [9/12]

sf::Shader::Shader | ( | std::string_view | vertexShader,
|  | std::string_view | geometryShader,
|  | std::string_view | fragmentShader )

Construct from vertex, geometry and fragment shaders in memory.

This constructor loads the vertex, geometry and fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShader | String containing the source code of the vertex shader
  geometryShader | String containing the source code of the geometry shader
  fragmentShader | String containing the source code of the fragment shader

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [10/12]

sf::Shader::Shader | ( | InputStream & | stream,
|  | Type | type )

Construct from a shader stream.

This constructor loads a single shader, vertex, geometry or fragment, identified by the second argument. The source code must be a valid shader in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  stream | Source stream to read from
  type | Type of shader (vertex, geometry or fragment)

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [11/12]

sf::Shader::Shader | ( | InputStream & | vertexShaderStream,
|  | InputStream & | fragmentShaderStream )

Construct from vertex and fragment shader streams.

This constructor loads both the vertex and the fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The source codes must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderStream | Source stream to read the vertex shader from
  fragmentShaderStream | Source stream to read the fragment shader from

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## ◆ Shader() [12/12]

sf::Shader::Shader | ( | InputStream & | vertexShaderStream,
|  | InputStream & | geometryShaderStream,
|  | InputStream & | fragmentShaderStream )

Construct from vertex, geometry and fragment shader streams.

This constructor loads the vertex, geometry and fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The source codes must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderStream | Source stream to read the vertex shader from
  geometryShaderStream | Source stream to read the geometry shader from
  fragmentShaderStream | Source stream to read the fragment shader from

Exceptions

  sf::Exception | if loading was unsuccessful

See also
  loadFromFile, loadFromMemory, loadFromStream

## Member Function Documentation

## ◆ bind()

void sf::Shader::bind | ( | const Shader * | shader | ) |

static

Bind a shader for rendering.

This function is not part of the graphics API, it mustn't be used when drawing SFML entities. It must be used only if you mix sf::Shader with OpenGL code.

sf::Shader s1, s2;
...
sf::Shader::bind(&s1);
// draw OpenGL stuff that use s1...
sf::Shader::bind(&s2);
// draw OpenGL stuff that use s2...
sf::Shader::bind(nullptr);
// draw OpenGL stuff that use no shader...
sf::Shader
Shader class (vertex, geometry and fragment).
Definition Shader.hpp:54

Parameters

  shader | Shader to bind, can be null to use no shader

## ◆ getNativeHandle()

unsigned int sf::Shader::getNativeHandle | ( |  | ) | const

nodiscard

Get the underlying OpenGL handle of the shader.

You shouldn't need to use this function, unless you have very specific stuff to implement that SFML doesn't support, or implement a temporary workaround until a bug is fixed.

Returns
  OpenGL handle of the shader or 0 if not yet loaded

## ◆ isAvailable()

bool sf::Shader::isAvailable | ( |  | ) |

static nodiscard

Tell whether or not the system supports shaders.

This function should always be called before using the shader features. If it returns false, then any attempt to use sf::Shader will fail.

Returns
  true if shaders are supported, false otherwise

## ◆ isGeometryAvailable()

bool sf::Shader::isGeometryAvailable | ( |  | ) |

static nodiscard

Tell whether or not the system supports geometry shaders.

This function should always be called before using the geometry shader features. If it returns false, then any attempt to use sf::Shader geometry shader features will fail.

This function can only return true if isAvailable() would also return true, since shaders in general have to be supported in order for geometry shaders to be supported as well.

Note: The first call to this function, whether by your code or SFML will result in a context switch.

Returns
  true if geometry shaders are supported, false otherwise

## ◆ loadFromFile() [1/3]

bool sf::Shader::loadFromFile | ( | const std::filesystem::path & | filename,
|  | Type | type )

nodiscard

Load the vertex, geometry or fragment shader from a file.

This function loads a single shader, vertex, geometry or fragment, identified by the second argument. The source must be a text file containing a valid shader in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  filename | Path of the vertex, geometry or fragment shader file to load
  type | Type of shader (vertex, geometry or fragment)

Returns
  true if loading succeeded, false if it failed

See also
  loadFromMemory, loadFromStream

## ◆ loadFromFile() [2/3]

bool sf::Shader::loadFromFile | ( | const std::filesystem::path & | vertexShaderFilename,
|  | const std::filesystem::path & | fragmentShaderFilename )

nodiscard

Load both the vertex and fragment shaders from files.

This function loads both the vertex and the fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be text files containing valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderFilename | Path of the vertex shader file to load
  fragmentShaderFilename | Path of the fragment shader file to load

Returns
  true if loading succeeded, false if it failed

See also
  loadFromMemory, loadFromStream

## ◆ loadFromFile() [3/3]

bool sf::Shader::loadFromFile | ( | const std::filesystem::path & | vertexShaderFilename,
|  | const std::filesystem::path & | geometryShaderFilename,
|  | const std::filesystem::path & | fragmentShaderFilename )

nodiscard

Load the vertex, geometry and fragment shaders from files.

This function loads the vertex, geometry and fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be text files containing valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderFilename | Path of the vertex shader file to load
  geometryShaderFilename | Path of the geometry shader file to load
  fragmentShaderFilename | Path of the fragment shader file to load

Returns
  true if loading succeeded, false if it failed

See also
  loadFromMemory, loadFromStream

## ◆ loadFromMemory() [1/3]

bool sf::Shader::loadFromMemory | ( | std::string_view | shader,
|  | Type | type )

nodiscard

Load the vertex, geometry or fragment shader from a source code in memory.

This function loads a single shader, vertex, geometry or fragment, identified by the second argument. The source code must be a valid shader in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  shader | String containing the source code of the shader
  type | Type of shader (vertex, geometry or fragment)

Returns
  true if loading succeeded, false if it failed

See also
  loadFromFile, loadFromStream

## ◆ loadFromMemory() [2/3]

bool sf::Shader::loadFromMemory | ( | std::string_view | vertexShader,
|  | std::string_view | fragmentShader )

nodiscard

Load both the vertex and fragment shaders from source codes in memory.

This function loads both the vertex and the fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShader | String containing the source code of the vertex shader
  fragmentShader | String containing the source code of the fragment shader

Returns
  true if loading succeeded, false if it failed

See also
  loadFromFile, loadFromStream

## ◆ loadFromMemory() [3/3]

bool sf::Shader::loadFromMemory | ( | std::string_view | vertexShader,
|  | std::string_view | geometryShader,
|  | std::string_view | fragmentShader )

nodiscard

Load the vertex, geometry and fragment shaders from source codes in memory.

This function loads the vertex, geometry and fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The sources must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShader | String containing the source code of the vertex shader
  geometryShader | String containing the source code of the geometry shader
  fragmentShader | String containing the source code of the fragment shader

Returns
  true if loading succeeded, false if it failed

See also
  loadFromFile, loadFromStream

## ◆ loadFromStream() [1/3]

bool sf::Shader::loadFromStream | ( | InputStream & | stream,
|  | Type | type )

nodiscard

Load the vertex, geometry or fragment shader from a custom stream.

This function loads a single shader, vertex, geometry or fragment, identified by the second argument. The source code must be a valid shader in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  stream | Source stream to read from
  type | Type of shader (vertex, geometry or fragment)

Returns
  true if loading succeeded, false if it failed

See also
  loadFromFile, loadFromMemory

## ◆ loadFromStream() [2/3]

bool sf::Shader::loadFromStream | ( | InputStream & | vertexShaderStream,
|  | InputStream & | fragmentShaderStream )

nodiscard

Load both the vertex and fragment shaders from custom streams.

This function loads both the vertex and the fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The source codes must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderStream | Source stream to read the vertex shader from
  fragmentShaderStream | Source stream to read the fragment shader from

Returns
  true if loading succeeded, false if it failed

See also
  loadFromFile, loadFromMemory

## ◆ loadFromStream() [3/3]

bool sf::Shader::loadFromStream | ( | InputStream & | vertexShaderStream,
|  | InputStream & | geometryShaderStream,
|  | InputStream & | fragmentShaderStream )

nodiscard

Load the vertex, geometry and fragment shaders from custom streams.

This function loads the vertex, geometry and fragment shaders. If one of them fails to load, the shader is left empty (the valid shader is unloaded). The source codes must be valid shaders in GLSL language. GLSL is a C-like language dedicated to OpenGL shaders; you'll probably need to read a good documentation for it before writing your own shaders.

Parameters

  vertexShaderStream | Source stream to read the vertex shader from
  geometryShaderStream | Source stream to read the geometry shader from
  fragmentShaderStream | Source stream to read the fragment shader from

Returns
  true if loading succeeded, false if it failed

See also
  loadFromFile, loadFromMemory

## ◆ operator=() [1/2]

Shader & sf::Shader::operator= | ( | const Shader & |  | ) |

delete

Deleted copy assignment.

## ◆ operator=() [2/2]

Shader & sf::Shader::operator= | ( | Shader && | right | ) |

noexcept

Move assignment.

## ◆ setUniform() [1/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | bool | x )

Specify value for `bool` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  x | Value of the bool scalar

## ◆ setUniform() [2/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Bvec3 & | vector )

Specify value for `bvec3` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the bvec3 vector

## ◆ setUniform() [3/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Bvec4 & | vector )

Specify value for `bvec4` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the bvec4 vector

## ◆ setUniform() [4/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Ivec3 & | vector )

Specify value for `ivec3` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the ivec3 vector

## ◆ setUniform() [5/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Ivec4 & | vector )

Specify value for `ivec4` uniform.

This overload can also be called with sf::Color objects that are converted to sf::Glsl::Ivec4.

If color conversions are used, the ivec4 uniform in GLSL will hold the same values as the original sf::Color instance. For example, sf::Color(255, 127, 0, 255) is mapped to ivec4(255, 127, 0, 255).

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the ivec4 vector

## ◆ setUniform() [6/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Mat3 & | matrix )

Specify value for `mat3` matrix.

Parameters

  name | Name of the uniform variable in GLSL
  matrix | Value of the mat3 matrix

## ◆ setUniform() [7/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Mat4 & | matrix )

Specify value for `mat4` matrix.

Parameters

  name | Name of the uniform variable in GLSL
  matrix | Value of the mat4 matrix

## ◆ setUniform() [8/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Vec3 & | vector )

Specify value for `vec3` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the vec3 vector

## ◆ setUniform() [9/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Glsl::Vec4 & | vector )

Specify value for `vec4` uniform.

This overload can also be called with sf::Color objects that are converted to sf::Glsl::Vec4.

It is important to note that the components of the color are normalized before being passed to the shader. Therefore, they are converted from range [0 .. 255] to range [0 .. 1]. For example, a sf::Color(255, 127, 0, 255) will be transformed to a vec4(1.0, 0.5, 0.0, 1.0) in the shader.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the vec4 vector

## ◆ setUniform() [10/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Texture && | texture )

delete

Disallow setting from a temporary texture.

## ◆ setUniform() [11/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | const Texture & | texture )

Specify a texture as `sampler2D` uniform.

name is the name of the variable to change in the shader. The corresponding parameter in the shader must be a 2D texture (`sampler2D` GLSL type).

Example:

uniform sampler2D the_texture; // this is the variable in the shader
sf::Texture texture;
...
shader.setUniform("the_texture", texture);
sf::Texture
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56

It is important to note that texture must remain alive as long as the shader uses it, no copy is made internally.

To use the texture of the object being drawn, which cannot be known in advance, you can pass the special value sf::Shader::CurrentTexture:

shader.setUniform("the_texture", sf::Shader::CurrentTexture).

Parameters

  name | Name of the texture in the shader
  texture | Texture to assign

## ◆ setUniform() [12/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | CurrentTextureType | )

Specify current texture as `sampler2D` uniform.

This overload maps a shader texture variable to the texture of the object being drawn, which cannot be known in advance. The second argument must be sf::Shader::CurrentTexture. The corresponding parameter in the shader must be a 2D texture (`sampler2D` GLSL type).

Example:

uniform sampler2D current; // this is the variable in the shader
shader.setUniform("current", sf::Shader::CurrentTexture);

Parameters

  name | Name of the texture in the shader

## ◆ setUniform() [13/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | float | x )

Specify value for `float` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  x | Value of the float scalar

## ◆ setUniform() [14/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | Glsl::Bvec2 | vector )

Specify value for `bvec2` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the bvec2 vector

## ◆ setUniform() [15/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | Glsl::Ivec2 | vector )

Specify value for `ivec2` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the ivec2 vector

## ◆ setUniform() [16/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | Glsl::Vec2 | vector )

Specify value for `vec2` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vector | Value of the vec2 vector

## ◆ setUniform() [17/17]

void sf::Shader::setUniform | ( | const std::string & | name,
|  | int | x )

Specify value for `int` uniform.

Parameters

  name | Name of the uniform variable in GLSL
  x | Value of the int scalar

## ◆ setUniformArray() [1/6]

void sf::Shader::setUniformArray | ( | const std::string & | name,
|  | const float * | scalarArray,
|  | std::size_t | length )

Specify values for `float`[] array uniform.

Parameters

  name | Name of the uniform variable in GLSL
  scalarArray | pointer to array of `float` values
  length | Number of elements in the array

## ◆ setUniformArray() [2/6]

void sf::Shader::setUniformArray | ( | const std::string & | name,
|  | const Glsl::Mat3 * | matrixArray,
|  | std::size_t | length )

Specify values for `mat3`[] array uniform.

Parameters

  name | Name of the uniform variable in GLSL
  matrixArray | pointer to array of `mat3` values
  length | Number of elements in the array

## ◆ setUniformArray() [3/6]

void sf::Shader::setUniformArray | ( | const std::string & | name,
|  | const Glsl::Mat4 * | matrixArray,
|  | std::size_t | length )

Specify values for `mat4`[] array uniform.

Parameters

  name | Name of the uniform variable in GLSL
  matrixArray | pointer to array of `mat4` values
  length | Number of elements in the array

## ◆ setUniformArray() [4/6]

void sf::Shader::setUniformArray | ( | const std::string & | name,
|  | const Glsl::Vec2 * | vectorArray,
|  | std::size_t | length )

Specify values for `vec2`[] array uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vectorArray | pointer to array of `vec2` values
  length | Number of elements in the array

## ◆ setUniformArray() [5/6]

void sf::Shader::setUniformArray | ( | const std::string & | name,
|  | const Glsl::Vec3 * | vectorArray,
|  | std::size_t | length )

Specify values for `vec3`[] array uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vectorArray | pointer to array of `vec3` values
  length | Number of elements in the array

## ◆ setUniformArray() [6/6]

void sf::Shader::setUniformArray | ( | const std::string & | name,
|  | const Glsl::Vec4 * | vectorArray,
|  | std::size_t | length )

Specify values for `vec4`[] array uniform.

Parameters

  name | Name of the uniform variable in GLSL
  vectorArray | pointer to array of `vec4` values
  length | Number of elements in the array

## Member Data Documentation

## ◆ CurrentTexture

CurrentTextureType sf::Shader::CurrentTexture

inline static

Represents the texture of the object being drawn.

See also
  setUniform(const std::string&, CurrentTextureType)

Definition at line 85 of file Shader.hpp.

The documentation for this class was generated from the following file:

- Shader.hpp