Jump to content
  • Advertisement
Sign in to follow this  
kRogue

OpenGL frustrum culling.. what in the world am I doing wrong??

This topic is 4239 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

All right, I have wasted sooo much time trying to figure out what in the world I do wrong: I jsut wat to to simple frustum culling, using Oriented boxes: with the notation of openGL in mind: I first define an eye_frustrum with the same parameters that one passes to glFrustum, i.e. left,right,top,bottom,near and far. because I have been getting depserate I have computed the corners of the frustum as follows:
  //near
  m_left_top_near=vec3(proj.m_left, proj.m_top, proj.m_near);
  m_left_bottom_near=vec3(proj.m_left, proj.m_bottom, proj.m_near);
  m_right_top_near=vec3(proj.m_right, proj.m_top, proj.m_near);
  m_right_bottom_near=vec3(proj.m_right, proj.m_bottom, proj.m_near);

  //far
  ratio=proj.m_far/proj.m_near;
  m_left_top_far=ratio*m_left_top_near;
  m_left_bottom_far=ratio*m_left_bottom_near;
  m_right_top_far=ratio*m_right_top_near;
  m_right_bottom_far=ratio*m_right_bottom_near;

  m_middle=0.5f*vec3(proj.m_left+proj.m_right, proj.m_top+proj.m_bottom, proj.m_near+proj.m_far)


I know that for Frustum culling you really do not need the corners, but I have them so I can draw the frustum more easily, I then get the planes with:
//set the left plane:
normal=VectorCrossProduct(m_left_top_far - m_left_top_near, m_left_bottom_far- m_left_bottom_near);
m_planes[left_plane].Set(normal, m_left_top_far);

  //set the right plane:
normal=VectorCrossProduct(m_right_bottom_far - m_right_bottom_near, m_right_top_far - m_right_top_near);
m_planes[right_plane].Set(normal, m_right_bottom_far);

  
//set the top plane:
normal=VectorCrossProduct(m_right_top_far - m_right_top_near, m_left_top_far - m_left_top_near);
m_planes[top_plane].Set(normal, m_right_top_far);

//set the bottom plane:
normal=VectorCrossProduct(m_left_bottom_far - m_left_bottom_near, m_right_bottom_far - m_right_bottom_near);
m_planes[bottom_plane].Set(normal, m_left_bottom_far);

//set the near plane:
normal=VectorCrossProduct(m_right_bottom_near - m_left_bottom_near, m_left_top_near-m_left_bottom_near);
m_planes[near_plane].Set(normal, m_right_bottom_near);

//set the far plane:
normal=VectorCrossProduct(m_left_top_far-m_left_bottom_far, m_right_bottom_far - m_left_bottom_far);
m_planes[far_plane].Set(normal, m_right_bottom_far);


and then I get my frustum in world co-ordinates with:
#define lazy(x) do { x=GenericCameraClass::world_transformPt_eye(eye_matrix_world, eye.x); } while(0)

  lazy(m_left_top_near);
  lazy(m_left_bottom_near);
  lazy(m_right_top_near);
  lazy(m_right_bottom_near);

  lazy(m_left_top_far);
  lazy(m_left_bottom_far);
  lazy(m_right_top_far);
  lazy(m_right_bottom_far);

  lazy(m_middle)


the relavent goo for the Plane code is:
class Plane
{
 //stuff....

public:
  void Set(const vec3 &normal, const vec3 pt) 
  {
    m_normal=normal/normal.magnitude();
    m_value=dot(pt,m_normal); 
    SetAABHelper();
  }

  void Set(const vec3 &normal, float v)
  {
    float mag;

    mag=normal.magnitude();

    m_normal=normal/mag;
    m_value=v/mag;
    SetAABHelper();
  }

 bool Intersects(const GeneralCube &cube) const;
 //stuff...



};  

//(in another file)

bool Plane::Intersects(const GeneralCube &cube) const
{
  float p,v;
  int i;
  bool retval,retval2;
  
  v=dot(cube.center(), m_normal);
  p=abs(cube.radiuses(0)*dot(m_normal,cube.axis(0)) ) + 
    abs(cube.radiuses(1)*dot(m_normal,cube.axis(1)) ) + 
    abs(cube.radiuses(2)*dot(m_normal,cube.axis(2)) );
  
  LogStream(FrustumLog,"p=" << p << " v=" << v << " m_value=" << m_value << "\n");
  
  retval=(p+v>=m_value);
  
  //paranoia: the above test should be enough, but we'll go
  //ahead and check all vertices anyways.
  for(retval2=false, i=0;i<8;++i)
    {
      retval2=retval2 || (dot(m_normal, cube.Vertex(i))>=m_value);	
    }
  
  ASSERT(retval==retval2);
  return retval;
}


