void Sphere::intersect(HitRecord& hit, const RenderContext& rc, const Ray& ray)const{ /*float a = dot(ray.d(), ray.d()); Vector v = ray.p()-p;/*Vector(ray.p().x(), ray.p().y(), ray.p().z());*/ /*float b = 2 * dot(ray.d(), v); float c = dot(v, v) - r2(); float disc = b * b - 4 * a * c; if (disc < 0.f){ hit.hit(std::numeric_limits<float>::infinity(), this, material); return; } float distSqrt = sqrtf(disc); float q; if (b < 0) q = (-b - distSqrt)/2.0f; else q = (-b + distSqrt)/2.0f; float t0 = q / a; float t1 = c / q; if (t0 > t1){ float temp = t0; t0 = t1; t1 = temp; } if (t1 < 0) hit.hit(std::numeric_limits<float>::infinity(), this, material); if (t0 < 0) hit.hit(t1, this, material); else hit.hit(t0, this, material);*/ Vector dist = ray.p() - p; float b = dot(dist, ray.d()); float c = dot(dist, dist) - r2(); float d = b*b - c; float t = d > 0 ? -b - sqrt(d) : std::numeric_limits<float>::infinity(); hit.hit(t, this, this->material); }
void Plane::intersect(HitRecord& hit, const RenderContext& rc, const Ray& ray)const{ //Vector p(ray.p().x(),ray.p().y(),ray.p().z()); //Vector o(point.x(),point.y(),point.z()); //float d = dot(o, norm); float num = dot(-norm, ray.p()-point); float denom = dot(norm, ray.d()); float t=num/denom; if(denom==0 || num==0 || t<0) hit.hit(std::numeric_limits<float>::infinity(), this, material); else hit.hit(num/denom, this, material); //float num = -(dot(norm,p)+d); //float denom = dot(norm, ray.d()); //if((denom==0 || num==0)) hit.hit(std::numeric_limits<float>::infinity(), this, material); //else hit.hit(num/denom, this, material); }
bool Box::intersect(HitRecord& hit, const RenderContext& context, const Ray& ray) const { bool hasHit = false; float txMin, txMax, tymMin, tymMax, tzmMin, tzmMax; if (ray.d.x() >= 0) { txMin = (mMin.x() - ray.o.x()) / ray.d.x(); txMax = (mMax.x() - ray.o.x()) / ray.d.x(); } else { txMin = (mMax.x() - ray.o.x()) / ray.d.x(); txMax = (mMin.x() - ray.o.x()) / ray.d.x(); } if (ray.d.y() >= 0) { tymMin = (mMin.y() - ray.o.y()) / ray.d.y(); tymMax = (mMax.y() - ray.o.y()) / ray.d.y(); } else { tymMin = (mMax.y() - ray.o.y()) / ray.d.y(); tymMax = (mMin.y() - ray.o.y()) / ray.d.y(); } if ((txMin > tymMax) || (tymMin > txMax)) return false; if (tymMin > txMin) txMin = tymMin; if (tymMax < txMax) txMax = tymMax; if (ray.d.z() >= 0) { tzmMin = (mMin.z() - ray.o.z()) / ray.d.z(); tzmMax = (mMax.z() - ray.o.z()) / ray.d.z(); } else { tzmMin = (mMax.z() - ray.o.z()) / ray.d.z(); tzmMax = (mMin.z() - ray.o.z()) / ray.d.z(); } if ( (txMin > tzmMax) || (tzmMin > txMax) ) return false; if (tzmMin > txMin) txMin = tzmMin; if (tzmMax < txMax) txMax = tzmMax; hasHit = (hit.hit(txMin, this, material)) || hasHit; hasHit = (hit.hit(txMax, this, material)) || hasHit; return hasHit; }
HitRecord BezierCurve::intersect(const Ray &ray, const double dist, const double time) const { HitRecord hit = HitRecord(dist); //These are the endpoints of the line segment double t = findIntersection(0.0, 1.0, ray); if (t != 0.0){ hit.hit(t, this, matl, ray, n); } return hit; }
bool Sphere::intersect(HitRecord& hit, const RenderContext& context, const Ray& ray) const { Vector o_minus_c = ray.o - mCenter; double a = dot(ray.d, ray.d); double b = 2.0 * dot(ray.d, o_minus_c); double c = dot(o_minus_c, o_minus_c) - (mRadius2); double disc = b*b - 4.0*a*c; //Does it intersect? if (disc > 0) { disc = sqrt(disc); double t = (-b - disc) / (2.0*a); //Early termination if (hit.hit(t, this, material)) return true; t = ( -b + disc) / (2.0*a); return (hit.hit(t, this, material)); } return false; }
bool Disk::hit(HitRecord& record, const RenderContext& context, const Ray& ray) const { bool hasHit = false; float t = dot(-normal, ray.origin() - center)/dot(normal, ray.direction()); Point temp_point = ray.point(t); Vector D = temp_point - center; if(dot(D, D) < (radius*radius)) { hasHit = record.hit(t, this, ray); } return hasHit; }