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

Author Topic: Using VertexArray  (Read 2761 times)

0 Members and 1 Guest are viewing this topic.

LedLoaf

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Using VertexArray
« on: April 25, 2019, 02:51:33 am »
Hello,

So I'm messing around with VertexArray for displaying tiles. I've checked out the tutorial example here on the SFML site. I'm trying to figure out how I can scale the tile bigger (for learning and clarity purposes). I can do this just by displaying 1 tile, but when the for loop is involved, I'm a bit confused?

The tiles are 32x32, and the tile sheet provided in the example.

       
sf::Vector2i tileSize{32,32};
float Scale = 128;
...    
for loop(... i < 20 ...)
for loop (... j < 10...)
{
// define it as a rectangle, located at (10, 10) and with size 100x100
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.x);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x [b]+ Scale[/b], j * tileSize.y);      
quad[2].position = sf::Vector2f((i + 1) * tileSize.x [b]+ Scale[/b], (j + 1) * tileSize.y [b] + Scale[/b]);
quad[3].position = sf::Vector2f(i* tileSize.x, (j+1)*tileSize.y [b]+ scale[/b]);
...
}

I know this must be a simple formula adjustment. I haven't worked with anything like this yet, but I know it's similar to placing a rectangle in OpenGL.

Also, I believe I read something about using a VertexArray for your tilemap improves speed. Something about how it can be turned into one big image? This was before I got into looking at VertexArray (looked too complicated) and I was trying to figure out the best camera culling methods (not sure of the correct term).

Does anyone perhaps have an example of a big map using VertexArray or perhaps even comparing the differences of using a Sprite for each tile? And even more ideal example would be with culling?

Hope that makes sense, and have a great day.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Using VertexArray
« Reply #1 on: April 25, 2019, 04:52:29 am »
If your tile size at scale 1 is tileSize, then at scale 128 it's tileSize * 128, which is tileSize * Scale. (sounds huge but whatever)

So basically quads positions would be:
quad[0].position = sf::Vector2f(i * tileSize.x * Scale, j * tileSize.y * Scale);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x * Scale, j * tileSize.y);      
quad[2].position = sf::Vector2f((i + 1) * tileSize.x * Scale, (j + 1) * tileSize.y * Scale);
quad[3].position = sf::Vector2f(i* tileSize.x * Scale, (j+1)*tileSize.y * Scale);

texCoords don't change with scale, if you're using them.

LedLoaf

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Using VertexArray
« Reply #2 on: May 01, 2019, 03:31:12 am »
If your tile size at scale 1 is tileSize, then at scale 128 it's tileSize * 128, which is tileSize * Scale. (sounds huge but whatever)

So basically quads positions would be:
quad[0].position = sf::Vector2f(i * tileSize.x * Scale, j * tileSize.y * Scale);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x * Scale, j * tileSize.y);      
quad[2].position = sf::Vector2f((i + 1) * tileSize.x * Scale, (j + 1) * tileSize.y * Scale);
quad[3].position = sf::Vector2f(i* tileSize.x * Scale, (j+1)*tileSize.y * Scale);

texCoords don't change with scale, if you're using them.

Hey, sorry for the late reply. Thank you very much! This question got lost between the million other places I've asked questions haha. Have a great day.

EDIT: Oh, I forgot to add. Is it true VertexArray is more efficient for big tile maps? Thanks!
EDIT2: I had to * Scale at quad[1] on Y as well. But it works!
« Last Edit: May 01, 2019, 03:49:38 am by LedLoaf »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Using VertexArray
« Reply #3 on: May 01, 2019, 11:46:42 am »
Yeah oops I skipped quad[1].
It's (usually?) true that drawing X quads with one vertex array is faster than drawing X sprites.