2

I have a look up texture as red channel (R8). I upload it to Pixel Shader and there I want to read pixel value (range 0 - 255) and use value for indexing an array of colors. Texture is non-power of two. If I do

float v = texture2D(s, c).r;
int index = int(v * 255);

results are not always correct. First, there is linear filter pressent, that mess up result (and for NPT texture, only linear filter is supported in ES 2.0) and sometimes also round error occurs (in interpolation of texcoord)

Is there any solution ?

Martin Perry
  • 9,232
  • 8
  • 46
  • 114

1 Answers1

0

It's not true that only linear filter is supported in ES 2.0 with NPOT textures. Nearest filtering is supported as well. You do however, often have to set CLAMP_TO_EDGE for NPOT. (Newer hardware has no limitations on NPOT at all though)

Set this when creating the texture:

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

You might also want to declare the precision if you are having rounding errors. e.g. use 'highp float' instead of 'float'

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
Muzza
  • 1,236
  • 9
  • 13