forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.hpp
210 lines (171 loc) · 5.89 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
// 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_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);
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;
};