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

};

相关推荐
技术小甜甜4 小时前
[Godot] 解决导出APK安装失败的常见问题:深入分析与调试方法
游戏引擎·godot·游戏开发
式5165 小时前
线性代数(六)列空间和零空间
线性代数
式5166 小时前
线性代数(九)线性相关性、基与维数
线性代数·算法·机器学习
技术小甜甜8 小时前
[Godot][入门] 安装与版本选择:3.x 还是 4.x?(按项目类型一键决策)
游戏引擎·godot·游戏开发·2d
陈言必行8 小时前
Unity 之 物理引擎中三种刚体力施加方式详解
unity·游戏引擎
foreveryao1238 小时前
Unity渲染流程(底层逻辑)
unity·游戏引擎·图形渲染
好风凭借力,送我上青云9 小时前
Pytorch经典卷积神经网络-----激活函数篇
人工智能·pytorch·深度学习·算法·矩阵·cnn
small-pudding9 小时前
Unity中的PBR(基于物理的渲染)
unity·游戏引擎
式51612 小时前
线性代数(五)向量空间与子空间
人工智能·线性代数·机器学习
式51612 小时前
线性代数(七)主变量与特解
线性代数·算法