Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
cone.hpp
Go to the documentation of this file.
1 
23 #ifndef __CONE__
24 #define __CONE__
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 cone_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 cone_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 
83 class Cone : public Tucano::Mesh {
84 
85 private:
86 
89 
91  Eigen::Vector4f color;
92 
94  float cone_height;
95 
97  float cone_radius;
98 
99 public:
100 
105  {
107  create(0.1, 0.5);
108 
109  color << 0.0, 0.48, 1.0, 1.0;
110 
111  cone_shader.setShaderName("coneShader");
112  cone_shader.initializeFromStrings(cone_vertex_code, cone_fragment_code);
113 
114  }
115 
117  ~Cone()
118  {}
119 
120 
125  void setColor (const Eigen::Vector4f &c)
126  {
127  color = c;
128  }
129 
133  void render (const Tucano::Camera& camera, const Tucano::Camera& light)
134  {
135  Eigen::Vector4f viewport = camera.getViewport();
136  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
137 
138  cone_shader.bind();
139 
140  cone_shader.setUniform("modelMatrix", model_matrix);
141  cone_shader.setUniform("viewMatrix", camera.getViewMatrix());
142  cone_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
143  cone_shader.setUniform("lightViewMatrix", light.getViewMatrix());
144  cone_shader.setUniform("in_Color", color);
145 
146  this->setAttributeLocation(&cone_shader);
147 
148  glEnable(GL_DEPTH_TEST);
149  this->bindBuffers();
150  this->renderElements();
151  this->unbindBuffers();
152  glDisable(GL_DEPTH_TEST);
153 
154  cone_shader.unbind();
155 
156  #ifdef TUCANODEBUG
157  Misc::errorCheckFunc(__FILE__, __LINE__);
158  #endif
159 
160  }
161 
168  void create (float r, float h, int s = 32)
169  {
170  cone_radius = r;
171  cone_height = h;
172  createGeometry(s);
173  }
174 
178  float getHeight (void)
179  {
180  return cone_height;
181  }
182 
186  float getRadius (void)
187  {
188  return cone_radius;
189  }
190 
191 
192 private:
193 
194 
203  void createGeometry (int subdivisions)
204  {
205  reset();
206 
207  vector< Eigen::Vector4f > vert;
208  vector< Eigen::Vector3f > norm;
209  vector< GLuint > faces;
210 
211  float x, y, theta;
212  // create vertices for body
213  for (int i = 0; i < subdivisions; ++i)
214  {
215  theta = 2.0*M_PI*i/(float)subdivisions;
216  x = sin(theta)*cone_radius;
217  y = cos(theta)*cone_radius;
218  vert.push_back(Eigen::Vector4f(x, y, 0.0, 1.0));
219  norm.push_back(Eigen::Vector3f(x, y, 0.0));
220  }
221 
222  // apex vertex
223  vert.push_back(Eigen::Vector4f(0.0, 0.0, cone_height, 1.0));
224  norm.push_back(Eigen::Vector3f(0.0, 0.0, 1.0));
225 
226  // create a face with every two vertices and apex
227  for (int i = 0; i < subdivisions; ++i)
228  {
229  faces.push_back(i);
230  faces.push_back((i+1)%(subdivisions));
231  faces.push_back(vert.size()-1);
232  }
233 
234  // create cap
235  vert.push_back(Eigen::Vector4f(0.0, 0.0, 0.0, 1.0));
236  norm.push_back(Eigen::Vector3f(0.0, 0.0, -1.0));
237  int center_index = vert.size()-1;
238  int offset = vert.size();
239  for (int i = 0; i < subdivisions; ++i)
240  {
241  theta = 2.0*M_PI*i/(float)subdivisions;
242  x = sin(theta)*cone_radius;
243  y = cos(theta)*cone_radius;
244  vert.push_back(Eigen::Vector4f(x, y, 0.0, 1.0));
245  norm.push_back(Eigen::Vector3f(0.0, 0.0, -1.0));
246  }
247 
248  for (int i = 0; i < subdivisions; ++i)
249  {
250  faces.push_back(i+offset);
251  faces.push_back((i+1)%(subdivisions) + offset);
252  faces.push_back(center_index);
253  }
254 
255 
256  loadVertices(vert);
257  loadNormals(norm);
258  loadIndices(faces);
259 
261 
262  }
263 
264 };
265 }
266 }
267 #endif
A simple cone shape.
Definition: cone.hpp:83
void create(float r, float h, int s=32)
Create cone with given parameters.
Definition: cone.hpp:168
Eigen::Vector4f color
Cone color.
Definition: cone.hpp:91
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 createGeometry(int subdivisions)
Define cone geometry.
Definition: cone.hpp:203
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Tucano::Shader cone_shader
Shader to render cone.
Definition: cone.hpp:88
Definition: bufferobject.hpp:34
void setShaderName(string name)
Sets the shader name, very useful for debugging.
Definition: shader.hpp:329
float getRadius(void)
Returns cone cone_radius.
Definition: cone.hpp:186
float getHeight(void)
Returns cone cone_height.
Definition: cone.hpp:178
A Shader object represents one GLSL program.
Definition: shader.hpp:45
float cone_height
Cone cone_height.
Definition: cone.hpp:94
const string cone_vertex_code
Default vertex shader for rendering cone.
Definition: cone.hpp:58
float cone_radius
Cone cone_radius.
Definition: cone.hpp:97
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
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
void setColor(const Eigen::Vector4f &c)
Sets the cone color.
Definition: cone.hpp:125
void render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render cone.
Definition: cone.hpp:133
const string cone_fragment_code
Default fragment shader for rendering cone.
Definition: cone.hpp:37
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 reset(void)
Definition: mesh.hpp:294
Cone()
Default Constructor.
Definition: cone.hpp:104
~Cone()
Default destructor.
Definition: cone.hpp:117
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
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
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