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

Author Topic: Question about RenderTexture  (Read 6181 times)

0 Members and 1 Guest are viewing this topic.

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Question about RenderTexture
« on: May 29, 2012, 10:05:38 pm »
I have some question about RenderTexture. I'm making a tilemap. Each tile is 256x256 px and i want to test if i can handle huge map (3900x3900). First time i used a vector and there was a very bad way. Now i've switched to RenderTexture and i draw each tile into it. The problem is one... 3900x3900 or a little example (100x100) isn't possible due to RenderTexture::create() limit. It say me i can create max 8192x8192 px texture (32x32 tile map). Now i have these questions:
1) Is max texture size same in all GPUs or is it a MY GPU limit?
2) To manage bigger map, will it be a good idea to create a vector<RenderTexture> each 32x32?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about RenderTexture
« Reply #1 on: May 29, 2012, 10:12:19 pm »
Quote
1) Is max texture size same in all GPUs or is it a MY GPU limit?
Each GPU has its own limit. Crappy ones can have a limit of 512x512.

Quote
2) To manage bigger map, will it be a good idea to create a vector<RenderTexture> each 32x32?
The best way to manage a tilemap is to use SFML 2's vertex arrays :)
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Question about RenderTexture
« Reply #2 on: May 29, 2012, 10:19:08 pm »
Quote
1) Is max texture size same in all GPUs or is it a MY GPU limit?
Each GPU has its own limit. Crappy ones can have a limit of 512x512.
Quote
2) To manage bigger map, will it be a good idea to create a vector<RenderTexture> each 32x32?
The best way to manage a tilemap is to use SFML 2's vertex arrays :)
Ok, so i will learn about vertex arrays. There are some limitation on vertex arrays?
Max texture size will affect vertex arrays as well? If yes, there is a function that return max texture size?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about RenderTexture
« Reply #3 on: May 29, 2012, 10:38:44 pm »
Vertex arrays are about geometry, they have nothing to do with textures and pixels. There's no limitation on them.
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Question about RenderTexture
« Reply #4 on: May 29, 2012, 10:48:32 pm »
Vertex arrays are about geometry, they have nothing to do with textures and pixels. There's no limitation on them.
Uhm i see... i'm reading documentation, but i can't find for the moment a way to draw a simple 2x2 tilemap :S
How can i put in VertexArray each tile? I'm confused xD

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about RenderTexture
« Reply #5 on: May 30, 2012, 08:01:32 am »
sf::VertexArray tiles(sf::Quads);

