[How] Convert Float to ByteFloat

A place for to discuss the development of our standalone NIF viewer and editor, NifSkope.
Active developers: jonwd7

[How] Convert Float to ByteFloat

Postby Anton » Sun Jan 22, 2017 11:18 am

Hello,
Help me please to understand: how Nifskope converts Float type (4 bytes) to ByteFloat type (1 byte) ?
I made few tries to make this conversion with C but failed.

I have tried: S EEE MMMM, S EE MMMMM, S EEEE MMM representations of ByteFloat but my Normals looks weird in Nifskope after import :(
where:
S - sign bit
E - exponent bits
M - mantissa bits

I noticed Nifskope understands: -1.0 as 0x00, 1.0 as 0xFF , 0.5 as 0xBF , -0.5 as 0x40 - but this must to be wrong because of sign bit ...

How you do this trick ? Why your normals looks good in Fallout 4 ?
Anton
 
Posts: 4
Joined: Sun Jul 29, 2012 7:37 am

Re: [How] Convert Float to ByteFloat

Postby jonwd7 » Sun Jan 22, 2017 10:12 pm

The conversion is arithmetic.

Code: Select all
         quint8 x, y, z;
         float xf, yf, zf;

         *dataStream >> x;
         *dataStream >> y;
         *dataStream >> z;

         xf = (double(x) / 255.0) * 2.0 - 1.0;
         yf = (double(y) / 255.0) * 2.0 - 1.0;
         zf = (double(z) / 255.0) * 2.0 - 1.0;
   
         Vector3 * v = static_cast<Vector3 *>(val.val.data);
         v->xyz[0] = xf; v->xyz[1] = yf; v->xyz[2] = zf;
jonwd7
NifTools Developer
NifTools Developer
 
Posts: 287
Joined: Thu Nov 08, 2012 3:10 pm

Re: [How] Convert Float to ByteFloat

Postby Anton » Mon Jan 23, 2017 1:21 pm

Thank you !
I don't know why but it works ! :)
(my conversion function based on IEEE 754 was much more complicated and didn't want to give me desired result)

Now normals for my imported to NIF model is the same (in OBJ file and in Nifskope).

Unfortunatly, it seems Blender 2.7 have a problem to export normals to OBJ file right way. But this is an another story ))

Thanks again for your help !
Anton
 
Posts: 4
Joined: Sun Jul 29, 2012 7:37 am


Return to NifSkope Development

Who is online

Users browsing this forum: No registered users and 1 guest