Search code examples
graphicsgeometryraytracing

Ray-tracing a sphere with displacement mapping


I'm interested in building a simple "Google Earth" type app (for overlaying my own information, not the huge quantity of data that Google has). I'd like it to just be a simple X11 app that ray-traces a sphere with displacement (topographic) information. Ray-sphere intersection is pretty simple, but when the displayment mapping is throw in there it starts to get muddy in my head.

I was wondering if there's a simple technique to extend basic ray-sphere intersection to include displacement data...


Solution

  • enter image description here

    I know its been 12 years since first post, but I've uploaded a complete solution to this problem here: https://github.com/ramakarl/just_math

    See the sample for math_displace_sphere. This raytraces a displaced sphere terrain at arbitrary detail w/o any mesh. Pure CPU code, could be ported to shader/GPU. MIT licensed.

    The solution is to march along a ray, while projecting the ray sample points back down to lat-long, and sampling the terrain texture to determine the displaced height at that sample. Heights are specified relative to center of sphere.

    Pseudo-code:

    for ( ; ray_hgt >= terrain_hgt && ray_hgt <= shell_hgt; ) { 
    
            sample += ray_dir * dt; 
    
            ray_hgt = (sample - sphere_center).Length();
    
            // given sample, compute surface_pnt and lat, long
            ComputeSphereUV ( sample, surface_pnt, lat, long ); 
                    
            pixel_val = terrain_map->GetPixelUV ( lat, long ).x;                                    
    
            terrain_hgt = sphere_radius + pixel_val * terrain_depth;    
    }