[SOLVED] N00b shader question

Some background: I want to read sprite from a sprite sheet by using a custom shader. Nehon gave me some code to start and I’ve managed to hack something. The spritesheet looks like this:

0   1   2   3   4
5   6   7   8   9
10  11  12

I have this shader code for detecting the portion of a sprite sheet:

float tPointerY = (floor(t / m_SizeX)) / m_SizeY;

it works correctly, in that I get the pointer of the sprite… from the bottom. Meaning that when “t” is equal to 0, I get the sprite “10”. To fix this, I’ve changed the code to:

float tPointerY = ((m_SizeY - 1.0) - floor(t / m_SizeX)) / m_SizeY;

But I only see the second row. :frowning:

Full code here:



uniform mat4 g_WorldViewProjectionMatrix;
uniform float m_SizeX;
uniform float m_SizeY;
uniform float m_Position;

attribute vec3 inPosition;
attribute vec2 inTexCoord;

varying vec2 texCoord;

void main(){

    float t = m_Position;
    float tPointerY = ((m_SizeY - 1.0) - floor(t / m_SizeX)) / m_SizeY;
    float tPointerX = (t - (tPointerY * m_SizeX * m_SizeY)) / m_SizeX;
    texCoord.x = inTexCoord.x / m_SizeX + tPointerX;
    texCoord.y = inTexCoord.y / m_SizeY + tPointerY;

    gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
}

If it’s upside down then why not just:
float tPointerY = 1.0 - ((floor(t / m_SizeX)) / m_SizeY);

…or whatever.

1 Like

mhhh, the code I gave you should work from the top.
I don’t remember having that issue

Fixed code:

    float t = m_Position;
    float tPointerY = 1.0 - ((floor(m_Position / m_SizeX)) / m_SizeY) - 1.0 / m_SizeY;
    float tPointerYOffset = (floor(t / m_SizeX)) / m_SizeY;
    float tPointerX = (t - (tPointerYOffset * m_SizeX * m_SizeY)) / m_SizeX;
    texCoord.x = inTexCoord.x / m_SizeX + tPointerX;
    texCoord.y = inTexCoord.y / m_SizeY + tPointerY;

TY all! :smile:

1 Like

Thanks for sharing solution :smiley: