axis.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/axis.h"
00018 #include "tango-gl/shaders.h"
00019 
00020 namespace tango_gl {
00021 
00022 static const float float_vertices[] = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
00023                                        0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
00024                                        0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
00025 
00026 static const float float_colors[] = {
00027     1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
00028     0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f};
00029 
00030 Axis::Axis() : Line(3.0f, GL_LINES) {
00031   // Implement SetShader here, not using the dedault one.
00032   shader_program_ =
00033       util::CreateProgram(shaders::GetColorVertexShader().c_str(),
00034                           shaders::GetBasicFragmentShader().c_str());
00035   if (!shader_program_) {
00036     LOGE("Could not create program.");
00037   }
00038   uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
00039   attrib_colors_ = glGetAttribLocation(shader_program_, "color");
00040   attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
00041 
00042   size_t size = sizeof(float_vertices) / (sizeof(float) * 3);
00043   for (size_t i = 0; i < size; i++) {
00044     vec_vertices_.push_back(glm::vec3(float_vertices[i * 3],
00045                                       float_vertices[i * 3 + 1],
00046                                       float_vertices[i * 3 + 2]));
00047     vec_colors_.push_back(
00048         glm::vec4(float_colors[i * 4], float_colors[i * 4 + 1],
00049                   float_colors[i * 4 + 2], float_colors[i * 4 + 3]));
00050   }
00051 }
00052 
00053 void Axis::Render(const glm::mat4& projection_mat,
00054                   const glm::mat4& view_mat) const {
00055   glUseProgram(shader_program_);
00056   glLineWidth(line_width_);
00057   glm::mat4 model_mat = GetTransformationMatrix();
00058   glm::mat4 mvp_mat = projection_mat * view_mat * model_mat;
00059   glUniformMatrix4fv(uniform_mvp_mat_, 1, GL_FALSE, glm::value_ptr(mvp_mat));
00060 
00061   glEnableVertexAttribArray(attrib_vertices_);
00062   glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE,
00063                         sizeof(glm::vec3), &vec_vertices_[0]);
00064 
00065   glEnableVertexAttribArray(attrib_colors_);
00066   glVertexAttribPointer(attrib_colors_, 4, GL_FLOAT, GL_FALSE,
00067                         sizeof(glm::vec4), &vec_colors_[0]);
00068 
00069   glDrawArrays(render_mode_, 0, vec_vertices_.size());
00070 
00071   glDisableVertexAttribArray(attrib_vertices_);
00072   glDisableVertexAttribArray(attrib_colors_);
00073   glUseProgram(0);
00074 }
00075 }  // namespace tango_gl


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