Frustum challenge !!! (help please)



Hi, this is similar to my previous post, but with more code.

I'm trying to get the frustum planes of the camera to know if a point is inside or outside of them.
The code, how I obtain the planes seems correct, but I obtain different results when I move or rotate the camera.

I was looking for tutorials and other newbee problems, but my code is similar of all them (but doesn't work).

For those who wants a challenge here are part of my code:

1- First set up the camera:

...
// Sets projection matrix
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(_fov, _aspect, _zNear, _zFar);

// Sets model and view matrix
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glTranslatef(_xoffset, _yoffset, 0f);
gl.glTranslatef(0f, 0f, _distance);

gl.glRotatef(_rotate[0], 1.0f, 0.0f, 0.0f);
gl.glRotatef(_rotate[1], 0.0f, 1.0f, 0.0f);
gl.glRotatef(_rotate[2], 0.0f, 0.0f, 1.0f);
...

2- Then gets the frustum planes:

...
double[] proj_mat = new double[16];
double[] view_mat = new double[16];
double[] matrix = new double[16];

/* Get MODELVIEW and PROJECTION matrix and multiply it to get
* the culling space coordinates. OpenGL draw vertex as the
* result of:
* v' = P(M(v))
* then, we multiply MODELVIEW and PROJECTION matrices as:
* matrix = P*M
*/
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, proj_mat, 0);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, view_mat, 0);

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glMultMatrixd(proj_mat, 0);
gl.glMultMatrixd(view_mat, 0);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, matrix, 0);
gl.glPopMatrix();

/* Matrix in OpenGL are stored in column-major order. Supposing we have:
* 1 2 3
* 4 5 6
* it will be stores as:
* 1 4 2 5 3 6
*
* The offset to a desired element is calulated as:
* offset = row_index + column_index*NUMROWS
*
* (Extracted from: http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf)
*
* In the next notation: mAB, A is the row and B is the column number, that is:
* m11 m12 m13 m14
* m21 m22 m23 m24
* ...
*/

// Left clipping plane
_left.A = matrix[3+0*4] + matrix[0+0*4]; // m41 + m11
_left.B = matrix[3+1*4] + matrix[0+1*4]; // m42 + m12
_left.C = matrix[3+2*4] + matrix[0+2*4]; // m43 + m13
_left.D = matrix[3+3*4] + matrix[0+3*4]; // m44 + m14

// Right clipping plane
_right.A = matrix[3+0*4] - matrix[0+0*4]; // m41 - m11
_right.B = matrix[3+1*4] - matrix[0+1*4]; // m42 - m12
_right.C = matrix[3+2*4] - matrix[0+2*4]; // m43 - m13
_right.D = matrix[3+3*4] - matrix[0+3*4]; // m44 - m14

// Top clipping plane
_top.A = matrix[3+0*4] - matrix[1+0*4]; // m41 - m21
_top.B = matrix[3+1*4] - matrix[1+1*4]; // m42 - m22
_top.C = matrix[3+2*4] - matrix[1+2*4]; // m43 - m23
_top.D = matrix[3+3*4] - matrix[1+3*4]; // m44 - m24

// Bottom clipping plane
_bottom.A = matrix[3+0*4] + matrix[1+0*4]; // m41 + m21
_bottom.B = matrix[3+1*4] + matrix[1+1*4]; // m42 + m22
_bottom.C = matrix[3+2*4] + matrix[1+2*4]; // m43 + m23
_bottom.D = matrix[3+3*4] + matrix[1+3*4]; // m44 + m24

// Near clipping plane
_near.A = matrix[3+0*4] + matrix[2+0*4]; // m41 + m31
_near.B = matrix[3+1*4] + matrix[2+1*4]; // m42 + m32
_near.C = matrix[3+2*4] + matrix[2+2*4]; // m43 + m33
_near.D = matrix[3+3*4] + matrix[2+3*4]; // m44 + m34

// Far clipping plane
_far.A = matrix[3+0*4] - matrix[2+0*4]; // m41 - m31
_far.B = matrix[3+1*4] - matrix[2+1*4]; // m42 - m32
_far.C = matrix[3+2*4] - matrix[2+2*4]; // m43 - m33
_far.D = matrix[3+3*4] - matrix[2+3*4]; // m44 - m34

// Normalize planes
_left.normalize();
_right.normalize();
_top.normalize();
_bottom.normalize();
_near.normalize();
_far.normalize();
...

3- Finaly a draw object. I only draw a point at (5,5,5) coordinates and check if it is inside the frustum.
public boolean containsPoint3D(Point3D point) {

double dist_left = _left.A*point.X + _left.B*point.Y + _left.C*point.Z + _left.D;
double dist_right = _right.A*point.X + _right.B*point.Y + _right.C*point.Z + _right.D;
double dist_top = _top.A*point.X + _top.B*point.Y + _top.C*point.Z + _top.D;
double dist_bottom = _bottom.A*point.X + _bottom.B*point.Y + _bottom.C*point.Z + _bottom.D;
double dist_near = _near.A*point.X + _near.B*point.Y + _near.C*point.Z + _near.D;
double dist_far = _far.A*point.X + _far.B*point.Y + _far.C*point.Z + _far.D;

if(dist_left >= 0 && dist_right >= 0 &&
dist_top >= 0 && dist_bottom >= 0 &&
dist_near >= 0 && dist_far >= 0){
return true;
} else {
return false;
}
}


If I move the camera, when the points goes out to left or right all is right. But if a rotate the camera then the result is wrong, when the point is inseide the window the answer is "point out of the frustum".

For top and bottom we obtain a wrong answer. That is, after the points diseapear out of the window the previous function answer me the point is out of the frustum.


Help for this newbee, please !!!
Thanks a lot.
.



Relevant Pages

  • Re: Camera plane
    ... The sentence only describes a line not a plane. ... the line connecting the camera & subject. ... is at the outside edge of the circle so we are able to move ... lights moving around the circle makes sense in the scenario I described. ...
    (alt.photography)
  • Re: How do you define the viewing frustrum?
    ... I wrote a program in C to find the coordinates of the view plane, ... the camera position, the point where the camera is pointing, the field ... struct point newPoint (float x, float y, float z) ...
    (comp.graphics.rendering.raytracing)
  • Re: Best approach...
    ... WTH loquated like no one had ever loquated before ... > writing a plugin for a rendering system and I need to ... > plane because in the initial usage this will be the case [to be ... > point on that plane regardless of the orientation of the camera. ...
    (comp.games.development.programming.algorithms)
  • Re: Bob Hatch, camera gear
    ... I shoot other aircraft with which I'm ... Rapid relative speeds also sometimes mean that I need a camera that can ... If your plane doesn't have a cig lighter jack, carrying a small, even ... how about a shot of the frontal ...
    (rec.outdoors.rv-travel)
  • Re: OT:9/11 Pentagon video goes public for first time
    ... surveillance style camera perhaps it would've been a better clip. ... this camera doesn't capture many frames per second, ... the plane. ... This aint going to change shit for the 9-11 conspiracy people. ...
    (rec.gambling.poker)