mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-07 23:12:00 +00:00
calculate ambient, diffuse and specular
This commit is contained in:
parent
bb8819013c
commit
27bdd95fa6
5 changed files with 21 additions and 1 deletions
|
@ -9,6 +9,7 @@ Vector3f Geometry::ambient() const { return ca; }
|
||||||
float Geometry::coefDiffuse() const { return kd; }
|
float Geometry::coefDiffuse() const { return kd; }
|
||||||
float Geometry::coefSpecular() const { return ks; }
|
float Geometry::coefSpecular() const { return ks; }
|
||||||
float Geometry::coefAmbient() const { return ka; }
|
float Geometry::coefAmbient() const { return ka; }
|
||||||
|
float Geometry::getPhong() const { return phong; }
|
||||||
|
|
||||||
void Geometry::setTransform(const Matrix4f &transform) {
|
void Geometry::setTransform(const Matrix4f &transform) {
|
||||||
this->transform = transform;
|
this->transform = transform;
|
||||||
|
|
|
@ -39,6 +39,7 @@ public:
|
||||||
float coefDiffuse() const;
|
float coefDiffuse() const;
|
||||||
float coefSpecular() const;
|
float coefSpecular() const;
|
||||||
float coefAmbient() const;
|
float coefAmbient() const;
|
||||||
|
float getPhong() const;
|
||||||
void setTransform(const Matrix4f &);
|
void setTransform(const Matrix4f &);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,10 @@ Vector3f HitRecord::getPoint() const {
|
||||||
return r.getOrigin() + t * r.getDirection();
|
return r.getOrigin() + t * r.getDirection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector3f HitRecord::viewDirection() const {
|
||||||
|
return -r.getDirection().normalized();
|
||||||
|
}
|
||||||
|
|
||||||
Vector3f HitRecord::normal() const { return n; }
|
Vector3f HitRecord::normal() const { return n; }
|
||||||
|
|
||||||
void HitRecord::calcNormal() { n = g->getNormal(getPoint()); }
|
void HitRecord::calcNormal() { n = g->getNormal(getPoint()); }
|
||||||
|
|
|
@ -21,6 +21,7 @@ private:
|
||||||
public:
|
public:
|
||||||
Geometry *geometry() const;
|
Geometry *geometry() const;
|
||||||
Vector3f getPoint() const;
|
Vector3f getPoint() const;
|
||||||
|
Vector3f viewDirection() const;
|
||||||
Vector3f normal() const;
|
Vector3f normal() const;
|
||||||
void calcNormal();
|
void calcNormal();
|
||||||
};
|
};
|
||||||
|
|
15
src/Light.cc
15
src/Light.cc
|
@ -1,4 +1,6 @@
|
||||||
#include "Light.h"
|
#include "Light.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
void Light::setTransform(const Matrix4f &transform) {
|
void Light::setTransform(const Matrix4f &transform) {
|
||||||
this->transform = transform;
|
this->transform = transform;
|
||||||
|
@ -19,7 +21,18 @@ Vector3f PointLight::illumination(const HitRecord &hit,
|
||||||
if (g != geometry && g->intersect(shadowRay).hasValue())
|
if (g != geometry && g->intersect(shadowRay).hasValue())
|
||||||
return Vector3f::Zero();
|
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,
|
Vector3f AreaLight::illumination(const HitRecord &hit,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue