Search code examples
c++mathvectorraytracingcoordinate-systems

Transform world space vectors to local space


I'm writing a ray tracing program that intersect sphere with area light source. I have that vectors

  • wo (w - outgoing)
  • wi (w - incoming)
  • n (surface normal)

enter image description here

How do i transfer that vectors from world coordinate system to local coordinate system. I searching an c++ algorithm, transformation for that vectors are lie in the samehemisphere. Thank you.

Note: For example normal vector coordinates in that local coordinate system are: n2 = Vector(x=0, y=1, z=0)

Edit: I'm searching "wo1", "wi1" and "n2" local xyz coordinate values.


Solution

  • You project your vectors that lie in the world coordinate system to the vectors that form the basis of your local coordinate system.

    For example if your local coordinate system basis is a triplet of vectors (e.g., Lx, Ly, Lz) that span your local vector space. wo coordinates in the local system will be:

    wo_local_x = wo . Lx;
    wo_local_y = wo . Ly;
    wo_local_z = wo . Lz;
    

    where . denotes the dot product.