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 :
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; iCamera.getWorldPosition(cf); GLfloat cf; GLfloat v; v = (dxf - dxn) / sqrtf((dxf - dxn) * (dxf - dxn) + (dyf - dyn) * (dyf - dyn) + (dzf - dzn) * (dzf - dzn)); v = (dyf - dyn) / sqrtf((dxf - dxn) * (dxf - dxn) + (dyf - dyn) * (dyf - dyn) + (dzf - dzn) * (dzf - dzn)); v = (dzf - dzn) / sqrtf((dxf - dxn) * (dxf - dxn) + (dyf - dyn) * (dyf - dyn) + (dzf - dzn) * (dzf - dzn)); GLfloat t = -cf / v; // Z plane = 0 intersection GLfloat p; p = cf + (v * t); p = cf + (v * t); p = cf + (v * 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.