quad.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/quad.h"
00018 #include "tango-gl/util.h"
00019 
00020 namespace tango_gl {
00021 
00022 static const char kVertexShader[] =
00023     "attribute vec4 vertex;\n"
00024     "attribute vec2 inputTextureCoordinate;\n"
00025     "varying vec2 textureCoordinate;\n"
00026     "uniform mat4 mvp;\n"
00027     "void main() {\n"
00028     "  gl_Position = mvp*vertex;\n"
00029     "  textureCoordinate = inputTextureCoordinate.xy;\n"
00030     "}\n";
00031 
00032 static const char kFragmentShader[] =
00033     "varying vec2 textureCoordinate;\n"
00034     "uniform sampler2D inputTexture;\n"
00035     "void main() {\n"
00036     "  gl_FragColor = texture2D(inputTexture, textureCoordinate);\n"
00037     "}\n";
00038 
00039 static const float vertices[] = {-0.5f, -0.5f, 0.5f, -0.5f,
00040                                  -0.5f, 0.5f,  0.5f, 0.5f};
00041 
00042 static const GLfloat texture_coords[] = {0.0f, 1.0f, 1.0f, 1.0f,
00043                                          0.0f, 0.0f, 1.0f, 0.0f, };
00044 
00045 Quad::Quad() {
00046   shader_program_ = util::CreateProgram(kVertexShader, kFragmentShader);
00047   if (!shader_program_) {
00048     LOGE("Could not create program.");
00049   }
00050   uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
00051   attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
00052   texture_coords_ =
00053       glGetAttribLocation(shader_program_, "inputTextureCoordinate");
00054   texture_handle = glGetUniformLocation(shader_program_, "inputTexture");
00055   glGenBuffers(1, &vertex_buffer_);
00056 }
00057 
00058 Quad::~Quad() { glDeleteShader(shader_program_); }
00059 
00060 void Quat::SetTextureId(GLuint texture_id) { texture_id_ = texture_id; }
00061 
00062 void Quad::Render(const glm::mat4& projection_mat,
00063                   const glm::mat4& view_mat) const {
00064   glEnable(GL_CULL_FACE);
00065   glEnable(GL_BLEND);
00066   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00067   glUseProgram(shader_program_);
00068 
00069   glBindTexture(GL_TEXTURE_2D, texture_id_);
00070   glUniform1i(texture_handle, 0);
00071 
00072   // Calculate MVP matrix and pass it to shader.
00073   glm::mat4 model_mat = GetTransformationMatrix();
00074   glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
00075   glUniformMatrix4fv(uniform_mvp_mat_, 1, GL_FALSE, glm::value_ptr(mvp_mat));
00076 
00077   // Vertice binding
00078   glEnableVertexAttribArray(attrib_vertices_);
00079   glVertexAttribPointer(attrib_vertices_, 2, GL_FLOAT, GL_FALSE, 0, vertices);
00080   glBindBuffer(GL_ARRAY_BUFFER, 0);
00081 
00082   glEnableVertexAttribArray(texture_coords_);
00083   glVertexAttribPointer(texture_coords_, 2, GL_FLOAT, GL_FALSE, 0,
00084                         texture_coords);
00085 
00086   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
00087   glUseProgram(0);
00088   glBindTexture(GL_TEXTURE_2D, 0);
00089 }
00090 
00091 }  // namespace tango_gl


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