<p>I'm not entirely sure if I understood the question, but it seems like you want to implement environment map illumination in your ray tracer (you seem to be thinking in real-time, though). I will approach the general solution for offline renderers without assuming anything about the scene except the environment sphere.</p>

<p>This is generally done by considering your environment as a light source. Let's say that you use a sphere with a radius big enough to contain your scene. Then, you sample it as you would sample an area light source. You can think of this sphere as a <strong>textured light source</strong>.</p>

<p>The contribution of the direct lighting on the radiance leaving a point $x$ on the direction $\Theta$ would be:
$$
L_{\text{direct}}(x \rightarrow \Theta) = L_{\text{lights}}(x \rightarrow \Theta) + L_{\text{env}}(x \rightarrow \Theta)
$$</p>

<p>Where the environment map contribution is:</p>

<p>$$
L_{\text{env}}(x \rightarrow \Theta) = {\int_{\Omega_x} L_{\text{tex}}}(x \leftarrow \Psi)f_r(x, \Theta \leftrightarrow \Psi)V(x, \Psi) \cos(\Psi, N_x)\, d\omega_{\Psi} \qquad (1)
$$</p>

<p>Note that, here, $\Psi$ are directions to the sphere, $V(x, \Psi) = 1$ if and only if the sphere is visible from that direction and $V(x, \Psi) = 0$ otherwise, $f_r$ is the BRDF and $L_{\text{tex}}$ is the color on the texture. </p>

<p>Directions $\Psi$ can be generated with a parameterization of the sphere. If you want something simple, then this is enough, <strong>just treat it as a textured area light and emit whatever color is on the texture on the sampled point</strong>. If you want something more elaborate, consider the rest of the text.</p>

<p>With this said, environment mapping can be inefficient if not implemented with care. </p>

<p>Also, I am assuming you only implemented area lights that emit uniformly, so it was okay to sample its area uniformly. However, it is clear that the sphere is a light source where some parts contribute more than others since the colors vary on the texture. This, combined with the fact that the sphere occupies the entire solid angle around a point (instead of the hemisphere that you usually sample), skyrockets the variance of the estimator of $(1)$. The visibility term $V(x, \Psi)$ further complicates things regarding variance, depending on your scene (if it very exposed or not).</p>

<p>In order to sample and estimate $(1)$, you have to consider:</p>

<p><strong>Parameterization of the sphere</strong>: You need to decide how to parameterize your sphere. You need to have a space where you can generate random points and map to a point on the sphere to get a direction $\Psi$ and the color from the texture. You can just use typical the latitude-longitude parameterization. </p>

<p>For more elaborate, better distributed parameterizations: <a href="http://gamedevs.org/uploads/realistic-hardware-accelerated-shading-and-lighting.pdf" rel="nofollow noreferrer">paraboloid parameterization</a> [page 4]; <a href="https://pdfs.semanticscholar.org/4322/6a3916a85025acbb3a58c17f6dc0756b35ac.pdf" rel="nofollow noreferrer">concentric-map parameterization</a>.</p>

<p><strong>PDF $p(\Psi)$ used for sampling the sphere</strong>: It is possible to reduce the variance that I mentioned earlier. You can importance sample only using the term $\cos(\Psi, N_x)$ (i.e. cosine lobe sampling), but even so it is possible to do better than that. If the BRDF $f_r$ is simple, you can sample the product of the BRDF with $\cos(\Psi, N_x)$.</p>

<p>There are other methods. In <a href="https://www.uni-kl.de/AG-Heinrich/EIbHDRI.pdf" rel="nofollow noreferrer">[Kollig, Keller; 03]</a> a PDF is built from the 2D array of the texture itself. This way, it is possible to get more samples from directions from where the map contributes more. A PDF from the product of this with the BRDF would yield the best results considering a compromise between the sampling cost and actual variance reduction.</p>

<p>For a more elaborate and adaptive solution, check <a href="http://graphics.ucsd.edu/~sagarwal/structured.pdf" rel="nofollow noreferrer">[Agarwal et. al, 03]</a>.</p>

CC BY-SA 3.0
2017-01-01T18:24:19.897Z
Great Answer but wouldn't having a hemisphere around the entire scene centered at the camera that has a sky texture (created with a sky rendering model) on the inside do the trick fine? I mean I will of course represent the sun as a disk light and apply some sort of physically based color transform on the disk light (will have to check a siggraph paper I read for the actual name). What are the advantages of using the method I described over the method you said?
Arjan Singh
CC BY-SA 3.0
2017-01-01T18:35:22.263Z
Using the method I described, the scene will appear illuminated by the whole environment. Think of the indirect illumination and bounces between the scene geometry too. What you are describing is a model with a texture, and no emission. If you only set emission on the disk and illuminate the scene just with that, it is the same as having an area light on that solid angle of the disk, you are not emitting the color of the whole sky onto the objects and making that radiance bounce between them as indirect illumination.
Samu
CC BY-SA 3.0
2017-01-01T18:42:18.053Z
Of course, using the sun light source separated from the sphere would work if you wanted to simulate the whole atmosphere physically, with scattering. But it would be way more involved than my answer and I doubt that is what you want. Generally, we pick an image of the sky and emit that image onto the whole scene as light (the process I described).
Samu
CC BY-SA 3.0
2017-01-01T17:14:19.177Z
2017-01-01T17:14:19.177Z
Samu
<p>I have implemented a sky rendering model in my ray tracer which saves the color of the sky to an image. I was wondering how I can render the sky dome and texture map the image on to it.</p>

<p>Do I render the sphere at the center of the environment and add the image to the inside of the sphere? How would I add a texture to the inside of a sphere as opposed to the outside in code? Do I just reverse the normals?</p>

