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

Author Topic: png Transparency problem in sfml2  (Read 4487 times)

0 Members and 1 Guest are viewing this topic.

klfwip

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
png Transparency problem in sfml2
« on: August 27, 2012, 09:12:37 am »
I have been enjoying SFML2 since switching over recently, but I cannot get transparency to work at all. Searching the forums and google has not turned up anything yet on why my attempts are not working. I have been trying to get this working for hours so if you can give me any suggestions on what to try next I would really appreciate it.

To summarize the problem: I am trying to load a png (attached), created in gimp which has a transparent background. In any photo viewer the checkered pattern shows up indicating that the background is being saved transparently correctly. I have tried playing around with each possible RenderTarget blend mode when drawing and this does not seem to help, and regardless from what I have read sfml automatically uses the alpha channels in png files. Ideally I want the transparent part of the sprite to not block the images behind it, as it is there is a black rectangle around every sprite. The simplest demonstration I can make of this is as follows:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow app(sf::VideoMode(960,540,32),"SFML alpha test");
    sf::Event event;
    sf::Texture texture;
    sf::Sprite sprite1;
    sf::Sprite sprite2;

    texture.loadFromFile("alpha2.png");
    sprite1.setTexture(texture);
    sprite2.setTexture(texture);

    sprite2.setPosition(50,50);

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

        app.clear(sf::Color(128,128,128,128)); //set background to grey
        app.draw(sprite1);
        app.draw(sprite2);
        app.display();
    }
    return 0;
}
 


If it makes any difference, I am using g++ and Code::Blocks from the Ubuntu repositories, and sfml 2 was compiled from the snapshot  named "LaurentGomila-SFML-18f1b62"; the most recent version of SFML at the time of posting.

[attachment deleted by admin]
« Last Edit: August 27, 2012, 09:15:17 am by klfwip »

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: png Transparency problem in sfml2
« Reply #1 on: August 27, 2012, 09:42:29 am »
I'm not sure if it may be related, but:
app.clear(sf::Color(128,128,128,128)); //set background to grey
You clear the window with an alpha value of 128.
I'm not sure what the result of that would be to be quite frank, since I cannot test it right now, but it's very awkward. You'll probably want to use just sf::Color(128,128,128).

I would be surprised, but maybe that even fixes your issue. The png itself is fine and so is your code; there should be no more requirements to get it displaying correctly in SFML.
JSFML - The Java binding to SFML.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: png Transparency problem in sfml2
« Reply #2 on: August 27, 2012, 10:15:15 am »
I would be surprised, but maybe that even fixes your issue. The png itself is fine and so is your code; there should be no more requirements to get it displaying correctly in SFML.
I quickly tested it and it seemed to have no effect on the background, but yes one shouldn't clear the background with a transparent color. ;)

Also the images got displayed with transparancy on my system, so it seems it's related to your graphics card.
It may be the same problem as described in this thread. What do you get as console output with the following code:
#include <SFML/Window.hpp>
#include <iostream>
#include <GL/gl.h>

int main()
{
  sf::Window w(sf::VideoMode(640, 480, 32),
      "OpenGL", sf::Style::Default);

  int b;
  glGetIntegerv(GL_ALPHA_BITS, &b);
  std::cout << b << std::endl;
}
« Last Edit: August 27, 2012, 11:19:22 am 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
Re: png Transparency problem in sfml2
« Reply #3 on: August 27, 2012, 10:36:49 am »
Quote
but yes one should clear the background with a transparent color.
I guess that you forgot "not" once again ;D
And no, one can perfectly clear with a transparent color. This can be useful if:
- you want to use blend modes that use the destination alpha
- you want to save the color buffer to a png

Quote
It may be the same problem as described in this thread.
No, GL_ALPHA_BITS has no impact on what's drawn. It's even explained in the thread:
Quote
Btw, blending with GL_BLEND works fine
Laurent Gomila - SFML developer

klfwip

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: png Transparency problem in sfml2
« Reply #4 on: August 27, 2012, 10:49:26 am »
pdinklag, I did not know that leaving the alpha channel to defaults when clearing was the norm, I will do that in the future though. Unfortunately it seems to have no effect on the problem.

eXpl0it3r, I ran your code and the output was 8 in linux. In windows it just said "undefined reference to 'glGetIntegerv@8' " which I will try to figure out later. I have not used windows for development much, so there is probably just something missing.

I just ran the exact same code, with the same png in windows 7, and everything works fine. I also tried it on a computer with an Nvidia card in linux and it works. My main computer has a fairly new AMD card, which generally means you are going to have a plethora of driver issues, but I still hope I can find a way to get this working. I will keep playing with drivers and SFML builds to see if I can get this working properly.

 

anything