Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Custom Blending Modes and other requests  (Read 8259 times)

0 Members and 1 Guest are viewing this topic.

TurboLento

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • http://pjmendes.blogspot.com/
Custom Blending Modes and other requests
« on: August 30, 2010, 02:03:06 am »
Hi, Laurent. I'd like to present a few ideas and to humbly request for a few features to allow such ideas to be made.

Recently I've been looking into how to go about creating Masking/Sub Windows using SFML/OpenGL, using masks including their alpha information (so that we can have windows with areas with partial transparency, etc)

An example of what I mean:
Using the following 3 images, the first would be the window contents, the second would serve as the window mask (the grey rectangles area has alpha = 0, and some areas near the inner borders have slightly transparent areas), and the last image would be the background.


This would result in the image:

Note the intended fading detail around the mask borders.

After a week of research and experimenting, I found the following solutions as to how to go about doing this, using OpenGL:

- use the stencil buffer (works ok, except that I couldn't get the stencil buffer to store alpha information; not supported in older hardware)
- multi-texturing (complicated)
- render to texture, using multiple images with the intended alphas and blending modes (not supported in older hardware and some mobile devices (i.e. IPhone, from what I gather))
- using blending, writing to the color buffer and defining appropriate blending modes, and overlaying images (works well and is the most supported action, including very old hardware).

So I've decided to go with the last option. I've managed to implement this using plain OpenGL, and by doing the following actions:

- draw background image(s)
- enable blending
- disable writing to r,g or b color channels, allow only writing to alpha channel in the color buffer (glColorMask(false, false, false, true))
- set blend mode to glBlendFunc(GL_SRC_ALPHA, GL_ZERO) (include the source (picture we are drawing) alpha, ignore the destination (info already in color buffer) alpha)
- draw mask (image containing stuff to mask by having pixels with alpha < 1)
- enable writing to all channels in the color buffer with glColorMask(true, true, true, true)
- set blend mode to write pixels taking in consideration the alpha in the DESTINATION (what's already written to the color buffer's alpha channel) glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA); (blend with alpha info in color buffer, which was written by previous image)
- draw images that are inside the window (thus masking their pixels using info in the color buffer)
- repeat previous steps for each supplemental image to draw inside the "window"/mask

I see requests for these features in several other threads (both masking and custom blend modes), so I think this is of interest to current and future SFML users.

So here comes the request. I'd like if one of these were made officially in future SFML releases:

OPTION 1 (simpler in my opinion):
- make Drawable::Draw() virtual (so as to allow to inherit from Sprite, redefine Sprite::Draw, and create custom blending modes, or adjust the code called in Drawable::Draw to our needs).

OPTION 2:
- create a custom BlendMode option and allow passing direct OpenGL glBlend parameters, or a more high-level approach, as you have with BlendMode::Add, Multiply etc.

I'd be willing to post code/do a wiki on how to use masking to do a window effect.

Thanks!
Game development blog: http://pjmendes.blogspot.com/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Custom Blending Modes and other requests
« Reply #1 on: August 30, 2010, 07:27:14 pm »
Hi

Quote
OPTION 1 (simpler in my opinion):
- make Drawable::Draw() virtual (so as to allow to inherit from Sprite, redefine Sprite::Draw, and create custom blending modes, or adjust the code called in Drawable::Draw to our needs).

This is not a good idea, this function contains too many internal details that need to be there, people can't customize it easily.
Moreover, this is not possible in SFML 2 anymore, because Drawable::Draw uses sf::Renderer, not direct OpenGL calls. So I would have to add more features to sf::Renderer as well.

Quote
OPTION 2:
- create a custom BlendMode option and allow passing direct OpenGL glBlend parameters, or a more high-level approach, as you have with BlendMode::Add, Multiply etc.

This is a better solution, but I want to keep things simple, I don't want to have thousands of blending modes.

Both solutions forget the glColorMask stuff. It probably "works" if you call it directly in SFML 1, but it will definitely not work in SFML 2, it would need a special API.

This doesn't mean that I don't care about image masks, but there is maybe another solution that doesn't require to "pollute" the graphics API, maybe something more specific; a solution that would work with geometric masks as well.

And I'm currently thinking about modifications in the graphics module, and it will probably make this kind of things easier to implement.
Laurent Gomila - SFML developer

