Pixel/fragment shader class. More...
#include <Shader.hpp>
Public Member Functions | |
Shader () | |
Default constructor. | |
Shader (const Shader ©) | |
Copy constructor. | |
~Shader () | |
Destructor. | |
bool | LoadFromFile (const std::string &filename) |
Load the shader from a file. | |
bool | LoadFromMemory (const std::string &shader) |
Load the shader from a source code in memory. | |
void | SetParameter (const std::string &name, float x) |
Change a float parameter of the shader. | |
void | SetParameter (const std::string &Name, float x, float y) |
Change a 2-components vector parameter of the shader. | |
void | SetParameter (const std::string &Name, float x, float y, float z) |
Change a 3-components vector parameter of the shader. | |
void | SetParameter (const std::string &Name, float x, float y, float z, float w) |
Change a 4-components vector parameter of the shader. | |
void | SetParameter (const std::string &name, const Vector2f &vector) |
Change a 2-components vector parameter of the shader. | |
void | SetParameter (const std::string &name, const Vector3f &vector) |
Change a 2-components vector parameter of the shader. | |
void | SetTexture (const std::string &name, const Image &texture) |
Change a texture parameter of the shader. | |
void | Bind () const |
Bind the shader for rendering (activate it). | |
void | Unbind () const |
Bind the shader (deactivate it). | |
Shader & | operator= (const Shader &right) |
Overload of assignment operator. | |
Static Public Member Functions | |
static bool | IsAvailable () |
Tell whether or not the system supports shaders. | |
Static Public Attributes | |
static const Image | CurrentTexture |
Special image representing the texture used by the object being drawn. | |
Friends | |
class | Renderer |
Pixel/fragment shader class.
Pixel shaders (or fragment shaders) are programs written using a specific language, executed directly by the graphics card and allowing to apply per-pixel real-time operations to the rendered entities.
Pixel 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 shader has its own variables that you can set from your C++ application. sf::Shader handles 3 different types of variables:
The value of the variables can be changed at any time with either Shader::SetParameter or Shader::SetTexture:
shader.SetParameter("offset", 2.f); shader.SetParameter("color", 0.5f, 0.8f, 0.3f); shader.SetTexture("image", image); // image is a sf::Image shader.SetTexture("current", sf::Shader::CurrentTexture);
Shader::CurrentTexture is a special value that represents the texture that the object being drawn is using.
To apply a shader to a drawable, you must pass it as an additional parameter to the Draw function:
window.Draw(sprite, shader);
Shaders can be used on any drawable, but they are mainly made for sf::Sprite. Using a shader on a sf::String is more limited, because the texture of the string is not the actual text that you see on screen, it is a big image 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. Using a shader with sf::Shape is even more limited, as shapes don't use any texture.
Like sf::Image that can be used as a raw OpenGL texture, sf::Shader can also be used directly as a raw fragment shader for custom OpenGL geometry.
window.SetActive(); shader.Bind(); ... render OpenGL geometry ... shader.Unbind();
Definition at line 47 of file Shader.hpp.
sf::Shader::Shader | ( | ) |
Default constructor.
This constructor creates an invalid shader
Definition at line 45 of file Shader.cpp.
sf::Shader::Shader | ( | const Shader & | copy | ) |
sf::Shader::~Shader | ( | ) |
Destructor.
Definition at line 67 of file Shader.cpp.
void sf::Shader::Bind | ( | ) | const |
Bind the shader for rendering (activate it).
This function is normally for internal use only, unless you want to use the shader with a custom OpenGL rendering instead of a SFML drawable.
window.SetActive(); shader.Bind(); ... render OpenGL geometry ... shader.Unbind();
Definition at line 241 of file Shader.cpp.
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.
Definition at line 283 of file Shader.cpp.
bool sf::Shader::LoadFromFile | ( | const std::string & | filename | ) |
Load the shader from a file.
The source must be a text file containing a valid fragment 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.
filename | Path of the shader file to load |
Definition at line 76 of file Shader.cpp.
bool sf::Shader::LoadFromMemory | ( | const std::string & | shader | ) |
Load the shader from a source code in memory.
The source code must be a valid fragment 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.
shader | String containing the source code of the shader |
Definition at line 97 of file Shader.cpp.
Overload of assignment operator.
right | Instance to assign |
Definition at line 269 of file Shader.cpp.
void sf::Shader::SetParameter | ( | const std::string & | name, | |
const Vector3f & | vector | |||
) |
Change a 2-components vector parameter of the shader.
name is the name of the variable to change in the shader. For example:
uniform vec3 myparam; // this is the variable in the pixel shader
shader.SetParameter("myparam", sf::Vector3f(5.2f, 6.0f, -8.1f));
name | Name of the parameter in the shader | |
vector | Vector to assign |
Definition at line 203 of file Shader.cpp.
void sf::Shader::SetParameter | ( | const std::string & | name, | |
const Vector2f & | vector | |||
) |
Change a 2-components vector parameter of the shader.
name is the name of the variable to change in the shader. For example:
uniform vec2 myparam; // this is the variable in the pixel shader
shader.SetParameter("myparam", sf::Vector2f(5.2f, 6.0f));
name | Name of the parameter in the shader | |
vector | Vector to assign |
Definition at line 196 of file Shader.cpp.
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.
name is the name of the variable to change in the shader. For example:
uniform vec4 myparam; // this is the variable in the pixel shader
shader.SetParameter("myparam", 5.2f, 6.0f, -8.1f, 0.4f);
name | Name of the parameter in the shader | |
x | First component of the value to assign | |
y | Second component of the value to assign | |
z | Third component of the value to assign | |
w | Fourth component of the value to assign |
Definition at line 174 of file Shader.cpp.
void sf::Shader::SetParameter | ( | const std::string & | Name, | |
float | x, | |||
float | y, | |||
float | z | |||
) |
Change a 3-components vector parameter of the shader.
name is the name of the variable to change in the shader. For example:
uniform vec3 myparam; // this is the variable in the pixel shader
shader.SetParameter("myparam", 5.2f, 6.0f, -8.1f);
name | Name of the parameter in the shader | |
x | First component of the value to assign | |
y | Second component of the value to assign | |
z | Third component of the value to assign |
Definition at line 152 of file Shader.cpp.
void sf::Shader::SetParameter | ( | const std::string & | Name, | |
float | x, | |||
float | y | |||
) |
Change a 2-components vector parameter of the shader.
name is the name of the variable to change in the shader. For example:
uniform vec2 myparam; // this is the variable in the pixel shader
shader.SetParameter("myparam", 5.2f, 6.0f);
name | Name of the parameter in the shader | |
x | First component of the value to assign | |
y | Second component of the value to assign |
Definition at line 130 of file Shader.cpp.
void sf::Shader::SetParameter | ( | const std::string & | name, | |
float | x | |||
) |
Change a float parameter of the shader.
name is the name of the variable to change in the shader. For example:
uniform float myparam; // this is the variable in the pixel shader
shader.SetParameter("myparam", 5.2f);
name | Name of the parameter in the shader | |
x | Value to assign |
Definition at line 108 of file Shader.cpp.
void sf::Shader::SetTexture | ( | const std::string & | name, | |
const Image & | texture | |||
) |
Change a texture parameter of the shader.
name is the name of the texture to change in the shader. To tell the shader to use the current texture of the object being drawn, pass Shader::CurrentTexture. Example:
// These are the variables in the pixel shader
uniform sampler2D current;
uniform sampler2D other;
sf::Image image; ... shader.SetParameter("current", sf::Shader::CurrentTexture); shader.SetParameter("other", image);
It is important to note that texture must remain alive as long as the shader uses it, no copy is made internally.
name | Name of the texture in the shader | |
texture | Image to assign |
Definition at line 210 of file Shader.cpp.
void sf::Shader::Unbind | ( | ) | const |
Bind the shader (deactivate it).
This function is normally for internal use only, unless you want to use the shader with a custom OpenGL rendering instead of a SFML drawable.
Definition at line 262 of file Shader.cpp.
const Image sf::Shader::CurrentTexture [static] |
Special image representing the texture used by the object being drawn.
Definition at line 318 of file Shader.hpp.