Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
sphere.hpp
Go to the documentation of this file.
1 
23 #ifndef __SPHERE__
24 #define __SPHERE__
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 sphere_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 eyeDirection = -normalize(vert.xyz);\n"
47  " if (dot(normal, eyeDirection) < 0.0) discard;\n"
48  " vec3 lightDirection = (lightViewMatrix * vec4(0.0, 0.0, 1.0, 0.0)).xyz;\n"
49  " lightDirection = normalize(lightDirection);\n"
50  " vec3 lightReflection = reflect(-lightDirection, normal);\n"
51  " float shininess = 100.0;\n"
52  " vec4 ambientLight = color * 0.4;\n"
53  " vec4 diffuseLight = color * 0.6 * max(dot(lightDirection, normal),0.0);\n"
54  " vec4 specularLight = vec4(1.0) * max(pow(dot(lightReflection, eyeDirection), shininess),0.0);\n"
55  " out_Color = vec4(ambientLight.xyz + diffuseLight.xyz + specularLight.xyz, color.w);\n"
56  "}\n";
57 
59 const string sphere_vertex_code = "\n"
60  "#version 430\n"
61  "layout(location=0) in vec4 in_Position;\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_Position.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 Sphere : public Tucano::Mesh {
84 
85 private:
86 
87  // Shader to render sphere
89 
90 public:
91 
97  Sphere(int subdivisions = 4)
98  {
100  createGeometry(subdivisions);
101 
102  default_color << 1.0, 0.48, 0.16, 1.0;
103 
104  sphere_shader.setShaderName("sphereShader");
105  sphere_shader.initializeFromStrings(sphere_vertex_code, sphere_fragment_code);
106 
107  #ifdef TUCANODEBUG
108  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__, "sphere constructor");
109  #endif
110  }
111 
115  void render (const Tucano::Camera& camera, const Tucano::Camera& light)
116  {
117  Eigen::Vector4f viewport = camera.getViewport();
118  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
119 
120  sphere_shader.bind();
121 
122  sphere_shader.setUniform("modelMatrix", model_matrix);
123  sphere_shader.setUniform("viewMatrix", camera.getViewMatrix());
124  sphere_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
125  sphere_shader.setUniform("lightViewMatrix", light.getViewMatrix());
126  sphere_shader.setUniform("in_Color", default_color);
127 
128  this->setAttributeLocation(&sphere_shader);
129 
130  this->bindBuffers();
131  this->renderElements();
132  this->unbindBuffers();
133 
134  sphere_shader.unbind();
135 
136  #ifdef TUCANODEBUG
137  Misc::errorCheckFunc(__FILE__, __LINE__);
138  #endif
139  }
140 
141 private:
142 
143 
151  void createGeometry (int subdivisions)
152  {
153  vector< Eigen::Vector4f > vert;
154  vector< GLuint > faces;
155 
156  vert.push_back ( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
157  vert.push_back ( Eigen::Vector4f(-1.0, 0.0, 0.0, 1.0) );
158  vert.push_back ( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
159  vert.push_back ( Eigen::Vector4f( 0.0,-1.0, 0.0, 1.0) );
160  vert.push_back ( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
161  vert.push_back ( Eigen::Vector4f( 0.0, 0.0,-1.0, 1.0) );
162 
163  int a[24] = { 0, 4, 2, 2, 4, 1, 1, 4, 3, 3, 4, 0, 0, 2, 5, 2, 1, 5, 1, 3, 5, 3, 0, 5 };
164  faces.insert(faces.end(), a, a+24);
165 
166  // now subdivide, divide each triangle into 4
167  for (int s = 0; s < subdivisions; ++s)
168  {
169  vector< GLuint > sub_faces;
170  for (int i = 0; i < (int)faces.size(); i=i+3)
171  {
172  Eigen::Vector4f p0 = vert[faces[i+0]];
173  Eigen::Vector4f p1 = vert[faces[i+1]];
174  Eigen::Vector4f p2 = vert[faces[i+2]];
175 
176  Eigen::Vector4f p3 = (p0 + p1)*0.5;
177  Eigen::Vector4f p4 = (p0 + p2)*0.5;
178  Eigen::Vector4f p5 = (p1 + p2)*0.5;
179  p3.head(3).normalize();
180  p4.head(3).normalize();
181  p5.head(3).normalize();
182 
183  vert.push_back(p3);
184  vert.push_back(p4);
185  vert.push_back(p5);
186 
187  int ind = vert.size()-3;
188  // new faces are: (p0, p4, p3), (p4, p1, p5), (p3, p4, p5), (p3, p5, p2)
189  int b[12] = {(int)faces[i+0], ind, ind+1, ind+1, ind+2, (int)faces[i+2], ind, ind+2, ind+1, ind, (int)faces[i+1], ind+2};
190  sub_faces.insert(sub_faces.end(), b, b+12);
191  }
192  faces.clear();
193  faces = sub_faces;
194  }
195 
196  loadVertices(vert);
197  loadIndices(faces);
198 
200 
201  }
202 
203 };
204 }
205 }
206 #endif
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
A simple sphere shape.
Definition: sphere.hpp:83
Eigen::Vector4f default_color
Default color.
Definition: model.hpp:36
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
void setShaderName(string name)
Sets the shader name, very useful for debugging.
Definition: shader.hpp:329
const string sphere_fragment_code
Default fragment shader for rendering sphere.
Definition: sphere.hpp:37
A Shader object represents one GLSL program.
Definition: shader.hpp:45
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 createGeometry(int subdivisions)
Creates geometry for a unit-radius sphere.
Definition: sphere.hpp:151
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
Tucano::Shader sphere_shader
Definition: sphere.hpp:88
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
const string sphere_vertex_code
Default vertex shader for rendering sphere.
Definition: sphere.hpp:59
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
void render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render sphere.
Definition: sphere.hpp:115
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
Sphere(int subdivisions=4)
Default constructor.
Definition: sphere.hpp:97
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