forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.hpp
326 lines (272 loc) · 9.53 KB
/
example.hpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
#pragma once
#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>
#include <string>
#include <sstream>
#include <iostream>
//////////////////////////////
// Basic Data Types //
//////////////////////////////
struct float3 { float x, y, z; };
struct float2 { float x, y; };
struct rect
{
float x, y;
float w, h;
// Create new rect within original boundaries with give aspect ration
rect adjust_ratio(float2 size) const
{
auto H = static_cast<float>(h), W = static_cast<float>(h) * size.x / size.y;
if (W > w)
{
auto scale = w / W;
W *= scale;
H *= scale;
}
return{ x + (w - W) / 2, y + (h - H) / 2, W, H };
}
};
//////////////////////////////
// Simple font loading code //
//////////////////////////////
#include "../third-party/stb_easy_font.h"
inline void draw_text(int x, int y, const char * text)
{
char buffer[60000]; // ~300 chars
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 16, buffer);
glDrawArrays(GL_QUADS, 0, 4 * stb_easy_font_print((float)x, (float)(y - 7), (char *)text, nullptr, buffer, sizeof(buffer)));
glDisableClientState(GL_VERTEX_ARRAY);
}
////////////////////////
// Image display code //
////////////////////////
class texture
{
public:
void render(const rs2::video_frame& frame, const rect& r)
{
upload(frame);
show(r.adjust_ratio({ float(width), float(height) }));
}
void upload(const rs2::video_frame& frame)
{
if (!frame) return;
if (!gl_handle)
glGenTextures(1, &gl_handle);
GLenum err = glGetError();
auto format = frame.get_profile().format();
width = frame.get_width();
height = frame.get_height();
stream = frame.get_profile().stream_type();
glBindTexture(GL_TEXTURE_2D, gl_handle);
switch (format)
{
case RS2_FORMAT_RGB8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.get_data());
break;
case RS2_FORMAT_RGBA8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame.get_data());
break;
case RS2_FORMAT_Y8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame.get_data());
break;
default:
throw std::runtime_error("The requested format is not supported by this demo!");
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
GLuint get_gl_handle() { return gl_handle; }
void show(const rect& r) const
{
if (!gl_handle)
return;
glBindTexture(GL_TEXTURE_2D, gl_handle);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUAD_STRIP);
glTexCoord2f(0.f, 1.f); glVertex2f(r.x, r.y + r.h);
glTexCoord2f(0.f, 0.f); glVertex2f(r.x, r.y);
glTexCoord2f(1.f, 1.f); glVertex2f(r.x + r.w, r.y + r.h);
glTexCoord2f(1.f, 0.f); glVertex2f(r.x + r.w, r.y);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
draw_text((int)r.x + 15, (int)r.y + 20, rs2_stream_to_string(stream));
}
private:
GLuint gl_handle = 0;
int width = 0;
int height = 0;
rs2_stream stream = RS2_STREAM_ANY;
};
class window
{
public:
std::function<void(bool)> on_left_mouse = [](bool) {};
std::function<void(double, double)> on_mouse_scroll = [](double, double) {};
std::function<void(double, double)> on_mouse_move = [](double, double) {};
std::function<void(int)> on_key_release = [](int) {};
window(int width, int height, const char* title)
: _width(width), _height(height)
{
glfwInit();
win = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!win)
throw std::runtime_error("Could not open OpenGL window, please check your graphic drivers or use the textual SDK tools");
glfwMakeContextCurrent(win);
glfwSetWindowUserPointer(win, this);
glfwSetMouseButtonCallback(win, [](GLFWwindow * win, int button, int action, int mods)
{
auto s = (window*)glfwGetWindowUserPointer(win);
if (button == 0) s->on_left_mouse(action == GLFW_PRESS);
});
glfwSetScrollCallback(win, [](GLFWwindow * win, double xoffset, double yoffset)
{
auto s = (window*)glfwGetWindowUserPointer(win);
s->on_mouse_scroll(xoffset, yoffset);
});
glfwSetCursorPosCallback(win, [](GLFWwindow * win, double x, double y)
{
auto s = (window*)glfwGetWindowUserPointer(win);
s->on_mouse_move(x, y);
});
glfwSetKeyCallback(win, [](GLFWwindow * win, int key, int scancode, int action, int mods)
{
auto s = (window*)glfwGetWindowUserPointer(win);
if (0 == action) // on key release
{
s->on_key_release(key);
}
});
}
float width() const { return float(_width); }
float height() const { return float(_height); }
operator bool()
{
glPopMatrix();
glfwSwapBuffers(win);
auto res = !glfwWindowShouldClose(win);
glfwPollEvents();
glfwGetFramebufferSize(win, &_width, &_height);
// Clear the framebuffer
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, _width, _height);
// Draw the images
glPushMatrix();
glfwGetWindowSize(win, &_width, &_height);
glOrtho(0, _width, _height, 0, -1, +1);
return res;
}
~window()
{
glfwDestroyWindow(win);
glfwTerminate();
}
operator GLFWwindow*() { return win; }
private:
GLFWwindow* win;
int _width, _height;
};
// Struct for managing rotation of pointcloud view
struct glfw_state {
glfw_state() : yaw(15.0), pitch(15.0), last_x(0.0), last_y(0.0),
ml(false), offset_x(2.f), offset_y(2.f), tex() {}
double yaw;
double pitch;
double last_x;
double last_y;
bool ml;
float offset_x;
float offset_y;
texture tex;
};
// Handles all the OpenGL calls needed to display the point cloud
void draw_pointcloud(float width, float height, glfw_state& app_state, rs2::points& points)
{
if (!points)
return;
// OpenGL commands that prep screen for the pointcloud
glPopMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);
glClearColor(153.f / 255, 153.f / 255, 153.f / 255, 1);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluPerspective(60, width / height, 0.01f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(0, 0, 0, 0, 0, 1, 0, -1, 0);
glTranslatef(0, 0, +0.5f + app_state.offset_y*0.05f);
glRotated(app_state.pitch, 1, 0, 0);
glRotated(app_state.yaw, 0, 1, 0);
glTranslatef(0, 0, -0.5f);
glPointSize(width / 640);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, app_state.tex.get_gl_handle());
float tex_border_color[] = { 0.8f, 0.8f, 0.8f, 0.8f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, tex_border_color);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 0x812F); // GL_CLAMP_TO_EDGE
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 0x812F); // GL_CLAMP_TO_EDGE
glBegin(GL_POINTS);
/* this segment actually prints the pointcloud */
auto vertices = points.get_vertices(); // get vertices
auto tex_coords = points.get_texture_coordinates(); // and texture coordinates
for (int i = 0; i < points.size(); i++)
{
if (vertices[i].z)
{
// upload the point and texture coordinates only for points we have depth data for
glVertex3fv(vertices[i]);
glTexCoord2fv(tex_coords[i]);
}
}
// OpenGL cleanup
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glPushMatrix();
}
// Registers the state variable and callbacks to allow mouse control of the pointcloud
void register_glfw_callbacks(window& app, glfw_state& app_state)
{
app.on_left_mouse = [&](bool pressed)
{
app_state.ml = pressed;
};
app.on_mouse_scroll = [&](double xoffset, double yoffset)
{
app_state.offset_x -= static_cast<float>(xoffset);
app_state.offset_y -= static_cast<float>(yoffset);
};
app.on_mouse_move = [&](double x, double y)
{
if (app_state.ml)
{
app_state.yaw -= (x - app_state.last_x);
app_state.yaw = std::max(app_state.yaw, -120.0);
app_state.yaw = std::min(app_state.yaw, +120.0);
app_state.pitch += (y - app_state.last_y);
app_state.pitch = std::max(app_state.pitch, -80.0);
app_state.pitch = std::min(app_state.pitch, +80.0);
}
app_state.last_x = x;
app_state.last_y = y;
};
app.on_key_release = [&](int key)
{
if (key == 32) // Escape
{
app_state.yaw = app_state.pitch = 0; app_state.offset_x = app_state.offset_y = 0.0;
}
};
}