-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderer.cs
More file actions
188 lines (160 loc) · 7.22 KB
/
Renderer.cs
File metadata and controls
188 lines (160 loc) · 7.22 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
using Microsoft.Azure.Kinect.BodyTracking;
using OpenGL;
using OpenGL.CoreUI;
using System;
using System.IO;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
namespace Csharp_3d_viewer
{
public class Renderer
{
// private SphereRenderer SphereRenderer;
// private CylinderRenderer CylinderRenderer;
// private PointCloudRenderer PointCloudRenderer;
private readonly VisualizerData visualizerData;
// private List<Vertex> pointCloud = null;
public Renderer(VisualizerData visualizerData)
{
this.visualizerData = visualizerData;
}
public bool IsActive { get; private set; }
public bool IsHuman { get; private set; } = false;
public string day;
public string scene;
public string now;
public void StartVisualizationThread()
{
Task.Run(() =>
{
using (NativeWindow nativeWindow = NativeWindow.Create())
{
IsActive = true;
// nativeWindow.ContextCreated += NativeWindow_ContextCreated;
nativeWindow.Render += NativeWindow_Render;
nativeWindow.KeyDown += (object obj, NativeWindowKeyEventArgs e) =>
{
switch (e.Key)
{
case KeyCode.Escape:
nativeWindow.Stop();
IsActive = false;
break;
case KeyCode.F:
nativeWindow.Fullscreen = !nativeWindow.Fullscreen;
break;
}
};
nativeWindow.Animation = true;
nativeWindow.Create(0, 0, 640, 480, NativeWindowStyle.Overlapped);
nativeWindow.Show();
nativeWindow.Run();
}
});
}
private void NativeWindow_ContextCreated(object sender, NativeWindowEventArgs e)
{
Gl.ReadBuffer(ReadBufferMode.Back);
Gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.Enable(EnableCap.Blend);
Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
Gl.LineWidth(2.5f);
CreateResources();
}
private static float ToRadians(float degrees)
{
return degrees / 180.0f * (float)Math.PI;
}
public void NativeWindow_Render(object sender, NativeWindowEventArgs e)
{
using (var lastFrame = visualizerData.TakeFrameWithOwnership())
{
if (lastFrame == null)
{
return;
}
NativeWindow nativeWindow = (NativeWindow)sender;
// Gl.Viewport(0, 0, (int)nativeWindow.Width, (int)nativeWindow.Height);
// Gl.Clear(ClearBufferMask.ColorBufferBit);
// Update model/view/projective matrices in shader
// var proj = Matrix4x4.CreatePerspectiveFieldOfView(ToRadians(65.0f), (float)nativeWindow.Width / nativeWindow.Height, 0.1f, 150.0f);
// var view = Matrix4x4.CreateLookAt(Vector3.Zero, Vector3.UnitZ, -Vector3.UnitY);
if (lastFrame.NumberOfBodies > 0)
{
// SphereRenderer.View = view;
// SphereRenderer.Projection = proj;
// CylinderRenderer.View = view;
// CylinderRenderer.Projection = proj;
// PointCloudRenderer.View = view;
// PointCloudRenderer.Projection = proj;
// PointCloud.ComputePointCloud(lastFrame.Capture.Depth, ref pointCloud);
// PointCloudRenderer.Render(pointCloud, new Vector4(1, 1, 1, 1));
if (!IsHuman)
{
this.day = DateTime.Now.ToString("yyyyMMdd");
this.scene = DateTime.Now.ToString("HHmmssfff");
string path = $@"C:\Users\gekka\temp\{this.day}\{this.scene}\depth";
Directory.CreateDirectory(path);
IsHuman = true;
}
for (uint i = 0; i < lastFrame.NumberOfBodies; ++i)
{
// System.Diagnostics.Debug.WriteLine(i);
DirectoryUtils.SafeCreateDirectory($@"C:\Users\gekka\temp\{this.day}\{this.scene}\{i}");
string filename = $@"C:\Users\gekka\temp\{this.day}\{this.scene}\{i}\pos.csv";
var append = true;
var skeleton = lastFrame.GetBodySkeleton(i);
var bodyId = lastFrame.GetBodyId(i);
var bodyColor = BodyColors.GetColorAsVector(bodyId);
using (var sw = new System.IO.StreamWriter(filename, append))
{
this.now = DateTime.Now.ToString("HHmmssfff");
sw.Write("{0}, ", this.now);
for (int jointId = 0; jointId < (int)JointId.Count; ++jointId)
{
var joint = skeleton.GetJoint(jointId);
sw.Write("{0}, {1}, {2},", joint.Position.X, joint.Position.Y, joint.Position.Z);
// GUI描画
// Render the joint as a sphere.
// const float radius = 0.024f;
// SphereRenderer.Render(joint.Position / 1000, radius, bodyColor);
// if (JointConnections.JointParent.TryGetValue((JointId)jointId, out JointId parentId))
// {
// //Render a bone connecting this joint and its parent as a cylinder.
// CylinderRenderer.Render(joint.Position / 1000, skeleton.GetJoint((int)parentId).Position / 1000, bodyColor);
// }
}
sw.Write("\r\n");
}
}
}
else
{
IsHuman = false;
}
}
}
private void CreateResources()
{
// SphereRenderer = new SphereRenderer();
// CylinderRenderer = new CylinderRenderer();
// PointCloudRenderer = new PointCloudRenderer();
}
public static class DirectoryUtils
{
/// <summary>
/// 指定したパスにディレクトリが存在しない場合
/// すべてのディレクトリとサブディレクトリを作成します
/// </summary>
public static DirectoryInfo SafeCreateDirectory( string path )
{
if ( Directory.Exists( path ) )
{
return null;
}
return Directory.CreateDirectory( path );
}
}
}
}