SFML community forums

Help => Graphics => Topic started by: Tivi on February 27, 2015, 04:32:41 pm

Title: [Solved][Trash] texture coordinates in vertex for any shape
Post by: Tivi on February 27, 2015, 04:32:41 pm
How do I set the texture coordinates in sf::Vertex automatically for any shape like sfml do it?
Title: Re: Own vertex shape and texture
Post by: Laurent on February 27, 2015, 10:41:26 pm
We can't help you to fix your code if we don't see it.
Title: Re: Own vertex shape and texture
Post by: Tivi on February 28, 2015, 10:58:13 am
Thanks for your answer. I wrote it too complicated. I need a function to set texture coordinates for any vertices/shape like in any sfml shape.

Here's how i did for rectangle:

int main()
{

        sf::Texture texture;
        texture.loadFromFile("test.png");

        sf::RenderWindow window(sf::VideoMode(900, 800), "Test");

        sf::Vertex* a = new sf::Vertex[4];

        a[0].position = sf::Vector2f(1, 1);
        a[1].position = sf::Vector2f(100, 1);
        a[2].position = sf::Vector2f(100, 100);
        a[3].position = sf::Vector2f(1, 100);

        sf::Vector2u size = texture.getSize();

        a[0].texCoords = sf::Vector2f(0, 0);
        a[1].texCoords = sf::Vector2f(size.x - 1.0f, 0);
        a[2].texCoords = sf::Vector2f(size.x - 1, size.y - 1);
        a[3].texCoords = sf::Vector2f(0, size.y - 1.0f);


        window.draw(a, 4, sf::PrimitiveType::TrianglesFan, &texture);
        window.display();

        delete[] a;

        while (window.isOpen())
        {
        }
}
 

But when i add more vertices and make it convex the texture doesn't display correctly. I understand why it can't work and would like to know how it's done in sfml-shape.
Title: Re: texture coordinates in vertex for any shape
Post by: Laurent on February 28, 2015, 11:34:17 am
Quote
I understand why it can't work and would like to know how it's done in sfml-shape.
Well then look at the source code, I think it's easy enough.
Title: Re: texture coordinates in vertex for any shape
Post by: Tivi on February 28, 2015, 01:00:25 pm
Solved. I didn't understood the source but now i got it. Thanks anyway.