Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
camerarep.hpp
Go to the documentation of this file.
1 
23 #ifndef __CAMERAREP__
24 #define __CAMERAREP__
25 
26 #include <tucano/mesh.hpp>
28 #include <Eigen/Dense>
29 #include <cmath>
30 
31 namespace Tucano
32 {
33 
34 namespace Shapes
35 {
36 
37 
39 const string camerarep_fragment_code = "\n"
40  "#version 430\n"
41  "in vec4 ex_Color;\n"
42  "out vec4 out_Color;\n"
43  "in float depth;\n"
44  "void main(void)\n"
45  "{\n"
46  " out_Color = ex_Color;\n"
47  " gl_FragDepth = depth;\n"
48  "}\n";
49 
51 const string camerarep_vertex_code = "\n"
52  "#version 430\n"
53  "in vec4 in_Position;\n"
54  "out vec4 ex_Color;\n"
55  "out float depth;\n"
56  "uniform mat4 modelMatrix;\n"
57  "uniform mat4 viewMatrix;\n"
58  "uniform mat4 projectionMatrix;\n"
59  "uniform vec4 in_Color;\n"
60  "uniform float nearPlane;\n"
61  "uniform float farPlane;\n"
62  "void main(void)\n"
63  "{\n"
64  " gl_Position = (viewMatrix * modelMatrix) * in_Position;\n"
65  " depth = (farPlane+nearPlane)/(farPlane-nearPlane) + ( (2*nearPlane*farPlane)/(farPlane-nearPlane) ) * (1/gl_Position[2]);\n"
66  " depth = (depth+1.0)/2.0;\n"
67  " gl_Position = projectionMatrix * gl_Position;\n"
68  " ex_Color = in_Color;\n"
69  "}\n";
70 
71 
77 class CameraRep : public Tucano::Mesh {
78 
79 private:
80 
82 
83 public:
84 
90  {
91 
94 
95  #ifdef TUCANOSHADERDIR
96  initialize(stringify(TUCANOSHADERDIR));
97  #endif
98  }
99 
104  void initialize (string shader_dir = "../effects/shaders/")
105  {
106  camerarep_shader.load("phongshader", shader_dir);
107  camerarep_shader.initialize();
108  }
109 
110 
114  void render (const Tucano::Camera& camera, const Tucano::Camera& light)
115  {
116  Eigen::Vector4f viewport = camera.getViewport();
117  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
118 
119  camerarep_shader.bind();
120 
121  camerarep_shader.setUniform("viewMatrix", camera.getViewMatrix());
122  camerarep_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
123  camerarep_shader.setUniform("nearPlane", camera.getNearPlane());
124  camerarep_shader.setUniform("farPlane", camera.getFarPlane());
125 
126  Eigen::Vector4f color (1.0, 1.0, 0.0, 1.0);
127  camerarep_shader.setUniform("modelMatrix", model_matrix);
128  camerarep_shader.setUniform("lightViewMatrix", light.getViewMatrix());
129  camerarep_shader.setUniform("has_color", true);
130  camerarep_shader.setUniform("default_color", color);
131 
132  setAttributeLocation(camerarep_shader);
133 
134  glEnable(GL_DEPTH_TEST);
135  bindBuffers();
136  renderElements();
137  unbindBuffers();
138  glDisable(GL_DEPTH_TEST);
139 
140  camerarep_shader.unbind();
141 
142  }
143 
144 private:
145 
151  void createGeometry (void)
152  {
153  vector< Eigen::Vector4f > vert;
154  vector< Eigen::Vector3f > norm;
155  vector< Eigen::Vector4f > color;
156  vector< GLuint > elementsVertices;
157 
158  // repeating vertices for each face to make life easier with normals
159  // and other attributes
160 
161  // right face
162  vert.push_back ( Eigen::Vector4f( 0.25, -0.25, 0.5, 1.0) );
163  vert.push_back ( Eigen::Vector4f( 0.50, -0.50, -0.5, 1.0) );
164  vert.push_back ( Eigen::Vector4f( 0.50, 0.50, -0.5, 1.0) );
165  vert.push_back ( Eigen::Vector4f( 0.25, 0.25, 0.5, 1.0) );
166  norm.push_back ( Eigen::Vector3f( 1.0, 0.0, 0.0) );
167  norm.push_back ( Eigen::Vector3f( 1.0, 0.0, 0.0) );
168  norm.push_back ( Eigen::Vector3f( 1.0, 0.0, 0.0) );
169  norm.push_back ( Eigen::Vector3f( 1.0, 0.0, 0.0) );
170  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
171  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
172  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
173  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
174  elementsVertices.push_back(0);
175  elementsVertices.push_back(1);
176  elementsVertices.push_back(2);
177  elementsVertices.push_back(2);
178  elementsVertices.push_back(3);
179  elementsVertices.push_back(0);
180 
181  // left face
182  vert.push_back ( Eigen::Vector4f(-0.25, -0.25, 0.5, 1.0) );
183  vert.push_back ( Eigen::Vector4f(-0.50, -0.50, -0.5, 1.0) );
184  vert.push_back ( Eigen::Vector4f(-0.50, 0.50, -0.5, 1.0) );
185  vert.push_back ( Eigen::Vector4f(-0.25, 0.25, 0.5, 1.0) );
186  norm.push_back ( Eigen::Vector3f( -1.0, 0.0, 0.0) );
187  norm.push_back ( Eigen::Vector3f( -1.0, 0.0, 0.0) );
188  norm.push_back ( Eigen::Vector3f( -1.0, 0.0, 0.0) );
189  norm.push_back ( Eigen::Vector3f( -1.0, 0.0, 0.0) );
190  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
191  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
192  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
193  color.push_back( Eigen::Vector4f( 1.0, 0.0, 0.0, 1.0) );
194  elementsVertices.push_back(4);
195  elementsVertices.push_back(7);
196  elementsVertices.push_back(6);
197  elementsVertices.push_back(6);
198  elementsVertices.push_back(5);
199  elementsVertices.push_back(4);
200 
201  // bottom face
202  vert.push_back ( Eigen::Vector4f( 0.25, -0.25, 0.5, 1.0) );
203  vert.push_back ( Eigen::Vector4f( 0.50, -0.50, -0.5, 1.0) );
204  vert.push_back ( Eigen::Vector4f(-0.25, -0.25, 0.5, 1.0) );
205  vert.push_back ( Eigen::Vector4f(-0.50, -0.50, -0.5, 1.0) );
206  norm.push_back ( Eigen::Vector3f( 0.0, -1.0, 0.0) );
207  norm.push_back ( Eigen::Vector3f( 0.0, -1.0, 0.0) );
208  norm.push_back ( Eigen::Vector3f( 0.0, -1.0, 0.0) );
209  norm.push_back ( Eigen::Vector3f( 0.0, -1.0, 0.0) );
210  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
211  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
212  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
213  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
214  elementsVertices.push_back(8);
215  elementsVertices.push_back(10);
216  elementsVertices.push_back(11);
217  elementsVertices.push_back(11);
218  elementsVertices.push_back(9);
219  elementsVertices.push_back(8);
220 
221  // top face
222  vert.push_back ( Eigen::Vector4f( 0.25, 0.25, 0.5, 1.0) );
223  vert.push_back ( Eigen::Vector4f( 0.50, 0.50, -0.5, 1.0) );
224  vert.push_back ( Eigen::Vector4f(-0.25, 0.25, 0.5, 1.0) );
225  vert.push_back ( Eigen::Vector4f(-0.50, 0.50, -0.5, 1.0) );
226  norm.push_back ( Eigen::Vector3f( 0.0, 1.0, 0.0) );
227  norm.push_back ( Eigen::Vector3f( 0.0, 1.0, 0.0) );
228  norm.push_back ( Eigen::Vector3f( 0.0, 1.0, 0.0) );
229  norm.push_back ( Eigen::Vector3f( 0.0, 1.0, 0.0) );
230  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
231  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
232  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
233  color.push_back( Eigen::Vector4f( 0.0, 1.0, 0.0, 1.0) );
234  elementsVertices.push_back(12);
235  elementsVertices.push_back(13);
236  elementsVertices.push_back(15);
237  elementsVertices.push_back(15);
238  elementsVertices.push_back(14);
239  elementsVertices.push_back(12);
240 
241  // front face
242  vert.push_back ( Eigen::Vector4f( 0.5, -0.50, -0.5, 1.0) );
243  vert.push_back ( Eigen::Vector4f( 0.5, 0.50, -0.5, 1.0) );
244  vert.push_back ( Eigen::Vector4f(-0.5, -0.50, -0.5, 1.0) );
245  vert.push_back ( Eigen::Vector4f(-0.5, 0.50, -0.5, 1.0) );
246  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, -1.0) );
247  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, -1.0) );
248  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, -1.0) );
249  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, -1.0) );
250  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
251  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
252  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
253  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
254  elementsVertices.push_back(16);
255  elementsVertices.push_back(18);
256  elementsVertices.push_back(19);
257  elementsVertices.push_back(19);
258  elementsVertices.push_back(17);
259  elementsVertices.push_back(16);
260 
261  // back face
262  vert.push_back ( Eigen::Vector4f( 0.25, -0.25, 0.5, 1.0) );
263  vert.push_back ( Eigen::Vector4f( 0.25, 0.25, 0.5, 1.0) );
264  vert.push_back ( Eigen::Vector4f(-0.25, -0.25, 0.5, 1.0) );
265  vert.push_back ( Eigen::Vector4f(-0.25, 0.25, 0.5, 1.0) );
266  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, 1.0) );
267  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, 1.0) );
268  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, 1.0) );
269  norm.push_back ( Eigen::Vector3f( 0.0, 0.0, 1.0) );
270  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
271  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
272  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
273  color.push_back( Eigen::Vector4f( 0.0, 0.0, 1.0, 1.0) );
274  elementsVertices.push_back(20);
275  elementsVertices.push_back(21);
276  elementsVertices.push_back(23);
277  elementsVertices.push_back(23);
278  elementsVertices.push_back(22);
279  elementsVertices.push_back(20);
280 
281  loadVertices(vert);
282  loadNormals(norm);
283  loadColors(color);
284  loadIndices(elementsVertices);
285 
287 
288  }
289 
290 };
291 }
292 }
293 #endif
float getNearPlane(void) const
Returns near plane value.
Definition: camera.hpp:319
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 loadColors(vector< Eigen::Vector4f > &clrs)
Load colors (r,g,b,a) as a vertex attribute.
Definition: mesh.hpp:438
void initialize(string shader_dir="../effects/shaders/")
Initializes shaders.
Definition: camerarep.hpp:104
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
const string camerarep_vertex_code
Default vertex shader for rendering trackball representation.
Definition: camerarep.hpp:51
Definition: bufferobject.hpp:34
float getFarPlane(void) const
Returns far plane value.
Definition: camera.hpp:328
void initialize(void)
Calls all the functions related to the shader initialization, i.e., creates, loads the shaders from t...
Definition: shader.hpp:641
void render(const Tucano::Camera &camera, const Tucano::Camera &light)
Render camera representation.
Definition: camerarep.hpp:114
A Shader object represents one GLSL program.
Definition: shader.hpp:45
Camera visual representation.
Definition: camerarep.hpp:77
void loadIndices(vector< GLuint > &ind)
Load indices into indices array.
Definition: mesh.hpp:448
const string camerarep_fragment_code
Default fragment shader for rendering camera representation.
Definition: camerarep.hpp:39
CameraRep()
Default Constructor.
Definition: camerarep.hpp:89
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
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
Tucano::Shader camerarep_shader
Definition: camerarep.hpp:81
void createGeometry(void)
Define camera geometry.
Definition: camerarep.hpp:151
void load(string name, string shader_dir="")
Loads a shader given a directory and a name. Searches for all shader extensions in directory...
Definition: shader.hpp:436
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
#define stringify(x)
Definition: misc.hpp:33
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