Convert Float to vec4

In my glsl frag shader I calculate a value which is a float. I want to write it to the RGB channels of the frame buffer…

float myFloat = Calculate();
vec4 fragColor = Format(myFloat);
gl_FragData = fragColor;

fragColor.r will have 1 byte of the float, .g will have 1 byte, etc.

I have tried dinking around with converting the float to fixed point but I am not sure I am doing this right.

Has anyone tried this yet? Any pointers?

I need to do this because my rendering output is pass to a post processing app via the DVI output, and it will reassemble the rgb into the calculated float on it’s end.


This should work:

vec4 fragColor = vec4(myFloat);

Be aware that you’ll also have the alpha value of fragColor set to myFloat, which might lead to unexpected results. You might therefore also want to add

fragColor.a = 1.0;

Hope this helps,


What is the exact algorithm to reassemble RGB to a float? DVI usually transmits 3*8 bits per pixel so you only get 24 bits, while GLSL floats on modern GPUs are 32 bit wide. This means you have to sacrifice precision and/or range.

i don’t understand if you can use the gl_FragData’s alpha channel or not… if you can use it you can try to encode in some integer encoding format such as RGBE, but you have to sacrifice precision or range (as Xmas said…)

it depends on the accuracy needed by your application