# pouët.net

## Heightmap/terrain raytracing/disctance function

category: code [glöplog]
Is there any way to make a distance table or function, without using a 3D texture?
added on the 2010-03-23 20:29:32 by xernobyl
It doesn't... they use 3D textures.
added on the 2010-03-23 20:40:23 by xernobyl
added on the 2010-03-23 21:08:44 by すすれ
I'm not sure about your question... If you mean "is it possible to perform a raymarching algo on a 2D-heighmap without using a precalculated 3D-distance-map based on this 2D-heighmap?"... well, I have never tested it and I might be wrong, but I suspect the code to be too much gpu intensive on a per-pixel shading : every pixel should have to recalculate the distance map from the heighmap, even if nearby pixels will in fact be using the same distance map calculated values...
added on the 2010-03-23 22:09:35 by xoofx
[url]http://en.wikipedia.org/wiki/Distance_transform[url]

There are very fast and good approximations on this in O(n). There is also an exact solution in O(n), but by my tests it doesn't help so much rendering speed but the precalc time is longer and the algorithm is far more complex.
added on the 2010-03-23 22:18:25 by texel
In elevated they choose another approach : precompute the heighmap with a regular triangle-mesh rendering... and apply a final texturing once the distance is computed, enabling to achieve it in realtime.

Instead of having a 3D-distance map, they let the GPU calculate the exact distance for each pixel, avoiding a costly and unnecessary raymarching algo...
added on the 2010-03-23 22:18:32 by xoofx
It's more like "is there another aproach instead of having a 3D texture", an approach that uses less memory. Some property that I was missing... with an heightmap you have the distance to the ground, you could have the nearest distance from the ground to some sort of feature that would give you a good enough step approximation.
added on the 2010-03-23 22:20:52 by xernobyl
Oh, and, in software rendering it speed ups a lot, but of course it depends on the terrain properties. I don't know about gpu rendering, but probably it is not going to be as good as optimization as in software rendering.
added on the 2010-03-23 22:24:29 by texel
Oh, now I see what you mean... I'm doing not the same...
added on the 2010-03-23 22:27:11 by texel
I still don't get it, are we talking about a 2D height-map or a 3D height-map? Because you can't approximate cavities and other features not pertaining to the surface of your height-map if you are talking about a non-3D, light-weight, non-generative, approximative storage of height-map derivatives and their relative, respective distances.
added on the 2010-03-23 22:36:50 by decipher
xernobyl, you could probably compute a local distance map working on a small kernel of the 2D heighmap (like 4x4 pixels)... around the current "camera" pixel. This approach would result in more "marching" steps but would save the cost of building a 3D distance map...
added on the 2010-03-23 22:40:04 by xoofx
Decipher, indeed It's not possible to reconstitute cavities from a 2D heighmap... as I understand xernobyl question, he is talking about the process of calculating a 3D distance map from a 2D heighmap (textured or calculated). This 3D distance map that enable to perform a correct ray marching other the heighmap... (as it is explained in the GPU Gems2 article)...
added on the 2010-03-23 22:47:57 by xoofx
@decipher: Z(X,Y) = heightmap, and D(X,Y,Z) = distance table.
And I don't want a fucking huge D table with w*h*256*sizeof(float) bytes.

It's not that I don't want it, but I was just wondering if there's a lower memory solution.
added on the 2010-03-23 22:49:46 by xernobyl
And I know that it isn't that much memory for today's machines, that have at least 4GB of ram, and 1GB of video ram.
added on the 2010-03-23 22:52:14 by xernobyl
@lx: I think the only problem with your approach is, if there's no immediate storage for this 4x4 kernel then a fragment calculated within another kernel won't be able to occlude or cast-shadows on the current fragment, which means incorrect lighting.

If we were to store the kernel then he might as well just load the texture :).
On the other hand, there really is no benefit of slicing the calculations as the distance information has to be somehow generated from the 2D height-map in realtime. He might as well brute-force :).

PS: I hope I got your technique right.
added on the 2010-03-23 23:14:34 by decipher
So you want a table as end result, but you don't want to use the memory for it? ;)
If you just want to raymarch, you can get along with a little lower distance estimate, as long as the slopes are limited. Doing many simple steps can easily be faster than fewer expensive.
If you really want the right distance from a point, you could make a maximum-filtered mipmap chain and do some quadtree like traversion within in the sphere defined by the distance straight down.
added on the 2010-03-23 23:25:15 by Psycho
@Decipher, yep the kernel idea was a bad technique/vocabulary "transposition"... in fact, the main idea is to compute only the distance from the heighmap along the projected 2D camera ray... performing a "coarse grain" distance map from the 2D heightmap... then it depends on the granularity of the heighmap.... It would probably worth to work on a heighmap with a lower LOD, to perform the coarse grain distance map, and gradually use higher lod...
But this has to be tested... I'm not sure there is a definitive algo for such a problem...
added on the 2010-03-23 23:26:39 by xoofx
@lx: Sounds like you and Psycho are actually talking of pretty similar things :).
added on the 2010-03-23 23:28:29 by decipher
Quote:
a maximum-filtered mipmap chain and do some quadtree like traversion

I guess Psycho has formulated much better than me what I was meaning... ;)
added on the 2010-03-23 23:30:13 by xoofx
@Decipher... hehe, exactly :D
added on the 2010-03-23 23:30:34 by xoofx
texel: Linear speed exact distance transforms aren't that complicated. Sure the math behind "Distance Transforms of Sampled Functions" (Felzenszwalb et al.) might be a bit tricky to grok, but my implementation cooked down to around 30 LOC. And "dead reckoning" is pretty much trivial - I'd say it's even simpler to grok than most approximations ;)
added on the 2010-03-24 12:36:05 by kusma
Are you looking for something like anistotropic cone mapping ? I think olivieras work at UNC might be applicable... transform the heightmap to a cone map before rendering so the lookup into the texture map gives distance that can be stepped?
added on the 2010-03-24 16:46:53 by auld
assuming the max slope in the terrain is M, you can compute a conservative cone on the fly and compute the biggest safe step size like this:

Code:``` float t1 = h / ( M*dx - dy); float t2 = h / (-M*dx - dy); if( t1<0.0 && t2<0.0 ) return false; float dt = max(t1,t2); ```

h is the height difference between your camera and the heightmap at current camera position. dy is the y (vertical) component of the ray direction, and dx = sqrtf( dx*dx+dz*dz ) the horizontal component of the ray direction. Speeds up quite a lot compared to constant size/distance unaware raymarching.

added on the 2010-03-24 17:42:25 by iq