Conversion from Cartesian to spherical coordinate for vectors - ray tracing application

1k Views Asked by At

I'm implementing a ray tracer that support physically based rendering, so is based on various BRDF models. At the moment I'm focused on Oren-Nayar and Torrance-Sparrow model. Each one of these is based on spherical coordinates used to express incident $w_i$ and outgoing $w_o$ light direction vectors.

My question is: which is the way to correctly convert $w_i$ and $w_o$ from Cartesian coordinates to spherical coordinates when the vectors to be converted are not centered in the origin of the Cartesian coordinate system (but have their tail connected to the point of intersection of the ray with the current object)?

I'm applying the standard formula reported here https://en.wikipedia.org/wiki/Spherical_coordinate_system#Coordinate_system_conversions

$\theta = \arccos\left(\frac{z}{\sqrt{x^2 + y^2 + z^2}}\right) \\ \phi=\arctan\left(\frac{y}{x}\right)$

but I'm not sure I'm doing the right thing, because my vectors are not with tail at the origin of the Cartesian coordinate system, but are centered in the intersection point of the ray with the object.

I know this is a math question site, but for completeness I also link to my current implementation of BDRFS and spherical coordinates conversion:

https://github.com/chicio/Multispectral-Ray-tracing/tree/brdf/RayTracing/RayTracer/Objects/BRDF

https://github.com/chicio/Multispectral-Ray-tracing/blob/brdf/RayTracing/RayTracer/Math/Vector3D.cpp

Can anyone help me giving an explanation of the correct way to convert the $w_i$ and $w_o$ vector from Cartesian to spherical coordinates?

1

There are 1 best solutions below

2
On

If the vector to be converted has the tail at the coordinates $t = (t_x, t_y, t_z)$, then just translate the usual formulae by $t$. Explicitly (and using the same conventions as the ones on Wikipedia), this means

$r = \sqrt {(x-t_x)^2 + (y-t_y)^2 + (z-t_z)^2} \\ \theta = \arccos \left( \dfrac {z-t_z} {r} \right) \\ \varphi = \arctan \left( \dfrac {y-t_y} {x-t_x} \right) .$