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

Author Topic: alpha being drawn to render texture as black  (Read 2857 times)

0 Members and 1 Guest are viewing this topic.

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
alpha being drawn to render texture as black
« on: April 10, 2016, 08:53:50 am »
Hello, I'm having issues at the moment, because I am trying to get layers working in my map editor. So basically I have a layer for the next height level and one for the ground, these are basically just vectors of tile numbers. These then get drawn to their own render texture allowing me to split the rendering so that i can render characters and other objects in between. i.e so a character is in a doorway rather than on top of it, etc.

however for some reason the alpha from my top layer is being rendered as black and is hiding the ground layer. I don't have these issues when using one render texture, but i would like them split up for the above reasons as well as for versatility. Here is a minimal example that produces the annomally:

sf::RenderWindow window;
        window.create(sf::VideoMode(400, 400), "test");
        window.setFramerateLimit(60);
        //initialise and load texture of tilesheet
        sf::Texture tileset;
        tileset.loadFromFile("graphics/tileSheet.png");

        //create sprite that uses the tilesheet as its texture
        sf::Sprite tileSprite(tileset);

        //create 2 render textures to act as layers, one for the ground, one for a higher level
        sf::RenderTexture tempTexture1;
        sf::RenderTexture tempTexture2;
        tempTexture1.create(400, 400);
        tempTexture2.create(400, 400);

        //create 2 sprites refering to the render textures
        sf::Sprite tempSprite1(tempTexture1.getTexture());
        sf::Sprite tempSprite2(tempTexture2.getTexture());
        //set texture rects so that one is positioned where tiles are in the tilesheet
        //the second is positioned where empty alpha tiles are for test purposes
        tempSprite1.setTextureRect(sf::IntRect(0, 0, 400, 400));
        tempSprite2.setTextureRect(sf::IntRect(0, 200, 400, 400));
        //set sprites location so the drawn layers will overlap
        tempSprite1.setPosition(0, 0);
        tempSprite2.setPosition(0, 0);
        //draw the sections of the tilesheet to the two different render textures
        tempTexture1.clear();
        tempTexture1.draw(tileSprite);
        tempTexture1.display();
        tempTexture2.clear();
        tempTexture2.draw(tileSprite);
        tempTexture2.display();
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }
                //draw the bottom layer and then the top
                window.clear();
                window.draw(tempSprite1);
                window.draw(tempSprite2);
                window.display();
        }
 

just to clarify, the tilesheet is only half full of tiles, so the second render texture has blank transparent tiles rendered to it.

Any help would be appreciated, pulling hair out etc etc :/

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: alpha being drawn to render texture as black
« Reply #1 on: April 10, 2016, 08:57:17 am »
I've already come up with a new idea. I'm satrting to think that it is transparent, but because im clearing it first, the black from the clear is what is causing it to be opaque. I will see if i can fix that and come back and update this thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: alpha being drawn to render texture as black
« Reply #2 on: April 10, 2016, 09:40:30 am »
Clear your render-textures with a transparent color (anything with alpha = 0).
Laurent Gomila - SFML developer

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: alpha being drawn to render texture as black
« Reply #3 on: April 10, 2016, 09:48:21 am »
Thank you Laurent, I had just found out that using sf::BlendNone worked. I can only assume that using a transparent clear is more efficient?

thanks for your reply!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: alpha being drawn to render texture as black
« Reply #4 on: April 10, 2016, 10:41:30 am »
If you fill your render-texture with a big sprite that has transparent pixels, then yes BlendNone has the same effect, because alpha will be directly copied instead of being interpreted. I said to use a transparent clear color because I didn't know that your sprite(s) cover the whole target; in this case the clear color is irrelevant.
Laurent Gomila - SFML developer

 

anything