Reputation: 14583
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...
Upvotes: 4
Views: 3448
Reputation: 134
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;
}
Upvotes: 2
Reputation: 14583
I found this paper: http://www.cgl.uwaterloo.ca/~ecdfourq/GI2008/FourquetGI2008.pdf
Thought I'd share as it seems to cover exactly what I want to do, thanks guys!
Upvotes: 1
Reputation: 1235
Well the Ray-Tracing process is the same no matter if you have a simple 20-poly sphere or a complex displaced 2k-poly sphere. The ray traverses the scene no matter what it contains. But it is used for achieving visual effects such as transparency, reflections, refractions, etc. and from what you said in your question I think you could go without those in your project. It is very likely that you can get away with simple low-cost Ray-Casting here.
So, once you get the Rendering engine in place, you can add all the displacement you want to the scene. Two most common ways of modifying geometry are:
Displacement mapping adds real polygons to existing geometry, while bump mapping only simulates the visual effect by bending surface normals and thus influencing shadowing of the object. And while bending normals is a far quicker and less costly operation than tessalating geometry and adding new polys, it does not produce precise shadowing results, so watch out for that if it is of any concern to your application.
Also, consider using adaptive Level of Detail algorithms and data structures, because the further away you are from the geometry the less detail it needs.
Upvotes: 0
Reputation: 13700
Displacement mapping is pretty easy -- just tessellate the sphere, add offsets to the vertex positions based on the altitude sampled from the maps, and ray-trace all the pieces.
How far away is the camera from the sphere/earth? If you're right near the surface, it's probably not worth making a whole "sphere" at all, just make a "height field." If you're far (viewing the whole planet at once), then even the tallest mountains shouldn't visibly displace the surface, so you should be using simple bump mapping instead. Consider also using a combination -- a coarse tessellation that's truly displaced, and bump mapping on the residual height differences.
But in any case, I can't imagine why you would ray trace, as you've described the problem. Just chop it into triangles and use OpenGL. You probably don't need any ray-traced effects.
Upvotes: 1