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 kFragmentShader =
38  "#extension GL_OES_EGL_image_external : require\n"
39 
40  "precision mediump float;\n"
41  "varying vec2 v_TexCoord;\n"
42  "uniform samplerExternalOES sTexture;\n"
43 
44  "void main() {\n"
45  " vec4 sample = texture2D(sTexture, v_TexCoord);\n"
46  " float grey = 0.21 * sample.r + 0.71 * sample.g + 0.07 * sample.b;\n"
47  " gl_FragColor = vec4(grey, grey, grey, 0.5);\n"
48  "}\n";
49 
50 } // namespace
51 
53 {
54  texture_id_ = textureId;
55 
57  if (!shader_program_) {
58  LOGE("Could not create program.");
59  }
60  glUseProgram(shader_program_);
61  attribute_vertices_ = glGetAttribLocation(shader_program_, "a_Position");
62  attribute_uvs_ = glGetAttribLocation(shader_program_, "a_TexCoord");
63  glUseProgram(0);
64 }
65 
66 void BackgroundRenderer::Draw(const float * transformed_uvs) {
67  static_assert(std::extent<decltype(BackgroundRenderer_kVertices)>::value == kNumVertices * 2, "Incorrect kVertices length");
68 
69  glUseProgram(shader_program_);
70  glDepthMask(GL_FALSE);
71  glEnable (GL_BLEND);
72 
73  glActiveTexture(GL_TEXTURE0);
74  glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
75 
76  glVertexAttribPointer(attribute_vertices_, 2, GL_FLOAT, GL_FALSE, 0, BackgroundRenderer_kVertices);
77  glVertexAttribPointer(attribute_uvs_, 2, GL_FLOAT, GL_FALSE, 0, transformed_uvs);
78 
79  glEnableVertexAttribArray(attribute_vertices_);
80  glEnableVertexAttribArray(attribute_uvs_);
81 
82  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
83 
84  glDisableVertexAttribArray(attribute_vertices_);
85  glDisableVertexAttribArray(attribute_uvs_);
86 
87  glUseProgram(0);
88  glDepthMask(GL_TRUE);
89  glDisable (GL_BLEND);
90  tango_gl::util::CheckGlError("BackgroundRenderer::Draw() error");
91 }
92 
static const char kFragmentShader[]
Definition: quad.cpp:32
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
#define LOGE(...)
static constexpr int kNumVertices
unsigned int GLuint
Definition: dummy.cpp:78
static const GLfloat BackgroundRenderer_kVertices[]
void InitializeGlContent(GLuint textureId)
#define GL_FALSE
Definition: dummy.cpp:79
void CheckGlError(const char *operation)
Definition: util.cpp:43
static const char kVertexShader[]
Definition: quad.cpp:22
void Draw(const float *transformed_uvs)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:58