forked from karlphillip/GraphicsProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
executable file
·304 lines (260 loc) · 9.8 KB
/
window.cpp
File metadata and controls
executable file
·304 lines (260 loc) · 9.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Copyright (C) 2015 Karl Phillip Buhr <karlphillip@gmail.com>
*
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
* To view a copy of this license, visit:
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
*
* Or to read the human-readable summary of the license:
* https://creativecommons.org/licenses/by-sa/2.5/
*/
#include "window.h"
#include <iostream>
#include <QApplication>
Window::Window()
: _tick_ms(33), _ar_mode(Qt::IgnoreAspectRatio), _fps(0),
_video_width(640), _video_height(480), _timer(NULL), _nui_sensor(NULL)
{
setWindowTitle("QT demo with Kinect");
resize(_video_width, _video_height);
// Initialize device and prepare it to send RGB data
if (!_initKinect())
{
std::cout << "* FAILED! Is your Kinect device connected? Is it powered?" << std::endl;
return;
}
std::cout << "* Special keys: " << std::endl;
std::cout << "\tESC - Quit application" << std::endl;
std::cout << "\tM - Change aspect ratio" << std::endl;
std::cout << "\tUp - Increase elevation" << std::endl;
std::cout << "\tDown - Decrease elevation" << std::endl;
std::cout << "\tLEFT - Reset elevation to 0 degrees" << std::endl;
// Start timer to read frames from Kinect
_timer = new QTimer();
_timer->start(_tick_ms);
QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(_tick()));
}
Window::~Window()
{
if (_timer)
{
_timer->stop();
delete _timer;
}
// Shutdown Kinect properly
if (_nui_sensor)
_nui_sensor->NuiShutdown();
}
/* _tick(): called every few milliseconds.
* It reads data from Kinect's capture interface and saves the frame as QImage.
*/
void Window::_tick()
{
if (!_nui_sensor)
{
std::cout << "_tick !!! _initKinect() failed: ABORT!" << std::endl;
return;
}
/* Read data from Kinect */
NUI_IMAGE_FRAME _nui_frame;
if (_nui_sensor->NuiImageStreamGetNextFrame(_rgb_stream, 0, &_nui_frame) < 0)
return;
NUI_LOCKED_RECT locked_rect;
INuiFrameTexture* texture = _nui_frame.pFrameTexture;
HRESULT result = texture->LockRect(0, &locked_rect, NULL, 0);
if (result == S_OK && locked_rect.Pitch != 0)
{
NUI_SURFACE_DESC surface_desc;
texture->GetLevelDesc(0, &surface_desc);
if (_video_width != surface_desc.Width || _video_height != surface_desc.Height)
{
_video_width = surface_desc.Width;
_video_height = surface_desc.Height;
resize(_video_width, _video_height);
}
// Copy pixels to QImage
_image = QImage((const uchar*)locked_rect.pBits,
surface_desc.Width, surface_desc.Height,
QImage::Format_RGB32);
// Trigger paint event to redraw the window
if (!_image.isNull())
emit update();
}
texture->UnlockRect(0);
_nui_sensor->NuiImageStreamReleaseFrame(_rgb_stream, &_nui_frame);
}
bool Window::_initKinect()
{
int sensor_count = 0;
if (NuiGetSensorCount(&sensor_count) < 0 || sensor_count < 1)
{
std::cout << "_initKinect !!! NuiGetSensorCount() failed" << std::endl;
return false;
}
std::cout << "* Number of compatible sensors detected: " << sensor_count << std::endl;
/* Look at each Kinect sensor */
bool sensor_found = false;
for (int i = 0; i < sensor_count; i++)
{
std::cout << "* Checking sensor #" << i << " ... ";
// Create the sensor so we can check status, if we can't create it, move on to the next
if (NuiCreateSensorByIndex(i, &_nui_sensor) < 0)
{
std::cout << " SKIPPED!" << std::endl;
continue;
}
// Get the status of the sensor, and if connected, then we can initialize it
if (_nui_sensor->NuiStatus() != S_OK)
{
std::cout << " NOT READY!" << std::endl;
}
else
{
std::cout << " SELECTED!" << std::endl;
sensor_found = true;
break;
}
// This sensor wasn't OK, so release it since we're not using it
_nui_sensor->Release();
}
if (_nui_sensor == NULL || !sensor_found)
{
std::cout << "_initKinect !!! No ready Kinect found." << std::endl;
return false;
}
// Initialize the Kinect and specify that we'll be using color
if (_nui_sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR) >= 0) // if (SUCCEEDED(hr))
{
// Open a color image stream to receive color frames
if (_nui_sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, // Depth camera or rgb camera?
NUI_IMAGE_RESOLUTION_640x480, // Image resolution
0, // Image stream flags, e.g. near mode
2, // Number of frames to buffer
NULL, // Event handle
&_rgb_stream) < 0)
{
std::cout << "_initKinect !!! NuiImageStreamOpen() failed" << std::endl;
return false;
}
}
return true;
}
void Window::paintEvent(QPaintEvent* e)
{
QWidget::paintEvent(e);
// Draw a frame
QPainter painter(this);
_draw_video_frame(painter);
}
void Window::_draw_video_frame(QPainter& painter)
{
// When no image is loaded, paint a black frame
if (_image.isNull())
{
// QImage is not ready yet
painter.fillRect(QRectF(QPoint(0, 0), QSize(width(), height())), Qt::black);
return;
}
// To simply draw on the window, execute the code below.
//painter.drawImage(QPoint(0, 0), _image);
// But we are drawing the image according to AR settings:
// http://doc.qt.digia.com/stable/qt.html#AspectRatioMode-enum
switch (_ar_mode)
{
default:
case Qt::IgnoreAspectRatio:
{
painter.drawImage(QRectF(QPoint(0, 0), QSize(_video_width, _video_height)),
_image,
QRectF(QPoint(0, 0), _image.size()));
}
break;
case Qt::KeepAspectRatio:
{
painter.fillRect(QRectF(QPoint(0, 0), QSize(width(), height())), Qt::black);
QImage scaled_img = _image.scaled(QSize(width(), height()), Qt::KeepAspectRatio, Qt::FastTransformation);
painter.drawImage(qRound(width()/2.0) - qRound(scaled_img.size().width()/2.0),
qRound(height()/2.0) - qRound(scaled_img.size().height()/2.0),
scaled_img);
}
break;
case Qt::KeepAspectRatioByExpanding:
{
QImage scaled_img = _image.scaled(QSize(width(), height()), Qt::KeepAspectRatioByExpanding, Qt::FastTransformation);
painter.drawImage(QRectF(QPoint(0, 0), QSize(width(), height())),
scaled_img,
QRectF(QPoint(qRound(scaled_img.size().width()/2.0) - qRound(width()/2.0),
qRound(scaled_img.size().height()/2.0) - qRound(height()/2.0)),
QSize(width(), height())));
}
break;
}
}
void Window::keyPressEvent(QKeyEvent* event)
{
switch (event->key())
{
// ESC: exit application
case Qt::Key_Escape:
{
std::cout << "* (ESC) Bye bye." << std::endl;
QApplication::instance()->quit();
}
break;
// M: changes aspect ratio mode
case Qt::Key_M:
{
if (_ar_mode == Qt::IgnoreAspectRatio)
{
_ar_mode = Qt::KeepAspectRatio;
std::cout << "* (M) AR = keep aspect ratio" << std::endl;
}
else if (_ar_mode == Qt::KeepAspectRatio)
{
_ar_mode = Qt::KeepAspectRatioByExpanding;
std::cout << "* (M) AR = keep aspect ratio by expanding" << std::endl;
}
else
{
_ar_mode = Qt::IgnoreAspectRatio;
std::cout << "* (M) AR = ignore aspect ratio" << std::endl;
}
}
break;
// Up arrow: increases elevation angle by 2 degrees
case Qt::Key_Up:
{
long angle = -1;
if (NuiCameraElevationGetAngle(&angle) >= 0)
{
angle += 2;
if (NuiCameraElevationSetAngle(angle) >= 0)
std::cout << "* (UP) Current elevation angle: " << angle << std::endl;
}
}
break;
// Down arrow: decreases elevation angle by 2 degrees
case Qt::Key_Down:
{
long angle = -1;
if (NuiCameraElevationGetAngle(&angle) >= 0)
{
angle -= 2;
if (NuiCameraElevationSetAngle(angle) >= 0)
std::cout << "* (DOWN) Current elevation angle: " << angle << std::endl;
}
}
break;
// Left arrow: reset elevation angle
case Qt::Key_Left:
{
long angle = -1;
if (NuiCameraElevationGetAngle(&angle) >= 0)
{
angle = 0;
if (NuiCameraElevationSetAngle(angle) >= 0)
std::cout << "* (LEFT) Current elevation angle: " << angle << std::endl;
}
}
break;
}
}