<p>Do you have any "input data" from which you constructed the matrix (like fov, near plane distance, etc.)? Or is the matrix itself <em>the only</em> data you have?</p>

<p>EDIT:
Ok, so if you want to know the size of the near plane while you have viewProj matrix available do this:</p>

<p><strong>a)</strong> compute viewProj^-1</p>

<p><strong>b)</strong> vertices of the near plane in clip-space have coordinates (in OpenGL):
(-1, -1, -1, 1)
(-1,  1, -1, 1)
( 1, -1, -1, 1)
( 1,  1, -1, 1)</p>

<p>and on the far plane:
(-1, -1, 1, 1)
(-1,  1, 1, 1)
( 1, -1, 1, 1)
( 1,  1, 1, 1)</p>

<p>It's a unit cube spanning (-1, -1, -1) and (1, 1, 1) coordinates with w=1</p>

<p><strong>c)</strong> take each of those vertices, mutiply by viewProj^-1 and finally divide all resulting components by the w-component. That gives you the frustum's vertices in world space. From this you can easily find world space planes constraining the view frustum or the bounding box or whatever you like.</p>

<p>I think it's not possible to extract left/right/bottom/top/near/far from the matrix directly. But since you can compute the world space coordinates of frustum's corners (as shown above) you can easily find left/right/bottom/top/near/far.</p>

CC BY-SA 3.0
2017-11-22T13:00:10.290Z
yes the only data I have is matrix itself. Only matrices (world, view, proj, projView) are cached inside camera.
recp
CC BY-SA 3.0
2017-11-22T13:52:55.327Z
but this  [Decompose the OpenGL projection matrix](http://lektiondestages.blogspot.com.tr/2013/11/decompose-opengl-projection-matrix.html) works for glFrustum, maybe some similar formula can work for glPerspective, no way? I will try your steps (thanks), it also seems interesting and matching to my goal (get and convert view frustum coordinates to another view space (light's point of view)
recp
CC BY-SA 3.0
2017-11-22T14:10:10.110Z
That formula also works for getting near and far from glPerspective, only left, right, top and bottom need to be calculated. Maybe m00 (f / aspect) and m11 (f) relation could help to get aspectRatio. Then we will have aspectRatio, near and far, then maybe we could get the remain values, I will give a try
recp
CC BY-SA 3.0
2017-11-22T14:19:40.970Z
Well, I think it does not matter whether the matrix was created with glFrustum or gluPerspective. If the formula from the link you provided works "for glFrustum" it should equally well work "for gluPerspective". In fact, gluPerspective is built on top of glFrustum. gluPerspective simply "remaps" fov and aspect on left/right/top/bottom and calls glFrustum.
maxest
CC BY-SA 3.0
2017-11-22T14:31:58.370Z
My confusion was that some items used to decompose always is zero in glPerspective, maybe this doesn't change the result. I will compare same view frustum for both
recp
CC BY-SA 3.0
2017-11-25T15:42:01.100Z
I tried to implement what you said to get frustum vertices in world space, is it correct? Here: https://gist.github.com/recp/8d9a3f65d3573bd2775b658ffab46212
recp
CC BY-SA 3.0
2017-11-22T12:50:26.863Z
2017-11-22T13:36:12.673Z
2017-11-22T13:36:12.673Z
maxest
<p>I want to decompose projection matrices into <strong>near</strong>, <strong>far</strong>, <strong>top</strong>, <strong>bottom</strong>, <strong>left</strong> and <strong>right</strong> values to create a bounding box for view frustum. I used the formula described in  this site <a href="http://lektiondestages.blogspot.com.tr/2013/11/decompose-opengl-projection-matrix.html" rel="nofollow noreferrer">Decompose the OpenGL projection matrix</a>. This formula works for perspective which is created with frustum function (<a href="https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml" rel="nofollow noreferrer">glFrustum</a>). But it does not work for perspective which is created via perspective func (<a href="http://lektiondestages.blogspot.com.tr/2013/11/decompose-opengl-projection-matrix.html" rel="nofollow noreferrer">gluPerspective</a>) </p>

<p>Frustum and perspective may create same perspective effect but generated matrices seems different. So it seems I can't use single formula to extract perspective parameters. </p>

<ol>
<li>Is there any proof of that decomposition formula, or does it always work? </li>
<li>How do I decompose the matrix generated by perspective function into <strong>near</strong>, <strong>far</strong>, <strong>top</strong>, <strong>bottom</strong>, <strong>left</strong> and <strong>right</strong> values? Is there any formula which is already used in industry?</li>
</ol>

<p>My implementations of ortho, frustum and perspetive is here: <a href="https://github.com/recp/cglm/blob/master/include/cglm/cam.h" rel="nofollow noreferrer">https://github.com/recp/cglm/blob/master/include/cglm/cam.h</a></p>

<matrix><projections><shadow-mapping>
How to decompose projection matrix?
recp
# How to decompose projection matrix?

(Asked by: username_0 on 2017-11-22)

I want to decompose projection matrices into **near**, **far**, **top**, **bottom**, **left** and **right** values to create a bounding box for view frustum. I used the formula described in this site [Decompose the OpenGL projection matrix](http://lektiondestages.blogspot.com.tr/2013/11/decompose-opengl-projection-matrix.html). This formula works for perspective which is created with frustum function ([glFrustum](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml)). But it does not work for perspective which is created via perspective func ([gluPerspective](http://lektiondestages.blogspot.com.tr/2013/11/decompose-opengl-projection-matrix.html))

Frustum and perspective may create same perspective effect but generated matrices seems different. So it seems I can't use single formula to extract perspective parameters.

1. Is there any proof of that decomposition formula, or does it always work?
2. How do I decompose the matrix generated by perspective function into **near**, **far**, **top**, **bottom**, **left** and **right** values? Is there any formula which is already used in industry?

My implementations of ortho, frustum and perspetive is here: <https://github.com/username_0/cglm/blob/master/include/cglm/cam.h>

**Tags:** matrix, projections, shadow-mapping


---

## 1 answers

### Answer by username_1 on 2017-11-22. Score: 0 | Accepted answer

Do you have any "input data" from which you constructed the matrix (like fov, near plane distance, etc.)? Or is the matrix itself *the only* data you have?

EDIT:
Ok, so if you want to know the size of the near plane while you have viewProj matrix available do this:

**a)** compute viewProj^-1

**b)** vertices of the near plane in clip-space have coordinates (in OpenGL):
(-1, -1, -1, 1)
(-1, 1, -1, 1)
( 1, -1, -1, 1)
( 1, 1, -1, 1)

