Clamping texture coordinates

Hello,

My problem is that I have an array of vertices with a “hue” value that determines the index into my texture, this “hue” value is clamped to a different range on user input.
For example if the max and min value for the hue is -200 - 200( -200 maps to 0 in the texture and 200 to 1 in the texture), if the user change the range to -50 - 50 I want all the values from -50 to 50 to stay the same but all the values from -200 to - 50 to be mapped to 50 and all the values greater than 50 to be mapped to 50.
I don’t want to recreate the texture coordinates for all the vertices on every range change, could this be done?

I know I can do it with shaders, but my current hardware configuration does not support shaders.

Thanks,
Ido

Hi Ido,

you can do that with the texture matrix (see chapter 2.11.2 in the 1.5 spec). It allows you to do scaling/translation of texture coords before the texture access, and is generally better supported of systems that can’t do shaders.

Hope it helps

Dirk

Hi,

I thought about using the texture matrix, but I can’t get the same mapping, I want the all the value that are inside the new range not to change and all the values outside the range to be clamped to the range.
I’ve tried using clamped texture and translating and scaling the texture matrix by the range factors, but results are different as I want only “selected” coordinates to change.

Is there another solution or did I use the texture matrix the wrong way?

Thanks,
Ido

Wouldn’t it work to use the texture matrix to scale the desired range (e.g., [-50,50] or [-200,200] in your examples) to [0,1] then use one of the existing texture coordinate clamping modes (i.e., GL_CLAMP_TO_EDGE or GL_CLAMP_TO_BORDER) to deal with values that end up outside [0,1] after the scale / bias?

A fairly easy way would be to use a fragment program/shader.

First apply a linear texcoordmapping which maps the corners of your selected region to [0,1]x[0,1]. So texcoords out of the selected region are out of the [0,1]x[0,1] range with possible negative values or values greater than 1.

Part below executed in fragment program
Apply saturation on all of the texture coordinates. Finally apply the reverse mapping from before on these texture coordinates to the selected region and use them for texture access.

Nico

Originally posted by idr:
Wouldn’t it work to use the texture matrix to scale the desired range (e.g., [-50,50] or [-200,200] in your examples) to [0,1] then use one of the existing texture coordinate clamping modes (i.e., GL_CLAMP_TO_EDGE or GL_CLAMP_TO_BORDER) to deal with values that end up outside [0,1] after the scale / bias?
Nope, if I understood it right now he wants to change the clamp range from [0 … 1] to something like [.3 … .8], so that values inside the clamp range don’t change, only values outside are clamped.

I can’t think of a fixed function way to really do that. The best solution I can see without a shader would be to use the texmatrix to do the clamping to the desired range, and regenerate and redefine the texture to only contain hte colors you want. If you really just have a gradient this will be just a 1D texture, which shouldn’t be a problem to generate and upload.

Hope it helps

 Dirk

Thanks All,

By Dirk:

Nope, if I understood it right now he wants to change the clamp range from [0 … 1] to something like [.3 … .8], so that values inside the clamp range don’t change, only values outside are clamped.

Thats exactly what I want, my texture is a 2D texture with different color gradient at each row, I know I can change the texture but I thought there may be some way to do it more cleanly with some smart mapping.

Thanks all for the help.
Ido

Ah. From the original problem description, I thought something completely different was wanted. It sounds like you want something like GL_SGIX_texture_coordinate_clamp , but that allows clamping the min and max. Sadly, nothing like that exists.

Not to give you even more bad news, but this is another case, like emulating paletted textures that will really only work with GL_NEAREST sampling if you do it in a fragment program (because you may get blended samples outside the desired range).