Jump to content
  • Advertisement
Sign in to follow this  

Help with oblique near-plane clipping

This topic is 4627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, more questions. I’m trying to use this code to clip with reflections but its not working quite right. The clipping appears to be correct but the depth is wrong. This probably has to do with using a right handed system as apposed to the standard D3D left handed system but I'm not sure how to fix it. Can someone please help?

Share this post

Link to post
Share on other sites
Well, no suggestions yet so maybe a picture will help.
This picture is using the oblique near-plane clipping projection matrix.
The area circled clearly shows the inverted depth.

Using my broken projection

This picture is just using my original projection matrix.

Using the normal projection

[Edited by - Nameles on October 23, 2005 7:17:24 PM]

Share this post

Link to post
Share on other sites
Sure, I can post some of the code.

First, the oblique near plane clipping code (almost a cut and paste of the original). Not sure it its important, but the resulting projection matrix can not be inverted (at least thats what D3DX tells me).

void CreateObliqueNearPlaneClippingMatrix(const Plane& clipPlane, const Matrix4 &matrix, Matrix4 &outClipMatrix)
Vector4 q;

// Calculate the clip-space corner point opposite the clipping plane
// as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
// transform it into camera space by multiplying it
// by the inverse of the projection matrix

q.x = sgn(clipPlane.a) / matrix._11;
q.y = sgn(clipPlane.b) / matrix._22;
q.z = 1.0F;
q.w = (1.0F - matrix._33) / matrix._43;

// Calculate the scaled plane vector
// dot = (a*x + b*y + c*z + d*w)
Vector4 c = clipPlane * (1.0F / clipPlane.Dot(q));

// Replace the third column of the projection matrix
outClipMatrix = matrix;
outClipMatrix._13 = c.x;
outClipMatrix._23 = c.y;
outClipMatrix._33 = c.z;
outClipMatrix._43 = c.w;

Now the camera update code that computes the reflection transforms

void ReflectionCamera::DoUpdate() const throw()
// create the reflection matrix
D3DXMatrixReflect(m_matReflect, m_reflectPlane);

// reflect the parent cameras view (parent camera is the one that will display the reflection)
m_view = m_matReflect*pParent->GetWorldToViewMatrix();

// create the camera space clip plane
Plane cameraSpacePlane;
D3DXPlaneTransform(cameraSpacePlane, m_reflectPlane, m_view);

// now set up the clipping projection matrix
static bool clip = false;
CreateObliqueNearPlaneClippingMatrix(cameraSpacePlane, pParent->GetProjectionMatrix(), m_proj);
m_proj = pParent->GetProjectionMatrix();

// cache the view*projection and inverse of the view and projection
m_viewProj = m_view*m_proj;
D3DXMatrixInverse(m_viewInv, NULL, m_view);
D3DXMatrixInverse(m_invProj, NULL, m_proj);

// _22 is negative five because we are RH Z-Up, I think this is correct
// of not, why does it work?
static Matrix4 kRemappingTM(
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f);

// create a mirrored version of view * projection * remap matrix
m_matTexCoordGen = m_viewProj * kRemappingTM;

// build the view frustum
Frustum3::FromViewProjection(m_viewProj, m_frustum);
m_dirty = false;

The reflection camera is used to render to the reflection target.
The parent camera (the main camera viewing the scene in this case) renders to the back buffer and the effect attached to the reflection plane uses m_matTexCoordGen to transform the positions into texture coordinates.
Values from the parent view matrix

_11 -0.99999994, _12 0.00000000, _13 0.00000000, _14 0.00000000
_21 0.00000000, _22 -0.44721431, _23 0.89442676, _24 0.00000000
_31 0.00000000, _32 0.89442670, _33 0.44721434, _34 0.00000000
_41 0.00000000, _42 1.9073486e-005, _43 -22.360678, _44 1.0000000

Values from the parent projection matrix

_11 3.7867479, _12 0.0000000, _13 0.0000000, _14 0.0000000
_21 0.0000000, _22 4.8260131, _23 0.0000000, _24 0.0000000
_31 0.0000000, _32 0.0000000, _33 -1.001001, _34 -1.000000
_41 0.0000000, _42 0.0000000, ,_43 -0.50050050, _44 0.0000000

The resulting projection matrix

_11 3.7867479, _12 0.0000000, _13 0.0000000, _14 0.0000000
_21 0.0000000, _22 4.8260131, _23 -0.89442647, _24 0.00000000
_31 0.00000000, _32 0.00000000, _33 -0.44721422, _34 -1.0000000
_41 0.00000000, _42 0.00000000, _43 0.00000000, _44 0.00000000

Everything looks great when I use the parents projection.
Only the oblique near plane clipping projection causes the problem

Share this post

Link to post
Share on other sites

Reading my own post and I think I see the problem!!!
I don’t think the plane is being transformed correctly!!!

D3DXPlaneTransform(cameraSpacePlane, m_reflectPlane, m_view);

[embarrass]Should be this:
D3DXPlaneTransform(cameraSpacePlane, m_reflectPlane, m_view.GetInverse().GetTranspose());

I'll try it out but I'm almost sure that’s the problem!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!