Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Far Plane given very large value clips everything

I found if I define the far plane distance for the perspective matrix to be 1,000,000,000 , then all the object inside that range get clipped. Range of 100,000,000 works fine. Anyone can explain this? I mean , it still doesn't approximates float number Max range. Or am I wrong on this ? For calculating perspective I use GLM library. No fixed pipeline stiff.

UPDATE: (JAVA) Perspective matrix calculation:

 public static Mat4 perspective(float fovy, float aspect, float zNear, float zFar) {
    float range = (float) (Math.tan(Math.toRadians(fovy / 2.0f)) * zNear);
    float left = -range * aspect;
    float right = range * aspect;
    float bottom = -range;
    float top = range;

    Mat4 res = new Mat4(0.0f);

    res.matrix[0] = (2.0f * zNear) / (right - left);
    res.matrix[5] = (2.0f * zNear) / (top - bottom);
    res.matrix[10] = -(zFar + zNear) / (zFar - zNear);
    res.matrix[11] = -1.0f;
    res.matrix[14] = -(2.0f * zFar * zNear) / (zFar - zNear);

    return res;
  }
like image 773
Michael IV Avatar asked Oct 29 '12 09:10

Michael IV


1 Answers

What you are seeing is a rounding problem due to the very finite precision of floating point numbers.

Although floating point numbers have huge (for most practical applications "infinite") range, they have a limited precision which is well below that of an integer of the same size. A single precision (32-bit) float can represent little over 7 decimal digits. You can have extremely small or large (smaller and larger than you can imagine) numbers, but they still only have 7.22 valid decimal digits.

The only numbers representable as a single precision float between 999,999,900 and 1,000,000,100 are: 999999872, 999999936, 1000000000, and 1000000064. You can easily verify this by counting an integer variable in a for loop, casting to a float variable, and printing it.

Which means that for example 999,999,950 and 999,999,951 and 999,999,999 are exactly the same number, so 999,999,950 may get clipped although it is "obviously" in front of the clipping plane.

EDIT:

Little demo program with output:

#include <stdio.h>

int main()
{
    float f = 0.0f;
    for(int i = 999999900; i < 1000000100; ++i)
    {
        f = i;
        printf("%d\t%f\n", i, f);
    }
    return 0;
}

999999900       999999872.000000
999999901       999999872.000000
999999902       999999872.000000
999999903       999999872.000000
999999904       999999872.000000
999999905       999999936.000000
999999906       999999936.000000
999999907       999999936.000000
...
[some lines omitted]
...
999999967       999999936.000000
999999968       1000000000.000000
999999969       1000000000.000000
999999970       1000000000.000000
999999971       1000000000.000000
999999972       1000000000.000000
...
[some lines omitted]
...
1000000028      1000000000.000000
1000000029      1000000000.000000
1000000030      1000000000.000000
1000000031      1000000000.000000
1000000032      1000000000.000000
1000000033      1000000064.000000
1000000034      1000000064.000000
1000000035      1000000064.000000
1000000036      1000000064.000000
1000000037      1000000064.000000
1000000038      1000000064.000000
1000000039      1000000064.000000
1000000040      1000000064.000000
1000000041      1000000064.000000
1000000042      1000000064.000000
1000000043      1000000064.000000
like image 166
Damon Avatar answered Nov 15 '22 08:11

Damon