1
$\begingroup$

I am trying to only change the near clipping plane of a given perspective projection matrix for OpenGL. My problem is, that the near clipping plane is way too close, and the far clipping plane is very far away. That causes depth buffer precision problems, which are visible (see image)

Visualization of the artefacts

The wrong parts are marked with red circles.

I would like to solve this issue by only changing the near clipping plane. Is there a fast and easy way to keep all parameter the same except the near clipping plane? I don't have any of the parameters. The only input I have is the ProjectionMatrix which should be changed

$\endgroup$
1
$\begingroup$

It is probably easier just to write your own projection matrix function, check out this derivation from the code guru website.

But you can also modify the near plane of an existing projection. There are easier methods of going about it (then the function below) but this one is easier to understand as it makes each step clear (or at least that is my intent):

It occurs to me that this function assumes a clip volume where zmin=0, zmax=1 and default opengl has zmin=-1 zmax=1.0

void RecreateProjection( Matrix4x4 &projection, float new_near_distance)
{
    // extract the current settings
    // NOTE: the array operator here assume matrix[column][row] indexing
    float fovy_rads = atan(1.0f / projection[1][1])) * 2.0f;
    float aspect =  projection[0][0] * (1.0f/projection[1][1]);
    aspect  = 1.0f / aspect;
    float  near = -projection[3][2] / projection[2][2];
    float k = projection[2][2]; 
    float far = -(near * k) / (1.0f - k );

    // Now that we have all the settings for the current projection
    // Just call your favorite projection creation function to get your modified projection
    projection = GenerateProjection( fovy_rads, aspect, new_near_distance, far);
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.