TurboLento

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • http://pjmendes.blogspot.com/
Custom Blending Modes and other requests
« Reply #2 on: September 12, 2010, 10:35:36 pm »
Ok, I understand that you pay a lot of attention and thought to the structure and cleanliness of SFML, which shows.

I went ahead and implemented a way to use masks as/on Sprites, Shapes and Strings. May not be perfect but it's a start and works for me:). Hope it may help others. It does involve changing the original SFML 1.6 code, adding the "virtual" keyword to the
Code: [Select]
sf::Drawable::Draw function.
I'll add this to the wiki if it's ok.
NOTE: it hasn't been extensively tested so there may be the occasional bug.

Below i'll post the code for several new classes i've added, and then a few example programs on how to use each class.


Ok, to start, you'll have to change a line in the original SFML 1.6 code, and recompile sfml or the "sfml-graphics" lib.

Go to the SFML source root folder, and open "include/SFML/Graphics/Drawable.hpp".
Then find the function definition
Code: [Select]
void Draw(RenderTarget& Target) const; (around line 335)
and replace it with
Code: [Select]
virtual void Draw(RenderTarget& Target) const; (add "virtual" at the start).
And recompile sfml-graphics.

New files:
MaskTypes.h - has the several masking types that you can use. Define a MaskType by using "MaskTypes::Type" and set them using "MaskTypes::BlendWithPreviousMask". The mask types are the following:
    MaskType::None - use the normal sf::BlendMode.
    MaskType::Mask - use this Drawable's Alpha mask as a Mask (only drawing the Drawable's Alpha info, not any color). Will blend the mask with previous existing masks.
    MaskType::MaskOverWritePreviousMask - similar to the previous, but will overwrite the previous mask instead of blending into it (allows to "cut" into an existing mask, and is used by example with "StringMask" - check the example)
    MaskType::BlendWithPreviousMask - set this mask to have a Drawable blend with the mask previously drawn on its area (only draw where alpha > 0)
    MaskType::BlendWithPreviousMaskReversed - set this mask to have a Drawable blend with the NEGATIVE of the mask previously drawn on its area (transparent areas become opaque and vice-versa).[/list]

    MaskUtils.h - Internal class, no need to call this from your code.
    Basically where all the magic happens, OpenGL calls are made, states are saved and restored, etc. Some code that was in "sf::Drawable" related to blend modes was pasted here.
    RenderWindowMask.h - must be used instead of a regular sf::RenderWindow, inherits from it. Adds the "ClearMask()" function, which clears just the color buffer's alpha channel.
    SpriteMasked.h - use this to draw a Sprite using masking, or to use a Sprite AS a mask. Inherits from sf::Sprite. Adds the "maskSetType/maskGetType" functions necessary to define the masking type.
    ShapeMasked.h - use this to use a Shape as a mask or to mask a Shape. Inherits from sf::Shape. Adds the "maskSetType/maskGetType" functions necessary to define the masking type.
    StringMasked.h - use this to use a String as a mask or to mask a String. Inherits from sf::String. Adds the "maskSetType/maskGetType" functions necessary to define the masking type.

    P.S. no need to preserve states (use "App.PreserveOpenGLStates(true)"), it's all done internally.

    MaskTypes.h
    Code: [Select]
    #ifndef MASKTYPES_H_INCLUDED
    #define MASKTYPES_H_INCLUDED

    class MaskTypes
    {
        public:
        enum Type
        {
            None = 0,
            Mask,
            MaskOverWritePreviousMask,
            BlendWithPreviousMask,
            BlendWithPreviousMaskReversed
        };
    };

    #endif // MASKTYPES_H_INCLUDED


    MaskUtils.h
    Code: [Select]

    #ifndef MASKUTILS_H_INCLUDED
    #define MASKUTILS_H_INCLUDED

    #include <SFML/Graphics/Drawable.hpp>

    using namespace sf;

    class MaskUtils
    {
        public:
            MaskUtils()
            {
                reset();
            }

            void reset()
            {
                restoreColorBuffer = false;
                restoreBlendMode = false;
            }

            void setBlendModesBasedOnMaskType(MaskTypes::Type maskType, Blend::Mode blendMode)
            {
                switch(maskType)
                {
                    case MaskTypes::None:
                    {
                        restoreBlendMode = true;
                        blendModePreviouslyEnabled = glIsEnabled(GL_BLEND);

                        // Setup alpha-blending
                        Blend::Mode myBlendMode = blendMode;
                        if (myBlendMode == Blend::None)
                        {
                            glDisable(GL_BLEND);
                        }
                        else
                        {
                            glEnable(GL_BLEND);

                            switch (myBlendMode)
                            {
                                case Blend::Alpha :    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break;
                                case Blend::Add :      glBlendFunc(GL_SRC_ALPHA, GL_ONE);                 break;
                                case Blend::Multiply : glBlendFunc(GL_DST_COLOR, GL_ZERO);                break;
                                default :                                                                 break;
                            }
                        }
                    }
                    break;

                    case MaskTypes::Mask:
                    {
                        blendModePreviouslyEnabled = glIsEnabled(GL_BLEND);
                        glEnable(GL_BLEND);
                        glColorMask(false, false, false, true);// we can just write the alpha info
                        glBlendFunc(GL_DST_ALPHA, GL_SRC_ALPHA); // include the source (picture we are drawing) alpha, ignore the destination (info already in color buffer) alpha
                        restoreColorBuffer = true;
                        restoreBlendMode = true;
                    }
                    break;
                    case MaskTypes::MaskOverWritePreviousMask:
                    {
                        blendModePreviouslyEnabled = glIsEnabled(GL_BLEND);
                        glEnable(GL_BLEND);
                        glColorMask(false, false, false, true);// we can just write the alpha info
                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // include the source (picture we are drawing) alpha, ignore the destination (info already in color buffer) alpha
                        restoreColorBuffer = true;
                        restoreBlendMode = true;
                    }
                    break;
                    case MaskTypes::BlendWithPreviousMask:
                    {
                        blendModePreviouslyEnabled = glIsEnabled(GL_BLEND);

                        // draw stuff inside window
                        glEnable(GL_BLEND);
                        glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA); // blend with alpha info in color buffer, which was written by previous image
                        restoreBlendMode = true;
                    }
                    break;
                    case MaskTypes::BlendWithPreviousMaskReversed:
                    {
                        blendModePreviouslyEnabled = glIsEnabled(GL_BLEND);

                        // draw stuff inside window
                        glEnable(GL_BLEND);
                        glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA); // blend with alpha info in color buffer, which was written by previous image
                        restoreBlendMode = true;
                    }
                    break;
                }
            }

            void restoreStates()
            {
                if(restoreColorBuffer)
                    glColorMask(true, true, true, true);  // make sure that for further images that we are writing all colors

                if(restoreBlendMode)
                {
                    if(blendModePreviouslyEnabled)
                        glEnable(GL_BLEND);
                    else
                        glDisable(GL_BLEND);
                }
            }

        private:
            bool restoreColorBuffer;
            bool restoreBlendMode;
            bool blendModePreviouslyEnabled;
    };

    #endif // MASKUTILS_H_INCLUDED


    RenderWindowMask.h
    Code: [Select]

    #ifndef RENDERWINDOWMASK_H_INCLUDED
    #define RENDERWINDOWMASK_H_INCLUDED

    #include <SFML/Graphics.hpp>
    #include "MaskTypes.h"

    using namespace sf;

    class RenderWindowMask : public RenderWindow
    {
        public:
            RenderWindowMask(
                                VideoMode Mode,
                                const std::string& Title,
                                unsigned long WindowStyle = Style::Resize | Style::Close,
                                const WindowSettings& Params = WindowSettings())
            {
                Create(Mode, Title, WindowStyle, Params);
            }

            RenderWindowMask(   WindowHandle Handle,
                                const WindowSettings& Params = WindowSettings())
            {
                Create(Handle, Params);
            }

            // clears the alpha channel
            void ClearMask()
            {
                glColorMask(false, false, false, true); // disable messing with the rgb channels, just change the alpha
                glClear(GL_COLOR_BUFFER_BIT);    // clear the color buffer (just the alpha channel)
                //glClearColor(FillColor.r / 255.f, FillColor.g / 255.f, FillColor.b / 255.f, FillColor.a / 255.f));
                glColorMask(true, true, true, true); // enable messing with the rgb channels, just change the alpha
            }
    };

    #endif // RENDERWINDOWMASK_H_INCLUDED


    SpriteMasked.h
    Code: [Select]

    #ifndef SPRITEMASKED_H_INCLUDED
    #define SPRITEMASKED_H_INCLUDED

    #include <SFML/Graphics/Sprite.hpp>
    #include "MaskTypes.h"
    #include "MaskUtils.h"

    using namespace sf;

    class SpriteMasked : public Sprite
    {
        public:
            SpriteMasked() :
            Sprite(),
            maskType(MaskTypes::None)
            {
            }

            void Draw(RenderTarget& Target) const
            {
                // Save the current modelview matrix and set the new one
                glMatrixMode(GL_MODELVIEW);
                glPushMatrix();
                glMultMatrixf(GetMatrix().Get4x4Elements());

                MaskUtils maskUtils;
                maskUtils.setBlendModesBasedOnMaskType(this->maskType, GetBlendMode());

                // Set color
                Color myColor = GetColor();
                glColor4f(myColor.r / 255.f, myColor.g / 255.f, myColor.b / 255.f, myColor.a / 255.f);

                // Let the derived class render the object geometry
                Render(Target);

                maskUtils.restoreStates();

                // Restore the previous modelview matrix
                glMatrixMode(GL_MODELVIEW);
                glPopMatrix();
            }

            void maskSetType(MaskTypes::Type maskType)
            {
                this->maskType = maskType;
            }

            MaskTypes::Type maskGetType()
            {
                return this->maskType;
            }

        private:
            MaskTypes::Type maskType;

    };

    #endif // SPRITEMASKED_H_INCLUDED


    ShapeMasked.h
    Code: [Select]

    #ifndef SHAPEMASKED_H_INCLUDED
    #define SHAPEMASKED_H_INCLUDED

    #include <SFML/Graphics/Shape.hpp>
    #include "MaskTypes.h"
    #include "MaskUtils.h"

    using namespace sf;

    class ShapeMasked : public Shape
    {
        public:
            ShapeMasked() :
            Shape(),
            maskType(MaskTypes::None)
            {}

            ShapeMasked(sf::Shape& shape) :
            Shape(),
            maskType(MaskTypes::None)
            {
                // copy points
                for(unsigned int idxPoint = 0; idxPoint < shape.GetNbPoints(); ++idxPoint)
                {
                    this->AddPoint(shape.GetPointPosition(idxPoint), shape.GetPointColor(idxPoint), shape.GetPointOutlineColor(idxPoint));
                }

                this->SetOutlineWidth(shape.GetOutlineWidth());
                this->SetColor(shape.GetColor());
            }

            void Draw(RenderTarget& Target) const
            {
                // Save the current modelview matrix and set the new one
                glMatrixMode(GL_MODELVIEW);
                glPushMatrix();
                glMultMatrixf(GetMatrix().Get4x4Elements());

                MaskUtils maskUtils;
                maskUtils.setBlendModesBasedOnMaskType(this->maskType, GetBlendMode());

                // Set color
                Color myColor = GetColor();
                glColor4f(myColor.r / 255.f, myColor.g / 255.f, myColor.b / 255.f, myColor.a / 255.f);

                // Let the derived class render the object geometry
                Render(Target);

                maskUtils.restoreStates();

                // Restore the previous modelview matrix
                glMatrixMode(GL_MODELVIEW);
                glPopMatrix();
            }

            void maskSetType(MaskTypes::Type maskType)
            {
                this->maskType = maskType;
            }

        private:
            MaskTypes::Type maskType;
    };

    #endif // SHAPEMASKED_H_INCLUDED


    StringMasked.h
    Code: [Select]

    #ifndef TEXTMASKED_H_INCLUDED
    #define TEXTMASKED_H_INCLUDED

    #include <SFML/Graphics.hpp>
    #include "MaskTypes.h"
    #include "MaskUtils.h"

    using namespace sf;

    class StringMasked : public sf::String
    {
        public:
            StringMasked() :
            String(),
            maskType(MaskTypes::None)
            {}

            StringMasked(sf::Shape& shape) :
            String(),
            maskType(MaskTypes::None)
            {
            }

            void Draw(RenderTarget& Target) const
            {
                // Save the current modelview matrix and set the new one
                glMatrixMode(GL_MODELVIEW);
                glPushMatrix();
                glMultMatrixf(GetMatrix().Get4x4Elements());

                bool restoreColorBuffer = false;
                bool restoreBlendMode = false;
                bool blendModePreviouslyEnabled;

                MaskUtils maskUtils;
                maskUtils.setBlendModesBasedOnMaskType(this->maskType, GetBlendMode());

                // Set color
                Color myColor = GetColor();
                glColor4f(myColor.r / 255.f, myColor.g / 255.f, myColor.b / 255.f, myColor.a / 255.f);

                // Let the derived class render the object geometry
                Render(Target);

                maskUtils.restoreStates();

                // Restore the previous modelview matrix
                glMatrixMode(GL_MODELVIEW);
                glPopMatrix();
            }

            void maskSetType(MaskTypes::Type maskType)
            {
                this->maskType = maskType;
            }

            MaskTypes::Type maskGetType()
            {
                return this->maskType;
            }

        private:
            MaskTypes::Type maskType;
    };

    #endif // TEXTMASKED_H_INCLUDED



    Example programs:

    SpriteMask example:

    (actually, shows two examples: on the left, we can see a simple masking example. On the right, a way to do sub-windowing, by combining overlapping masked elements).
    Code: [Select]
    #include <SFML/System.hpp>
    #include <SFML/Graphics.hpp>
    #include <iostream>
    #include <fstream>

    #include "SpriteMasked.h"
    #include "RenderWindowMask.h"

    int main()
    {
        //sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
        RenderWindowMask App(sf::VideoMode(800, 600, 32), "SFML Graphics");
        //App.PreserveOpenGLStates(true);

        sf::Clock Clock;

        sf::Image Image;
        Image.LoadFromFile("northeast.jpg");


        sf::Image ImageMask;
        ImageMask.LoadFromFile("images/window.tga");

        SpriteMasked spriteMask;
        spriteMask.SetImage(ImageMask);
        spriteMask.SetPosition(200, 200);
        spriteMask.maskSetType(MaskTypes::Mask);


        sf::Image ImageInsideWindow;
        ImageInsideWindow.LoadFromFile("images/image.tga");
        SpriteMasked spriteInsideWindow;
        spriteInsideWindow.SetImage(ImageInsideWindow);
        spriteInsideWindow.SetPosition(200, 200);


        const sf::Input& Input = App.GetInput();
        int             mouseXPrev = Input.GetMouseX(),
                        mouseYPrev = Input.GetMouseY(),
                        mouseX, mouseY;

        float timeSinceLastUpdate;

        int a = 0;


        // Start game loop
        while (App.IsOpened())
        {
            // Process events
            sf::Event Event;
            while (App.GetEvent(Event))
            {
                // Close window : exit
                if (Event.Type == sf::Event::Closed)
                    App.Close();
                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                    App.Close();
            }

            timeSinceLastUpdate = Clock.GetElapsedTime();
            Clock.Reset();

            mouseX = Input.GetMouseX();
            mouseY = Input.GetMouseY();
            float mouseMoveX = (float)(mouseXPrev - mouseX) * timeSinceLastUpdate * 6.0f;
            float mouseMoveY = (float)(mouseYPrev - mouseY) * timeSinceLastUpdate * 6.0f;
            mouseXPrev = mouseX;
            mouseYPrev = mouseY;

            App.Clear(sf::Color(200,0,0,255));


            /////
            ///// Example 1: simple mask
            /////
            // draw sprite to use as mask
            spriteMask.SetScale(1, 1);
            spriteMask.SetPosition(100, 250);
            spriteMask.maskSetType(MaskTypes::Mask);
            App.Draw(spriteMask);

            // draw normal sprite
            spriteInsideWindow.maskSetType(MaskTypes::BlendWithPreviousMask);
            spriteInsideWindow.SetScale(1, 1);
            spriteInsideWindow.SetPosition(100, 250);
            App.Draw(spriteInsideWindow);


            /////
            ///// Example 2: use masking to do sub-windowing
            /////

            // how to draw sub-windows:
            // 1) draw window contents first
            // 2) draw stuff outside afterwards
            // details:
            // 1) draw window contents - background to foreground
            //   a) draw window bg (don't care about mask)
            //   b) draw mask
            //   c) draw "negative" image (BlendWithPreviousMaskReversed)
            //   d) draw other stuff on top of it
            //   e) draw parent mask on top of it
            // 2) draw stuff outside
            //   a) draw "positive" other stuff over it (BlendWithPreviousMask)
            //   ( b) draw mask again if necessary for every new image to draw? dunno)


            // 1) draw window contents - background to foreground
            //   a) draw window bg (don't care about mask)
            //   b) draw mask for next element
            //   c) draw "negative" image (BlendWithPreviousMaskReversed)
            //   d) draw other stuff on top of it
            //   e) draw parent mask on top of it

            //   a) draw window bg (don't care about mask)
            spriteInsideWindow.SetScale(1,1);
            spriteInsideWindow.maskSetType(MaskTypes::None);
            spriteInsideWindow.SetPosition(500, 250);
            App.Draw(spriteInsideWindow);


            //   b) draw mask for next element (1)
            spriteMask.SetScale(0.4, 0.4);
            spriteMask.SetPosition(550, 250);
            spriteMask.maskSetType(MaskTypes::Mask);
            App.Draw(spriteMask);


            //   c) draw "negative" image (BlendWithPreviousMaskReversed) (1)
            spriteInsideWindow.maskSetType(MaskTypes::BlendWithPreviousMask);
            spriteInsideWindow.SetScale(0.4, 0.4);
            spriteInsideWindow.SetPosition(550, 250);
            App.Draw(spriteInsideWindow);


            //   b) draw mask for next element (2)
            spriteMask.SetScale(0.4, 0.4);
            spriteMask.SetPosition(590, 250);
            spriteMask.maskSetType(MaskTypes::Mask);
            App.Draw(spriteMask);

            //   c) draw "negative" image (BlendWithPreviousMaskReversed) (2)
            spriteInsideWindow.maskSetType(MaskTypes::BlendWithPreviousMask);
            spriteInsideWindow.SetScale(0.4, 0.4);
            spriteInsideWindow.SetPosition(590, 250);
            App.Draw(spriteInsideWindow);



            //   e) draw parent mask on top of it
            spriteMask.SetScale(1.0, 1.0);
            spriteMask.SetPosition(500, 200);
            spriteMask.maskSetType(MaskTypes::Mask);
            App.Draw(spriteMask);


            // 2) draw stuff outside
            //   a) draw "positive" other stuff over it (BlendWithPreviousMask)
            spriteInsideWindow.maskSetType(MaskTypes::BlendWithPreviousMaskReversed);
            spriteInsideWindow.SetPosition(350,150);
            spriteInsideWindow.SetScale(2,2);
            App.Draw(spriteInsideWindow);

            // Display window contents on screen
            App.Display();
        }

        return 0;
    }


    ShapeMask example:
    (masking using a sf::Circle)

    Code: [Select]

    #include <SFML/System.hpp>
    #include <SFML/Graphics.hpp>
    #include <iostream>
    #include <fstream>

    #include "SpriteMasked.h"
    #include "ShapeMasked.h"
    #include "RenderWindowMask.h"



    int main()
    {
        //sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
        RenderWindowMask App(sf::VideoMode(800, 600, 32), "SFML Graphics");

        sf::Clock Clock;


        sf::Image ImageInsideWindow;
        ImageInsideWindow.LoadFromFile("images/image.tga");
        SpriteMasked spriteInsideWindow;
        spriteInsideWindow.SetImage(ImageInsideWindow);
        spriteInsideWindow.SetPosition(200, 200);
        spriteInsideWindow.maskSetType(MaskTypes::BlendWithPreviousMask);


        const sf::Input& Input = App.GetInput();
        int             mouseXPrev = Input.GetMouseX(),
                        mouseYPrev = Input.GetMouseY(),
                        mouseX, mouseY;

        float timeSinceLastUpdate;


        // masking for shapes
        sf::Shape circle = sf::Shape::Circle(0, 0, 100, sf::Color(255,255,255,0)); // note: zero alpha
        ShapeMasked shapeMask(circle);
        shapeMask.maskSetType(MaskTypes::Mask);

        // Start game loop
        while (App.IsOpened())
        {
            // Process events
            sf::Event Event;
            while (App.GetEvent(Event))
            {
                // Close window : exit
                if (Event.Type == sf::Event::Closed)
                    App.Close();
                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                    App.Close();
            }

            timeSinceLastUpdate = Clock.GetElapsedTime();
            Clock.Reset();

            App.Clear(sf::Color(200,0,0,255));


            // draw shape mask
            shapeMask.SetPosition(500,300);
            App.Draw(shapeMask);

            // draw masked image
            spriteInsideWindow.SetPosition(450, 200);
            App.Draw(spriteInsideWindow);


            // Display window contents on screen
            App.Display();
        }

    }


    StringMask example:

    Code: [Select]

    #include <SFML/System.hpp>
    #include <SFML/Graphics.hpp>
    #include <iostream>
    #include <fstream>

    #include "StringMasked.h"
    #include "SpriteMasked.h"
    #include "RenderWindowMask.h"


    int main()
    {
        //sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
        RenderWindowMask App(sf::VideoMode(800, 600, 32), "SFML Graphics"); // we'll need to use our RenderWindow to have access to "ClearMask" function
        //App.PreserveOpenGLStates(true);

        //std::ofstream myfile;
        //myfile.open ("pixels.txt");

        sf::Clock Clock;

        sf::Image Image;
        Image.LoadFromFile("northeast.jpg");

        sf::Image ImageInsideText;
        ImageInsideText.LoadFromFile("images/image.tga");

        SpriteMasked spriteInsideText;
        spriteInsideText.SetImage(ImageInsideText);

        float timeSinceLastUpdate;


        // masking for text
        // Create a graphical string
        StringMasked stringMask;
        stringMask.SetText("Hello !\nHow are you ?");
        stringMask.SetFont(sf::Font::GetDefaultFont());
        stringMask.SetPosition(100,50);

        stringMask.maskSetType(MaskTypes::Mask);
        stringMask.SetColor(sf::Color(0,0,0,255));

        // Start game loop
        while (App.IsOpened())
        {
            // Process events
            sf::Event Event;
            while (App.GetEvent(Event))
            {
                // Close window : exit
                if (Event.Type == sf::Event::Closed)
                    App.Close();
                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                    App.Close();
            }

            timeSinceLastUpdate = Clock.GetElapsedTime();
            Clock.Reset();


            App.Clear(sf::Color(200,0,0,255)); // clear the background to red

            //// How to correctly draw a String to Mask/MaskReverse a sprite:
            //// 1. clear the Window Alpha Mask before drawing the text (or the sprite may appear where you drew previous masks)
            //// 2. draw the SPRITE as the mask (masking everywhere where the sprite would be)
            //// 3. draw the STRING using the mask type "MaskOverWritePreviousMask" (thus "cutting" into the mask the previous sprite left)
            //// 4. draw the Sprite again, normally, using the mask type "BlendWithPreviousMask"

            App.ClearMask(); // 1. clears just the Window's alpha channel

            // 2. use sprite to draw as mask
            spriteInsideText.SetPosition(100,10);

            // save the sprite mask and color state (may be necessary if you reuse this sprite elsewhere)
            MaskTypes::Type prevMaskType = spriteInsideText.maskGetType();
            sf::Color prevSpriteColor = spriteInsideText.GetColor();

            spriteInsideText.SetColor(sf::Color(0,0,0,0));    // set the sprite's alpha to 0
            spriteInsideText.maskSetType(MaskTypes::Mask);    // set the sprite to act as a mask
            App.Draw(spriteInsideText);

            // restore sprite mask+color state
            spriteInsideText.maskSetType(prevMaskType);
            spriteInsideText.SetColor(prevSpriteColor);


            // 3. draw text mask
            stringMask.SetPosition(100,50);
            stringMask.maskSetType(MaskTypes::MaskOverWritePreviousMask);
            App.Draw(stringMask);

            // 4. draw the masked sprite
            spriteInsideText.SetPosition(100,10);
            // using this blending mode, the sprite will be drawn normally and the text area will be "cut" from the sprite
            spriteInsideText.maskSetType(MaskTypes::BlendWithPreviousMask);

            App.Draw(spriteInsideText);




            // Example 2:
            App.ClearMask(); // 1. clears just the Window's alpha channel

            // 2. use sprite to draw as mask
            spriteInsideText.SetPosition(500,10);

            // save the sprite mask and color state (may be necessary if you reuse this sprite elsewhere)
            prevMaskType = spriteInsideText.maskGetType();
            prevSpriteColor = spriteInsideText.GetColor();

            spriteInsideText.SetColor(sf::Color(0,0,0,0));    // set the sprite's alpha to 0
            spriteInsideText.maskSetType(MaskTypes::Mask);    // set the sprite to act as a mask
            App.Draw(spriteInsideText);

            // restore sprite mask+color state
            spriteInsideText.maskSetType(prevMaskType);
            spriteInsideText.SetColor(prevSpriteColor);


            // 3. draw text mask
            stringMask.SetPosition(500,50);
            stringMask.maskSetType(MaskTypes::MaskOverWritePreviousMask);
            App.Draw(stringMask);

            // 4. draw the masked sprite
            spriteInsideText.SetPosition(500,10);

            // using this blending mode, the sprite will be drawn "inside" the text's characters
            spriteInsideText.maskSetType(MaskTypes::BlendWithPreviousMaskReversed);

            App.Draw(spriteInsideText);


            App.Display(); // Display window contents on screen
        }

        //myfile.close();
        return 0;
    }


    This method has a few inconveniences, such as having the overhead of redrawing polygons, and Strings have some boring extra work needed and work the other way around than sprites and shapes, but I think it's altogether a good and flexible solution, and all-encompassing, technology-wise (supporting older hardware).


    Plus, here's a little function for debugging, that saves the current alpha buffer to a file named "imageAlpha.png" (upside down, OpenGL stuff). May help you debug if you run into problems.
    Code: [Select]

    void GetGLPixels()
    {
        unsigned int startX = 0, startY = 0;
        unsigned int width = 800, height = 600;

        GLubyte* pixels = new GLubyte[width * height * 4];

        glReadPixels(startX, startY, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);


        for(unsigned int idxPixelRow = 0; idxPixelRow < height; idxPixelRow++)
        {
            for(unsigned int idxPixelColumn = 0; idxPixelColumn < width; idxPixelColumn++)
            {
                unsigned int index = ((idxPixelRow * width) + idxPixelColumn) * 4;
                unsigned int indexAlpha = ((idxPixelRow * width) + idxPixelColumn);

                float valueRed = pixels[index + 0];
                float valueGreen = pixels[index + 1];
                float valueBlue = pixels[index + 2];
                float valueAlpha = pixels[index + 3];

                pixels[index] = pixels[index + 1] = pixels[index + 2] = 0;
            }
        }


        // create and save image
        sf::Image imageAlpha;
        imageAlpha.LoadFromPixels(width, height, pixels);
        imageAlpha.SaveToFile("imageAlpha.png");


        delete pixels;
    }
    Game development blog: http://pjmendes.blogspot.com/

    Finn

    • Jr. Member
    • **
    • Posts: 90
      • View Profile
    Custom Blending Modes and other requests
    « Reply #3 on: September 12, 2010, 10:59:20 pm »
    Great work :)

    Laurent

    • Administrator
    • Hero Member
    • *****
    • Posts: 32504
      • View Profile
      • SFML's website
      • Email
    Custom Blending Modes and other requests
    « Reply #4 on: September 12, 2010, 11:26:15 pm »
    That's really awesome :)

    I'm sure it will be useful to a lot of users (it definitely has to be on the wiki), and even me when I'll start implementing this kind of stuff in SFML 2.
    Laurent Gomila - SFML developer

    TurboLento

    • Newbie
    • *
    • Posts: 9
      • View Profile
      • http://pjmendes.blogspot.com/
    Custom Blending Modes and other requests
    « Reply #5 on: September 13, 2010, 12:37:46 am »
    Glad it's helpful! I'll add it to the wiki.

    It's at:
    http://www.sfml-dev.org/wiki/en/sources/masking_using_alpha_and_blending
    linked at the "Sources" area from the english wiki.
    Hope it's all good :)
    Game development blog: http://pjmendes.blogspot.com/

     

    anything