4 #include "../include/librealsense2/rs.hpp" 5 #include "../include/librealsense2/rsutil.h" 26 "in vec2 textCoords;\n" 27 "out vec4 output_xyz;\n" 28 "out vec4 output_uv;\n" 29 "uniform sampler2D textureSampler;\n" 30 "uniform float opacity;\n" 31 "uniform mat4 extrinsics;\n" 33 "uniform vec2 focal1;\n" 34 "uniform vec2 principal1;\n" 35 "uniform float is_bc1;\n" 36 "uniform float coeffs1[5];\n" 38 "uniform vec2 focal2;\n" 39 "uniform vec2 principal2;\n" 40 "uniform float is_bc2;\n" 41 "uniform float coeffs2[5];\n" 43 "uniform float depth_scale;\n" 44 "uniform float width1;\n" 45 "uniform float height1;\n" 46 "uniform float width2;\n" 47 "uniform float height2;\n" 49 "uniform float needs_projection;\n" 52 " float px = textCoords.x * width1;\n" 53 " float py = (1.0 - textCoords.y) * height1;\n" 54 " float x = (px - principal1.x) / focal1.x;\n" 55 " float y = (py - principal1.y) / focal1.y;\n" 58 " if(is_bc1 == 2.0)\n" 60 " for (int i = 0; i < 10; i++)\n" 62 " float r2 = x * x + y * y;\n" 63 " float icdist = 1.0 / (1.0 + ((coeffs1[4] * r2 + coeffs1[1])*r2 + coeffs1[0])*r2);\n" 64 " float xq = x / icdist;\n" 65 " float yq = y / icdist;\n" 66 " float delta_x = 2 * coeffs1[2] * xq*yq + coeffs1[3] * (r2 + 2 * xq*xq);\n" 67 " float delta_y = 2 * coeffs1[3] * xq*yq + coeffs1[2] * (r2 + 2 * yq*yq);\n" 68 " x = (xo - delta_x)*icdist;\n" 69 " y = (yo - delta_y)*icdist;\n" 72 " if (is_bc1 == 4.0)\n" 74 " for (int i = 0; i < 10; i++)\n" 76 " float r2 = x * x + y * y;\n" 77 " float icdist = 1.0 / (1.0 + ((coeffs1[4] * r2 + coeffs1[1])*r2 + coeffs1[0])*r2);\n" 78 " float delta_x = 2 * coeffs1[2] * x*y + coeffs1[3] * (r2 + 2 * x*x);\n" 79 " float delta_y = 2 * coeffs1[3] * x*y + coeffs1[2] * (r2 + 2 * y*y);\n" 80 " x = (xo - delta_x)*icdist;\n" 81 " y = (yo - delta_y)*icdist;\n" 84 " vec2 tex = vec2(textCoords.x, 1.0 - textCoords.y);\n" 85 " vec4 dp = texture(textureSampler, tex);\n" 86 " float nd = (dp.x + dp.y * 256.0) * 256.0;\n" 87 " float depth = depth_scale * nd;\n" 88 " vec4 xyz = vec4(x * depth, y * depth, depth, 1.0);\n" 89 " output_xyz = xyz;\n" 91 " if (needs_projection > 0) {" 92 " vec4 trans = extrinsics * xyz;\n" 93 " x = trans.x / trans.z;\n" 94 " y = trans.y / trans.z;\n" 96 " if(is_bc2 == 2.0)\n" 98 " float r2 = x*x + y*y;\n" 99 " float f = 1.0 + coeffs2[0]*r2 + coeffs2[1]*r2*r2 + coeffs2[4]*r2*r2*r2;\n" 102 " float dx = x + 2.0*coeffs2[2]*x*y + coeffs2[3]*(r2 + 2.0*x*x);\n" 103 " float dy = y + 2.0*coeffs2[3]*x*y + coeffs2[2]*(r2 + 2.0*y*y);\n" 107 " if (is_bc2 == 4.0)\n" 109 " float r2 = x * x + y * y;\n" 110 " float f = 1 + coeffs2[0] * r2 + coeffs2[1] * r2*r2 + coeffs2[4] * r2*r2*r2;\n" 111 " float xf = x * f;\n" 112 " float yf = y * f;\n" 113 " float dx = xf + 2 * coeffs2[2] * x*y + coeffs2[3] * (r2 + 2 * x*x);\n" 114 " float dy = yf + 2 * coeffs2[3] * x*y + coeffs2[2] * (r2 + 2 * y*y);\n" 118 " // TODO: Enable F-Thetha\n" 119 " //if (intrin->model == RS2_DISTORTION_FTHETA)\n" 121 " // float r = sqrtf(x*x + y*y);\n" 122 " // float rd = (float)(1.0f / intrin->coeffs[0] * atan(2 * r* tan(intrin->coeffs[0] / 2.0f)));\n" 127 " float u = (x * focal2.x + principal2.x) / width2;\n" 128 " float v = (y * focal2.y + principal2.y) / height2;\n" 129 " output_uv = vec4(u, v, 0.0, 1.0);\n" 131 " output_uv = vec4(textCoords.x, 1.0 - textCoords.y, 0.0, 1.0);\n" 137 "attribute vec3 position;\n" 138 "attribute vec2 textureCoords;\n" 139 "varying vec2 textCoords;\n" 140 "varying vec2 occuTextureCoords[10];\n" 141 "uniform vec2 elementPosition;\n" 142 "uniform vec2 elementScale;\n" 143 "uniform float width;\n" 144 "uniform float height;\n" 145 "uniform int vscan;\n" 148 " gl_Position = vec4(position * vec3(elementScale, 1.0) + vec3(elementPosition, 0.0), 1.0);\n" 149 " textCoords = textureCoords;\n" 150 " float pixelsize = 1.0 / width;\n" 151 " float shift = 0.0;\n" 152 " for (int i = 0; i < 10; i++)\n" 156 " occuTextureCoords[i] = textureCoords + vec2(0.0, shift);\n" 157 " pixelsize = 2.0 / height;\n" 159 " occuTextureCoords[i] = textureCoords - vec2(shift, 0.0);\n" 160 " pixelsize = 2.0 / width;\n" 162 " shift += pixelsize;\n" 168 "varying vec2 textCoords;\n" 169 "varying vec2 occuTextureCoords[10];\n" 170 "out vec4 texture_xyz;\n" 171 "out vec4 texture_uv;\n" 172 "uniform sampler2D xyzSampler;\n" 173 "uniform sampler2D uvSampler;\n" 174 "uniform float opacity;\n" 175 "uniform int vscan;\n" 176 "void main(void) {\n" 179 " float uvmax = 0.0;\n" 182 " for (int i = 0; i < 10; i++)\n" 184 " vec2 tex = vec2(occuTextureCoords[i].x, 1.0 - occuTextureCoords[i].y);\n" 185 " xyz[i] = texture2D(xyzSampler, tex);\n" 186 " uv[i] = texture2D(uvSampler, tex);\n" 187 " if (uv[i].y > uvmax && xyz[i].z > 0.0)\n" 189 " uvmax = uv[i].y;\n" 192 " if (xyz[0].z > 0.0)\n" 194 " if (uv[0].y < uvmax)\n" 196 " texture_xyz = vec4(0.0, 0.0, 0.0, 1.0);\n" 197 " texture_uv = vec4(0.0, 0.0, 0.0, 1.0);\n" 199 " texture_xyz = xyz[0];\n" 200 " texture_uv = uv[0];\n" 204 " texture_xyz = xyz[0];\n" 205 " texture_uv = uv[0];\n" 208 " for (int i = 0; i < 10; i++)\n" 210 " vec2 tex = vec2(occuTextureCoords[i].x, 1.0 - occuTextureCoords[i].y);\n" 211 " xyz[i] = texture2D(xyzSampler, tex);\n" 212 " uv[i] = texture2D(uvSampler, tex);\n" 213 " if (uv[i].x > uvmax && xyz[i].z > 0.0)\n" 215 " uvmax = uv[i].x;\n" 218 " if (xyz[0].z > 0.0)\n" 220 " if (uv[0].x < uvmax)\n" 222 " texture_xyz = vec4(0.0, 0.0, 0.0, 1.0);\n" 223 " texture_uv = vec4(0.0, 0.0, 0.0, 1.0);\n" 225 " texture_xyz = xyz[0];\n" 226 " texture_uv = uv[0];\n" 230 " texture_xyz = xyz[0];\n" 231 " texture_uv = uv[0];\n" 243 "position",
"textureCoords",
244 "output_xyz",
"output_uv"))
246 _focal_location[0] = _shader->get_uniform_location(
"focal1");
247 _principal_location[0] = _shader->get_uniform_location(
"principal1");
248 _is_bc_location[0] = _shader->get_uniform_location(
"is_bc1");
249 _coeffs_location[0] = _shader->get_uniform_location(
"coeffs1");
251 _focal_location[1] = _shader->get_uniform_location(
"focal2");
252 _principal_location[1] = _shader->get_uniform_location(
"principal2");
253 _is_bc_location[1] = _shader->get_uniform_location(
"is_bc2");
254 _coeffs_location[1] = _shader->get_uniform_location(
"coeffs2");
256 _depth_scale_location = _shader->get_uniform_location(
"depth_scale");
257 _width_location[0] = _shader->get_uniform_location(
"width1");
258 _height_location[0] = _shader->get_uniform_location(
"height1");
259 _width_location[1] = _shader->get_uniform_location(
"width2");
260 _height_location[1] = _shader->get_uniform_location(
"height2");
261 _extrinsics_location = _shader->get_uniform_location(
"extrinsics");
263 _requires_projection_location = _shader->get_uniform_location(
"needs_projection");
268 _shader->load_uniform(_requires_projection_location, val ? 1.
f : 0.
f);
273 _shader->load_uniform(_width_location[
id], (
float)w);
274 _shader->load_uniform(_height_location[
id], (
float)h);
281 _shader->load_uniform(_focal_location[idx],
focal);
282 _shader->load_uniform(_principal_location[idx], principal);
283 _shader->load_uniform(_is_bc_location[idx], (
float)(
int)intr.
model);
289 _shader->load_uniform(_depth_scale_location, depth_scale);
295 for (
int i = 0;
i < 4;
i++)
296 for (
int j = 0;
j < 4;
j++)
300 else if (
i < 3 &&
j == 3) m.
mat[
i][
j] = 0.f;
301 else m.
mat[
i][
j] = 1.f;
303 _shader->load_uniform(_extrinsics_location, m);
327 "position",
"textureCoords",
328 "texture_xyz",
"texture_uv"))
330 _width_location = _shader->get_uniform_location(
"width");
331 _height_location = _shader->get_uniform_location(
"height");
333 _xyz_sampler_location = _shader->get_uniform_location(
"xyzSampler");
334 _uv_sampler_location = _shader->get_uniform_location(
"uvSampler");
336 _scanning_location = _shader->get_uniform_location(
"vscan");
341 _shader->load_uniform(_width_location, width);
346 _shader->load_uniform(_height_location, height);
351 _shader->load_uniform(_xyz_sampler_location, xyz);
356 _shader->load_uniform(_uv_sampler_location, uv);
361 _shader->load_uniform(_scanning_location, mode);
373 void pointcloud_gl::cleanup_gpu_resources()
375 _projection_renderer.reset();
376 _occu_renderer.reset();
379 void pointcloud_gl::create_gpu_resources()
383 _projection_renderer = std::make_shared<visualizer_2d>(std::make_shared<project_shader>());
384 _occu_renderer = std::make_shared<visualizer_2d>(std::make_shared<occulution_shader>());
386 _enabled = glsl_enabled() ? 1 : 0;
389 pointcloud_gl::~pointcloud_gl()
391 perform_gl_action([&]()
393 cleanup_gpu_resources();
397 pointcloud_gl::pointcloud_gl()
402 auto opt = std::make_shared<librealsense::ptr_option<int>>(
403 0, 1, 0, 1, &
_enabled,
"GLSL enabled");
428 const unsigned int width,
429 const unsigned int height,
444 depth_texture = input_frame.get_texture_id(0);
460 gf->get_gpu_section().output_texture(0, &output_xyz,
TEXTYPE_XYZ);
461 gf->get_gpu_section().output_texture(1, &output_uv,
TEXTYPE_UV);
472 if (run_glsl_occlusion_removal)
479 fbo1_xyz = output_xyz;
484 fbo fbo1(width, height);
504 gf->get_gpu_section().set_size(width, height);
522 shader.set_intrinsics(1, other_intrinsics);
523 shader.set_extrinsics(extr);
524 shader.set_size(0, width, height);
527 viz->draw_texture(depth_texture);
533 if (run_glsl_occlusion_removal)
537 fbo fbo2(width, height);
562 gf->get_gpu_section().set_size(width, height);
570 occu_shader.set_width((
float) width);
571 occu_shader.set_height((
float) height);
573 occu_shader.set_xyz_sampler(0);
574 occu_shader.set_uv_sampler(1);
579 occu_shader.set_scanning(1);
584 occu_shader.set_scanning(0);
587 oviz->draw_texture(fbo1_xyz, fbo1_uv);
std::shared_ptr< rs2::visualizer_2d > _projection_renderer
#define GL_TEXTURE_MAG_FILTER
std::shared_ptr< occlusion_filter > _occlusion_filter
void set_xyz_sampler(int xyz)
uint32_t _xyz_sampler_location
void set_height(float height)
rs2::stream_profile _output_stream
void requires_projection(bool val)
#define glFramebufferTexture2D
void add_extension(rs2_extension ex)
void set_intrinsics(int idx, const rs2_intrinsics &intr)
uint32_t _height_location
const void * get_data() const
GLdouble GLdouble GLdouble w
rs2_intrinsics _depth_intr
GLfloat GLfloat GLfloat GLfloat h
const float3 * depth_to_points(rs2::points output, const rs2_intrinsics &depth_intrinsics, const rs2::depth_frame &depth_frame, float depth_scale) override
rs2_extrinsics identity_matrix()
#define GL_TEXTURE_WRAP_T
void register_option(rs2_option id, std::shared_ptr< option > option)
void set_size(int id, int w, int h)
void get_texture_map(rs2::points output, const float3 *points, const unsigned int width, const unsigned int height, const rs2_intrinsics &other_intrinsics, const rs2_extrinsics &extr, float2 *pixels_ptr) override
GLsizei const GLenum * attachments
#define GL_COLOR_BUFFER_BIT
#define GL_COLOR_ATTACHMENT1
GLint GLsizei GLsizei height
static const char * occulution_fragment_text
#define GL_TEXTURE_MIN_FILTER
rs2::depth_frame _depth_data
void set_uv_sampler(int uv)
void set_scanning(int mode)
rs2::points allocate_points(const rs2::frame_source &source, const rs2::frame &f) override
void set_extrinsics(const rs2_extrinsics &extr)
librealsense::stream_profile_interface * profile
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
#define RS2_EXTENSION_VIDEO_FRAME_GL
uint32_t _requires_projection_location
const rs2_stream_profile * get() const
static const char * project_fragment_text
void perform_gl_action(T action, S fallback)
synthetic_source _source_wrapper
GLsizei GLsizei GLchar * source
#define GL_COLOR_ATTACHMENT0
uint32_t _scanning_location
frame_interface * allocate_points(std::shared_ptr< stream_profile_interface > stream, frame_interface *original, rs2_extension frame_type=RS2_EXTENSION_POINTS) override
static const char * occulution_vertex_shader_text
std::shared_ptr< rs2::visualizer_2d > _occu_renderer
void set_depth_scale(float depth_scale)
#define GL_TEXTURE_WRAP_S
uint32_t _depth_scale_location
void set_width(float width)
struct rs2_frame rs2_frame
bool run__occlusion_filter(const rs2_extrinsics &extr) override
uint32_t _extrinsics_location
uint32_t _uv_sampler_location