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

Author Topic: Android: Texture from sf::RenderTexture is flipped when copied (Bug?)  (Read 1638 times)

0 Members and 1 Guest are viewing this topic.

Exilef

  • Newbie
  • *
  • Posts: 7
    • View Profile
Hello everyone!
I'm making an Android app and I have a weird problem that I can't explain. When I draw something to a sf::RenderTexture, retrieve the Texture and then copy it to an image the image is flipped. So, when I do something like this:

#include <SFML/Window/Event.hpp>
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 800), "Test");
    window.setVerticalSyncEnabled(true);

    sf::RectangleShape rect({ 100.f, 200.f });    rect.setOrigin(rect.getSize()/2.f);
    rect.rotate(40.f);
    rect.setPosition(rect.getGlobalBounds().width/2.f, rect.getGlobalBounds().height/2.f);

    sf::RenderTexture renderTex;
    renderTex.create(rect.getGlobalBounds().width, rect.getGlobalBounds().height);
    renderTex.draw(rect);    renderTex.display();
    sf::Image img = renderTex.getTexture().copyToImage();  // Flipped here
    sf::Texture imgTex;    imgTex.loadFromImage(img);

    sf::Sprite spriteTex(renderTex.getTexture());
    sf::Sprite spriteImg(imgTex);
      spriteImg.setPosition(spriteTex.getGlobalBounds().width, 0.f);

    while(window.isOpen())
    {
        window.clear();   window.draw(spriteTex);   window.draw(spriteImg);   window.display();

        sf::Event e;    window.pollEvent(e);
        if(e.type == sf::Event::Closed)  window.close();
    }
}
on my Android device (or emulator) I get a rectangle on the left that is rotated clockwise (as it should be) and next to it the rectangle from the image that is flipped. If I set a Texture to the RectangleShape you can see it is flipped vertically. But on Linux/Windows with the same code I get two identical rectangles.

The same thing also happens if the Texture from the RenderTexture is copied to another Texture. And as the Copy-Constructor for sf::Texture (or operator=) also internally calls sf::Texture::copyToImage that function is probably the problem. There does seem to be some OpenGL ES specific code in that function (https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Texture.cpp#L322), but I don't really understand what happens there. If the copied Texture is copied again (or if a normal Texture is loaded from a file and copied) it won't get flipped, only when the Texture bound to the RenderTexture is copied.

Usually if you forget to call sf::RenderTexture::display the resulting Texture is flipped vertically, as it happens here. This does happen if the referenced Texture is given directly to a sprite. But if it is copied without calling display() it won't get flipped again. Thus, copying the Texture acts like display() was never called.

Did I miss something? Or did I find a new bug? I should have the latest SFML version. I temporarily fixed it by calling sf::Image::flipVertically, but an explanation would be nice.

Edit: Shortened the post.
Edit2: Almost 2 weeks and no response. Did I do something wrong? :(
« Last Edit: January 15, 2017, 04:19:28 pm by Exilef »

Exilef

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Android: Texture from sf::RenderTexture is flipped when copied (Bug?)
« Reply #1 on: February 08, 2017, 04:10:40 pm »
So, does no one here care about Android? :(
The problem has now gotten bigger. On one device (Huawei P9) a RenderTexture (not copied) doesn't show anything. Calling display() does nothing at all. Only copyToImage() (without using the copy) updates the original Texture, as display() should do.
I haven't seen it myself, but I was told that after leaving it ~5 min it suddenly gets updated and shows the Texture, not sure if that information is useful.
I know it's still called experimental, but I was hoping someone would know a workaround or could tell me how to fix it myself.

Copying every frame is slow, so I don't know how to deal with this problem anymore...

 

anything