for (int i = 0; i < nbTilesX; ++i)
    for (int j = 0; j < nbTilesY; ++j)
    {
        float u = /* X position of the tile in the tilemap texture */
        float v = /* Y position of the tile in the tilemap texture */

        tiles.append(sf::Vertex(sf::Vector2f(i * tileSize, j * tileSize), sf::Vector2f(u, v));
        tiles.append(sf::Vertex(sf::Vector2f((i + 1) * tileSize, j * tileSize), sf::Vector2f(u + tileSize, v));
        tiles.append(sf::Vertex(sf::Vector2f((i + 1) * tileSize, (j + 1) * tileSize), sf::Vector2f(u + tileSize, v + tileSize));
        tiles.append(sf::Vertex(sf::Vector2f(i * tileSize, (j + 1) * tileSize), sf::Vector2f(u, v + tileSize));
    }
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Question about RenderTexture
« Reply #6 on: May 30, 2012, 03:38:27 pm »
sf::VertexArray tiles(sf::Quads);

for (int i = 0; i < nbTilesX; ++i)
    for (int j = 0; j < nbTilesY; ++j)
    {
        float u = /* X position of the tile in the tilemap texture */
        float v = /* Y position of the tile in the tilemap texture */

        tiles.append(sf::Vertex(sf::Vector2f(i * tileSize, j * tileSize), sf::Vector2f(u, v));
        tiles.append(sf::Vertex(sf::Vector2f((i + 1) * tileSize, j * tileSize), sf::Vector2f(u + tileSize, v));
        tiles.append(sf::Vertex(sf::Vector2f((i + 1) * tileSize, (j + 1) * tileSize), sf::Vector2f(u + tileSize, v + tileSize));
        tiles.append(sf::Vertex(sf::Vector2f(i * tileSize, (j + 1) * tileSize), sf::Vector2f(u, v + tileSize));
    }
Yes after some cross searhing i was able to draw a square with 1 tile texture inside. This example wil lbe very usefull for my learning, thx! :D
But i've a question. I see that i can draw all tilemap like this:
window.draw(tiles, &aTexture);
So it will draw only aTexture inside all tilemap, wont't it?
How can i draw something like 3/4++ different tile in my VertexArray with 1 draw call?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about RenderTexture
« Reply #7 on: May 30, 2012, 03:46:33 pm »
You can't draw multiple textures in a single call, you need at least as many calls to 'draw' as the number of textures. Which implies that you must have one vertex array per texture.

But you can usually have all your tiles inside a unique texture, so this shouldn't be a problem.
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Question about RenderTexture and VertexArray!
« Reply #8 on: May 30, 2012, 04:05:29 pm »
You can't draw multiple textures in a single call, you need at least as many calls to 'draw' as the number of textures. Which implies that you must have one vertex array per texture.

But you can usually have all your tiles inside a unique texture, so this shouldn't be a problem.
Uhm i see. I can load single tile and then put them inside a big renderTexture, or i can mape a single .png with all tiles inside (maybe not due to tile size, 256x256)

However, i get this:
http://img513.imageshack.us/img513/1036/88921204.png
From this code:
#define TILE_SIZE 256

sf::RenderWindow window(sf::VideoMode::getDesktopMode(),"MapTest",sf::Style::Fullscreen);
while (window.isOpen()){
        window.clear(sf::Color::Blue);
        sf::Texture temp;
        temp.loadFromFile("data/tileTest.png");
        sf::VertexArray tileMap(sf::Quads);

        for (int i = 0; i < 2; ++i)
                for (int j = 0; j < 2; ++j)
                {
                        float u = (float)i*TILES_SIZE;/* X position of the tile in the tilemap texture */
                        float v = (float)j*TILES_SIZE;/* Y position of the tile in the tilemap texture */

                        tileMap.append(sf::Vertex(sf::Vector2f(i * TILES_SIZE, j * TILES_SIZE), sf::Vector2f(u, v)));
                        tileMap.append(sf::Vertex(sf::Vector2f((i + 1) * TILES_SIZE, j * TILES_SIZE), sf::Vector2f(u + TILES_SIZE, v)));
                        tileMap.append(sf::Vertex(sf::Vector2f((i + 1) * TILES_SIZE, (j + 1) * TILES_SIZE), sf::Vector2f(u + TILES_SIZE, v + TILES_SIZE)));
                        tileMap.append(sf::Vertex(sf::Vector2f(i * TILES_SIZE, (j + 1) * TILES_SIZE), sf::Vector2f(u, v + TILES_SIZE)));
                        }
        window.draw(tileMap,&temp);
        window.display();
        }
return EXIT_SUCCESS;
}
 
This is what i expected:
http://img12.imageshack.us/img12/924/48291745.png

You can download tileTest.png if you want test!

[attachment deleted by admin]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about RenderTexture
« Reply #9 on: May 30, 2012, 04:16:33 pm »
The second argument of sf::Vertex, the texture coordinates, defines what part of the texture is mapped on the quad. So if you want to display four identical tiles, their texture coordinates should be the same instead of depending on i and j.

i and j should only be used to calculate the position of the quads, so that they are drawn next to each other.

This code:
tileMap.append(sf::Vertex(sf::Vector2f(i * TILES_SIZE, j * TILES_SIZE), sf::Vector2f(u, v)));
tileMap.append(sf::Vertex(sf::Vector2f((i + 1) * TILES_SIZE, j * TILES_SIZE), sf::Vector2f(u + TILES_SIZE, v)));
tileMap.append(sf::Vertex(sf::Vector2f((i + 1) * TILES_SIZE, (j + 1) * TILES_SIZE), sf::Vector2f(u + TILES_SIZE, v + TILES_SIZE)));
tileMap.append(sf::Vertex(sf::Vector2f(i * TILES_SIZE, (j + 1) * TILES_SIZE), sf::Vector2f(u, v + TILES_SIZE)));
 
... is equivalent to:
sf::Sprite sprite;
sprite.setPosition(i * TILE_SIZE, j * TILE_SIZE);
sprite.setTextureRect(sf::IntRect(u, v, TILE_SIZE, TILE_SIZE));
« Last Edit: May 30, 2012, 04:21:47 pm by Laurent »
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Question about RenderTexture
« Reply #10 on: May 30, 2012, 04:42:38 pm »
Great!
for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j)
        {
             tileMap.append(sf::Vertex(sf::Vector2f(i * TILES_SIZE, j * TILES_SIZE), sf::Vector2f(0, 0)));
             tileMap.append(sf::Vertex(sf::Vector2f((i + 1) * TILES_SIZE, j * TILES_SIZE), sf::Vector2f(TILES_SIZE, 0)));
             tileMap.append(sf::Vertex(sf::Vector2f((i + 1) * TILES_SIZE, (j + 1) * TILES_SIZE), sf::Vector2f(TILES_SIZE,  TILES_SIZE)));
             tileMap.append(sf::Vertex(sf::Vector2f(i * TILES_SIZE, (j + 1) * TILES_SIZE), sf::Vector2f(0,TILES_SIZE)));
        }
 
With this code i get the tile repeated 4 times!
Now lets make it more difficult...
To draw 2 different tile inside a tileMap, have I to create different VertexArray for each tile?
Example:
I've a map like this:
0 1 0
1 0 1
0 1 0
Where 0 is tileTest.png and 1 is anothe tile image.
Have i to create 2 VertexArray wich one store the 0s quad's coordinates and the other store for 1, then to draw them use something like this:
window.draw(zeroVertexArray,&tileTest);
windows.draw(oneVertexArray,&otherTileTexture);
So finally to draw n tile in tile map the fastest way is to work with std::vector<VertexArray>,  isn't it?


PS: Sorry for bad english XD

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about RenderTexture
« Reply #11 on: May 30, 2012, 04:48:01 pm »
Yes, correct. But try to gather as many tiles as possible inside single textures. You can even do it programmatically, so that you can use the maximum texture size allowed by the user's graphics card (with recent graphics cards, you can have 1024 different tiles in one texture!).
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
Re: Question about RenderTexture
« Reply #12 on: May 30, 2012, 07:16:23 pm »
Yes, correct. But try to gather as many tiles as possible inside single textures. You can even do it programmatically, so that you can use the maximum texture size allowed by the user's graphics card (with recent graphics cards, you can have 1024 different tiles in one texture!).
Yes, with my HD 4890 i can load a 32x32 tile texture :D Now the next step is to use one texture with all tiles inside!
If all will go well i can draw my map in one call. I don't thing i will use more than 1024 tiles for my game, for the moment.
Thx for all your help and tips :D

 

anything