-
Notifications
You must be signed in to change notification settings - Fork 18
/
Helpers.cpp
224 lines (174 loc) · 8.35 KB
/
Helpers.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "Helpers.h"
#include "Globals.h"
using namespace DirectX;
bool Rust::MainCam::WorldToScreen(const XMFLOAT4X4 & ViewProjMatrix, const Cheat::Vector3 & worldpos, Cheat::Vector2 & screenpos, int width, int height)
{
const XMFLOAT4X4& matrix = ViewProjMatrix;
XMVECTOR trans = XMLoadFloat4(&XMFLOAT4(matrix._41, matrix._42, matrix._43, 0.0f));
XMVECTOR up = XMLoadFloat4(&XMFLOAT4(matrix._21, matrix._22, matrix._23, 0.0f));
XMVECTOR right = XMLoadFloat4(&XMFLOAT4(matrix._11, matrix._12, matrix._13, 0.0f));
XMVECTOR pos = XMLoadFloat4(&(XMFLOAT4)worldpos);
float depth = XMVectorGetX(XMVector4Dot(trans, pos)) + matrix._44;
if (depth < 0.098f)
return false;
float XXXX = XMVectorGetX(XMVector4Dot(right, pos)) + matrix._14;
float YYYY = XMVectorGetX(XMVector4Dot(up, pos)) + matrix._24;
screenpos.x = (int)(width / 2) * (1.f + XXXX / depth);
screenpos.y = (int)(height / 2) * (1.f - YYYY / depth);
return true;
}
/*
bool Rust::Helpers::WorldToScreen(const XMFLOAT4X4 & ViewProjMatrix, const Cheat::Vector3 & worldpos, Cheat::Vector2 & screenpos, int width, int height, float fovX)
{
//XMMATRIX VPM = XMMatrixTranspose(XMLoadFloat4x4(&ViewProjMatrix));
//XMFLOAT4X4 matrix; XMStoreFloat4x4(&matrix, VPM);
const XMFLOAT4X4& matrix = ViewProjMatrix;
XMVECTOR trans = XMLoadFloat4(&XMFLOAT4(matrix._41, matrix._42, matrix._43, 0.0f));
XMVECTOR up = XMLoadFloat4(&XMFLOAT4(matrix._21, matrix._22, matrix._23, 0.0f));
XMVECTOR right = XMLoadFloat4(&XMFLOAT4(matrix._11, matrix._12, matrix._13, 0.0f));
XMVECTOR pos = XMLoadFloat4(&(XMFLOAT4)worldpos);
float depth = XMVectorGetX(XMVector4Dot(trans, pos)) + matrix._44;
if (depth < 0.098f)
return false;
float XXXX = XMVectorGetX(XMVector4Dot(right, pos)) + matrix._14;
float YYYY = XMVectorGetX(XMVector4Dot(up, pos)) + matrix._24;
float fovXrad = fovX * 3.141592f / 180.f;
float fovYrad = fovXrad * height / width;
screenpos.x = (int)(width / 2) * (1.f + XXXX / depth);
screenpos.y = (int)(height / 2) * (1.f - YYYY / depth);
return true;
}
bool Rust::Helpers::WorldToScreenViewMatrix(const DirectX::XMFLOAT4X4 & ViewMatrix, const Cheat::Vector3 & worldpos, Cheat::Vector2 & screenpos, int width, int height, float fovX)
{
//XMMATRIX VPM = XMMatrixTranspose(XMLoadFloat4x4(&ViewProjMatrix));
//XMFLOAT4X4 matrix; XMStoreFloat4x4(&matrix, VPM);
const XMFLOAT4X4& matrix = ViewMatrix;
XMVECTOR trans = XMLoadFloat4(&XMFLOAT4(matrix._41, matrix._42, matrix._43, matrix._44));
XMVECTOR up = XMLoadFloat4(&XMFLOAT4(matrix._21, matrix._22, matrix._23, matrix._24));
XMVECTOR right = XMLoadFloat4(&XMFLOAT4(matrix._11, matrix._12, matrix._13, matrix._14));
XMVECTOR pos = XMLoadFloat4(&(XMFLOAT4)worldpos);
float depth = XMVectorGetX(XMVector4Dot(trans, pos));
if (depth < 0.098f)
return false;
float XXXX = XMVectorGetX(XMVector4Dot(right, pos));
float YYYY = XMVectorGetX(XMVector4Dot(up, pos));
float fovXrad = fovX * 3.141592f / 180.f;
float fovYrad = fovXrad * height / width;
screenpos.x = (int)(width / 2) * (1.f + XXXX / fovXrad / depth);
screenpos.y = (int)(height / 2) * (1.f - YYYY / fovYrad / depth);
return true;
return false;
}
bool Rust::Helpers::WorldToScreenTest(const DirectX::XMFLOAT4X4 & worldtoClipSpace, const Cheat::Vector3 & worldpos, Cheat::Vector2 & screenpos, int width, int height)
{
XMVECTOR worldPOS = XMLoadFloat4(&(XMFLOAT4)worldpos);
XMMATRIX viewproj = XMLoadFloat4x4(&viewProj);
XMVECTOR screenVec = DirectX::XMVector3Project(worldPOS, 0, 0, width, height,
0.1f, 3000.f, DirectX::XMMatrixIdentity(), viewproj, DirectX::XMMatrixIdentity());
if (DirectX::XMVectorGetZ(screenVec) < 1.f)
return false;
DirectX::XMStoreFloat2((XMFLOAT2*)(&screenpos), screenVec);
return true;
}
*/
Cheat::Vector3 Rust::MainCam::GetPosition(uint64_t pTransform)
{
__m128 result;
const __m128 mulVec0 = { -2.000, 2.000, -2.000, 0.000 };
const __m128 mulVec1 = { 2.000, -2.000, -2.000, 0.000 };
const __m128 mulVec2 = { -2.000, -2.000, 2.000, 0.000 };
TransformAccessReadOnly pTransformAccessReadOnly = Rust::Globals::hack_data.RustMemory->Read<TransformAccessReadOnly>(pTransform + 0x38);
unsigned int index = Rust::Globals::hack_data.RustMemory->Read<unsigned int>(pTransform + 0x40);
TransformData transformData = Rust::Globals::hack_data.RustMemory->Read<TransformData>(pTransformAccessReadOnly.pTransformData + 0x18);
SIZE_T sizeMatriciesBuf = sizeof(Matrix34) * index + sizeof(Matrix34);
SIZE_T sizeIndicesBuf = sizeof(int) * index + sizeof(int);
// Allocate memory for storing large amounts of data (matricies and indicies)
PVOID pMatriciesBuf = malloc(sizeMatriciesBuf);
PVOID pIndicesBuf = malloc(sizeIndicesBuf);
if (pMatriciesBuf && pIndicesBuf)
{
// Read Matricies array into the buffer
Rust::Globals::hack_data.RustMemory->ReadRaw(pMatriciesBuf, (void*)transformData.pTransformArray, sizeMatriciesBuf);
// Read Indices array into the buffer
Rust::Globals::hack_data.RustMemory->ReadRaw(pIndicesBuf, (void*)transformData.pTransformIndices, sizeIndicesBuf);
result = *(__m128*)((ULONGLONG)pMatriciesBuf + 0x30 * index);
int transformIndex = *(int*)((ULONGLONG)pIndicesBuf + 0x4 * index);
while (transformIndex >= 0)
{
Matrix34 matrix34 = *(Matrix34*)((ULONGLONG)pMatriciesBuf + 0x30 * transformIndex);
__m128 xxxx = _mm_castsi128_ps(_mm_shuffle_epi32(*(__m128i*)(&matrix34.vec1), 0x00)); // xxxx
__m128 yyyy = _mm_castsi128_ps(_mm_shuffle_epi32(*(__m128i*)(&matrix34.vec1), 0x55)); // yyyy
__m128 zwxy = _mm_castsi128_ps(_mm_shuffle_epi32(*(__m128i*)(&matrix34.vec1), 0x8E)); // zwxy
__m128 wzyw = _mm_castsi128_ps(_mm_shuffle_epi32(*(__m128i*)(&matrix34.vec1), 0xDB)); // wzyw
__m128 zzzz = _mm_castsi128_ps(_mm_shuffle_epi32(*(__m128i*)(&matrix34.vec1), 0xAA)); // zzzz
__m128 yxwy = _mm_castsi128_ps(_mm_shuffle_epi32(*(__m128i*)(&matrix34.vec1), 0x71)); // yxwy
__m128 tmp7 = _mm_mul_ps(*(__m128*)(&matrix34.vec2), result);
result = _mm_add_ps(
_mm_add_ps(
_mm_add_ps(
_mm_mul_ps(
_mm_sub_ps(
_mm_mul_ps(_mm_mul_ps(xxxx, mulVec1), zwxy),
_mm_mul_ps(_mm_mul_ps(yyyy, mulVec2), wzyw)),
_mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(tmp7), 0xAA))),
_mm_mul_ps(
_mm_sub_ps(
_mm_mul_ps(_mm_mul_ps(zzzz, mulVec2), wzyw),
_mm_mul_ps(_mm_mul_ps(xxxx, mulVec0), yxwy)),
_mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(tmp7), 0x55)))),
_mm_add_ps(
_mm_mul_ps(
_mm_sub_ps(
_mm_mul_ps(_mm_mul_ps(yyyy, mulVec0), yxwy),
_mm_mul_ps(_mm_mul_ps(zzzz, mulVec1), zwxy)),
_mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(tmp7), 0x00))),
tmp7)), *(__m128*)(&matrix34.vec0));
transformIndex = *(int*)((ULONGLONG)pIndicesBuf + 0x4 * transformIndex);
}
free(pMatriciesBuf);
free(pIndicesBuf);
}
return Cheat::Vector3(result.m128_f32[0], result.m128_f32[1], result.m128_f32[2]);
}
DirectX::XMFLOAT4X4 Rust::MainCam::GetViweMatrix()
{
XMFLOAT4X4 ViewMatrix;
Globals::hack_data.RustMemory->ReadRaw(&ViewMatrix, (void*)(Globals::hack_data.MainCam.pOwnClassObject + 0xC0), sizeof(ViewMatrix));
XMStoreFloat4x4(&ViewMatrix, XMMatrixTranspose(XMLoadFloat4x4(&ViewMatrix)));
return ViewMatrix;
}
DirectX::XMFLOAT4X4 Rust::MainCam::GetViewProjClipMatrix()
{
XMFLOAT4X4 ViewProjClipMatrix;
Globals::hack_data.RustMemory->ReadRaw(&ViewProjClipMatrix, (void*)(Globals::hack_data.MainCam.pOwnClassObject + 0xC0), sizeof(ViewProjClipMatrix));
XMStoreFloat4x4(&ViewProjClipMatrix, XMMatrixTranspose(XMLoadFloat4x4(&ViewProjClipMatrix)));
return ViewProjClipMatrix;
}
float Rust::MainCam::GetFov()
{
return Globals::hack_data.RustMemory->Read<float>(Globals::hack_data.MainCam.pOwnClassObject + 0x140);
}
template <typename T>
T SafeRead(T* Data)
{
if (Data != nullptr)
return *Data;
}
template <typename T>
bool SafeReadToBuffer(T* Data, T* Buffer, size_t Size)
{
if (Data != nullptr && Buffer != nullptr && Size != 0)
{
memcpy(Buffer, Data, Size);
return true;
}
return false;
}
/*
D3DXVECTOR3 GetBoneByID(BaseEntity* Entity, int Bone)
{
auto BoneInfo = Entity->ModelState->SkinnedMultiMesh->BoneDictionary->BoneInfo;
auto BoneValue = *(CBoneValue**)((uintptr_t)BoneInfo + 0x30 + ((Bone - 1) * 0x18));
return (GetPosition(BoneValue->Transform));
}
*/