<p>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!</p>

CC BY-SA 4.0
2024-04-02T01:01:07.147Z
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](https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates). 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$.
Hubble
CC BY-SA 4.0
2024-04-02T05:17:16.537Z
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: https://registry.khronos.org/OpenGL-Refpages/gl4/html/texture.xhtml . Here is also a tutorial about environment mapping: https://learnopengl.com/Advanced-OpenGL/Cubemaps
Thomas
CC BY-SA 4.0
2024-04-02T09:18:17.510Z
Thank you! I figured it out.
Derek Kwon
<c++><vectors><mapping>
Environment map: inverting (phi, theta) to (x,y,z) mapping
Derek Kwon
# Environment map: inverting (phi, theta) to (x,y,z) mapping

(Asked by: username_0 on 2024-04-01)

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!

**Tags:** c++, vectors, mapping

> **Comments:**
> **username_1**: 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](https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates). 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$.  
> **username_2**: 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: https://registry.khronos.org/OpenGL-Refpages/gl4/html/texture.xhtml . Here is also a tutorial about environment mapping: https://learnopengl.com/Advanced-OpenGL/Cubemaps  
> **username_0**: Thank you! I figured it out.  

---

## 0 answers

