## Question regarding planes

Sage
Posts: 1,199
Joined: 2004.10
Post: #1
I'm debugging my code for trimming near and far planes for shadow mapping ( to increase precision ) and as far as I can tell, my math is all wrong.

I'm trimming near and far by creating a plane passing through the camera's origin, facing in the same direction as the camera. Then, by checking the distance of the objects being rendered against this plane, I expand/contract my far/near values. Later, those values are used to build the projection matrix. Obviously, I'm not using the rendered entities' positions, but rather bounding spheres. You'll see below.

The trouble is, it's looking like my math for creating that reference plane is wrong.

I adapted this code -- IIRC -- from some NVIDIA sample code. It's a constructor for my plane class which creates a plane facing along a normal passing through a point.

Code:
```plane_( const vec3_<T> &normal, const vec3_<T> &point ) {     vec3_<T> N( normalize( normal ));     a = N.x; b = N.y; c = N.z;                 d = dot( point, N ); }```

And here's my code to check the distance of a point from the plane:
Code:
```T distance( vec3_<T> p ) const {     return a*p.x + b*p.y + c*p.z + d; }```

This is a snippet of my code for trimming near and far for a depth pass:
Code:
```_suggestedNearPlaneForDepthPass = FLT_MAX; _suggestedFarPlaneForDepthPass = -FLT_MAX; //... later, for each object rendering in the depth pass. 'e' is the entity being rendered vec3 look( camera->look() ),      pos( e->aabb().center() ); plane cameraPlane( look, camera->position() ); float radius( e->aabb().radius() ),       nearDistance = cameraPlane.distance( pos - look*radius ),       farDistance = cameraPlane.distance( pos + look*radius ); if ( ShadowCameraRef sc = dynamic_pointer_cast< ShadowCamera >( camera )) {     // debug output, crazy values!     std::cout << "near-far expansion for " << e->name() << " near: " << nearDistance << " far: " << farDistance << std::endl; } _suggestedNearPlaneForDepthPass = std::min( nearDistance, _suggestedNearPlaneForDepthPass ); _suggestedFarPlaneForDepthPass = std::max( farDistance, _suggestedFarPlaneForDepthPass );```

Does anything look obviously wrong? I'm getting negative values, and the values don't seem to always make sense. My gut reaction is that my 'cameraPlane' is not what I want it to be: e.g., a plane passing through the camera's position facing the same direction as the camera.
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
I've gotten it to work by inverting cameraPlane.d...

This tells me I've got something wonky in my plane maths. Any suggestions?
Oldtimer
Posts: 834
Joined: 2002.09
Post: #3
No, that's correct, the plane's D value is defined as -dot(n, point). Remember, Ax + By + Cz + D = 0.

(So you should invert all planes' d values.)
Moderator
Posts: 869
Joined: 2003.01
Post: #4
I always prefered not to use the plane equation, and just do the vector algebra. Namely, if N is your normal, P is the point on the plane, and X is the point you're checking,
then the signed distance is d = (X-P) dot N

Admittedly, this means two additional operations in this form, but if refactored into d = X dot N - P dot N, you pretty much have the plane equation. Except that it's not a,b,c,d, but meaningfully named components