Documentation of SFML 2.6.2

Warning: this page refers to an old version of SFML. Click here to switch to the latest version.
Loading...
Searching...
No Matches
Shader.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2023 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
41namespace sf
42{
43class Color;
44class InputStream;
45class Texture;
46class Transform;
47
52class SFML_GRAPHICS_API Shader : GlResource, NonCopyable
53{
54public:
55
60 enum Type
61 {
64 Fragment
65 };
66
75
83
84public:
85
93
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
694private:
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
Utility class for manipulating RGBA colors.
Definition Color.hpp:41
Base class for classes that require an OpenGL context.
Abstract class for custom file input streams.
Utility class that makes any derived class non-copyable.
Shader class (vertex, geometry and fragment)
Definition Shader.hpp:53
bool loadFromFile(const std::string &filename, Type type)
Load the vertex, geometry or fragment shader from a file.
void setUniformArray(const std::string &name, const Glsl::Mat4 *matrixArray, std::size_t length)
Specify values for mat4[] array uniform.
static void bind(const Shader *shader)
Bind a shader for rendering.
Shader()
Default constructor.
bool loadFromFile(const std::string &vertexShaderFilename, const std::string &geometryShaderFilename, const std::string &fragmentShaderFilename)
Load the vertex, geometry and fragment shaders from files.
void setUniform(const std::string &name, const Glsl::Ivec2 &vector)
Specify value for ivec2 uniform.
bool loadFromStream(InputStream &stream, Type type)
Load the vertex, geometry or fragment shader from a custom stream.
void setUniform(const std::string &name, const Glsl::Ivec4 &vector)
Specify value for ivec4 uniform.
void setParameter(const std::string &name, const Vector2f &vector)
Change a 2-components vector parameter of the shader.
bool loadFromStream(InputStream &vertexShaderStream, InputStream &fragmentShaderStream)
Load both the vertex and fragment shaders from custom streams.
static bool isGeometryAvailable()
Tell whether or not the system supports geometry shaders.
void setParameter(const std::string &name, float x)
Change a float parameter of the shader.
void setUniform(const std::string &name, const Glsl::Vec2 &vector)
Specify value for vec2 uniform.
~Shader()
Destructor.
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 float *scalarArray, std::size_t length)
Specify values for float[] array uniform.
void setUniform(const std::string &name, const Texture &texture)
Specify a texture as sampler2D uniform.
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, const Texture &texture)
Change a texture parameter of the shader.
void setParameter(const std::string &name, const Transform &transform)
Change a matrix parameter of the shader.
void setParameter(const std::string &name, const Vector3f &vector)
Change a 3-components vector parameter of the shader.
void setUniform(const std::string &name, const Glsl::Ivec3 &vector)
Specify value for ivec3 uniform.
bool loadFromStream(InputStream &vertexShaderStream, InputStream &geometryShaderStream, InputStream &fragmentShaderStream)
Load the vertex, geometry and fragment shaders from custom streams.
void setParameter(const std::string &name, const Color &color)
Change a color parameter of the shader.
void setUniformArray(const std::string &name, const Glsl::Vec4 *vectorArray, std::size_t length)
Specify values for vec4[] array uniform.
void setUniform(const std::string &name, const Glsl::Vec3 &vector)
Specify value for vec3 uniform.
void setUniform(const std::string &name, const Glsl::Bvec3 &vector)
Specify value for bvec3 uniform.
void setUniform(const std::string &name, CurrentTextureType)
Specify current texture as sampler2D uniform.
void setUniform(const std::string &name, const Glsl::Bvec2 &vector)
Specify value for bvec2 uniform.
void setUniformArray(const std::string &name, const Glsl::Vec2 *vectorArray, std::size_t length)
Specify values for vec2[] array uniform.
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.
void setParameter(const std::string &name, float x, float y)
Change a 2-components vector parameter of the shader.
void setUniform(const std::string &name, const Glsl::Vec4 &vector)
Specify value for vec4 uniform.
void setUniform(const std::string &name, float x)
Specify value for float uniform.
void setUniform(const std::string &name, const Glsl::Mat3 &matrix)
Specify value for mat3 matrix.
unsigned int getNativeHandle() const
Get the underlying OpenGL handle of the shader.
static CurrentTextureType CurrentTexture
Represents the texture of the object being drawn.
Definition Shader.hpp:82
void setUniform(const std::string &name, const Glsl::Bvec4 &vector)
Specify value for bvec4 uniform.
bool loadFromMemory(const std::string &shader, Type type)
Load the vertex, geometry or fragment shader from a source code in memory.
bool loadFromFile(const std::string &vertexShaderFilename, const std::string &fragmentShaderFilename)
Load both the vertex and fragment shaders from files.
void setUniform(const std::string &name, const Glsl::Mat4 &matrix)
Specify value for mat4 matrix.
static bool isAvailable()
Tell whether or not the system supports shaders.
bool loadFromMemory(const std::string &vertexShader, const std::string &fragmentShader)
Load both the vertex and fragment shaders from source codes in memory.
void setUniform(const std::string &name, int x)
Specify value for int uniform.
void setUniformArray(const std::string &name, const Glsl::Vec3 *vectorArray, std::size_t length)
Specify values for vec3[] array uniform.
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, CurrentTextureType)
Change a texture parameter of the shader.
void setUniform(const std::string &name, bool x)
Specify value for bool uniform.
Type
Types of shaders.
Definition Shader.hpp:61
@ Geometry
Geometry shader.
Definition Shader.hpp:63
@ Vertex
Vertex shader
Definition Shader.hpp:62
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:49
Define a 3x3 transform matrix.
Definition Transform.hpp:43
Utility template class for manipulating 3-dimensional vectors.
Definition Vector3.hpp:38
implementation defined Mat4
4x4 float matrix (mat4 in GLSL)
Definition Glsl.hpp:181
implementation defined Ivec4
4D int vector (ivec4 in GLSL)
Definition Glsl.hpp:124
implementation defined Vec4
4D float vector (vec4 in GLSL)
Definition Glsl.hpp:110
implementation defined Bvec4
4D bool vector (bvec4 in GLSL)
Definition Glsl.hpp:130
implementation defined Mat3
3x3 float matrix (mat3 in GLSL)
Definition Glsl.hpp:155
Special type that can be passed to setUniform(), and that represents the texture of the object being ...
Definition Shader.hpp:74