Jump to content
  • Advertisement
Sign in to follow this  
arkangel2803

Float to Float4, Float4 to Float Problems

This topic is 3316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all Im testing this code i found in this forum to encode some floats to a single float value and viceversa.
float4 FloatToFloat4( float rawValue )
{	
	float4 packFactors = float4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
	float4 bitMask = float4(1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);

	float4 packedValue = float4(frac(packFactors*rawValue));
	packedValue -= packedValue.xxyz * bitMask;
	return packedValue;
}

float Float4ToFloat(float4 encodedValue)
{
	float4 unpackFactors = float4( 1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0 );

	return dot(encodedValue,unpackFactors);
}
I tryed to encode float4(150.0f,100.0f,50.0f,25.0f) and the results when i try to decode the single float is float4(0.0f,159.0f,100.0f,204.0f) Ofcourse something wrong is happening, but i dont know what, even this, i dont know if i can use this code to encode float4 -> float -> float4. Im trying to pack values from material specular (power, exponent, fresnel_min, fresnel_max) to a 32bits float value, and then unpack it again on a post process. Knows you a better way to achive this ? or i need to pass each value on a color channel in a Render Target ? Thanks for your time and help :) LLORENS

Share this post


Link to post
Share on other sites
Advertisement
Your trying to pack 128bits of data into 32bits...
Looking at it, it seems this is will pack 4 8bit normalized floats.
Try numbers like 1.0 0.75 0.5 0.25.

You'll still get some errors cause a float only has 24bits of precission.

Share this post


Link to post
Share on other sites
Hi Ibasa, and thx for your answer.

I tested float4(1.0f,0.75f,0.5f,0.25f) and the result is float4(0.0f,191.0f,128.0f,191.0f);

It's strange, but i cant get it working, i dont know if precision is making these to fail or what...

Any ideas? Or any other methodology to pack some float values in a 32bits Render Target channel ?

Thx in advance :)

LLORENS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!