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