PDA

View Full Version : What is best way to do colormap and brightness/pixel accumulation/decay?



Elan Hickler
03-08-2017, 06:40 AM
What is the best way to apply a colormap and do brightness/pixel accumulation/decay?

I'm working on an application that will rely on translating pixel brightness to color. I created a function with a hardcoded index size to demonstrate what I mean

see results here: i .imgur .com/OGXaXvS.png (forum doesn't let me post links)


vec3 interpolateColormap(float a)
{
a = clamp(a, 0.0, 1.0);
float index_pos = a * 9.0f;
float brightness = clamp(a, 0.0f, 1.0f);
int i0 = int(index_pos);
int i1 = min(i0 + 1, 9);

vec3 colorA = vec3(
colormap_R_color[i0],
colormap_G_color[i0],
colormap_B_color[i0]
);

vec3 colorB = vec3(
colormap_R_color[i1],
colormap_G_color[i1],
colormap_B_color[i1]
);

float interp = index_pos - i0;

float r = mix(colorA.r, colorB.r, interp);
float g = mix(colorA.g, colorB.g, interp);
float b = mix(colorA.b, colorB.b, interp);

return vec3(r,g,b);
}

In this example I create a few lines and then calculate the brightness of a given pixel in the fragment shader by calculating pixel distance from line center as well as distance to the end of the line, so I have a length-wise and width-wise brightness calculation. That gives me my brightness value. I then need to store those pixel values so that I can then translate brightness to RGB color. However, I also need to accumulate and do a decay on all pixels. So, let's say over 3 frames I draw 3 lines. The first line will be... let's say... kinda dim. Drawing a second line which overlaps the first line will cause a bright overlap area. Where the lines don't overlap, we show the color blue. Where the lines do overlap we show the color red based on mapping brightness to color. As the 3rd frame is drawn that overlap area now turns blue and the first line disappears because those pixels have decayed away.

a low res example:

[CODE]first frame: add a vertical line of brightness "4"

0 4 0
0 4 0
0 4 0

second frame: add a horizontal line of brightness "4" and decay all pixels -1

0 3 0
4 7 4
0 3 0

third frame: add diagonal line of brightness "4" (max can be 9) and decay all pixels -1

0 2 4
3 9 3
4 2 0

RGB color for any given pixel is vec3(colormap[brightness]), of course I have to use a function to interpolate between indexes.


What's the best way to implement this? I think I would not do a glClearColor and I would somehow get the fragment shader to write pixel values to a buffer, do accumulations and decays on that buffer, and finally translate that buffer to an image. Should I use a texture? Do I need multiple fragment shaders running in series? Is that even possible? It would help greatly just to explain some terms and how things work. You don't need to write code examples (unless you want to!), I am finding resources such as manuals and references which will help me write the code.