Quaternion values and 2.x

From wowdev
Jump to: navigation, search

In WoW 2.0+ Blizzard are now storing rotation data in 16bit values instead of 32bit. I don't really understand why as its only a very minor saving in model sizes and adds extra overhead in processing the models. Need this structure to read the data into.

The conversion is done with these two functions:

inline float stf(unsigned short Short) {
	return (Short / float (MAX_SHORT)) - 1.0f; // (Short > 0 ? Short-32767 : Short+32767)/32767.0;
}
inline unsigned short fts(float Float) {
	return (Float + 1.0f) * float (MAX_SHORT); //  (short)(Float > 0 ? Float * 32767.0 - 32768 : Float * 32767.0 + 32768);
}

Doing it for a whole Quaternion is like this:

struct Quat16 {   
	__int16 x,y,z,w;  
}; 
using M2CompQuat = Quat16;

struct Quat32 {   
	float x,y,z,w;  
}; 

static const Quat32 Quat16ToQuat32(const Quat16 t)
{
	return Quaternion(
		float(t.x < 0? t.x + 32768 : t.x - 32767)/ 32767.0f, 
		float(t.y < 0? t.y + 32768 : t.y - 32767)/ 32767.0f,
		float(t.z < 0? t.z + 32768 : t.z - 32767)/ 32767.0f,
		float(t.w < 0? t.w + 32768 : t.w - 32767)/ 32767.0f);
}

--Koward 08:07, 26 November 2015 (UTC) Please notice that unlike the standard mathematical notations, the scalar part (w) is AFTER the vector part (x,y,z) in the Blizzard structures. Most well-known math libraries uses the standard (w, x, y, z) so be careful and switch properly.