# Thread: Near and far planes with sizes from 1e-6 to 1e+6

1. ## Near and far planes with sizes from 1e-6 to 1e+6

Hi All,

Here is a quick question:

We are adjusting the near and far planes according to the model size and pulling near 1% of the model depth closer and pushing far the 1% farther as a safety factor.

This works pretty fine.

Only one issue occurs. With orthographic projection (with perspective one is working fine) and very tiny models (around 1e-6) we get a near | far like 99.99999 | 10.000001 and the model gets cut and flickers during rotation.

What are we doing wrong?

Thanks,

Alberto

2. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Keeping near and far clip-planes close to each other is good practice, but everything has its limits.

The coefficient Po[2][2] of the Ortho-projection matrix is equal to [-2/(far-near)] and it defines Z-value of the "projected" vertex. To be more precise, Z' (projected value of the Z-coordinate) depends on Z and W.

Z' = -2*Z/(far-near) - (far+near)*W/(far-near)

I think previous formula is enough descriptive to find out why it happens, especially if you are using floats.

3. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Hi Aleksandar,

I still can't figure out how to specify near and far planes for model sizes that go from 1e-9 to 1e+9 in orthographic projection. First of all: is it possible?

What about getting the glOrtho() source code and recode it with doubles?

Thanks,

Alberto

4. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Originally Posted by devdept
I still can't figure out how to specify near and far planes for model sizes that go from 1e-9 to 1e+9 in orthographic projection. First of all: is it possible?
Of course! But I suggest you to scale such tiny models so that hey have "normal" dimensions. All coordinates multiply with, for example, 1E6, and everything will be much easier to handle. Or even better, find a radius of the bounding sphere, and multiply all local coordinates of the model with its reciprocal value (or divide with it, but multiplication is faster than division), and you'll get a unit-size model.

Originally Posted by devdept
What about getting the glOrtho() source code and recode it with doubles?
Sooner or later you will need to implement your own Ortho-matrix, because all matrix manipulation functions are deprecated. I, myself, prefer to do all calculations with doubles, and before sending to GPU convert them to floats. Keep in mind that most of modern GPUs deals only with floats.

Another limit is a 24-bit precision of depth buffer. So, to avoid all possible problems with the precision, scale your models and keep doing the same way you did before...

5. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Aleksandar,

Of course you can scale the model, but we want to know how far we can go without scaling and know what are opengl (floats) limits. Do you know what is the small and big limits of OpenGL in Orthographic projection?

I can't remember where to find the glOrtho source code, can you point me to it?

Thanks again,

Alberto

6. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Originally Posted by devdept
I can't remember where to find the glOrtho source code, can you point me to it?
OpenGL Programming Guide, Appendix F. Scroll to the end. It just loads this matrix.

Alternatively, see the Mesa3D source code.

Keep in mind: 1e-6 ... 1e6 is ~12-13 decimal sigfigs. You only get ~6-7 from float. And that's only if you compute the number in doubles (with well conditioned functions) and only thunk down to float at the end. If you do your compute in float even with well conditioned functions the most you can expect is ~3-4 sigfigs.

7. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Thanks Dark Photon,

Do you see? Therefore an OpenGL limit exists. Even using doubles how far can you expect to go (as model size range):

From 1e-3 to 1e+3 or
From 1e-6 to 1e+6 or
From 1e-9 to 1e+9 or
From 1e-12 to 1e+12

I simply wanted to get an idea of this to know when we are close to the OpenGL platform limit or to our program implementation limit.

Thanks again,

Alberto

8. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Originally Posted by devdept
Do you see? Therefore an OpenGL limit exists.
Everything has its limits!
But what you have mentioned is not a limit of OpenGL but of floating-point arithmetics and graphical hardware constraints. Try to use DX and you will have the same problems.

9. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Aleksandar,

I wanted to say:

Do you see? Therefore an float limit exists. Even using doubles how far can you expect to go (as model size range):

From 1e-3 to 1e+3 or
From 1e-6 to 1e+6 or
From 1e-9 to 1e+9 or
From 1e-12 to 1e+12

Thanks,

Alberto

10. ## Re: Near and far planes with sizes from 1e-6 to 1e+6

Hi Dark Photon,

By trial and error I found the following formula but I am not yet satisfied:
Code :
```switch (projectionMode)
{
case Orthographic:
cameraMinZ -= 1e+3 * modelDepth;
cameraMaxZ += 1e+3 * modelDepth;
break;
}```
Where I can find a more mathematical approach in setting the near and far planes for Ortho projection that work for very small to very big models?

Thanks,

Alberto

Page 1 of 3 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•