Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
arrow.hpp
Go to the documentation of this file.
1 
23 #ifndef __ARROW__
24 #define __ARROW__
25 
26 #include <tucano/mesh.hpp>
27 #include <Eigen/Dense>
28 #include <cmath>
29 
30 namespace Tucano
31 {
32 
33 namespace Shapes
34 {
35 
37 const string arrow_fragment_code = "\n"
38  "#version 430\n"
39  "in vec4 color;\n"
40  "in vec3 normal;\n"
41  "in vec4 vert;\n"
42  "out vec4 out_Color;\n"
43  "uniform mat4 lightViewMatrix;\n"
44  "void main(void)\n"
45  "{\n"
46  " vec3 lightDirection = (lightViewMatrix * vec4(0.0, 0.0, 1.0, 0.0)).xyz;\n"
47  " lightDirection = normalize(lightDirection);\n"
48  " vec3 lightReflection = reflect(-lightDirection, normal);\n"
49  " vec3 eyeDirection = -normalize(vert.xyz);\n"
50  " float shininess = 100.0;\n"
51  " vec4 ambientLight = color * 0.4;\n"
52  " vec4 diffuseLight = color * 0.6 * max(dot(lightDirection, normal),0.0);\n"
53  " vec4 specularLight = vec4(1.0) * max(pow(dot(lightReflection, eyeDirection), shininess),0.0);\n"
54  " out_Color = vec4(ambientLight.xyz + diffuseLight.xyz + specularLight.xyz, color.w);\n"
55  "}\n";
56 
58 const string arrow_vertex_code = "\n"
59  "#version 430\n"
60  "in vec4 in_Position;\n"
61  "in vec4 in_Normal;\n"
62  "out vec4 color;\n"
63  "out vec3 normal;\n"
64  "out vec4 vert;\n"
65  "uniform mat4 modelMatrix;\n"
66  "uniform mat4 viewMatrix;\n"
67  "uniform mat4 projectionMatrix;\n"
68  "uniform vec4 in_Color;\n"
69  "void main(void)\n"
70  "{\n"
71  " mat4 modelViewMatrix = viewMatrix * modelMatrix;\n"
72  " mat4 normalMatrix = transpose(inverse(modelViewMatrix));\n"
73  " normal = normalize(vec3(normalMatrix * vec4(in_Normal.xyz,0.0)).xyz);\n"
74  " vert = modelViewMatrix * in_Position;\n"
75  " gl_Position = projectionMatrix * modelViewMatrix * in_Position;\n"
76  " color = in_Color;\n"
77  "}\n";
78 
79 
80 
85 class Arrow : public Tucano::Mesh {
86 
87 private:
88 
91 
93  Eigen::Vector4f color;
94 
96  float body_height;
97 
99  float head_height;
100 
102  float body_radius;
103 
105  float head_radius;
106 
107 public:
108 
112  Arrow(float rcyl = 0.05, float hcyl = 1.0, float rcon = 0.1, float hcon = 0.15, int subs = 32 )
113  {
115 
116  body_height = hcyl;
117  head_height = hcon;
118  body_radius = rcyl;
119  head_radius = rcon;
120  createGeometry(subs);
121 
122  setColor(Eigen::Vector4f(0.0, 0.7, 0.7, 1.0));
123 
124  arrow_shader.setShaderName("arrowShader");
125  arrow_shader.initializeFromStrings(arrow_vertex_code, arrow_fragment_code);
126 
127  cout << "created arrow" << endl;
128 
129  }
130 
134  void setColor (const Eigen::Vector4f c)
135  {
136  color = c;
137  }
138 
142  void render (const Tucano::Camera &camera, const Tucano::Camera &light)
143  {
144  Eigen::Vector4f viewport = camera.getViewport();
145  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
146 
147  arrow_shader.bind();
148 
149  arrow_shader.setUniform("modelMatrix", model_matrix);
150  arrow_shader.setUniform("viewMatrix", camera.getViewMatrix());
151  arrow_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
152  arrow_shader.setUniform("lightViewMatrix", light.getViewMatrix());
153  arrow_shader.setUniform("in_Color", color);
154 
155  this->setAttributeLocation(&arrow_shader);
156 
157  glEnable(GL_DEPTH_TEST);
158  this->bindBuffers();
159  this->renderElements();
160  this->unbindBuffers();
161 
162  arrow_shader.unbind();
163 
164  #ifdef TUCANODEBUG
165  Misc::errorCheckFunc(__FILE__, __LINE__);
166  #endif
167  }
168 
169 private:
170 
176  void createGeometry (int subdivisions)
177  {
178  vector< Eigen::Vector4f > vert;
179  vector< Eigen::Vector3f > norm;
180  vector< GLuint > faces;
181 
182  float x, y, theta;
183  // create vertices for top and bottom caps
184  for (int i = 0; i < subdivisions; ++i)
185  {
186  theta = 2.0*M_PI*i/(float)subdivisions;
187  x = sin(theta)*body_radius;
188  y = cos(theta)*body_radius;
189  vert.push_back(Eigen::Vector4f(x, y, body_height, 1.0) );
190  vert.push_back(Eigen::Vector4f(x, y, 0.0, 1.0));
191  norm.push_back(Eigen::Vector3f(x, y, 0.0));
192  norm.push_back(Eigen::Vector3f(x, y, 0.0));
193  }
194 
195  // create a face with every three vertices for arrow body
196  for (int i = 0; i < subdivisions*2; ++i)
197  {
198  faces.push_back(i);
199  faces.push_back((i+1)%(subdivisions*2));
200  faces.push_back((i+2)%(subdivisions*2));
201  }
202 
203  // create bottom cap
204  vert.push_back(Eigen::Vector4f(0.0, 0.0, 0.0, 1.0));
205  norm.push_back(Eigen::Vector3f(0.0, 0.0, -1.0));
206  int center_index = vert.size()-1;
207  int offset = vert.size();
208  for (int i = 0; i < subdivisions; ++i)
209  {
210  theta = 2.0*M_PI*i/(float)subdivisions;
211  x = sin(theta)*body_radius;
212  y = cos(theta)*body_radius;
213  vert.push_back(Eigen::Vector4f(x, y, 0.0, 1.0));
214  norm.push_back(Eigen::Vector3f(0.0, 0.0, -1.0));
215  }
216 
217  for (int i = 0; i < subdivisions; ++i)
218  {
219  faces.push_back(i+offset);
220  faces.push_back((i+1)%(subdivisions) + offset);
221  faces.push_back(center_index);
222  }
223 
224  // create vertices for arrow head
225  offset = vert.size();
226  for (int i = 0; i < subdivisions; ++i)
227  {
228  theta = 2.0*M_PI*i/(float)subdivisions;
229  x = sin(theta)*head_radius;
230  y = cos(theta)*head_radius;
231  vert.push_back(Eigen::Vector4f(x, y, body_height, 1.0));
232  norm.push_back(Eigen::Vector3f(x, y, 0.0));
233  }
234 
235  // apex vertex
236  vert.push_back(Eigen::Vector4f(0.0, 0.0, body_height+head_height, 1.0));
237  norm.push_back(Eigen::Vector3f(0.0, 0.0, 1.0));
238 
239 
240  // create a face with every two vertices and apex
241  for (int i = 0; i < subdivisions; ++i)
242  {
243  faces.push_back(i+offset);
244  faces.push_back((i+1)%(subdivisions) + offset);
245  faces.push_back(vert.size()-1);
246  }
247 
248  // create head cap
249  vert.push_back(Eigen::Vector4f(0.0, 0.0, body_height, 1.0));
250  norm.push_back(Eigen::Vector3f(0.0, 0.0, -1.0));
251  center_index = vert.size()-1;
252  offset = vert.size();
253  for (int i = 0; i < subdivisions; ++i)
254  {
255  theta = 2.0*M_PI*i/(float)subdivisions;
256  x = sin(theta)*head_radius;
257  y = cos(theta)*head_radius;
258  vert.push_back(Eigen::Vector4f(x, y, body_height, 1.0));
259  norm.push_back(Eigen::Vector3f(0.0, 0.0, -1.0));
260  }
261 
262  for (int i = 0; i < subdivisions; ++i)
263  {
264  faces.push_back(i+offset);
265  faces.push_back((i+1)%(subdivisions) + offset);
266  faces.push_back(center_index);
267  }
268 
269  loadVertices(vert);
270  loadNormals(norm);
271  loadIndices(faces);
272 
274 
275  }
276 
277 };
278 }
279 }
280 #endif
void setColor(const Eigen::Vector4f c)
Sets the arrow color.
Definition: arrow.hpp:134
void createGeometry(int subdivisions)
Define arrow geometry.
Definition: arrow.hpp:176
void loadNormals(vector< Eigen::Vector3f > &norm)
Load normals (x,y,z) as a vertex attribute.
Definition: mesh.hpp:384
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
const string arrow_fragment_code
Default fragment shader for rendering arrow.
Definition: arrow.hpp:37
float body_height
Height of body.
Definition: arrow.hpp:96
Arrow(float rcyl=0.05, float hcyl=1.0, float rcon=0.1, float hcon=0.15, int subs=32)
Default Constructor.
Definition: arrow.hpp:112
void setShaderName(string name)
Sets the shader name, very useful for debugging.
Definition: shader.hpp:329
float head_radius
Head radius.
Definition: arrow.hpp:105
A Shader object represents one GLSL program.
Definition: shader.hpp:45
Eigen::Vector4f color
Arrow color.
Definition: arrow.hpp:93
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
void initializeFromStrings(string in_vertex_code, string in_fragment_code, string in_geometry_code="", string in_tessellation_evaluation_code="", string in_tessellation_control_code="")
Initializes shader directly from string, no files.
Definition: shader.hpp:589
Tucano::Shader arrow_shader
Shader to render arrow.
Definition: arrow.hpp:90
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
float head_height
Height of head.
Definition: arrow.hpp:99
const string arrow_vertex_code
Default vertex shader for rendering arrow.
Definition: arrow.hpp:58
void setDefaultAttribLocations(void)
Sets default attribute locations. vertex coords -> location 0 normals -> location 1 colors -> locatio...
Definition: mesh.hpp:474
void resetModelMatrix(void)
Resets the model matrix.
Definition: model.hpp:159
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
void render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render arrow.
Definition: arrow.hpp:142
Eigen::Affine3f model_matrix
Model matrix, holds information about the models location and orientation.
Definition: model.hpp:21
virtual void unbindBuffers(void)
Unbinds all buffers.
Definition: mesh.hpp:702
void loadVertices(vector< Eigen::Vector4f > &vert)
Load vertices (x,y,z,w) and creates appropriate vertex attribute. The default attribute name is "in_P...
Definition: mesh.hpp:312
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
virtual void renderElements(void)
Call the draw method for rendering triangles. This method requires that a index buffer has been creat...
Definition: mesh.hpp:726
float body_radius
Body radius.
Definition: arrow.hpp:102
virtual void bindBuffers(void)
Binds all buffers.
Definition: mesh.hpp:677
A common Mesh, usually containing triagles or points.
Definition: mesh.hpp:194
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
void setAttributeLocation(Shader *shader)
Automatically sets the attribute locations for a given Shader.
Definition: mesh.hpp:541
Eigen::Vector4f getViewport(void) const
Returns the viewport coordinates.
Definition: camera.hpp:246
A rounded arrow shape defined by a arrow and a cone.
Definition: arrow.hpp:85
void setUniform(GLint location, GLint a, GLint b, GLint c, GLint d)
Sets an uniform integer 4D vector (ivec4) given a location and the vector values. ...
Definition: shader.hpp:1258