pc-shader.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #include "pc-shader.h"
5 #include "synthetic-stream-gl.h"
6 #include <glad/glad.h>
7 
8 #include "option.h"
9 #include "tiny-profiler.h"
10 
11 static const char* vertex_shader_text =
12 "#version 130\n"
13 "\n"
14 "attribute vec3 position;\n"
15 "attribute vec2 textureCoords;\n"
16 "\n"
17 "out float valid;\n"
18 "out vec2 sampledUvs;\n"
19 "out vec4 outPos;\n"
20 "out vec3 normal;\n"
21 "\n"
22 "uniform mat4 transformationMatrix;\n"
23 "uniform mat4 projectionMatrix;\n"
24 "uniform mat4 cameraMatrix;\n"
25 "\n"
26 "uniform sampler2D uvsSampler;\n"
27 "uniform sampler2D positionsSampler;\n"
28 "\n"
29 "uniform float imageWidth;\n"
30 "uniform float imageHeight;\n"
31 "uniform float minDeltaZ;\n"
32 "\n"
33 "void main(void) {\n"
34 " float pixelWidth = 1.0 / imageWidth;\n"
35 " float pixelHeight = 1.0 / imageHeight;\n"
36 " vec2 tex = vec2(textureCoords.x, textureCoords.y);\n"
37 " vec4 pos = texture2D(positionsSampler, tex);\n"
38 " vec4 uvs = texture2D(uvsSampler, tex);\n"
39 "\n"
40 " vec2 tex_left = vec2(max(textureCoords.x - pixelWidth, 0.0), textureCoords.y);\n"
41 " vec2 tex_right = vec2(min(textureCoords.x + pixelWidth, 1.0), textureCoords.y);\n"
42 " vec2 tex_top = vec2(textureCoords.x, max(textureCoords.y - pixelHeight, 0.0));\n"
43 " vec2 tex_buttom = vec2(textureCoords.x, min(textureCoords.y + pixelHeight, 1.0));\n"
44 "\n"
45 " vec4 pos_left = texture2D(positionsSampler, tex_left);\n"
46 " vec4 pos_right = texture2D(positionsSampler, tex_right);\n"
47 " vec4 pos_top = texture2D(positionsSampler, tex_top);\n"
48 " vec4 pos_buttom = texture2D(positionsSampler, tex_buttom);\n"
49 "\n"
50 " vec3 axis1 = vec3(normalize(mix(pos_right - pos, pos - pos_left, 0.5)));\n"
51 " vec3 axis2 = vec3(normalize(mix(pos_top - pos, pos - pos_buttom, 0.5)));\n"
52 " normal = cross(axis1, axis2);\n"
53 "\n"
54 " valid = 0.0;\n"
55 " if (uvs.x < 0.0) valid = 1.0;\n"
56 " if (uvs.y < 0.0) valid = 1.0;\n"
57 " if (uvs.x >= 1.0) valid = 1.0;\n"
58 " if (uvs.y >= 1.0) valid = 1.0;\n"
59 " if (abs(pos_left.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
60 " if (abs(pos_right.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
61 " if (abs(pos_top.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
62 " if (abs(pos_buttom.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
63 " if (abs(pos.z) < 0.01) valid = 1.0;\n"
64 " if (valid > 0.0) pos = vec4(1.0, 1.0, 1.0, 0.0);\n"
65 " else pos = vec4(pos.xyz, 1.0);\n"
66 " vec4 worldPosition = transformationMatrix * pos;\n"
67 " gl_Position = projectionMatrix * cameraMatrix * worldPosition;\n"
68 "\n"
69 " sampledUvs = uvs.xy;\n"
70 " outPos = pos;\n"
71 "}\n";
72 
73 static const char* fragment_shader_text =
74 "#version 130\n"
75 "\n"
76 "in float valid;\n"
77 "in vec4 outPos;\n"
78 "in vec2 sampledUvs;\n"
79 "in vec3 normal;\n"
80 "out vec4 output_rgb;\n"
81 "\n"
82 "uniform sampler2D textureSampler;\n"
83 "uniform float pickedID;\n"
84 "uniform float shaded;\n"
85 "\n"
86 "const float Epsilon = 1e-10;\n"
87 "\n"
88 "vec3 rgb2hsv(vec3 c)\n"
89 "{\n"
90 " vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n"
91 " vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
92 " vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
93 " float d = q.x - min(q.w, q.y);\n"
94 " float e = 1.0e-10;\n"
95 " return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
96 "}\n"
97 "vec3 hsv2rgb(vec3 c)\n"
98 "{\n"
99 " vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"
100 " vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
101 " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
102 "}\n"
103 "void main(void) {\n"
104 " if (valid > 0.0) discard;\n"
105 " vec4 color = texture2D(textureSampler, sampledUvs);\n"
106 " if (shaded > 0.0) {\n"
107 " vec3 light0 = vec3(0.0, 1.0, 0.0);"
108 " vec3 light1 = vec3(0.5, -1.0, 0.0);"
109 " vec3 light2 = vec3(-0.5, -1.0, 0.0);"
110 " vec3 light_dir0 = light0 - vec3(outPos);\n"
111 " vec3 light_dir1 = light1 - vec3(outPos);\n"
112 " vec3 light_dir2 = light2 - vec3(outPos);\n"
113 " float diffuse_factor0 = max(dot(normal,light_dir0), 0.0);\n"
114 " float diffuse_factor1 = max(dot(normal,light_dir1), 0.0);\n"
115 " float diffuse_factor2 = max(dot(normal,light_dir2), 0.0);\n"
116 " float diffuse_factor = 0.6 + diffuse_factor0 * 0.2 + diffuse_factor1 * 0.2 + diffuse_factor2 * 0.2;\n"
117 " color = clamp(diffuse_factor, 0.0, 1.0) * color;\n"
118 " }\n"
119 " output_rgb = vec4(color.xyz, 1.0);\n"
120 "}\n";
121 
122 
123 static const char* vertex_shader_text_picking =
124 "#version 130\n"
125 "\n"
126 "attribute vec3 position;\n"
127 "attribute vec2 textureCoords;\n"
128 "\n"
129 "out float valid;\n"
130 "out vec2 sampledUvs;\n"
131 "out vec4 outPos;\n"
132 "\n"
133 "uniform mat4 transformationMatrix;\n"
134 "uniform mat4 projectionMatrix;\n"
135 "uniform mat4 cameraMatrix;\n"
136 "\n"
137 "uniform sampler2D uvsSampler;\n"
138 "uniform sampler2D positionsSampler;\n"
139 "\n"
140 "uniform float imageWidth;\n"
141 "uniform float imageHeight;\n"
142 "uniform float minDeltaZ;\n"
143 "\n"
144 "void main(void) {\n"
145 " float pixelWidth = 1.0 / imageWidth;\n"
146 " float pixelHeight = 1.0 / imageHeight;\n"
147 " vec2 tex = vec2(textureCoords.x, textureCoords.y);\n"
148 " vec4 pos = texture2D(positionsSampler, tex);\n"
149 " vec4 uvs = texture2D(uvsSampler, tex);\n"
150 "\n"
151 " vec2 tex_left = vec2(max(textureCoords.x - pixelWidth, 0.0), textureCoords.y);\n"
152 " vec2 tex_right = vec2(min(textureCoords.x + pixelWidth, 1.0), textureCoords.y);\n"
153 " vec2 tex_top = vec2(textureCoords.x, max(textureCoords.y - pixelHeight, 0.0));\n"
154 " vec2 tex_buttom = vec2(textureCoords.x, min(textureCoords.y + pixelHeight, 1.0));\n"
155 "\n"
156 " vec4 pos_left = texture2D(positionsSampler, tex_left);\n"
157 " vec4 pos_right = texture2D(positionsSampler, tex_right);\n"
158 " vec4 pos_top = texture2D(positionsSampler, tex_top);\n"
159 " vec4 pos_buttom = texture2D(positionsSampler, tex_buttom);\n"
160 "\n"
161 " valid = 0.0;\n"
162 " if (uvs.x < 0.0) valid = 1.0;\n"
163 " if (uvs.y < 0.0) valid = 1.0;\n"
164 " if (uvs.x >= 1.0) valid = 1.0;\n"
165 " if (uvs.y >= 1.0) valid = 1.0;\n"
166 " if (abs(pos_left.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
167 " if (abs(pos_right.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
168 " if (abs(pos_top.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
169 " if (abs(pos_buttom.z - pos.z) > minDeltaZ * pos.z) valid = 1.0;\n"
170 " if (abs(pos.z) < 0.01) valid = 1.0;\n"
171 " if (valid > 0.0) pos = vec4(1.0, 1.0, 1.0, 0.0);\n"
172 " else pos = vec4(pos.xyz, 1.0);\n"
173 " vec4 worldPosition = transformationMatrix * pos;\n"
174 " gl_Position = projectionMatrix * cameraMatrix * worldPosition;\n"
175 "\n"
176 " sampledUvs = uvs.xy;\n"
177 " outPos = pos;\n"
178 "}\n";
179 
180 static const char* fragment_shader_text_picking =
181 "#version 130\n"
182 "\n"
183 "in float valid;\n"
184 "in vec4 outPos;\n"
185 "in vec2 sampledUvs;\n"
186 "in vec3 normal;\n"
187 "out vec4 output_rgb;\n"
188 "out vec3 output_xyz;\n"
189 "\n"
190 "uniform sampler2D textureSampler;\n"
191 "uniform float pickedID;\n"
192 "uniform float shaded;\n"
193 "\n"
194 "void main(void) {\n"
195 " if (valid > 0.0) discard;\n"
196 " output_rgb = vec4(1.0);\n"
197 " output_xyz = outPos.xyz;\n"
198 "}\n";
199 
200 
201 
202 #define NORMAL_WINDOW_SIZE 3
203 
204 using namespace rs2;
205 
206 matrix4 frustum(float left, float right, float bottom, float top, float zNear, float zFar, float ox, float oy)
207 {
208  matrix4 res;
209  auto& m = res.mat;
210  float zDelta = (zFar-zNear);
211  float w = (right-left);
212  float h = (top-bottom);
213 
214  m[0][0]=2.0f*zNear/w;
215  m[0][1]=0.0f;
216  m[0][2]=2.0f*(right+left)/w;
217  m[0][3]=0.0f;
218 
219  m[1][0]=0.0f;
220  m[1][1]=2.0f*zNear/h;
221  m[1][2]=2.0f*(top+bottom)/h;
222  m[1][3]=0.0f;
223 
224  m[2][0]=ox;
225  m[2][1]=oy;
226  m[2][2]=-(zFar+zNear)/zDelta;
227  m[2][3]=-(2.f * zNear*zFar)/zDelta;
228 
229  m[3][0]=0.0f;
230  m[3][1]=0.0f;
231  m[3][2]=-1.0f;
232  m[3][3]=0.0f;
233 
234  return res;
235 }
236 
237 namespace librealsense
238 {
239  namespace gl
240  {
241  union Fp32
242  {
244  float f;
245  };
246 
248  {
249  /*
250  * https://gist.github.com/rygorous/2144712
251  * Public domain, by Fabian "ryg" Giesen
252  */
253  const Fp32 magic = { (254U - 15U) << 23 };
254  const Fp32 was_infnan = { (127U + 16U) << 23 };
255  Fp32 out;
256 
257  out.u = (value & 0x7FFFU) << 13; /* exponent/mantissa bits */
258  out.f *= magic.f; /* exponent adjust */
259  if (out.f >= was_infnan.f) /* make sure Inf/NaN survive */
260  {
261  out.u |= 255U << 23;
262  }
263  out.u |= (value & 0x8000U) << 16; /* sign bit */
264 
265  return out.f;
266  }
267 
268  pointcloud_shader::pointcloud_shader(std::unique_ptr<shader_program> shader)
269  : _shader(std::move(shader))
270  {
271  init();
272  }
273 
274  pointcloud_shader::pointcloud_shader(const char* vertex_shader, const char* fragment_shader)
275  {
276  _shader = shader_program::load(
277  vertex_shader,
278  fragment_shader,
279  "position", "textureCoords",
280  "output_rgb", "output_xyz");
281 
282  init();
283  }
284 
286  {
287  _transformation_matrix_location = _shader->get_uniform_location("transformationMatrix");
288  _projection_matrix_location = _shader->get_uniform_location("projectionMatrix");
289  _camera_matrix_location = _shader->get_uniform_location("cameraMatrix");
290 
291  _width_location = _shader->get_uniform_location("imageWidth");
292  _height_location = _shader->get_uniform_location("imageHeight");
293  _min_delta_z_location = _shader->get_uniform_location("minDeltaZ");
294  _picked_id_location = _shader->get_uniform_location("pickedID");
295  _shaded_location = _shader->get_uniform_location("shaded");
296 
297  auto texture0_sampler_location = _shader->get_uniform_location("textureSampler");
298  auto texture1_sampler_location = _shader->get_uniform_location("positionsSampler");
299  auto texture2_sampler_location = _shader->get_uniform_location("uvsSampler");
300 
301  _shader->begin();
302  _shader->load_uniform(_min_delta_z_location, 0.05f);
303  _shader->load_uniform(texture0_sampler_location, texture_slot());
304  _shader->load_uniform(texture1_sampler_location, geometry_slot());
305  _shader->load_uniform(texture2_sampler_location, uvs_slot());
306  _shader->end();
307  }
308 
309  void pointcloud_shader::begin() { _shader->begin(); }
310  void pointcloud_shader::end() { _shader->end(); }
311 
313  const matrix4& view,
314  const matrix4& projection)
315  {
316  _shader->load_uniform(_transformation_matrix_location, model);
317  _shader->load_uniform(_camera_matrix_location, view);
318  _shader->load_uniform(_projection_matrix_location, projection);
319  }
320 
322  {
323  _shader->load_uniform(_width_location, (float)width);
324  _shader->load_uniform(_height_location, (float)height);
325  }
326 
328  {
329  _shader->load_uniform(_picked_id_location, pid);
330  }
331 
333  {
334  _shader->load_uniform(_shaded_location, shaded);
335  }
336 
337  void pointcloud_shader::set_min_delta_z(float min_delta_z)
338  {
339  _shader->load_uniform(_min_delta_z_location, min_delta_z);
340  }
341 
343  {
344  glDeleteTextures(1, &color_tex);
345  glDeleteTextures(1, &xyz_tex);
346 
347  _origin_rgba_pbo.reset();
348  _rgba_pbo.reset();
349  _xyz_pbo.reset();
350 
351  _shader.reset();
352  _pick_shader.reset();
353  _model.reset();
354  _vertex_texture.reset();
355  _uvs_texture.reset();
356  _viz.reset();
357  _fbo.reset();
358  }
359 
361  {
362  perform_gl_action([&]()
363  {
364  cleanup_gpu_resources();
365  });
366  }
367 
369  {
370  if (glsl_enabled())
371  {
372  _shader = std::make_shared<pointcloud_shader>(vertex_shader_text, fragment_shader_text);
373  _pick_shader = std::make_shared<pointcloud_shader>(vertex_shader_text_picking, fragment_shader_text_picking);
374 
375  _vertex_texture = std::make_shared<rs2::texture_buffer>();
376  _uvs_texture = std::make_shared<rs2::texture_buffer>();
377 
378  obj_mesh mesh = make_grid(_width, _height);
379  _model = vao::create(mesh);
380 
381  _fbo = std::make_shared<fbo>(1, 1);
383 
384  _viz = std::make_shared<rs2::texture_visualizer>();
385 
386  glGenTextures(1, &color_tex);
387  glGenTextures(1, &xyz_tex);
388 
389  _xyz_pbo.init(NORMAL_WINDOW_SIZE, NORMAL_WINDOW_SIZE);
390  _rgba_pbo.init(1, 1);
391  _origin_rgba_pbo.init(1, 1);
392  }
393  }
394 
396  : stream_filter_processing_block("Pointcloud Renderer")
397  {
398  register_option(OPTION_FILLED, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 0, 1 }));
399  register_option(OPTION_SHADED, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 0, 1 }));
400  register_option(OPTION_MOUSE_X, std::make_shared<librealsense::float_option>(option_range{ 0, 10000, 0, 0 }));
401  register_option(OPTION_MOUSE_Y, std::make_shared<librealsense::float_option>(option_range{ 0, 10000, 0, 0 }));
402  register_option(OPTION_MOUSE_PICK, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 1, 0 }));
403 
404  register_option(OPTION_PICKED_X, std::make_shared<librealsense::float_option>(option_range{ -1000, 1000, 0, 0 }));
405  register_option(OPTION_PICKED_Y, std::make_shared<librealsense::float_option>(option_range{ -1000, 1000, 0, 0 }));
406  register_option(OPTION_PICKED_Z, std::make_shared<librealsense::float_option>(option_range{ -1000, 1000, 0, 0 }));
407  register_option(OPTION_PICKED_ID, std::make_shared<librealsense::float_option>(option_range{ 0, 32, 1, 0 }));
408 
409  register_option(OPTION_SELECTED, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 0, 1 }));
410  register_option(OPTION_ORIGIN_PICKED, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 0, 1 }));
411 
412  register_option(OPTION_NORMAL_X, std::make_shared<librealsense::float_option>(option_range{ -1.f, 1.f, 0, 0 }));
413  register_option(OPTION_NORMAL_Y, std::make_shared<librealsense::float_option>(option_range{ -1.f, 1.f, 0, 0 }));
414  register_option(OPTION_NORMAL_Z, std::make_shared<librealsense::float_option>(option_range{ -1.f, 1.f, 0, 0 }));
415 
416  register_option(OPTION_SCALE_FACTOR, std::make_shared<librealsense::float_option>(option_range{ 1, 4, 0, 1 }));
417 
433 
434  initialize();
435  }
436 
438  {
439  //scoped_timer t("pointcloud_renderer");
440  if (auto points = f.as<rs2::points>())
441  {
442  perform_gl_action([&]()
443  {
445  while (_durations.size())
446  {
447  auto front = _durations.front();
448  if (now - front > std::chrono::seconds(1))
449  _durations.pop_front();
450  else break;
451  }
452  _durations.push_back(now);
453 
454  //scoped_timer t("pointcloud_renderer.gl");
455 
456  GLint curr_tex;
458 
459  clear_gl_errors();
460 
461  auto vf_profile = f.get_profile().as<video_stream_profile>();
462  int width = vf_profile.width();
463  int height = vf_profile.height();
464 
468 
469 
470 
471  if (glsl_enabled())
472  {
473  if (_width != width || _height != height)
474  {
475  obj_mesh mesh = make_grid(width, height);
476  _model = vao::create(mesh);
477 
478  _width = width;
479  _height = height;
480  }
481 
482  auto points_f = (frame_interface*)points.get();
483 
484  uint32_t vertex_tex_id = 0;
485  uint32_t uv_tex_id = 0;
486 
487  bool error = false;
488 
489  if (auto g = dynamic_cast<gpu_points_frame*>(points_f))
490  {
491  if (!g->get_gpu_section().input_texture(0, &vertex_tex_id) ||
492  !g->get_gpu_section().input_texture(1, &uv_tex_id)
493  ) error = true;
494  }
495  else
496  {
498  vertex_tex_id = _vertex_texture->get_gl_handle();
499 
501  uv_tex_id = _uvs_texture->get_gl_handle();
502  }
503 
504  if (!error)
505  {
506  auto render_pc = [this, &vf_profile, curr_tex, vertex_tex_id, uv_tex_id]
507  (std::shared_ptr<pointcloud_shader> shader, const rs2::matrix4& p){
508  shader->begin();
509  shader->set_mvp(get_matrix(
512  p
513  );
514  shader->set_image_size(vf_profile.width(), vf_profile.height());
515 
516  shader->set_picked_id(_picked_id_opt->query());
517  shader->set_shaded(_shaded_opt->query());
518 
519  glActiveTexture(GL_TEXTURE0 + shader->texture_slot());
520  glBindTexture(GL_TEXTURE_2D, curr_tex);
521 
522  glActiveTexture(GL_TEXTURE0 + shader->geometry_slot());
523  glBindTexture(GL_TEXTURE_2D, vertex_tex_id);
524 
525  glActiveTexture(GL_TEXTURE0 + shader->uvs_slot());
526  glBindTexture(GL_TEXTURE_2D, uv_tex_id);
527 
528  if (_filled_opt->query() > 0.f) _model->draw();
529  else _model->draw_points();
530 
531  glActiveTexture(GL_TEXTURE0 + shader->texture_slot());
532 
533  shader->end();
534  };
535 
536  auto mouse_pick = [this, &render_pc](rs2::float2 xy, rs2::float2 wh, pbo<rgba8>& pbo,
537  bool fetch_xyz, rs2::float3* xyz, rs2::float3* normal)
538  {
540 
541  auto near_plane = gl_p(2,3)/(gl_p(2,2)-1);
542  auto far_plae = gl_p(2,3)/(gl_p(2,2)+1);
543  auto bottom = near_plane * (gl_p(1,2)-1)/gl_p(1,1);
544  auto top = near_plane * (gl_p(1,2)+1)/gl_p(1,1);
545  auto left = near_plane * (gl_p(0,2)-1)/gl_p(0,0);
546  auto right = near_plane * (gl_p(0,2)+1)/gl_p(0,0);
547  auto ox = 0.f;
548  auto oy = 0.f;
549 
550  ox = (xy.x / wh.x) * 2 - 1;
551  oy = (xy.y / wh.y) * 2 - 1;
552 
553  auto p = frustum(left/(0.5f*wh.x), right/(0.5f*wh.x),
554  bottom / (0.5f * wh.y), top / (0.5f * wh.y), near_plane, far_plae,
555  ox * (0.5f * wh.x), oy * (0.5f * wh.y));
556 
557  auto fbo_width = 3;
558  auto fbo_height = 3;
559 
560  _fbo->set_dims(fbo_width, fbo_height);
561 
564 
565  _fbo->createTextureAttachment(color_tex);
566 
570  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, fbo_width, fbo_height, 0, GL_RGBA, GL_HALF_FLOAT, nullptr);
573 
575 
577 
578  _fbo->bind();
579 
581  glDrawBuffers(2, attachments);
582 
583  glClearColor(0, 0, 0, 0);
585 
586  render_pc(_pick_shader, p);
587 
588  _fbo->unbind();
589 
591  check_gl_error();
592 
594  check_gl_error();
595 
596  rgba8 rgba { 0, 0, 0, 0 };
597 
598  {
599  //scoped_timer t("rgba");
600  pbo.query(&rgba, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE);
601  }
602 
604  auto center = size / 2;
605  const auto half_window = NORMAL_WINDOW_SIZE / 2;
606 
607  std::vector<half4> pos_halfs(size);
608 
609  if (fetch_xyz)
610  {
611  glReadBuffer(GL_COLOR_ATTACHMENT1);
612  check_gl_error();
613 
614  //scoped_timer t("xyz");
615  _xyz_pbo.query(pos_halfs.data(), 0, 0,
618 
619  if (rgba.a > 0)
620  {
621  std::vector<rs2::float3> pos_floats(size);
622  for (int i = 0; i < size; i++)
623  {
624  auto pos = pos_halfs[i];
625  auto x1 = halfToNativeIeee(pos.x);
626  auto y1 = halfToNativeIeee(pos.y);
627  auto z1 = halfToNativeIeee(pos.z);
628  pos_floats[i] = { x1, y1, z1 };
629  }
630 
631  auto up = pos_floats[center - half_window * NORMAL_WINDOW_SIZE];
632  auto down = pos_floats[center + half_window * NORMAL_WINDOW_SIZE];
633  auto left = pos_floats[center - half_window];
634  auto right = pos_floats[center + half_window];
635  auto pos = pos_floats[center];
636 
637  auto right_left = lerp(pos - left, right - pos, 0.5f);
638  auto down_up = lerp(pos - up, down - pos, 0.5f) * (-1.f);
639  *normal = cross(right_left, down_up).normalize();
640  *xyz = pos;
641  }
642  }
643 
646 
647  return (rgba.a > 0);
648  };
649 
650  int32_t vp[4];
652  check_gl_error();
653 
654  float scale = _scale_factor_opt->query();
655 
656  auto fbo_width = vp[2] / scale;
657  auto fbo_height = vp[3] / scale;
658  const auto viewport_x = vp[0] / scale;
659  const auto viewport_y = vp[1] / scale;
660 
661  const auto do_mouse_pick = _mouse_pick_opt->query() > 0.f;
662 
663  _picked_id_opt->set(0.f);
664 
665  if (do_mouse_pick) {
666 
667  auto x = _mouse_x_opt->query() - viewport_x;
668  auto y = _mouse_y_opt->query() - viewport_y;
669 
671 
672  if (mouse_pick(rs2::float2{ x, y },
673  rs2::float2{ fbo_width, fbo_height },
674  _rgba_pbo, true, &pos, &normal))
675  {
676  _normal_x_opt->set(normal.x);
677  _normal_y_opt->set(normal.y);
678  _normal_z_opt->set(normal.z);
679 
680  _picked_x_opt->set(pos.x);
681  _picked_y_opt->set(pos.y);
682  _picked_z_opt->set(pos.z);
683 
684  _picked_id_opt->set(1.f);
685  }
686 
687  _mouse_pick_opt->set(0.f);
688 
689  glActiveTexture(GL_TEXTURE0 + _shader->texture_slot());
690 
691  rs2::float3 org{ 0.f, 0.f, 0.f };
692  rs2::float2 origin = translate_3d_to_2d(org, p, v, f, vp);
693  origin.x = origin.x - vp[0];
694  origin.y = origin.y - vp[1];
695  _origin_picked_opt->set(0.f);
696 
697  if (mouse_pick(origin, rs2::float2{ float(vp[2]), float(vp[3]) },
698  _origin_rgba_pbo, false, nullptr, nullptr))
699  {
700  _origin_picked_opt->set(1.f);
701  }
702  }
703 
705  }
706  }
707  else
708  {
710  glPushMatrix();
711 
714 
715  glLoadMatrixf(v * t);
716 
717  auto vertices = points.get_vertices();
718  auto tex_coords = points.get_texture_coordinates();
719 
720  glBindTexture(GL_TEXTURE_2D, curr_tex);
721 
722  if (_filled_opt->query() > 0.f)
723  {
724  glBegin(GL_QUADS);
725 
726  const auto threshold = 0.05f;
727  for (int x = 0; x < width - 1; ++x) {
728  for (int y = 0; y < height - 1; ++y) {
729  auto a = y * width + x, b = y * width + x + 1, c = (y + 1)*width + x, d = (y + 1)*width + x + 1;
730  if (vertices[a].z && vertices[b].z && vertices[c].z && vertices[d].z
731  && abs(vertices[a].z - vertices[b].z) < threshold && abs(vertices[a].z - vertices[c].z) < threshold
732  && abs(vertices[b].z - vertices[d].z) < threshold && abs(vertices[c].z - vertices[d].z) < threshold) {
733  glVertex3fv(vertices[a]); glTexCoord2fv(tex_coords[a]);
734  glVertex3fv(vertices[b]); glTexCoord2fv(tex_coords[b]);
735  glVertex3fv(vertices[d]); glTexCoord2fv(tex_coords[d]);
736  glVertex3fv(vertices[c]); glTexCoord2fv(tex_coords[c]);
737  }
738  }
739  }
740  glEnd();
741  }
742  else
743  {
745  for (int i = 0; i < points.size(); i++)
746  {
747  if (vertices[i].z)
748  {
750  glTexCoord2fv(tex_coords[i + 1]);
751  }
752  }
753  glEnd();
754  }
755 
756  glPopMatrix();
757  }
758  });
759  }
760 
761  return f;
762  }
763  }
764 }
static const char * fragment_shader_text
Definition: pc-shader.cpp:73
float3 cross(const float3 &a, const float3 &b)
Definition: rendering.h:153
float lerp(float a, float b, float t)
Definition: rendering.h:122
#define GL_TEXTURE_MAG_FILTER
obj_mesh make_grid(int a, int b)
Definition: opengl3.h:272
GLboolean GLboolean GLboolean b
GLboolean GLboolean g
GLint y
float3 normalize() const
Definition: rendering.h:142
static const auto OPTION_ORIGIN_PICKED
Definition: pc-shader.h:81
#define glFramebufferTexture2D
GLdouble GLdouble GLdouble top
#define GL_TEXTURE_2D
#define GL_RGBA
pointcloud_shader(const char *vertex_shader, const char *fragment_shader)
Definition: pc-shader.cpp:274
virtual void set(float value)=0
float2 * get_texture_coordinates()
Definition: archive.cpp:149
#define glBegin
GLfloat GLfloat p
Definition: glext.h:12687
const GLfloat * m
Definition: glext.h:6814
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:10806
#define GL_UNSIGNED_BYTE
#define glGetIntegerv
std::shared_ptr< pointcloud_shader > _shader
Definition: pc-shader.h:95
#define GL_TEXTURE_BINDING_2D
#define glPopMatrix
GLfloat value
stream_profile get_profile() const
Definition: rs_frame.hpp:557
#define glDrawBuffers
matrix4 frustum(float left, float right, float bottom, float top, float zNear, float zFar, float ox, float oy)
Definition: pc-shader.cpp:206
#define glTexCoord2fv
#define glVertex3fv
std::shared_ptr< rs2::texture_buffer > _vertex_texture
Definition: pc-shader.h:98
unsigned short uint16_t
Definition: stdint.h:79
Definition: cah-model.h:10
GLdouble GLdouble GLdouble w
d
Definition: rmse.py:171
#define glReadBuffer
static const char * vertex_shader_text_picking
Definition: pc-shader.cpp:123
rs2::frame process_frame(const rs2::frame_source &source, const rs2::frame &f) override
Definition: pc-shader.cpp:437
GLenum src
Definition: glext.h:1751
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
GLdouble GLdouble z
#define glTexImage2D
normal
Definition: rmse.py:164
GLdouble GLdouble GLdouble GLdouble GLdouble zFar
option & get_option(rs2_option id) override
Definition: options.h:58
virtual float query() const =0
GLdouble GLdouble GLdouble GLdouble zNear
void register_option(rs2_option id, std::shared_ptr< option > option)
Definition: options.h:86
#define glActiveTexture
GLdouble t
GLboolean GLboolean GLboolean GLboolean a
GLdouble f
#define GL_RGBA16F
#define GL_TEXTURE0
#define GL_NONE
#define glGenTextures
GLsizeiptr size
GLsizei const GLenum * attachments
Definition: glext.h:2477
#define GL_COLOR_BUFFER_BIT
#define GL_VIEWPORT
#define glBindFramebuffer
#define glTexParameteri
const GLubyte * c
Definition: glext.h:12690
std::shared_ptr< pointcloud_shader > _pick_shader
Definition: pc-shader.h:96
int GLint
#define GL_COLOR_ATTACHMENT1
#define NORMAL_WINDOW_SIZE
Definition: pc-shader.cpp:202
GLdouble x
unsigned int uint32_t
Definition: stdint.h:80
#define glPushMatrix
#define glLoadMatrixf
#define GL_UNPACK_ALIGNMENT
float3 * get_vertices()
Definition: archive.cpp:26
#define glPixelStorei
#define glClear
std::shared_ptr< rs2::vao > _model
Definition: pc-shader.h:97
GLint GLsizei GLsizei height
std::shared_ptr< rs2::fbo > _fbo
Definition: pc-shader.h:101
#define GL_TEXTURE_MIN_FILTER
#define GL_QUADS
#define glEnd
void set_image_size(int width, int height)
Definition: pc-shader.cpp:321
GLint left
Definition: glext.h:1963
std::shared_ptr< rs2::texture_buffer > _uvs_texture
Definition: pc-shader.h:99
float mat[4][4]
Definition: rendering.h:274
void set_min_delta_z(float min_delta_z)
Definition: pc-shader.cpp:337
#define GL_DEPTH_BUFFER_BIT
#define GL_MODELVIEW
#define glDeleteTextures
GLuint GLfloat GLfloat GLfloat x1
Definition: glext.h:9721
static const struct @18 vertices[3]
unsigned int GLuint
GLdouble right
#define GL_READ_FRAMEBUFFER
static GLuint mesh
Definition: heightmap.c:113
#define glBindTexture
static const auto OPTION_SCALE_FACTOR
Definition: pc-shader.h:87
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
#define GL_HALF_FLOAT
void clear_gl_errors()
Definition: opengl3.cpp:722
#define glClearColor
xyz
Definition: rmse.py:152
float rs2_vector::* pos
static const auto OPTION_PICKED_ID
Definition: pc-shader.h:78
#define GL_COLOR_ATTACHMENT0
int i
GLuint res
Definition: glext.h:8856
#define GL_PACK_ALIGNMENT
GLuint shader
float2 translate_3d_to_2d(float3 point, matrix4 p, matrix4 v, matrix4 f, int32_t vp[4])
Definition: rendering.h:1650
std::deque< std::chrono::high_resolution_clock::time_point > _durations
Definition: pc-shader.h:114
signed int int32_t
Definition: stdint.h:77
#define glDrawBuffer
static const char * vertex_shader_text
Definition: pc-shader.cpp:11
const rs2::matrix4 & get_matrix(rs2_gl_matrix_type type) const
#define check_gl_error()
Definition: opengl3.h:17
static const auto OPTION_MOUSE_PICK
Definition: pc-shader.h:72
#define GL_NEAREST
void set_mvp(const rs2::matrix4 &model, const rs2::matrix4 &view, const rs2::matrix4 &projection)
Definition: pc-shader.cpp:312
GLdouble v
static const char * fragment_shader_text_picking
Definition: pc-shader.cpp:180
#define GL_POINTS
GLdouble y1
#define glMatrixMode
#define GL_FRAMEBUFFER
GLint GLsizei width
float halfToNativeIeee(uint16_t value)
Definition: pc-shader.cpp:247
T as() const
Definition: rs_frame.hpp:580
GLdouble GLdouble bottom
std::unique_ptr< rs2::shader_program > _shader
Definition: pc-shader.h:44


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39