56 const std::string& render_vertex_shader,
57 const std::string& render_fragment_shader,
58 const std::string& filter_vertex_shader,
59 const std::string& filter_fragment_shader)
60 : sensor_parameters_(sensor_parameters.clone())
61 , next_handle_(FIRST_LABEL)
62 , min_handle_(FIRST_LABEL)
64 , transform_callback_(transform_callback)
66 , padding_offset_(0.01)
67 , shadow_threshold_(0.5)
70 std::thread([
this, render_vertex_shader, render_fragment_shader, filter_vertex_shader, filter_fragment_shader] {
71 run(render_vertex_shader, render_fragment_shader, filter_vertex_shader, filter_fragment_shader);
76 const std::string& render_fragment_shader,
77 const std::string& filter_vertex_shader,
78 const std::string& filter_fragment_shader)
80 mesh_renderer_ = std::make_shared<GLRenderer>(sensor_parameters_->getWidth(), sensor_parameters_->getHeight(),
81 sensor_parameters_->getNearClippingPlaneDistance(),
82 sensor_parameters_->getFarClippingPlaneDistance());
83 depth_filter_ = std::make_shared<GLRenderer>(sensor_parameters_->getWidth(), sensor_parameters_->getHeight(),
84 sensor_parameters_->getNearClippingPlaneDistance(),
85 sensor_parameters_->getFarClippingPlaneDistance());
87 mesh_renderer_->setShadersFromString(render_vertex_shader, render_fragment_shader);
88 depth_filter_->setShadersFromString(filter_vertex_shader, filter_fragment_shader);
90 depth_filter_->begin();
92 glGenTextures(1, &sensor_depth_texture_);
94 glUniform1i(glGetUniformLocation(depth_filter_->getProgramID(),
"sensor"), 0);
95 glUniform1i(glGetUniformLocation(depth_filter_->getProgramID(),
"depth"), 2);
96 glUniform1i(glGetUniformLocation(depth_filter_->getProgramID(),
"label"), 4);
98 shadow_threshold_location_ = glGetUniformLocation(depth_filter_->getProgramID(),
"shadow_threshold");
100 depth_filter_->end();
102 canvas_ = glGenLists(1);
103 glNewList(canvas_, GL_COMPILE);
108 glVertex3f(-1, -1, 1);
111 glVertex3f(1, -1, 1);
117 glVertex3f(-1, 1, 1);
159 mesh_renderer_->setBufferSize(width, height);
160 mesh_renderer_->setCameraParameters(width, width, width >> 1, height >> 1);
162 depth_filter_->setBufferSize(width, height);
163 depth_filter_->setCameraParameters(width, width, width >> 1, height >> 1);
270 const std::string& render_fragment_shader,
271 const std::string& filter_vertex_shader,
272 const std::string& filter_fragment_shader)
274 initialize(render_vertex_shader, render_fragment_shader, filter_vertex_shader, filter_fragment_shader);
278 std::unique_lock<std::mutex> lock(jobs_mutex_);
280 if (jobs_queue_.empty())
281 jobs_condition_.wait(lock);
283 if (!jobs_queue_.empty())
285 JobPtr job = jobs_queue_.front();
297 if (type != GL_FLOAT && type != GL_UNSIGNED_SHORT)
299 std::stringstream msg;
300 msg <<
"unknown type \"" << type <<
"\". Allowed values are GL_FLOAT or GL_UNSIGNED_SHORT.";
301 throw std::runtime_error(msg.str());
304 JobPtr job = std::make_shared<FilterJob<void>>([
this, sensor_data, type] { doFilter(sensor_data, type); });
312 std::unique_lock<std::mutex> _(transform_callback_mutex_);
314 mesh_renderer_->begin();
315 sensor_parameters_->setRenderParameters(*mesh_renderer_);
317 glEnable(GL_TEXTURE_2D);
318 glEnable(GL_DEPTH_TEST);
319 glDepthFunc(GL_LESS);
320 glEnable(GL_CULL_FACE);
321 glCullFace(GL_FRONT);
322 glDisable(GL_ALPHA_TEST);
325 GLuint padding_coefficients_id = glGetUniformLocation(mesh_renderer_->getProgramID(),
"padding_coefficients");
326 Eigen::Vector3f padding_coefficients =
327 sensor_parameters_->getPaddingCoefficients() * padding_scale_ + Eigen::Vector3f(0, 0, padding_offset_);
328 glUniform3f(padding_coefficients_id, padding_coefficients[0], padding_coefficients[1], padding_coefficients[2]);
330 Eigen::Isometry3d transform;
331 for (
const std::pair<const MeshHandle, GLMeshPtr>& mesh : meshes_)
333 if (transform_callback_(mesh.first, transform))
334 mesh.second->render(transform);
337 mesh_renderer_->end();
342 depth_filter_->begin();
343 sensor_parameters_->setFilterParameters(*depth_filter_);
344 glEnable(GL_TEXTURE_2D);
345 glEnable(GL_DEPTH_TEST);
346 glDepthFunc(GL_ALWAYS);
347 glDisable(GL_CULL_FACE);
348 glDisable(GL_ALPHA_TEST);
353 glUniform1f(shadow_threshold_location_, shadow_threshold_);
355 GLuint depth_texture = mesh_renderer_->getDepthTexture();
356 GLuint color_texture = mesh_renderer_->getColorTexture();
359 glActiveTexture(GL_TEXTURE0);
360 glBindTexture(GL_TEXTURE_2D, sensor_depth_texture_);
363 1.0 / (sensor_parameters_->getFarClippingPlaneDistance() - sensor_parameters_->getNearClippingPlaneDistance());
365 if (encoding == GL_UNSIGNED_SHORT)
372 glPixelTransferf(GL_DEPTH_SCALE, scale * 65.535);
376 glPixelTransferf(GL_DEPTH_SCALE, scale);
378 glPixelTransferf(GL_DEPTH_BIAS, -scale * sensor_parameters_->getNearClippingPlaneDistance());
380 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, sensor_parameters_->getWidth(), sensor_parameters_->getHeight(), 0,
381 GL_DEPTH_COMPONENT, encoding, sensor_data);
382 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
383 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
388 glActiveTexture(GL_TEXTURE2);
389 glBindTexture(GL_TEXTURE_2D, depth_texture);
392 glActiveTexture(GL_TEXTURE4);
393 glBindTexture(GL_TEXTURE_2D, color_texture);
395 depth_filter_->end();