mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
reformat geometry
This commit is contained in:
parent
d95b0f85e2
commit
08cbf7733f
4 changed files with 39 additions and 39 deletions
|
@ -3,14 +3,14 @@
|
|||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
|
||||
Vector3f Geometry::diffuse() const { return cd; }
|
||||
Vector3f Geometry::specular() const { return cs; }
|
||||
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; }
|
||||
Geometry::Type Geometry::getType() const { return type; }
|
||||
Vector3f Geometry::cd() const { return cd_; }
|
||||
Vector3f Geometry::cs() const { return cs_; }
|
||||
Vector3f Geometry::ca() const { return ca_; }
|
||||
float Geometry::kd() const { return kd_; }
|
||||
float Geometry::ks() const { return ks_; }
|
||||
float Geometry::ka() const { return ka_; }
|
||||
float Geometry::phong() const { return phong_; }
|
||||
Geometry::Type Geometry::type() const { return type_; }
|
||||
|
||||
void Geometry::setTransform(const Matrix4f &transform) {
|
||||
this->transform = transform;
|
||||
|
@ -36,7 +36,7 @@ Optional<float> Sphere::intersect(const Ray &r) const {
|
|||
return Optional<float>::nullopt;
|
||||
}
|
||||
|
||||
Vector3f Sphere::getNormal(const Vector3f &p) const {
|
||||
Vector3f Sphere::normal(const Vector3f &p) const {
|
||||
return (p - center).normalized();
|
||||
}
|
||||
|
||||
|
@ -52,18 +52,18 @@ bool isInRectangle(const Vector3f &p, const Vector3f &a, const Vector3f &b,
|
|||
}
|
||||
|
||||
Optional<float> Rectangle::intersect(const Ray &r) const {
|
||||
float denom = normal.dot(r.getDirection());
|
||||
float denom = normal_.dot(r.getDirection());
|
||||
if (abs(denom) < 1e-6f)
|
||||
return Optional<float>::nullopt;
|
||||
|
||||
float t = -normal.dot(r.getOrigin() - p1) / denom;
|
||||
float t = -normal_.dot(r.getOrigin() - p1) / denom;
|
||||
if (t <= 0)
|
||||
return Optional<float>::nullopt;
|
||||
|
||||
Vector3f p = r.getOrigin() + t * r.getDirection();
|
||||
|
||||
return isInRectangle(p, p1, p2, p3, p4, normal) ? Optional<float>(t)
|
||||
: Optional<float>::nullopt;
|
||||
return isInRectangle(p, p1, p2, p3, p4, normal_) ? Optional<float>(t)
|
||||
: Optional<float>::nullopt;
|
||||
}
|
||||
|
||||
Vector3f Rectangle::getNormal(const Vector3f &p) const { return normal; }
|
||||
Vector3f Rectangle::normal(const Vector3f &p) const { return normal_; }
|
||||
|
|
|
@ -19,29 +19,30 @@ public:
|
|||
|
||||
virtual ~Geometry() = default;
|
||||
virtual Optional<float> intersect(const Ray &) const = 0;
|
||||
virtual Vector3f getNormal(const Vector3f &) const = 0;
|
||||
virtual Vector3f normal(const Vector3f &) const = 0;
|
||||
|
||||
protected:
|
||||
Geometry(Type type, float ka, float kd, float ks, const Vector3f &ca,
|
||||
const Vector3f &cd, const Vector3f &cs, float pc)
|
||||
: type(type), ka(ka), kd(kd), ks(ks), ca(ca), cd(cd), cs(cs), phong(pc) {}
|
||||
: type_(type), ka_(ka), kd_(kd), ks_(ks), ca_(ca), cd_(cd), cs_(cs),
|
||||
phong_(pc) {}
|
||||
|
||||
Type type;
|
||||
float ka, kd, ks; // coefficients for ambient, diffuse and specular
|
||||
Vector3f ca, cd, cs; // ambient, diffuse and specular reflection color
|
||||
float phong; // phone coefficient, for `pc`
|
||||
Type type_;
|
||||
float ka_, kd_, ks_; // coefficients for ambient, diffuse and specular
|
||||
Vector3f ca_, cd_, cs_; // ambient, diffuse and specular reflection color
|
||||
float phong_; // phone coefficient, for `pc`
|
||||
Matrix4f transform = Matrix4f::Identity();
|
||||
|
||||
public:
|
||||
Vector3f diffuse() const;
|
||||
Vector3f specular() const;
|
||||
Vector3f ambient() const;
|
||||
float coefDiffuse() const;
|
||||
float coefSpecular() const;
|
||||
float coefAmbient() const;
|
||||
float getPhong() const;
|
||||
Vector3f cd() const;
|
||||
Vector3f cs() const;
|
||||
Vector3f ca() const;
|
||||
float kd() const;
|
||||
float ks() const;
|
||||
float ka() const;
|
||||
float phong() const;
|
||||
Type type() const;
|
||||
void setTransform(const Matrix4f &);
|
||||
Type getType() const;
|
||||
};
|
||||
|
||||
class Sphere : public Geometry {
|
||||
|
@ -52,7 +53,7 @@ public:
|
|||
center(center) {}
|
||||
|
||||
Optional<float> intersect(const Ray &) const override;
|
||||
Vector3f getNormal(const Vector3f &) const override;
|
||||
Vector3f normal(const Vector3f &) const override;
|
||||
|
||||
private:
|
||||
float radius;
|
||||
|
@ -65,14 +66,14 @@ public:
|
|||
const Vector3f &cs, float pc, const Vector3f &p1,
|
||||
const Vector3f &p2, const Vector3f &p3, const Vector3f &p4)
|
||||
: Geometry(Type::RECTANGLE, ka, kd, ks, ca, cd, cs, pc), p1(p1), p2(p2),
|
||||
p3(p3), p4(p4), normal((p2 - p1).cross(p3 - p1).normalized()) {}
|
||||
p3(p3), p4(p4), normal_((p2 - p1).cross(p3 - p1).normalized()) {}
|
||||
|
||||
Optional<float> intersect(const Ray &) const override;
|
||||
Vector3f getNormal(const Vector3f &) const override;
|
||||
Vector3f normal(const Vector3f &) const override;
|
||||
|
||||
private:
|
||||
Vector3f p1, p2, p3, p4;
|
||||
Vector3f normal;
|
||||
Vector3f normal_;
|
||||
};
|
||||
|
||||
#endif // !GEOMETRY_H_
|
||||
|
|
|
@ -16,4 +16,4 @@ Vector3f HitRecord::viewDirection() const {
|
|||
|
||||
Vector3f HitRecord::normal() const { return n; }
|
||||
|
||||
void HitRecord::calcNormal() { n = g->getNormal(getPoint()); }
|
||||
void HitRecord::calcNormal() { n = g->normal(getPoint()); }
|
||||
|
|
11
src/Light.cc
11
src/Light.cc
|
@ -27,20 +27,19 @@ Vector3f PointLight::illumination(const HitRecord &hit,
|
|||
|
||||
for (auto g : geometries)
|
||||
if (g != geometry && g->intersect(shadowRay).hasValue() &&
|
||||
g->getType() == Geometry::Type::SPHERE)
|
||||
g->type() == Geometry::Type::SPHERE)
|
||||
return Vector3f::Zero();
|
||||
|
||||
Vector3f ambient_ = geometry->coefAmbient() * geometry->ambient();
|
||||
Vector3f ambient_ = geometry->ka() * geometry->ca();
|
||||
|
||||
Vector3f diffuse_ = geometry->coefDiffuse() * geometry->diffuse().array() *
|
||||
Vector3f diffuse_ = geometry->kd() * geometry->cd().array() *
|
||||
diffuse.array() *
|
||||
std::max(0.0f, hit.normal().dot(rayDirection));
|
||||
|
||||
Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized();
|
||||
Vector3f specular_ =
|
||||
geometry->coefSpecular() * geometry->specular().array() *
|
||||
specular.array() *
|
||||
pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->getPhong());
|
||||
geometry->ks() * geometry->cs().array() * specular.array() *
|
||||
pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->phong());
|
||||
|
||||
return specular_ + ambient_ + diffuse_;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue