oop-labs-collection/labs/3/renderSuite/Sphere.java

44 lines
983 B
Java
Raw Permalink Normal View History

2023-04-27 14:23:46 +03:00
package renderSuite;
import renderSuite.Vec3;
import renderSuite.Util;
import renderSuite.Ray;
public class Sphere extends Surface {
2023-04-27 14:23:46 +03:00
double r;
public Sphere(Vec3 position, Vec3 color, double radius) {
this.p = position;
this.c = color;
this.r = radius;
}
public void main() {
}
public boolean intersect(Ray r) {
// OLD
//double dist = Util.d(ray_position, position);
//return (Util.d(ray_position, position) <= radius);
// using RTX
Vec3 l = new Vec3(this.p.x - r.p.x,
this.p.y - r.p.y,
this.p.z - r.p.z);
Vec3 nl = l.get_norm();
double cosine = Util.dot(r.f, nl);
// >90 degrees = no intersection
if (cosine < 0) {
return false;
}
double tc = l.len() * cosine;
double d = Math.sqrt( l.len()*l.len() - tc*tc );
return (d < this.r);
}
}