t
Loading...
Searching...
No Matches
BlinnPhong.hpp
1#include "materials/Material.hpp"
2#include <cmath>
3
4#ifndef BLINNPHONG_HPP
5#define BLINNPHONG_HPP
6
7namespace t {
8
15class BlinnPhong : public Material {
16public:
19 double shininess;
30 BlinnPhong(Color _diffuseColor, Color _specularColor, double _shininess)
31 : diffuseColor(_diffuseColor), specularColor(_specularColor),
32 shininess(_shininess) {};
33
34 Vector4 vertexShader(const Uniforms &uniforms,
35 const Attributes &attributes) override {
36 return uniforms.projectionMatrix * uniforms.modelViewMatrix *
37 Vector4(attributes.localPosition, 1.0);
38 }
39
41 const Uniforms &uniforms, const Varyings &varyings,
42 const std::vector<std::reference_wrapper<Light>> &lights) override {
43 Color outputColor(0, 0, 0);
44
45 for (Light &light : lights) {
46 if (light.isAmbientLight()) {
47 const auto ambientLight = dynamic_cast<AmbientLight &>(light);
48 const auto ambient = ambientLight.intensity * ambientLight.color;
49
50 outputColor += ambient * diffuseColor;
51 } else if (light.isPointLight()) {
52 const auto pointLight = dynamic_cast<PointLight &>(light);
53
54 const auto fragWorldPosition =
55 (uniforms.modelMatrix * Vector4(varyings.localPosition, 1))
56 .toVector3();
57 const auto lightWorldPosition =
58 (light.modelMatrix * Vector4(light.localPosition, 1)).toVector3();
59 const auto worldNormal =
60 (uniforms.normalMatrix * varyings.localNormal).normalize();
61
62 const auto lightDirection = lightWorldPosition - fragWorldPosition;
63 const double distance = Vector3::dot(lightDirection, lightDirection);
64 lightDirection.unit();
65
66 auto lambertian =
67 std::max(Vector3::dot(lightDirection, worldNormal), 0.0);
68 auto specular = 0.0;
69
70 if (lambertian > 0) {
71 const auto viewDirection =
72 (uniforms.cameraPosition - fragWorldPosition).normalize();
73 const auto halfway = (lightDirection + viewDirection).normalize();
74 const auto specularAngle =
75 std::max(Vector3::dot(halfway, worldNormal), 0.0);
76 specular = std::pow(specularAngle, shininess);
77 }
78
79 outputColor += diffuseColor * lambertian * pointLight.color *
80 pointLight.power() / distance +
81 specularColor * specular * pointLight.color *
82 pointLight.power() / distance;
83 }
84 }
85
86 return outputColor;
87 }
88};
89
90} // namespace t
91
92#endif // BLINNPHONG_HPP
The ambient light, which illuminates all objects in the scene equally.
Definition AmbientLight.hpp:26
double intensity
The intensity of this ambient light.
Definition AmbientLight.hpp:29
A shiny material that uses the Blinn-Phong reflection model.
Definition BlinnPhong.hpp:15
Color specularColor
The color of the specular highlight.
Definition BlinnPhong.hpp:18
Vector4 vertexShader(const Uniforms &uniforms, const Attributes &attributes) override
The vertex shader of this material, which will be run for every vertex of the mesh's geometry.
Definition BlinnPhong.hpp:34
double shininess
The shininess constant of this material.
Definition BlinnPhong.hpp:19
Color fragmentShader(const Uniforms &uniforms, const Varyings &varyings, const std::vector< std::reference_wrapper< Light > > &lights) override
The fragment shader of this material, which will be run for every fragment that the mesh covers on th...
Definition BlinnPhong.hpp:40
Color diffuseColor
The base color of this material.
Definition BlinnPhong.hpp:17
BlinnPhong(Color _diffuseColor, Color _specularColor, double _shininess)
Creates a new shiny material with the specified diffuse color, specular color, and shininess.
Definition BlinnPhong.hpp:30
The color class.
Definition Color.hpp:18
The base lighting class.
Definition Light.hpp:17
The base material class.
Definition Material.hpp:24
The point light, which gets emitted from a single point in all directions.
Definition PointLight.hpp:22
static double dot(const Vector3 &a, const Vector3 &b)
Returns the dot product of two vectors.
Definition Vector3.hpp:67
The 4D vector class.
Definition Vector4.hpp:20
The t software 3D graphics library namespace.
Definition algorithms.hpp:12
The attributes of a vertex, available to vertex shaders.
Definition Attributes.hpp:14
Vector3 & localPosition
The local position of the current vertex.
Definition Attributes.hpp:15
The uniforms available to vertex shaders and fragment shaders.
Definition Uniforms.hpp:19
Matrix4x4 & projectionMatrix
The projection matrix of the active camera.
Definition Uniforms.hpp:24
Matrix4x4 & modelMatrix
The model matrix of the current 3D mesh.
Definition Uniforms.hpp:20
Vector3 & cameraPosition
The position of the camera in world space.
Definition Uniforms.hpp:27
Matrix4x4 & modelViewMatrix
The product of the view matrix and the model matrix.
Definition Uniforms.hpp:21
Matrix3x3 & normalMatrix
The normal matrix of the current 3D mesh.
Definition Uniforms.hpp:26
The varyings available to fragment shaders.
Definition Varyings.hpp:19
Vector3 & localPosition
The position in local space associateed with the current fragment.
Definition Varyings.hpp:20
Vector3 & localNormal
The normal vector in local space associated with the current fragment.
Definition Varyings.hpp:22