• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

### #ActualChris_F

Posted 17 January 2013 - 04:13 PM

So I've looked around, and there is a lot of info to be found on this subject, but it is only for encoding floats in the range [0, 1], and I want to encode floats of arbitrary magnitude. Can you tell me if my method is sound?

On the CPU:

glm::ivec4 FloatToRGBA(float input)
{
int exponent;
float significand = frexp(input, &exponent);
int sig_int = glm::round(significand * 16777215.0f);
glm::vec4 output;
output.x = exponent + 128;
output.y = sig_int & 0x000000FF;
output.z = (sig_int & 0x0000FF00) >> 8;
output.w = (sig_int & 0x00FF0000) >> 16;
return output;
}


On the GPU:

float RGBAtoFloat(float4 input)
{
float significand = dot(input.yzw, float3(16711680.0f, 65280.0f, 255.0f)) / 16777215.0f;
return ldexp(significand, input.x * 255.0f - 128.0f);
}


### #1Chris_F

Posted 17 January 2013 - 04:10 PM

So I've looked around, and there is a lot of info to be found on this subject, but it is only for encoding floats in the range [0, 1], and I want to encode floats of arbitrary magnitude. Can you tell me if my method is sound?

glm::ivec4 FloatToRGBA(float input)
{
int exponent;
float significand = frexp(input, &exponent);
int sig_int = glm::round(significand * 16777215.0f);
glm::vec4 output;
output.x = exponent + 128;
output.y = sig_int & 0x000000FF;
output.z = (sig_int & 0x0000FF00) >> 8;
output.w = (sig_int & 0x00FF0000) >> 16;
return output;
}


float RGBAtoFloat(float4 input)
{
float significand = dot(input.yzw, float3(16711680.0f, 65280.0f, 255.0f)) / 16777215.0f;
return ldexp(significand, input.x * 255.0f - 128.0f);
}


PARTNERS