forked from themrdemonized/xray-monolith
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHUDCrosshair.cpp
More file actions
218 lines (180 loc) · 5.3 KB
/
HUDCrosshair.cpp
File metadata and controls
218 lines (180 loc) · 5.3 KB
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
// HUDCrosshair.cpp: êðåñòèê ïðèöåëà, îòîáðàæàþùèé òåêóùóþ äèñïåðñèþ
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HUDCrosshair.h"
#include "../xrEngine/CustomHUD.h"
#include "../xrEngine/igame_persistent.h"
#include "ui_base.h"
string32 crosshair_shader = "hud\\cursor";
string32 crosshair_texture = "ui\\cursor";
float crosshair_near_size = 1.f;
CHUDCrosshair::CHUDCrosshair()
{
strcpy(lastCrosshairShader, "");
strcpy(lastCrosshairTexture, "");
shaderWire->create("hud\\crosshair");
transform = Fmatrix().identity();
minRadius = 0.001f;
maxRadius = 0.004f;
crossColor = 0;
}
CHUDCrosshair::~CHUDCrosshair()
{
}
void CHUDCrosshair::Load()
{
minRadius = pSettings->r_float(HUD_CURSOR_SECTION, "min_radius");
maxRadius = pSettings->r_float(HUD_CURSOR_SECTION, "max_radius");
crossColor = pSettings->r_fcolor(HUD_CURSOR_SECTION, "cross_color").get();
}
void CHUDCrosshair::SetTransform(const Fmatrix& m)
{
transform.set(m);
}
void CHUDCrosshair::SetColor(u32 c)
{
crossColor = c;
}
void CHUDCrosshair::SetDispersion(float d)
{
// Stubbed out for now, as the existing code did not use the provided dispersion values
// Potential for a proper treatment later down the line
}
extern ENGINE_API BOOL g_bRendering;
static float lerp(float a, float b, float t)
{
return a * (1 - t) + b * t;
}
void CHUDCrosshair::DeinitShaderCrosshair()
{
if (shaderCrosshair->inited())
{
shaderCrosshair->destroy();
}
}
bool CHUDCrosshair::InitShaderCrosshair()
{
if (strcmp(lastCrosshairShader, crosshair_shader) || strcmp(lastCrosshairTexture, crosshair_texture))
{
DeinitShaderCrosshair();
shaderCrosshair->create(crosshair_shader, crosshair_texture);
strcpy(lastCrosshairShader, crosshair_shader);
strcpy(lastCrosshairTexture, crosshair_texture);
}
return shaderCrosshair->inited();
}
void CHUDCrosshair::RenderShaderCrosshair()
{
UIRender->StartPrimitive(4, IUIRender::ptTriStrip, UI().m_currentPointType);
Fvector4 pt;
Device.mFullTransform.transform(pt, transform.c);
pt.y = -pt.y;
Fvector2 scr_size = { float(Device.dwWidth), float(Device.dwHeight) };
Fvector verts[4] = {
{-maxRadius, -maxRadius},
{-maxRadius, maxRadius},
{maxRadius, -maxRadius},
{maxRadius, maxRadius},
};
Fvector2 uvs[4] = {
{0, 1},
{0, 0},
{1, 1},
{1, 0},
};
// Bring our transform into view space
Fvector pos = Fvector();
transform.transform_tiny(pos);
Device.mView.transform_tiny(pos);
// Project without W-divide to retrieve linear depth
Fvector pos_ = Fvector().set(pos);
Device.mProject.transform_tiny(pos_);
// Calculate size from linear depth
float zNear = Device.ViewportNear;
float zFar = g_pGamePersistent->Environment().CurrentEnv->far_plane;
float size = lerp(zNear + (crosshair_near_size - 1), zFar, (pos_.z - zNear) / (zFar - zNear));
// Transform and push vertices
for (int i = 0; i < 4; i++)
{
Fvector vert = verts[i];
vert.mul(size);
transform.transform_tiny(vert);
Device.mView.transform_tiny(vert);
Device.mProject.transform(vert);
vert.x = (vert.x + 1.f) * 0.5f * scr_size.x;
vert.y = (-vert.y + 1.f) * 0.5f * scr_size.y;
Fvector2 uv = uvs[i];
UIRender->PushPoint(vert.x, vert.y, 0, crossColor, uv.x, uv.y);
}
// Draw
UIRender->SetShader(*shaderCrosshair);
UIRender->FlushPrimitive();
}
void CHUDCrosshair::RenderWireCrosshair()
{
// Fetch the render target size
Fvector2 scr_size = {
float(::Render->getTarget()->get_width()),
float(::Render->getTarget()->get_height())
};
// Create vertices from our size metrics
Fvector verts[8] = {
{ minRadius, 0 },
{ maxRadius, 0 },
{ -minRadius, 0 },
{ -maxRadius, 0 },
{ 0, minRadius },
{ 0, maxRadius },
{ 0, -minRadius },
{ 0, -maxRadius },
};
// Transform into view space
Fvector pos = Fvector();
transform.transform_tiny(pos);
Device.mView.transform_tiny(pos);
// Project without W-divide to retrieve linear depth
Fvector pos_ = Fvector().set(pos);
Device.mProject.transform_tiny(pos_);
// Calculate size from linear depth
float zNear = Device.ViewportNear;
float zFar = g_pGamePersistent->Environment().CurrentEnv->far_plane;
float size = lerp(zNear + (crosshair_near_size - 1), zFar, (pos_.z - zNear) / (zFar - zNear));
// Project into NDC with W-divide
Device.mProject.transform(pos);
// Project vertices for accurate scaling
UIRender->StartPrimitive(8, IUIRender::ptLineList, UI().m_currentPointType);
for (int i = 0; i < 8; i++)
{
Fvector vert = verts[i];
vert.mul(size);
transform.transform_tiny(vert);
Device.mView.transform_tiny(vert);
Device.mProject.transform(vert);
vert.x = (vert.x + 1.f) * 0.5f * scr_size.x;
vert.y = (-vert.y + 1.f) * 0.5f * scr_size.y;
UIRender->PushPoint(vert.x, vert.y, 0, crossColor, 0, 0);
}
// Apply perspective divide to aim point and transform into screen space
pos.x = (pos.x + 1.f) * 0.5f * scr_size.x;
pos.y = (-pos.y + 1.f) * 0.5f * scr_size.y;
// Render a 1px wide line for the center dot
UIRender->PushPoint(pos.x - 0.5f, pos.y, 0, crossColor, 0, 0);
UIRender->PushPoint(pos.x + 0.5f, pos.y, 0, crossColor, 0, 0);
UIRender->SetShader(*shaderWire);
UIRender->FlushPrimitive();
}
void CHUDCrosshair::OnRender()
{
VERIFY(g_bRendering);
if (psHUD_Flags.is(HUD_SHADER_CROSSHAIR))
{
if (InitShaderCrosshair())
RenderShaderCrosshair();
}
else
{
DeinitShaderCrosshair();
RenderWireCrosshair();
}
}