for a project i have to interpolate between -2048 and 2047, right now i add 2048 to all values, and then divide by 4095. which ensures the values are in the range
[0…1]. But since it’s used for displaying colors i only get greyscale values that have 256 different tones of grey. What i really would like to have is a ‘algorithm’ that interpolates between -2048(pure red) for instance and 2047(pure green) much like the gradient paint found in graphics2d class.
how to make this
If I understand you correctly, you want to interpolate from one colour to another. This is relatively trivial, you simply interpolate each component individually. Given r1, g1, b1
the components of color1
, and r2, g2, b2
the components of color2
, as well as n
the number of steps, you compute dr=(r2-r1)/n
, dg=(g2-g1)/n
, db=(b2-b1)/n
the delta values from one interpolation step to the next, then starting from color1 you just keep adding the delta values n times. Beware of rounding errors along the way…
On the other hand, why not just let OpenGL do the job for you, with smooth shading which is the default shading model as well…
if you still want to do it yourself, here is the code
ALPHA=0xFF000000
R=0x00ff0000
G=0x0000ff00
B=0x000000ff
public static int mix(int s, int d, double f)
{
if(f>=1)return d;
if(f<=0)return s;
int Ad=(int)(255*f);
int As=255-Ad;
return(ALPHA|((As*(s&R)+Ad*(d&R))>>8)&R|((As*(s&G)+Ad*(d&G))>>8)&G|((As*(s&B)+Ad*(d&B))>>8)&B);
}
how to do it in openGL?
right now i have it like this (greyscale coloring)
float intsenity = getIntensity() + 2048;
intensity / = 4095;
…
…
…
glColor3f( intensity, intensity, intensity );
but intensity should then be replaced by other values.
[quote]how to do it in openGL?
[/quote]
I think you are talking about different matters here.
mabraham probably means this effect:
http://nehe.gamedev.net/data/lessons/lesson05.jpg
which is caused by the eges of the triangle having different color values and opengl automatically generates the desired gradient
See the Nehe Tutorials for more information
If I understand you correctly, you want something different: a colormapping to an intensity value you get (e.g. for height map colorizing or infrared images). The code of Hansdampf is the way to go even if it is quite abstract due to the source and destination colors being given as int values s and d with f being the ratio between source (0) and destination (1).
the basic idea would be to divide your intensity in different gradient sections with two pole-colors (source and destination). For a given intensity find out the gradient section and feed your two pole colors to the mix() function with f being the ratio between the two pole intensities:
example:
-2048: 00ff0000 (red)
-1024: 0000ffff (turquoise)
0: 0000000 (black)
1023: 00ffff00 (yellow)
2047: 0000ff00 (green)
intensity: 344
this results in gradient section black (intensity 0) to yellow (intensity 1023). 344 is about a third on the way between 0 and 1023 so the mix factor f is 0.34.
Now you can feed the mix function with s=0x0000000, d=0x00ffff00 and f=0.34 and get your desired blended color.
Just play arround with the different pole colors to get the best gradient for your needs.
- Create a 1d texture (eg. 64x1) and paint your colour gradient on it.
- Load and bind your texture
- use glTexCoord1f( intensity ); for every vertex
- Be happy, you’ll get per-pixel interpolation without any extra effort. ;D
that’s excaclty what i wanted thanks:)