SFML community forums

Help => Graphics => Topic started by: LimeFruut on June 23, 2017, 02:01:45 am

Title: 1024 x 1024 tile map
Post by: LimeFruut on June 23, 2017, 02:01:45 am
I was browsing the forum and I've read the tutorial on drawing tile maps, but it uses quads only. I would like to use a vector of vertex in order to draw each line of the array as a triangle strip, but I don't know how to do it yet. Currently this is the way I am storing each tile type from a file. I plan to use this array for path finding at a later point. Maybe I can use this to insert into the vertex vector?

if (file.is_open())
   {
      while (!file.eof())
         for (int row = 0; row < 1024; row++)
         {
            getline(file, line);
            stringstream ssLine(line);
            for (int col = 0; col < 1024; col++)
            {
               string sValue;
               getline(ssLine, sValue, ',');

               stringstream convertor(sValue);
               convertor >> MapTiles[row][col];
            

            }
         }

      file.close();
   }

the idea is to to have 1024 triangle strips, which I will draw, thus making it quicker.

Thanks in advance!
Title: Re: 1024 x 1024 tile map
Post by: eXpl0it3r on June 23, 2017, 06:28:32 am
You didn't tell us with what you're stuck with. How can we help you?
Title: Re: 1024 x 1024 tile map
Post by: LimeFruut on June 23, 2017, 11:05:56 am
So what I want to do is create the vertecies while im filling up the array. So every 1024 columns would be 1 triangle strip. Also I am aware that i have to select the texture/color of the maptile as I am giving them vector2f value. This way I would reduce my draw calls from 1024*1024 to only 1024
Title: Re: 1024 x 1024 tile map
Post by: Arcade on June 23, 2017, 04:47:55 pm
Like eXpl0it3r, I'm not sure I'm understanding what your actual question is. I think I understand what you are trying to do, but what are you actually stuck on? Here is the tutorial for vertex arrays (https://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php). Is this the one you read? Do you not know how to even get started or are you just unsure on how to calculate the position of the vertices?
Title: Re: 1024 x 1024 tile map
Post by: LimeFruut on June 23, 2017, 05:40:43 pm
So I've added this method to the loop which creates 4 vertecies in a
vector<vector<sf::Vertex>> vertices;

this is the way I'm currently assigning them

void CGameMap::CreateRenderingVertecies(int row,int col)
{
   int temp = col * 4;
   sf::Vertex* tStrip = &vertices[row][temp];
   tStrip[0].position = sf::Vector2f(temp / 4 * multiplier row * multiplier);
   tStrip[1].position = sf::Vector2f(temp / 4 * multiplier, (row + 1) * multiplier);
   tStrip[2].position = sf::Vector2f((temp / 4 + 1)*multiplier, row * multiplier);
   tStrip[3].position = sf::Vector2f((temp / 4 + 1)*multiplier, (row + 1) * multiplier);

   tStrip[0].texCoords = sf::Vector2f(0, 0);
   tStrip[1].texCoords = sf::Vector2f(0, 16);
   tStrip[2].texCoords = sf::Vector2f(16, 0);
   tStrip[3].texCoords = sf::Vector2f(16, 16);
}

I am not sure, how to attach the texture and/or states in order to use it in my draw method where im passing a reference of the window.
Title: Re: 1024 x 1024 tile map
Post by: Hapax on July 01, 2017, 04:09:01 pm
For a row of triangle strips, it's better to think of it in terms of two vertices at a time rather than four: top then bottom for each side (strip edge or adjoining quads). The number of these "sides" should be the number of quads plus one. Basically, loop quad+1 number of times and process only two vertices at a time.

Note that 1024 draw calls (one for each row vertex array) can be quite a lot. It would make more sense to draw the entire map with just one call: add each row to the same vertex array. You can use the triangle strip to draw all rows in one go!
There are a couple of things to note:

You may want to consider that it's simpler to just use quads (instead of a triangle strip) in a single vertex array. That is, you can just add multiple (mostly) independent quads to a vertex array. This is still only one draw call, which is better than 1024 triangle strips and much better than the call per quad that you were originally trying to avoid.
Title: Re: 1024 x 1024 tile map
Post by: Stauricus on July 02, 2017, 01:19:10 am
you want a tilemap made of triangles? like this?
(https://www.researchgate.net/profile/Ryutarou_Ohbuchi2/publication/221571429/figure/fig5/AS:305454300319746@1449837388154/Figure-14-A-triangle-strip-consisting-of-27-triangles-was-cut-out-a-triangle-mesh-214.png)

is that it?