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

Author Topic: Help with texture coordinates on a vertex array  (Read 4787 times)

0 Members and 1 Guest are viewing this topic.

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Help with texture coordinates on a vertex array
« on: February 07, 2014, 06:53:47 pm »
Hello everyone! For something I'm doing (giving a sprite perspective by skewing parts of the image), I need to map an image to a vertexArray (specifically the sf::Quad variety). I've read the tutorials, looked at pretty much all the questions that have been answered concerning vertexArrays. But I still don't even remotely understand what numbers I should plug into the vertex's texCoords variable.

For testing purposes, I'm trying to map this image to a square



but it comes out looking like this



my code is as follows

sf::Texture texture;
texture.loadFromFile("boxie.png");
sf::Vector2u size = texture.getSize();

sf::VertexArray lines(sf::Quads, 4);

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

lines[0].texCoords = sf::Vector2f(size.x/2,size.y/2);
lines[0].texCoords = sf::Vector2f(size.x/2,size.y);
lines[0].texCoords = sf::Vector2f(size.x,size.y);
lines[0].texCoords = sf::Vector2f(size.x,size.y/2);

I know that this must be something obvious that I'm missing. But I just can't figure out what numbers I should plug in to get the top left corner of the square to be the top left of the image, the bottom left to be the bottom left, etc. Does anyone know what I'm doing wrong? (and have some sort of explanation as to WHY that's the case?

Ultimately, I hope to have the upper-torso from the front,back, and side in a single file, then use the texCoords to select the appropriate part of the image for the direction the entity is moving (and adjust the shape of the quad to give perspective during certain parts of the animation). But I'm just completely stuck when it comes to the texCoords.

Thanks for reading!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with texture coordinates on a vertex array
« Reply #1 on: February 07, 2014, 07:07:51 pm »
Ah, I see you found the VertexArrays yourself :)

The texture coordinates should follow the perimeter of the texture:
(0, 0)
(size.x - 1, 0)
(size.x - 1, size.y - 1)
(0, size.y - 1)

You're currently only mapping the top-right quarter ;)

Also, n should not all be zero in "lines[n]"

Try this:
lines[0].texCoords = sf::Vector2f(0, 0);
lines[1].texCoords = sf::Vector2f(0, size.y - 1);
lines[2].texCoords = sf::Vector2f(size.x - 1,size.y - 1);
lines[3].texCoords = sf::Vector2f(size.x - 1, 0);

EDIT: Just noticed that your coordinates are in the opposite direction so I've edited the code.
« Last Edit: February 07, 2014, 07:11:05 pm by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Help with texture coordinates on a vertex array
« Reply #2 on: February 07, 2014, 07:20:44 pm »
*facepalm* I can't believe I forgot to change the lines[0] to the appropriate number each time. I feel dumb XD

Thanks so much for helping me! I honestly don't think I would have ever noticed, and I would have just tried completely rewriting it eventually and never understood what I'd done wrong the first time.

Out of curiosity, why am I subtracting 1 from the size each time? What's going on there?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with texture coordinates on a vertex array
« Reply #3 on: February 07, 2014, 07:23:24 pm »
They are positions of the final pixel in the texture, not sizes of the rectangle. If the first coord is at (1, 1), the last pixel is (size.x, size.y). Since the first coord is at (0, 0), the last pixel would be at (size.x - 1, size.y - 1).

And, you're welcome :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Help with texture coordinates on a vertex array
« Reply #4 on: February 07, 2014, 07:31:01 pm »
Ah ok. That makes a lot of sense.

Well now that I'm armed with a full knowledge of how the texture coordinates work, time to make a custom drawable!

Thanks again!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with texture coordinates on a vertex array
« Reply #5 on: February 07, 2014, 07:43:32 pm »
No need to thank me. This is my first attempt at using vertex arrays and I did it because your question inspired me to learn about it, so I should be thanking you :p
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything