# Ray Tracing: Texture Map a Sphere

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

## Recommended Posts

I've been looking for this, does someone here know the equations to texture map a sphere, that has a polar north vector, and a rotation arround that vector?

Something along those lines at least, so I can extrapolate.

Thank you!

##### Share on other sites
http://www.flipcode.com/archives/Raytracing_Topics_Techniques-Part_6_Textures_Cameras_and_Speed.shtml

...but the code isn't very well kept, and the discription of the algorithm contradicts the code in places.

Any tips?

##### Share on other sites
Do you mean your sphere has a rotation attribute defined by an azimuth and inclination angles? If so, then it's relatively easy if you already know how to texture-map a sphere using spherical coordinates, you can just offset your UV angles according to your sphere's rotation. What format are your sphere textures in? The circular type or the rectangular ones?

##### Share on other sites
You can use cube mapping to texture a sphere, the normal would be the texture coord. Edited by Ashaman73

##### Share on other sites
Bacterius: Thanks for your reply. Offsetting my U coordinates allows me to rotate the sphere along the equator. The same does not work with the V register, as it puts the south pole along the equator, stretching it. My current textures are classic earth, moon, mars textures.

I have a screen shot here: http://www.positronicdreams.com/stuff/fup/_fileup/raytracing001.png

Everything seems perfect in that image, but it isn't. I can mess with the U coordinate, but I can't rotate the sphere or anything. In the link I provide as source, it seems that the equations take a north vector, a right vector, and a 3rd vector (cross-product of the first 2), so this would give us 3 axis to manipulate the sphere, but alas, whenever I mess with those, the resulting image has flaws, most common is texture mirroring along some axis on the sphere...

Ashaman73: Thanks for replying. I'm really set on doing classic texture mapping, onto a sphere. All I want is a good pointer where I can read up on this and get it right, or at least copy some code. I really don't want to do cube mapping at this time.

##### Share on other sites
Can you show your texture mapping code? There is a "perfect" mapping method that involves converting the position on the sphere to spherical coordinates and sampling your texture this way, but it is rather compute-intensive (a couple inverse trigonometric functions, so quite undesirable) and from your description my guess is you are using the tangent/bitangent/normal technique to texture your sphere, which - iirc - has a few gotchas to keep in mind, and I think one of them, is that you must modify your "up" vector in your cross products to be the sphere's polar vector instead of just using a constant up vector if you want to "wrap" the texture differently, are you doing that?

##### Share on other sites
Bacterius, thank you for your continued interest. I'll write a reply later when I get home with the code I'm using. Again, thank you.

##### Share on other sites
Bacterius: Ah, solved it! Also added matrix operations, and a rotation procedure, and was able to tilt the sphere by 30º, and saw that the texturing was all correct. No mirroring at the poles.

Can I bother you with a few questions?

1) Regarding colors, as I trace a ray through the scene, through reflections, textures, etc, for each step of the way how do I handle the computation of the final color?

I'm, for now, going with:

rgb_final = (rgb1 + rgb2) /2;
...basically averaging colors for each ray collision...

I've heard something about color multiplication, like so:
rgb_final = rgb1 * rgb2;

2) If I hit a reflective surface, should I follow the surface's Normal vector, or should I calculate the reflection as below?
R = 2(AP . N) x N - N

AP - Vector from view to Intersected Point, normalized
N - Normal Vector of the surface, normalized

3) Any online resources you advise?

Thank you!

##### Share on other sites

Bacterius: Ah, solved it! Also added matrix operations, and a rotation procedure, and was able to tilt the sphere by 30º, and saw that the texturing was all correct. No mirroring at the poles.

1) Regarding colors, as I trace a ray through the scene, through reflections, textures, etc, for each step of the way how do I handle the computation of the final color?

I'm, for now, going with:

rgb_final = (rgb1 + rgb2) /2;
...basically averaging colors for each ray collision...

I've heard something about color multiplication, like so:
rgb_final = rgb1 * rgb2;

Well, this is a tricky question, but in theory you multiply bounces. This is because in real life, light comes from light sources, and bounces into your eye. So say a light ray starts with energy E, then hits a reflective surface like a mirror. It'll bounce in the reflected direction, but might lose, say, 4% of its energy. Then, it hits a diffuse surface, and loses another 10% of its energy. Then it enters your eye. So in total, its new energy is E * 0.96 * 0.90. If you do this backwards (which is allowed because of reciprocity), it becomes 0.90 (reflectance at first bounce) * 0.96 (reflectance at second bounce) * E (emittance at light source). It's a bit more complicated if your surfaces both emit and reflect light (it becomes a recursive multiplication of emittance + reflectance).

It's the same with RGB, except instead of raw "energy" you directly work with RGB channels (which are, by the way, simply measures of energy for three wavelengths: red, green and blue). So, yes, colors at each bounce must be multiplied component-wise, and your ray should also start at values (1.0, 1.0, 1.0) by definition of the empty product.

Note this also means your light RGB values will typically be greater than 1, that's ok and completely expected (and if you get final RGB values which are bigger than 1 and you have to clamp them, you should look into tone-mapping)

2) If I hit a reflective surface, should I follow the surface's Normal vector, or should I calculate the reflection as below?
R = 2(AP . N) x N - N[/quote]
The formula above is wrong, I think, it's actually R = I - 2 * N * dot(I, N) where I is the incident vector (your AP) and N the normal. You must always follow this new reflected vector for perfectly mirror surfaces, anything else will look very wrong. However, not all reflective surfaces work like that. Most actually reflect light over a small "cone" focused on the reflection vector, so that the reflected image looks kind of blurred. Others (diffuse surfaces) reflect any incident light randomly in any direction. It's a fairly complex topic, but in general, if you hit an ideal mirror, you always use the equation above. This comes from the principle of least time, which states that when bouncing off a surface, light will take the shortest path from point A to point B - which happens to be described by the equation above.

3) Any online resources you advise?[/quote]
Many. If you ever need physics information about whether what you're doing is right, Wikipedia and Hyperphysics are the place to go. Also, Physically Based Rendering (book) covers a lot of ground when it comes to raytracing. You can also have a look at this in-depth tutorial about ray tracing, or perhaps this one both of which I found useful to work out some concepts, particularly regarding cameras.

##### Share on other sites

dx, dy, dz - Normal of the sphere.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632981
• Total Posts
3009687
• ### Who's Online (See full list)

There are no registered users currently online

×