# GLSL texture "tile selection" algorithm?

This topic is 1658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In theory, let's say I have a vertex shader which knows:

• The dimensions of a 2D texture/tilesheet (in pixels)
• The dimensions of each tile within that texture (in pixels)
• The number of tiles within the texture (not necessarily RowCount * ColumnCount... The final row may fall short X number of tiles)
• A given "TileIndex", representing a desired tile # which increments from left to right, top to bottom (in that order)

Using the built-in gl_VertexID input variable to aid in the creation of proper texture coordinates, what sort of math/algorithm must be used to get this shader to "select" the correct tile index and corresponding texture coordinates (to be passed on to the fragment shader)?

I believe it has something to do with X + Y * Width, or perhaps the use of a modulus operation somewhere, but it becomes complicated when the only given is a single tile index value...

So, I've only gotten as far as the following:

#version 330 core

layout(location=0) in vec3 in_pos;      // Vertex Position
layout(location=1) in int in_tileIndex; // Desired Tile Index
out vec2 out_texCoord;                  // Resulting Texture Coordinates
//  (passed on to the frag shader)

void main()
{
// The following variables will eventually be uniforms/attributes, and are only
//  used here for more immediate debugging while I figure this algorithm out...

float tileWidth = 32;               // In pixels
float tileHeight = 32;              // In pixels
float tileCount = 88;               // Not always rows * cols
float imgWidth = 255;               // In pixels
float imgHeight = 351;              // In pixels
float width = 1.0f / tileWidth;     // In textels (To be used as a scalar down below?)
float height = 1.0f / tileHeight;   // In textels (To be used as a scalar down below?)

///////////////////////////////////////
// NOT SURE WHAT TO PUT HERE...
// (need to find a way to use the
//  above info to generate correct
//  texture coordinates below...)
///////////////////////////////////////

int vid = gl_VertexID % 4;
switch(vid) {
case 0:
out_texCoord = vec2(0, 1);  // Currently set to the entire image (instead of a single tile within it)
break;
case 1:
out_texCoord = vec2(1, 1);  // Currently set to the entire image (instead of a single tile within it)
break;
case 2:
out_texCoord = vec2(0, 0);  // Currently set to the entire image (instead of a single tile within it)
break;
case 3:
out_texCoord = vec2(1, 0);  // Currently set to the entire image (instead of a single tile within it)
break;
}
gl_Position = vec4(in_pos, 1);
}

Edited by Rectangle

##### Share on other sites

You have no use for how many tiles there are in total. You need the number of tiles in a row, which I will call tilesWidth:

int tileX = tileID % tilesWidth;

int tileY = tileID / tilesWidth;

out_texCoord = vec2(float(tileX) * tileWidth, float(tileY) * tileHeight);

which can be further reduced to:

out_texCoord = vec2(float(tileX), float(tileY)) * tileSize;

for gl_VertexID = 0:

out_texCoord += vec2(0, tileHeight);

You can also use a static array of texture coordinates to add based on gl_VertexID % 4. No need for branches.

Edited by Kaptein

##### Share on other sites

Here's a more visual example of what I am trying to accomplish...

Let's say I have the following texture:

You might notice that the tile index goes from left to right, top to bottom.

There is no X/Y or Row/Column variables necessary, as long as we know that each cell is 32x32 pixels, and that the overall image is 256x128...

You may also notice that the last 4 tiles are not part of this tilesheet, making a total of only 28 tiles in this image (out of a possible 32, in this case).

But when dealing with shaders, we are working with normalized textels which represent the entire image.

Effectively, this means that we would need to scale down the width & height of each cell to be some arbitrary value between 0 and 1, depending on the actual dimensions of the image AND the dimensions of each cell within the image, and somehow use that information (along with the current tile index and maximum number of tiles within the image) to come up with all four corners (in textels) of the corresponding desired cell.

Then, depending on which vertex # we are working on, we would need to pass the correct values to the fragment shader, where the sampler can make use of these newly calculated texture coordinates, effectively displaying a single tile/cell of the texture.

Any idea how can this be achieved?

##### Share on other sites

You have no use for how many tiles there are in total. You need the number of tiles in a row, which I will call tilesWidth:

