FS#25 - Change sf::PostFx to a more general purpose pixel shader class (sf::Shader)
Updated the PostFx sample, renamed to Shader Renamed all the static X::CanUseX() functions to X::IsAvailable() to make the API more consistent Moved .def files from /build/VC200X to /src in CSFML Minors fixes in CSFML git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1258 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
63e07cec84
commit
d7bd00afc0
125 changed files with 1606 additions and 2348 deletions
|
@ -107,13 +107,13 @@ private :
|
|||
/// and adds a function to retrieve the recorded sound buffer
|
||||
/// (GetBuffer()).
|
||||
///
|
||||
/// As usual, don't forget to call the CanCapture() function
|
||||
/// As usual, don't forget to call the IsAvailable() function
|
||||
/// before using this class (see sf::SoundRecorder for more details
|
||||
/// about this).
|
||||
///
|
||||
/// Usage example:
|
||||
/// \code
|
||||
/// if (SoundBufferRecorder::CanCapture())
|
||||
/// if (SoundBufferRecorder::IsAvailable())
|
||||
/// {
|
||||
/// // Record some audio data
|
||||
/// SoundBufferRecorder recorder;
|
||||
|
|
|
@ -96,7 +96,7 @@ public :
|
|||
/// \return True if audio capture is supported, false otherwise
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static bool CanCapture();
|
||||
static bool IsAvailable();
|
||||
|
||||
protected :
|
||||
|
||||
|
@ -212,7 +212,7 @@ private :
|
|||
///
|
||||
/// The audio capture feature may not be supported or activated
|
||||
/// on every platform, thus it is recommended to check its
|
||||
/// availability with the CanCapture() function. If it returns
|
||||
/// availability with the IsAvailable() function. If it returns
|
||||
/// false, then any attempt to use an audio recorder will fail.
|
||||
///
|
||||
/// It is important to note that the audio capture happens in a
|
||||
|
@ -253,7 +253,7 @@ private :
|
|||
/// }
|
||||
///
|
||||
/// // Usage
|
||||
/// if (CustomRecorder::CanCapture())
|
||||
/// if (CustomRecorder::IsAvailable())
|
||||
/// {
|
||||
/// CustomRecorder recorder;
|
||||
/// recorder.Start();
|
||||
|
|
|
@ -34,10 +34,10 @@
|
|||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <SFML/Graphics/Glyph.hpp>
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include <SFML/Graphics/PostFX.hpp>
|
||||
#include <SFML/Graphics/RenderImage.hpp>
|
||||
#include <SFML/Graphics/RenderQueue.hpp>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/Graphics/Shader.hpp>
|
||||
#include <SFML/Graphics/Shape.hpp>
|
||||
#include <SFML/Graphics/Sprite.hpp>
|
||||
#include <SFML/Graphics/String.hpp>
|
||||
|
|
|
@ -119,7 +119,7 @@ public :
|
|||
/// \return True if the RenderImage class can be used
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static bool CanUseRenderImage();
|
||||
static bool IsAvailable();
|
||||
|
||||
private :
|
||||
|
||||
|
|
|
@ -31,7 +31,6 @@
|
|||
#include <SFML/Config.hpp>
|
||||
#include <SFML/System/NonCopyable.hpp>
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include <SFML/Graphics/Matrix3.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <vector>
|
||||
|
@ -45,6 +44,9 @@ namespace priv
|
|||
class GeometryRenderer;
|
||||
}
|
||||
|
||||
class Image;
|
||||
class Shader;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Implements a queue of rendering commands
|
||||
///
|
||||
|
@ -194,6 +196,17 @@ public :
|
|||
////////////////////////////////////////////////////////////
|
||||
void SetTexture(const Image* texture);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Set the current shader
|
||||
///
|
||||
/// Note: any call to this function after a call to BeginBatch
|
||||
/// will be ignored, and delayed until BeginBatch is called again.
|
||||
///
|
||||
/// \param shader New Shader
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetShader(const Shader* shader);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Begin a new geometry batch
|
||||
///
|
||||
|
@ -326,6 +339,7 @@ private :
|
|||
RenderStates* myCurrentStates; ///< Current set of render states
|
||||
Matrix3 myCurrentTransform; ///< Current combined projection-model-view matrix
|
||||
const Image* myCurrentTexture; ///< Current texture
|
||||
const Shader* myCurrentShader; ///< Current pixel shader
|
||||
Blend::Mode myCurrentBlendMode; ///< Current blending mode
|
||||
IntRect myCurrentViewport; ///< Current target viewport
|
||||
Vector2f myCurrentViewportSize; ///< Size of the current viewport (for vertex calculations)
|
||||
|
@ -358,10 +372,8 @@ private :
|
|||
///
|
||||
/// Usage example:
|
||||
/// \begincode
|
||||
/// void MyDrawable::Render(sf::RenderTarget& target)
|
||||
/// void MyDrawable::Render(sf::RenderTarget& target, sf::RenderQueue& queue)
|
||||
/// {
|
||||
/// RenderQueue& queue = target.GetRenderQueue();
|
||||
///
|
||||
/// queue.SetTexture(myImage);
|
||||
/// queue.BeginBatch();
|
||||
/// {
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
namespace sf
|
||||
{
|
||||
class Drawable;
|
||||
class Shader;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Base class for all render targets (window, image, ...)
|
||||
|
@ -65,7 +66,16 @@ public :
|
|||
/// \param object : Object to draw
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual void Draw(const Drawable& object);
|
||||
void Draw(const Drawable& object);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Draw something into the target with a shader
|
||||
///
|
||||
/// \param object : Object to draw
|
||||
/// \param shader : Shader to apply
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Draw(const Drawable& object, const Shader& shader);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Make sure that what has been drawn so far is rendered
|
||||
|
|
|
@ -36,8 +36,6 @@
|
|||
|
||||
namespace sf
|
||||
{
|
||||
class Drawable;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Simple wrapper for sf::Window that allows easy
|
||||
/// 2D rendering
|
||||
|
|
|
@ -22,15 +22,16 @@
|
|||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef SFML_POSTFX_HPP
|
||||
#define SFML_POSTFX_HPP
|
||||
#ifndef SFML_SHADER_HPP
|
||||
#define SFML_SHADER_HPP
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Config.hpp>
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include <istream>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/System/Vector3.hpp>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
@ -38,9 +39,9 @@
|
|||
namespace sf
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// PostFX is used to apply a post effect to a window
|
||||
/// Wrapper for pixel shaders
|
||||
////////////////////////////////////////////////////////////
|
||||
class SFML_API PostFX : public Drawable
|
||||
class SFML_API Shader
|
||||
{
|
||||
public :
|
||||
|
||||
|
@ -48,7 +49,7 @@ public :
|
|||
/// Default constructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
PostFX();
|
||||
Shader();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Copy constructor
|
||||
|
@ -56,18 +57,18 @@ public :
|
|||
/// \param copy : Instance to copy
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
PostFX(const PostFX& copy);
|
||||
Shader(const Shader& copy);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
~PostFX();
|
||||
~Shader();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Load the effect from a file
|
||||
/// Load the shader from a file
|
||||
///
|
||||
/// \param filename : Path of the effect file to load
|
||||
/// \param filename : Path of the shader file to load
|
||||
///
|
||||
/// \return True on success
|
||||
///
|
||||
|
@ -75,120 +76,138 @@ public :
|
|||
bool LoadFromFile(const std::string& filename);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Load the effect from a text in memory
|
||||
/// Load the shader from a text in memory
|
||||
///
|
||||
/// \param effect : String containing the code of the effect
|
||||
/// \param effect : String containing the code of the shader
|
||||
///
|
||||
/// \return True on success
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool LoadFromMemory(const std::string& effect);
|
||||
bool LoadFromMemory(const std::string& shader);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change a parameter of the effect (1 float)
|
||||
/// Change a parameter of the shader (1 float)
|
||||
///
|
||||
/// \param name : Name of the parameter in the effect
|
||||
/// \param name : Name of the parameter in the shader
|
||||
/// \param x : Value to assign
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetParameter(const std::string& name, float x);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change a parameter of the effect (2 floats)
|
||||
/// Change a parameter of the shader (2 floats)
|
||||
///
|
||||
/// \param name : Name of the parameter in the effect
|
||||
/// \param name : Name of the parameter in the shader
|
||||
/// \param x, y : Values to assign
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetParameter(const std::string& Name, float x, float y);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change a parameter of the effect (3 floats)
|
||||
/// Change a parameter of the shader (3 floats)
|
||||
///
|
||||
/// \param name : Name of the parameter in the effect
|
||||
/// \param name : Name of the parameter in the shader
|
||||
/// \param x, y, z : Values to assign
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetParameter(const std::string& Name, float x, float y, float z);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change a parameter of the effect (4 floats)
|
||||
/// Change a parameter of the shader (4 floats)
|
||||
///
|
||||
/// \param name : Name of the parameter in the effect
|
||||
/// \param name : Name of the parameter in the shader
|
||||
/// \param x, y, z, w : Values to assign
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetParameter(const std::string& Name, float x, float y, float Z, float w);
|
||||
void SetParameter(const std::string& Name, float x, float y, float z, float w);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change a parameter of the shader (1 vector2)
|
||||
///
|
||||
/// \param name : Name of the parameter in the shader
|
||||
/// \param v : Value to assign
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetParameter(const std::string& name, const Vector2f& v);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change a parameter of the shader (1 vector3)
|
||||
///
|
||||
/// \param name : Name of the parameter in the shader
|
||||
/// \param v : Value to assign
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetParameter(const std::string& name, const Vector3f& v);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Set a texture parameter
|
||||
///
|
||||
/// \param name : Name of the texture in the effect
|
||||
/// \param texture : Image to set (pass NULL to use content of current framebuffer)
|
||||
/// \param name : Name of the texture in the shader
|
||||
/// \param texture : Image to set (pass Shader::CurrentTexture to use the current texture)
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetTexture(const std::string& name, const Image* texture);
|
||||
void SetTexture(const std::string& name, const Image& texture);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Bind the shader for rendering
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Bind() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Unbind the shader
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Unbind() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Assignment operator
|
||||
///
|
||||
/// \param other : Instance to assign
|
||||
/// \param right Instance to assign
|
||||
///
|
||||
/// \return Reference to the post-effect
|
||||
/// \return Reference to self
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
PostFX& operator =(const PostFX& other);
|
||||
Shader& operator =(const Shader& right);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Tell whether or not the system supports post-effects
|
||||
/// Tell whether or not the system supports shaders
|
||||
///
|
||||
/// \return True if the system can use post-effects
|
||||
/// \return True if the system can use shaders
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static bool CanUsePostFX();
|
||||
|
||||
protected :
|
||||
static bool IsAvailable();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// /see Drawable::Render
|
||||
///
|
||||
// Static member data
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual void Render(RenderTarget& target, RenderQueue& queue) const;
|
||||
static const Image CurrentTexture; ///< Special image representing the texture used by the object being drawn
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Preprocess a SFML effect file
|
||||
/// to convert it to a valid GLSL fragment shader
|
||||
///
|
||||
/// \param file : Stream containing the code to process
|
||||
///
|
||||
/// \return Valid fragment shader source code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
static std::string PreprocessEffect(std::istream& file);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Create the program and attach the shaders
|
||||
///
|
||||
/// \return True on success, false if any error happened
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void CreateProgram();
|
||||
bool CompileProgram();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Types
|
||||
////////////////////////////////////////////////////////////
|
||||
typedef std::map<std::string, const Image*> TextureTable;
|
||||
typedef std::map<int, const Image*> TextureTable;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
unsigned int myShaderProgram; ///< OpenGL identifier for the program
|
||||
TextureTable myTextures; ///< Texture variables in the effect
|
||||
std::string myFragmentShader; ///< Fragment shader source code
|
||||
mutable Image myFrameBuffer; ///< Texture containing the current frame buffer
|
||||
unsigned int myShaderProgram; ///< OpenGL identifier for the program
|
||||
int myCurrentTexture; ///< Location of the current texture in the shader
|
||||
TextureTable myTextures; ///< Texture variables in the shader, mapped to their location
|
||||
std::string myFragmentShader; ///< Fragment shader source code
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
||||
|
||||
#endif // SFML_POSTFX_HPP
|
||||
#endif // SFML_SHADER_HPP
|
Loading…
Add table
Add a link
Reference in a new issue