Projection Matrix generation

Started by
6 comments, last by Wilhelm van Huyssteen 10 years, 10 months ago

Hi.

This s a followup question to the one I asked here http://www.gamedev.net/topic/640939-projection-matrix-issue/.

In order to pull the near clipping pane close to the camera I had to scale all the values (except for far clipping pane) to glFrustum smaller. This works except it creates alot of jitter in animating models. Im asuming its some kind of percision error. I thought that the solution would simply be to move the far clipping pane closer as wel but no matter how close I move it the jitter remains.

How should I improve this?

Thanks in Advance!

[source]
//old values
//l = -1.0;
//r = 1.0;
//b = -768.0 / 1024.0; //-height / width
//t = 768.0 / 1024.0; //height / width
//n = 1.0; //near clipping pane
//f = 10000.0; //far clipping pane
//new values with clipping pane moved closer but with unacceptable jitter
//l = -0.1;
//r = 0.1;
//b = -768.0 / 1024.0 / 10; //-height / width
//t = 768.0 / 1024.0 / 10; //height / width
//n = 0.1; //near clipping pane
//f = 10000.0; //far clipping pane
public static Matrix4 glFrustum(float l, float r, float b, float t, float n,float f)
{
FloatBuffer fb = FloatBuffer.allocate(16);
fb.put((2*n)/(r-l));
fb.put(0);
fb.put(0);
fb.put(0);
fb.put(0);
fb.put((2*n)/(t-b));
fb.put(0);
fb.put(0);
fb.put((r+l)/(r-l));
fb.put((t+b)/(t-b));
fb.put(-(f+n)/(f-n));
fb.put(-1);
fb.put(0);
fb.put(0);
fb.put((-2*f*n)/(f-n));
fb.put(0);
return new Matrix4(fb);
}
[/source]
Advertisement

The depth buffer resolution at a certain depth is almost exclusively determined by the near clip plane, the far clip plane has almost no impact at all. Trying to improve the depth resolution by adjusting the far clip plane is usually not going to help at all.

There was a suggestion in that other thread: make sure that the view point doesn't get too close to the object to introduce clipping at the near plane. This just ensures that the clipping from your first thread cannot happen in the first place. Just don't get closer to the wall than the distance to corners of the near clip plane.

Another suggestion to improve the over-all depth buffer resolution is to partition the depth range. That is, split the depth range into sections: given your numbers, let's say two sections from 1 to 100, and from 100 to 10000, and use glDepthRange. then you draw the corresponding depth ranges individually (everything from 1 to 100 in one pass, and the rest in a second pass), but that may be difficult if you have a continuous scene with no easy way to partition objects by depth such that there are no overlap between depth ranges.

But to begin with, ensure that you have a maximum resolution depth buffer. Do you have a 24 or 32 bit depth buffer, or do you actually have a reason why you must use a 16 bit depth buffer if that's what you're using?

The depth buffer resolution at a certain depth is almost exclusively determined by the near clip plane, the far clip plane has almost no impact at all. Trying to improve the depth resolution by adjusting the far clip plane is usually not going to help at all.

A recent paper has indeed argued that setting the far plane to infinity can actually improve the precision (particularly if the modelview and projection matrices are not multiplied together before transforming the vertices). But it's the near plane who makes the bigger difference. One should always try to make the near plane as far as possible.

hmm. Setting my depth buffer percision to 32 bit doesnt seem to make a difference (unless im doing it wrong). Im just going to move my near clipping pane back to what it was and then im going to scale everything in the scene up 10 times. (1 unit = 1 meter ingame, now 10 units will be 1 meter). That will give the same effect as moving the near clipping pane from 1 meter to 0.1 meter. Its a very tedious solution at this point though but I guess its my only solid option.

Moving the clip plane and scaling the scene at the same time will do nothing for the precision, because your scene is still at the same relative distance from the near clip plane.

Im not quite sure I understand. Or we might not be talking about exactly the same thing. If I make the near clipping pane bigger (like it was) that should fix my precision. Why would using bigger models in my scene reduce my precision again? I might be misunderstanding something simple though. Maybe its a matter of just using the available bits in the depth buffer better?

The precision at a certian depth depends on the ratio between the near plane and the depth you draw at. If you scale the near plane and scale the models at the same time, you negate the effect since the ratio will be unchanged, and the ratio dictates the precision. If you want to improve the precision you need to scale the near plane without scaling your models. THAT will reduce the relative distance of your models to the near plane where the precision is greater.

Ok. thanks for the insight.

This topic is closed to new replies.

Advertisement