121 lines
2.3 KiB
C++
121 lines
2.3 KiB
C++
#include "Global.h"
|
|
#include "bx/constants.h"
|
|
#include "bx/math.h"
|
|
#include <cassert>
|
|
|
|
namespace
|
|
{
|
|
SharedData* SharedInstance = nullptr;
|
|
Game::GameInstance* GameInst = nullptr;
|
|
} // namespace
|
|
|
|
void Vec2::Normalize()
|
|
{
|
|
float len = bx::sqrt(X * X + Y * Y);
|
|
X /= len;
|
|
Y /= len;
|
|
}
|
|
|
|
void Vec3::Normalize()
|
|
{
|
|
float len = bx::sqrt(X * X + Y * Y + Z * Z);
|
|
X /= len;
|
|
Y /= len;
|
|
Z /= len;
|
|
}
|
|
|
|
Quat Quat::FromEuler(float x, float y, float z)
|
|
{
|
|
x *= bx::kPi / 180.0f;
|
|
y *= bx::kPi / 180.0f;
|
|
z *= bx::kPi / 180.0f;
|
|
|
|
float cX = bx::cos(x);
|
|
float cY = bx::cos(y);
|
|
float cZ = bx::cos(z);
|
|
float sX = bx::sin(x);
|
|
float sY = bx::sin(y);
|
|
float sZ = bx::sin(z);
|
|
|
|
return Quat{cX * cY * cZ - sX * sY * sZ,
|
|
sY * cX * cZ - sX * sZ * cY,
|
|
sX * sY * cZ + sZ * cX * cY,
|
|
sX * cY * cZ + sY * sZ * cX};
|
|
}
|
|
|
|
Quat Quat::FromEuler(Vec3 rot)
|
|
{
|
|
return Quat::FromEuler(rot.X, rot.Y, rot.Z);
|
|
}
|
|
|
|
void Mat4::CreateTransform(float* out, Vec3 pos, Quat rot, Vec3 scale)
|
|
{
|
|
if (out == nullptr) return;
|
|
float rMat[16]{0};
|
|
float tMat[16]{0};
|
|
float sMat[16]{0};
|
|
bx::mtxFromQuaternion(rMat, bx::Quaternion{rot.X, rot.Y, rot.Z, rot.W});
|
|
bx::mtxTranslate(tMat, pos.X, pos.Y, pos.Z);
|
|
bx::mtxScale(sMat, scale.X, scale.Y, scale.Z);
|
|
float buf[16]{0};
|
|
bx::mtxMul(buf, rMat, sMat);
|
|
bx::mtxMul(out, buf, tMat);
|
|
}
|
|
|
|
void Mat4::TranslateLocal(Vec3 offset)
|
|
{
|
|
M[12] += offset.X;
|
|
M[13] += offset.Y;
|
|
M[14] += offset.Z;
|
|
}
|
|
|
|
void Mat4::Rotate(Vec3 rotation)
|
|
{
|
|
float rot[16]{0};
|
|
bx::mtxRotateXYZ(rot, rotation.X, rotation.Y, rotation.Z);
|
|
float temp[16]{0};
|
|
bx::mtxMul(temp, M, rot);
|
|
for (int32_t i = 0; i < 16; ++i)
|
|
M[i] = temp[i];
|
|
}
|
|
|
|
Vec3 Mat4::Right()
|
|
{
|
|
return {M[0], M[1], M[2]};
|
|
}
|
|
|
|
Vec3 Mat4::Up()
|
|
{
|
|
return {M[4], M[5], M[6]};
|
|
}
|
|
|
|
Vec3 Mat4::Forward()
|
|
{
|
|
return {M[8], M[9], M[10]};
|
|
}
|
|
|
|
namespace Game
|
|
{
|
|
SharedData& GetShared()
|
|
{
|
|
assert(SharedInstance != nullptr);
|
|
return *SharedInstance;
|
|
}
|
|
|
|
void SetShared(SharedData& instance)
|
|
{
|
|
SharedInstance = &instance;
|
|
}
|
|
|
|
GameInstance& GetInstance()
|
|
{
|
|
assert(GameInst != nullptr);
|
|
return *GameInst;
|
|
}
|
|
|
|
void SetInstance(Game::GameInstance& instance)
|
|
{
|
|
GameInst = &instance;
|
|
}
|
|
} // namespace Game
|