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

Author Topic: is it possible to subtract a texture from an other  (Read 14636 times)

0 Members and 1 Guest are viewing this topic.

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
is it possible to subtract a texture from an other
« on: September 20, 2012, 08:06:49 pm »
First of all, sorry for my worst english,

i hope you understand me anyway.



i want do get the effect of the third picture of the first row.

is it possible?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11001
    • View Profile
    • development blog
    • Email
Re: is it possible to subtract a texture from an other
« Reply #1 on: September 20, 2012, 10:07:24 pm »
I don't know of any SFML function that could do this, so I guess it's not possible. If yxou really need it you might be able to do such a thing with OpenGL, but I'm not an expert on OpenGL... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: is it possible to subtract a texture from an other
« Reply #2 on: September 20, 2012, 10:14:51 pm »
thanks for the fast answering, i hope you are wrong, i doesn't know much about OpenGl.

In an German Forum some guy said that i have to use Render Textures,  but he can't explain i have to do this.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: is it possible to subtract a texture from an other
« Reply #3 on: September 20, 2012, 10:22:06 pm »
Do you want this effect to be applied in real time? Or can it be done once at init?
Laurent Gomila - SFML developer

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: is it possible to subtract a texture from an other
« Reply #4 on: September 20, 2012, 10:45:17 pm »
it should "Simulate" a point light in real time

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: is it possible to subtract a texture from an other
« Reply #5 on: September 20, 2012, 11:58:47 pm »
Hum... how does subtracting two textures simulate a point light?
Laurent Gomila - SFML developer

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: is it possible to subtract a texture from an other
« Reply #6 on: September 21, 2012, 09:38:53 am »
I make the whole screen black and on the place i want i cut a circle out. So i have a simple point light

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: is it possible to subtract a texture from an other
« Reply #7 on: September 21, 2012, 09:43:55 am »
Ok, so in fact you want to create a mask.

This is possible, you can do it this way:

- create a RenderTexture which covers your screen
- clear it with opaque black
- draw your spot light to it, with blend mode = sf::BlendNone (so that its alpha is copied rather than interpreted)
- draw your RenderTexture to your window with a sprite
Laurent Gomila - SFML developer

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: is it possible to subtract a texture from an other
« Reply #8 on: September 21, 2012, 02:15:00 pm »
Sounds good, but can you write a short sample, i don't really get it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11001
    • View Profile
    • development blog
    • Email
Re: is it possible to subtract a texture from an other
« Reply #9 on: September 21, 2012, 02:24:36 pm »
I think something like this (untested):

sf::CircleShape point(10);
point.setColor(255, 255, 255, 1);

sf::RenderTexture rtex;
rtex.create(windowWidth, windowHeight);
rtex.clear();
rtex.draw(point, sf::BlendNone);
rtex.display();

sf::Sprite sprite;
sprite.setTexture(rtex.getTexture());

// ...

window.draw(sprite);

Interesting enough the alpha channel of the circle/punch hole, can't be fully transparent, otherwise it will just be ignored.
@Laurent is this the intended behavior?

Anyways since I found the question interesting and somehow on another board has asked about the same questions here goes a complete example: ;D
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(300, 300), "Hello World!");
    window.setFramerateLimit(30);
    window.setMouseCursorVisible(false);

    sf::RectangleShape rect(sf::Vector2f(100.f, 100.f));
    rect.setFillColor(sf::Color::Red);
    rect.setPosition(10, 10);

    sf::CircleShape circle(20);
    circle.setRadius(20);
    circle.setPosition(110, 110);
    circle.setFillColor(sf::Color(255, 255, 255, 1));

    sf::RenderTexture rtex;
    rtex.create(300, 300);

    sf::Sprite sprite;
    sprite.setTexture(rtex.getTexture(), true);
    sprite.setPosition(0, 0);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

        circle.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)));

        rtex.clear();
        rtex.draw(circle, sf::BlendNone);
        rtex.display();

        sprite.setTexture(rtex.getTexture(), true);

        window.clear(sf::Color::Blue);

        window.draw(rect);
        window.draw(sprite);

        window.display();
    }
}
 
But keep in mind drawing to the render texture every frame is quite a heavy task, it's like drawing two frames at once, so if it's not really needed don't do this every frame iteration. ;)
« Last Edit: September 21, 2012, 03:09:29 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: is it possible to subtract a texture from an other
« Reply #10 on: September 21, 2012, 03:07:05 pm »
You all are awesome, i'll test it and post the result.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: is it possible to subtract a texture from an other
« Reply #11 on: September 21, 2012, 03:24:07 pm »
Quote
@Laurent is this the intended behavior?
Did you use the RC? I thought I already fixed it.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11001
    • View Profile
    • development blog
    • Email
Re: is it possible to subtract a texture from an other
« Reply #12 on: September 21, 2012, 03:28:26 pm »
Quote
@Laurent is this the intended behavior?
Did you use the RC? I thought I already fixed it.
Nope, a one or two week old SFML release...

Edit: Just updated SFML to the latest commit and transparency with alpha channel = 0 and BlendNone still just doesn't draw anything...
(Windows, MinGW 4.7.0, static, debug)
« Last Edit: September 21, 2012, 03:41:22 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: is it possible to subtract a texture from an other
« Reply #14 on: September 21, 2012, 04:12:11 pm »
Ok, wait, there's another bug (was supposed to be a feature ;D), specific to shapes.

I'll see what I can do.
Laurent Gomila - SFML developer