34 #include <opencv2/imgproc/imgproc.hpp>
36 #include "pcl/common/transforms.h"
39 #include <GLES2/gl2.h>
41 #include <OpenGLES/ES2/gl.h>
64 "precision highp float;\n"
65 "precision mediump int;\n"
66 "attribute vec3 aVertex;\n"
67 "attribute vec3 aColor;\n"
69 "uniform mat4 uMVP;\n"
70 "uniform float uPointSize;\n"
72 "varying vec3 vColor;\n"
73 "varying float vLightWeighting;\n"
76 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
77 " gl_PointSize = uPointSize;\n"
78 " vLightWeighting = 1.0;\n"
82 "precision highp float;\n"
83 "precision mediump int;\n"
84 "attribute vec3 aVertex;\n"
85 "attribute vec3 aNormal;\n"
86 "attribute vec3 aColor;\n"
88 "uniform mat4 uMVP;\n"
90 "uniform vec3 uLightingDirection;\n"
91 "uniform float uPointSize;\n"
93 "varying vec3 vColor;\n"
94 "varying float vLightWeighting;\n"
97 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
98 " gl_PointSize = uPointSize;\n"
99 " vec3 transformedNormal = uN * aNormal;\n"
100 " vLightWeighting = max(dot(transformedNormal, uLightingDirection)*0.5+0.5, 0.0);\n"
101 " if(vLightWeighting<0.5)"
102 " vLightWeighting=0.5;\n"
103 " vColor = aColor;\n"
107 "precision highp float;\n"
108 "precision mediump int;\n"
109 "uniform float uGainR;\n"
110 "uniform float uGainG;\n"
111 "uniform float uGainB;\n"
112 "varying vec3 vColor;\n"
113 "varying float vLightWeighting;\n"
115 " vec4 textureColor = vec4(vColor.z, vColor.y, vColor.x, 1.0);\n"
116 " gl_FragColor = vec4(textureColor.r * uGainR * vLightWeighting, textureColor.g * uGainG * vLightWeighting, textureColor.b * uGainB * vLightWeighting, textureColor.a);\n"
119 "precision highp float;\n"
120 "precision mediump int;\n"
121 "uniform float uGainR;\n"
122 "uniform float uGainG;\n"
123 "uniform float uGainB;\n"
124 "uniform float uNearZ;\n"
125 "uniform float uFarZ;\n"
126 "uniform sampler2D uDepthTexture;\n"
127 "uniform vec2 uScreenScale;\n"
128 "varying vec3 vColor;\n"
129 "varying float vLightWeighting;\n"
131 " vec4 textureColor = vec4(vColor.z, vColor.y, vColor.x, 1.0);\n"
132 " float alpha = 1.0;\n"
133 " vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
134 " vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
135 " float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
136 " float num = (2.0 * uNearZ * uFarZ);\n"
137 " float diff = (uFarZ - uNearZ);\n"
138 " float add = (uFarZ + uNearZ);\n"
139 " float ndcDepth = depth * 2.0 - 1.0;\n"
140 " float linearDepth = num / (add - ndcDepth * diff);\n"
141 " float ndcFragz = gl_FragCoord.z * 2.0 - 1.0;\n"
142 " float linearFragz = num / (add - ndcFragz * diff);\n"
143 " if(linearFragz > linearDepth + 0.05)\n"
145 " gl_FragColor = vec4(textureColor.r * uGainR * vLightWeighting, textureColor.g * uGainG * vLightWeighting, textureColor.b * uGainB * vLightWeighting, alpha);\n"
149 "precision highp float;\n"
150 "precision mediump int;\n"
151 "attribute vec3 aVertex;\n"
152 "uniform mat4 uMVP;\n"
153 "uniform float uPointSize;\n"
155 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
156 " gl_PointSize = uPointSize;\n"
159 "precision highp float;\n"
160 "precision mediump int;\n"
162 " vec4 enc = vec4(1.,255.,65025.,16581375.) * gl_FragCoord.z;\n"
163 " enc = fract(enc);\n"
164 " enc -= enc.yzww * vec2(1./255., 0.).xxxy;\n"
165 " gl_FragColor = enc;\n"
170 "precision highp float;\n"
171 "precision mediump int;\n"
172 "attribute vec3 aVertex;\n"
173 "attribute vec3 aColor;\n"
174 "attribute vec2 aTexCoord;\n"
176 "uniform mat4 uMVP;\n"
178 "varying vec3 vColor;\n"
179 "varying vec2 vTexCoord;\n"
180 "varying float vLightWeighting;\n"
183 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
185 " if(aTexCoord.x < 0.0) {\n"
186 " vTexCoord.x = 1.0;\n"
187 " vTexCoord.y = 1.0;\n"
189 " vTexCoord = aTexCoord;\n"
192 " vColor = aColor;\n"
193 " vLightWeighting = 1.0;\n"
196 "precision highp float;\n"
197 "precision mediump int;\n"
198 "attribute vec3 aVertex;\n"
199 "attribute vec3 aColor;\n"
200 "attribute vec3 aNormal;\n"
201 "attribute vec2 aTexCoord;\n"
203 "uniform mat4 uMVP;\n"
205 "uniform vec3 uLightingDirection;\n"
207 "varying vec3 vColor;\n"
208 "varying vec2 vTexCoord;\n"
209 "varying float vLightWeighting;\n"
212 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
214 " if(aTexCoord.x < 0.0) {\n"
215 " vTexCoord.x = 1.0;\n"
216 " vTexCoord.y = 1.0;\n"
218 " vTexCoord = aTexCoord;\n"
221 " vColor = aColor;\n"
222 " vec3 transformedNormal = uN * aNormal;\n"
223 " vLightWeighting = max(dot(transformedNormal, uLightingDirection)*0.5+0.5, 0.0);\n"
224 " if(vLightWeighting<0.5) \n"
225 " vLightWeighting=0.5;\n"
228 "precision highp float;\n"
229 "precision mediump int;\n"
230 "uniform sampler2D uTexture;\n"
231 "uniform float uGainR;\n"
232 "uniform float uGainG;\n"
233 "uniform float uGainB;\n"
234 "uniform int uHideSeams;\n"
235 "varying vec3 vColor;\n"
236 "varying vec2 vTexCoord;\n"
237 "varying float vLightWeighting;\n"
240 " vec4 textureColor;\n"
241 " if(uHideSeams==1) {\n"
242 " if(vTexCoord.x>0.99 && vTexCoord.y>0.99) {\n"
243 " textureColor = vec4(vColor.z, vColor.y, vColor.x, 1.0);\n"
245 " textureColor = texture2D(uTexture, vTexCoord);\n"
248 " textureColor = texture2D(uTexture, vTexCoord) * vec4(vColor.z, vColor.y, vColor.x, 1.0);\n"
250 " gl_FragColor = vec4(textureColor.r * uGainR * vLightWeighting, textureColor.g * uGainG * vLightWeighting, textureColor.b * uGainB * vLightWeighting, textureColor.a);\n"
253 "precision highp float;\n"
254 "precision mediump int;\n"
255 "uniform sampler2D uTexture;\n"
256 "uniform sampler2D uDepthTexture;\n"
257 "uniform float uGainR;\n"
258 "uniform float uGainG;\n"
259 "uniform float uGainB;\n"
260 "uniform vec2 uScreenScale;\n"
261 "uniform float uNearZ;\n"
262 "uniform float uFarZ;\n"
263 "varying vec3 vColor;\n"
264 "varying vec2 vTexCoord;\n"
265 "varying float vLightWeighting;\n"
268 " vec4 textureColor = texture2D(uTexture, vTexCoord);\n"
269 " float alpha = 1.0;\n"
270 " vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
271 " vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
272 " float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
273 " float num = (2.0 * uNearZ * uFarZ);\n"
274 " float diff = (uFarZ - uNearZ);\n"
275 " float add = (uFarZ + uNearZ);\n"
276 " float ndcDepth = depth * 2.0 - 1.0;\n"
277 " float linearDepth = num / (add - ndcDepth * diff);\n"
278 " float ndcFragz = gl_FragCoord.z * 2.0 - 1.0;\n"
279 " float linearFragz = num / (add - ndcFragz * diff);\n"
280 " if(linearFragz > linearDepth + 0.05)\n"
282 " gl_FragColor = vec4(textureColor.r * uGainR * vLightWeighting, textureColor.g * uGainG * vLightWeighting, textureColor.b * uGainB * vLightWeighting, alpha);\n"
325 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
326 const pcl::IndicesPtr & indices,
333 pose_(
rtabmap::Transform::getIdentity()),
348 bool createWireframe) :
352 pose_(
rtabmap::Transform::getIdentity()),
395 for(
int i=0;
i<4; ++
i)
408 size_t polygonSize = polygons[0].vertices.size();
410 std::vector<std::vector<GLuint> > indexes(4);
411 indexes[0].resize(polygons.size() * polygonSize);
413 indexes[2].resize(indexes[0].
size()*2);
416 for(
size_t i=0;
i<polygons.size(); ++
i)
419 for(
unsigned int j=0;
j<polygonSize; ++
j)
430 if(polygonsLowRes.size())
432 size_t polygonSize = polygonsLowRes[0].vertices.size();
434 indexes[1].resize(polygonsLowRes.size() * polygonSize);
436 indexes[3].resize(indexes[1].
size()*2);
439 for(
unsigned int i=0;
i<polygonsLowRes.size(); ++
i)
442 for(
unsigned int j=0;
j<polygonSize; ++
j)
455 for(
size_t i=0;
i<indexes.size(); ++
i)
462 LOGE(
"OpenGL: could not generate index buffer %ld\n",
i);
466 LOGD(
"Adding polygon index %ld size=%ld",
i, indexes[
i].
size());
469 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
uint32_t) * indexes[
i].
size(), indexes[
i].
data(), GL_STATIC_DRAW);
470 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
473 GLint
error = glGetError();
474 if(
error != GL_NO_ERROR)
476 LOGE(
"OpenGL: Could not allocate indexes (0x%x)\n",
error);
487 UASSERT(cloud.get() && !cloud->empty());
519 LOGE(
"OpenGL: could not generate vertex buffers\n");
525 size_t totalPoints = 0;
526 std::vector<GLuint> verticesLowRes;
527 std::vector<GLuint> verticesLowLowRes;
532 verticesLowRes.resize(cloud->isOrganized()?totalPoints:0);
533 verticesLowLowRes.resize(cloud->isOrganized()?totalPoints:0);
538 const pcl::PointXYZRGB & pt = cloud->at(
indices->at(
i));
546 if(cloud->isOrganized())
550 verticesLowRes[oi_low++] =
i;
554 verticesLowLowRes[oi_lowlow++] =
i;
558 verticesLowRes.resize(oi_low);
559 verticesLowLowRes.resize(oi_lowlow);
563 totalPoints = cloud->size();
565 verticesLowRes.resize(cloud->isOrganized()?totalPoints:0);
566 verticesLowLowRes.resize(cloud->isOrganized()?totalPoints:0);
569 for(
unsigned int i=0;
i<cloud->size(); ++
i)
571 const pcl::PointXYZRGB & pt = cloud->at(
i);
579 if(cloud->isOrganized())
583 verticesLowRes[oi_low++] =
i;
587 verticesLowLowRes[oi_lowlow++] =
i;
591 verticesLowRes.resize(oi_low);
592 verticesLowLowRes.resize(oi_lowlow);
596 glBufferData(GL_ARRAY_BUFFER,
sizeof(GLfloat) * (
int)
vertices.size(), (
const void *)
vertices.data(), GL_STATIC_DRAW);
597 glBindBuffer(GL_ARRAY_BUFFER, 0);
599 GLint
error = glGetError();
600 if(
error != GL_NO_ERROR)
602 LOGE(
"OpenGL: Could not allocate point cloud (0x%x)\n",
error);
608 for(
size_t i=4;
i<5; ++
i)
610 if((
i==4 && !verticesLowRes.empty()) ||
611 (
i==5 && !verticesLowLowRes.empty()))
616 LOGE(
"OpenGL: could not generate index buffer %ld\n",
i);
622 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
uint32_t) *
index_buffers_count_[
i],
i==4?verticesLowRes.data():verticesLowLowRes.data(), GL_STATIC_DRAW);
623 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
626 GLint
error = glGetError();
627 if(
error != GL_NO_ERROR)
629 LOGE(
"OpenGL: Could not allocate indexes (0x%x)\n",
error);
667 bool textureUpdate =
false;
676 textureUpdate =
true;
682 LOGE(
"OpenGL: could not generate vertex buffers\n");
692 LOGE(
"OpenGL: could not generate texture buffers\n");
700 std::vector<pcl::Vertices> polygons = mesh.
polygons;
701 std::vector<pcl::Vertices> polygonsLowRes;
704 if(mesh.
cloud->isOrganized())
709 std::vector<GLuint> verticesLowRes;
710 std::vector<GLuint> verticesLowLowRes;
711 verticesLowRes.resize(totalPoints);
712 verticesLowLowRes.resize(totalPoints);
719 for(
unsigned int i=0;
i<mesh.
indices->size(); ++
i)
721 const pcl::PointXYZRGB & pt = mesh.
cloud->at(mesh.
indices->at(
i));
748 verticesLowRes[oi_low++] =
i;
752 verticesLowLowRes[oi_lowlow++] =
i;
761 for(
unsigned int i=0;
i<mesh.
indices->size(); ++
i)
763 const pcl::PointXYZRGB & pt = mesh.
cloud->at(mesh.
indices->at(
i));
783 verticesLowRes[oi_low++] =
i;
787 verticesLowLowRes[oi_lowlow++] =
i;
791 verticesLowRes.resize(oi_low);
792 verticesLowLowRes.resize(oi_lowlow);
795 for(
size_t i=4;
i<5; ++
i)
797 if((
i==4 && !verticesLowRes.empty()) ||
798 (
i==5 && !verticesLowLowRes.empty()))
803 LOGE(
"OpenGL: could not generate index buffer %ld\n",
i);
809 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
uint32_t) *
index_buffers_count_[
i],
i==4?verticesLowRes.data():verticesLowLowRes.data(), GL_STATIC_DRAW);
810 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
812 GLint
error = glGetError();
813 if(
error != GL_NO_ERROR)
815 LOGE(
"OpenGL: Could not allocate indexes (0x%x)\n",
error);
839 uFormat(
"%d vs %d x %d", (
int)mesh.
texCoords.size(), (
int)polygons[0].vertices.size(), (
int)polygons.size()).c_str());
842 for(
unsigned int i=0;
i<polygons.size(); ++
i)
844 pcl::Vertices &
v = polygons[
i];
845 for(
unsigned int j=0;
j<
v.vertices.size(); ++
j)
850 const pcl::PointXYZRGB & pt = mesh.
cloud->at(
v.vertices[
j]);
880 v.vertices[
j] = (
int)oi;
891 totalPoints = (
int)mesh.
cloud->size();
896 for(
unsigned int i=0;
i<mesh.
cloud->size(); ++
i)
898 const pcl::PointXYZRGB & pt = mesh.
cloud->at(
i);
920 glBufferData(GL_ARRAY_BUFFER,
sizeof(GLfloat) * (
int)
vertices.size(), (
const void *)
vertices.data(), GL_STATIC_DRAW);
921 glBindBuffer(GL_ARRAY_BUFFER, 0);
923 GLint
error = glGetError();
924 if(
error != GL_NO_ERROR)
926 LOGE(
"OpenGL: Could not allocate point cloud (0x%x)\n",
error);
941 glBindTexture(GL_TEXTURE_2D,
texture_);
942 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
943 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
944 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
945 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
947 cv::cvtColor(mesh.
texture, rgbImage, cv::COLOR_BGR2RGBA);
949 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
953 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rgbImage.cols, rgbImage.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgbImage.data);
955 GLint
error = glGetError();
956 if(
error != GL_NO_ERROR)
958 LOGE(
"OpenGL: Could not allocate texture (0x%x)\n",
error);
992 pcl::PointCloud<pcl::PointXYZ>
corners;
1003 pcl::PointCloud<pcl::PointXYZ> cornersTransformed;
1008 for(
unsigned int i=0;
i<cornersTransformed.size(); ++
i)
1020 bool textureRendering,
1022 float distanceToCameraSqr,
1023 const GLuint & depthTexture,
1026 float nearClipPlane,
1028 bool packDepthToColorChannel,
1030 bool hideSeams)
const
1039 if(packDepthToColorChannel || !(meshRendering && textureRendering &&
texture_))
1041 textureRendering =
false;
1045 if(packDepthToColorChannel)
1049 else if(textureRendering)
1072 glUseProgram(program);
1075 GLuint mvp_handle = glGetUniformLocation(program,
"uMVP");
1077 glm::mat4 mvp_mat = projectionMatrix * mv_mat;
1080 GLint attribute_vertex = glGetAttribLocation(program,
"aVertex");
1081 glEnableVertexAttribArray(attribute_vertex);
1082 GLint attribute_color = 0;
1083 GLint attribute_texture = 0;
1084 GLint attribute_normal = 0;
1086 if(packDepthToColorChannel || !textureRendering)
1088 GLuint point_size_handle_ = glGetUniformLocation(program,
"uPointSize");
1089 glUniform1f(point_size_handle_, pointSize);
1093 if(!packDepthToColorChannel)
1095 GLuint gainR_handle = glGetUniformLocation(program,
"uGainR");
1096 GLuint gainG_handle = glGetUniformLocation(program,
"uGainG");
1097 GLuint gainB_handle = glGetUniformLocation(program,
"uGainB");
1098 glUniform1f(gainR_handle,
gainR_);
1099 glUniform1f(gainG_handle,
gainG_);
1100 glUniform1f(gainB_handle,
gainB_);
1103 if(depthTexture > 0)
1106 glActiveTexture(GL_TEXTURE1);
1108 glBindTexture(GL_TEXTURE_2D, depthTexture);
1110 GLuint depth_texture_handle = glGetUniformLocation(program,
"uDepthTexture");
1111 glUniform1i(depth_texture_handle, 1);
1113 GLuint zNear_handle = glGetUniformLocation(program,
"uNearZ");
1114 GLuint zFar_handle = glGetUniformLocation(program,
"uFarZ");
1115 glUniform1f(zNear_handle, nearClipPlane);
1116 glUniform1f(zFar_handle, farClipPlane);
1118 GLuint screenScale_handle = glGetUniformLocation(program,
"uScreenScale");
1119 glUniform2f(screenScale_handle, 1.0
f/(
float)screenWidth, 1.0
f/(
float)screenHeight);
1124 GLuint n_handle = glGetUniformLocation(program,
"uN");
1127 normalMatrix = glm::transpose(normalMatrix);
1130 GLuint lightingDirection_handle = glGetUniformLocation(program,
"uLightingDirection");
1131 glUniform3f(lightingDirection_handle, 0.0, 0.0, 1.0);
1133 attribute_normal = glGetAttribLocation(program,
"aNormal");
1134 glEnableVertexAttribArray(attribute_normal);
1137 if(textureRendering)
1140 glActiveTexture(GL_TEXTURE0);
1142 glBindTexture(GL_TEXTURE_2D,
texture_);
1144 GLuint texture_handle = glGetUniformLocation(program,
"uTexture");
1145 glUniform1i(texture_handle, 0);
1147 attribute_texture = glGetAttribLocation(program,
"aTexCoord");
1148 glEnableVertexAttribArray(attribute_texture);
1150 if(depthTexture == 0)
1152 GLuint hideSeams_handle = glGetUniformLocation(program,
"uHideSeams");
1153 glUniform1i(hideSeams_handle, hideSeams?1:0);
1157 attribute_color = glGetAttribLocation(program,
"aColor");
1158 glEnableVertexAttribArray(attribute_color);
1165 glVertexAttribPointer(attribute_vertex, 3, GL_FLOAT,
GL_FALSE, (
hasNormals_?9:6)*
sizeof(GLfloat), 0);
1166 if(textureRendering)
1168 glVertexAttribPointer(attribute_texture, 2, GL_FLOAT,
GL_FALSE, (
hasNormals_?9:6)*
sizeof(GLfloat), (GLvoid*) (4 *
sizeof(GLfloat)));
1170 if(!packDepthToColorChannel)
1172 glVertexAttribPointer(attribute_color, 3, GL_UNSIGNED_BYTE, GL_TRUE, (
hasNormals_?9:6)*
sizeof(GLfloat), (GLvoid*) (3 *
sizeof(GLfloat)));
1176 glVertexAttribPointer(attribute_normal, 3, GL_FLOAT,
GL_FALSE, 9*
sizeof(GLfloat), (GLvoid*) (6 *
sizeof(GLfloat)));
1181 glVertexAttribPointer(attribute_vertex, 3, GL_FLOAT,
GL_FALSE, (
hasNormals_?7:4)*
sizeof(GLfloat), 0);
1182 if(!packDepthToColorChannel)
1184 glVertexAttribPointer(attribute_color, 3, GL_UNSIGNED_BYTE, GL_TRUE, (
hasNormals_?7:4)*
sizeof(GLfloat), (GLvoid*) (3 *
sizeof(GLfloat)));
1188 glVertexAttribPointer(attribute_normal, 3, GL_FLOAT,
GL_FALSE, 7*
sizeof(GLfloat), (GLvoid*) (4 *
sizeof(GLfloat)));
1196 float dist = meshRendering?50.0f:16.0f;
1233 else if(distanceToCameraSqr>150.0
f)
1240 glDrawArrays(GL_POINTS, 0,
nPoints_);
1245 glDrawArrays(GL_POINTS, 0,
nPoints_);
1250 glDisableVertexAttribArray(0);
1251 glBindBuffer(GL_ARRAY_BUFFER, 0);
1252 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);