#### Archived

This topic is now archived and is closed to further replies.

# Calculating a Frustum

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

## Recommended Posts

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.

##### Share on other sites

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 = 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 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.

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 9
• 12
• 10
• 13
• 104
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009705
• ### Who's Online (See full list)

There are no registered users currently online

×