Documentation of SFML 2.5.1

sf::Shader Class Reference

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

#include <Shader.hpp>

Inheritance diagram for sf::Shader:
sf::GlResource sf::NonCopyable

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  Type { Vertex, Geometry, Fragment }
 Types of shaders. More...
 

Public Member Functions

 Shader ()
 Default constructor. More...
 
 ~Shader ()
 Destructor. More...
 
bool loadFromFile (const std::string &filename, Type type)
 Load the vertex, geometry or fragment shader from a file. More...
 
bool loadFromFile (const std::string &vertexShaderFilename, const std::string &fragmentShaderFilename)
 Load both the vertex and fragment shaders from files. More...
 
bool loadFromFile (const std::string &vertexShaderFilename, const std::string &geometryShaderFilename, const std::string &fragmentShaderFilename)
 Load the vertex, geometry and fragment shaders from files. More...
 
bool loadFromMemory (const std::string &shader, Type type)
 Load the vertex, geometry or fragment shader from a source code in memory. More...
 
bool loadFromMemory (const std::string &vertexShader, const std::string &fragmentShader)
 Load both the vertex and fragment shaders from source codes in memory. More...
 
bool loadFromMemory (const std::string &vertexShader, const std::string &geometryShader, const std::string &fragmentShader)
 Load the vertex, geometry and fragment shaders from source codes in memory. More...
 
bool loadFromStream (InputStream &stream, Type type)
 Load the vertex, geometry or fragment shader from a custom stream. More...
 
bool loadFromStream (InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
 Load both the vertex and fragment shaders from custom streams. More...
 
bool loadFromStream (InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
 Load the vertex, geometry and fragment shaders from custom streams. More...
 
void setUniform (const std::string &name, float x)
 Specify value for float uniform. More...
 
void setUniform (const std::string &name, const Glsl::Vec2 &vector)
 Specify value for vec2 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Vec3 &vector)
 Specify value for vec3 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Vec4 &vector)
 Specify value for vec4 uniform. More...
 
void setUniform (const std::string &name, int x)
 Specify value for int uniform. More...
 
void setUniform (const std::string &name, const Glsl::Ivec2 &vector)
 Specify value for ivec2 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Ivec3 &vector)
 Specify value for ivec3 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Ivec4 &vector)
 Specify value for ivec4 uniform. More...
 
void setUniform (const std::string &name, bool x)
 Specify value for bool uniform. More...
 
void setUniform (const std::string &name, const Glsl::Bvec2 &vector)
 Specify value for bvec2 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Bvec3 &vector)
 Specify value for bvec3 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Bvec4 &vector)
 Specify value for bvec4 uniform. More...
 
void setUniform (const std::string &name, const Glsl::Mat3 &matrix)
 Specify value for mat3 matrix. More...
 
void setUniform (const std::string &name, const Glsl::Mat4 &matrix)
 Specify value for mat4 matrix. More...
 
void setUniform (const std::string &name, const Texture &texture)
 Specify a texture as sampler2D uniform. More...
 
void setUniform (const std::string &name, CurrentTextureType)
 Specify current texture as sampler2D uniform. More...
 
void setUniformArray (const std::string &name, const float *scalarArray, std::size_t length)
 Specify values for float[] array uniform. More...
 
void setUniformArray (const std::string &name, const Glsl::Vec2 *vectorArray, std::size_t length)
 Specify values for vec2[] array uniform. More...
 
void setUniformArray (const std::string &name, const Glsl::Vec3 *vectorArray, std::size_t length)
 Specify values for vec3[] array uniform. More...
 
void setUniformArray (const std::string &name, const Glsl::Vec4 *vectorArray, std::size_t length)
 Specify values for vec4[] array uniform. More...
 
void setUniformArray (const std::string &name, const Glsl::Mat3 *matrixArray, std::size_t length)
 Specify values for mat3[] array uniform. More...
 
void setUniformArray (const std::string &name, const Glsl::Mat4 *matrixArray, std::size_t length)
 Specify values for mat4[] array uniform. More...
 
void setParameter (const std::string &name, float x)
 Change a float parameter of the shader. More...
 
void setParameter (const std::string &name, float x, float y)
 Change a 2-components vector parameter of the shader. More...
 
void setParameter (const std::string &name, float x, float y, float z)
 Change a 3-components vector parameter of the shader. More...
 
void setParameter (const std::string &name, float x, float y, float z, float w)
 Change a 4-components vector parameter of the shader. More...
 
void setParameter (const std::string &name, const Vector2f &vector)
 Change a 2-components vector parameter of the shader. More...
 
void setParameter (const std::string &name, const Vector3f &vector)
 Change a 3-components vector parameter of the shader. More...
 
void setParameter (const std::string &name, const Color &color)
 Change a color parameter of the shader. More...
 
void setParameter (const std::string &name, const Transform &transform)
 Change a matrix parameter of the shader. More...
 
void setParameter (const std::string &name, const Texture &texture)
 Change a texture parameter of the shader. More...
 
void setParameter (const std::string &name, CurrentTextureType)
 Change a texture parameter of the shader. More...
 
unsigned int getNativeHandle () const
 Get the underlying OpenGL handle of the shader. More...
 

Static Public Member Functions

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

Static Public Attributes

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

Static Private Member Functions

static void registerContextDestroyCallback (ContextDestroyCallback callback, void *arg)
 Register a function to be called when a context is destroyed. More...
 

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:

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);

The old setParameter() overloads are deprecated and will be removed in a future version. You should use their setUniform() equivalents instead.

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:

states.shader = &shader;
window.draw(sprite, states);

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 (like the old sf::PostFx class in SFML 1). 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.

... render OpenGL geometry ...
sf::Shader::bind(NULL);
See also
sf::Glsl

Definition at line 52 of file Shader.hpp.

Member Enumeration Documentation

◆ Type

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()

sf::Shader::Shader ( )

Default constructor.

This constructor creates an invalid shader.

◆ ~Shader()

sf::Shader::~Shader ( )

Destructor.

Member Function Documentation

◆ bind()

static 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...
// draw OpenGL stuff that use s2...
// draw OpenGL stuff that use no shader...
Parameters
shaderShader to bind, can be null to use no shader

◆ getNativeHandle()

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

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()

static bool sf::Shader::isAvailable ( )
static

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()

static bool sf::Shader::isGeometryAvailable ( )
static

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::string &  filename,
Type  type 
)

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
filenamePath of the vertex, geometry or fragment shader file to load
typeType 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::string &  vertexShaderFilename,
const std::string &  fragmentShaderFilename 
)

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
vertexShaderFilenamePath of the vertex shader file to load
fragmentShaderFilenamePath 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::string &  vertexShaderFilename,
const std::string &  geometryShaderFilename,
const std::string &  fragmentShaderFilename 
)

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
vertexShaderFilenamePath of the vertex shader file to load
geometryShaderFilenamePath of the geometry shader file to load
fragmentShaderFilenamePath 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 ( const std::string &  shader,
Type  type 
)

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
shaderString containing the source code of the shader
typeType 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 ( const std::string &  vertexShader,
const std::string &  fragmentShader 
)

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
vertexShaderString containing the source code of the vertex shader
fragmentShaderString 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 ( const std::string &  vertexShader,
const std::string &  geometryShader,
const std::string &  fragmentShader 
)

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
vertexShaderString containing the source code of the vertex shader
geometryShaderString containing the source code of the geometry shader
fragmentShaderString 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 
)

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
streamSource stream to read from
typeType 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 
)

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
vertexShaderStreamSource stream to read the vertex shader from
fragmentShaderStreamSource 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 
)

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
vertexShaderStreamSource stream to read the vertex shader from
geometryShaderStreamSource stream to read the geometry shader from
fragmentShaderStreamSource stream to read the fragment shader from
Returns
True if loading succeeded, false if it failed
See also
loadFromFile, loadFromMemory

◆ setParameter() [1/10]

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

Change a float parameter of the shader.

Deprecated:
Use setUniform(const std::string&, float) instead.

◆ setParameter() [2/10]

void sf::Shader::setParameter ( const std::string &  name,
float  x,
float  y 
)

Change a 2-components vector parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Vec2&) instead.

◆ setParameter() [3/10]

void sf::Shader::setParameter ( const std::string &  name,
float  x,
float  y,
float  z 
)

Change a 3-components vector parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Vec3&) instead.

◆ setParameter() [4/10]

void sf::Shader::setParameter ( const std::string &  name,
float  x,
float  y,
float  z,
float  w 
)

