PDA

View Full Version : Frustum projection on ground plane



Victor Hugo
06-30-2009, 04:16 AM
Hi All,

I'm developing a terrain engine, and for that I've setup my frustum and extracted the frustum six planes.

I've done this correctly, however has my data is organized as a grid I need to project the frustum into my ground plane, in order to extract the exact 2D grid cells that contains frustum.

This image in this post represents what I'm trying to do :

http://www.gamedev.net/community/forums/topic.asp?topic_id=531400

My problem is that this works until my camera reach a pitch of 60 after that the values I get are wrong.

My code is like this:



GLfloat dxn, dxf, dyn, dyf, dzn, dzf;
GLfloat x, y, z;
GLfloat nx,ny, nz;
bool rn = gluUnProjectf(aXi, aYi,0.1f, model_viewf, projectionf, iViewPort, &dxn, &dyn, &dzn);

bool rf = gluUnProjectf(aXi, aYi,0.5f, model_viewf, projectionf, iViewPort, &dxf, &dyf, &dzf);

GLfloat cf[3];
iCamera.getWorldPosition(cf);

GLfloat cf[3];

GLfloat v[3];

v[0] = (dxf - dxn) / sqrtf((dxf - dxn) * (dxf - dxn) + (dyf - dyn) * (dyf - dyn) + (dzf - dzn) * (dzf - dzn));

v[1] = (dyf - dyn) / sqrtf((dxf - dxn) * (dxf - dxn) + (dyf - dyn) * (dyf - dyn) + (dzf - dzn) * (dzf - dzn));

v[2] = (dzf - dzn) / sqrtf((dxf - dxn) * (dxf - dxn) + (dyf - dyn) * (dyf - dyn) + (dzf - dzn) * (dzf - dzn));

GLfloat t = -cf[2] / v[2]; // Z plane = 0 intersection

GLfloat p[3];

p[0] = cf[0] + (v[0] * t);
p[1] = cf[1] + (v[1] * t);
p[2] = cf[2] + (v[2] * t);



p is the point where we intersect the Z ground plane.

I do this for each screen corner in order to get the projected frustum bounding in world coordinates.

For me the theory look correct, maybe I'm missing some restriction in my calculations.

Any help will be appreciated, please.

Victor

dletozeun
06-30-2009, 05:31 AM
Do you handle the case when one or more frustum corner does not hit the ground?

Victor Hugo
06-30-2009, 05:44 AM
Hi dletozeun,

Well I was assuming that it always it the ground. I was supposing that only when the ray is parallel ( pitch = 90 ) to the Z plane then we never reach the ground plane.

Is it possible to happen now ?

I check the point "p" and the Z value is 0 even when I get wrong results so I though it intercept the ground plane.

How could I have sure about that ?

Thanks for helping me,

Victor

dletozeun
06-30-2009, 06:01 AM
First thing. Where are you looking when pitch angle is zero? sky or the ground? In my conventions it is the sky ;)

You have to handle all cases when one ray does not hit the ground. You can easily find all these cases on paper.

For example, if a ray is turned toward the vertical axis, it will probably not hit the ground (but that also depends on the terrain relief).

Now, I did not implement this stuff myself so I can't help you about the math details.

Victor Hugo
06-30-2009, 06:05 AM
Hi,

I start looking to the ground when pitch is 0. As pitch increases the camera is looking upper and upper .

So maybe some of my corners doesn't hit the ground.

I will try to check the math behind this stuff.

Thanks for all,

Victor

Victor Hugo
06-30-2009, 08:13 AM
Hi All,

I didn't fix the bug yet, however I have some debug to try help fixing this situation.

We have two outputs, both to the same screen point using gluUnproject from the code above.

The second output is when things goes wrong.



Camera : [0.000000,-17.300568,13.672989]
v [0.285711,0.958287,-0.007436]
t [1838.649658]




Camera : [0.000000,-17.447235,13.379364]
v [0.285712,0.958275,0.008763]
t [-1526.760986]


If someone could give me some help understanding the problem.

Thanks.

Victor