OpenGL Near and Far planes...

This topic is 3846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey, I want the coordinates of the edge points of my near and far planes... I have been looking at the following tutorial: http://www.lighthouse3d.com/opengl/viewfrustum/index.php?gimp They do the following: void FrustumG::setCamDef(Vec3 &p, Vec3 &l, Vec3 &u) { Vec3 dir,nc,fc,X,Y,Z; // compute the Z axis of camera // this axis points in the opposite direction from // the looking direction Z = p - l; Z.normalize(); // X axis of camera with given "up" vector and Z axis X = u * Z; X.normalize(); // the real "up" vector is the cross product of Z and X Y = Z * X; // compute the centers of the near and far planes nc = p - Z * nearD; fc = p - Z * farD; ... I do that and draw the near and far planes with GL_LINES and I can see my near plane in front of me.... to make matters worse if i spin the camera around the near plane stays exactly where it is... What is wrong with that code? Is it not taking into account something? Just for extra information, my code is as follows: Vector3 position = camera->GetPosition(); Vector3 direction = camera->GetViewVector(); direction.normalize(); Vector3 upVector = camera->GetUpVector(); Vector3 rightVector = camera->GetRightVector(); GLfloat screenRatio = camera->GetScreenRatio(); GLfloat fov = camera->GetFieldOfView(); GLfloat nearDist = camera->GetNearDistance(); // get the near and far frustum plane's width and height GLfloat tang = tan(fov * ANG2RAD); GLfloat nearHeight = 2 * tan(fov / 2) * nearDist; GLfloat nearWidth = nearHeight * screenRatio; GLfloat farHeight = 2 * tan(fov / 2) * FARDIST; GLfloat farWidth = farHeight * screenRatio; Vector3 zAxis = position - direction; zAxis.normalize(); Vector3 xAxis = upVector * zAxis; xAxis.normalize(); Vector3 yAxis = zAxis * xAxis; Vector3 nc = position - zAxis * nearDist; Vector3 fc = position - zAxis * FARDIST;

Share on other sites
I've recently had a similar problem where I was trying to do a nice efficient frustum culling routine. I initially tried their method but had some trouble getting it right, and I ended up just directly extracting the coords from the view*projection matrix like so (code from my project but originally taken from http://www.chadvernon.com/blog/tutorials/directx9/frustum-culling/):

g_pd3dDevice->GetTransform(D3DTS_PROJECTION,&matProj);
D3DXMatrixMultiply( &viewProjection, &matView, &matProj );
...
// Near plane
m_frustum[4].a = viewProjection._13;
m_frustum[4].b = viewProjection._23;
m_frustum[4].c = viewProjection._33;
m_frustum[4].d = viewProjection._43;

// Far plane
m_frustum[5].a = viewProjection._14 - viewProjection._13;
m_frustum[5].b = viewProjection._24 - viewProjection._23;
m_frustum[5].c = viewProjection._34 - viewProjection._33;
m_frustum[5].d = viewProjection._44 - viewProjection._43;
// Normalize planes
for ( int i = 0; i < 6; i++ )
{
D3DXPlaneNormalize( &m_frustum, &m_frustum );
}

Thats for DirectX but shouldn't be too hard to port to GL if that method would be suitible for you.

I skimmed over your code and noticed the following:

GLfloat tang = tan(fov * ANG2RAD);
GLfloat nearHeight = 2 * tan(fov / 2) * nearDist;
GLfloat nearWidth = nearHeight * screenRatio;
GLfloat farHeight = 2 * tan(fov / 2) * FARDIST;

In the first line, you convert fov (a quantity in degrees) into radians, which is correct. But you don't do this for the following two tan(fov/2) calls, that might be causing a problem. There may be additional problems but I didnt spot them in the brief glance I had.

GLfloat tang = tan(fov * ANG2RAD / 2.0f);
GLfloat nearHeight = 2 * tang * nearDist;
GLfloat nearWidth = nearHeight * screenRatio;
GLfloat farHeight = 2 * tang * FARDIST;

That should fix it, or at least make it slightly less broken :)

Share on other sites
I was looking at that as well, but how do you get the x,y,z coordinates for the edge points of the near and far planes? That only returns a single floating point value

Share on other sites
Those floats are then used by the following code to actually work out the vertices of the planes, taken further down from the setCamDef function:

// compute the 4 corners of the frustum on the near plane
ntl = nc + Y * nh - X * nw;
ntr = nc + Y * nh + X * nw;
nbl = nc - Y * nh - X * nw;
nbr = nc - Y * nh + X * nw;

// compute the 4 corners of the frustum on the far plane
ftl = fc + Y * fh - X * fw;
ftr = fc + Y * fh + X * fw;
fbl = fc - Y * fh - X * fw;
fbr = fc - Y * fh + X * fw;

For example, 'ntl' is a Vec3 representing the top-left vertex of the near plane. Are you using all of the lines of the function setCamDef as defined on the website? They are just about all necessary to work out the vertex positions, and that should be giving you the result you want.

Share on other sites
if you have the forward,upward and right vector of the camera including the viewport and near far parameters you can just as easy calculate the frustrum corners with simple trigonometry

I won t tell you how to do that, its pretty trivial

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634136
• Total Posts
3015756
×