mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 15:31:59 +00:00
finish ray casting
This commit is contained in:
parent
bd99b001bb
commit
0633159427
2 changed files with 17 additions and 6 deletions
|
@ -15,18 +15,29 @@ bool Sphere::intersect(const Ray &r) const {
|
||||||
return b * b - 4 * a * c >= 0;
|
return b * b - 4 * a * c >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isInRectangle(const Vector3f &p, const Vector3f &a, const Vector3f &b,
|
||||||
|
const Vector3f &c, const Vector3f &d, const Vector3f &n) {
|
||||||
|
float s1 = (b - a).cross(p - a).dot(n);
|
||||||
|
float s2 = (c - b).cross(p - b).dot(n);
|
||||||
|
float s3 = (d - c).cross(p - c).dot(n);
|
||||||
|
float s4 = (a - d).cross(p - d).dot(n);
|
||||||
|
|
||||||
|
return (s1 >= 0 && s2 >= 0 && s3 >= 0 && s4 >= 0) ||
|
||||||
|
(s1 <= 0 && s2 <= 0 && s3 <= 0 && s4 <= 0);
|
||||||
|
}
|
||||||
|
|
||||||
bool Rectangle::intersect(const Ray &r) const {
|
bool Rectangle::intersect(const Ray &r) const {
|
||||||
Vector3f normal = (p2 - p1).cross(p3 - p1).normalized();
|
Vector3f normal = (p2 - p1).cross(p3 - p1).normalized();
|
||||||
float d = normal.dot(p1);
|
|
||||||
|
|
||||||
float denom = normal.dot(r.getDirection());
|
float denom = normal.dot(r.getDirection());
|
||||||
if (abs(denom) < 1e-6f)
|
if (abs(denom) < 1e-6f)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
float t = (normal.dot(r.getOrigin() - p1) - d) / denom;
|
float t = -normal.dot(r.getOrigin() - p1) / denom;
|
||||||
if (t <= 0)
|
if (t <= 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Vector3f p = r.getOrigin() + t * r.getDirection();
|
Vector3f p = r.getOrigin() + t * r.getDirection();
|
||||||
return true;
|
|
||||||
|
return isInRectangle(p, p1, p2, p3, p4, normal);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,12 +31,12 @@ void RayTracer::render(Scene *scene) {
|
||||||
Vector3f up = scene->getUpVector();
|
Vector3f up = scene->getUpVector();
|
||||||
float vpHeight = 2 * tan(fov / 180 * M_PI / 2) * lookAt.norm();
|
float vpHeight = 2 * tan(fov / 180 * M_PI / 2) * lookAt.norm();
|
||||||
float vpWidth = vpHeight * width / height;
|
float vpWidth = vpHeight * width / height;
|
||||||
Vector3f vpU = Vector3f(-vpWidth, 0, 0);
|
Vector3f vpU = Vector3f(vpWidth, 0, 0);
|
||||||
Vector3f vpV = Vector3f(0, vpHeight, 0);
|
Vector3f vpV = Vector3f(0, -vpHeight, 0);
|
||||||
Vector3f du = vpU / width;
|
Vector3f du = vpU / width;
|
||||||
Vector3f dv = vpV / height;
|
Vector3f dv = vpV / height;
|
||||||
|
|
||||||
Vector3f vpUpperLeft = cameraPos - lookAt - vpU / 2.0 - vpV / 2.0;
|
Vector3f vpUpperLeft = cameraPos + lookAt - vpU / 2.0 - vpV / 2.0;
|
||||||
Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
|
Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
|
||||||
|
|
||||||
Buffer buffer(width * height * 3);
|
Buffer buffer(width * height * 3);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue