Applying color palette to Texture

1.1k views Asked by At

Updated after many stupid questions

Objective: To apply the texture palette means GL-shaders (emulate Indexed8 texture format). Internet is full of articles on a subject, and all contained the same solution. I have it, unfortunately, it don't work for me.

So.

The fragment shader:

uniform sampler2D texture;
uniform sampler2D palette;
uniform int paletteIndex;
layout (origin_upper_left) in vec4 gl_FragCoord;

void main()
{
    int colorIndex = int(texture2D(texture, gl_TexCoord[0].xy, 0).r * 255);
    gl_FragColor = texelFetch(palette, ivec2(colorIndex, paletteIndex), 0);
}

Vertex shader:

void main () {
   gl_Position = ftransform ();
   gl_TexCoord [0] = gl_MultiTexCoord0;
}

This is my result (left - my picture, right - original):

  result

Damn, I just set incorrect vertex coords! >.<

Thanks for good man who tried to help. Now I know that different from texture2d and fetchTexel and found reason of issue! D:

1

There are 1 answers

0
Albeoris On BEST ANSWER

Well...

I made ​​a few mistakes:

  1. I generated the wrong texture palette.
  2. I put paletteIndex, as a float, and not as int (wrong uniform param type).
  3. I did not understand the difference between Textrue2D (texture coordinates 0..1) and texelFetch (texel integer coordinates).
  4. Calls the function GL.Viewport (0, 0, w * 2, h * 2); because of this, the image is twice the original.

All these problems have been solved, and now correctly applies palette to the original image. Correct shaders shown above. Thank you for your attention.