mesh.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "tango-gl/mesh.h"
18 #include "tango-gl/shaders.h"
19 
20 namespace tango_gl {
22  render_mode_ = GL_TRIANGLES;
23 }
24 Mesh::Mesh(GLenum render_mode) {
25  render_mode_ = render_mode;
26 }
27 
30  // Default mode set to no lighting.
31  is_lighting_on_ = false;
32  // Default mode set to without bounding box detection.
33  is_bounding_box_on_ = false;
34 }
35 
36 void Mesh::SetShader(bool is_lighting_on) {
37  if (is_lighting_on) {
41  if (!shader_program_) {
42  LOGE("Could not create program.");
43  }
44  uniform_mvp_mat_ = glGetUniformLocation(shader_program_, "mvp");
45  uniform_mv_mat_ = glGetUniformLocation(shader_program_, "mv");
46  uniform_light_vec_ = glGetUniformLocation(shader_program_, "lightVec");
47  uniform_color_ = glGetUniformLocation(shader_program_, "color");
48 
49  attrib_vertices_ = glGetAttribLocation(shader_program_, "vertex");
50  attrib_normals_ = glGetAttribLocation(shader_program_, "normal");
51  is_lighting_on_ = true;
52  // Set a defualt direction for directional light.
53  light_direction_ = glm::vec3(-1.0f, -3.0f, -1.0f);
55  } else {
56  SetShader();
57  }
58 }
59 
61  // Traverse all the vertices to define an axis-aligned
62  // bounding box for this mesh, needs to be called after SetVertices().
63  if(vertices_.size()==0){
64  LOGE("Please set up vertices first!");
65  return;
66  }
67  is_bounding_box_on_ = true;
69 }
70 
71 void Mesh::SetLightDirection(const glm::vec3& light_direction) {
72  light_direction_ = light_direction;
73 }
74 
75 bool Mesh::IsIntersecting(const Segment& segment) {
76  // If there is no bounding box defined based on all vertices,
77  // we can not calculate intersection.
78  if (!is_bounding_box_on_) {
79  LOGE("Mesh::IsIntersecting, bounding box is not available.");
80  return false;
81  }
84 }
85 
86 void Mesh::Render(const glm::mat4& projection_mat,
87  const glm::mat4& view_mat) const {
88  glUseProgram(shader_program_);
89  glm::mat4 model_mat = GetTransformationMatrix();
90  glm::mat4 mv_mat = view_mat * model_mat;
91  glm::mat4 mvp_mat = projection_mat * mv_mat;
93  glUniform4f(uniform_color_, red_, green_, blue_, alpha_);
94 
95  if (is_lighting_on_) {
97 
98  glEnableVertexAttribArray(attrib_normals_);
99  glVertexAttribPointer(attrib_normals_, 3, GL_FLOAT, GL_FALSE,
100  3 * sizeof(GLfloat), &normals_[0]);
101  glm::vec3 light_direction = glm::mat3(view_mat) * light_direction_;
102  glUniform3fv(uniform_light_vec_, 1, glm::value_ptr(light_direction));
103  }
104 
105  glEnableVertexAttribArray(attrib_vertices_);
106 
107  if (!indices_.empty()) {
108  glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE,
109  3 * sizeof(GLfloat), vertices_.data());
110  glDrawElements(render_mode_, indices_.size(), GL_UNSIGNED_SHORT,
111  indices_.data());
112  } else {
113  glVertexAttribPointer(attrib_vertices_, 3, GL_FLOAT, GL_FALSE,
114  3 * sizeof(GLfloat), &vertices_[0]);
115  glDrawArrays(render_mode_, 0, vertices_.size() / 3);
116  }
117 
118  glDisableVertexAttribArray(attrib_vertices_);
119  if (is_lighting_on_) {
120  glDisableVertexAttribArray(attrib_normals_);
121  }
122  glUseProgram(0);
123 }
124 } // namespace tango_gl
tango_gl::Mesh::is_bounding_box_on_
bool is_bounding_box_on_
Definition: mesh.h:39
tango_gl::BoundingBox::IsIntersecting
bool IsIntersecting(const Segment &segment, const glm::quat &rotation, const glm::mat4 &transformation)
Definition: bounding_box.cpp:20
tango_gl::DrawableObject::alpha_
float alpha_
Definition: drawable_object.h:50
tango_gl::DrawableObject::attrib_vertices_
GLuint attrib_vertices_
Definition: drawable_object.h:59
tango_gl::DrawableObject::red_
float red_
Definition: drawable_object.h:47
tango_gl::shaders::GetBasicFragmentShader
std::string GetBasicFragmentShader()
Definition: shaders.cpp:33
tango_gl::Mesh::is_lighting_on_
bool is_lighting_on_
Definition: mesh.h:38
tango_gl::shaders::GetShadedVertexShader
std::string GetShadedVertexShader()
Definition: shaders.cpp:78
glm::value_ptr
GLM_FUNC_DECL const genType::value_type * value_ptr(genType const &vec)
glm::vec3
highp_vec3 vec3
Definition: type_vec.hpp:392
tango_gl::DrawableObject::normals_
std::vector< GLfloat > normals_
Definition: drawable_object.h:53
tango_gl::Mesh::uniform_light_vec_
GLuint uniform_light_vec_
Definition: mesh.h:42
glm::normalize
GLM_FUNC_DECL genType normalize(genType const &x)
tango_gl::DrawableObject::blue_
float blue_
Definition: drawable_object.h:49
glm::detail::tvec3
Definition: type_mat.hpp:37
glm::detail::tmat4x4
Definition: type_mat.hpp:47
tango_gl::DrawableObject::green_
float green_
Definition: drawable_object.h:48
tango_gl::DrawableObject::render_mode_
GLenum render_mode_
Definition: drawable_object.h:55
LOGE
#define LOGE(...)
Definition: tango-gl/include/tango-gl/util.h:61
glm::mat3
mat3x3 mat3
Definition: type_mat.hpp:436
tango_gl::Segment
Definition: segment.h:23
GL_FALSE
#define GL_FALSE
Definition: dummy.cpp:79
shaders.h
tango_gl::util::CreateProgram
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
tango_gl::DrawableObject::uniform_color_
GLuint uniform_color_
Definition: drawable_object.h:57
tango_gl::DrawableObject::attrib_normals_
GLuint attrib_normals_
Definition: drawable_object.h:60
mesh.h
tango_gl::Mesh::SetBoundingBox
void SetBoundingBox()
Definition: mesh.cpp:60
tango_gl::Mesh::Mesh
Mesh()
Definition: mesh.cpp:21
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
tango_gl::DrawableObject::indices_
std::vector< GLushort > indices_
Definition: drawable_object.h:51
segment
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE FixedSegmentReturnType< N >::Type segment(Index start, Index n=N)
tango_gl::Mesh::bounding_box_
BoundingBox * bounding_box_
Definition: mesh.h:37
tango_gl::BoundingBox
Definition: bounding_box.h:26
tango_gl::DrawableObject::vertices_
std::vector< GLfloat > vertices_
Definition: drawable_object.h:52
c_str
const char * c_str(Args &&...args)
tango_gl::DrawableObject::SetShader
void SetShader()
Definition: drawable_object.cpp:22
tango_gl::DrawableObject::uniform_mvp_mat_
GLuint uniform_mvp_mat_
Definition: drawable_object.h:58
tango_gl::Mesh::SetLightDirection
void SetLightDirection(const glm::vec3 &light_direction)
Definition: mesh.cpp:71
tango_gl::Mesh::SetShader
void SetShader()
Definition: mesh.cpp:28
glUniformMatrix4fv
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
tango_gl
Definition: axis.cpp:20
tango_gl::DrawableObject::shader_program_
GLuint shader_program_
Definition: drawable_object.h:56
tango_gl::Transform::GetTransformationMatrix
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
tango_gl::Transform::GetRotation
glm::quat GetRotation() const
Definition: transform.cpp:47
tango_gl::Mesh::Render
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
Definition: mesh.cpp:86
tango_gl::Mesh::light_direction_
glm::vec3 light_direction_
Definition: mesh.h:40
tango_gl::Mesh::IsIntersecting
bool IsIntersecting(const Segment &segment)
Definition: mesh.cpp:75
tango_gl::Mesh::uniform_mv_mat_
GLuint uniform_mv_mat_
Definition: mesh.h:41


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:13