# SPHERICAL ENVIRONMENT MAPPING SHADER - MATH FORMULA EXPLANATION

This topic is 1555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there,

I am trying to understand the Math formula used to access the sphere map texture. Please see following websites:

http://www.reindelsoftware.com/Docum...g/Mapping.html

Here are some questions I have:

Q1- why are we adding 1 to Rz?
Q2- why is the magnitude multiplied by 2?
Q3- why are we adding 0.5?

Thank you again in advance and I appreciate it if someone could explain this in details.

Ali

##### Share on other sites
The reflection vector is in the range [-1;1]
Multiplying the magnitude then dividing ensures it's now in the rangerange [-0.5; 0.5]
Adding 0.5 offsets to the range [0;1] which is what we need for sampling the texture.
We add 1 to the z component because otherwise the back and front sides would map to the same uvs, so by shifting the z to the [0;2] range, the back side gets mapped close to the centre of the sphere map while the front side is mapped further away from it

Cheers

##### Share on other sites

Here are some questions I have:

Q1- why are we adding 1 to Rz?
Q2- why is the magnitude multiplied by 2?
Q3- why are we adding 0.5?

Q1: On the last page of the PPT presentation that Alvaro links, you should see "eye-space sphere normal n = (rx, ry, rz) + (0, 0, 1)" with the notation for (0,0,1) "direction to eye in local space." Thus, n (eye space) = (rx, ry, rz+1) by vector addition.

Q2 and Q3:

First, the variable m is just a convenient factor to calculate before u & v are calc'd as it applies to both equations - i.e., calculate it once rather than twice.

Actually, it's easier to understand if you work backwards from the tex coord equations:

u = Rx/m + 1/2

v = Ry/m + 1/2

Texture coordinates run from 0 to 1 over the width and height of the texture (sphere map). The sphere map is an unusual beast which maps sphere normalized coordinates to colors. Without getting into the actual generation of the sphere map (that's left as an exercise for the student), the first parameter to calculate is the normalized sphere coordinates. The sphere normal vector "points" to a position on the sphere which is the desired color.

CXnormalized = Rx/|n|

CYnormalized = Ry/|n|

Due to the way sphere map textures are generated, Rx / |n| maps to the range -1 to +1. Dividing by 2 changes that range to -0.5 to 0.5 from map bottom-left to top-right. Tex coords need to be in the range 0 to 1, which is done by adding 0.5: e.g.,  (-0.5 + 0.5, 0.5 + 0.5) == (0, 1).

So:

u = Rx / (2 * |n| ) + 1/2

v = Ry / (2 * |n|) + 1/2

No need to calculate 2 * |n| twice, so define m = 2 * sqrt[ rx2 + ry2 + (rz+1)2 ]

EDIT: had |R| rather than |n| in several places.

Edited by Buckeye

##### Share on other sites

BuckEye, Alvaro, and Matias:

Thank you so much. Now this makes complete sense. I really appreciate it.

Cheers,

-A

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633723
• Total Posts
3013546
×