mirror of
https://github.com/introlab/rtabmap.git
synced 2026-09-02 01:20:25 +08:00
android: fixed z-fighting on some android devices
This commit is contained in:
@@ -57,7 +57,7 @@ enum PointCloudShaders
|
|||||||
|
|
||||||
// PointCloud shaders
|
// PointCloud shaders
|
||||||
const std::string kPointCloudVertexShader =
|
const std::string kPointCloudVertexShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"attribute vec3 aVertex;\n"
|
"attribute vec3 aVertex;\n"
|
||||||
"attribute vec3 aColor;\n"
|
"attribute vec3 aColor;\n"
|
||||||
@@ -75,7 +75,7 @@ const std::string kPointCloudVertexShader =
|
|||||||
" vColor = aColor;\n"
|
" vColor = aColor;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const std::string kPointCloudLightingVertexShader =
|
const std::string kPointCloudLightingVertexShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"attribute vec3 aVertex;\n"
|
"attribute vec3 aVertex;\n"
|
||||||
"attribute vec3 aNormal;\n"
|
"attribute vec3 aNormal;\n"
|
||||||
@@ -100,7 +100,7 @@ const std::string kPointCloudLightingVertexShader =
|
|||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
const std::string kPointCloudFragmentShader =
|
const std::string kPointCloudFragmentShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"uniform float uGainR;\n"
|
"uniform float uGainR;\n"
|
||||||
"uniform float uGainG;\n"
|
"uniform float uGainG;\n"
|
||||||
@@ -127,7 +127,8 @@ const std::string kPointCloudBlendingFragmentShader =
|
|||||||
" vec4 textureColor = vec4(vColor.z, vColor.y, vColor.x, 1.0);\n"
|
" vec4 textureColor = vec4(vColor.z, vColor.y, vColor.x, 1.0);\n"
|
||||||
" float alpha = 1.0;\n"
|
" float alpha = 1.0;\n"
|
||||||
" vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
|
" vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
|
||||||
" float depth = texture2D(uDepthTexture, coord).r;\n"
|
" vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
|
||||||
|
" float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
|
||||||
" float num = (2.0 * uNearZ * uFarZ);\n"
|
" float num = (2.0 * uNearZ * uFarZ);\n"
|
||||||
" float diff = (uFarZ - uNearZ);\n"
|
" float diff = (uFarZ - uNearZ);\n"
|
||||||
" float add = (uFarZ + uNearZ);\n"
|
" float add = (uFarZ + uNearZ);\n"
|
||||||
@@ -141,7 +142,7 @@ const std::string kPointCloudBlendingFragmentShader =
|
|||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
const std::string kPointCloudDepthPackingVertexShader =
|
const std::string kPointCloudDepthPackingVertexShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"attribute vec3 aVertex;\n"
|
"attribute vec3 aVertex;\n"
|
||||||
"uniform mat4 uMVP;\n"
|
"uniform mat4 uMVP;\n"
|
||||||
@@ -154,15 +155,15 @@ const std::string kPointCloudDepthPackingFragmentShader =
|
|||||||
"precision highp float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"void main() {\n"
|
"void main() {\n"
|
||||||
" float toFixed = 255.0/256.0;\n"
|
" vec4 enc = vec4(1.,255.,65025.,16581375.) * gl_FragCoord.z;\n"
|
||||||
" vec4 enc = vec4(1.0, 255.0, 65025.0, 160581375.0) * toFixed * gl_FragCoord.z;\n"
|
|
||||||
" enc = fract(enc);\n"
|
" enc = fract(enc);\n"
|
||||||
|
" enc -= enc.yzww * vec2(1./255., 0.).xxxy;\n"
|
||||||
" gl_FragColor = enc;\n"
|
" gl_FragColor = enc;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
// Texture shaders
|
// Texture shaders
|
||||||
const std::string kTextureMeshVertexShader =
|
const std::string kTextureMeshVertexShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"attribute vec3 aVertex;\n"
|
"attribute vec3 aVertex;\n"
|
||||||
"attribute vec2 aTexCoord;\n"
|
"attribute vec2 aTexCoord;\n"
|
||||||
@@ -185,7 +186,7 @@ const std::string kTextureMeshVertexShader =
|
|||||||
" vLightWeighting = 1.0;\n"
|
" vLightWeighting = 1.0;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const std::string kTextureMeshLightingVertexShader =
|
const std::string kTextureMeshLightingVertexShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"attribute vec3 aVertex;\n"
|
"attribute vec3 aVertex;\n"
|
||||||
"attribute vec3 aNormal;\n"
|
"attribute vec3 aNormal;\n"
|
||||||
@@ -214,7 +215,7 @@ const std::string kTextureMeshLightingVertexShader =
|
|||||||
" vLightWeighting=0.5;\n"
|
" vLightWeighting=0.5;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
const std::string kTextureMeshFragmentShader =
|
const std::string kTextureMeshFragmentShader =
|
||||||
"precision mediump float;\n"
|
"precision highp float;\n"
|
||||||
"precision mediump int;\n"
|
"precision mediump int;\n"
|
||||||
"uniform sampler2D uTexture;\n"
|
"uniform sampler2D uTexture;\n"
|
||||||
"uniform float uGainR;\n"
|
"uniform float uGainR;\n"
|
||||||
@@ -245,7 +246,8 @@ const std::string kTextureMeshBlendingFragmentShader =
|
|||||||
" vec4 textureColor = texture2D(uTexture, vTexCoord);\n"
|
" vec4 textureColor = texture2D(uTexture, vTexCoord);\n"
|
||||||
" float alpha = 1.0;\n"
|
" float alpha = 1.0;\n"
|
||||||
" vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
|
" vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
|
||||||
" float depth = texture2D(uDepthTexture, coord).r;\n"
|
" vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
|
||||||
|
" float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
|
||||||
" float num = (2.0 * uNearZ * uFarZ);\n"
|
" float num = (2.0 * uNearZ * uFarZ);\n"
|
||||||
" float diff = (uFarZ - uNearZ);\n"
|
" float diff = (uFarZ - uNearZ);\n"
|
||||||
" float add = (uFarZ + uNearZ);\n"
|
" float add = (uFarZ + uNearZ);\n"
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ Scene::Scene() :
|
|||||||
g_(0.0f),
|
g_(0.0f),
|
||||||
b_(0.0f),
|
b_(0.0f),
|
||||||
fboId_(0),
|
fboId_(0),
|
||||||
|
rboId_(0),
|
||||||
depthTexture_(0),
|
depthTexture_(0),
|
||||||
screenWidth_(0),
|
screenWidth_(0),
|
||||||
screenHeight_(0),
|
screenHeight_(0),
|
||||||
@@ -176,6 +177,8 @@ void Scene::DeleteResources() {
|
|||||||
{
|
{
|
||||||
glDeleteFramebuffers(1, &fboId_);
|
glDeleteFramebuffers(1, &fboId_);
|
||||||
fboId_ = 0;
|
fboId_ = 0;
|
||||||
|
glDeleteRenderbuffers(1, &rboId_);
|
||||||
|
rboId_ = 0;
|
||||||
glDeleteTextures(1, &depthTexture_);
|
glDeleteTextures(1, &depthTexture_);
|
||||||
depthTexture_ = 0;
|
depthTexture_ = 0;
|
||||||
}
|
}
|
||||||
@@ -220,16 +223,23 @@ void Scene::SetupViewPort(int w, int h) {
|
|||||||
UASSERT(gesture_camera_ != 0);
|
UASSERT(gesture_camera_ != 0);
|
||||||
gesture_camera_->SetWindowSize(static_cast<float>(w), static_cast<float>(h));
|
gesture_camera_->SetWindowSize(static_cast<float>(w), static_cast<float>(h));
|
||||||
glViewport(0, 0, w, h);
|
glViewport(0, 0, w, h);
|
||||||
if(screenWidth_ != w || fboId_ == 0)
|
if(screenWidth_ != w || screenHeight_ != h || fboId_ == 0)
|
||||||
{
|
{
|
||||||
if(fboId_>0)
|
if(fboId_>0)
|
||||||
{
|
{
|
||||||
glDeleteFramebuffers(1, &fboId_);
|
glDeleteFramebuffers(1, &fboId_);
|
||||||
fboId_ = 0;
|
fboId_ = 0;
|
||||||
|
glDeleteRenderbuffers(1, &rboId_);
|
||||||
|
rboId_ = 0;
|
||||||
glDeleteTextures(1, &depthTexture_);
|
glDeleteTextures(1, &depthTexture_);
|
||||||
depthTexture_ = 0;
|
depthTexture_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// regenerate fbo texture
|
||||||
|
// create a framebuffer object, you need to delete them when program exits.
|
||||||
|
glGenFramebuffers(1, &fboId_);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, fboId_);
|
||||||
|
|
||||||
// Create depth texture
|
// Create depth texture
|
||||||
glGenTextures(1, &depthTexture_);
|
glGenTextures(1, &depthTexture_);
|
||||||
glBindTexture(GL_TEXTURE_2D, depthTexture_);
|
glBindTexture(GL_TEXTURE_2D, depthTexture_);
|
||||||
@@ -237,16 +247,17 @@ void Scene::SetupViewPort(int w, int h) {
|
|||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
|
||||||
// regenerate fbo texture
|
glGenRenderbuffers(1, &rboId_);
|
||||||
// create a framebuffer object, you need to delete them when program exits.
|
glBindRenderbuffer(GL_RENDERBUFFER, rboId_);
|
||||||
glGenFramebuffers(1, &fboId_);
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, w, h);
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fboId_);
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||||
|
|
||||||
// Set the texture to be at the depth attachment point of the FBO
|
// Set the texture to be at the color attachment point of the FBO (we pack depth 32 bits in color)
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture_, 0);
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, depthTexture_, 0);
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboId_);
|
||||||
|
|
||||||
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||||
if ( status != GL_FRAMEBUFFER_COMPLETE)
|
if ( status != GL_FRAMEBUFFER_COMPLETE)
|
||||||
@@ -464,14 +475,13 @@ int Scene::Render(const float * uvsTransformed, glm::mat4 arViewMatrix, glm::mat
|
|||||||
// set the rendering destination to FBO
|
// set the rendering destination to FBO
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fboId_);
|
glBindFramebuffer(GL_FRAMEBUFFER, fboId_);
|
||||||
|
|
||||||
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
glClearColor(0, 0, 0, 0);
|
||||||
glClearColor(1, 1, 1, 1);
|
|
||||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
if(renderBackgroundCamera)
|
if(renderBackgroundCamera)
|
||||||
{
|
{
|
||||||
PointCloudDrawable drawable(occlusionMesh);
|
PointCloudDrawable drawable(occlusionMesh);
|
||||||
drawable.Render(projectionMatrix, viewMatrix, true, pointSize_, false, false, 999.0f);
|
drawable.Render(projectionMatrix, viewMatrix, true, pointSize_, false, false, 999.0f, 0, 0, 0, 0, 0, true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -479,13 +489,12 @@ int Scene::Render(const float * uvsTransformed, glm::mat4 arViewMatrix, glm::mat
|
|||||||
for(std::vector<PointCloudDrawable*>::const_iterator iter=cloudsToDraw.begin(); iter!=cloudsToDraw.end(); ++iter)
|
for(std::vector<PointCloudDrawable*>::const_iterator iter=cloudsToDraw.begin(); iter!=cloudsToDraw.end(); ++iter)
|
||||||
{
|
{
|
||||||
// set large distance to cam to use low res polygons for fast processing
|
// set large distance to cam to use low res polygons for fast processing
|
||||||
(*iter)->Render(projectionMatrix, viewMatrix, meshRendering_, pointSize_, false, false, 999.0f);
|
(*iter)->Render(projectionMatrix, viewMatrix, meshRendering_, pointSize_, false, false, 999.0f, 0, 0, 0, 0, 0, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// back to normal window-system-provided framebuffer
|
// back to normal window-system-provided framebuffer
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
|
||||||
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(doubleTapOn_ && gesture_camera_->GetCameraType() != tango_gl::GestureCamera::kFirstPerson)
|
if(doubleTapOn_ && gesture_camera_->GetCameraType() != tango_gl::GestureCamera::kFirstPerson)
|
||||||
@@ -501,8 +510,7 @@ int Scene::Render(const float * uvsTransformed, glm::mat4 arViewMatrix, glm::mat
|
|||||||
|
|
||||||
GLubyte zValue[4];
|
GLubyte zValue[4];
|
||||||
glReadPixels(doubleTapPos_.x*screenWidth_, screenHeight_-doubleTapPos_.y*screenHeight_, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, zValue);
|
glReadPixels(doubleTapPos_.x*screenWidth_, screenHeight_-doubleTapPos_.y*screenHeight_, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, zValue);
|
||||||
float fromFixed = 256.0f/255.0f;
|
float zValueF = float(zValue[0]/255.0f) + float(zValue[1]/255.0f)/255.0f + float(zValue[2]/255.0f)/65025.0f + float(zValue[3]/255.0f)/160581375.0f;
|
||||||
float zValueF = float(zValue[0]/255.0f)*fromFixed + float(zValue[1]/255.0f)*fromFixed/255.0f + float(zValue[2]/255.0f)*fromFixed/65025.0f + float(zValue[3]/255.0f)*fromFixed/160581375.0f;
|
|
||||||
|
|
||||||
if(zValueF != 0.0f)
|
if(zValueF != 0.0f)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -204,6 +204,7 @@ class Scene {
|
|||||||
float g_;
|
float g_;
|
||||||
float b_;
|
float b_;
|
||||||
GLuint fboId_;
|
GLuint fboId_;
|
||||||
|
GLuint rboId_;
|
||||||
GLuint depthTexture_;
|
GLuint depthTexture_;
|
||||||
GLsizei screenWidth_;
|
GLsizei screenWidth_;
|
||||||
GLsizei screenHeight_;
|
GLsizei screenHeight_;
|
||||||
|
|||||||
Reference in New Issue
Block a user