Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
quad.hpp
Go to the documentation of this file.
1 
23 #ifndef __QUAD__
24 #define __QUAD__
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 quad_fragment_code = "\n"
38  "#version 430\n"
39  "in vec4 color;\n"
40  "out vec4 out_Color;\n"
41  "void main(void)\n"
42  "{\n"
43  " out_Color = color;\n"
44  "}\n";
45 
47 const string quad_vertex_code = "\n"
48  "#version 430\n"
49  "in vec4 in_Position;\n"
50  "out vec4 color;\n"
51  "uniform mat4 modelMatrix;\n"
52  "uniform mat4 viewMatrix;\n"
53  "uniform mat4 projectionMatrix;\n"
54  "uniform vec4 in_Color;\n"
55  "void main(void)\n"
56  "{\n"
57  " mat4 modelViewMatrix = viewMatrix * modelMatrix;\n"
58  " gl_Position = projectionMatrix * modelViewMatrix * in_Position;\n"
59  " color = in_Color;\n"
60  "}\n";
61 
62 
66 class Quad : public Tucano::Mesh {
67 
68 private:
69 
72 
74  Eigen::Vector4f color;
75 
76 
77 public:
78 
82  Quad(void)
83  {
86 
87  color << 1.0, 0.48, 0.16, 1.0;
88 
89  quad_shader.setShaderName("quadShader");
90  quad_shader.initializeFromStrings(quad_vertex_code, quad_fragment_code);
91 
92  }
93 
95  ~Quad()
96  {}
97 
102  void setColor (const Eigen::Vector4f &c)
103  {
104  color = c;
105  }
106 
110  void render (const Tucano::Camera& camera, const Tucano::Camera& light)
111  {
112  Eigen::Vector4f viewport = camera.getViewport();
113  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
114 
115  quad_shader.bind();
116 
117  quad_shader.setUniform("modelMatrix", model_matrix);
118  quad_shader.setUniform("viewMatrix", camera.getViewMatrix());
119  quad_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
120  quad_shader.setUniform("lightViewMatrix", light.getViewMatrix());
121  quad_shader.setUniform("in_Color", color);
122 
123  vector <string> attribs;
124  quad_shader.getActiveAttributes(attribs);
125 
126  this->setAttributeLocation(&quad_shader);
127 
128  glEnable(GL_DEPTH_TEST);
129  this->bindBuffers();
130  this->renderElements();
131  this->unbindBuffers();
132  glDisable(GL_DEPTH_TEST);
133 
134  quad_shader.unbind();
135 
136  #ifdef TUCANODEBUG
137  Misc::errorCheckFunc(__FILE__, __LINE__);
138  #endif
139 
140  }
141 
142 private:
143 
147  void createGeometry (void)
148  {
149  vector<Eigen::Vector4f> vert;
150  vector<Eigen::Vector2f> texCoord;
151  vector<GLuint> elementsVertices;
152 
153  vert.push_back ( Eigen::Vector4f( -0.5, -0.5, 0.0, 1.0) );
154  vert.push_back ( Eigen::Vector4f( 0.5, -0.5, 0.0, 1.0) );
155  vert.push_back ( Eigen::Vector4f( 0.5, 0.5, 0.0, 1.0) );
156  vert.push_back ( Eigen::Vector4f( -0.5, 0.5, 0.0, 1.0) );
157 
158  elementsVertices.push_back(0);
159  elementsVertices.push_back(1);
160  elementsVertices.push_back(2);
161  elementsVertices.push_back(2);
162  elementsVertices.push_back(3);
163  elementsVertices.push_back(0);
164 
165  texCoord.push_back ( Eigen::Vector2f(0.0, 0.0) );
166  texCoord.push_back ( Eigen::Vector2f(1.0, 0.0) );
167  texCoord.push_back ( Eigen::Vector2f(1.0, 1.0) );
168  texCoord.push_back ( Eigen::Vector2f(0.0, 1.0) );
169 
170  loadVertices(vert);
171  loadTexCoords(texCoord);
172  loadIndices(elementsVertices);
173 
175  }
176 
177 };
178 }
179 }
180 #endif
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
A simple unitary quad.
Definition: quad.hpp:66
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
A Shader object represents one GLSL program.
Definition: shader.hpp:45
~Quad()
Default destructor.
Definition: quad.hpp:95
void render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render quad.
Definition: quad.hpp:110
Quad(void)
Default Constructor.
Definition: quad.hpp:82
void getActiveAttributes(vector< string > &attribs)
Detaches and deletes the shaders and the shader program.
Definition: shader.hpp:1205
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
Eigen::Vector4f color
Sphere color.
Definition: quad.hpp:74
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
const string quad_vertex_code
Default vertex shader for rendering quad.
Definition: quad.hpp:47
const string quad_fragment_code
Default fragment shader for rendering quad.
Definition: quad.hpp:37
void createGeometry(void)
Creates the quad geometry with associated tex coords.
Definition: quad.hpp:147
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 quad_shader
Shader to render sphere.
Definition: quad.hpp:71
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 setColor(const Eigen::Vector4f &c)
Sets the quad color.
Definition: quad.hpp:102
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 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