Ray: distance travelled between 2 planes

Started by
8 comments, last by hagerprof 9 years, 10 months ago

I'm trying to fake cheap-ass ground fog. I made my own version, but it has problems, which I don't understand.

Since math and physical activity are things I'll get to - eventually; I might as well ask for some help.

This is my own version:


float foglen = max(0.0, fogTopY - point.y) / max(0.1, pointDir.y);

The double max() was a signal to me that things were not done correctly.

pointDir is the direction vector from camera to a point in world.

point is the points world coordinates.

fogTopY is the top of the fog plane, and point.y is the bottom of the fog plane. (when travelling)

Both planes' normals can be assumed to be vec3(0, 1, 0)

I'm already using math-fu to smooth the top and bottom of the fog.

Still, my equation does work, but I'm wondering if there's anything wrong with it.

Advertisement

What distance are you trying to find exactly? Is it the distance along the camera path from the first plane intersection to the second plane intersection?

What distance are you trying to find exactly? Is it the distance along the camera path from the first plane intersection to the second plane intersection?

I'm trying to find the distance travelled in fog, so the distance ray has to travel from cameraPos to point, except only in fog. Fog starts at fogTopY and goes all the way down. The point is a world coordinate belonging to a pixel/fragment. Which is what you guessed.

is pointDir.y always negative? If it is always negative then max(0.1, pointDir.y) will always be 0.1;
My current game project Platform RPG

is pointDir.y always negative? If it is always negative then max(0.1, pointDir.y) will always be 0.1;

Only if I'm looking upwards, which happens. Removing the max() doesn't help much because ray.y can be 0 when looking straight forward into the fog.

The thing is, I know there is a way to calculate this travel distance properly, because my equation is basically just me and a paper - not much.

The ideal solution is something that is [0, ZFAR], so that traveldistance / ZFAR => [0, 1].

Your basic len calculation looks ok, take a look at Eric's fog document over here, it should help you with your ground fog.

Every time i read a whitepaper, I ask myself if there is some stigma attached to explaining things clearly with proper names and function descriptions.

Not that it's completely unreadable.

Well, it works now, at least with smoothing the top and bottom:


float foglen = max(0.0, fogTopY - point.y) / abs(ray.y);

Oops, I forgot to hit post on this and fell behind in the discussion:

"I'm probably not totally understanding the context. If y is positive down, your numerator will always be 0 because the fogTopY - point.y should always be negative.

If y is positive up, and your camera is always facing the ground (from the air), your denominator will always be 0.1, because pointDir.y will always be negative."

The article has a nice 3D solution. However, it looked like it pre-solved the dot products in its code, which I don't think would fit your known data. Your code would have to do this.

Is ray.y equal to pointDir.y? Does ray.y have a fixed length? If so you may have a problem when you have a horizontally facing camera as the equation will approach infinity. This would be right if your camera is in the fog, but would throw an exception. If the camera were above the fog, it would give an incorrect answer.

Also, I'm assuming that the camera can be above or in the fog. The article adeptly uses a saturation function to control the distance of non-traversed fog. Since you haven't used one, you'd probably need an "if" statement( especially if you don't have saturation available in your programming language.)

If you want we can work through a specific equation for your case(s).

as the equation will approach infinity

Indeed, the entire fog area becomes white. I smoothed it out on the top and bottom, and the results were ok. The reason I posted on this forum was simply to know if there was a way to do this thing that made some beautiful mathematical sense. Typically how you have issues with angles, but not with vectors.

What does the saturation() function do?

In GLSL you have min() and max(), step() and other stuff to avoid if () branches.


	// distance in fog is calculated with a simple intercept
	float foglen = max(0.0, fogTopY - point.y) / abs(ray.y);
	foglen = min(1.0, foglen / ZFAR);
	
	// how far are we from center of fog?
	float foglevel = min(1.0, abs(point.y - fogY) / HEIGHT);
	foglevel = 1.0 - foglevel * foglevel;

That's how I calculate the fog now.

foglen is the length the ray traversed into the fog and foglevel is my crossfade top/bottom.

Multiplied together the fog will no longer have hard edges, add some noise on top and its decent.

Where fogY is the center of the fog (absolute Y-coordinate) and HEIGHT is how far the fog goes up and down.

Before I forget, saturation is explained way better than I can do in this.

I think that I might still be not getting it, but your foglen essentailly is driven by the height of your fog itself. So, if your camera is angled the same at point, but as both move (with no relationship change), foglen can change if the fog height grows. This would work fine if you are approximating the fog thickness with the height of the fog. I'm not sure that is what you want.

Why not use an equation like:

foglen = abs((cam.y-point.y)*Math.sqrt(ray.x*ray.x+ray.y*ray.y+ray.z*ray.z)/ ray.y) (add your math libs as needed)

where cam.y is your camera's y position? You can apply a max or min if desired.

This topic is closed to new replies.

Advertisement