|
|
|||
File indexing completed on 2026-06-02 08:58:14
0001 //////////////////////////////////////////////////////////// 0002 // 0003 // SFML - Simple and Fast Multimedia Library 0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org) 0005 // 0006 // This software is provided 'as-is', without any express or implied warranty. 0007 // In no event will the authors be held liable for any damages arising from the use of this software. 0008 // 0009 // Permission is granted to anyone to use this software for any purpose, 0010 // including commercial applications, and to alter it and redistribute it freely, 0011 // subject to the following restrictions: 0012 // 0013 // 1. The origin of this software must not be misrepresented; 0014 // you must not claim that you wrote the original software. 0015 // If you use this software in a product, an acknowledgment 0016 // in the product documentation would be appreciated but is not required. 0017 // 0018 // 2. Altered source versions must be plainly marked as such, 0019 // and must not be misrepresented as being the original software. 0020 // 0021 // 3. This notice may not be removed or altered from any source distribution. 0022 // 0023 //////////////////////////////////////////////////////////// 0024 0025 #ifndef SFML_SHADER_HPP 0026 #define SFML_SHADER_HPP 0027 0028 //////////////////////////////////////////////////////////// 0029 // Headers 0030 //////////////////////////////////////////////////////////// 0031 #include <SFML/Graphics/Export.hpp> 0032 #include <SFML/Graphics/Glsl.hpp> 0033 #include <SFML/Window/GlResource.hpp> 0034 #include <SFML/System/NonCopyable.hpp> 0035 #include <SFML/System/Vector2.hpp> 0036 #include <SFML/System/Vector3.hpp> 0037 #include <map> 0038 #include <string> 0039 0040 0041 namespace sf 0042 { 0043 class Color; 0044 class InputStream; 0045 class Texture; 0046 class Transform; 0047 0048 //////////////////////////////////////////////////////////// 0049 /// \brief Shader class (vertex, geometry and fragment) 0050 /// 0051 //////////////////////////////////////////////////////////// 0052 class SFML_GRAPHICS_API Shader : GlResource, NonCopyable 0053 { 0054 public: 0055 0056 //////////////////////////////////////////////////////////// 0057 /// \brief Types of shaders 0058 /// 0059 //////////////////////////////////////////////////////////// 0060 enum Type 0061 { 0062 Vertex, //!< %Vertex shader 0063 Geometry, //!< Geometry shader 0064 Fragment //!< Fragment (pixel) shader 0065 }; 0066 0067 //////////////////////////////////////////////////////////// 0068 /// \brief Special type that can be passed to setUniform(), 0069 /// and that represents the texture of the object being drawn 0070 /// 0071 /// \see setUniform(const std::string&, CurrentTextureType) 0072 /// 0073 //////////////////////////////////////////////////////////// 0074 struct CurrentTextureType {}; 0075 0076 //////////////////////////////////////////////////////////// 0077 /// \brief Represents the texture of the object being drawn 0078 /// 0079 /// \see setUniform(const std::string&, CurrentTextureType) 0080 /// 0081 //////////////////////////////////////////////////////////// 0082 static CurrentTextureType CurrentTexture; 0083 0084 public: 0085 0086 //////////////////////////////////////////////////////////// 0087 /// \brief Default constructor 0088 /// 0089 /// This constructor creates an invalid shader. 0090 /// 0091 //////////////////////////////////////////////////////////// 0092 Shader(); 0093 0094 //////////////////////////////////////////////////////////// 0095 /// \brief Destructor 0096 /// 0097 //////////////////////////////////////////////////////////// 0098 ~Shader(); 0099 0100 //////////////////////////////////////////////////////////// 0101 /// \brief Load the vertex, geometry or fragment shader from a file 0102 /// 0103 /// This function loads a single shader, vertex, geometry or 0104 /// fragment, identified by the second argument. 0105 /// The source must be a text file containing a valid 0106 /// shader in GLSL language. GLSL is a C-like language 0107 /// dedicated to OpenGL shaders; you'll probably need to 0108 /// read a good documentation for it before writing your 0109 /// own shaders. 0110 /// 0111 /// \param filename Path of the vertex, geometry or fragment shader file to load 0112 /// \param type Type of shader (vertex, geometry or fragment) 0113 /// 0114 /// \return True if loading succeeded, false if it failed 0115 /// 0116 /// \see loadFromMemory, loadFromStream 0117 /// 0118 //////////////////////////////////////////////////////////// 0119 bool loadFromFile(const std::string& filename, Type type); 0120 0121 //////////////////////////////////////////////////////////// 0122 /// \brief Load both the vertex and fragment shaders from files 0123 /// 0124 /// This function loads both the vertex and the fragment 0125 /// shaders. If one of them fails to load, the shader is left 0126 /// empty (the valid shader is unloaded). 0127 /// The sources must be text files containing valid shaders 0128 /// in GLSL language. GLSL is a C-like language dedicated to 0129 /// OpenGL shaders; you'll probably need to read a good documentation 0130 /// for it before writing your own shaders. 0131 /// 0132 /// \param vertexShaderFilename Path of the vertex shader file to load 0133 /// \param fragmentShaderFilename Path of the fragment shader file to load 0134 /// 0135 /// \return True if loading succeeded, false if it failed 0136 /// 0137 /// \see loadFromMemory, loadFromStream 0138 /// 0139 //////////////////////////////////////////////////////////// 0140 bool loadFromFile(const std::string& vertexShaderFilename, const std::string& fragmentShaderFilename); 0141 0142 //////////////////////////////////////////////////////////// 0143 /// \brief Load the vertex, geometry and fragment shaders from files 0144 /// 0145 /// This function loads the vertex, geometry and fragment 0146 /// shaders. If one of them fails to load, the shader is left 0147 /// empty (the valid shader is unloaded). 0148 /// The sources must be text files containing valid shaders 0149 /// in GLSL language. GLSL is a C-like language dedicated to 0150 /// OpenGL shaders; you'll probably need to read a good documentation 0151 /// for it before writing your own shaders. 0152 /// 0153 /// \param vertexShaderFilename Path of the vertex shader file to load 0154 /// \param geometryShaderFilename Path of the geometry shader file to load 0155 /// \param fragmentShaderFilename Path of the fragment shader file to load 0156 /// 0157 /// \return True if loading succeeded, false if it failed 0158 /// 0159 /// \see loadFromMemory, loadFromStream 0160 /// 0161 //////////////////////////////////////////////////////////// 0162 bool loadFromFile(const std::string& vertexShaderFilename, const std::string& geometryShaderFilename, const std::string& fragmentShaderFilename); 0163 0164 //////////////////////////////////////////////////////////// 0165 /// \brief Load the vertex, geometry or fragment shader from a source code in memory 0166 /// 0167 /// This function loads a single shader, vertex, geometry 0168 /// or fragment, identified by the second argument. 0169 /// The source code must be a valid shader in GLSL language. 0170 /// GLSL is a C-like language dedicated to OpenGL shaders; 0171 /// you'll probably need to read a good documentation for 0172 /// it before writing your own shaders. 0173 /// 0174 /// \param shader String containing the source code of the shader 0175 /// \param type Type of shader (vertex, geometry or fragment) 0176 /// 0177 /// \return True if loading succeeded, false if it failed 0178 /// 0179 /// \see loadFromFile, loadFromStream 0180 /// 0181 //////////////////////////////////////////////////////////// 0182 bool loadFromMemory(const std::string& shader, Type type); 0183 0184 //////////////////////////////////////////////////////////// 0185 /// \brief Load both the vertex and fragment shaders from source codes in memory 0186 /// 0187 /// This function loads both the vertex and the fragment 0188 /// shaders. If one of them fails to load, the shader is left 0189 /// empty (the valid shader is unloaded). 0190 /// The sources must be valid shaders in GLSL language. GLSL is 0191 /// a C-like language dedicated to OpenGL shaders; you'll 0192 /// probably need to read a good documentation for it before 0193 /// writing your own shaders. 0194 /// 0195 /// \param vertexShader String containing the source code of the vertex shader 0196 /// \param fragmentShader String containing the source code of the fragment shader 0197 /// 0198 /// \return True if loading succeeded, false if it failed 0199 /// 0200 /// \see loadFromFile, loadFromStream 0201 /// 0202 //////////////////////////////////////////////////////////// 0203 bool loadFromMemory(const std::string& vertexShader, const std::string& fragmentShader); 0204 0205 //////////////////////////////////////////////////////////// 0206 /// \brief Load the vertex, geometry and fragment shaders from source codes in memory 0207 /// 0208 /// This function loads the vertex, geometry and fragment 0209 /// shaders. If one of them fails to load, the shader is left 0210 /// empty (the valid shader is unloaded). 0211 /// The sources must be valid shaders in GLSL language. GLSL is 0212 /// a C-like language dedicated to OpenGL shaders; you'll 0213 /// probably need to read a good documentation for it before 0214 /// writing your own shaders. 0215 /// 0216 /// \param vertexShader String containing the source code of the vertex shader 0217 /// \param geometryShader String containing the source code of the geometry shader 0218 /// \param fragmentShader String containing the source code of the fragment shader 0219 /// 0220 /// \return True if loading succeeded, false if it failed 0221 /// 0222 /// \see loadFromFile, loadFromStream 0223 /// 0224 //////////////////////////////////////////////////////////// 0225 bool loadFromMemory(const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader); 0226 0227 //////////////////////////////////////////////////////////// 0228 /// \brief Load the vertex, geometry or fragment shader from a custom stream 0229 /// 0230 /// This function loads a single shader, vertex, geometry 0231 /// or fragment, identified by the second argument. 0232 /// The source code must be a valid shader in GLSL language. 0233 /// GLSL is a C-like language dedicated to OpenGL shaders; 0234 /// you'll probably need to read a good documentation for it 0235 /// before writing your own shaders. 0236 /// 0237 /// \param stream Source stream to read from 0238 /// \param type Type of shader (vertex, geometry or fragment) 0239 /// 0240 /// \return True if loading succeeded, false if it failed 0241 /// 0242 /// \see loadFromFile, loadFromMemory 0243 /// 0244 //////////////////////////////////////////////////////////// 0245 bool loadFromStream(InputStream& stream, Type type); 0246 0247 //////////////////////////////////////////////////////////// 0248 /// \brief Load both the vertex and fragment shaders from custom streams 0249 /// 0250 /// This function loads both the vertex and the fragment 0251 /// shaders. If one of them fails to load, the shader is left 0252 /// empty (the valid shader is unloaded). 0253 /// The source codes must be valid shaders in GLSL language. 0254 /// GLSL is a C-like language dedicated to OpenGL shaders; 0255 /// you'll probably need to read a good documentation for 0256 /// it before writing your own shaders. 0257 /// 0258 /// \param vertexShaderStream Source stream to read the vertex shader from 0259 /// \param fragmentShaderStream Source stream to read the fragment shader from 0260 /// 0261 /// \return True if loading succeeded, false if it failed 0262 /// 0263 /// \see loadFromFile, loadFromMemory 0264 /// 0265 //////////////////////////////////////////////////////////// 0266 bool loadFromStream(InputStream& vertexShaderStream, InputStream& fragmentShaderStream); 0267 0268 //////////////////////////////////////////////////////////// 0269 /// \brief Load the vertex, geometry and fragment shaders from custom streams 0270 /// 0271 /// This function loads the vertex, geometry and fragment 0272 /// shaders. If one of them fails to load, the shader is left 0273 /// empty (the valid shader is unloaded). 0274 /// The source codes must be valid shaders in GLSL language. 0275 /// GLSL is a C-like language dedicated to OpenGL shaders; 0276 /// you'll probably need to read a good documentation for 0277 /// it before writing your own shaders. 0278 /// 0279 /// \param vertexShaderStream Source stream to read the vertex shader from 0280 /// \param geometryShaderStream Source stream to read the geometry shader from 0281 /// \param fragmentShaderStream Source stream to read the fragment shader from 0282 /// 0283 /// \return True if loading succeeded, false if it failed 0284 /// 0285 /// \see loadFromFile, loadFromMemory 0286 /// 0287 //////////////////////////////////////////////////////////// 0288 bool loadFromStream(InputStream& vertexShaderStream, InputStream& geometryShaderStream, InputStream& fragmentShaderStream); 0289 0290 //////////////////////////////////////////////////////////// 0291 /// \brief Specify value for \p float uniform 0292 /// 0293 /// \param name Name of the uniform variable in GLSL 0294 /// \param x Value of the float scalar 0295 /// 0296 //////////////////////////////////////////////////////////// 0297 void setUniform(const std::string& name, float x); 0298 0299 //////////////////////////////////////////////////////////// 0300 /// \brief Specify value for \p vec2 uniform 0301 /// 0302 /// \param name Name of the uniform variable in GLSL 0303 /// \param vector Value of the vec2 vector 0304 /// 0305 //////////////////////////////////////////////////////////// 0306 void setUniform(const std::string& name, const Glsl::Vec2& vector); 0307 0308 //////////////////////////////////////////////////////////// 0309 /// \brief Specify value for \p vec3 uniform 0310 /// 0311 /// \param name Name of the uniform variable in GLSL 0312 /// \param vector Value of the vec3 vector 0313 /// 0314 //////////////////////////////////////////////////////////// 0315 void setUniform(const std::string& name, const Glsl::Vec3& vector); 0316 0317 //////////////////////////////////////////////////////////// 0318 /// \brief Specify value for \p vec4 uniform 0319 /// 0320 /// This overload can also be called with sf::Color objects 0321 /// that are converted to sf::Glsl::Vec4. 0322 /// 0323 /// It is important to note that the components of the color are 0324 /// normalized before being passed to the shader. Therefore, 0325 /// they are converted from range [0 .. 255] to range [0 .. 1]. 0326 /// For example, a sf::Color(255, 127, 0, 255) will be transformed 0327 /// to a vec4(1.0, 0.5, 0.0, 1.0) in the shader. 0328 /// 0329 /// \param name Name of the uniform variable in GLSL 0330 /// \param vector Value of the vec4 vector 0331 /// 0332 //////////////////////////////////////////////////////////// 0333 void setUniform(const std::string& name, const Glsl::Vec4& vector); 0334 0335 //////////////////////////////////////////////////////////// 0336 /// \brief Specify value for \p int uniform 0337 /// 0338 /// \param name Name of the uniform variable in GLSL 0339 /// \param x Value of the int scalar 0340 /// 0341 //////////////////////////////////////////////////////////// 0342 void setUniform(const std::string& name, int x); 0343 0344 //////////////////////////////////////////////////////////// 0345 /// \brief Specify value for \p ivec2 uniform 0346 /// 0347 /// \param name Name of the uniform variable in GLSL 0348 /// \param vector Value of the ivec2 vector 0349 /// 0350 //////////////////////////////////////////////////////////// 0351 void setUniform(const std::string& name, const Glsl::Ivec2& vector); 0352 0353 //////////////////////////////////////////////////////////// 0354 /// \brief Specify value for \p ivec3 uniform 0355 /// 0356 /// \param name Name of the uniform variable in GLSL 0357 /// \param vector Value of the ivec3 vector 0358 /// 0359 //////////////////////////////////////////////////////////// 0360 void setUniform(const std::string& name, const Glsl::Ivec3& vector); 0361 0362 //////////////////////////////////////////////////////////// 0363 /// \brief Specify value for \p ivec4 uniform 0364 /// 0365 /// This overload can also be called with sf::Color objects 0366 /// that are converted to sf::Glsl::Ivec4. 0367 /// 0368 /// If color conversions are used, the ivec4 uniform in GLSL 0369 /// will hold the same values as the original sf::Color 0370 /// instance. For example, sf::Color(255, 127, 0, 255) is 0371 /// mapped to ivec4(255, 127, 0, 255). 0372 /// 0373 /// \param name Name of the uniform variable in GLSL 0374 /// \param vector Value of the ivec4 vector 0375 /// 0376 //////////////////////////////////////////////////////////// 0377 void setUniform(const std::string& name, const Glsl::Ivec4& vector); 0378 0379 //////////////////////////////////////////////////////////// 0380 /// \brief Specify value for \p bool uniform 0381 /// 0382 /// \param name Name of the uniform variable in GLSL 0383 /// \param x Value of the bool scalar 0384 /// 0385 //////////////////////////////////////////////////////////// 0386 void setUniform(const std::string& name, bool x); 0387 0388 //////////////////////////////////////////////////////////// 0389 /// \brief Specify value for \p bvec2 uniform 0390 /// 0391 /// \param name Name of the uniform variable in GLSL 0392 /// \param vector Value of the bvec2 vector 0393 /// 0394 //////////////////////////////////////////////////////////// 0395 void setUniform(const std::string& name, const Glsl::Bvec2& vector); 0396 0397 //////////////////////////////////////////////////////////// 0398 /// \brief Specify value for \p bvec3 uniform 0399 /// 0400 /// \param name Name of the uniform variable in GLSL 0401 /// \param vector Value of the bvec3 vector 0402 /// 0403 //////////////////////////////////////////////////////////// 0404 void setUniform(const std::string& name, const Glsl::Bvec3& vector); 0405 0406 //////////////////////////////////////////////////////////// 0407 /// \brief Specify value for \p bvec4 uniform 0408 /// 0409 /// \param name Name of the uniform variable in GLSL 0410 /// \param vector Value of the bvec4 vector 0411 /// 0412 //////////////////////////////////////////////////////////// 0413 void setUniform(const std::string& name, const Glsl::Bvec4& vector); 0414 0415 //////////////////////////////////////////////////////////// 0416 /// \brief Specify value for \p mat3 matrix 0417 /// 0418 /// \param name Name of the uniform variable in GLSL 0419 /// \param matrix Value of the mat3 matrix 0420 /// 0421 //////////////////////////////////////////////////////////// 0422 void setUniform(const std::string& name, const Glsl::Mat3& matrix); 0423 0424 //////////////////////////////////////////////////////////// 0425 /// \brief Specify value for \p mat4 matrix 0426 /// 0427 /// \param name Name of the uniform variable in GLSL 0428 /// \param matrix Value of the mat4 matrix 0429 /// 0430 //////////////////////////////////////////////////////////// 0431 void setUniform(const std::string& name, const Glsl::Mat4& matrix); 0432 0433 //////////////////////////////////////////////////////////// 0434 /// \brief Specify a texture as \p sampler2D uniform 0435 /// 0436 /// \a name is the name of the variable to change in the shader. 0437 /// The corresponding parameter in the shader must be a 2D texture 0438 /// (\p sampler2D GLSL type). 0439 /// 0440 /// Example: 0441 /// \code 0442 /// uniform sampler2D the_texture; // this is the variable in the shader 0443 /// \endcode 0444 /// \code 0445 /// sf::Texture texture; 0446 /// ... 0447 /// shader.setUniform("the_texture", texture); 0448 /// \endcode 0449 /// It is important to note that \a texture must remain alive as long 0450 /// as the shader uses it, no copy is made internally. 0451 /// 0452 /// To use the texture of the object being drawn, which cannot be 0453 /// known in advance, you can pass the special value 0454 /// sf::Shader::CurrentTexture: 0455 /// \code 0456 /// shader.setUniform("the_texture", sf::Shader::CurrentTexture). 0457 /// \endcode 0458 /// 0459 /// \param name Name of the texture in the shader 0460 /// \param texture Texture to assign 0461 /// 0462 //////////////////////////////////////////////////////////// 0463 void setUniform(const std::string& name, const Texture& texture); 0464 0465 //////////////////////////////////////////////////////////// 0466 /// \brief Specify current texture as \p sampler2D uniform 0467 /// 0468 /// This overload maps a shader texture variable to the 0469 /// texture of the object being drawn, which cannot be 0470 /// known in advance. The second argument must be 0471 /// sf::Shader::CurrentTexture. 0472 /// The corresponding parameter in the shader must be a 2D texture 0473 /// (\p sampler2D GLSL type). 0474 /// 0475 /// Example: 0476 /// \code 0477 /// uniform sampler2D current; // this is the variable in the shader 0478 /// \endcode 0479 /// \code 0480 /// shader.setUniform("current", sf::Shader::CurrentTexture); 0481 /// \endcode 0482 /// 0483 /// \param name Name of the texture in the shader 0484 /// 0485 //////////////////////////////////////////////////////////// 0486 void setUniform(const std::string& name, CurrentTextureType); 0487 0488 //////////////////////////////////////////////////////////// 0489 /// \brief Specify values for \p float[] array uniform 0490 /// 0491 /// \param name Name of the uniform variable in GLSL 0492 /// \param scalarArray pointer to array of \p float values 0493 /// \param length Number of elements in the array 0494 /// 0495 //////////////////////////////////////////////////////////// 0496 void setUniformArray(const std::string& name, const float* scalarArray, std::size_t length); 0497 0498 //////////////////////////////////////////////////////////// 0499 /// \brief Specify values for \p vec2[] array uniform 0500 /// 0501 /// \param name Name of the uniform variable in GLSL 0502 /// \param vectorArray pointer to array of \p vec2 values 0503 /// \param length Number of elements in the array 0504 /// 0505 //////////////////////////////////////////////////////////// 0506 void setUniformArray(const std::string& name, const Glsl::Vec2* vectorArray, std::size_t length); 0507 0508 //////////////////////////////////////////////////////////// 0509 /// \brief Specify values for \p vec3[] array uniform 0510 /// 0511 /// \param name Name of the uniform variable in GLSL 0512 /// \param vectorArray pointer to array of \p vec3 values 0513 /// \param length Number of elements in the array 0514 /// 0515 //////////////////////////////////////////////////////////// 0516 void setUniformArray(const std::string& name, const Glsl::Vec3* vectorArray, std::size_t length); 0517 0518 //////////////////////////////////////////////////////////// 0519 /// \brief Specify values for \p vec4[] array uniform 0520 /// 0521 /// \param name Name of the uniform variable in GLSL 0522 /// \param vectorArray pointer to array of \p vec4 values 0523 /// \param length Number of elements in the array 0524 /// 0525 //////////////////////////////////////////////////////////// 0526 void setUniformArray(const std::string& name, const Glsl::Vec4* vectorArray, std::size_t length); 0527 0528 //////////////////////////////////////////////////////////// 0529 /// \brief Specify values for \p mat3[] array uniform 0530 /// 0531 /// \param name Name of the uniform variable in GLSL 0532 /// \param matrixArray pointer to array of \p mat3 values 0533 /// \param length Number of elements in the array 0534 /// 0535 //////////////////////////////////////////////////////////// 0536 void setUniformArray(const std::string& name, const Glsl::Mat3* matrixArray, std::size_t length); 0537 0538 //////////////////////////////////////////////////////////// 0539 /// \brief Specify values for \p mat4[] array uniform 0540 /// 0541 /// \param name Name of the uniform variable in GLSL 0542 /// \param matrixArray pointer to array of \p mat4 values 0543 /// \param length Number of elements in the array 0544 /// 0545 //////////////////////////////////////////////////////////// 0546 void setUniformArray(const std::string& name, const Glsl::Mat4* matrixArray, std::size_t length); 0547 0548 //////////////////////////////////////////////////////////// 0549 /// \brief Change a float parameter of the shader 0550 /// 0551 /// \deprecated Use setUniform(const std::string&, float) instead. 0552 /// 0553 //////////////////////////////////////////////////////////// 0554 SFML_DEPRECATED void setParameter(const std::string& name, float x); 0555 0556 //////////////////////////////////////////////////////////// 0557 /// \brief Change a 2-components vector parameter of the shader 0558 /// 0559 /// \deprecated Use setUniform(const std::string&, const Glsl::Vec2&) instead. 0560 /// 0561 //////////////////////////////////////////////////////////// 0562 SFML_DEPRECATED void setParameter(const std::string& name, float x, float y); 0563 0564 //////////////////////////////////////////////////////////// 0565 /// \brief Change a 3-components vector parameter of the shader 0566 /// 0567 /// \deprecated Use setUniform(const std::string&, const Glsl::Vec3&) instead. 0568 /// 0569 //////////////////////////////////////////////////////////// 0570 SFML_DEPRECATED void setParameter(const std::string& name, float x, float y, float z); 0571 0572 //////////////////////////////////////////////////////////// 0573 /// \brief Change a 4-components vector parameter of the shader 0574 /// 0575 /// \deprecated Use setUniform(const std::string&, const Glsl::Vec4&) instead. 0576 /// 0577 //////////////////////////////////////////////////////////// 0578 SFML_DEPRECATED void setParameter(const std::string& name, float x, float y, float z, float w); 0579 0580 //////////////////////////////////////////////////////////// 0581 /// \brief Change a 2-components vector parameter of the shader 0582 /// 0583 /// \deprecated Use setUniform(const std::string&, const Glsl::Vec2&) instead. 0584 /// 0585 //////////////////////////////////////////////////////////// 0586 SFML_DEPRECATED void setParameter(const std::string& name, const Vector2f& vector); 0587 0588 //////////////////////////////////////////////////////////// 0589 /// \brief Change a 3-components vector parameter of the shader 0590 /// 0591 /// \deprecated Use setUniform(const std::string&, const Glsl::Vec3&) instead. 0592 /// 0593 //////////////////////////////////////////////////////////// 0594 SFML_DEPRECATED void setParameter(const std::string& name, const Vector3f& vector); 0595 0596 //////////////////////////////////////////////////////////// 0597 /// \brief Change a color parameter of the shader 0598 /// 0599 /// \deprecated Use setUniform(const std::string&, const Glsl::Vec4&) instead. 0600 /// 0601 //////////////////////////////////////////////////////////// 0602 SFML_DEPRECATED void setParameter(const std::string& name, const Color& color); 0603 0604 //////////////////////////////////////////////////////////// 0605 /// \brief Change a matrix parameter of the shader 0606 /// 0607 /// \deprecated Use setUniform(const std::string&, const Glsl::Mat4&) instead. 0608 /// 0609 //////////////////////////////////////////////////////////// 0610 SFML_DEPRECATED void setParameter(const std::string& name, const Transform& transform); 0611 0612 //////////////////////////////////////////////////////////// 0613 /// \brief Change a texture parameter of the shader 0614 /// 0615 /// \deprecated Use setUniform(const std::string&, const Texture&) instead. 0616 /// 0617 //////////////////////////////////////////////////////////// 0618 SFML_DEPRECATED void setParameter(const std::string& name, const Texture& texture); 0619 0620 //////////////////////////////////////////////////////////// 0621 /// \brief Change a texture parameter of the shader 0622 /// 0623 /// \deprecated Use setUniform(const std::string&, CurrentTextureType) instead. 0624 /// 0625 //////////////////////////////////////////////////////////// 0626 SFML_DEPRECATED void setParameter(const std::string& name, CurrentTextureType); 0627 0628 //////////////////////////////////////////////////////////// 0629 /// \brief Get the underlying OpenGL handle of the shader. 0630 /// 0631 /// You shouldn't need to use this function, unless you have 0632 /// very specific stuff to implement that SFML doesn't support, 0633 /// or implement a temporary workaround until a bug is fixed. 0634 /// 0635 /// \return OpenGL handle of the shader or 0 if not yet loaded 0636 /// 0637 //////////////////////////////////////////////////////////// 0638 unsigned int getNativeHandle() const; 0639 0640 //////////////////////////////////////////////////////////// 0641 /// \brief Bind a shader for rendering 0642 /// 0643 /// This function is not part of the graphics API, it mustn't be 0644 /// used when drawing SFML entities. It must be used only if you 0645 /// mix sf::Shader with OpenGL code. 0646 /// 0647 /// \code 0648 /// sf::Shader s1, s2; 0649 /// ... 0650 /// sf::Shader::bind(&s1); 0651 /// // draw OpenGL stuff that use s1... 0652 /// sf::Shader::bind(&s2); 0653 /// // draw OpenGL stuff that use s2... 0654 /// sf::Shader::bind(NULL); 0655 /// // draw OpenGL stuff that use no shader... 0656 /// \endcode 0657 /// 0658 /// \param shader Shader to bind, can be null to use no shader 0659 /// 0660 //////////////////////////////////////////////////////////// 0661 static void bind(const Shader* shader); 0662 0663 //////////////////////////////////////////////////////////// 0664 /// \brief Tell whether or not the system supports shaders 0665 /// 0666 /// This function should always be called before using 0667 /// the shader features. If it returns false, then 0668 /// any attempt to use sf::Shader will fail. 0669 /// 0670 /// \return True if shaders are supported, false otherwise 0671 /// 0672 //////////////////////////////////////////////////////////// 0673 static bool isAvailable(); 0674 0675 //////////////////////////////////////////////////////////// 0676 /// \brief Tell whether or not the system supports geometry shaders 0677 /// 0678 /// This function should always be called before using 0679 /// the geometry shader features. If it returns false, then 0680 /// any attempt to use sf::Shader geometry shader features will fail. 0681 /// 0682 /// This function can only return true if isAvailable() would also 0683 /// return true, since shaders in general have to be supported in 0684 /// order for geometry shaders to be supported as well. 0685 /// 0686 /// Note: The first call to this function, whether by your 0687 /// code or SFML will result in a context switch. 0688 /// 0689 /// \return True if geometry shaders are supported, false otherwise 0690 /// 0691 //////////////////////////////////////////////////////////// 0692 static bool isGeometryAvailable(); 0693 0694 private: 0695 0696 //////////////////////////////////////////////////////////// 0697 /// \brief Compile the shader(s) and create the program 0698 /// 0699 /// If one of the arguments is NULL, the corresponding shader 0700 /// is not created. 0701 /// 0702 /// \param vertexShaderCode Source code of the vertex shader 0703 /// \param geometryShaderCode Source code of the geometry shader 0704 /// \param fragmentShaderCode Source code of the fragment shader 0705 /// 0706 /// \return True on success, false if any error happened 0707 /// 0708 //////////////////////////////////////////////////////////// 0709 bool compile(const char* vertexShaderCode, const char* geometryShaderCode, const char* fragmentShaderCode); 0710 0711 //////////////////////////////////////////////////////////// 0712 /// \brief Bind all the textures used by the shader 0713 /// 0714 /// This function each texture to a different unit, and 0715 /// updates the corresponding variables in the shader accordingly. 0716 /// 0717 //////////////////////////////////////////////////////////// 0718 void bindTextures() const; 0719 0720 //////////////////////////////////////////////////////////// 0721 /// \brief Get the location ID of a shader uniform 0722 /// 0723 /// \param name Name of the uniform variable to search 0724 /// 0725 /// \return Location ID of the uniform, or -1 if not found 0726 /// 0727 //////////////////////////////////////////////////////////// 0728 int getUniformLocation(const std::string& name); 0729 0730 //////////////////////////////////////////////////////////// 0731 /// \brief RAII object to save and restore the program 0732 /// binding while uniforms are being set 0733 /// 0734 /// Implementation is private in the .cpp file. 0735 /// 0736 //////////////////////////////////////////////////////////// 0737 struct UniformBinder; 0738 0739 //////////////////////////////////////////////////////////// 0740 // Types 0741 //////////////////////////////////////////////////////////// 0742 typedef std::map<int, const Texture*> TextureTable; 0743 typedef std::map<std::string, int> UniformTable; 0744 0745 //////////////////////////////////////////////////////////// 0746 // Member data 0747 //////////////////////////////////////////////////////////// 0748 unsigned int m_shaderProgram; //!< OpenGL identifier for the program 0749 int m_currentTexture; //!< Location of the current texture in the shader 0750 TextureTable m_textures; //!< Texture variables in the shader, mapped to their location 0751 UniformTable m_uniforms; //!< Parameters location cache 0752 }; 0753 0754 } // namespace sf 0755 0756 0757 #endif // SFML_SHADER_HPP 0758 0759 0760 //////////////////////////////////////////////////////////// 0761 /// \class sf::Shader 0762 /// \ingroup graphics 0763 /// 0764 /// Shaders are programs written using a specific language, 0765 /// executed directly by the graphics card and allowing 0766 /// to apply real-time operations to the rendered entities. 0767 /// 0768 /// There are three kinds of shaders: 0769 /// \li %Vertex shaders, that process vertices 0770 /// \li Geometry shaders, that process primitives 0771 /// \li Fragment (pixel) shaders, that process pixels 0772 /// 0773 /// A sf::Shader can be composed of either a vertex shader 0774 /// alone, a geometry shader alone, a fragment shader alone, 0775 /// or any combination of them. (see the variants of the 0776 /// load functions). 0777 /// 0778 /// Shaders are written in GLSL, which is a C-like 0779 /// language dedicated to OpenGL shaders. You'll probably 0780 /// need to learn its basics before writing your own shaders 0781 /// for SFML. 0782 /// 0783 /// Like any C/C++ program, a GLSL shader has its own variables 0784 /// called \a uniforms that you can set from your C++ application. 0785 /// sf::Shader handles different types of uniforms: 0786 /// \li scalars: \p float, \p int, \p bool 0787 /// \li vectors (2, 3 or 4 components) 0788 /// \li matrices (3x3 or 4x4) 0789 /// \li samplers (textures) 0790 /// 0791 /// Some SFML-specific types can be converted: 0792 /// \li sf::Color as a 4D vector (\p vec4) 0793 /// \li sf::Transform as matrices (\p mat3 or \p mat4) 0794 /// 0795 /// Every uniform variable in a shader can be set through one of the 0796 /// setUniform() or setUniformArray() overloads. For example, if you 0797 /// have a shader with the following uniforms: 0798 /// \code 0799 /// uniform float offset; 0800 /// uniform vec3 point; 0801 /// uniform vec4 color; 0802 /// uniform mat4 matrix; 0803 /// uniform sampler2D overlay; 0804 /// uniform sampler2D current; 0805 /// \endcode 0806 /// You can set their values from C++ code as follows, using the types 0807 /// defined in the sf::Glsl namespace: 0808 /// \code 0809 /// shader.setUniform("offset", 2.f); 0810 /// shader.setUniform("point", sf::Vector3f(0.5f, 0.8f, 0.3f)); 0811 /// shader.setUniform("color", sf::Glsl::Vec4(color)); // color is a sf::Color 0812 /// shader.setUniform("matrix", sf::Glsl::Mat4(transform)); // transform is a sf::Transform 0813 /// shader.setUniform("overlay", texture); // texture is a sf::Texture 0814 /// shader.setUniform("current", sf::Shader::CurrentTexture); 0815 /// \endcode 0816 /// 0817 /// The old setParameter() overloads are deprecated and will be removed in a 0818 /// future version. You should use their setUniform() equivalents instead. 0819 /// 0820 /// The special Shader::CurrentTexture argument maps the 0821 /// given \p sampler2D uniform to the current texture of the 0822 /// object being drawn (which cannot be known in advance). 0823 /// 0824 /// To apply a shader to a drawable, you must pass it as an 0825 /// additional parameter to the \ref RenderWindow::draw function: 0826 /// \code 0827 /// window.draw(sprite, &shader); 0828 /// \endcode 0829 /// 0830 /// ... which is in fact just a shortcut for this: 0831 /// \code 0832 /// sf::RenderStates states; 0833 /// states.shader = &shader; 0834 /// window.draw(sprite, states); 0835 /// \endcode 0836 /// 0837 /// In the code above we pass a pointer to the shader, because it may 0838 /// be null (which means "no shader"). 0839 /// 0840 /// Shaders can be used on any drawable, but some combinations are 0841 /// not interesting. For example, using a vertex shader on a sf::Sprite 0842 /// is limited because there are only 4 vertices, the sprite would 0843 /// have to be subdivided in order to apply wave effects. 0844 /// Another bad example is a fragment shader with sf::Text: the texture 0845 /// of the text is not the actual text that you see on screen, it is 0846 /// a big texture containing all the characters of the font in an 0847 /// arbitrary order; thus, texture lookups on pixels other than the 0848 /// current one may not give you the expected result. 0849 /// 0850 /// Shaders can also be used to apply global post-effects to the 0851 /// current contents of the target (like the old sf::PostFx class 0852 /// in SFML 1). This can be done in two different ways: 0853 /// \li draw everything to a sf::RenderTexture, then draw it to 0854 /// the main target using the shader 0855 /// \li draw everything directly to the main target, then use 0856 /// sf::Texture::update(Window&) to copy its contents to a texture 0857 /// and draw it to the main target using the shader 0858 /// 0859 /// The first technique is more optimized because it doesn't involve 0860 /// retrieving the target's pixels to system memory, but the 0861 /// second one doesn't impact the rendering process and can be 0862 /// easily inserted anywhere without impacting all the code. 0863 /// 0864 /// Like sf::Texture that can be used as a raw OpenGL texture, 0865 /// sf::Shader can also be used directly as a raw shader for 0866 /// custom OpenGL geometry. 0867 /// \code 0868 /// sf::Shader::bind(&shader); 0869 /// ... render OpenGL geometry ... 0870 /// sf::Shader::bind(NULL); 0871 /// \endcode 0872 /// 0873 /// \see sf::Glsl 0874 /// 0875 ////////////////////////////////////////////////////////////
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|