Calculating a Frustum
Im having problems calculating the planes for a viewing frustum given a camera''s position and oreintation. Any websites on the matter? or can someone give me some ideas? thanks.
I got this out of an OpenGL program I''ve downloaded somewhere. I don''t use
it because I use DX, but it might prove useful. I plan on using frustrum
planes myself sometime in the future:
struct PlaneInfo { vector Normal; float D; void Set(float nx, float ny, float nz, float d) { Normal.SetXYZ(nx, ny, nz); D = d; }};static PlaneInfo FrustumPlane[6];static PlaneInfo TransformedFrustumPlane[6];void DisplayReshape(int w, int h)// Called when window changes dimensions. Set up viewport & projection matrix.{ glViewport(0, 0, w, h); // // Set up projection matrix. // This is slightly non-standard, since I prefer right-handed view coordinates, while // OpenGL defaults to a left-handed system. // float nearz = 4.0; float farz = 80000; float AspectRatio = float(h) / float(w); float ViewAngleH = 90 * (PI / 180); float ViewAngleV = atan(tan(ViewAngleH/2) * AspectRatio) * 2; glMatrixMode(GL_PROJECTION); glLoadIdentity(); float m[16]; int i; for (i = 0; i < 16; i++) m<i> = 0; m[0] = -1.0 / tan(ViewAngleH / 2); m[5] = -m[0] / AspectRatio; m[10] = (farz + nearz) / (farz - nearz); m[11] = 1; m[14] = - 2 * farz * nearz / (farz - nearz); glMultMatrixf(m); glMatrixMode(GL_MODELVIEW); // Compute values for the frustum planes. FrustumPlane[0].Set(0, 0, 1, nearz); // near. FrustumPlane[1].Set(0, 0, -1, -farz); // far. FrustumPlane[2].Set(-cos(ViewAngleH/2), 0, sin(ViewAngleH/2), 0); // left. FrustumPlane[3].Set(cos(ViewAngleH/2), 0, sin(ViewAngleH/2), 0); // right. FrustumPlane[4].Set(0, -cos(ViewAngleV/2), sin(ViewAngleV/2), 0); // top. FrustumPlane[5].Set(0, cos(ViewAngleV/2), sin(ViewAngleV/2), 0); // bottom.}void Display()// Display function.{ // Turn on z-buffering. glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); // Set up the view matrix. glMatrixMode(GL_MODELVIEW); glLoadIdentity(); matrix ViewMatrix; ViewMatrix.View(ViewerDir, ViewerUp, ViewerLoc); OGLViewMatrix(ViewMatrix); // Transform the frustum planes from view coords into world coords. int i; for (i = 0; i < 6; i++) { // Rotate the plane from view coords into world coords. I''m pretty sure this is not a slick // way to do this <img src="smile.gif" width=15 height=15 align=middle> PlaneInfo& tp = TransformedFrustumPlane[i]; PlaneInfo& p = FrustumPlane[i]; ViewMatrix.ApplyInverseRotation(&tp.Normal, p.Normal); vector v; ViewMatrix.ApplyInverse(&v, p.Normal * p.D); tp.D = v * tp.Normal; } // Clear buffers. glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw the quadtree. if (root) { root->Update(RootCornerData, (const float*) ViewerLoc, Detail); TriangleCounter += root->Render(RootCornerData, Textured); } // Show frame. glutSwapBuffers();}
From what I can tell, it looks like your 6 basic frustrum planes are setup
in local space at the origin. Then when it is time to use them at some
point in world space, all six planes are transformed. I haven''t studied
the code much yet, so I am unfamilar exactly how the planes are transformed.
But it looks like they do something with the planes'' normals and D equation
values.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement