Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
phongshader.hpp
Go to the documentation of this file.
1 
23 #ifndef __PHONG__
24 #define __PHONG__
25 
26 #include <tucano/effect.hpp>
27 #include <tucano/camera.hpp>
28 #include <tucano/mesh.hpp>
29 
30 namespace Tucano
31 {
32 namespace Effects
33 {
34 
38 class Phong : public Tucano::Effect
39 {
40 
41 private:
42 
45 
47  Eigen::Vector4f default_color = Eigen::Vector4f (0.7, 0.7, 0.7, 1.0);
48 
50  float ka = 0.5;
51 
53  float kd = 0.8;
54 
56  float ks = 0.5;
57 
59  float shininess = 10;
60 
61 public:
62 
66  Phong (void)
67  {}
68 
72  virtual void initialize (void)
73  {
74  // searches in default shader directory (/shaders) for shader files phongShader.(vert,frag,geom,comp)
75  loadShader(phong_shader, "phongshader") ;
76  }
77 
81  void setDefaultColor ( const Eigen::Vector4f& color )
82  {
83  default_color = color;
84  }
85 
90  void setAmbientCoeff (float value)
91  {
92  ka = value;
93  }
94 
99  void setDiffuseCoeff (float value)
100  {
101  kd = value;
102  }
103 
108  void setSpecularCoeff (float value)
109  {
110  ks = value;
111  }
112 
117  void setShininessCoeff (float value)
118  {
119  shininess = value;
120  }
121 
122 
123  float getDiffuseCoeff (void ) {return kd;}
124  float getAmbientCoeff (void ) {return ka;}
125  float getSpecularCoeff (void ) {return ks;}
126  float getShininessCoeff (void ) {return shininess;}
127 
133  void render (Tucano::Mesh& mesh, const Tucano::Camera& camera, const Tucano::Camera& lightTrackball)
134  {
135 
136  Eigen::Vector4f viewport = camera.getViewport();
137  glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
138 
139  phong_shader.bind();
140 
141  // sets all uniform variables for the phong shader
142  phong_shader.setUniform("projectionMatrix", camera.getProjectionMatrix());
143  phong_shader.setUniform("modelMatrix", mesh.getModelMatrix());
144  phong_shader.setUniform("viewMatrix", camera.getViewMatrix());
145  phong_shader.setUniform("lightViewMatrix", lightTrackball.getViewMatrix());
146  phong_shader.setUniform("has_color", mesh.hasAttribute("in_Color"));
147  phong_shader.setUniform("default_color", mesh.getColor());
148  phong_shader.setUniform("ka", ka);
149  phong_shader.setUniform("kd", kd);
150  phong_shader.setUniform("ks", ks);
151  phong_shader.setUniform("shininess", shininess);
152 
153  mesh.setAttributeLocation(phong_shader);
154 
155  mesh.render();
156 
157  phong_shader.unbind();
158  }
159 
160 
161 };
162 }
163 }
164 
165 
166 #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
Renders a mesh using a Phong shader.
Definition: phongshader.hpp:38
Definition: bufferobject.hpp:34
void setSpecularCoeff(float value)
Set specular coefficient.
Definition: phongshader.hpp:108
A Shader object represents one GLSL program.
Definition: shader.hpp:45
void setDefaultColor(const Eigen::Vector4f &color)
Sets the default color, usually used for meshes without color attribute.
Definition: phongshader.hpp:81
float getDiffuseCoeff(void)
Definition: phongshader.hpp:123
float getAmbientCoeff(void)
Definition: phongshader.hpp:124
Phong(void)
Default constructor.
Definition: phongshader.hpp:66
float kd
Diffuse coefficient.
Definition: phongshader.hpp:53
bool hasAttribute(const string &name) const
Returns wether an attribute exists or not.
Definition: mesh.hpp:503
Eigen::Vector4f default_color
Default color.
Definition: phongshader.hpp:47
Tucano::Shader phong_shader
Phong Shader.
Definition: phongshader.hpp:44
void setDiffuseCoeff(float value)
Set diffuse coefficient.
Definition: phongshader.hpp:99
float getSpecularCoeff(void)
Definition: phongshader.hpp:125
void setShininessCoeff(float value)
Set shininess exponent.
Definition: phongshader.hpp:117
virtual void initialize(void)
Load and initialize shaders.
Definition: phongshader.hpp:72
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
float ka
Ambient coefficient.
Definition: phongshader.hpp:50
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
float ks
Specular coefficient.
Definition: phongshader.hpp:56
float shininess
Shininess.
Definition: phongshader.hpp:59
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
void setAmbientCoeff(float value)
Set ambient coefficient.
Definition: phongshader.hpp:90
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
The Effect class is a holder for Shaders. It is completely optional, but is contains a few methods fo...
Definition: effect.hpp:43
void render(Tucano::Mesh &mesh, const Tucano::Camera &camera, const Tucano::Camera &lightTrackball)
Render the mesh given a camera and light, using a Phong shader.
Definition: phongshader.hpp:133
float getShininessCoeff(void)
Definition: phongshader.hpp:126
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