Ray Tracing: Texture Map a Sphere

Started by
8 comments, last by MrOMGWTF 11 years, 5 months ago
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!
Advertisement
The closes I seem to find is this article:
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?
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?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You can use cube mapping to texture a sphere, the normal would be the texture coord.
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.
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?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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.
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! :D

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.

Glad you solved it!


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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

e967228d6f4a80a55220a7c4b70ebcba.png
ad415e568bc911ec29c8233d7c26bef2.png

dx, dy, dz - Normal of the sphere.

This topic is closed to new replies.

Advertisement