# Environment map: inverting (phi, theta) to (x,y,z) mapping

I'm currently working on a ray tracer in C++ as an assignment. This ray tracer needs to take into account environment maps, which I also need to implement a part of. From my understanding of an environment map, I need to follow this process: Given a 3d vector that represents the direction of the ray, I need to find a mapping from x,y,z to phi,theta coordinates, then use that mapping to get the color on the environment map. The (phi, theta) to (x, y, z) mapping is already given to me, but I need to invert it using acos and atan2 (C++ for arccos and arctan in range -pi to pi). I'm lost on what inverting the mapping means or how to do it. I'd really appreciate it if anyone could outline the process of inverting and describe the steps along the way. Thank you all!

• Inverting the mapping just means finding the spherical coordinates $(\phi,\theta)$ that produced the Euclidean coordinates $(x,y,z)$. The formula can be found here. You can derive those yourself from the definition of your forward map. For instance, since $x = r\sin\theta\cos\phi$ and $y = \sin\theta\sin\phi$, then for $x>0$, $\tan\phi = y/x$. Commented Apr 2 at 1:01
• In OpenGL you simply use Cubemap textures. There you use the ray direction (x, y, z) and get the color gvec4 texture(gsamplerCube sampler, vec3 P, [float bias]);. Take a look at this: registry.khronos.org/OpenGL-Refpages/gl4/html/texture.xhtml . Here is also a tutorial about environment mapping: learnopengl.com/Advanced-OpenGL/Cubemaps Commented Apr 2 at 5:17
• Thank you! I figured it out. Commented Apr 2 at 9:18