-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKinectPointCloudRenderer.cs
More file actions
241 lines (189 loc) · 7.87 KB
/
KinectPointCloudRenderer.cs
File metadata and controls
241 lines (189 loc) · 7.87 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace KinectMapping
{
internal class KinectPointCloudRenderer
{
private KinectUtil kinectUtil;
private GLControl glControl;
private int vboVertexId;
private int vboColorId;
private bool vboInitialized = false;
private float cameraAngle = 0.0f;
private float cameraDistance = 3.0f;
private float cameraPitch = 15.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
private float roll = 0.0f;
public KinectPointCloudRenderer(KinectUtil kinectUtil, GLControl glControl)
{
this.kinectUtil = kinectUtil;
this.glControl = glControl;
SetupOpenGL();
glControl.Paint += GlControl_Paint;
glControl.Resize += GlControl_Resize;
}
private void SetupOpenGL()
{
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL.ClearDepth(1.0f);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
GL.Enable(EnableCap.PointSmooth);
GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
GlControl_Resize(glControl, EventArgs.Empty);
InitializeVBOs();
}
private void InitializeVBOs()
{
if (vboInitialized)
{
GL.DeleteBuffer(vboVertexId);
GL.DeleteBuffer(vboColorId);
}
int width = kinectUtil.getDepthWidth();
int height = kinectUtil.getDepthHeight();
// Generate vertex buffer
vboVertexId = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vboVertexId);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(width * height * 3 * sizeof(float)),
IntPtr.Zero, BufferUsageHint.DynamicDraw);
// Generate color buffer
vboColorId = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vboColorId);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(width * height * 3 * sizeof(float)),
IntPtr.Zero, BufferUsageHint.DynamicDraw);
vboInitialized = true;
}
private void UpdateVBOs()
{
if (!vboInitialized)
InitializeVBOs();
var points = kinectUtil.GetPointCloud();
if (points.Count == 0)
return;
float[] vertices = new float[points.Count * 3];
float[] colors = new float[points.Count * 3];
float minDepth = 0;
float maxDepth = 4.3F;
var colorPoints = kinectUtil.MapDepthPointsToColorSpace();
byte[] colorData = kinectUtil.GetColorPixelData();
int colorWidth = kinectUtil.GetColorWidth();
int colorHeight = kinectUtil.GetColorHeight();
int validPointCount = 0;
for (int i = 0; i < points.Count; i++)
{
var point = points[i];
var colorPoint = colorPoints[i];
vertices[validPointCount * 3] = point.X;
vertices[validPointCount * 3 + 1] = point.Y;
vertices[validPointCount * 3 + 2] = -point.Z;
if (colorPoint.X >= 0 && colorPoint.X < colorWidth &&
colorPoint.Y >= 0 && colorPoint.Y < colorHeight)
{
int colorIndex = ((int)colorPoint.Y * colorWidth + (int)colorPoint.X) * 4;
// BGR to RGB
colors[validPointCount * 3] = colorData[colorIndex + 2] / 255.0f;
colors[validPointCount * 3 + 1] = colorData[colorIndex + 1] / 255.0f;
colors[validPointCount * 3 + 2] = colorData[colorIndex] / 255.0f;
}
else
{
// Fallback in case somethings wrong
colors[validPointCount * 3] = 1.0f;
colors[validPointCount * 3 + 1] = 1.0f;
colors[validPointCount * 3 + 2] = 1.0f;
}
validPointCount++;
}
GL.BindBuffer(BufferTarget.ArrayBuffer, vboVertexId);
GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero,
(IntPtr)(vertices.Length * sizeof(float)), vertices);
GL.BindBuffer(BufferTarget.ArrayBuffer, vboColorId);
GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero,
(IntPtr)(colors.Length * sizeof(float)), colors);
}
private void RotateCamera()
{
GL.LoadIdentity();
float camX = cameraDistance * (float)Math.Sin(MathHelper.DegreesToRadians(cameraAngle));
float camZ = cameraDistance * (float)Math.Cos(MathHelper.DegreesToRadians(cameraAngle));
Matrix4 lookAt = Matrix4.LookAt(
new Vector3(camX, 0, camZ), // Camera position
new Vector3(0, 0, 0), // Target (looking at origin)
new Vector3(0, 1, 0) // Up vector
);
GL.LoadMatrix(ref lookAt);
GL.Rotate(yaw, 0.0f, 1.0f, 0.0f); // Yaw around Y
GL.Rotate(pitch, 1.0f, 0.0f, 0.0f); // Pitch around X
GL.Rotate(roll, 0.0f, 0.0f, 1.0f); // Roll around Z
}
private void DrawPointCloud()
{
UpdateVBOs();
int pointCount = kinectUtil.GetPointCloud().Count;
if (pointCount == 0)
return;
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vboVertexId);
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, vboColorId);
GL.ColorPointer(3, ColorPointerType.Float, 0, IntPtr.Zero);
GL.PointSize(1.0f);
GL.DrawArrays(PrimitiveType.Points, 0, pointCount);
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.ColorArray);
}
private void GlControl_Paint(object sender, PaintEventArgs e)
{
if (!glControl.Context.IsCurrent)
glControl.MakeCurrent();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
RotateCamera();
DrawPointCloud();
glControl.SwapBuffers();
}
private void GlControl_Resize(object sender, EventArgs e)
{
GL.Viewport(0, 0, glControl.Width, glControl.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
float aspectRatio = (float)glControl.Width / glControl.Height;
Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(
MathHelper.DegreesToRadians(45),
aspectRatio,
0.1f,
20.0f
);
Matrix4 zFlip = Matrix4.CreateScale(1, 1, -1);
perspective *= zFlip;
GL.LoadMatrix(ref perspective);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
}
public void SetYawPitchRoll(float yaw, float pitch, float roll)
{
this.yaw = yaw;
this.pitch = pitch;
this.roll = roll;
glControl.Invalidate();
}
public void SetCameraDistance(float distance)
{
this.cameraDistance = Math.Max(0.5f, Math.Min(10.0f, distance));
glControl.Invalidate();
}
public void Dispose()
{
if (vboInitialized)
{
GL.DeleteBuffer(vboVertexId);
GL.DeleteBuffer(vboColorId);
vboInitialized = false;
}
}
}
}