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

Author Topic: [Solved]Vertex arrays  (Read 2903 times)

0 Members and 1 Guest are viewing this topic.

SentUnto_1

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved]Vertex arrays
« on: February 21, 2021, 11:04:08 pm »
I am making a tile-based game and have just finished reading through the vertex array tutorial. After some background reading it seems that using vertex arrays to handle my tile generation I save alot of hassle and hardware stress. The part that confuses me, and I know it's probably a silly question, is how do I create a vertex array for more than 4 textures? E.g. my tile-map contains currently 10 different textures, but I can't see how the tutorial can be adapted for more than 4. Apologies for such a low level question.
« Last Edit: February 22, 2021, 04:21:01 pm by SentUnto_1 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Vertex arrays
« Reply #1 on: February 22, 2021, 12:02:09 am »
Generally it's also recommended to reduce the number of textures where possible/it makes sense, also known as texture atlas.
If you want to keep the textures separated, you'll need a vertex array for each texture.
You could then for example use a std::vector<sf::VertexArray> to handle the 10 different vertex arrays.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SentUnto_1

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Vertex arrays
« Reply #2 on: February 22, 2021, 12:13:11 am »
Thank you for your reply!

So the tutorial shows an example that uses an atlas that contains 4 different tiles (that's what an atlas is right, same as a tile-set?). Say my 10 textures were now each a seperate tile in an atlas and I now have 1 texture with 10 tile options. The example demonstrates how to use the atlas that contains 4 tiles, but how can I adapt it to handle my 10? All I can see is quad[0].position to quad[3].position and quad[0].texCoords to quad[3].texCoords. I guess my question is, at what part of the tutorial am I able to change how many tiles that I can use from 4 to >4?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Vertex arrays
« Reply #3 on: February 22, 2021, 08:10:37 am »
You need to understand what each part does.
The main goal is to select a sub-section of the texture and render that to the screen.

The texCoords defines the coordinates for the vertex on the texture. By setting texCoords for all four corners of you quad on the texture, you're selecting the wanted sub-section on the texture.

The position defines where in the world space the vertex is placed. You can place each vertex in a 1:1 scale to the selected sub-section so it will be render with the same size. But you can also make it larger or smaller depending on your needs.

To support more than four tiles, you just modify the texCoords further to select your additional tiles.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SentUnto_1

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Vertex arrays
« Reply #4 on: February 22, 2021, 11:01:35 am »
Thank you so much for your help, I've started to play with the texCoords and it's working well! There's still a bit of a distortion on the first column of tiles but I'm sure i'll figure it out from here. Is there any way to close this question now that you have answered it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Vertex arrays
« Reply #5 on: February 22, 2021, 01:19:10 pm »
Glad to hear. Make sure you have the correct index for all the vertices and that they are in a counter-clockwise rotation.

Is there any way to close this question now that you have answered it?
Not really. Some edit the title of the first post and add [Solved] or similar, but it's not required
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/