Change a 4-components vector parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Vec4&) instead.

◆ setParameter() [5/10]

void sf::Shader::setParameter ( const std::string &  name,
const Vector2f vector 
)

Change a 2-components vector parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Vec2&) instead.

◆ setParameter() [6/10]

void sf::Shader::setParameter ( const std::string &  name,
const Vector3f vector 
)

Change a 3-components vector parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Vec3&) instead.

◆ setParameter() [7/10]

void sf::Shader::setParameter ( const std::string &  name,
const Color color 
)

Change a color parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Vec4&) instead.

◆ setParameter() [8/10]

void sf::Shader::setParameter ( const std::string &  name,
const Transform transform 
)

Change a matrix parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Glsl::Mat4&) instead.

◆ setParameter() [9/10]

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

Change a texture parameter of the shader.

Deprecated:
Use setUniform(const std::string&, const Texture&) instead.

◆ setParameter() [10/10]

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

Change a texture parameter of the shader.

Deprecated:
Use setUniform(const std::string&, CurrentTextureType) instead.

◆ setUniform() [1/16]

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

Specify value for float uniform.

Parameters
nameName of the uniform variable in GLSL
xValue of the float scalar

◆ setUniform() [2/16]

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

Specify value for vec2 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the vec2 vector

◆ setUniform() [3/16]

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

Specify value for vec3 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the vec3 vector

◆ setUniform() [4/16]

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
nameName of the uniform variable in GLSL
vectorValue of the vec4 vector

◆ setUniform() [5/16]

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

Specify value for int uniform.

Parameters
nameName of the uniform variable in GLSL
xValue of the int scalar

◆ setUniform() [6/16]

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

Specify value for ivec2 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the ivec2 vector

◆ setUniform() [7/16]

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

Specify value for ivec3 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the ivec3 vector

◆ setUniform() [8/16]

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
nameName of the uniform variable in GLSL
vectorValue of the ivec4 vector

◆ setUniform() [9/16]

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

Specify value for bool uniform.

Parameters
nameName of the uniform variable in GLSL
xValue of the bool scalar

◆ setUniform() [10/16]

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

Specify value for bvec2 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the bvec2 vector

◆ setUniform() [11/16]

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

Specify value for bvec3 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the bvec3 vector

◆ setUniform() [12/16]

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

Specify value for bvec4 uniform.

Parameters
nameName of the uniform variable in GLSL
vectorValue of the bvec4 vector

◆ setUniform() [13/16]

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

Specify value for mat3 matrix.

Parameters
nameName of the uniform variable in GLSL
matrixValue of the mat3 matrix

◆ setUniform() [14/16]

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

Specify value for mat4 matrix.

Parameters
nameName of the uniform variable in GLSL
matrixValue of the mat4 matrix

◆ setUniform() [15/16]

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);

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
nameName of the texture in the shader
textureTexture to assign

◆ setUniform() [16/16]

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
nameName of the texture in the shader

◆ 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
nameName of the uniform variable in GLSL
scalarArraypointer to array of float values
lengthNumber of elements in the array

◆ setUniformArray() [2/6]

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

Specify values for vec2[] array uniform.

Parameters
nameName of the uniform variable in GLSL
vectorArraypointer to array of vec2 values
lengthNumber of elements in the array

◆ setUniformArray() [3/6]

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

Specify values for vec3[] array uniform.

Parameters
nameName of the uniform variable in GLSL
vectorArraypointer to array of vec3 values
lengthNumber of elements in the array

◆ setUniformArray() [4/6]

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

Specify values for vec4[] array uniform.

Parameters
nameName of the uniform variable in GLSL
vectorArraypointer to array of vec4 values
lengthNumber of elements in the array

◆ setUniformArray() [5/6]

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

Specify values for mat3[] array uniform.

Parameters
nameName of the uniform variable in GLSL
matrixArraypointer to array of mat3 values
lengthNumber of elements in the array

◆ setUniformArray() [6/6]

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

Specify values for mat4[] array uniform.

Parameters
nameName of the uniform variable in GLSL
matrixArraypointer to array of mat4 values
lengthNumber of elements in the array

Member Data Documentation

◆ CurrentTexture

CurrentTextureType sf::Shader::CurrentTexture
static

Represents the texture of the object being drawn.

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

Definition at line 82 of file Shader.hpp.


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