PDA

View Full Version : lookup table problem...



UT666
05-05-2006, 08:50 AM
hello,

im trying to use a lookup table to remap the values of a 3d texture to some function... However i seem to be getting some errors even with the simplest of functions ie: (linear)..

here is the 3d texture loading code:


glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glextensions.glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, volobj->texwidth, volobj->texheight, volobj->texdepth, 0, GL_RGBA, GL_UNSIGNED_BYTE, volobj->texture3d);Note volobj->texture3d is of type unsigned char.
Next i upload my lookup table;



glBindTexture(GL_TEXTURE_2D, 1);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &rgb_tfunc[0]);Again rgb_tfunc is of type unsigned and is linear ie: 0 -> 255.
Then in my shader i do the following:



//get the texture rgb data
vec4 texValue = texture3D(volumeTexture, gl_TexCoord[0].stp);

//get lookup table values for the texValue
RGBlookupValue.x = (texture2D(RGBlookupTexture, vec2(texValue.x, 0.0))).x;
RGBlookupValue.y = (texture2D(RGBlookupTexture, vec2(texValue.y, 0.0))).y;
RGBlookupValue.z = (texture2D(RGBlookupTexture, vec2(texValue.z, 0.0))).z;

//check if values are the same.
if(texValue.x!=RGBlookupValue.x)
{
finalColour.x = 1.0;
finalColour.y = 1.0;
finalColour.z = 0.0;
finalColour.w = 1.0;
}And what i seem to get is that it works fine unless my 3d texture value is equal or above 240. If the later is true then my if statement is true. Thought it could be some clamping issue, but tried repeat & mirrored with no luck, also tried to set the 3d texture filter to nearest too, but appart from making everything look horrible still have the problem.

Im out of ideas, any help would be greatful.
Thanks,

ut.

def
05-05-2006, 08:20 PM
Try to sample from the center of the texels not the edges and do a compare by amount of difference. Using != on floats is never really save.


texture2D(RGBlookupTexture, vec2(texValue.x + (1.0/256.0/2.0), 0.5))

UT666
05-06-2006, 07:08 AM
Hello,

Thanks for the help, sampling the lookup at the center instead of edges seems to have sorted my
problem.

Tried to look at the difference, but hard to quantify as i was mapping it as a shade of yellow, was all pretty black, so i guess the difference is small...

Also took two screenshots:

1) with my shader enabled and a linear function remap (ie shouldnt change the data)
2) with the shader disabled

and did a substract in photoshop and now they are the same (probably not the best test, but easy). As I said above I was getting artifacts with my previous code for values above 240.

One thing i dont understand is if it's a problem with where i sample my texture, ie: edge vs centre, how come the error was not for all values
and just for >240. Was i accumulating some sampling error?

thanks again.

ut.

def
05-06-2006, 03:02 PM
It has to do with limited precision and floating point numbers.
You were sampling inbetween texels with NEAREST filtering, so which texel to sample depends on the smallest fraction and can go either way.

sqrt[-1]
05-06-2006, 09:36 PM
Did you try GL_CLAMP_TO_EDGE instead of GL_CLAMP? (Unless you need texture borders for some reason - seeing this a bit lately)