# directx matrices

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

in my app i need to set the projection matrix myself (meaning i CAN'T use D3DXMatrixPerspectiveFovLH) i went to the documentation that comes with directx and read about D3DXMatrixPerspectiveFovLH and this is what they say:
xScale     0          0               0
0        yScale       0               0
0          0       zf/(zf-zn)         1
0          0       -zn*zf/(zf-zn)     0
where:
yScale = cot(fovY/2)

xScale = aspect ratio / yScale

when i use it it seems to be completely wrong! i can only make it work this way: Change #1: i do xScale = yScale / aspect ratio (and not the other way as they say) Change #2: i negate xScale. that's the only way i get visual output, but i get it ofcourse mirrored on the x axis ... can anyone here please help me on this ?

##### Share on other sites
cot(x) = 1 / tan(x). Do you do it that way ?
I ask because the first time I read this I thought cot(x) was a typo and that it was cos(x).

I also found this one :

h = cos(fov/2) / sin(fov/2);
w = h / aspect;

2*zn/w 0 0 0
0 2*zn/h 0 0
0 0 zf/(zf-zn) 1
0 0 zn*zf/(zn-zf) 0

##### Share on other sites
like you, for cot i used cos/sin.

i'll try the projection matrix you offered, i hope it will work ;)

thanks :)

##### Share on other sites
The matrix you got from D3DXMatrixPerspectiveFovLH looks correct. Make sure you are doing the multiplying correctly. It seems that for some reason your code works when you multiply by the inverse (or something like that) of the projection matrix.

BTW, why can't you use D3DXMatrixPerspectiveFovLH?

##### Share on other sites
The reason i need to use the directx matrix is that my engine supports both directx and opengl, being that way the engine is an abstraction above both of those graphcis apis, so i want both outputs to be perfectly identical so i want to completely control what happens inside all the projection/modelview etc...

##### Share on other sites
Are you aware that GL and D3D utilize different matrix representations, and if so are you correctly compensating?

##### Share on other sites
The projection matrices for OpenGL and DirectX are different because the locations of the clip space planes are different.

##### Share on other sites
Quote:
 Original post by RavyneAre you aware that GL and D3D utilize different matrix representations, and if so are you correctly compensating?

i'm aware that it's left handed verses right handed, and i negate the z value of coordniates, but nothing more.

am i missing something ?

##### Share on other sites
Yes, AFAIK OpenGL matrices are in column-major form, where the translation components tx, ty, tz occupy elements 13, 14 and 15 of the matrix (with respect to a 16 element, single-dimentional array containing the matrix).

DirectX, on the other hand, uses row-major form, where the translation components tx, ty, tz occupy elements 4, 8 and 12 of the matrix. D3D matrices are the transpose of equivilant GL matrices.

| 01 02 03 04 |
| 05 06 07 08 |
| 09 10 11 12 |
| 13 14 15 16 |

##### Share on other sites
Also note that the screen-space z-axis bounds are different between OpenGL and DirectX (as indicated by JohnBolton)

In DirectX the near-plane is z = 0 and the far-plane is z = 1
In OpenGL the near-plane is z = -1 and the far-plane is z = 1

I can't remember the exact changes you have to make to the projection matrix to account for this but they're fairly simple (a sign change and a multiplication if I remember correctly).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634782
• Total Posts
3019266
×