• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Rectangle

GLSL texture "tile selection" algorithm?

5 posts in this topic

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
0

Share this post


Link to post
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
0

Share this post


Link to post
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:

 

example.png

 

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?

0

Share this post


Link to post
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!

0

Share this post


Link to post
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
0

Share this post


Link to post
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0