mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
reformat ray and hitrecord
This commit is contained in:
parent
08cbf7733f
commit
9e9d7351bd
6 changed files with 25 additions and 25 deletions
|
@ -17,9 +17,9 @@ void Geometry::setTransform(const Matrix4f &transform) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<float> Sphere::intersect(const Ray &r) const {
|
Optional<float> Sphere::intersect(const Ray &r) const {
|
||||||
Vector3f originCenter = r.getOrigin() - center;
|
Vector3f originCenter = r.origin() - center;
|
||||||
float a = r.getDirection().dot(r.getDirection());
|
float a = r.direction().dot(r.direction());
|
||||||
float b = 2.0f * originCenter.dot(r.getDirection());
|
float b = 2.0f * originCenter.dot(r.direction());
|
||||||
float c = originCenter.dot(originCenter) - radius * radius;
|
float c = originCenter.dot(originCenter) - radius * radius;
|
||||||
|
|
||||||
float delta = b * b - 4 * a * c;
|
float delta = b * b - 4 * a * c;
|
||||||
|
@ -52,15 +52,15 @@ 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.direction());
|
||||||
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.origin() - 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.origin() + t * r.direction();
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
@ -4,16 +4,16 @@ bool HitRecord::operator<(const HitRecord &other) const {
|
||||||
return this->t > other.t; // to get the nearest t
|
return this->t > other.t; // to get the nearest t
|
||||||
}
|
}
|
||||||
|
|
||||||
Geometry *HitRecord::geometry() const { return g; }
|
Geometry *HitRecord::geometry() const { return geometry_; }
|
||||||
|
|
||||||
Vector3f HitRecord::getPoint() const {
|
Vector3f HitRecord::point() const {
|
||||||
return r.getOrigin() + t * r.getDirection();
|
return ray_.origin() + t * ray_.direction();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3f HitRecord::viewDirection() const {
|
Vector3f HitRecord::viewDirection() const {
|
||||||
return -r.getDirection().normalized();
|
return -ray_.direction().normalized();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3f HitRecord::normal() const { return n; }
|
Vector3f HitRecord::normal() const { return normal_; }
|
||||||
|
|
||||||
void HitRecord::calcNormal() { n = g->normal(getPoint()); }
|
void HitRecord::calcNormal() { normal_ = geometry_->normal(point()); }
|
||||||
|
|
|
@ -9,18 +9,18 @@ using Eigen::Vector3f;
|
||||||
|
|
||||||
class HitRecord {
|
class HitRecord {
|
||||||
public:
|
public:
|
||||||
HitRecord(float t, const Ray &r, Geometry *g) : t(t), r(r), g(g) {}
|
HitRecord(float t, const Ray &r, Geometry *g) : t(t), ray_(r), geometry_(g) {}
|
||||||
bool operator<(const HitRecord &) const;
|
bool operator<(const HitRecord &) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float t;
|
float t;
|
||||||
Ray r;
|
Ray ray_;
|
||||||
Vector3f n;
|
Vector3f normal_;
|
||||||
Geometry *g;
|
Geometry *geometry_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Geometry *geometry() const;
|
Geometry *geometry() const;
|
||||||
Vector3f getPoint() const;
|
Vector3f point() const;
|
||||||
Vector3f viewDirection() const;
|
Vector3f viewDirection() const;
|
||||||
Vector3f normal() const;
|
Vector3f normal() const;
|
||||||
void calcNormal();
|
void calcNormal();
|
||||||
|
|
|
@ -20,7 +20,7 @@ bool Light::isUse() const { return use; }
|
||||||
|
|
||||||
Vector3f PointLight::illumination(const HitRecord &hit,
|
Vector3f PointLight::illumination(const HitRecord &hit,
|
||||||
const vector<Geometry *> &geometries) const {
|
const vector<Geometry *> &geometries) const {
|
||||||
Vector3f shadingPoint = hit.getPoint();
|
Vector3f shadingPoint = hit.point();
|
||||||
Vector3f rayDirection = (center - shadingPoint).normalized();
|
Vector3f rayDirection = (center - shadingPoint).normalized();
|
||||||
Geometry *geometry = hit.geometry();
|
Geometry *geometry = hit.geometry();
|
||||||
Ray shadowRay(shadingPoint, rayDirection);
|
Ray shadowRay(shadingPoint, rayDirection);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "Ray.h"
|
#include "Ray.h"
|
||||||
|
|
||||||
Vector3f Ray::getOrigin() const { return origin; }
|
Vector3f Ray::origin() const { return origin_; }
|
||||||
|
|
||||||
Vector3f Ray::getDirection() const { return direction; }
|
Vector3f Ray::direction() const { return direction_; }
|
||||||
|
|
10
src/Ray.h
10
src/Ray.h
|
@ -7,15 +7,15 @@ using Eigen::Vector3f;
|
||||||
|
|
||||||
class Ray {
|
class Ray {
|
||||||
public:
|
public:
|
||||||
Ray(const Vector3f &o, const Vector3f &d) : origin(o), direction(d) {}
|
Ray(const Vector3f &o, const Vector3f &d) : origin_(o), direction_(d) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector3f origin;
|
Vector3f origin_;
|
||||||
Vector3f direction;
|
Vector3f direction_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Vector3f getOrigin() const;
|
Vector3f origin() const;
|
||||||
Vector3f getDirection() const;
|
Vector3f direction() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // !RAY_H_
|
#endif // !RAY_H_
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue