Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
box.hpp
Go to the documentation of this file.
1 
23 #ifndef __BOX__
24 #define __BOX__
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 box_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 box_vertex_code = "\n"
60  "#version 430\n"
61  "in vec4 in_Position;\n"
62  "in vec4 in_Normal;\n"
63  "out vec4 color;\n"
64  "out vec3 normal;\n"
65  "out vec4 vert;\n"
66  "uniform mat4 modelMatrix;\n"
67  "uniform mat4 viewMatrix;\n"
68  "uniform mat4 projectionMatrix;\n"
69  "uniform vec4 in_Color;\n"
70  "void main(void)\n"
71  "{\n"
72  " mat4 modelViewMatrix = viewMatrix * modelMatrix;\n"
73  " mat4 normalMatrix = transpose(inverse(modelViewMatrix));\n"
74  " normal = normalize(vec3(normalMatrix * vec4(in_Normal.xyz,0.0)).xyz);\n"
75  " vert = modelViewMatrix * in_Position;\n"
76  " gl_Position = projectionMatrix * modelViewMatrix * in_Position;\n"
77  " color = in_Color;\n"
78  "}\n";
79 
80 
84 class Box : public Tucano::Mesh {
85 
86 private:
87 
88  // Shader to render sbox
90 
92  float width;
93 
95  float height;
96 
98  float depth;
99 
100 public:
101 
105  Box(float w = 1.0, float h = 1.0, float d = 1.0) : width(w), height(h), depth(d)
106  {
108  createGeometry();
109 
110  default_color << 1.0, 0.48, 0.16, 1.0;
111 
112  box_shader.setShaderName("boxShader");
113  box_shader.initializeFromStrings(box_vertex_code, box_fragment_code);
114 
115  #ifdef TUCANODEBUG
116  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__, "box constructor");
117  #endif
118  }
119 
123  void render (const Tucano::Camera& camera, const Tucano::Camera& light)
124  {
125  Eigen::Vector4f viewport = camera.getViewport();
126  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
127 
128  box_shader.bind();
129 
130  box_shader.setUniform("modelMatrix", model_matrix);
131  box_shader.setUniform("viewMatrix", camera.getViewMatrix());
132  box_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
133  box_shader.setUniform("lightViewMatrix", light.getViewMatrix());
134  box_shader.setUniform("in_Color", default_color);
135 
136  this->setAttributeLocation(&box_shader);
137 
138  this->bindBuffers();
139  this->renderElements();
140  this->unbindBuffers();
141 
142  box_shader.unbind();
143 
144  #ifdef TUCANODEBUG
145  Misc::errorCheckFunc(__FILE__, __LINE__);
146  #endif
147  }
148 
149 private:
150 
151 
156  void createGeometry (void)
157  {
158  vector< Eigen::Vector4f > vert;
159  vector< Eigen::Vector3f > normals;
160  vector< GLuint > faces;
161 
162  // front
163  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, -depth*0.5, 1.0) );
164  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, -depth*0.5, 1.0) );
165  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, -depth*0.5, 1.0) );
166  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, -depth*0.5, 1.0) );
167  normals.push_back ( Eigen::Vector3f (0.0, 0.0, -1.0) );
168  normals.push_back ( Eigen::Vector3f (0.0, 0.0, -1.0) );
169  normals.push_back ( Eigen::Vector3f (0.0, 0.0, -1.0) );
170  normals.push_back ( Eigen::Vector3f (0.0, 0.0, -1.0) );
171 
172  // back
173  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, depth*0.5, 1.0) );
174  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, depth*0.5, 1.0) );
175  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, depth*0.5, 1.0) );
176  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, depth*0.5, 1.0) );
177  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
178  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
179  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
180  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
181 
182  // bottom
183  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, depth*0.5, 1.0) );
184  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, depth*0.5, 1.0) );
185  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, -depth*0.5, 1.0) );
186  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, -depth*0.5, 1.0) );
187  normals.push_back ( Eigen::Vector3f (0.0, -1.0, 0.0) );
188  normals.push_back ( Eigen::Vector3f (0.0, -1.0, 0.0) );
189  normals.push_back ( Eigen::Vector3f (0.0, -1.0, 0.0) );
190  normals.push_back ( Eigen::Vector3f (0.0, -1.0, 0.0) );
191 
192  // top
193  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, -depth*0.5, 1.0) );
194  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, -depth*0.5, 1.0) );
195  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, depth*0.5, 1.0) );
196  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, depth*0.5, 1.0) );
197  normals.push_back ( Eigen::Vector3f (0.0, 1.0, 0.0) );
198  normals.push_back ( Eigen::Vector3f (0.0, 1.0, 0.0) );
199  normals.push_back ( Eigen::Vector3f (0.0, 1.0, 0.0) );
200  normals.push_back ( Eigen::Vector3f (0.0, 1.0, 0.0) );
201 
202  // right
203  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, -depth*0.5, 1.0) );
204  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, depth*0.5, 1.0) );
205  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, depth*0.5, 1.0) );
206  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, -depth*0.5, 1.0) );
207  normals.push_back ( Eigen::Vector3f (1.0, 0.0, 0.0) );
208  normals.push_back ( Eigen::Vector3f (1.0, 0.0, 0.0) );
209  normals.push_back ( Eigen::Vector3f (1.0, 0.0, 0.0) );
210  normals.push_back ( Eigen::Vector3f (1.0, 0.0, 0.0) );
211 
212  // left
213  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, -depth*0.5, 1.0) );
214  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, -depth*0.5, 1.0) );
215  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, depth*0.5, 1.0) );
216  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, depth*0.5, 1.0) );
217  normals.push_back ( Eigen::Vector3f (-1.0, 0.0, 0.0) );
218  normals.push_back ( Eigen::Vector3f (-1.0, 0.0, 0.0) );
219  normals.push_back ( Eigen::Vector3f (-1.0, 0.0, 0.0) );
220  normals.push_back ( Eigen::Vector3f (-1.0, 0.0, 0.0) );
221 
222  int a[36] = { 0, 1, 2, 2, 3, 0,
223  4, 5, 6, 6, 7, 4,
224  8, 9, 10, 10, 11, 8,
225  12, 13, 14, 14, 15, 12,
226  16, 17, 18, 18, 19, 16,
227  20, 21, 22, 22, 23, 20};
228  faces.insert(faces.end(), a, a+36);
229 
230  loadVertices(vert);
231  loadNormals(normals);
232  loadIndices(faces);
233 
235 
236  }
237 
238 };
239 }
240 }
241 #endif
void loadNormals(vector< Eigen::Vector3f > &norm)
Load normals (x,y,z) as a vertex attribute.
Definition: mesh.hpp:384
void createGeometry(void)
Define box geometry.
Definition: box.hpp:156
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
Tucano::Shader box_shader
Definition: box.hpp:89
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
float width
Box width.
Definition: box.hpp:92
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
Box(float w=1.0, float h=1.0, float d=1.0)
Default Constructor.
Definition: box.hpp:105
A simple box with bounded limits.
Definition: box.hpp:84
float depth
Box depth.
Definition: box.hpp:98
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
const string box_fragment_code
Default fragment shader for rendering box.
Definition: box.hpp:37
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
float height
Box height.
Definition: box.hpp:95
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 render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render box.
Definition: box.hpp:123
const string box_vertex_code
Default vertex shader for rendering box.
Definition: box.hpp:59
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