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

Author Topic: [solved] [2.1] Can't get PNG transparency to work  (Read 7415 times)

0 Members and 1 Guest are viewing this topic.

vlanore

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
[solved] [2.1] Can't get PNG transparency to work
« on: March 01, 2014, 09:57:11 pm »
Hello,
I'm new to SFML and I'm trying to draw sprites with transparency (mostly to draw things on top of other things).
From the tutorials and forum topics I've browsed, I understand that PNG texture transparency should work without doing anything special.

Here is what I've done (slightly modified base tuto):
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::Texture myTexture;
    myTexture.loadFromFile("./textures.png");
    sf::Sprite mySprite;
    mySprite.setTexture(myTexture);

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

And when I run it, at the places where my texture is transparent I see a white rectangle instead of the blue background. I'd like the background (or anything drawn before) to be displayed through the transparent parts of the image. I have tried various PNG images with no success.

Did I do something wrong or miss anything? Is this a bug/config problem on my end?

I'm running the git version of SFML compiled by hand on debian on a virtual machine (3d acceleration working,  no direct rendering) and with an ATI radeon 6950.

Any help is welcome :)

EDIT: my problem was solved (see my last post below) and was VirtualBox-related.
« Last Edit: March 11, 2014, 12:04:27 am by vlanore »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: [2.1] Can't get PNG transparency to work
« Reply #1 on: March 01, 2014, 10:15:19 pm »
At least your code is OK. Could you give us one of your PNG?
Maybe your drivers aren't up to date, or it's a virtual machine related problem. (= I don't know :p )

vlanore

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: [2.1] Can't get PNG transparency to work
« Reply #2 on: March 01, 2014, 11:00:23 pm »
Thanks for the quick answer.

Here is a PNG texture. I've tried with several PNGs including random ones from the web and it works with none.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [2.1] Can't get PNG transparency to work
« Reply #3 on: March 01, 2014, 11:27:51 pm »
Using your source code and image without any modifications, it runs perfectly for me:


A screenshot of your problem occurring may help.
« Last Edit: March 01, 2014, 11:35:25 pm by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

vlanore

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: [2.1] Can't get PNG transparency to work
« Reply #4 on: March 01, 2014, 11:44:05 pm »
Here is a screen of what I get (attachment).

Since it runs fine for you it's probably driver/opengl/virtualbox/thing - related. I'll look into that. Any idea is welcome :)

thomas9459

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: [2.1] Can't get PNG transparency to work
« Reply #5 on: March 02, 2014, 06:20:02 pm »
Quote from: vlanore
Code: [Select]
export LIBGL_ALWAYS_INDIRECT=1

That line is most likely the source of the problem. Doing a little research, it seems that it drops OpenGL support back to 1.4 (lots of information on it here), and I believe SFML needs at least OpenGL 2.0. Can you add the following line to your code and report back the results.

sf::ContextSettings settings = window.getSettings();
std::cout << "version:" << settings.majorVersion << "." << settings.minorVersion << std::endl;

vlanore

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: [2.1] Can't get PNG transparency to work
« Reply #6 on: March 02, 2014, 07:04:32 pm »
Seems you are right:
vlanore@vlanore-vm:~/zzbl/test$ ./test
Segmentation fault
vlanore@vlanore-vm:~/zzbl/test$ export LIBGL_ALWAYS_INDIRECT=1
vlanore@vlanore-vm:~/zzbl/test$ ./test
version:1.4

As exemplified by these commands I use LIBGL_ALWAYS_INDIRECT as a workaround to avoid insta-segfaulting on window creation. It seems though that it's too brutal and cripples SFML :/

Further investigation revealed the segfault is probably video-drivers-related and I'm currently in the process of trying to upgrade them to the latest possible version. I'm hoping that avoiding the LIBGL_ALWAYS_INDIRECT workaround will fix my transparency problem.


vlanore

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: [2.1] Can't get PNG transparency to work
« Reply #7 on: March 11, 2014, 12:03:08 am »
Okay I managed to make everything work. Latest VirtualBox version and guest additions didn't help but I discovered that instead of using LIBGL_ALWAYS_INDIRECT to avoid the segfault I could use LIBGL_ALWAYS_SOFTWARE which does not reduce OpenGL to 1.4.

Here is the result of the same code as the previous post:
vlanore@vlanore-vm:~/zzbl/test$ ./test
Segmentation fault
vlanore@vlanore-vm:~/zzbl/test$ export LIBGL_ALWAYS_SOFTWARE=1
vlanore@vlanore-vm:~/zzbl/test$ ./test
version:2.1

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [2.1] Can't get PNG transparency to work
« Reply #8 on: March 11, 2014, 12:14:17 am »
Okay I managed to make everything work. Latest VirtualBox version and guest additions didn't help but I discovered that instead of using LIBGL_ALWAYS_INDIRECT to avoid the segfault I could use LIBGL_ALWAYS_SOFTWARE which does not reduce OpenGL to 1.4.
Nice. Glad you solved your problem.
Just wanted to say that this helped me too on a VirtualBox VM where 3D passthrough does not work since the host doesn't have X.org >= 1.15 - your solution worked like a charm - thanks :)

 

anything