...

You can also use a static array of texture coordinates to add based on gl_VertexID % 4. No need for branches.

Hrmm... Strangely enough, your post didn't show up until after I made my last post...
Anyhow, yes there is a use for a maximum tile count (see my last post for details).
And could you please elaborate on what you mean by "You can also use a static array of texture coordinates to add based on gl_VertexID % 4"?
I'm only using gl_VertexID to verify which "corner" of the image we are currently dealing with. Am I missing an alternative, more efficient solution to this?

Thanks!

##### Share on other sites

Using the following code (and with help from Kaptein), I could've sworn I had this whole thing solved...

Guess not. It just shows a solid color from the texture...

#version 330 core

layout(location=0) in vec3 in_pos;
out vec2 out_texCoord;

void main()
{
int tileNum = 1;
int tileCount = 88;
int tileIndex = tileNum % tileCount;

float tileWidth = 32;
float tileHeight = 32;
float imgWidth = 255;
float imgHeight = 351;

int columnCount = int(imgWidth / tileWidth);
int rowCount = int(imgHeight / tileHeight); // Unused
int tileX = tileIndex % columnCount;
int tileY = int(float(tileIndex) / float(columnCount));

float startX = float(tileX) * tileWidth;
float startY = float(tileY) * tileHeight;
float endX = 1.0f / (startX + tileWidth);
float endY = 1.0f / (startY + tileHeight);
startX = 1.0f / startX;
startY = 1.0f / startY;

int vid = gl_VertexID % 4;
switch(vid) {
case 0:
out_texCoord = vec2(startX, endY);
break;
case 1:
out_texCoord = vec2(endX, endY);
break;
case 2:
out_texCoord = vec2(startX, startY);
break;
case 3:
out_texCoord = vec2(endX, startY);
break;
}
gl_Position = vec4(in_pos, 1);
}

Edited by Rectangle

##### Share on other sites

int tileY = int(float(tileIndex) / float(columnCount));

you want integer division here

you are still not % by rowsize, instead you are % by total count, which makes no sense to me

i also seriously doubt that the image is 255 wide

so, lets try again:

#version 330 core

layout(location=0) in vec3 in_pos;
out vec2 out_texCoord;

void main()
{
int tileIndex = 1;

int tileWidth  = 32;
int tileHeight = 32;
int imgWidth  = 256;
int imgHeight = 352;

// tiles per row (send as uniform)
int tilesX = imgWidth / tileWidth;
int tileX = tileIndex % tilesX;
int tileY = tileIndex / tilesX;

// tile size (send this as vec2 uniform)
float tx = float(tileWidth) / float(imgWidth);
float ty = float(tileHeight) / float(imgHeight);
// start position
vec2 start = vec2(float(tileX), float(tileY)) * vec2(tx, ty);
// make it so that the tiles start from the topmost row
start.y = 1.0 - start.y - ty;

int vid = gl_VertexID % 4;
switch(vid)
{
case 0:
out_texCoord = start + vec2(0.0, 0.0); // (0, 0)
break;
case 1:
out_texCoord = start + vec2(tx, 0.0); // (1, 0)
break;
case 2:
out_texCoord = start + vec2(tx, ty); // (1, 1)
break;
case 3:
out_texCoord = start + vec2(tx, ty); // (0, 1)
break;
}
gl_Position = vec4(in_pos, 1);
}


You also may want to use mod(x, y) instead of %, and you have a fixed amount of tiles in width (8) you can use bit-operations instead of % and idiv.

You can also get texture size from this: https://www.opengl.org/sdk/docs/man/html/textureSize.xhtml

But I recommend just sending the required uniforms precalculated.

All in all, you need a vec2 uniform: vec2(tileSizeX, tileSizeY) and the tiles per row: int tilesX

I wrote the new switch() statement like I did to prove a point. As you can see you are multiplying with the constants I put in the comments.

vec2 texCoords[] =

{

vec2(0.0, 0.0),

vec2(1.0, 0.0)

vec2(1.0, 1.0),

vec2(0.0, 1.0)

};

so out_TexCoords = start + tileSize * texCoords[vid];

No need for switch() statement.

Edited by Kaptein

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015504
×