Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
toon.hpp
Go to the documentation of this file.
1 
23 #ifndef __TOON__
24 #define __TOON__
25 
26 #include <tucano/effect.hpp>
27 #include <tucano/camera.hpp>
28 
29 namespace Tucano
30 {
31 namespace Effects
32 {
33 
37 class Toon : public Tucano::Effect
38 {
39 
40 private:
41 
44 
46  float quantization_level = 8;
47 
48 public:
49 
53  Toon (void) {}
54 
59  void setQuantizationLevel(int level)
60  {
61  quantization_level = level;
62  }
63 
69  {
70  return quantization_level;
71  }
72 
73 
77  virtual void initialize (void)
78  {
79  loadShader(toon_shader, "toonshader");
80  }
81 
88  virtual void render (Tucano::Mesh& mesh, const Tucano::Camera& cameraTrackball, const Tucano::Camera& lightTrackball)
89  {
90 
91  Eigen::Vector4f viewport = cameraTrackball.getViewport();
92  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
93 
94  toon_shader.bind();
95 
96  toon_shader.setUniform("projectionMatrix", cameraTrackball.getProjectionMatrix());
97  toon_shader.setUniform("modelMatrix", mesh.getModelMatrix());
98  toon_shader.setUniform("viewMatrix", cameraTrackball.getViewMatrix());
99  toon_shader.setUniform("lightViewMatrix", lightTrackball.getViewMatrix());
100  toon_shader.setUniform("has_color", mesh.hasAttribute("in_Color"));
101  toon_shader.setUniform("default_color", mesh.getColor());
102  toon_shader.setUniform("quantizationLevel", quantization_level);
103 
104  mesh.setAttributeLocation(toon_shader);
105  mesh.render();
106 
107  toon_shader.unbind();
108  }
109 };
110 }
111 }
112 
113 #endif
Eigen::Vector4f getColor(void)
Returns the default color of the model.
Definition: model.hpp:56
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
virtual void render(Tucano::Mesh &mesh, const Tucano::Camera &cameraTrackball, const Tucano::Camera &lightTrackball)
Render the mesh given a camera and light trackball, using a Toon shader.
Definition: toon.hpp:88
A Shader object represents one GLSL program.
Definition: shader.hpp:45
virtual void initialize(void)
Load and initialize shaders.
Definition: toon.hpp:77
Renders a mesh using a Toon shader.
Definition: toon.hpp:37
bool hasAttribute(const string &name) const
Returns wether an attribute exists or not.
Definition: mesh.hpp:503
void setQuantizationLevel(int level)
Set the quantization level.
Definition: toon.hpp:59
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
Tucano::Shader toon_shader
Toon shader.
Definition: toon.hpp:43
virtual Shader * loadShader(string shader_name)
Loads a shader by filename, initializes it, and inserts in shaders list.
Definition: effect.hpp:73
virtual void render(void)
Render the mesh triangles. The method binds the buffers, calls the method to render triangles...
Definition: mesh.hpp:756
Eigen::Affine3f getModelMatrix(void) const
Returns the model matrix.
Definition: model.hpp:103
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
Toon(void)
Default constructor.
Definition: toon.hpp:53
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
int getQuantizationLevel(void)
Returns the quantization level.
Definition: toon.hpp:68
Eigen::Vector4f getViewport(void) const
Returns the viewport coordinates.
Definition: camera.hpp:246
The Effect class is a holder for Shaders. It is completely optional, but is contains a few methods fo...
Definition: effect.hpp:43
float quantization_level
Number of colors that will be used in color quantization to create the toonish effect.
Definition: toon.hpp:46
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