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

};

相关推荐
ssl_xxy2 小时前
矩阵杂题第二弹
线性代数·矩阵
tobias.b4 小时前
高考导数专题
线性代数·高考
松树戈9 小时前
godot项目【宝石捕手】04~添加音效、记分
游戏引擎·godot
粉色大象9 小时前
考研线性代数 · 二次型核心知识点全梳理(附2009数二真题)
线性代数·考研·矩阵
知识分享小能手13 小时前
线性代数学习教程,从入门到精通,矩阵及其运算 — 完整知识点与详细推导(3)
学习·线性代数·矩阵
HyperAI超神经13 小时前
基于 Strassen 与 LCMA 低复杂度矩阵乘,腾讯 FalconGEMM 探索超越硬件峰值的矩阵乘优化
人工智能·线性代数·矩阵·智能体·推理·ai编译器
OuO-21 天前
笔试强训 Day 34:ISBN 号码、kotori 和迷宫、矩阵最长递增路径
java·算法·矩阵
ue星空1 天前
【Godot】更换编辑器外观主题
编辑器·游戏引擎·godot
fanfan_hongyun1 天前
unity 与cad 坐标系映射
unity·游戏引擎
AIVOClaw容剪1 天前
AI 成片系统搭建如何让矩阵号差异化从「同一脚本 50 个号发」变成「反推提示词每号一版」——5 模块防止同质化被判搬运 · AI 视频智能体平台
人工智能·矩阵·音视频