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

Author Topic: [Solved][Trash] texture coordinates in vertex for any shape  (Read 2036 times)

0 Members and 1 Guest are viewing this topic.

Tivi

  • Newbie
  • *
  • Posts: 3
    • View Profile
[Solved][Trash] texture coordinates in vertex for any shape
« 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?
« Last Edit: February 28, 2015, 03:57:09 pm by Tivi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Own vertex shape and texture
« Reply #1 on: February 27, 2015, 10:41:26 pm »
We can't help you to fix your code if we don't see it.
Laurent Gomila - SFML developer

Tivi

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Own vertex shape and texture
« Reply #2 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.
« Last Edit: February 28, 2015, 11:04:05 am by Tivi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: texture coordinates in vertex for any shape
« Reply #3 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.
Laurent Gomila - SFML developer

Tivi

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: texture coordinates in vertex for any shape
« Reply #4 on: February 28, 2015, 01:00:25 pm »
Solved. I didn't understood the source but now i got it. Thanks anyway.
« Last Edit: February 28, 2015, 03:55:55 pm by Tivi »

 

anything