Hi, i have my normals data stored in floats, and the fileformat of the game needs to have it in dec3n, i was searching a lot about how to convert that floats to a dec3n value, but i only found the inverse thing extract float values from a dec3n value.

Applying (2c+1)/(2^10-1) where c i -0.523 the result is -4.496578 so if i write this number into binary i have the 32 bits that represents this float and now, do i have to pick the first 10 bits of this float and put them into my future dec3n value?

Whoops, looks like I stated the formula for getting the value back out, sorry.

You need to invert that formula to convert the float into an integer. I would do some math but I've had a few beers and I'm about to cook some food.

You don't want to look at the bit representation of the float at all, you need to map it into the range 0 - 1023 (for the 10 bit fields), so you want to add 1, divide by 2, and multiply by 1023, then cast to an unigned int and shift it into the correct place, I think.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

The above post I made is correct. You need to map a float in the range -1.0 to 1.0 into the range 0 - 1023.

So you do this:

float_val += 1.0; // map into range 0.0 - 2.0

float_val /= 2.0; // map into range 0.0 - 1.0

float_val *= 1023; // map into range 0.0 - 1023.0

then cast to an int (EDIT: You probably want to add on 0.5 before the cast for rounding purposes), shift into the required position, and you are done. (logical or all the other components into the 32 bit int as well).

For unsigned floats in range 0.0 - 1.0, you don't need to add 1 or divide by 2, just multiply by 1023.

For the w value, multiply by 3 (= 2*2 - 1) instead.

Edited by Paradigm Shifter, 26 April 2013 - 02:52 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley