video_overlay.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2014 Google Inc. All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "tango-gl/video_overlay.h"
00018 #include "tango-gl/shaders.h"
00019 
00020 namespace tango_gl {
00021 
00022 static const GLfloat kVertices[] =
00023   {-1.0,  1.0, 0.0,
00024    -1.0, -1.0, 0.0,
00025     1.0,  1.0, 0.0,
00026     1.0, -1.0, 0.0};
00027 
00028 static const GLushort kIndices[] =
00029   {0, 1, 2, 2, 1, 3};
00030 
00031 static const GLfloat kTextureCoords[] =
00032   {0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0};
00033 
00034 VideoOverlay::VideoOverlay() {
00035   glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
00036   shader_program_ =
00037       util::CreateProgram(shaders::GetVideoOverlayVertexShader().c_str(),
00038                           shaders::GetVideoOverlayFragmentShader().c_str());
00039   if (!shader_program_) {
00040     LOGE("Could not create program.");
00041   }
00042 
00043   glGenTextures(1, &texture_id_);
00044   glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
00045   glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00046   glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00047   uniform_texture_ = glGetUniformLocation(shader_program_, "texture");
00048 
00049   glGenBuffers(3, vertex_buffers_);
00050   // Allocate vertices buffer.
00051   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
00052   glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 4, kVertices,
00053                GL_STATIC_DRAW);
00054   glBindBuffer(GL_ARRAY_BUFFER, 0);
00055 
00056   // Allocate triangle indices buffer.
00057   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_buffers_[1]);
00058   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * 6, kIndices,
00059                GL_STATIC_DRAW);
00060   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
00061 
00062   // Allocate texture coordinates buufer.
00063   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[2]);
00064   glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 4, kTextureCoords,
00065                GL_STATIC_DRAW);
00066   glBindBuffer(GL_ARRAY_BUFFER, 0);
00067 
00068   // Assign the vertices attribute data.
00069   attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
00070   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
00071   glEnableVertexAttribArray(attrib_vertices_);
00072   glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
00073   glBindBuffer(GL_ARRAY_BUFFER, 0);
00074 
00075   // Assign the texture coordinates attribute data.
00076   attrib_texture_coords_ = glGetAttribLocation(shader_program_, "textureCoords");
00077   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[2]);
00078   glEnableVertexAttribArray(attrib_texture_coords_);
00079   glVertexAttribPointer(attrib_texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
00080                         nullptr);
00081   glBindBuffer(GL_ARRAY_BUFFER, 0);
00082 
00083   uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
00084 }
00085 
00086 void VideoOverlay::Render(const glm::mat4& projection_mat,
00087                           const glm::mat4& view_mat) const {
00088   glUseProgram(shader_program_);
00089 
00090   glUniform1i(uniform_texture_, 0);
00091   glActiveTexture(GL_TEXTURE0);
00092   glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
00093 
00094   glm::mat4 model_mat = GetTransformationMatrix();
00095   glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
00096   glUniformMatrix4fv(uniform_mvp_mat_, 1, GL_FALSE, glm::value_ptr(mvp_mat));
00097 
00098   // Bind vertices buffer.
00099   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[0]);
00100   glEnableVertexAttribArray(attrib_vertices_);
00101   glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
00102   glBindBuffer(GL_ARRAY_BUFFER, 0);
00103 
00104   // Bind texture coordinates buffer.
00105   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers_[2]);
00106   glEnableVertexAttribArray(attrib_texture_coords_);
00107   glVertexAttribPointer(attrib_texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
00108                         nullptr);
00109   glBindBuffer(GL_ARRAY_BUFFER, 0);
00110 
00111   // Bind element array buffer.
00112   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_buffers_[1]);
00113   glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
00114   util::CheckGlError("glDrawElements");
00115   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
00116 
00117   glUseProgram(0);
00118   util::CheckGlError("glUseProgram()");
00119 }
00120 
00121 }  // namespace tango_gl


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:32