3d游戏引擎的math矩阵实现

1.Math.h

#pragma once

#include "CommonHeaders.h"

#include "MathTypes.h"

#include "PrimitiveTypes.h"

namespace primal::math

{

template<typename T>

constexpr T clamp(T value, T min, T max)

{

return (value < min) ? min : (value > max) ? max : value;

}

template<u32 bits>

constexpr u32 pack_unit_float(f32 f)

{

static_assert(bits <= sizeof(u32) * 8);

assert(f >= 0.f && f <= 1.f);

constexpr f32 intervals{ (f32)((1ui32 << bits) - 1) };

return (u32)(intervals * f + 0.5f);

}

template<u32 bits>

constexpr f32 unpack_to_unit_float(u32 i)

{

static_assert(bits <= sizeof(u32) * 8);

assert(i < (1ui32)<<bits);

constexpr f32 intervals{ (u32)((1ui32 << bits) - 1) };

return (f32)i / intervals;

}

template<u32 bits>

constexpr u32 pack_float(f32 f, f32 min, f32 max)

{

assert(min < max);

assert(f <= max && f >= min);

const f32 distance{(f - min) / (max - min)};

return pack_unit_float<bits>(distance);

}

template<u32 bits>

constexpr f32 unpack_to_float(u32 i, f32 min, f32 max)

{

assert(min < max);

return unpack_to_unit_float<bits>(i) * (max - min) + min;

}

}

MathTypes.h

#pragma once

#include "CommonHeaders.h"

#include "DirectXMath.h"

namespace primal::math

{

constexpr float pi = 3.14f;

//constexpr float pi = 2.f * pi;

constexpr float epsilon = 1e-5f;

#if defined(_WIN64)

/*

typedef const DirectX& XMFLOAT2;

typedef const DirectX& XMFLOAT2A;

typedef const DirectX& XMFLOAT3;

typedef const DirectX& XMFLOAT3A;

typedef const DirectX& XMFLOAT4;

typedef const DirectX& XMFLOAT4A;

typedef const DirectX& XMUINT2;

typedef const DirectX& XMUINT3;

typedef const DirectX& XMUINT4;

typedef const DirectX& XMINT2;

typedef const DirectX& XMINT3;

typedef const DirectX& XMINT4;

typedef const DirectX& XMFLOAT3X3;

typedef const DirectX& XMFLOAT4X4;

typedef const DirectX& XMFLOAT4X4A;

*/

using v2 = DirectX::XMFLOAT2;

using v2a = DirectX::XMFLOAT2A;

using v3 = DirectX::XMFLOAT3;

using v3a = DirectX::XMFLOAT3A;

using v4 = DirectX::XMFLOAT4;

using v4a = DirectX::XMFLOAT4A;

using u32v2 = DirectX::XMUINT2;

using u32v3 = DirectX::XMUINT3;

using u32v4 = DirectX::XMUINT4;

using s32v2 = DirectX::XMINT2;

using s32v3 = DirectX::XMINT3;

using s32v4 = DirectX::XMINT4;

using m3x3 = DirectX::XMFLOAT3X3;

using m4x4 = DirectX::XMFLOAT4X4;

using m4x4a = DirectX::XMFLOAT4X4A;

#endif

};

相关推荐
KhalilRuan5 小时前
Unity性能优化
unity·性能优化·游戏引擎
AI_yangxi5 小时前
靠谱的短视频矩阵系统
大数据·人工智能·矩阵
浪里镖客5 小时前
位姿转换矩阵写法-个人习惯(计算机理解其实是相反的)
线性代数·算法·矩阵
张宇Joaquin12 小时前
鲲鹏计算平台矩阵乘算子SGEMM优化实践——下篇
线性代数·矩阵
船厂电气自动化ai大模型15 小时前
AI大模型与数学第64课:矩阵×向量乘法(神经网络矩阵运算底层)
数据结构·深度学习·线性代数·机器学习·推荐算法
guanguan0_016 小时前
用 AI 做技术方案评审:输入 3 个方案,输出对比矩阵 + 推荐理由
javascript·人工智能·矩阵·ai编程
五仁烧饼17 小时前
Unity性能优化系列内存篇 - 移动端内存优化
unity·性能优化·游戏引擎
AI_yangxi2 天前
短视频矩阵系统选哪家
大数据·人工智能·矩阵
WiChP2 天前
【V0.1B15】从零开始的2D游戏引擎开发之路
游戏引擎
远航计算机3 天前
豆包”精采信”模式对企业官网运营有什么影响?2026年7月信源重构深度解读
人工智能·矩阵·重构·aigc·媒体