video_overlay.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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 #include "tango-gl/video_overlay.h"
18 #include "tango-gl/shaders.h"
19 
20 namespace tango_gl {
21 
22 static const GLfloat kVertices[] =
23  {-1.0, 1.0, 0.0,
24  -1.0, -1.0, 0.0,
25  1.0, 1.0, 0.0,
26  1.0, -1.0, 0.0};
27 
28 static const GLushort kIndices[] =
29  {0, 1, 2, 2, 1, 3};
30 
31 static const GLfloat kTextureCoords[] =
32  {0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0};
33 
39  if (!shader_program_) {
40  LOGE("Could not create program.");
41  }
42 
43  glGenTextures(1, &texture_id_);
44  glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
45  glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
46  glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
47  uniform_texture_ = glGetUniformLocation(shader_program_, "texture");
48 
49  glGenBuffers(3, vertex_buffers_);
50  // Allocate vertices buffer.
51  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
52  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 4, kVertices,
53  GL_STATIC_DRAW);
54  glBindBuffer(GL_ARRAY_BUFFER, 0);
55 
56  // Allocate triangle indices buffer.
57  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_buffers_[1]);
58  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * 6, kIndices,
59  GL_STATIC_DRAW);
60  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
61 
62  // Allocate texture coordinates buufer.
63  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[2]);
64  glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 4, kTextureCoords,
65  GL_STATIC_DRAW);
66  glBindBuffer(GL_ARRAY_BUFFER, 0);
67 
68  // Assign the vertices attribute data.
69  attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
70  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
71  glEnableVertexAttribArray(attrib_vertices_);
72  glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
73  glBindBuffer(GL_ARRAY_BUFFER, 0);
74 
75  // Assign the texture coordinates attribute data.
76  attrib_texture_coords_ = glGetAttribLocation(shader_program_, "textureCoords");
77  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[2]);
78  glEnableVertexAttribArray(attrib_texture_coords_);
79  glVertexAttribPointer(attrib_texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
80  nullptr);
81  glBindBuffer(GL_ARRAY_BUFFER, 0);
82 
83  uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
84 }
85 
86 void VideoOverlay::Render(const glm::mat4& projection_mat,
87  const glm::mat4& view_mat) const {
88  glUseProgram(shader_program_);
89 
90  glUniform1i(uniform_texture_, 0);
91  glActiveTexture(GL_TEXTURE0);
92  glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
93 
94  glm::mat4 model_mat = GetTransformationMatrix();
95  glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
97 
98  // Bind vertices buffer.
99  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
100  glEnableVertexAttribArray(attrib_vertices_);
101  glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
102  glBindBuffer(GL_ARRAY_BUFFER, 0);
103 
104  // Bind texture coordinates buffer.
105  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[2]);
106  glEnableVertexAttribArray(attrib_texture_coords_);
107  glVertexAttribPointer(attrib_texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
108  nullptr);
109  glBindBuffer(GL_ARRAY_BUFFER, 0);
110 
111  // Bind element array buffer.
112  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_buffers_[1]);
113  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
114  util::CheckGlError("glDrawElements");
115  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
116 
117  glUseProgram(0);
118  util::CheckGlError("glUseProgram()");
119 }
120 
121 } // namespace tango_gl
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
#define LOGE(...)
GLM_FUNC_DECL genType::value_type const * value_ptr(genType const &vec)
static const GLfloat kTextureCoords[]
static const GLfloat kVertices[]
std::string GetVideoOverlayVertexShader()
Definition: shaders.cpp:54
#define GL_FALSE
Definition: dummy.cpp:79
std::string GetVideoOverlayFragmentShader()
Definition: shaders.cpp:67
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
void CheckGlError(const char *operation)
Definition: util.cpp:43
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
static const GLushort kIndices[]
#define GL_VERTEX_PROGRAM_POINT_SIZE


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06