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;
}
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)
floatcan 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
floatbetween 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 aforloop, casting to afloatvariable, 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: