forked from sencercoltu/steamvr-undistort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointerModel.cs
65 lines (57 loc) · 2.53 KB
/
PointerModel.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
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Mathematics.Interop;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
using Valve.VR;
using static Undistort.Program;
namespace Undistort
{
public static class PointerModel
{
private static float[] vertices;
private static SharpDX.Direct3D11.Buffer vertexBuffer;
private static VertexBufferBinding vertexBufferBinding;
private static Shader shader;
private static Matrix Origin = Matrix.Translation(0, 0, 0);
private static Matrix Direction = Matrix.Translation(0, 0, -100);
public static Matrix WVP = Matrix.Zero;
public static void Init(SharpDX.Direct3D11.Device device)
{
shader = new Shader(device, "Pointer_VS", "Pointer_PS", new[]
{
new SharpDX.Direct3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0)
});
vertices = new float[] {
Origin.TranslationVector.X, Origin.TranslationVector.Y, Origin.TranslationVector.Z,
Direction.TranslationVector.X, Direction.TranslationVector.Y, Direction.TranslationVector.Z
};
vertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);
vertexBufferBinding = new VertexBufferBinding(vertexBuffer, sizeof(float) * 3, 0);
}
public static void Render(SharpDX.Direct3D11.DeviceContext context)
{
//we apply wvp here to update z
var origin = (Origin * WVP).TranslationVector;
var direction = (Direction * WVP).TranslationVector;
var ray = new SharpDX.Ray(origin, direction);
//if ray is inside panel, show
if (AdjustmentPanelModel.CheckBounds(ref ray, out var z))
{
var k = Direction.TranslationVector.Length();
vertices[5] = -z * k;
shader.Apply(context);
context.UpdateSubresource(vertices, vertexBuffer);
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
context.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
context.Draw(2, 0);
}
}
}
}