and I am using <cmath> so abs does do the right thing here... now at this point, if I just make a frustum, it works correctly, i.e. I correctly detect when a frustrum and cube intersect (actually when no plane of the frustum is entrirly on one side)... my cubes are in world coordinates always, and the code for GenericCameraClass::world_transformPt_eye is:
static vec3 world_transformPt_eye(const float4x4 &matrix, vec3 in) 
  {
    in-=vec3(matrix.GLElement(0,3), matrix.GLElement(1,3), matrix.GLElement(2,3));
    return in*matrix;
  }


the relavent code for the matrix stuff is:
class float4x4
{
public:
  GLfloat data[16];
  GLfloat *operator[](int i) 
  {
    ASSERT(0<=i && i<4);
    return data + 4*i;
  }
   
  const GLfloat *operator[](int i) const
  {
    ASSERT(0<=i && i<4);
    return data + 4*i;
  }

  GLfloat &GLElement(int i, int j)
  {
    ASSERT(0<=i && i<4);
    ASSERT(0<=j && j<4);

    return data[4*j+i];
  }

 //more stuff....

};


inline vec3 operator*(const float4x4 &matrix, const vec3 &in)
{
  vec3 out;
  int i,j;

  for(i=0;i<3;++i)
    {
      out=0.0f;
      for(j=0;j<3;++j)
	out+=matrix[j]*in[j];
    }

  return out;
}


and I pass the matrix to openGL for modelview stuff with (here camera is a const float4x4*) and state is state stuff for a mesh where m_matrixState is the matrix that goes from model co-ordinatres to eye-cooridnates.
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(camera->data);
glMultMatrixf(state->m_matrixState.data);


so what in the world am I doing wrong in my Frustum code? as of now meshes get culled if the viewer is not "inside" the meshes bounding box... [Edited by - kRogue on December 12, 2006 2:09:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
just one more note, reading the internet and man pages gives different anwers to what glFrustum does:

accorind to openGL specs from opengl.org:

M=| 2n/(r-l) 0 (r+l)/(r-l) 0 |
| 0 2n/(t-b) (t+b)/(t-b) 0 |
| 0 0 -(f+n)/(f-n) -2fn/(f-n) |
| 0 0 -1 0 |

where as apples website (http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/glFrustum.3.html) gives

M=| 2n/(r-l) 0 (r+l)/(r-l) 0 |
| 0 2n/(t-b) (t+b)/(t-b) 0 |
| 0 0 +(f+n)/(f-n) -2fn/(f-n) |
| 0 0 -1 0 |

another place (http://www.mevis.de/opengl/glFrustum.html) gives

M=| 2n/(r-l) 0 (r+l)/(r-l) 0 |
| 0 2n/(t-b) (t+b)/(t-b) 0 |
| 0 0 +(f+n)/(f-n) +2fn/(f-n) |
| 0 0 -1 0 |

and I would not be surprised if the 3rd row has 4 different answers depending on which man page one reads! Note: on my hardware (nVidia GeForce6600GT, Linux OS) I get the answer from openGL's specification.

note, this means something interesting about the planes (which I should ahve realized):
1) z_eye should be negative in order to see it with -far<=z_eye<=-near


at any rate, this made me realize that I was creating the eye_frustum incorrectly, the corner points should be:

/////////////////////////////////////////
//set the corners
//
// some horible truth, the near and far need
// to be negated to get an accurate picture of
// the frustum since the frustum is
// -far <= z_eye <= -near
// thus we use -near instead of near, and -far instead of far.
// note that to do this is the same as just changing
// m_*_near.z() to -near, and letting m_*_far=ratio*m_*_near;

//near
m_left_top_near=vec3(proj.m_left, proj.m_top, -proj.m_near);
m_left_bottom_near=vec3(proj.m_left, proj.m_bottom, -proj.m_near);
m_right_top_near=vec3(proj.m_right, proj.m_top, -proj.m_near);
m_right_bottom_near=vec3(proj.m_right, proj.m_bottom, -proj.m_near);

//far
ratio=proj.m_far/proj.m_near;
m_left_top_far=ratio*m_left_top_near;
m_left_bottom_far=ratio*m_left_bottom_near;
m_right_top_far=ratio*m_right_top_near;
m_right_bottom_far=ratio*m_right_bottom_near;

m_middle=0.5f*vec3(proj.m_left+proj.m_right, proj.m_top+proj.m_bottom, -proj.m_near-proj.m_far);




and there all the VectorCrossProducts in figuring out the normals to the plains need to be negated (or just change VectorCrossProduct(a,b) to VectorCrossProduct(b,a) )....

I put these posts up, because not only is some of the online docs wrong for glFrustum, but I wasted more than a day realzing this "negation" issue...


Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!