1

I try to create simple computer tomography viewer with OpenGL, data is 1D float array that represents single slice.

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,  width, height, 0, GL_LUMINANCE, GL_FLOAT, data);

Question

How to modify the colors/float values in fragment shader for the windowing purposes So displaying all values above some treshold as white and all below as black and the rest as greys between

my fragment shader

out vec4 FragColor;
  
in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
{
    FragColor = texture(ourTexture, TexCoord);
}

All works well yet I have some problems

Image I get now just for reference

Jakub Mitura
  • 159
  • 1
  • 14
  • 1
    You could use the `smoothstep` function: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/smoothstep.xhtml – dms Jul 16 '21 at 06:01

1 Answers1

3

well simply by combining thresholding with linear interpolation something like this:

out vec4 FragColor;
  
in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main()
    {
    vec4 col=texture(ourTexture, TexCoord);   // input color
    float t0=0.25,t1=0.75;                    // thresholds
    float t=length(col.rgb)/1.73205081;       // /sqrt(3) ... parameter is intensity of color
    t=(t-t0)/(t1-t0);                         // dynamic range <t0,t1> -> <0,1>
    t=max(t,0.0);                             // cut off unused range
    t=min(t,1.0);
    FragColor = vec4(t,t,t,col.a);
    }

Beware I did not test it as I wrote this directly in answer editor so there might be some hidden typos and too lazy to create MCVE from scratch...

Spektre
  • 49,595
  • 11
  • 110
  • 380