graph_drawable.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <sstream>
29 
30 #include "graph_drawable.h"
32 #include "util.h"
33 
34 #ifdef __ANDROID__
35 #include <GLES2/gl2.h>
36 #else //__APPLE__
37 #include <OpenGLES/ES2/gl.h>
38 #endif
39 
41  GLuint shaderProgram,
42  const std::map<int, rtabmap::Transform> & poses,
43  const std::multimap<int, rtabmap::Link> & links) :
44  vertex_buffers_(0),
45  pose_(1.0f),
46  visible_(true),
47  lineWidth_(3.0f),
48  shader_program_(shaderProgram)
49 {
50  UASSERT(!poses.empty());
51 
52  glGenBuffers(1, &vertex_buffers_);
53 
54  if(vertex_buffers_)
55  {
56  LOGI("Creating vertex buffer %d", vertex_buffers_);
57  std::vector<float> vertices = std::vector<float>(poses.size()*3);
58  int i=0;
59  std::map<int, int> idsToIndices;
60  for(std::map<int, rtabmap::Transform>::const_iterator iter=poses.begin(); iter!=poses.end(); ++iter)
61  {
62  vertices[i*3] = iter->second.x();
63  vertices[i*3+1] = iter->second.y();
64  vertices[i*3+2] = iter->second.z();
65  idsToIndices.insert(std::make_pair(iter->first, i));
66  ++i;
67  }
68 
69  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_);
70  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (int)vertices.size(), (const void *)vertices.data(), GL_STATIC_DRAW);
71  glBindBuffer(GL_ARRAY_BUFFER, 0);
72 
73  GLint error = glGetError();
74  if(error != GL_NO_ERROR)
75  {
76  LOGI("OpenGL: Could not allocate point cloud (0x%x)\n", error);
77  vertex_buffers_ = 0;
78  }
79  else if(links.size())
80  {
81  neighborIndices_.resize(links.size() * 2);
82  loopClosureIndices_.resize(links.size() * 2);
83  int oiNeighbors = 0;
84  int oiLoopClosures = 0;
85  for(std::multimap<int, rtabmap::Link>::const_iterator iter=links.begin(); iter!=links.end(); ++iter)
86  {
87  std::map<int, int>::const_iterator jterFrom = idsToIndices.find(iter->second.from());
88  std::map<int, int>::const_iterator jterTo = idsToIndices.find(iter->second.to());
89  if(jterFrom != idsToIndices.end() && jterTo != idsToIndices.end())
90  {
91  if(iter->second.type() == rtabmap::Link::kNeighbor)
92  {
93  neighborIndices_[oiNeighbors++] = (unsigned short)jterFrom->second;
94  neighborIndices_[oiNeighbors++] = (unsigned short)jterTo->second;
95  }
96  else
97  {
98  loopClosureIndices_[oiLoopClosures++] = (unsigned short)jterFrom->second;
99  loopClosureIndices_[oiLoopClosures++] = (unsigned short)jterTo->second;
100  }
101  }
102  }
103  neighborIndices_.resize(oiNeighbors);
104  loopClosureIndices_.resize(oiLoopClosures);
105  }
106  }
107 }
108 
110 {
111  LOGI("Freeing cloud buffer %d", vertex_buffers_);
112  if (vertex_buffers_)
113  {
114  glDeleteBuffers(1, &vertex_buffers_);
115  tango_gl::util::CheckGlError("GraphDrawable::~GraphDrawable()");
116  vertex_buffers_ = 0;
117  }
118 }
119 
121 {
122  UASSERT(!pose.isNull());
123 
124  pose_ = glmFromTransform(pose);
125 }
126 
127 void GraphDrawable::Render(const glm::mat4 & projectionMatrix, const glm::mat4 & viewMatrix) {
128 
129  if(vertex_buffers_ && (neighborIndices_.size() || loopClosureIndices_.size()) && visible_)
130  {
131  glUseProgram(shader_program_);
132  glLineWidth(lineWidth_);
133 
134  GLuint mvp_handle_ = glGetUniformLocation(shader_program_, "mvp");
135  glm::mat4 mvp_mat = projectionMatrix * viewMatrix * pose_;
136  glUniformMatrix4fv(mvp_handle_, 1, GL_FALSE, glm::value_ptr(mvp_mat));
137 
138  GLuint color_handle = glGetUniformLocation(shader_program_, "color");
139 
140  GLint attribute_vertex = glGetAttribLocation(shader_program_, "vertex");
141 
142  glEnableVertexAttribArray(attribute_vertex);
143  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_);
144  glVertexAttribPointer(attribute_vertex, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), 0);
145 
146  if(neighborIndices_.size())
147  {
148  glUniform3f(color_handle, 1.0f, 0.0f, 0.0f); // blue for neighbors
149  glDrawElements(GL_LINES, neighborIndices_.size(), GL_UNSIGNED_SHORT, neighborIndices_.data());
150  }
151  if(loopClosureIndices_.size())
152  {
153  glUniform3f(color_handle, 0.0f, 0.0f, 1.0f); // red for loop closures
154  glDrawElements(GL_LINES, loopClosureIndices_.size(), GL_UNSIGNED_SHORT, loopClosureIndices_.data());
155  }
156 
157  glDisableVertexAttribArray(0);
158  glBindBuffer(GL_ARRAY_BUFFER, 0);
159 
160  glUseProgram(0);
161  tango_gl::util::CheckGlError("GraphDrawable::Render()");
162  }
163 }
164 
glm::mat4 glmFromTransform(const rtabmap::Transform &transform)
Definition: util.h:124
void Render(const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix)
void setPose(const rtabmap::Transform &mapToOdom)
f
std::vector< GLushort > loopClosureIndices_
static const float vertices[]
Definition: quad.cpp:39
GLuint vertex_buffers_
glm::mat4 pose_
unsigned int GLuint
Definition: dummy.cpp:78
GLM_FUNC_DECL genType::value_type const * value_ptr(genType const &vec)
#define UASSERT(condition)
GraphDrawable(GLuint shaderProgram, const std::map< int, rtabmap::Transform > &poses, const std::multimap< int, rtabmap::Link > &links)
#define true
Definition: ConvertUTF.c:57
#define GL_FALSE
Definition: dummy.cpp:79
virtual ~GraphDrawable()
std::vector< GLushort > neighborIndices_
#define LOGI(...)
bool isNull() const
Definition: Transform.cpp:107
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
GLuint shader_program_
void CheckGlError(const char *operation)
Definition: util.cpp:43
ULogger class and convenient macros.


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:28