diff --git a/pyrt/geometry/sphere.py b/pyrt/geometry/sphere.py index 290d038..707204a 100644 --- a/pyrt/geometry/sphere.py +++ b/pyrt/geometry/sphere.py @@ -5,10 +5,10 @@ """ from ..geometry import Shape -from ..math import Ray, HitRecord, Vec3, dot3, G_EPSILON +from ..math import Ray, HitRecord, Vec2, Vec3, dot3, G_EPSILON from ..material import Material, PhongMaterial from .bbox import BBox -from math import sqrt, pi +from math import sqrt, pi, asin, atan2 class Sphere(Shape): @@ -23,6 +23,24 @@ def __init__(self, center: Vec3, radius: float, material: Material = PhongMateri self.material = material + def calcTexcoord(self, p: Vec3) -> Vec2: + """ + Returns texture-coordinate at cartesian position p + :param p: + :return: returns + """ + + # change coordinate system's origin to sphere's center + p_central = p - self.center + + # sphere radius + r = p_central.length() + + u = 1.0 - (atan2(p_central.z, p_central.x) + pi) / (2.0 * pi) + v = (asin(p_central.y / r) + pi / 2) / pi + return Vec2(u, v) + + def hit(self, ray: Ray, hitrecord: HitRecord) -> bool: """ Hit ray with sphere.