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  }
82  return bounding_box_->IsIntersecting(segment, GetRotation(),
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
bool is_lighting_on_
Definition: mesh.h:38
highp_vec3 vec3
Definition: type_vec.hpp:392
f
glm::quat GetRotation() const
Definition: transform.cpp:47
bool is_bounding_box_on_
Definition: mesh.h:39
std::string GetBasicFragmentShader()
Definition: shaders.cpp:33
std::vector< GLfloat > vertices_
glm::mat4 GetTransformationMatrix() const
Definition: transform.cpp:67
GLuint CreateProgram(const char *vertex_source, const char *fragment_source)
Definition: util.cpp:75
#define LOGE(...)
GLM_FUNC_DECL genType::value_type const * value_ptr(genType const &vec)
GLM_FUNC_DECL genType normalize(genType const &x)
GLuint uniform_light_vec_
Definition: mesh.h:42
#define GL_FALSE
Definition: dummy.cpp:79
bool IsIntersecting(const Segment &segment, const glm::quat &rotation, const glm::mat4 &transformation)
void SetLightDirection(const glm::vec3 &light_direction)
Definition: mesh.cpp:71
void glUniformMatrix4fv(GLuint, int, int, float *)
Definition: dummy.cpp:80
void SetBoundingBox()
Definition: mesh.cpp:60
std::vector< GLfloat > normals_
mat3x3 mat3
Definition: type_mat.hpp:436
std::vector< GLushort > indices_
BoundingBox * bounding_box_
Definition: mesh.h:37
void Render(const glm::mat4 &projection_mat, const glm::mat4 &view_mat) const
Definition: mesh.cpp:86
glm::vec3 light_direction_
Definition: mesh.h:40
bool IsIntersecting(const Segment &segment)
Definition: mesh.cpp:75
GLuint uniform_mv_mat_
Definition: mesh.h:41
std::string GetShadedVertexShader()
Definition: shaders.cpp:78
void SetShader()
Definition: mesh.cpp:28


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29