1#include "materials/Material.hpp"
42 const std::vector<std::reference_wrapper<Light>> &lights)
override {
43 Color outputColor(0, 0, 0);
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;
51 }
else if (light.isPointLight()) {
52 const auto pointLight =
dynamic_cast<PointLight &
>(light);
54 const auto fragWorldPosition =
57 const auto lightWorldPosition =
58 (light.modelMatrix *
Vector4(light.localPosition, 1)).toVector3();
59 const auto worldNormal =
62 const auto lightDirection = lightWorldPosition - fragWorldPosition;
63 const double distance =
Vector3::dot(lightDirection, lightDirection);
64 lightDirection.unit();
67 std::max(
Vector3::dot(lightDirection, worldNormal), 0.0);
71 const auto viewDirection =
73 const auto halfway = (lightDirection + viewDirection).normalize();
74 const auto specularAngle =
76 specular = std::pow(specularAngle,
shininess);
79 outputColor +=
diffuseColor * lambertian * pointLight.color *
80 pointLight.power() / distance +
82 pointLight.power() / distance;
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 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