float normals to dec3n

Started by
3 comments, last by Paradigm Shifter 10 years, 12 months ago

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.

How can i do this?

Maybe this link could be helpful, but i didn´r understood it:

http://www.khronos.org/registry/gles/extensions/OES/OES_vertex_type_10_10_10_2.txt

Advertisement

            GLType                     Conversion of (x, y, z)  Conversion of w
            -------                    ----------------------   ===============
            INT_10_10_10_2_OES        (2c + 1)/(2^10 - 1)       (2c + 1)/(2^2 - 1)
            UNSIGNED_10_10_10_2_OES    c / (2^10 - 1)            c / (2^2 - 1)

This is the info you need.

c is the input float value

and 2^x is pow(2, x) so 2^10 is 1024 and 2^2 is 4

You then need to pack the bits into a bitfield which you can do by shifting the numbers left the correct number of bits and logical or-ing them together to get the correct format.

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

So for instance, if my x float value is -0.523

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

Corned beef hash in the oven.

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.

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

This topic is closed to new replies.

Advertisement