I have got a sphere in my world space. I don't understand how can i find my sphere using my X and Y on my screen, because i don't understand what the value Z of my ray assuming the fact that we use world coordinate system.

  • \$\begingroup\$ Think about the world you want to draw: the terrain, walls, or floor. What coordinate system do you author those features in? \$\endgroup\$ – DMGregory May 3 '19 at 16:15
  • \$\begingroup\$ What do you mean by "author"? \$\endgroup\$ – Num Lock May 3 '19 at 16:24
  • \$\begingroup\$ "Make" "create" "produce" - somehow, scene that you want to draw with raytracing comes into being - whether you've arranged it in a level editor or loaded it from a file or generated it procedurally or expressed it as a signed distance field equation. The content of that scene - the 3-dimensional forms that you want to draw - is described in some coordinate system. What coordinate system is that? How will you compare a ray in screen space against a surface in this other space? \$\endgroup\$ – DMGregory May 3 '19 at 16:31
  • \$\begingroup\$ From what i understood in your question, i would use world space coordinate. \$\endgroup\$ – Num Lock May 3 '19 at 16:32
  • \$\begingroup\$ Your question says "I don't understand why we need world space coordinates." Now you say "I would use world space coordinate." So, do you now understand why you need world space coordinates, if you can see where you would use them? If not, can you edit your question to clarify what part you don't understand? \$\endgroup\$ – DMGregory May 3 '19 at 16:35

Raytracing is an algorithm, that is a recipe, or series of steps to achieve a result, like so:

For each pixel in final image, loop
    1. Convert pixel direction (x0, y0, 1) to world space direction (x1,y1,z1) using inverse view transform
    2. using the new ray direction, perform collision detection
    3. sort the list of hits in ascending order, and choose the lowest distance.
    4. get information (colour etc) from the hit data.

    5. If (lighting) do lighting calculations #optional

    6. while (reflectionsEnabled AND rayCount < maxRays) loop #optional
        6a. compute reflected ray
        6b. repeat steps 2-5 and add to result
        6c. Increment rayCount.
    end while loop

    7. while (refractionsEnabled) loop #optional
        7a. Compute refracted ray.
        7b. repeat steps 2-6 and add to result
    end while loop

    assign result to image at coordinates (x0,y0)
end for loop

Ignore steps 5-7 until you are ready for them, but remember that a good raytracer should be able to do them.

This algorithm requires a change of coordinate system (view space to world space). To do this, you need to know a little matrix mathematics, in particular, homogeneous coordinate systems and affine transformations. A little vector mathematics wouldn't hurt either.

You will have to generate a view space transform, then compute the inverse of it (fortunately only once for a frame), and use that to transform your (x * y) parallel ray directions into world space.

You will then need a starting point in world space for each of your rays.

You then compute a ray of [max ray distance] length, and use it to compute ray vs sphere collision detection. In your case, a single sphere, once you have worked out if you have a hit or not, you can do the colouring of the pixel.

If you want to do fancier stuff, you will need the collision normal vector, distance from the origin point, light direction etc, in order to perform realistic lighting, but don't worry about that for now.

You don't "find" the sphere, as you are not looking for it. If a ray hits it, then you update the pixel with it's colour. If not, leave the pixel black.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.