• Advertisement
Sign in to follow this  

Frustum corners

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to calculate the viewing frustum, but I'm getting slightly wacky results. Can someone spot any problems with the following code, assuming all functions I don't give do what they're supposed to?
Renderer::calculateCameraCorners (Vector3* corners)
  float tanFovDiv2 = tan ((cameraFov * (M_PI/180.0f)) / 2.0f);
  float Hnear = tanFovDiv2 * cameraZNear;
  float Wnear = Hnear * cameraRatio;

  float Hfar = tanFovDiv2 * cameraZFar;
  float Wfar = Hfar * cameraRatio;

  // Now calculate the eight corners
  // Near plane:
  corners[0].x = Wnear;  corners[0].y = Hnear;
  corners[1].x = -Wnear; corners[1].y = Hnear;
  corners[2].x = -Wnear;  corners[2].y = -Hnear;
  corners[3].x = Wnear; corners[3].y = -Hnear;

  corners[4].x = Wfar;  corners[4].y = Hfar;
  corners[5].x = -Wfar; corners[5].y = Hfar;
  corners[6].x = -Wfar;  corners[6].y = -Hfar;
  corners[7].x = Wfar; corners[7].y = -Hfar;

  for (int i=0; i<4; i++)
    corners .z = -cameraZNear;
  for (int i=4; i<8; i++)
    corners .z = -cameraZFar;

  for (int i=0; i<8; i++)
      corners = mul (invViewMatrix, corners);

Also, invViewMatrix was created as so:
Matrix4::makeLookAtInv (Vector3 eye, Vector3 center, Vector3 up)
  Vector3 f (sub (center, eye));

  Vector3 s (cross (f, up));
  Vector3 u (cross (s, f));

  Matrix4 orient (s.x, s.y, s.z, 0.0f,
		  u.x, u.y, u.z, 0.0f,
		  -f.x, -f.y, -f.z, 0.0f,
		  0.0f, 0.0f, 0.0f, 1.0f);

  Matrix4 translate (1.0f, 0.0f, 0.0f, 0.0f,
		     0.0f, 1.0f, 0.0f, 0.0f,
		     0.0f, 0.0f, 1.0f, 0.0f,
		     eye.x, eye.y, eye.z, 1.0f);

  return mul (translate, orient);

Probably unneeded explaination of OpenGL specific things: I can't think of anything I'm doing wrong, and I need to make sure my code makes sense to someone else. cameraRatio is width/height, cameraFov is in degrees, and cameraZNear correspond to a positive distance to the near /far clip planes. Also, I'm using opengl, so the view space is defined (I think) as originating from (0, 0, 0) and pointing down the negative z axis, with the planes at cameraZNear and cameraZFar and corners defined by the field of view. Thanks.

Share this post

Link to post
Share on other sites
I figured the problem out: I was simply doing the multiplication at the end of the inverse function in the wrong order. doh!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement