-
Notifications
You must be signed in to change notification settings - Fork 7
/
CMaths.h
106 lines (84 loc) · 2.79 KB
/
CMaths.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <d3dx9math.h>
#define M_RADPI 57.295779513082f
#define M_PI_F ((float)(M_PI)) // Shouldn't collide with anything.
#define DEG2RAD( x ) ( (float)(x) * (float)(M_PI_F / 180.f) )
class CMaths
{
private:
int ScreenW = 0;
int ScreenH = 0;
public:
VOID GetScreenSize(int& Screen_W, int& Screen_H)
{
Screen_W = ScreenW;
Screen_H = ScreenH;
}
VOID UpdateScreenResolution(int ScreenHeaight, int ScreenWidth)
{
ScreenH = ScreenHeaight;
ScreenW = ScreenWidth;
}
D3DXMATRIX ToMatrix(FRotator rot, Vector origin = Vector(0, 0, 0))
{
float radPitch = static_cast<float>(rot.Pitch * M_PI / 180.f);
float radYaw = static_cast<float>(rot.Yaw * M_PI / 180.f);
float radRoll = static_cast<float>(rot.Roll * M_PI / 180.f);
float SP = static_cast<float>(sin(radPitch));
float CP = static_cast<float>(cos(radPitch));
float SY = static_cast<float>(sin(radYaw));
float CY = static_cast<float>(cos(radYaw));
float SR = static_cast<float>(sin(radRoll));
float CR = static_cast<float>(cos(radRoll));
D3DXMATRIX matrix;
{
matrix.m[0][0] = CP * CY;
matrix.m[0][1] = CP * SY;
matrix.m[0][2] = SP;
matrix.m[0][3] = 0.f;
}
{
matrix.m[1][0] = SR * SP * CY - CR * SY;
matrix.m[1][1] = SR * SP * SY + CR * CY;
matrix.m[1][2] = -SR * CP;
matrix.m[1][3] = 0.f;
}
{
matrix.m[2][0] = -(CR * SP * CY + SR * SY);
matrix.m[2][1] = CY * SR - CR * SP * SY;
matrix.m[2][2] = CR * CP;
matrix.m[2][3] = 0.f;
}
{
matrix.m[3][0] = origin.x;
matrix.m[3][1] = origin.y;
matrix.m[3][2] = origin.z;
matrix.m[3][3] = 1.f;
}
return matrix;
}
bool WorldToScreen(Vector WorldLocation, FCameraCacheEntry CameraCacheL, Vector& out) //, int Swidth, int Sheight
{
auto POV = CameraCacheL.POV;
FRotator Rotation = POV.Rotation; // FRotator
D3DXMATRIX tempMatrix = ToMatrix(Rotation); // Matrix
Vector vAxisX = Vector(tempMatrix.m[0][0], tempMatrix.m[0][1], tempMatrix.m[0][2]),
vAxisY = Vector(tempMatrix.m[1][0], tempMatrix.m[1][1], tempMatrix.m[1][2]),
vAxisZ = Vector(tempMatrix.m[2][0], tempMatrix.m[2][1], tempMatrix.m[2][2]);
Vector vDelta = WorldLocation - POV.Location;
Vector vTransformed = Vector(vDelta.Dot(vAxisY), vDelta.Dot(vAxisZ), vDelta.Dot(vAxisX));
if (vTransformed.z < 1.f)
vTransformed.z = 1.f;
float FovAngle = POV.FOV;
float ScreenCenterX = ScreenW / 2;
float ScreenCenterY = ScreenH / 2;
out.x = ScreenCenterX + vTransformed.x * (ScreenCenterX / static_cast<float>(tan(FovAngle * static_cast<float>(M_PI) / 360.f))) /
vTransformed.z;
out.y = ScreenCenterY - vTransformed.y * (ScreenCenterX / static_cast<float>(tan(FovAngle * static_cast<float>(M_PI) / 360.f))) /
vTransformed.z;
out.x = out.x;
out.y = out.y;
if (out.x > ScreenW || out.y > ScreenH) return false;
return true;
}
};