**1**

# Ray Tracing: Texture Map a Sphere

###
#2
Members - Reputation: **885**

Posted 30 September 2012 - 06:17 PM

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?

###
#3
Crossbones+ - Reputation: **9887**

Posted 01 October 2012 - 03:35 AM

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*

###
#4
Crossbones+ - Reputation: **9264**

Posted 01 October 2012 - 05:42 AM

**Edited by Ashaman73, 01 October 2012 - 05:42 AM.**

My game: Gnoblins

Developer journal about Gnoblins

Small goodies: Simple alpha transparency in deferred shader

###
#5
Members - Reputation: **885**

Posted 01 October 2012 - 11:15 PM

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.

###
#6
Crossbones+ - Reputation: **9887**

Posted 01 October 2012 - 11:48 PM

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*

###
#8
Members - Reputation: **885**

Posted 08 October 2012 - 08:23 AM

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!

###
#9
Crossbones+ - Reputation: **9887**

Posted 08 October 2012 - 06:17 PM

Glad you solved it!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.

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).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;

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)

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.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

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.3) Any online resources you advise?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*