136 lines
2.3 KiB
C++
Executable File
136 lines
2.3 KiB
C++
Executable File
//
|
|
// Math.hpp
|
|
// amoeba
|
|
//
|
|
// Created by Timothy Prepscius on 12/30/16.
|
|
// Copyright © 2016 Timothy Prepscius. All rights reserved.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include "Vector2.inl"
|
|
#include "Vector3.inl"
|
|
#include "Vector4.inl"
|
|
|
|
namespace tjp {
|
|
namespace core {
|
|
namespace math {
|
|
|
|
constexpr int float_2_bits = 12;
|
|
constexpr int float_3_bits = 8;
|
|
constexpr int float_4_bits = 6;
|
|
|
|
const float float_2_max = 1 << float_2_bits;
|
|
const Vector2f float_2_decoder = { 1, (1<<float_2_bits) };
|
|
const Vector2f float_2_encoder = 1/float_2_decoder;
|
|
|
|
Vector2f unpack2u(r32 v)
|
|
{
|
|
auto a = float_2_decoder * v;
|
|
auto b = fract(a);
|
|
auto c = trunc(b * float_2_max) / float_2_max;
|
|
return c;
|
|
}
|
|
|
|
r32 pack2u (Vector2f v)
|
|
{
|
|
auto a = trunc(v * float_2_max);
|
|
auto b = a / float_2_max;
|
|
auto c = dot(b, float_2_encoder);
|
|
|
|
return c;
|
|
}
|
|
|
|
Vector2f unpack2s(r32 v)
|
|
{
|
|
return 2.0f * unpack2u(v) - Vector2f { 1, 1 };
|
|
}
|
|
|
|
r32 pack2s (Vector2f v)
|
|
{
|
|
return pack2u(0.5f * (v + Vector2f { 1, 1 }));
|
|
}
|
|
|
|
const float float_4_max = 1 << float_4_bits;
|
|
|
|
const Vector4f float_4_decoder = {
|
|
1,
|
|
(1<<float_4_bits),
|
|
(1<<(2*float_4_bits)),
|
|
(1<<(3*float_4_bits))
|
|
};
|
|
|
|
const Vector4f float_4_encoder = 1/float_4_decoder;
|
|
|
|
Vector4f unpack4u(r32 v)
|
|
{
|
|
auto a = float_4_decoder * v;
|
|
auto b = fract(a);
|
|
auto c = trunc(b * float_4_max);
|
|
auto d = c / float_4_max;
|
|
return d;
|
|
}
|
|
|
|
r32 pack4u (Vector4f v)
|
|
{
|
|
auto a = trunc(v * float_4_max);
|
|
auto b = a / float_4_max;
|
|
auto c = dot(b, float_4_encoder);
|
|
|
|
return c;
|
|
}
|
|
|
|
Vector4f unpack4s(r32 v)
|
|
{
|
|
return 2.0f * unpack4u(v) - Vector4f { 1, 1, 1, 1 };
|
|
}
|
|
|
|
r32 pack4s (Vector4f v)
|
|
{
|
|
return pack4u(0.5f * (v + Vector4f { 1, 1, 1, 1 }));
|
|
}
|
|
|
|
|
|
const float float_3_max = 1 << float_3_bits;
|
|
|
|
const Vector3f float_3_decoder = {
|
|
1,
|
|
(1<<float_3_bits),
|
|
(1<<(2*float_3_bits))
|
|
} ;
|
|
|
|
const Vector3f float_3_encoder = 1/float_3_decoder;
|
|
|
|
Vector3f unpack3u(r32 v)
|
|
{
|
|
auto a = float_3_decoder * v;
|
|
auto b = fract(a);
|
|
auto c = trunc(b * float_3_max);
|
|
auto d = c / float_3_max;
|
|
return d;
|
|
}
|
|
|
|
r32 pack3u (Vector3f v)
|
|
{
|
|
auto a = trunc(v * float_3_max);
|
|
auto b = a / float_3_max;
|
|
auto c = dot(b, float_3_encoder);
|
|
|
|
return c;
|
|
}
|
|
|
|
Vector3f unpack3s(r32 v)
|
|
{
|
|
return 2.0f * unpack3u(v) - Vector3f { 1, 1, 1 };
|
|
}
|
|
|
|
r32 pack3s (Vector3f v)
|
|
{
|
|
return pack3u(0.5f * (v + Vector3f { 1, 1, 1 }));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace
|
|
} // namespace
|
|
|