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

Author Topic: Transparent color and RenderTexture  (Read 3768 times)

0 Members and 1 Guest are viewing this topic.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Transparent color and RenderTexture
« on: October 22, 2013, 02:56:23 am »
hello everybody. consider this little piece of code:

#include <SFML/Graphics.hpp>
int main(){
    sf::RenderTexture temp_render;
    sf::Texture tex;
    sf::Sprite spr;
    temp_render.create(200, 200);
    temp_render.clear(sf::Color::Transparent);

    sf::VertexArray square;
    sf::Color color_1(255, 0, 0, 255);
    sf::Color color_2(255, 0, 0, 0);
    square.setPrimitiveType(sf::PrimitiveType::Quads);
    square.append(sf::Vertex(sf::Vector2f(0, 0), color_2));
    square.append(sf::Vertex(sf::Vector2f(0, 200), color_2));
    square.append(sf::Vertex(sf::Vector2f(200, 200), color_1));
    square.append(sf::Vertex(sf::Vector2f(200, 0), color_1));

    temp_render.draw(square);
    temp_render.display();
    tex = temp_render.getTexture();
    spr.setTexture(tex);

    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML Window");

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        window.draw(spr);
        window.display();
    }
    return 0;
}

if you render it, you'll get:


BUT, if you change the line
window.draw(spr);
to
window.draw(square);
meaning that you'll draw the square directly to the screen, instead of drawing it to a RenderTexture and then to the screen, you'll get:


what makes me think: why this difference? I believe that when setting something to sf::Color::Transparent, it stores black pixels (as sf::Color::Transparent = sf::Color(0, 0, 0, 0)). but these pixels shouldn't be shown at all, since they're transparent, right?
as i don't know if this is expected or is a bug, i tought it would be better to ask here :)
thanks!
« Last Edit: October 22, 2013, 02:58:36 am by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Transparent color and RenderTexture
« Reply #1 on: October 22, 2013, 03:06:19 am »
Nope, it's blend modes.  When you draw to a render texture and then draw that texture to a window, you're drawing the same thing twice, which means two blend modes are involved, and in the case of alpha blending (SFML's default blend mode) doing it twice is very different from doing it once.

Try using different blend modes in one or both draw()s and you should be able to get whichever effect you want.  It's part of the implicit RenderStates parameter.  Everything you need to know is in the documentation.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Transparent color and RenderTexture
« Reply #2 on: October 22, 2013, 07:46:44 am »
To be more precise, in one draw you probably want to do a direct copy and not interpret the alpha channel, so you must use sf::BlendNone.
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Transparent color and RenderTexture
« Reply #3 on: October 22, 2013, 01:18:43 pm »
oh, my bad! i tought it could be a bug, didn't knew about blend modes.
thanks for the info.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything