- WritePackN(val.w, off);
- WritePackN(val.x, off + 2);
- WritePackN(val.y, off + 4);
- WritePackN(val.z, off + 6);
+ // find the largest component
+ float largest = 0.0f;
+ int largest_index = -1;
+ for (int i = 0; i < 4; ++i) {
+ float iabs = abs(val[i]);
+ if (iabs > largest) {
+ largest = iabs;
+ largest_index = i;
+ }
+ }
+ // make sure it's positive
+ const glm::quat q(val[largest_index] < 0.0f ? -val : val);
+ // move index to the two most significant bits
+ uint64_t packed = uint64_t(largest_index) << 62;
+ // we have to map from [-0.7072,0.7072] to [-524287,524287] and move into positive range
+ constexpr float conv = 524287.0f / 0.7072f;
+ // if largest is 1, the other three are 0
+ // precision of comparison is the interval of our mapping
+ if (abs(1.0 - largest) < (1.0f / conv)) {
+ packed |= 0x7FFFF7FFFF7FFFF;
+ } else {
+ // pack the three smaller components into 20bit ints each
+ int shift = 40;
+ for (int i = 0; i < 4; ++i) {
+ if (i != largest_index) {
+ packed |= uint64_t(int(q[i] * conv) + 524287) << shift;
+ shift -= 20;
+ }
+ }
+ }
+ // and write it out
+ Write(packed, off);