SPHERICAL ENVIRONMENT MAPPING SHADER - MATH FORMULA EXPLANATION

Started by
3 comments, last by home3d2001 9 years, 7 months ago

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
http://www.clicktorelease.com/blog/c...mapping-shader

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

Advertisement
I haven't read it carefully, but there seems to be some explanation there: http://web.cse.ohio-state.edu/~whmin/courses/cse5542-2013-spring/17-env.pdf
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

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

BuckEye, Alvaro, and Matias:

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

Cheers,

-A

This topic is closed to new replies.

Advertisement