• Create Account

Banner advertising on our site currently available from just \$5!

# float normals to dec3n

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1jor1980  Members   -  Reputation: 151

Like
0Likes
Like

Posted 26 April 2013 - 09:34 AM

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?

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

### #2Paradigm Shifter  Crossbones+   -  Reputation: 5527

Like
0Likes
Like

Posted 26 April 2013 - 09:48 AM

            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

### #3jor1980  Members   -  Reputation: 151

Like
0Likes
Like

Posted 26 April 2013 - 02:00 PM

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?

### #4Paradigm Shifter  Crossbones+   -  Reputation: 5527

Like
0Likes
Like

Posted 26 April 2013 - 02:08 PM

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

### #5Paradigm Shifter  Crossbones+   -  Reputation: 5527

Like
0Likes
Like

Posted 26 April 2013 - 02:42 PM

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.

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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS