-
Notifications
You must be signed in to change notification settings - Fork 6
/
CrossHairModel.cs
249 lines (221 loc) · 9.38 KB
/
CrossHairModel.cs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using System;
using System.Collections.Generic;
namespace Undistort
{
public static class CrossHairModel
{
private static float[] vertices;
private static SharpDX.Direct3D11.Buffer vertexBuffer;
private static VertexBufferBinding vertexBufferBinding;
private static float[] points;
private static SharpDX.Direct3D11.Buffer pointBuffer;
private static VertexBufferBinding pointBufferBinding;
private static Shader shader;
public static float Radius;
private static int DefautVerticesCount;
private static int PointCount;
public static void Init(SharpDX.Direct3D11.Device device)
{
shader = new Shader(device, "CrossHair_VS", "CrossHair_PS", new[]
{
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0),
new InputElement("COLOR", 0, Format.R32G32B32_Float, 0),
});
var pointList = new List<float>();
var green = new float[] { 0, 1, 0 };
for (var x = -5f; x < 5f; x += 0.5f)
{
for (var y = 0f; y < 7f; y += 0.5f)
{
pointList.Add(x);
pointList.Add(y);
pointList.Add(5);
pointList.AddRange(green);
pointList.Add(x);
pointList.Add(y);
pointList.Add(4);
pointList.AddRange(green);
}
}
points = pointList.ToArray();
pointBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, points);
pointBufferBinding = new VertexBufferBinding(pointBuffer, sizeof(float) * 6, 0);
PointCount = pointList.Count / 6;
Radius = 0.3f;
ModifyCircles(device, 0);
}
public static void Render(DeviceContext context)
{
shader.Apply(context);
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
context.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
switch (Program.RenderMode)
{
case 0:
context.Draw(DefautVerticesCount, 0);
//case 1:
break;
default:
context.Draw(vertices.Length / 6, 0);
break;
}
}
public static void RenderPoints(DeviceContext context)
{
shader.Apply(context);
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
context.InputAssembler.SetVertexBuffers(0, pointBufferBinding);
context.Draw(PointCount, 0);
}
public static void ModifyCircles(SharpDX.Direct3D11.Device device, float adj)
{
var depth = -1f;
Radius += adj;
if (Radius > 1.0f) Radius = 1.0f;
if (Radius < 0.001f) Radius = 0.001f;
var verticesList = new List<float>();
float smallMarkerSize = 0.02f;
//float bigMarkerSize = 0.02f;
//vertical and horiz crosshair
verticesList.AddRange(new float[] {
-1f, 0f, depth, 0, 1, 0,
1f, 0f, depth, 0, 1, 0,
0f, -1f, depth, 0, 1, 0,
0f, 1f, depth, 0, 1, 0
});
if (Program.pixelShaderData._Undistort == 1)
{
int sk = 1;
//markers
var green = new float[] { 0, 1, 0 };
for (var y = 0.1f; y < 1.0f / Program.ScreenAspect; y += 0.1f)
{
float markerSize = smallMarkerSize * (1.2f * sk++);
//if ((sk++ % 5) == 0)
// markerSize = bigMarkerSize * 1.5f * bk++;
//vert
verticesList.Add((float)-markerSize);
verticesList.Add((float)y);
verticesList.Add(depth);
verticesList.AddRange(green);
verticesList.Add((float)markerSize);
verticesList.Add((float)y);
verticesList.Add(depth);
verticesList.AddRange(green);
verticesList.Add((float)-markerSize);
verticesList.Add((float)-y);
verticesList.Add(depth);
verticesList.AddRange(green);
verticesList.Add((float)markerSize);
verticesList.Add((float)-y);
verticesList.Add(depth);
verticesList.AddRange(green);
}
sk = 1;
for (var x = 0.1f; x < 1.0f; x += 0.1f)
{
float markerSize = smallMarkerSize * Program.ScreenAspect * (1.2f * sk++); ;
//if ((sk++ % 5) == 0)
// markerSize = bigMarkerSize * 1.5f * Program.ScreenAspect * bk++;
//horz
verticesList.Add((float)x);
verticesList.Add((float)-markerSize / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(green);
verticesList.Add((float)x);
verticesList.Add((float)markerSize / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(green);
verticesList.Add((float)-x);
verticesList.Add((float)-markerSize / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(green);
verticesList.Add((float)-x);
verticesList.Add((float)markerSize / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(green);
}
}
DefautVerticesCount = verticesList.Count / 6;
var white = new float[] { 1, 1, 1 };
//grid
for (var y = 0.1f; y < 1.0f / Program.ScreenAspect; y += 0.1f)
{
//horz
verticesList.Add((float)-1);
verticesList.Add((float)y);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)1);
verticesList.Add((float)y);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)-1);
verticesList.Add((float)-y);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)1);
verticesList.Add((float)-y);
verticesList.Add(depth);
verticesList.AddRange(white);
}
for (var x = 0.1f; x < 1.0f; x += 0.1f)
{
//horz
verticesList.Add((float)x);
verticesList.Add((float)-1 / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)x);
verticesList.Add((float)1 / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)-x);
verticesList.Add((float)-1 / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)-x);
verticesList.Add((float)1 / Program.ScreenAspect);
verticesList.Add(depth);
verticesList.AddRange(white);
}
//circles
for (var r = Radius; r < 1.0; r += Radius)
{
double px = 0.0;
double py = 0.0;
double x = 0.0;
double y = 0.0;
for (var d = 0; d <= 360; d++)
{
var dd = d * (Math.PI / 180.0);
x = r * Math.Cos(dd);
y = r * Math.Sin(dd);
if (d == 0)
{
px = x;
py = y;
}
verticesList.Add((float)px);
verticesList.Add((float)py);
verticesList.Add(depth);
verticesList.AddRange(white);
verticesList.Add((float)x);
verticesList.Add((float)y);
verticesList.Add(depth);
verticesList.AddRange(white);
px = x;
py = y;
}
}
vertices = verticesList.ToArray();
if (vertexBuffer != null)
vertexBuffer.Dispose();
vertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);
vertexBufferBinding = new VertexBufferBinding(vertexBuffer, sizeof(float) * 6, 0);
}
}
}