background_renderer.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // This modules handles drawing the passthrough camera image into the OpenGL
18 // scene.
19 
20 #include "background_renderer.h"
21 
22 #include <type_traits>
23 
24 namespace {
25 
26 const std::string kVertexShader =
27  "attribute vec4 a_Position;\n"
28  "attribute vec2 a_TexCoord;\n"
29 
30  "varying vec2 v_TexCoord;\n"
31 
32  "void main() {\n"
33  " gl_Position = a_Position;\n"
34  " v_TexCoord = a_TexCoord;\n"
35  "}\n";
36 
37 const std::string kFragmentShaderOES =
38  "#extension GL_OES_EGL_image_external : require\n"
39  "precision mediump float;\n"
40  "varying vec2 v_TexCoord;\n"
41  "uniform samplerExternalOES sTexture;\n"
42  "uniform bool uRedUnknown;\n"
43  "void main() {\n"
44  " vec4 sample = texture2D(sTexture, v_TexCoord);\n"
45  " float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;\n"
46  " gl_FragColor = vec4(grey, uRedUnknown?0.0:grey, uRedUnknown?0.0:grey, 0.5);\n"
47  "}\n";
48 
49 const std::string kFragmentShaderBlendingOES =
50  "#extension GL_OES_EGL_image_external : require\n"
51  "precision mediump float;\n"
52  "varying vec2 v_TexCoord;\n"
53  "uniform samplerExternalOES sTexture;\n"
54  "uniform sampler2D uDepthTexture;\n"
55  "uniform vec2 uScreenScale;\n"
56  "uniform bool uRedUnknown;\n"
57  "void main() {\n"
58  " vec4 sample = texture2D(sTexture, v_TexCoord);\n"
59  " vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
60  " vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
61  " float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
62  " if(depth > 0.0)\n"
63  " gl_FragColor = vec4(sample.r, sample.g, sample.b, 0.5);\n"
64  " else {\n"
65  " float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;\n"
66  " gl_FragColor = vec4(grey, uRedUnknown?0.0:grey, uRedUnknown?0.0:grey, 0.5);\n"
67  " }\n"
68  "}\n";
69 
70 const std::string kFragmentShader =
71  "precision mediump float;\n"
72  "varying vec2 v_TexCoord;\n"
73  "uniform sampler2D sTexture;\n"
74  "uniform bool uRedUnknown;\n"
75  "void main() {\n"
76  " vec4 sample = texture2D(sTexture, v_TexCoord);\n"
77  " float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;\n"
78  " gl_FragColor = vec4(grey, uRedUnknown?0.0:grey, uRedUnknown?0.0:grey, 0.5);\n"
79  "}\n";
80 
81 const std::string kFragmentShaderBlending =
82  "precision mediump float;\n"
83  "varying vec2 v_TexCoord;\n"
84  "uniform sampler2D sTexture;\n"
85  "uniform sampler2D uDepthTexture;\n"
86  "uniform vec2 uScreenScale;\n"
87  "uniform bool uRedUnknown;\n"
88  "void main() {\n"
89  " vec4 sample = texture2D(sTexture, v_TexCoord);\n"
90  " vec2 coord = uScreenScale * gl_FragCoord.xy;\n;"
91  " vec4 depthPacked = texture2D(uDepthTexture, coord);\n"
92  " float depth = dot(depthPacked, 1./vec4(1.,255.,65025.,16581375.));\n"
93  " if(depth > 0.0)\n"
94  " gl_FragColor = vec4(sample.r, sample.g, sample.b, 0.5);\n"
95  " else {\n"
96  " float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;\n"
97  " gl_FragColor = vec4(grey, uRedUnknown?0.0:grey, uRedUnknown?0.0:grey, 0.5);\n"
98  " }\n"
99  "}\n";
100 
101 /* To debug depth texture
102  const std::string kFragmentShader =
103  "precision mediump float;\n"
104  "varying vec2 v_TexCoord;\n"
105  "uniform sampler2D sTexture;\n"
106 
107  "void main() {\n"
108  " float uNearZ = 0.2;\n"
109  " float uFarZ = 1000.0;\n"
110  " float depth = texture2D(sTexture, v_TexCoord).r;\n"
111  " float num = (2.0 * uNearZ * uFarZ);\n"
112  " float diff = (uFarZ - uNearZ);\n"
113  " float add = (uFarZ + uNearZ);\n"
114  " float ndcDepth = depth * 2.0 - 1.0;\n" // Back to NDC
115  " float linearDepth = num / (add - ndcDepth * diff);\n" // inverse projection matrix
116  " float grey = linearDepth/3.0;\n"
117  " gl_FragColor = vec4(grey, grey, grey, 0.5);\n"
118  "}\n";
119  */
120 
121 } // namespace
122 
123 std::vector<GLuint> BackgroundRenderer::shaderPrograms_;
124 
126 {
127  for(unsigned int i=0; i<shaderPrograms_.size(); ++i)
128  {
129  glDeleteShader(shaderPrograms_[i]);
130  }
131  shaderPrograms_.clear();
132 }
133 
135 {
136  LOGI("textureId=%d", textureId);
137 
138  texture_id_ = textureId;
139 #ifdef __ANDROID__
140  oes_ = oes;
141 #endif
142 
143  if(shaderPrograms_.empty())
144  {
145  shaderPrograms_.resize(2,0);
147  kVertexShader.c_str(),
148  oes_?kFragmentShaderOES.c_str():kFragmentShader.c_str());
149  UASSERT(shaderPrograms_[0]!=0);
151  kVertexShader.c_str(),
152  oes_?kFragmentShaderBlendingOES.c_str():kFragmentShaderBlending.c_str());
153  UASSERT(shaderPrograms_[1]!=0);
154  }
155 }
156 
157 void BackgroundRenderer::Draw(const float * transformed_uvs, const GLuint & depthTexture, int screenWidth, int screenHeight, bool redUnknown) {
158  static_assert(std::extent<decltype(BackgroundRenderer_kVerticesDevice)>::value == kNumVertices * 2, "Incorrect kVertices length");
159 
160  GLuint program = shaderPrograms_[depthTexture>0?1:0];
161 
162  glUseProgram(program);
163  glDepthMask(GL_FALSE);
164  glEnable (GL_BLEND);
165 
166  glActiveTexture(GL_TEXTURE0);
167 #ifdef __ANDROID__
168  if(oes_)
169  glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
170  else
171 #endif
172  glBindTexture(GL_TEXTURE_2D, texture_id_);
173 
174  if(depthTexture>0)
175  {
176  // Texture activate unit 1
177  glActiveTexture(GL_TEXTURE1);
178  // Bind the texture to this unit.
179  glBindTexture(GL_TEXTURE_2D, depthTexture);
180  // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 1.
181  GLuint depth_texture_handle = glGetUniformLocation(program, "uDepthTexture");
182  glUniform1i(depth_texture_handle, 1);
183 
184  GLuint screenScale_handle = glGetUniformLocation(program, "uScreenScale");
185  glUniform2f(screenScale_handle, 1.0f/(float)screenWidth, 1.0f/(float)screenHeight);
186  }
187 
188  GLuint screenScale_handle = glGetUniformLocation(program, "uRedUnknown");
189  glUniform1i(screenScale_handle, redUnknown);
190 
191  GLuint attributeVertices = glGetAttribLocation(program, "a_Position");
192  GLuint attributeUvs = glGetAttribLocation(program, "a_TexCoord");
193 
194  glVertexAttribPointer(attributeVertices, 2, GL_FLOAT, GL_FALSE, 0, BackgroundRenderer_kVerticesDevice);
195  glVertexAttribPointer(attributeUvs, 2, GL_FLOAT, GL_FALSE, 0, transformed_uvs?transformed_uvs:BackgroundRenderer_kTexCoord);
196 
197  glEnableVertexAttribArray(attributeVertices);
198  glEnableVertexAttribArray(attributeUvs);
199 
200  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
201 
202  glDisableVertexAttribArray(attributeVertices);
203  glDisableVertexAttribArray(attributeUvs);
204 
205  glUseProgram(0);
206  glDepthMask(GL_TRUE);
207  glDisable (GL_BLEND);
208  tango_gl::util::CheckGlError("BackgroundRenderer::Draw() error");
209 }
210 
BackgroundRenderer::Draw
void Draw(const float *transformed_uvs, const GLuint &depthTexture, int screenWidth, int screenHeight, bool redUnknown)
Definition: background_renderer.cc:157
background_renderer.h
BackgroundRenderer_kTexCoord
static const GLfloat BackgroundRenderer_kTexCoord[]
Definition: background_renderer.h:40
BackgroundRenderer_kVerticesDevice
static const GLfloat BackgroundRenderer_kVerticesDevice[]
Definition: background_renderer.h:31
BackgroundRenderer::~BackgroundRenderer
~BackgroundRenderer()
Definition: background_renderer.cc:125
tango_gl::util::CheckGlError
void CheckGlError(const char *operation)
Definition: util.cpp:43
BackgroundRenderer::oes_
bool oes_
Definition: background_renderer.h:73
GL_FALSE
#define GL_FALSE
Definition: dummy.cpp:79
GLuint
unsigned int GLuint
Definition: dummy.cpp:78
tango_gl::util::CreateProgram
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
tango_gl::kFragmentShader
static const char kFragmentShader[]
Definition: quad.cpp:32
BackgroundRenderer::InitializeGlContent
void InitializeGlContent(GLuint textureId, bool oes)
Definition: background_renderer.cc:134
tango_gl::kVertexShader
static const char kVertexShader[]
Definition: quad.cpp:22
BackgroundRenderer::shaderPrograms_
static std::vector< GLuint > shaderPrograms_
Definition: background_renderer.h:71
UASSERT
#define UASSERT(condition)
BackgroundRenderer::texture_id_
GLuint texture_id_
Definition: background_renderer.h:72
LOGI
#define LOGI(...)
Definition: tango-gl/include/tango-gl/util.h:53
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
BackgroundRenderer::kNumVertices
static constexpr int kNumVertices
Definition: background_renderer.h:56
value
value
i
int i


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:06