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 vec2 aTexCoord;\n"
175 "uniform mat4 uMVP;\n"
177 "varying vec2 vTexCoord;\n"
178 "varying float vLightWeighting;\n"
181 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
183 " if(aTexCoord.x < 0.0) {\n"
184 " vTexCoord.x = 1.0;\n"
185 " vTexCoord.y = 1.0;\n"
187 " vTexCoord = aTexCoord;\n"
190 " vLightWeighting = 1.0;\n"
193 "precision highp float;\n"
194 "precision mediump int;\n"
195 "attribute vec3 aVertex;\n"
196 "attribute vec3 aNormal;\n"
197 "attribute vec2 aTexCoord;\n"
199 "uniform mat4 uMVP;\n"
201 "uniform vec3 uLightingDirection;\n"
203 "varying vec2 vTexCoord;\n"
204 "varying float vLightWeighting;\n"
207 " gl_Position = uMVP*vec4(aVertex.x, aVertex.y, aVertex.z, 1.0);\n"
209 " if(aTexCoord.x < 0.0) {\n"
210 " vTexCoord.x = 1.0;\n"
211 " vTexCoord.y = 1.0;\n"
213 " vTexCoord = aTexCoord;\n"
216 " vec3 transformedNormal = uN * aNormal;\n"
217 " vLightWeighting = max(dot(transformedNormal, uLightingDirection)*0.5+0.5, 0.0);\n"
218 " if(vLightWeighting<0.5) \n"
219 " vLightWeighting=0.5;\n"
222 "precision highp float;\n"
223 "precision mediump int;\n"
224 "uniform sampler2D uTexture;\n"
225 "uniform float uGainR;\n"
226 "uniform float uGainG;\n"
227 "uniform float uGainB;\n"
228 "varying vec2 vTexCoord;\n"
229 "varying float vLightWeighting;\n"
232 " vec4 textureColor = texture2D(uTexture, vTexCoord);\n"
233 " gl_FragColor = vec4(textureColor.r * uGainR * vLightWeighting, textureColor.g * uGainG * vLightWeighting, textureColor.b * uGainB * vLightWeighting, textureColor.a);\n"
236 "precision highp float;\n"
237 "precision mediump int;\n"
238 "uniform sampler2D uTexture;\n"
239 "uniform sampler2D uDepthTexture;\n"
240 "uniform float uGainR;\n"
241 "uniform float uGainG;\n"
242 "uniform float uGainB;\n"
243 "uniform vec2 uScreenScale;\n"
244 "uniform float uNearZ;\n"
245 "uniform float uFarZ;\n"
246 "varying vec2 vTexCoord;\n"
247 "varying float vLightWeighting;\n"
250 " vec4 textureColor = texture2D(uTexture, vTexCoord);\n"
251 " float alpha = 1.0;\n"
252 " vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
253 " vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
254 " float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
255 " float num = (2.0 * uNearZ * uFarZ);\n"
256 " float diff = (uFarZ - uNearZ);\n"
257 " float add = (uFarZ + uNearZ);\n"
258 " float ndcDepth = depth * 2.0 - 1.0;\n"
259 " float linearDepth = num / (add - ndcDepth * diff);\n"
260 " float ndcFragz = gl_FragCoord.z * 2.0 - 1.0;\n"
261 " float linearFragz = num / (add - ndcFragz * diff);\n"
262 " if(linearFragz > linearDepth + 0.05)\n"
264 " gl_FragColor = vec4(textureColor.r * uGainR * vLightWeighting, textureColor.g * uGainG * vLightWeighting, textureColor.b * uGainB * vLightWeighting, alpha);\n"
307 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud,
308 const pcl::IndicesPtr & indices,
315 pose_(
rtabmap::Transform::getIdentity()),
330 bool createWireframe) :
334 pose_(
rtabmap::Transform::getIdentity()),
377 for(
int i=0;
i<4; ++
i)
390 size_t polygonSize = polygons[0].vertices.size();
392 std::vector<std::vector<GLuint> > indexes(4);
393 indexes[0].resize(polygons.size() * polygonSize);
395 indexes[2].resize(indexes[0].
size()*2);
398 for(
size_t i=0;
i<polygons.size(); ++
i)
401 for(
unsigned int j=0;
j<polygonSize; ++
j)
412 if(polygonsLowRes.size())
414 size_t polygonSize = polygonsLowRes[0].vertices.size();
416 indexes[1].resize(polygonsLowRes.size() * polygonSize);
418 indexes[3].resize(indexes[1].
size()*2);
421 for(
unsigned int i=0;
i<polygonsLowRes.size(); ++
i)
424 for(
unsigned int j=0;
j<polygonSize; ++
j)
437 for(
size_t i=0;
i<indexes.size(); ++
i)
444 LOGE(
"OpenGL: could not generate index buffer %ld\n",
i);
448 LOGD(
"Adding polygon index %ld size=%ld",
i, indexes[
i].
size());
451 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
uint32_t) * indexes[
i].
size(), indexes[
i].
data(), GL_STATIC_DRAW);
452 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
455 GLint
error = glGetError();
456 if(
error != GL_NO_ERROR)
458 LOGE(
"OpenGL: Could not allocate indexes (0x%x)\n",
error);
469 UASSERT(cloud.get() && !cloud->empty());
501 LOGE(
"OpenGL: could not generate vertex buffers\n");
507 size_t totalPoints = 0;
508 std::vector<GLuint> verticesLowRes;
509 std::vector<GLuint> verticesLowLowRes;
514 verticesLowRes.resize(cloud->isOrganized()?totalPoints:0);
515 verticesLowLowRes.resize(cloud->isOrganized()?totalPoints:0);
520 const pcl::PointXYZRGB & pt = cloud->at(
indices->at(
i));
528 if(cloud->isOrganized())
532 verticesLowRes[oi_low++] =
i;
536 verticesLowLowRes[oi_lowlow++] =
i;
540 verticesLowRes.resize(oi_low);
541 verticesLowLowRes.resize(oi_lowlow);
545 totalPoints = cloud->size();
547 verticesLowRes.resize(cloud->isOrganized()?totalPoints:0);
548 verticesLowLowRes.resize(cloud->isOrganized()?totalPoints:0);
551 for(
unsigned int i=0;
i<cloud->size(); ++
i)
553 const pcl::PointXYZRGB & pt = cloud->at(
i);
561 if(cloud->isOrganized())
565 verticesLowRes[oi_low++] =
i;
569 verticesLowLowRes[oi_lowlow++] =
i;
573 verticesLowRes.resize(oi_low);
574 verticesLowLowRes.resize(oi_lowlow);
578 glBufferData(GL_ARRAY_BUFFER,
sizeof(GLfloat) * (
int)
vertices.size(), (
const void *)
vertices.data(), GL_STATIC_DRAW);
579 glBindBuffer(GL_ARRAY_BUFFER, 0);
581 GLint
error = glGetError();
582 if(
error != GL_NO_ERROR)
584 LOGE(
"OpenGL: Could not allocate point cloud (0x%x)\n",
error);
590 for(
size_t i=4;
i<5; ++
i)
592 if((
i==4 && !verticesLowRes.empty()) ||
593 (
i==5 && !verticesLowLowRes.empty()))
598 LOGE(
"OpenGL: could not generate index buffer %ld\n",
i);
604 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
uint32_t) *
index_buffers_count_[
i],
i==4?verticesLowRes.data():verticesLowLowRes.data(), GL_STATIC_DRAW);
605 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
608 GLint
error = glGetError();
609 if(
error != GL_NO_ERROR)
611 LOGE(
"OpenGL: Could not allocate indexes (0x%x)\n",
error);
649 bool textureUpdate =
false;
658 textureUpdate =
true;
664 LOGE(
"OpenGL: could not generate vertex buffers\n");
674 LOGE(
"OpenGL: could not generate texture buffers\n");
682 std::vector<pcl::Vertices> polygons = mesh.
polygons;
683 std::vector<pcl::Vertices> polygonsLowRes;
686 if(mesh.
cloud->isOrganized())
691 std::vector<GLuint> verticesLowRes;
692 std::vector<GLuint> verticesLowLowRes;
693 verticesLowRes.resize(totalPoints);
694 verticesLowLowRes.resize(totalPoints);
701 for(
unsigned int i=0;
i<mesh.
indices->size(); ++
i)
703 const pcl::PointXYZRGB & pt = mesh.
cloud->at(mesh.
indices->at(
i));
730 verticesLowRes[oi_low++] =
i;
734 verticesLowLowRes[oi_lowlow++] =
i;
743 for(
unsigned int i=0;
i<mesh.
indices->size(); ++
i)
745 const pcl::PointXYZRGB & pt = mesh.
cloud->at(mesh.
indices->at(
i));
765 verticesLowRes[oi_low++] =
i;
769 verticesLowLowRes[oi_lowlow++] =
i;
773 verticesLowRes.resize(oi_low);
774 verticesLowLowRes.resize(oi_lowlow);
777 for(
size_t i=4;
i<5; ++
i)
779 if((
i==4 && !verticesLowRes.empty()) ||
780 (
i==5 && !verticesLowLowRes.empty()))
785 LOGE(
"OpenGL: could not generate index buffer %ld\n",
i);
791 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
uint32_t) *
index_buffers_count_[
i],
i==4?verticesLowRes.data():verticesLowLowRes.data(), GL_STATIC_DRAW);
792 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
794 GLint
error = glGetError();
795 if(
error != GL_NO_ERROR)
797 LOGE(
"OpenGL: Could not allocate indexes (0x%x)\n",
error);
821 uFormat(
"%d vs %d x %d", (
int)mesh.
texCoords.size(), (
int)polygons[0].vertices.size(), (
int)polygons.size()).c_str());
824 for(
unsigned int i=0;
i<polygons.size(); ++
i)
826 pcl::Vertices &
v = polygons[
i];
827 for(
unsigned int j=0;
j<
v.vertices.size(); ++
j)
832 const pcl::PointXYZRGB & pt = mesh.
cloud->at(
v.vertices[
j]);
862 v.vertices[
j] = (
int)oi;
873 totalPoints = (
int)mesh.
cloud->size();
878 for(
unsigned int i=0;
i<mesh.
cloud->size(); ++
i)
880 const pcl::PointXYZRGB & pt = mesh.
cloud->at(
i);
902 glBufferData(GL_ARRAY_BUFFER,
sizeof(GLfloat) * (
int)
vertices.size(), (
const void *)
vertices.data(), GL_STATIC_DRAW);
903 glBindBuffer(GL_ARRAY_BUFFER, 0);
905 GLint
error = glGetError();
906 if(
error != GL_NO_ERROR)
908 LOGE(
"OpenGL: Could not allocate point cloud (0x%x)\n",
error);
923 glBindTexture(GL_TEXTURE_2D,
texture_);
924 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
925 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
926 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
927 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
929 cv::cvtColor(mesh.
texture, rgbImage, cv::COLOR_BGR2RGBA);
931 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
935 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rgbImage.cols, rgbImage.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgbImage.data);
937 GLint
error = glGetError();
938 if(
error != GL_NO_ERROR)
940 LOGE(
"OpenGL: Could not allocate texture (0x%x)\n",
error);
974 pcl::PointCloud<pcl::PointXYZ>
corners;
985 pcl::PointCloud<pcl::PointXYZ> cornersTransformed;
990 for(
unsigned int i=0;
i<cornersTransformed.size(); ++
i)
1002 bool textureRendering,
1004 float distanceToCameraSqr,
1005 const GLuint & depthTexture,
1008 float nearClipPlane,
1010 bool packDepthToColorChannel,
1011 bool wireFrame)
const
1020 if(packDepthToColorChannel || !(meshRendering && textureRendering &&
texture_))
1022 textureRendering =
false;
1026 if(packDepthToColorChannel)
1030 else if(textureRendering)
1053 glUseProgram(program);
1056 GLuint mvp_handle = glGetUniformLocation(program,
"uMVP");
1058 glm::mat4 mvp_mat = projectionMatrix * mv_mat;
1061 GLint attribute_vertex = glGetAttribLocation(program,
"aVertex");
1062 glEnableVertexAttribArray(attribute_vertex);
1063 GLint attribute_color = 0;
1064 GLint attribute_texture = 0;
1065 GLint attribute_normal = 0;
1067 if(packDepthToColorChannel || !textureRendering)
1069 GLuint point_size_handle_ = glGetUniformLocation(program,
"uPointSize");
1070 glUniform1f(point_size_handle_, pointSize);
1074 if(!packDepthToColorChannel)
1076 GLuint gainR_handle = glGetUniformLocation(program,
"uGainR");
1077 GLuint gainG_handle = glGetUniformLocation(program,
"uGainG");
1078 GLuint gainB_handle = glGetUniformLocation(program,
"uGainB");
1079 glUniform1f(gainR_handle,
gainR_);
1080 glUniform1f(gainG_handle,
gainG_);
1081 glUniform1f(gainB_handle,
gainB_);
1084 if(depthTexture > 0)
1087 glActiveTexture(GL_TEXTURE1);
1089 glBindTexture(GL_TEXTURE_2D, depthTexture);
1091 GLuint depth_texture_handle = glGetUniformLocation(program,
"uDepthTexture");
1092 glUniform1i(depth_texture_handle, 1);
1094 GLuint zNear_handle = glGetUniformLocation(program,
"uNearZ");
1095 GLuint zFar_handle = glGetUniformLocation(program,
"uFarZ");
1096 glUniform1f(zNear_handle, nearClipPlane);
1097 glUniform1f(zFar_handle, farClipPlane);
1099 GLuint screenScale_handle = glGetUniformLocation(program,
"uScreenScale");
1100 glUniform2f(screenScale_handle, 1.0
f/(
float)screenWidth, 1.0
f/(
float)screenHeight);
1105 GLuint n_handle = glGetUniformLocation(program,
"uN");
1108 normalMatrix = glm::transpose(normalMatrix);
1111 GLuint lightingDirection_handle = glGetUniformLocation(program,
"uLightingDirection");
1112 glUniform3f(lightingDirection_handle, 0.0, 0.0, 1.0);
1114 attribute_normal = glGetAttribLocation(program,
"aNormal");
1115 glEnableVertexAttribArray(attribute_normal);
1118 if(textureRendering)
1121 glActiveTexture(GL_TEXTURE0);
1123 glBindTexture(GL_TEXTURE_2D,
texture_);
1125 GLuint texture_handle = glGetUniformLocation(program,
"uTexture");
1126 glUniform1i(texture_handle, 0);
1128 attribute_texture = glGetAttribLocation(program,
"aTexCoord");
1129 glEnableVertexAttribArray(attribute_texture);
1133 attribute_color = glGetAttribLocation(program,
"aColor");
1134 glEnableVertexAttribArray(attribute_color);
1142 glVertexAttribPointer(attribute_vertex, 3, GL_FLOAT,
GL_FALSE, (
hasNormals_?9:6)*
sizeof(GLfloat), 0);
1143 if(textureRendering)
1145 glVertexAttribPointer(attribute_texture, 2, GL_FLOAT,
GL_FALSE, (
hasNormals_?9:6)*
sizeof(GLfloat), (GLvoid*) (4 *
sizeof(GLfloat)));
1147 else if(!packDepthToColorChannel)
1149 glVertexAttribPointer(attribute_color, 3, GL_UNSIGNED_BYTE, GL_TRUE, (
hasNormals_?9:6)*
sizeof(GLfloat), (GLvoid*) (3 *
sizeof(GLfloat)));
1153 glVertexAttribPointer(attribute_normal, 3, GL_FLOAT,
GL_FALSE, 9*
sizeof(GLfloat), (GLvoid*) (6 *
sizeof(GLfloat)));
1158 glVertexAttribPointer(attribute_vertex, 3, GL_FLOAT,
GL_FALSE, (
hasNormals_?7:4)*
sizeof(GLfloat), 0);
1159 if(!packDepthToColorChannel)
1161 glVertexAttribPointer(attribute_color, 3, GL_UNSIGNED_BYTE, GL_TRUE, (
hasNormals_?7:4)*
sizeof(GLfloat), (GLvoid*) (3 *
sizeof(GLfloat)));
1165 glVertexAttribPointer(attribute_normal, 3, GL_FLOAT,
GL_FALSE, 7*
sizeof(GLfloat), (GLvoid*) (4 *
sizeof(GLfloat)));
1173 float dist = meshRendering?50.0f:16.0f;
1210 else if(distanceToCameraSqr>150.0
f)
1217 glDrawArrays(GL_POINTS, 0,
nPoints_);
1222 glDrawArrays(GL_POINTS, 0,
nPoints_);
1227 glDisableVertexAttribArray(0);
1228 glBindBuffer(GL_ARRAY_BUFFER, 0);
1229 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);