27 #include <pcl/common/transforms.h> 28 #include <pcl/common/common.h> 51 "precision mediump float;\n" 52 "precision mediump int;\n" 53 "attribute vec3 vertex;\n" 54 "uniform vec3 color;\n" 56 "varying vec3 v_color;\n" 58 " gl_Position = mvp*vec4(vertex.x, vertex.y, vertex.z, 1.0);\n" 62 "precision mediump float;\n" 63 "precision mediump int;\n" 64 "varying vec3 v_color;\n" 66 " gl_FragColor = vec4(v_color.z, v_color.y, v_color.x, 1.0);\n" 72 background_renderer_(0),
83 frustumVisible_(
true),
86 graph_shader_program_(0),
90 meshRenderingTexture_(
true),
92 boundingBoxRendering_(
false),
94 backfaceCulling_(
true),
156 LOGI(
"Scene::DeleteResources()");
178 glDeleteFramebuffers(1, &
fboId_);
180 glDeleteRenderbuffers(1, &
rboId_);
192 LOGI(
"Scene::clear()");
197 for(std::map<int, tango_gl::Axis*>::iterator iter=
markers_.begin(); iter!=
markers_.end(); ++iter)
221 LOGE(
"Setup graphic height not valid");
226 glViewport(0, 0, w, h);
229 UINFO(
"Setup viewport OpenGL: %dx%d", w, h);
233 glDeleteFramebuffers(1, &
fboId_);
235 glDeleteRenderbuffers(1, &
rboId_);
242 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &originid);
246 glGenFramebuffers(1, &
fboId_);
247 glBindFramebuffer(GL_FRAMEBUFFER,
fboId_);
252 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
253 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
254 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
255 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
NULL);
257 glBindTexture(GL_TEXTURE_2D, 0);
259 glGenRenderbuffers(1, &
rboId_);
260 glBindRenderbuffer(GL_RENDERBUFFER,
rboId_);
261 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, w, h);
262 glBindRenderbuffer(GL_RENDERBUFFER, 0);
265 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
depthTexture_, 0);
266 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
rboId_);
268 GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
269 UASSERT ( status == GL_FRAMEBUFFER_COMPLETE);
270 glBindFramebuffer(GL_FRAMEBUFFER, originid);
279 std::vector<glm::vec4> planes(6);
283 planes[0].x = mat[0][3] + mat[0][0];
284 planes[0].y = mat[1][3] + mat[1][0];
285 planes[0].z = mat[2][3] + mat[2][0];
286 planes[0].w = mat[3][3] + mat[3][0];
290 planes[1].x = mat[0][3] - mat[0][0];
291 planes[1].y = mat[1][3] - mat[1][0];
292 planes[1].z = mat[2][3] - mat[2][0];
293 planes[1].w = mat[3][3] - mat[3][0];
297 planes[2].x = mat[0][3] + mat[0][1];
298 planes[2].y = mat[1][3] + mat[1][1];
299 planes[2].z = mat[2][3] + mat[2][1];
300 planes[2].w = mat[3][3] + mat[3][1];
304 planes[3].x = mat[0][3] - mat[0][1];
305 planes[3].y = mat[1][3] - mat[1][1];
306 planes[3].z = mat[2][3] - mat[2][1];
307 planes[3].w = mat[3][3] - mat[3][1];
311 planes[4].x = mat[0][3] + mat[0][2];
312 planes[4].y = mat[1][3] + mat[1][2];
313 planes[4].z = mat[2][3] + mat[2][2];
314 planes[4].w = mat[3][3] + mat[3][2];
318 planes[5].x = mat[0][3] - mat[0][2];
319 planes[5].y = mat[1][3] - mat[1][2];
320 planes[5].z = mat[2][3] - mat[2][2];
321 planes[5].w = mat[3][3] - mat[3][2];
325 for(
unsigned int i=0;i<planes.size(); ++i)
329 float d =
std::sqrt(planes[i].
x * planes[i].
x + planes[i].y * planes[i].y + planes[i].z * planes[i].z);
350 const std::vector<glm::vec4> &planes,
351 const pcl::PointXYZ &boxMin,
352 const pcl::PointXYZ &boxMax)
355 const pcl::PointXYZ * box[] = {&boxMin, &boxMax};
358 for (
unsigned int i = 0; i < planes.size(); ++i)
366 const int px = p.
x > 0.0f?1:0;
367 const int py = p.
y > 0.0f?1:0;
368 const int pz = p.
z > 0.0f?1:0;
376 (p.
z*box[pz]->z) + p.
w;
379 if (dp < 0) {
return false; }
417 bool renderBackgroundCamera =
423 if(renderBackgroundCamera)
425 if(projectionMatrix[0][0] > arProjectionMatrix[0][0]-0.3)
427 projectionMatrix = arProjectionMatrix;
428 viewMatrix = arViewMatrix;
432 renderBackgroundCamera =
false;
439 0.0
f, 0.0
f, 1.0
f, 0.0
f,
440 0.0
f, 1.0
f, 0.0
f, 0.0
f,
441 -1.0
f, 0.0
f, 0.0
f, 0.0
f);
445 std::vector<PointCloudDrawable*> cloudsToDraw(
pointClouds_.size());
454 if(iter->second->isVisible())
457 iter->second->aabbMinWorld(),
458 iter->second->aabbMaxWorld()))
460 cloudsToDraw[oi++] = iter->second;
464 cloudsToDraw.resize(oi);
467 glEnable(GL_DEPTH_TEST);
468 glDepthFunc(GL_LESS);
469 glDepthMask(GL_TRUE);
470 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
471 glDisable (GL_BLEND);
472 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
476 glEnable(GL_CULL_FACE);
480 glDisable(GL_CULL_FACE);
483 bool onlineBlending =
485 occlusionMesh.
cloud.get() &&
486 occlusionMesh.
cloud->size()) ||
490 (cloudsToDraw.size() > 1 || (renderBackgroundCamera &&
wireFrame_)));
492 if(onlineBlending &&
fboId_)
495 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &originid);
498 glBindFramebuffer(GL_FRAMEBUFFER,
fboId_);
500 glClearColor(0, 0, 0, 0);
501 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
504 for(std::vector<PointCloudDrawable*>::const_iterator iter=cloudsToDraw.begin(); iter!=cloudsToDraw.end(); ++iter)
506 Eigen::Vector3f cloudToCamera(
507 (*iter)->getPose().x() - openglCamera.
x(),
508 (*iter)->getPose().y() - openglCamera.
y(),
509 (*iter)->getPose().z() - openglCamera.
z());
510 float distanceToCameraSqr = cloudToCamera[0]*cloudToCamera[0] + cloudToCamera[1]*cloudToCamera[1] + cloudToCamera[2]*cloudToCamera[2];
511 (*iter)->Render(projectionMatrix, viewMatrix,
meshRendering_,
pointSize_,
false,
false, distanceToCameraSqr, 0, 0, 0, 0, 0,
true);
517 drawable.
Render(projectionMatrix, viewMatrix,
true,
pointSize_,
false,
false, 0, 0, 0, 0, 0, 0,
true);
521 glBindFramebuffer(GL_FRAMEBUFFER, originid);
526 glClearColor(0, 0, 0, 0);
527 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
530 for(std::vector<PointCloudDrawable*>::const_iterator iter=cloudsToDraw.begin(); iter!=cloudsToDraw.end(); ++iter)
532 Eigen::Vector3f cloudToCamera(
533 (*iter)->getPose().x() - openglCamera.
x(),
534 (*iter)->getPose().y() - openglCamera.
y(),
535 (*iter)->getPose().z() - openglCamera.
z());
536 float distanceToCameraSqr = cloudToCamera[0]*cloudToCamera[0] + cloudToCamera[1]*cloudToCamera[1] + cloudToCamera[2]*cloudToCamera[2];
538 (*iter)->Render(projectionMatrix, viewMatrix,
meshRendering_,
pointSize_*10.0
f,
false,
false, distanceToCameraSqr, 0, 0, 0, 0, 0,
true);
543 float zValueF = float(zValue[0]/255.0
f) + float(zValue[1]/255.0
f)/255.0f + float(zValue[2]/255.0
f)/65025.0f + float(zValue[3]/255.0
f)/160581375.0f;
547 zValueF = zValueF*2.0-1.0;
555 glClearColor(
r_,
g_,
b_, 1.0
f);
556 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
558 if(renderBackgroundCamera && (!onlineBlending || !
meshRendering_))
579 glm::vec3 positionCamera(cameraFrame.
x(), cameraFrame.
y(), cameraFrame.
z());
581 glm::quat rotationCamera(quatCamera.w(), quatCamera.x(), quatCamera.y(), quatCamera.z());
616 for(std::vector<PointCloudDrawable*>::const_iterator iter=cloudsToDraw.begin(); iter!=cloudsToDraw.end(); ++iter)
626 Eigen::Vector3f cloudToCamera(
629 cloud->
getPose().
z() - openglCamera.
z());
630 float distanceToCameraSqr = cloudToCamera[0]*cloudToCamera[0] + cloudToCamera[1]*cloudToCamera[1] + cloudToCamera[2]*cloudToCamera[2];
632 cloud->
Render(projectionMatrix, viewMatrix,
meshRendering_,
pointSize_,
meshRenderingTexture_,
lighting_, distanceToCameraSqr, onlineBlending?
depthTexture_:0,
screenWidth_,
screenHeight_,
gesture_camera_->
getNearClipPlane(),
gesture_camera_->
getFarClipPlane(),
false,
wireFrame_);
642 glDisable (GL_BLEND);
643 glDepthMask(GL_TRUE);
647 for(std::map<int, tango_gl::Axis*>::const_iterator iter=
markers_.begin(); iter!=
markers_.end(); ++iter)
649 iter->second->Render(projectionMatrix, viewMatrix);
652 return (
int)cloudsToDraw.size();
698 float y0,
float x1,
float y1) {
718 const std::map<int, rtabmap::Transform> & poses,
719 const std::multimap<int, rtabmap::Link> & links)
753 LOGI(
"add marker %d",
id);
754 std::map<int, tango_gl::Axis*>::iterator iter=
markers_.find(
id);
761 markers_.insert(std::make_pair(
id, drawable));
768 std::map<int, tango_gl::Axis*>::iterator iter=
markers_.find(
id);
774 iter->second->SetPosition(position);
775 iter->second->SetRotation(
rotation);
784 std::map<int, tango_gl::Axis*>::iterator iter=
markers_.find(
id);
798 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
799 const pcl::IndicesPtr & indices,
802 LOGI(
"add cloud %d (%d points %d indices)",
id, (
int)cloud->size(), indices.get()?(int)indices->size():0);
803 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
820 bool createWireframe)
822 LOGI(
"add mesh %d",
id);
823 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
842 for(
unsigned int i=0; i<mesh.
polygons.size(); ++i)
844 for(
unsigned int j=0; j<mesh.
polygons[i].vertices.size(); ++j)
856 if(mesh.
cloud->isOrganized())
858 for(
unsigned int i=0; i<mesh.
indices->size(); ++i)
869 for(
unsigned int i=0; i<mesh.
cloud->size(); ++i)
884 LOGD(
"compute min height %f s", time.
ticks());
892 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
895 iter->second->setPose(pose);
901 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
904 iter->second->setVisible(visible);
930 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
933 iter->second->updatePolygons(polygons);
939 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
942 iter->second->updateMesh(mesh);
948 std::map<int, PointCloudDrawable*>::iterator iter=
pointClouds_.find(
id);
951 iter->second->setGains(gainR, gainG, gainB);
void Render(const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix)
void Render(const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix, bool meshRendering=true, float pointSize=3.0f, bool textureRendering=false, bool lighting=true, float distanceToCamSqr=0.0f, const GLuint &depthTexture=0, int screenWidth=0, int screenHeight=0, float nearClipPlane=0, float farClipPlane=0, bool packDepthToColorChannel=false, bool wireFrame=false) const
static void releaseShaderPrograms()
const glm::vec3 kHeightOffset
void addMesh(int id, const rtabmap::Mesh &mesh, const rtabmap::Transform &pose, bool createWireframe=false)
void setGridVisible(bool visible)
BoundingBoxDrawable * box_
0 degree rotation (natural orientation)
rtabmap::Transform glmToTransform(const glm::mat4 &mat)
void updateCloudPolygons(int id, const std::vector< pcl::Vertices > &polygons)
void OnTouchEvent(int touch_count, tango_gl::GestureCamera::TouchEvent event, float x0, float y0, float x1, float y1)
void SetLineWidth(const float pixels)
cv::Point3f RTABMAP_EXP transformPoint(const cv::Point3f &pt, const Transform &transform)
void SetupViewPort(int w, int h)
bool hasMarker(int id) const
std::set< int > getAddedMarkers() const
CameraType GetCameraType() const
void setCloudPose(int id, const rtabmap::Transform &pose)
pcl::PointCloud< pcl::PointXYZRGB >::Ptr cloud
void SetCameraType(CameraType camera_index)
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
GLM_FUNC_DECL vecType< T, P > sqrt(vecType< T, P > const &x)
std::map< int, PointCloudDrawable * > pointClouds_
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
void setPose(const rtabmap::Transform &pose)
bool hasTexture(int id) const
void setGridColor(float r, float g, float b)
const glm::vec3 kFrustumScale
float getNearClipPlane() const
int Render(const float *uvsTransformed=0, glm::mat4 arViewMatrix=glm::mat4(0), glm::mat4 arProjectionMatrix=glm::mat4(0), const rtabmap::Mesh &occlusionMesh=rtabmap::Mesh(), bool mapping=false)
highp_quat quat
Quaternion of default single-precision floating-point numbers.
std::set< int > getAddedClouds() const
std::set< K > uKeysSet(const std::map< K, V > &m)
void Draw(const float *transformed_uvs, const GLuint &depthTexture, int screenWidth, int screenHeight, bool redUnknown)
void SetWindowSize(const float width, const float height)
GLM_FUNC_DECL T angle(detail::tquat< T, P > const &x)
BackgroundRenderer * background_renderer_
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
bool meshRenderingTexture_
std::vector< pcl::Vertices > polygons
tango_gl::Frustum * frustum_
void removeMarker(int id)
const std::string kGraphVertexShader
void setOrthoCropFactor(float value)
#define UASSERT(condition)
Wrappers of STL for convenient functions.
void updateGains(int id, float gainR, float gainG, float gainB)
GLM_FUNC_DECL genType normalize(genType const &x)
std::map< int, tango_gl::Axis * > markers_
void SetFieldOfView(const float fov)
static const rtabmap::Transform optical_T_opengl(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f)
const pcl::PointXYZ & aabbMinWorld() const
void SetCameraPose(const rtabmap::Transform &pose)
const std::string kGraphFragmentShader
void addCloud(int id, const pcl::PointCloud< pcl::PointXYZRGB >::Ptr &cloud, const pcl::IndicesPtr &indices, const rtabmap::Transform &pose)
const pcl::PointXYZ & aabbMaxWorld() const
void setCloudVisible(int id, bool visible)
std::vector< glm::vec4 > computeFrustumPlanes(const glm::mat4 &mat, bool normalize=true)
void updateGraph(const std::map< int, rtabmap::Transform > &poses, const std::multimap< int, rtabmap::Link > &links)
void SetColor(const Color &color)
bool hasCloud(int id) const
cv::Point2f doubleTapPos_
void OnTouchEvent(int touch_count, TouchEvent event, float x0, float y0, float x1, float y1)
rtabmap::ScreenRotation color_camera_to_display_rotation_
void addMarker(int id, const rtabmap::Transform &pose)
GLM_FUNC_DECL detail::tquat< T, P > rotation(detail::tvec3< T, P > const &orig, detail::tvec3< T, P > const &dest)
void setFrustumVisible(bool visible)
float getFarClipPlane() const
rtabmap::Transform getPose() const
void SetAnchorPosition(const glm::vec3 &pos, const glm::quat &rotation)
void SetOrthoCropFactor(float value)
bool intersectFrustumAABB(const std::vector< glm::vec4 > &planes, const pcl::PointXYZ &boxMin, const pcl::PointXYZ &boxMax)
void UpdateVertexArray(const glm::vec3 &v)
void setTraceVisible(bool visible)
void SetCameraType(tango_gl::GestureCamera::CameraType camera_type)
void SetAnchorOffset(const glm::vec3 &pos)
ULogger class and convenient macros.
void updateMesh(int id, const rtabmap::Mesh &mesh)
void setGraphVisible(bool visible)
bool hasMesh(int id) const
void setMarkerPose(int id, const rtabmap::Transform &pose)
static void createShaderPrograms()
static const rtabmap::Transform opticalRotationInv
bool boundingBoxRendering_
void setGridRotation(float angleDeg)
void updateVertices(const pcl::PointXYZ &min, const pcl::PointXYZ &max)
glm::mat4 GetProjectionMatrix()
tango_gl::GestureCamera * gesture_camera_
rtabmap::Transform GetOpenGLCameraPose(float *fov=0) const
const tango_gl::Color kGridColor(0.85f, 0.85f, 0.85f)
const tango_gl::Color kTraceColor(0.66f, 0.66f, 0.66f)
rtabmap::Transform * currentPose_
glm::mat4 GetViewMatrix()
GLM_FUNC_DECL matType< T, P > inverse(matType< T, P > const &m)
GLuint graph_shader_program_