Problems getting correct Viewing frustrum planes

Started by
23 comments, last by Zakwayda 18 years, 7 months ago
Ok I tried to normalize the planes using this code (again on the paper):

float mag;

mag = sqrt(plane.a * plane.a + plane.b * plane.b + plane.c + plane.c);

plane.a = plane.a / mag;
plane.b = plane.b / mag;
plane.c = plane.c / mag;
plane.d = plane.d / mag;


After moving of 6 units straight ahead,
here is the view matrix (The height of the camera is 3 units):

1 0 0 0
0 1 0 0
0 0 1 0
0 -3 -6 1

I think it is correct.

Immediately after doing
SetTransform(D3DTS_VIEW, &matView);

I extracted the planes, and tried to normalize the far clipping plane.

I got undefined result because the sqrt was done on a negative number !

plane.a = 0
plane.b = 0
plane.c = -1
plane.d = 7
(before normalizing)

I can't see any other thing than a bad extraction of the planes.
Still can't explain it.

I hope someone can.
Advertisement
The sqrt() operates on a sum of squares, which should always be positive. Are you sure your plane normalization code is correct? What you posted is right, but if that's what you're using I don't see how it could have failed...

Also, I'm not clear on where you're getting the matrix to extract your planes from. If your view matrix is V and your projection matrix is P, you should extract your planes from V*P.

Also, what are your far and near plane distances? The plane values in your post look like they might be correct. I'm guessing there's just some minor mistake in your code somewhere that's messing things up...
Quote:Original post by Juksosah
mag = sqrt(plane.a * plane.a + plane.b * plane.b + plane.c + plane.c);


That should be plane.c * plane.c not plane.c + plane.c.. that is why you're getting negative numbers in the sqrt.

www.marklightforunity.com | MarkLight: Markup Extension Framework for Unity

Quote:That should be plane.c * plane.c not plane.c + plane.c.. that is why you're getting negative numbers in the sqrt.
Oops. I can't believe I missed that :-|
OK I can't believe I missed that too...

After normalizing I still have the same results.

Mag is equal to 1, so nothing change.


But wait... I never multiply view by proj. matrix in my code.
I'll try this, i'm sure it'll solve my problem.


I always tought that you had to set up the proj. matrix at the beggining of your code, and never touch it again after.So I didn't bother storing this matrix in my code.

Ill keep you in touch
Jyk you got it !
When I multiplied the view and proj matrixes together I got something like (0,3,106) for the far plane, when standing at (0,3,6). I tried to extract the other planes, (top-bottom, right-left planes) and now I have the SAME PROBLEM than before!

Left and right planes as well as top and bottom ones are always at the same position than I am standing (0,3,6).

Here is the result of the mulitplication of view and proj matrix,
using D3DXMatrixMultiply(&test, &matView, &matProj)

1.82 , 0 , 0 , 0
0 , 2.42 , 0 , 0
0 , 0 , 1.02 , 1
0 , -7.24 , -7.07 , -6


Here is how I build my proj. matrix :
D3DXMatrixPerspectiveFovLH (&matProjection, D3DX_PI/4, 800/600,
1.0f, 100.0f);


I'm really clueless about that.
It works for the far plane, but not for the other planes...
Or maybe it is just normal I don't know.
P.S : I tried with normalized planes too.
Still not working, huh? Well, I may be out of ideas. I understand the algorithm and have implemented it successfully in my own code, but I haven't spotted whatever is causing it not to work for you.

Are you sure it's not actually correct and you're just misinterpreting the results? One last suggestion. One of the ways I test my frustum code is to construct the frustum from the point of view of an object other than the camera, and then draw it. This might make it easier to see if the frustum info is actually wrong, and if so how.

You can find the frustum corners by intersecting the normalized planes in sets of three, and then just draw lines between them.

Sorry I can't be of more help :-|
Ok that's a good idea.

I'll try it.

Again, thanks for ur help !
It's me again

I'm trying to find the intersection of three planes, but got some problems to implement it.

It says I must find the determinant of the 3 normal vector of each plane.

Solving this require a 3 x 3 matrix, and there is no build-in type for a 3x3 matrix, as far as I know. There is a build-in function to find a determinant for a 4x4 matrix, but how can I convert a 3x3 matrix to a 4x4 matrix ?

I really annoyed by matrixes so I wouldnt like to write a class to hold 3x3 matrixes and having to code a function to find a determinant by myself.

Is there a better solution ??
How did you do it Jyk ?
Hello again :)
Quote:I really annoyed by matrixes...
Well, you'll have to deal with matrices eventually :) However, the good news is you don't need them yet.

The determinant of a 3x3 matrix is equivalent to the vector triple product, which is expressed in terms of the dot and cross products, which I assume you already have. Note that vertical lines around a matrix mean determinant. So we have:
| ax ay az || bx by bz | = triple_product(a,b,c) = a.Dot(b.Cross(c))| cx cy cz |
(Or a cyclic permutation thereof.)

The equation for the intersection of three planes is:
d1(n2Xn3)+d2(n3Xn1)+d3(n1Xn2)-----------------------------        n1.(n2Xn3)
If the absolute value of the determinant is very small, the planes don't (robustly) intersect in a single point and you'll have to bail.

One last important detail. The d's in the above equation are from the plane equation p.n = d, whereas the frustum plane extraction returns planes in the form ax+by+cz+d = 0. So if you haven't already, you'll have to negate the d's in order for the plane intersection code to work.

This topic is closed to new replies.

Advertisement