SFML community forums

Help => Graphics => Topic started by: marchred on June 12, 2016, 09:33:43 am

Title: possible bug with sprite and raw textured vertices?
Post by: marchred on June 12, 2016, 09:33:43 am
Hi eveyrone,

Sorry in advance as I'm not sure if it is the right place to ask that kind of question...

Anyway, I'm currently facing a problem I can't explain and it might (unless I'm wrong) actually be a bug within the sfml.

long story short, code + result:

code:
#include <SFML/Graphics.hpp>

void    dump_texture(sf::RenderWindow& rwindow, const sf::Vector2f& pos, sf::Texture& tex);

int main()
{
    //
    // INIT

        // Create the main window
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

        // Load a sprite to display
        sf::Texture texture;
        if (!texture.loadFromFile("cute_image.jpeg"))
            return EXIT_FAILURE;
        sf::Sprite sprite(texture);

        // scale to match a 200x200 size
        sf::Vector2u    tsize = texture.getSize();
        sprite.scale(200.0f/tsize.x, 200.0f/tsize.y);

    // INIT
    //

    // Start the game loop
    while (window.isOpen())
    {
        //
        // EVENTS

            sf::Event event;
            while (window.pollEvent(event))
            {
                // Close window: exit
                if (event.type == sf::Event::Closed)
                    window.close();
            }

        // EVENTS
        //


        //
        // RENDER

            window.clear();

            dump_texture(window, sf::Vector2f(200,0), texture);

            window.draw(sprite);

            dump_texture(window, sf::Vector2f(400,0), texture);

            window.display();

        // RENDER
        //
    }
    return EXIT_SUCCESS;
}

void    dump_texture(sf::RenderWindow& rwindow, const sf::Vector2f& pos, sf::Texture& tex)
{
    sf::Texture::bind(&tex);

    sf::Vertex vertices[] =
    {
        sf::Vertex(sf::Vector2f(pos.x+  0, pos.y+  0), sf::Color::White, sf::Vector2f(0,0)),
        sf::Vertex(sf::Vector2f(pos.x+  0, pos.y+200), sf::Color::White, sf::Vector2f(0,1)),
        sf::Vertex(sf::Vector2f(pos.x+200, pos.y+200), sf::Color::White, sf::Vector2f(1,1)),
        sf::Vertex(sf::Vector2f(pos.x+200, pos.y+  0), sf::Color::White, sf::Vector2f(1,0))
    };

    rwindow.draw(vertices, 4, sf::Quads);

    sf::Texture::bind(NULL);
}
 

result
https://drive.google.com/open?id=0BxUZ1JUO9QAbakJyWVhZWkZZRlU (https://drive.google.com/open?id=0BxUZ1JUO9QAbakJyWVhZWkZZRlU)

Basically the sprite seems to "disable" the opengl texture2d state... which would make the second call to dump_texture to fail?

Any idea?
Title: Re: possible bug with sprite and raw textured vertices?
Post by: Laurent on June 12, 2016, 11:22:55 am
It's not a bug; it's a feature ;)

Texture::bind exists for interaction between OpenGL and SFML code. You're not supposed to use it when drawing SFML entities. The graphics module of SFML is self-contained, everything that you draw uses the entity's internal state + the render-states that you pass to the draw function (which is the way you should have done it). Everything set externally can produce wrong results, especially by breaking the internal state cache of SFML.

The correct version of your code is:
void    dump_texture(sf::RenderWindow& rwindow, const sf::Vector2f& pos, sf::Texture& tex)
{
    sf::Vertex vertices[] =
    {
        sf::Vertex(sf::Vector2f(pos.x+  0, pos.y+  0), sf::Color::White, sf::Vector2f(0,0)),
        sf::Vertex(sf::Vector2f(pos.x+  0, pos.y+200), sf::Color::White, sf::Vector2f(0,1)),
        sf::Vertex(sf::Vector2f(pos.x+200, pos.y+200), sf::Color::White, sf::Vector2f(1,1)),
        sf::Vertex(sf::Vector2f(pos.x+200, pos.y+  0), sf::Color::White, sf::Vector2f(1,0))
    };

    rwindow.draw(vertices, 4, sf::Quads, &tex);
}

Make sure to read the documentation (http://www.sfml-dev.org/tutorials/2.3/graphics-vertex-array.php#texturing) before trying random stuff.
Title: Re: possible bug with sprite and raw textured vertices?
Post by: marchred on June 12, 2016, 11:28:54 am
wow....

thx!

I'll read the doc more carefully next time.
Title: Re: possible bug with sprite and raw textured vertices?
Post by: marchred on June 13, 2016, 08:29:36 am
hum....

Sorry to ask again but I'm not able to make it work... and I've copy pasted your solution.

it's like the sf::VertexArray are not behaving on my machine (a Linux system... if you ask).

isok -> sprite alone
isko -> dump+sprite -> dump give a grey square
isko -> sprite+dump -> dump give a grey square
isko -> dump+sprite+dump -> dump give a grey square
isko -> dump alone -> dump give a grey square
Title: Re: possible bug with sprite and raw textured vertices?
Post by: Laurent on June 13, 2016, 08:43:15 am
As I said... please read the docs. Texture coordinates are not normalized, they must be given in pixels. It's written in the big red box you can see if you click the link I gave you previously.
Title: Re: possible bug with sprite and raw textured vertices?
Post by: marchred on June 13, 2016, 09:13:18 am
wow.....

ok.....

thx!

...


is there a way of saying "resolved" somewhere?
Title: Re: possible bug with sprite and raw textured vertices?
Post by: Laurent on June 13, 2016, 09:14:26 am
Quote
is there a way of saying "resolved" somewhere?
You can edit your first post to add it to the title.