Skip to content

Commit

Permalink
impl calcTextcoord() method for Sphere class
Browse files Browse the repository at this point in the history
  • Loading branch information
via8 committed Feb 2, 2021
1 parent abf01e2 commit 7c6e536
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pyrt/geometry/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down

0 comments on commit 7c6e536

Please sign in to comment.