Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
plane.hpp
Go to the documentation of this file.
1 
23 #ifndef __PLANE__
24 #define __PLANE__
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 plane_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 plane_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 Plane : public Tucano::Mesh {
85 
86 private:
87 
88  // Shader to render splane
90 
92  float width;
93 
95  float height;
96 
97 public:
98 
102  Plane(float w = 1.0, float h = 1.0)
103  {
104  width = w;
105  height = h;
106 
108  createGeometry();
109 
110  default_color << 1.0, 0.48, 0.16, 1.0;
111 
112  plane_shader.setShaderName("planeShader");
113  plane_shader.initializeFromStrings(plane_vertex_code, plane_fragment_code);
114 
115  #ifdef TUCANODEBUG
116  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__, "plane 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  plane_shader.bind();
129 
130  plane_shader.setUniform("modelMatrix", model_matrix);
131  plane_shader.setUniform("viewMatrix", camera.getViewMatrix());
132  plane_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
133  plane_shader.setUniform("lightViewMatrix", light.getViewMatrix());
134  plane_shader.setUniform("in_Color", default_color);
135 
136  this->setAttributeLocation(&plane_shader);
137 
138  this->bindBuffers();
139  this->renderElements();
140  this->unbindBuffers();
141 
142  plane_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<Eigen::Vector2f> texcoord;
161  vector< GLuint > faces;
162 
163  vert.push_back ( Eigen::Vector4f( -width*0.5, -height*0.5, 0.0, 1.0) );
164  vert.push_back ( Eigen::Vector4f( width*0.5, -height*0.5, 0.0, 1.0) );
165  vert.push_back ( Eigen::Vector4f( width*0.5, height*0.5, 0.0, 1.0) );
166  vert.push_back ( Eigen::Vector4f( -width*0.5, height*0.5, 0.0, 1.0) );
167 
168  texcoord.push_back ( Eigen::Vector2f(0.0, 0.0) );
169  texcoord.push_back ( Eigen::Vector2f(1.0, 0.0) );
170  texcoord.push_back ( Eigen::Vector2f(1.0, 1.0) );
171  texcoord.push_back ( Eigen::Vector2f(0.0, 1.0) );
172 
173  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
174  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
175  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
176  normals.push_back ( Eigen::Vector3f (0.0, 0.0, 1.0) );
177 
178  int a[6] = { 0, 1, 2, 2, 3, 0};
179  faces.insert(faces.end(), a, a+6);
180 
181  loadVertices(vert);
182  loadNormals(normals);
183  loadTexCoords(texcoord);
184  loadIndices(faces);
185 
187 
188  }
189 
190 };
191 }
192 }
193 #endif
void loadNormals(vector< Eigen::Vector3f > &norm)
Load normals (x,y,z) as a vertex attribute.
Definition: mesh.hpp:384
A simple plane with bounded limits.
Definition: plane.hpp:84
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
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
float width
Plane width.
Definition: plane.hpp:92
Definition: bufferobject.hpp:34
void setShaderName(string name)
Sets the shader name, very useful for debugging.
Definition: shader.hpp:329
float height
Plane height.
Definition: plane.hpp:95
A Shader object represents one GLSL program.
Definition: shader.hpp:45
Plane(float w=1.0, float h=1.0)
Default Constructor.
Definition: plane.hpp:102
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 createGeometry(void)
Define plane geometry.
Definition: plane.hpp:156
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
const string plane_fragment_code
Default fragment shader for rendering plane.
Definition: plane.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
Tucano::Shader plane_shader
Definition: plane.hpp:89
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
void loadTexCoords(vector< Eigen::Vector2f > &tex, bool normalize=false)
Load tex coords (u,v) as a vertex attribute. Optionally normalizes coords in range [0...
Definition: mesh.hpp:397
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 plane.
Definition: plane.hpp:123
const string plane_vertex_code
Default vertex shader for rendering plane.
Definition: plane.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