Skip to content

Commit

Permalink
Fixed NifSkope unable to save a mesh if it has bhkCompressedMeshShape…
Browse files Browse the repository at this point in the history
…Data blocks

Fixes hexabits#67 , niftools#242

Bonus: refactored the NifStream code so this kind of bugs would happen less likely in the future (added asserts in strategic places, moved boilerplate code to macros or functions, and so on). This refactor also fixes a few other potential bugs on reading and writing meshes.
  • Loading branch information
gavrant committed Jul 23, 2024
1 parent ccdf0d3 commit d35d9c8
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 506 deletions.
19 changes: 19 additions & 0 deletions lib/half.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,23 @@ half_sub( uint16_t ha, uint16_t hb )
return half_add( ha, hb ^ 0x8000 );
}


// Added for NifSkope - BEGIN

inline float halfToFloat( uint16_t u )
{
union { float f; uint32_t i; } hv;
hv.i = half_to_float( u );
return hv.f;
}

inline uint16_t floatToHalf( float f )
{
union { float f; uint32_t i; } hv;
hv.f = f;
return half_from_float( hv.i );
}

// Added for NifSkope - END

#endif /* HALF_H */
17 changes: 4 additions & 13 deletions src/io/MeshFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,12 @@ quint32 MeshFile::readMesh()

for ( int i = 0; i < coords[0].count(); i++ ) {
uint16_t u, v;
union { float f; uint32_t i; } uu, vu;

in >> u;
in >> v;

uu.i = half_to_float(u);
vu.i = half_to_float(v);

Vector2 coord;
coord[0] = uu.f;
coord[1] = vu.f;
coord[0] = halfToFloat( u );
coord[1] = halfToFloat( v );

coords[0][i] = coord;
}
Expand All @@ -154,16 +149,12 @@ quint32 MeshFile::readMesh()

for ( int i = 0; i < coords[1].count(); i++ ) {
uint16_t u, v;
union { float f; uint32_t i; } uu, vu;

in >> u;
in >> v;
uu.i = half_to_float(u);
vu.i = half_to_float(v);

Vector2 coord;
coord[0] = uu.f;
coord[1] = vu.f;
coord[0] = halfToFloat( u );
coord[1] = halfToFloat( v );
coords[1][i] = coord;
}

Expand Down
Loading

0 comments on commit d35d9c8

Please sign in to comment.