Finding the horizon position

Started by
4 comments, last by Bob Janova 17 years, 11 months ago
This is probably an easy problem, but for some reason the solution escapes me, so don't laugh too hard please. :) What I'd like to do is calculate the height of the horizon (the vanishing point) on-screen, as the camera pitch varies. Simply put, turning the camera downwards the horizon should move upwards, and vice versa. I need the y coordinate of the horizon line in world/view/screen space (whichever is easier). To simplify further, let's assume that we're talking about the horizon of the XZ plane (Y points upwards). I'd be thankful for any and all help.
"Morituri Nolumus Mori"
Advertisement
The y-coordinate is not really descriptive enought since it may be rotated..
What you really need is to compute a segment that represents the horizon.

Intersect the clip space planes with z = 0 (or whatever your horizon is). This would give you four lines. Parameterize these lines, such that the directing vector is in the same direction as that of your viewing direction. For each line determine the point that is the limit of the projection of the point respective to the parameter as this parameter approaches infinity. (This might work just by projecting a point whose xyz are that of the direction and the w is 0, but you should consider some rock-solid mathematical reference on this issue)

These four points are collinear, two of which are on the border of your screen.

This algorithm assumes that the projection is perspective, otherwise there is no horizon (unless you are dealing with a large sphere and other implications, but you are probably not)..
[ my blog ]
Quote:Original post by DragonL
This is probably an easy problem, but for some reason the solution escapes me, so don't laugh too hard please. :)

What I'd like to do is calculate the height of the horizon (the vanishing point) on-screen, as the camera pitch varies. Simply put, turning the camera downwards the horizon should move upwards, and vice versa. I need the y coordinate of the horizon line in world/view/screen space (whichever is easier). To simplify further, let's assume that we're talking about the horizon of the XZ plane (Y points upwards). I'd be thankful for any and all help.
You said world/view/screen space. The world space answer should be obvious: (-infinity, 0, infinity) to (infinity, 0, infinity). You could pick a suitably large number instead of infinity, and then translate that through your world-to-view-space transformation matrix. I assume that there is no roll applied to the transformation matrix.

Please provide some background on this, as there may be a better solution which doesn't even require you to find this out.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Thanks for your replies!

arithma, iMalc's assumption that there is no roll applied is correct, so the y coordinate is constant across the screen, and I don't need a segment. I'll freely admit that I don't fully understand the method you describe, although it seems interesting. By clip space planes, do you mean the frustum planes? Intersecting them with the ground plane (y = 0, in my case) would yield four lines forming a rectangle. I'm not sure how I could parameterise those lines using the view vector, since they are not parallell to that vector. Could you clarify a bit for me please?

iMalc, I have a feeling that translating an "infinite" number, as you suggest, might actually do the trick... It does sound simple enough to work! :) As for your question, what I want to do is render an infinite ocean plane, and I need to match the far edge to the horizon.

I'd be happy to hear more suggestions on how to accomplish this! ;)
"Morituri Nolumus Mori"
Quote:By clip space planes, do you mean the frustum planes? Intersecting them with the ground plane (y = 0, in my case) would yield four lines forming a rectangle. I'm not sure how I could parameterise those lines using the view vector, since they are not parallell to that vector. Could you clarify a bit for me please?

Yes frustrum planes. You have two options for the parameterizing vector, two opposite directions. Select the one that yields a positive dot product with the view vector.

Plus you don't actually have to intersect the planes. Thinking about it, you should only find the cross product.

I will go home now, think about it some more and I'll get back to you tomorrow..
Think a bit about it in 2d, it might help
[ my blog ]
If you can assume a flat horizon, with no zoom, then it solely depends on your vertical field of view and the camera pitch. If you are pretending the screen is part of a sphere (i.e. one pixel maps to a° over the whole screen) then the y coord is just ymiddle±(a×cameraPitch) (the sign of the operation depends on whether you're using Windows or PostScript style Y coordinates, and which way +ve pitch is). If you're saying the screen is flat (i.e. y=b tan phi) then similarly the y coord is just ymiddle±(b×tan cameraPitch).

I think a typical projection matrix (gluProjection or equivalent) is the second sort, but I haven't done 3D programming for long enough I forget.

This topic is closed to new replies.

Advertisement