View on GitHub

DSFML

dsfml.graphics.shader



class Shader;
Shader class (vertex and fragment).

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

There are two kinds of shaders: - Vertex shaders, that process vertices - Fragment (pixel) shaders, that process pixels

A DSFML Shader can be composed of either a vertex shader alone, a fragment shader alone, or both combined (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 D/C/C++ program, a shader has its own variables that you can set from your D application. DSFML's Shader handles 5 different types of variables: - floats - vectors (2, 3, or 4 components) - colors - textures - transforms (matrices)

Authors:
Laurent Gomila, Jeremy DeHaan

See Also:


http:
//www.sfml-dev.org/documentation/2.0/classsf_1_1Shader.php#details

enum Type: int;
Types of shaders.

Vertex
Vertex shader

Fragment
Fragment (pixel) shader.

struct CurrentTextureType;
Special type/value that can be passed to setParameter, and that represents the texture of the object being drawn.

bool loadFromFile(string filename, Type type);
Load either the vertex or fragment shader from a file.

This function loads a single shader, either vertex 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.

Params:
string filename Path of the vertex or fragment shader file to load
Type type Type of shader (vertex or fragment)

Returns:
True if loading succeeded, false if it failed.

bool loadFromFile(string vertexShaderFilename, 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.

Params:
string vertexShaderFilename Path of the vertex shader file to load
string fragmentShaderFilename Path of the fragment shader file to load

Returns:
True if loading succeeded, false if it failed.

bool loadFromMemory(string shader, Type type);
Load either the vertex or fragment shader from a source code in memory.

This function loads a single shader, either vertex 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.

Params:
string shader String containing the source code of the shader
Type type Type of shader (vertex or fragment)

Returns:
True if loading succeeded, false if it failed.

bool loadFromMemory(string vertexShader, 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.

Params:
string vertexShader String containing the source code of the vertex shader
string fragmentShader String containing the source code of the fragment shader

Returns:
True if loading succeeded, false if it failed.

bool loadFromStream(InputStream stream, Type type);
Load either the vertex or fragment shader from a custom stream.

This function loads a single shader, either vertex 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.

Params:
InputStream stream Source stream to read from
Type type Type of shader (vertex or fragment)

Returns:
True if loading succeeded, false if it failed.

bool loadFromStream(InputStream vertexShaderStream, InputStream fragmentShaderStream);
Load both the vertex and fragment shaders from custom streams.

This function loads a single shader, either vertex 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.

Params:
InputStream vertexShaderStream Source stream to read the vertex shader from
InputStream fragmentShaderStream Source stream to read the fragment shader from

Returns:
True if loading succeeded, false if it failed.

void setParameter(string name, float x);
void opIndexAssign(float x, string name);
Change a float parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a float (float GLSL type).
float x Value to assign

void setParameter(string name, float x, float y);
Change a 2-components vector parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 2x1 vector (vec2 GLSL type).
float x First component of the value to assign
float y Second component of the value to assign

void setParameter(string name, float x, float y, float z);
Change a 3-components vector parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 3x1 vector (vec3 GLSL type).
float x First component of the value to assign
float y Second component of the value to assign
float z Third component of the value to assign

void setParameter(string name, float x, float y, float z, float w);
Change a 4-components vector parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 4x1 vector (vec4 GLSL type).
float x First component of the value to assign
float y Second component of the value to assign
float z Third component of the value to assign
float w Fourth component of the value to assign

void opIndexAssign(float[] val, string name);
Change variable length vector parameter of the shader. The length of the set of floats must be between 1 and 4.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 4x1 vector (vec4 GLSL type).
float[] val The set of floats to assign.

void setParameter(string name, Vector2f vector);
void opIndexAssign(Vector2f vector, string name);
Change a 2-components vector parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 2x1 vector (vec2 GLSL type).
Vector2f vector Vector to assign

void setParameter(string name, Vector3f vector);
void opIndexAssign(Vector3f vector, string name);
Change a 3-components vector parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 3x1 vector (vec3 GLSL type).
Vector3f vector Vector to assign

void setParameter(string name, Color color);
void opIndexAssign(Color color, string name);
Change a color vector parameter of the shader.

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 Color(255, 125, 0, 255) will be transformed to a vec4(1.0, 0.5, 0.0, 1.0) in the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 4x1 vector (vec4 GLSL type).
Color color Color to assign

void setParameter(string name, Transform transform);
void opIndexAssign(Transform transform, string name);
Change a matrix parameter of the shader.

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 4x4 matrix (mat4 GLSL type).
Transform transform Transform to assign

void setParameter(string name, Texture texture);
void opIndexAssign(Texture texture, string name);
Change a texture parameter of the shader.

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

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

Params:
string name The name of the variable to change in the shader. The corresponding parameter in the shader must be a 2D texture (sampler2D GLSL type).
Texture texture Texture to assign

void setParameter(string name, CurrentTextureType);
Change a texture parameter of the shader.

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 Shader.CurrentTexture.

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

void opIndexAssign(CurrentTextureType, string name);
Change a texture parameter of the shader.

This overload maps a shader texture variable to the texture of the object being drawn, which cannot be known in advance. The value given must be Shader.CurrentTexture.

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

static void bind(Shader shader);
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 Shader with OpenGL code.

Params:
Shader shader Shader to bind. Can be null to use no shader.

static bool isAvailable();
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 DSFML Shader will fail.

Returns:
True if shaders are supported, false otherwise