Sign in to follow this  
kRogue

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

Recommended Posts

kRogue    100
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[i]=0.0f;
      for(j=0;j<3;++j)
	out[i]+=matrix[j][i]*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
kRogue    100
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
  • Popular Now