<p>It is probably easier just to write your own projection matrix function, check out this derivation <a href="https://www.codeguru.com/cpp/misc/misc/graphics/article.php/c10123/Deriving-Projection-Matrices.htm#page-1" rel="nofollow noreferrer">from the code guru website</a>.</p>
<p>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):</p>
<p>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</p>
<pre><code>void RecreateProjection( Matrix4x4 &amp;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);
}
</code></pre>

CC BY-SA 4.0
2021-06-12T14:59:27.527Z
2021-06-12T15:20:06.570Z
2021-06-12T15:20:06.570Z
pmw1234
<p>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)</p>
<p><strong>Visualization of the artefacts</strong></p>
<p><a href="https://i.sstatic.net/Sq07C.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Sq07C.png" alt="" /></a></p>
<p>The wrong parts are marked with red circles.</p>
<p>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</p>

<opengl><projections>
how to change the near clipping plane of a perspective projection matrix for OpenGL
Thomas
# how to change the near clipping plane of a perspective projection matrix for OpenGL

(Asked by: username_0 on 2021-06-10)

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**

[![](https://i.sstatic.net/Sq07C.png)](https://i.sstatic.net/Sq07C.png)

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

**Tags:** opengl, projections


---

## 1 answers

### Answer by username_1 on 2021-06-12. Score: 2 | Accepted answer

It is probably easier just to write your own projection matrix function, check out this derivation [from the code guru website](https://www.codeguru.com/cpp/misc/misc/graphics/article.php/c10123/Deriving-Projection-Matrices.htm#page-1).

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);
}

```


---

