From 27bdd95fa6df5c52cdb3fa43e9444773dd771da0 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Thu, 29 Feb 2024 01:16:49 -0500 Subject: [PATCH] calculate ambient, diffuse and specular --- src/Geometry.cc | 1 + src/Geometry.h | 1 + src/HitRecord.cc | 4 ++++ src/HitRecord.h | 1 + src/Light.cc | 15 ++++++++++++++- 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Geometry.cc b/src/Geometry.cc index 1969397..0a29ab8 100644 --- a/src/Geometry.cc +++ b/src/Geometry.cc @@ -9,6 +9,7 @@ Vector3f Geometry::ambient() const { return ca; } float Geometry::coefDiffuse() const { return kd; } float Geometry::coefSpecular() const { return ks; } float Geometry::coefAmbient() const { return ka; } +float Geometry::getPhong() const { return phong; } void Geometry::setTransform(const Matrix4f &transform) { this->transform = transform; diff --git a/src/Geometry.h b/src/Geometry.h index dc3be95..b3d1eee 100644 --- a/src/Geometry.h +++ b/src/Geometry.h @@ -39,6 +39,7 @@ public: float coefDiffuse() const; float coefSpecular() const; float coefAmbient() const; + float getPhong() const; void setTransform(const Matrix4f &); }; diff --git a/src/HitRecord.cc b/src/HitRecord.cc index 9f50540..6a07ee7 100644 --- a/src/HitRecord.cc +++ b/src/HitRecord.cc @@ -10,6 +10,10 @@ Vector3f HitRecord::getPoint() const { return r.getOrigin() + t * r.getDirection(); } +Vector3f HitRecord::viewDirection() const { + return -r.getDirection().normalized(); +} + Vector3f HitRecord::normal() const { return n; } void HitRecord::calcNormal() { n = g->getNormal(getPoint()); } diff --git a/src/HitRecord.h b/src/HitRecord.h index 87290fc..c7fad17 100644 --- a/src/HitRecord.h +++ b/src/HitRecord.h @@ -21,6 +21,7 @@ private: public: Geometry *geometry() const; Vector3f getPoint() const; + Vector3f viewDirection() const; Vector3f normal() const; void calcNormal(); }; diff --git a/src/Light.cc b/src/Light.cc index 68fb168..724dade 100644 --- a/src/Light.cc +++ b/src/Light.cc @@ -1,4 +1,6 @@ #include "Light.h" +#include +#include void Light::setTransform(const Matrix4f &transform) { this->transform = transform; @@ -19,7 +21,18 @@ Vector3f PointLight::illumination(const HitRecord &hit, if (g != geometry && g->intersect(shadowRay).hasValue()) return Vector3f::Zero(); - return Vector3f::Zero(); + float distance = (center - shadingPoint).norm(); + float att = 1.0f / distance / distance; + + Vector3f ambient_ = geometry->coefAmbient() * geometry->ambient(); + Vector3f diffuse_ = att * geometry->coefDiffuse() * diffuse * + std::max(0.0f, hit.normal().dot(rayDirection)); + + Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized(); + Vector3f specular_ = + att * geometry->coefSpecular() * specular * + pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->getPhong()); + return ambient_ + diffuse_ + specular_; } Vector3f AreaLight::illumination(const HitRecord &hit,