# OpenGL GLSL texture sampler precision

## Recommended Posts

Hi folks,

I'm currently working on a sample/learning project where I have some colourful dots moving around. And I'm trying to achieve that these dots are leaving trails that fade out after some time. For this purpose I render my frames to two textures (alternating each frame) and draw the last frame as background of the next frame. The sahder for this looks like this:

#version 420 core

in vec2 uv;
out vec4 out_Color;
uniform highp sampler2D texture;

void main()
{
highp vec3 col = texture2D(texture, uv).rgb * 0.97;
out_Color = vec4(col, 1.0);
}

This approach seems to be basically working but with the flaw that the color never converges to black but stops fading out at some reasonably dark color. It seems hard to believe to me that the precision of this calculation should be that low that it calculates x * 0.97 = x where x is nothing near to epsilon but let's say 0.05. But this would be a good explanation for the behaviou I observe. Or am I missing something else here?

It would be really great to get some advice on this or any hints how I could fix this issue.

##### Share on other sites

Why are you not fading out using alpha?

##### Share on other sites
Posted (edited)

Hi Irusan,
I agree, using alpha for this would be the obvious approach at hand for things like this. I tried it and it seems to suffer from the same issue in my use case. It seems that I have to be aware of the fact that the pixel data is stored as RGBA32 and not as floats. So if I darken a color value of e.g 16 by 2% to 15.68 it will remain a 16 when stored again as a byte. At least I got it working when I defined a minimum delta according to this consideration:

#version 420 core

in vec2 uv;
out vec4 out_Color;

uniform float Tail; // interval [0,1]
uniform highp sampler2D texture;

void main()
{
highp vec3 col = texture2D(texture, uv).rgb;
highp vec3 darken = col * Tail;
darken.r = max(darken.r, 1.0 / 256.0);
darken.g = max(darken.g, 1.0 / 256.0);
darken.b = max(darken.b, 1.0 / 256.0);
col -= darken;
out_Color = vec4(col, 1.0);
}

Edited by Joe Malik
fixed some typos

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 17
• 24
• 13
• 20