and on the far plane:
(-1, -1, 1, 1)
(-1, 1, 1, 1)
( 1, -1, 1, 1)
( 1, 1, 1, 1)

It's a unit cube spanning (-1, -1, -1) and (1, 1, 1) coordinates with w=1

**c)** take each of those vertices, mutiply by viewProj^-1 and finally divide all resulting components by the w-component. That gives you the frustum's vertices in world space. From this you can easily find world space planes constraining the view frustum or the bounding box or whatever you like.

I think it's not possible to extract left/right/bottom/top/near/far from the matrix directly. But since you can compute the world space coordinates of frustum's corners (as shown above) you can easily find left/right/bottom/top/near/far.

> **Comments:**
> **username_0**: yes the only data I have is matrix itself. Only matrices (world, view, proj, projView) are cached inside camera.  
> **username_0**: but this  [Decompose the OpenGL projection matrix](http://lektiondestages.blogspot.com.tr/2013/11/decompose-opengl-projection-matrix.html) works for glFrustum, maybe some similar formula can work for glPerspective, no way? I will try your steps (thanks), it also seems interesting and matching to my goal (get and convert view frustum coordinates to another view space (light's point of view)  
> **username_0**: That formula also works for getting near and far from glPerspective, only left, right, top and bottom need to be calculated. Maybe m00 (f / aspect) and m11 (f) relation could help to get aspectRatio. Then we will have aspectRatio, near and far, then maybe we could get the remain values, I will give a try  
> **username_1**: Well, I think it does not matter whether the matrix was created with glFrustum or gluPerspective. If the formula from the link you provided works "for glFrustum" it should equally well work "for gluPerspective". In fact, gluPerspective is built on top of glFrustum. gluPerspective simply "remaps" fov and aspect on left/right/top/bottom and calls glFrustum.  
> **username_0**: My confusion was that some items used to decompose always is zero in glPerspective, maybe this doesn't change the result. I will compare same view frustum for both  
> **username_0**: I tried to implement what you said to get frustum vertices in world space, is it correct? Here: https://gist.github.com/username_0/8d9a3f65d3573bd2775b658ffab46212  

---