CC BY-SA 3.0
2017-01-01T14:32:07.170Z
You mean accurate atmospheric scattering? Why not render vertices which form skydome which is centered at camera position and color it in fragment directly?
mdkdy
<raytracing><texture>
Skybox in a Ray Tracer
Arjan Singh
# Skybox in a Ray Tracer

(Asked by: username_0 on 2017-01-01)

I have implemented a sky rendering model in my ray tracer which saves the color of the sky to an image. I was wondering how I can render the sky dome and texture map the image on to it.

Do I render the sphere at the center of the environment and add the image to the inside of the sphere? How would I add a texture to the inside of a sphere as opposed to the outside in code? Do I just reverse the normals?

**Tags:** raytracing, texture

> **Comments:**
> **username_1**: You mean accurate atmospheric scattering? Why not render vertices which form skydome which is centered at camera position and color it in fragment directly?  

---

## 1 answers

### Answer by username_2 on 2017-01-01. Score: 4 | Accepted answer

I'm not entirely sure if I understood the question, but it seems like you want to implement environment map illumination in your ray tracer (you seem to be thinking in real-time, though). I will approach the general solution for offline renderers without assuming anything about the scene except the environment sphere.

This is generally done by considering your environment as a light source. Let's say that you use a sphere with a radius big enough to contain your scene. Then, you sample it as you would sample an area light source. You can think of this sphere as a **textured light source**.

The contribution of the direct lighting on the radiance leaving a point $x$ on the direction $\Theta$ would be:
$$
L\_{\text{direct}}(x \rightarrow \Theta) = L\_{\text{lights}}(x \rightarrow \Theta) + L\_{\text{env}}(x \rightarrow \Theta)
$$

Where the environment map contribution is:

$$
L\_{\text{env}}(x \rightarrow \Theta) = {\int\_{\Omega\_x} L\_{\text{tex}}}(x \leftarrow \Psi)f\_r(x, \Theta \leftrightarrow \Psi)V(x, \Psi) \cos(\Psi, N\_x)\, d\omega\_{\Psi} \qquad (1)
$$

Note that, here, $\Psi$ are directions to the sphere, $V(x, \Psi) = 1$ if and only if the sphere is visible from that direction and $V(x, \Psi) = 0$ otherwise, $f\_r$ is the BRDF and $L\_{\text{tex}}$ is the color on the texture.

Directions $\Psi$ can be generated with a parameterization of the sphere. If you want something simple, then this is enough, **just treat it as a textured area light and emit whatever color is on the texture on the sampled point**. If you want something more elaborate, consider the rest of the text.

With this said, environment mapping can be inefficient if not implemented with care.

Also, I am assuming you only implemented area lights that emit uniformly, so it was okay to sample its area uniformly. However, it is clear that the sphere is a light source where some parts contribute more than others since the colors vary on the texture. This, combined with the fact that the sphere occupies the entire solid angle around a point (instead of the hemisphere that you usually sample), skyrockets the variance of the estimator of $(1)$. The visibility term $V(x, \Psi)$ further complicates things regarding variance, depending on your scene (if it very exposed or not).

In order to sample and estimate $(1)$, you have to consider:

**Parameterization of the sphere**: You need to decide how to parameterize your sphere. You need to have a space where you can generate random points and map to a point on the sphere to get a direction $\Psi$ and the color from the texture. You can just use typical the latitude-longitude parameterization.

For more elaborate, better distributed parameterizations: [paraboloid parameterization](http://gamedevs.org/uploads/realistic-hardware-accelerated-shading-and-lighting.pdf) [page 4]; [concentric-map parameterization](https://pdfs.semanticscholar.org/4322/6a3916a85025acbb3a58c17f6dc0756b35ac.pdf).

**PDF $p(\Psi)$ used for sampling the sphere**: It is possible to reduce the variance that I mentioned earlier. You can importance sample only using the term $\cos(\Psi, N\_x)$ (i.e. cosine lobe sampling), but even so it is possible to do better than that. If the BRDF $f\_r$ is simple, you can sample the product of the BRDF with $\cos(\Psi, N\_x)$.

There are other methods. In [[Kollig, Keller; 03]](https://www.uni-kl.de/AG-Heinrich/EIbHDRI.pdf) a PDF is built from the 2D array of the texture itself. This way, it is possible to get more samples from directions from where the map contributes more. A PDF from the product of this with the BRDF would yield the best results considering a compromise between the sampling cost and actual variance reduction.

For a more elaborate and adaptive solution, check [[Agarwal et. al, 03]](http://graphics.ucsd.edu/~sagarwal/structured.pdf).

> **Comments:**
> **username_0**: Great Answer but wouldn't having a hemisphere around the entire scene centered at the camera that has a sky texture (created with a sky rendering model) on the inside do the trick fine? I mean I will of course represent the sun as a disk light and apply some sort of physically based color transform on the disk light (will have to check a siggraph paper I read for the actual name). What are the advantages of using the method I described over the method you said?  
> **username_2**: Using the method I described, the scene will appear illuminated by the whole environment. Think of the indirect illumination and bounces between the scene geometry too. What you are describing is a model with a texture, and no emission. If you only set emission on the disk and illuminate the scene just with that, it is the same as having an area light on that solid angle of the disk, you are not emitting the color of the whole sky onto the objects and making that radiance bounce between them as indirect illumination.  
> **username_2**: Of course, using the sun light source separated from the sphere would work if you wanted to simulate the whole atmosphere physically, with scattering. But it would be way more involved than my answer and I doubt that is what you want. Generally, we pick an image of the sky and emit that image onto the whole scene as light (the process I described).  

---

