Added arbitrary sphere code WIP
This doesn't actually seem to work yet
This commit is contained in:
parent
2961aabea3
commit
7c83ece187
1 changed files with 37 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
#include "common/ray.h"
|
||||
#include "primitive/sphere.h"
|
||||
#include <cmath>
|
||||
|
||||
// Constructor /////////////////////////////////////////////////////////////////
|
||||
|
||||
|
@ -14,16 +15,50 @@ bool Sphere::intersect(Ray &ray) const {
|
|||
// IMPLEMENT ME!
|
||||
|
||||
// Determine whether the ray intersects the sphere
|
||||
float A = pow(ray.direction[Vector3d::Dimension::X], 2) +
|
||||
pow(ray.direction[Vector3d::Dimension::Y], 2) +
|
||||
pow(ray.direction[Vector3d::Dimension::Z], 2);
|
||||
float B = 2 * (
|
||||
ray.direction[Vector3d::Dimension::X] * ray.origin[Vector3d::Dimension::X] +
|
||||
ray.direction[Vector3d::Dimension::Y] * ray.origin[Vector3d::Dimension::Y] +
|
||||
ray.direction[Vector3d::Dimension::Z] * ray.origin[Vector3d::Dimension::Z]
|
||||
);
|
||||
float C = pow(ray.origin[Vector3d::Dimension::X], 2) +
|
||||
pow(ray.origin[Vector3d::Dimension::Y], 2) +
|
||||
pow(ray.origin[Vector3d::Dimension::Z], 2) - this->radius;
|
||||
|
||||
float t = INFINITY;
|
||||
|
||||
if (B * B - 4 * A * C < 0) {
|
||||
return false;
|
||||
} else if (B * B - 4 + A * C == 0) {
|
||||
t = -B / (2 * A);
|
||||
} else {
|
||||
float q {static_cast<float>([A, B, C](){
|
||||
if (B < 0) {
|
||||
return -0.5f * (B - sqrt(B * B - 4 * A * C));
|
||||
} else {
|
||||
return -0.5f * (B + sqrt(B * B - 4 * A * C));
|
||||
}
|
||||
}())};
|
||||
float t0 = q / A;
|
||||
float t1 = C / q;
|
||||
t = std::min(t0, t1);
|
||||
}
|
||||
// Test whether this is the foremost primitive in front of the camera
|
||||
|
||||
if (t >= ray.length) {
|
||||
return false;
|
||||
}
|
||||
// (Optional for now) Calculate the normal
|
||||
|
||||
// (Optional for now) Calculate the surface position
|
||||
|
||||
// Set the new length and the current primitive
|
||||
|
||||
return false;
|
||||
// Set the new length and the current primitive
|
||||
ray.length = t;
|
||||
ray.primitive = this;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Bounding box ////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in a new issue