1

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:

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
Albeoris
  • 109
  • 1
  • 7
  • Nevertheless, you are not sampling from your palette texture correctly. If you want to use `paletteIndex` as an index you should either use `texelFetch (...)` and pass it as an integer or divide the coordinates by the texture size so that they are in the range [**0.0**,**1.0**]. But you also need to apply some extra math to make sure they are texel centers rather than borders. – Andon M. Coleman Jun 18 '14 at 21:02
  • Updated. Thank you! D: (But I don't see any popups on MF site. Hmm...) – Albeoris Jun 19 '14 at 16:23
  • I edited your question for you so the image is actually a part of it, but if you solved your own problem what you should really do is leave the original ***broken*** shader(s) in the question and explain your solution in an *answer* to the question. – Andon M. Coleman Jun 19 '14 at 18:27
  • It was a very chaotic question with many semantic and grammatical errors. I feel ashamed for it. :D Thank you again! – Albeoris Jun 20 '14 at 14:01

1 Answers1

1

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.

Albeoris
  • 109
  • 1
  • 7