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
tango_gl::kVertices
static const GLfloat kVertices[]
Definition: video_overlay.cpp:22
tango_gl::DrawableObject::attrib_vertices_
GLuint attrib_vertices_
Definition: drawable_object.h:59
tango_gl::shaders::GetVideoOverlayVertexShader
std::string GetVideoOverlayVertexShader()
Definition: shaders.cpp:54
video_overlay.h
tango_gl::util::CheckGlError
void CheckGlError(const char *operation)
Definition: util.cpp:43
glm::value_ptr
GLM_FUNC_DECL const genType::value_type * value_ptr(genType const &vec)
tango_gl::VideoOverlay::texture_id_
GLuint texture_id_
Definition: video_overlay.h:32
tango_gl::VideoOverlay::attrib_texture_coords_
GLuint attrib_texture_coords_
Definition: video_overlay.h:34
glm::detail::tmat4x4
Definition: type_mat.hpp:47
tango_gl::VideoOverlay::vertex_buffers_
GLuint vertex_buffers_[3]
Definition: video_overlay.h:36
LOGE
#define LOGE(...)
Definition: tango-gl/include/tango-gl/util.h:61
GL_FALSE
#define GL_FALSE
Definition: dummy.cpp:79
GL_VERTEX_PROGRAM_POINT_SIZE
#define GL_VERTEX_PROGRAM_POINT_SIZE
Definition: tango-gl/include/tango-gl/util.h:21
shaders.h
tango_gl::util::CreateProgram
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
tango_gl::shaders::GetVideoOverlayFragmentShader
std::string GetVideoOverlayFragmentShader()
Definition: shaders.cpp:67
tango_gl::kIndices
static const GLushort kIndices[]
Definition: video_overlay.cpp:28
tango_gl::VideoOverlay::VideoOverlay
VideoOverlay()
Definition: video_overlay.cpp:34
tango_gl::VideoOverlay::uniform_texture_
GLuint uniform_texture_
Definition: video_overlay.h:35
c_str
const char * c_str(Args &&...args)
tango_gl::DrawableObject::uniform_mvp_mat_
GLuint uniform_mvp_mat_
Definition: drawable_object.h:58
tango_gl::kTextureCoords
static const GLfloat kTextureCoords[]
Definition: video_overlay.cpp:31
glUniformMatrix4fv
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
tango_gl
Definition: axis.cpp:20
tango_gl::DrawableObject::shader_program_
GLuint shader_program_
Definition: drawable_object.h:56
tango_gl::Transform::GetTransformationMatrix
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
tango_gl::VideoOverlay::Render
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
Definition: video_overlay.cpp:86


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