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