iDevGames Forums
Question regarding planes - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Question regarding planes (/thread-2496.html)

Question regarding planes - TomorrowPlusX - Jul 31, 2008 05:42 AM

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.

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:
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:
_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.

Question regarding planes - TomorrowPlusX - Jul 31, 2008 05:55 AM

I've gotten it to work by inverting cameraPlane.d...

This tells me I've got something wonky in my plane maths. Any suggestions? Wacko

Question regarding planes - Fenris - Jul 31, 2008 04:43 PM

No, that's correct, the plane's D value is defined as -dot(n, point). Smile Remember, Ax + By + Cz + D = 0.

(So you should invert all planes' d values.)

Question regarding planes - DoG - Aug 1, 2008 02:38 AM

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 Smile