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 <Eigen/Dense>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
Vector3f Geometry::diffuse() const { return cd; }
|
Vector3f Geometry::cd() const { return cd_; }
|
||||||
Vector3f Geometry::specular() const { return cs; }
|
Vector3f Geometry::cs() const { return cs_; }
|
||||||
Vector3f Geometry::ambient() const { return ca; }
|
Vector3f Geometry::ca() const { return ca_; }
|
||||||
float Geometry::coefDiffuse() const { return kd; }
|
float Geometry::kd() const { return kd_; }
|
||||||
float Geometry::coefSpecular() const { return ks; }
|
float Geometry::ks() const { return ks_; }
|
||||||
float Geometry::coefAmbient() const { return ka; }
|
float Geometry::ka() const { return ka_; }
|
||||||
float Geometry::getPhong() const { return phong; }
|
float Geometry::phong() const { return phong_; }
|
||||||
Geometry::Type Geometry::getType() const { return type; }
|
Geometry::Type Geometry::type() const { return type_; }
|
||||||
|
|
||||||
void Geometry::setTransform(const Matrix4f &transform) {
|
void Geometry::setTransform(const Matrix4f &transform) {
|
||||||
this->transform = transform;
|
this->transform = transform;
|
||||||
|
@ -36,7 +36,7 @@ Optional<float> Sphere::intersect(const Ray &r) const {
|
||||||
return Optional<float>::nullopt;
|
return Optional<float>::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3f Sphere::getNormal(const Vector3f &p) const {
|
Vector3f Sphere::normal(const Vector3f &p) const {
|
||||||
return (p - center).normalized();
|
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 {
|
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)
|
if (abs(denom) < 1e-6f)
|
||||||
return Optional<float>::nullopt;
|
return Optional<float>::nullopt;
|
||||||
|
|
||||||
float t = -normal.dot(r.getOrigin() - p1) / denom;
|
float t = -normal_.dot(r.getOrigin() - p1) / denom;
|
||||||
if (t <= 0)
|
if (t <= 0)
|
||||||
return Optional<float>::nullopt;
|
return Optional<float>::nullopt;
|
||||||
|
|
||||||
Vector3f p = r.getOrigin() + t * r.getDirection();
|
Vector3f p = r.getOrigin() + t * r.getDirection();
|
||||||
|
|
||||||
return isInRectangle(p, p1, p2, p3, p4, normal) ? Optional<float>(t)
|
return isInRectangle(p, p1, p2, p3, p4, normal_) ? Optional<float>(t)
|
||||||
: Optional<float>::nullopt;
|
: 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 ~Geometry() = default;
|
||||||
virtual Optional<float> intersect(const Ray &) const = 0;
|
virtual Optional<float> intersect(const Ray &) const = 0;
|
||||||
virtual Vector3f getNormal(const Vector3f &) const = 0;
|
virtual Vector3f normal(const Vector3f &) const = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Geometry(Type type, float ka, float kd, float ks, const Vector3f &ca,
|
Geometry(Type type, float ka, float kd, float ks, const Vector3f &ca,
|
||||||
const Vector3f &cd, const Vector3f &cs, float pc)
|
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;
|
Type type_;
|
||||||
float ka, kd, ks; // coefficients for ambient, diffuse and specular
|
float ka_, kd_, ks_; // coefficients for ambient, diffuse and specular
|
||||||
Vector3f ca, cd, cs; // ambient, diffuse and specular reflection color
|
Vector3f ca_, cd_, cs_; // ambient, diffuse and specular reflection color
|
||||||
float phong; // phone coefficient, for `pc`
|
float phong_; // phone coefficient, for `pc`
|
||||||
Matrix4f transform = Matrix4f::Identity();
|
Matrix4f transform = Matrix4f::Identity();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Vector3f diffuse() const;
|
Vector3f cd() const;
|
||||||
Vector3f specular() const;
|
Vector3f cs() const;
|
||||||
Vector3f ambient() const;
|
Vector3f ca() const;
|
||||||
float coefDiffuse() const;
|
float kd() const;
|
||||||
float coefSpecular() const;
|
float ks() const;
|
||||||
float coefAmbient() const;
|
float ka() const;
|
||||||
float getPhong() const;
|
float phong() const;
|
||||||
|
Type type() const;
|
||||||
void setTransform(const Matrix4f &);
|
void setTransform(const Matrix4f &);
|
||||||
Type getType() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Sphere : public Geometry {
|
class Sphere : public Geometry {
|
||||||
|
@ -52,7 +53,7 @@ public:
|
||||||
center(center) {}
|
center(center) {}
|
||||||
|
|
||||||
Optional<float> intersect(const Ray &) const override;
|
Optional<float> intersect(const Ray &) const override;
|
||||||
Vector3f getNormal(const Vector3f &) const override;
|
Vector3f normal(const Vector3f &) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float radius;
|
float radius;
|
||||||
|
@ -65,14 +66,14 @@ public:
|
||||||
const Vector3f &cs, float pc, const Vector3f &p1,
|
const Vector3f &cs, float pc, const Vector3f &p1,
|
||||||
const Vector3f &p2, const Vector3f &p3, const Vector3f &p4)
|
const Vector3f &p2, const Vector3f &p3, const Vector3f &p4)
|
||||||
: Geometry(Type::RECTANGLE, ka, kd, ks, ca, cd, cs, pc), p1(p1), p2(p2),
|
: 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;
|
Optional<float> intersect(const Ray &) const override;
|
||||||
Vector3f getNormal(const Vector3f &) const override;
|
Vector3f normal(const Vector3f &) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector3f p1, p2, p3, p4;
|
Vector3f p1, p2, p3, p4;
|
||||||
Vector3f normal;
|
Vector3f normal_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !GEOMETRY_H_
|
#endif // !GEOMETRY_H_
|
||||||
|
|
|
@ -16,4 +16,4 @@ Vector3f HitRecord::viewDirection() const {
|
||||||
|
|
||||||
Vector3f HitRecord::normal() const { return n; }
|
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)
|
for (auto g : geometries)
|
||||||
if (g != geometry && g->intersect(shadowRay).hasValue() &&
|
if (g != geometry && g->intersect(shadowRay).hasValue() &&
|
||||||
g->getType() == Geometry::Type::SPHERE)
|
g->type() == Geometry::Type::SPHERE)
|
||||||
return Vector3f::Zero();
|
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() *
|
diffuse.array() *
|
||||||
std::max(0.0f, hit.normal().dot(rayDirection));
|
std::max(0.0f, hit.normal().dot(rayDirection));
|
||||||
|
|
||||||
Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized();
|
Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized();
|
||||||
Vector3f specular_ =
|
Vector3f specular_ =
|
||||||
geometry->coefSpecular() * geometry->specular().array() *
|
geometry->ks() * geometry->cs().array() * specular.array() *
|
||||||
specular.array() *
|
pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->phong());
|
||||||
pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->getPhong());
|
|
||||||
|
|
||||||
return specular_ + ambient_ + diffuse_;
|
return specular_ + ambient_ + diffuse_;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue