Sign in to follow this  
wong_bo

[MDX] Perspective view problem

Recommended Posts

I am rendering a simple 3D model consist only triangle lists. When I render it in Orthogonal mode, it looks fine. But when I render it in Perspective mode, it appears to be "broken" and only some of the triangles are actually rendered, not all. If in Perspective mode, I change my camera view angle, more triangles (or less, depends on the view angle) won't get rendered as shown in the following picture. Image Hosted by ImageShack.us I am using
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
Any ideas why? Thanks.

Share this post


Link to post
Share on other sites
It might be useful to know how you're constructing your view & projection matrices to help solve this problem. As a quick guess, there might be something wrong with these matrices, so they project your triangles out of the viewing frustum at certain view angles. You could quickly check if this is the case by setting a larger z-range in your projection matrix and see if this fixes the problem.

Hope this helps :)

Share this post


Link to post
Share on other sites
Thanks and it does help. I changed my near plane value and the problem is gone. But I noticed that as my camera approaches the 3D object, some part of the object gets clipped off. If I don't want to see clipping effect on the screen, what would be the value for the near and far plane?

In my case, my 3D object bounding box range is:

boundingBox.Min = new Vector3(60000, 700000, -50);
boundingBox.Max = new Vector3(65000, 702000, 300);

I translated the object by the center of the center of the bounding box(i.e. changed device.Transform.World). Should the near plan be -50 and far plane be 300 when I create perspective matrix? I noticed clipping on certain camera angle when the camera approaches the object.

As for the view matrix, what parameter should I put in when I create it?

mView = Matrix.LookAtLeftHanded(mPosition, mTarget, mUp);

Since I translated the object by the center of the bounding box, should mPosition and mTarget value be the RELATIVE value from the center of the bounding box? Relative value seems to be working for me.

Share this post


Link to post
Share on other sites
Relative values only make sense if your world/camera is somehow related to this mesh (which might be the case with your terrain). But there is no fixed relation between the camera position and the mesh. With Matrix.LookAtLeftHanded you just define a camera with some world-space position that's looking at some world-space target. What this position and target should be solely depends on what you want to look at and from where.

The same goes for the perspective matrix. There is a loose relationship between the geometry and the near/far clipping planes you set with the perspective matrix. You'll typically want to fit your geometry between your near and far clipping planes as tightly as possible to get the best z-range resolution (this reduces depth fighting, check here for more info).

But this doesn't mean that you need to reuse values from your object's bounding box or anything. Normally you'll set the near clipping plane to 1 or another low value greater than 0 to prevent near-cutoffs. The far clipping plane should be just far enough away so you can view all geometry you want to see from you current position. If your geometry is too big to fit into the view frustum with a reasonable far clipping plane, you can just let it be cut off by the far clipping plane and use a fog effect to mask this cutoff.

Share this post


Link to post
Share on other sites
Might also be worth making sure you are clearing the Z-Buffer every frame (or disable it if you don't use it).

Hope this helps.

Share this post


Link to post
Share on other sites
I did clear z buffer at the beginning of every frame by using device.Clear(ClearFlags.Target | ClearFlags.ZBuffer...).

I think my problem is related to the Projection and View matrix. My understanding is that the near plane value means the distance between the near clip plane and the camera. Most of the sample code I read set it between 0.1 and 1. As the camera moves closer to the object (i.e. changing the view matrix), near plane value doesn't need to be changed (so the projection matrix does not needs to be changed either).

I tried a lot of values for the near plane, the best effect I got is to set it to a negative value of half of the Z dimension, and far plane to the positive value of half of the Z dimension. Then the terrain only get clipped of when I am flying almost on the surface of the terrain. If I fly a little bit higher, I won't get the clipping effect. This is what I want, I just don't know why I am using those values for near and far plane.

Here is some of my code:
1. First we got the 3D object's bounding box:

boundingBox.Min = new Vector3(60000, 700000, -50);
boundingBox.Max = new Vector3(65000, 702000, 300);

2. The terrain (3D object) can be translated, scaled and rotated in world matrix. This is a bit different as in most of the games, where the world matrix does not change.

Matrix translate, scale, rotate;
translate = scale = rotate = Matrix.Identity;
Vector3 center = GetCenterOfBoundBox(boundBox); //get the center coordinate of the bounding box
translate.M41 = -center.X;
translate.M42 = -center.Y;
translate.M43 = -center.Z;
... //scale and rotation matrix can also be changed
device.Transform.World = translate * scale * rotate;

3. Set up the projection matrix

float zDimension = GetZDimension(boundingBox); //get the Z dimension, in this case the value is 350
mNearPlane = -zDimension/2;
mFarPlane = zDimension/2;
mProjection = Matrix.PerspectiveFieldOfViewLeftHanded(fieldOfViewAngle, whRatio, mNearPlane, mFarPlane);
device.Transform.Projection = mProjection;

4. Setup the view matrix (camera)

float maxDim = GetMaxDimension(boundingBox); //get max dimension of bounding box, in this case, the value is 5000
mPosition = new Vector3(0f, 0f, boundingBox.Min.Z - maxDim);
mTarget = mPosition + new Vector(0, 0, 1);
mUp = new Vector3(0.0f, 1.0f, 0.0f);
mView = Matrix.LookAtLeftHanded(mPosition, mTarget, mUp);
device.Transform.View = mView


Can anyone explain to me why I have to use negative z dimension as near plane value? Or is there anything wrong with the above code?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this