• Create Account

# Rectangle

Member Since 26 Aug 2012
Offline Last Active Jul 21 2014 05:21 PM

### In Topic: GLSL texture "tile selection" algorithm?

07 July 2014 - 01:10 AM

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);
}
```

### In Topic: GLSL texture "tile selection" algorithm?

06 July 2014 - 11:52 PM

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!

### In Topic: GLSL texture "tile selection" algorithm?

06 July 2014 - 11:44 PM

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?

### In Topic: Photoshop-style "Layer View" panel using SDL and C++/MFC?

27 June 2014 - 02:53 AM

Meh. Guess I'll just try my hand at SFML and see if it has better support for this sort of thing.

### In Topic: SlimDX Screenshot

25 June 2014 - 07:44 AM

There's really not enough information here, and it looks like you're not doing any error checking either (unless, of course, you've just simplified the code for posting)...

Does CreateOffscreenPlain succeed and give you a valid pointer? Or does it generate an error?

Is g_device3D a valid pointer to an initialized D3D::Device you've created?

PARTNERS