00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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
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
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
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
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
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
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
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 }